Archived

This topic is now archived and is closed to further replies.

Unidentified

C++ Language Error? Pixel Drawing - SDL

Recommended Posts

First I copied the code from a tutorial and converted it to C++, it didn't work. So i copied and pasted it and it worked. I re wrote it in C++ again today and it doesn't work. I checked the tutorial and it seems fine. The error is a runtime error. I'm thinking from the main function since all I had to do before was copy the tutorials main code. I don't know what the deal is. Does it have to do with C++ or not? Do I have to do something different when I'm programming in C++ with SDL than C? Here's the code if it doesn't have to do with main.cpp
#include <iostream>
#include <SDL/SDL.h>
#include "Draw.h"

using namespace std;

int main(int argc, char* argv[])
{
	if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0)
	{
		cerr << "Error initailizing SDL: " << SDL_GetError();
		return -1;
	}
	
	SDL_Surface* screen;
	screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE |
		SDL_DOUBLEBUF);
	
	if(screen = NULL)
	{
		cerr << "Could not set video mode, 640 x 480, 32 bbps: "
		     << SDL_GetError();
		SDL_Quit();
		return -1;
	}
	
	SDL_Event event;
	bool done = false;
	
	while(done == false)
	{
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
				done = true;
			else if(event.type == SDL_KEYDOWN)
				if(event.key.keysym.sym == SDLK_ESCAPE)
					done = true;
		}
		LockSurface(screen);
		DrawScene(screen);
		UnlockSurface(screen);
	}
	
	SDL_Quit();
	return 0;
}



Draw.h
   
/////////////////////////////////////////////////////

//Draw.h - Drawing Related Functions

/////////////////////////////////////////////////////

#ifndef   _GAURD_DRAW_H_
#define	  _GAURD_DRAW_H_

#include <iostream>
#include <SDL/SDL.h>

//-------------------------------------------

//Surface Locking/Unlocking Functions

//-------------------------------------------

void LockSurface(SDL_Surface* screen);
void UnlockSurface(SDL_Surface* screen);

//-----------------------------------------------

//Pixel Drawing Functions

//-----------------------------------------------

void DrawPixel(SDL_Surface* screen, int x, int y,
	Uint8 R, Uint8 G, Uint8 B);
void DrawScene(SDL_Surface* screen);		

#endif



Draw.cpp
   
/////////////////////////////////////////////////////

//Draw.cpp - Drawing Related Functions

/////////////////////////////////////////////////////

#include "Draw.h"

//-------------------------------------------

//Surface Locking/Unlocking Functions

//-------------------------------------------


void LockSurface(SDL_Surface* screen)
{
	if(SDL_MUSTLOCK(screen))
		if(SDL_LockSurface(screen) < 0)
		{
			std::cerr << "Error Locking Screen: " 
					  << SDL_GetError();
			return;
		}
}

void UnlockSurface(SDL_Surface* screen)
{
	if(SDL_MUSTLOCK(screen))
		SDL_UnlockSurface(screen);
}

//-----------------------------------------------

//Pixel Drawing Functions

//-----------------------------------------------

void DrawPixel(SDL_Surface* screen, int x, int y,
	Uint8 R, Uint8 G, Uint8 B)
{
	Uint32 color = SDL_MapRGB(screen->format, R, G, B);
	Uint8 bpp = screen->format->BytesPerPixel;
	Uint32* pxp = (Uint32*)screen->pixels + y * screen->pitch
	             + x * bpp;
	
	*pxp = color;
}

void DrawScene(SDL_Surface* screen)
{
	for(int x = 0; x < 640; ++x)
		for(int y = 0; y < 480; ++y)
			DrawPixel(screen, x, y, x, y, x + y % 256);
}
I'm using VC++ .Net 2002 Standard Edition to complie it. PS: I also have a question about the adress of the pixel. Does the (SDL_Surface*)->pitch * y give the pixel down and x * BitsPerPixel give the pixel right. And does that mean (SDL_Surface*)->pixels) give the adress of the top left pixel. I just read info about what the pitch is(scanlines I think) so I'm not use if it's correct. I got that equation from a the SDL site and it's different than from the tutorial. So not sure if it's the best way to do it but I understand it better. [edited by - Unidentified on January 4, 2004 5:31:13 PM]

Share this post


Link to post
Share on other sites
quote:

PS: I also have a question about the adress of the pixel. Does the (SDL_Surface*)->pitch * y give the pixel down and x * BitsPerPixel give the pixel right. And does that mean (SDL_Surface*)->pixels) give the adress of the top left pixel.


Yup.

Share this post


Link to post
Share on other sites
The error is Unhandled exception at 0x00411376 in SDL_1.exe: 0xC0000005: Access violation reading location 0x00000018. Which is located in the line containing SDL_MUSTLOCK(screen) in the LockSurface() function, according to the debugger. I replace it with the tutorial's code and it's the exact same error.

quote:
Original post by stro
quote:

PS: I also have a question about the adress of the pixel. Does the (SDL_Surface*)->pitch * y give the pixel down and x * BitsPerPixel give the pixel right. And does that mean (SDL_Surface*)->pixels) give the adress of the top left pixel.


Yup.


Ok thanks, I wasn't sure since there were different forms of the equation.

[edited by - Unidentified on January 4, 2004 5:34:46 PM]

[edited by - Unidentified on January 4, 2004 5:40:10 PM]

Share this post


Link to post
Share on other sites
Just comment out the "if(SDL_MUSTLOCK(screen))" lines in the two functions.

They should work perfectly without it. Try it out and tell us what happens.

Share this post


Link to post
Share on other sites
Now the error is Unhandled exception at 0x100210f5 in SDL_1.exe: 0xC0000005: Access violation reading location 0x0000002c. Which is at the call to the LockSurface function. I can''t figure out why.

Share this post


Link to post
Share on other sites
Thanks. I didn''t notice that. That isn''t the first time I accidentaly put = instead ==. But there wasn''t a error when I took out the pixel drawing function and the surface lock and unlocking functions were there.

Share this post


Link to post
Share on other sites
There another error, when in the line with *pxp = color. I guess there is something wrong with either the equation or the value I convert it to or possibly the color.

Share this post


Link to post
Share on other sites
Uint32* pxp = (Uint32*)screen->pixels + y * screen->pitch + x * bpp;

should be

Uint32* pxp = (Uint32*)screen->pixels + y * screen->pitch + x;

Share this post


Link to post
Share on other sites
I had to put

bufp = (Uint32 *)screen->pixels + y * screen->pitch/4 + x;

Which is the one from the Cone3D tutoiral. I don't get why you have to dived screen->pitch by the number of bytes per pixel The one from the SDL Doc Project didn't work which was the one I was using, it multiplied the bytes per pixel by x.



[edited by - Unidentified on January 4, 2004 6:16:57 PM]

Share this post


Link to post
Share on other sites
Screen->pixels points to 0,0.
To get to the right Y coordinate you mutliply y * the number of pixels per line. screen->pitch was the number of BYTES per line. Since you were using 32 bit graphics that meant divide by 4 to get the number of pixels. Then you simply add on the x and you''re there.

Does that answer your question?

Share this post


Link to post
Share on other sites
quote:
Original post by stro
Screen->pixels points to 0,0.
To get to the right Y coordinate you mutliply y * the number of pixels per line. screen->pitch was the number of BYTES per line. Since you were using 32 bit graphics that meant divide by 4 to get the number of pixels. Then you simply add on the x and you''re there.

Does that answer your question?



Ok. I understand it now.

Share this post


Link to post
Share on other sites