Sign in to follow this  

Problem with SDL

This topic is 4382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I'm a complete noob to SDL and I started my first tut on it(http://cone3d.gamedev.net/cgi-bin/index.pl?page=tutorials/gfxsdl/tut1) I typed in this code: #include <iostream> #include <cstdlib> #include <SDL/SDL.h> using namespace std; void DrawPixel(SDL_Surface * screen, int x, int y, Uint8 R, Uint8 G, Uint8 B); void Slock(SDL_Surface * screen); void Sulock(SDL_Surface * screen); int main(int argc, char *argv[]) { if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO)<0) //init SDL { cout << "Unable to init SDL: " << SDL_GetError() << endl; //check for errors on SDL_INIT return(1); } atexit(SDL_Quit); //telling SDL to clean up at end of program SDL_Surface *screen; screen=SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF); //setting video mode if(screen=NULL) //check for errors while setting video mode { cout << "Unable to set 640x480 video: " << SDL_GetError() << endl; return(1); } Slock(screen); for(int x=0; x<640; x++) { for(int y=0; y<480; y++) { DrawPixel(screen, x, y, y/2, y/2, x/3); } } Sulock(screen); SDL_Flip(screen); return 0; } void DrawPixel(SDL_Surface * screen, int x, int y, Uint8 R, Uint8 G, Uint8 B) { Uint32 color = SDL_MapRGB(screen->format, R, G, B); switch (screen->format->BytesPerPixel) { case 1: // Assuming 8-bpp { Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; *bufp = color; } break; case 2: // Probably 15-bpp or 16-bpp { Uint16 *bufp; bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; *bufp = color; } break; case 3: // Slow 24-bpp mode, usually not used { Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3; if(SDL_BYTEORDER == SDL_LIL_ENDIAN) { bufp[0] = color; bufp[1] = color >> 8; bufp[2] = color >> 16; } else { bufp[2] = color; bufp[1] = color >> 8; bufp[0] = color >> 16; } } break; case 4: // Probably 32-bpp { Uint32 *bufp; bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; *bufp = color; } break; } } void Slock(SDL_Surface *screen) { if ( SDL_MUSTLOCK(screen) ) { if ( SDL_LockSurface(screen) < 0 ) { return; } } } void Sulock(SDL_Surface *screen) { if ( SDL_MUSTLOCK(screen) ) { SDL_UnlockSurface(screen); } } and it gives me this error in stderr.txt: Fatal signal: Segmentation Fault (SDL Parachute Deployed) any idea whats up? Thanx for the help.

Share this post


Link to post
Share on other sites
Using = instead of == when you want to compare 2 values is a common mistake among people starting to program.

One small piece of advice, if you're using constant values - NULL in this case - consider writing expressions like this

if (NULL == screen)

// the code won't compile if you accidently do this
if (NULL = screen)

// but as you've learned the next line will compile and be a subtle bug
if (screen = NULL)

Share this post


Link to post
Share on other sites

This topic is 4382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this