Jump to content
  • Advertisement
Sign in to follow this  
TheNobleOne

SDL Error: Can't figure out what is wrong.

This topic is 5393 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

Ok this has got me puzzled. I am not quite sure what is going on here. I am trying out the first tutorial on cone3d's tutorials and I am getting this error. "Fatal signal: Segmentation Fault (SDL Parachute Deployed)" here is the code if you need it. Just so you know I sat there and compaired code but I can't find a difference in it. So I am puzzled.
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>

using namespace std;

// The DrawPixel function *don't need to fully understand*
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 + 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 + x;
            *bufp = color;
        }
        break;
    }
}                        

void Slock(SDL_Surface *screen) // lock the program screen surface
{
    if(SDL_MUSTLOCK(screen))
    {
        if(SDL_LockSurface(screen) < 0)
        {
            return;
        }
    }
}

void Sulock(SDL_Surface *screen) // unlock the program screen surface
{
    if(SDL_MUSTLOCK(screen))
    {
        SDL_UnlockSurface(screen);
    }
}

void DrawScene(SDL_Surface *screen)
{
    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);
}
    
int main(int argc, char *argv[])
{
    // Initialize SDL
    if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0)
    {
        printf("Unable to init SDL: %s\n", SDL_GetError());
        return 1;
    }
    
    // execute on program exit
    atexit(SDL_Quit);
    
    // define the screen
    SDL_Surface *screen;
    //set up the video mode
    screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    
    // Check to see if screen was set up
    if(screen == NULL)
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }
    
    // Drawing goes here 
    int done = 0;
    
    // game loop
    while(done == 0)
    {
        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
            {
                done = 1;
            }
            if(event.type == SDL_KEYDOWN)
            {
                if(event.key.keysym.sym == SDLK_ESCAPE)
                {
                    done == 1;
                }   
            }
        }
        
        DrawScene(screen); 
    }                                 
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Still getting the same error. But I failed to say before that this is not a compile time error. The window flashes fast and the error is outputed in a file called stderr.txt as I am using the Dev C++ ide.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
what don't you use debugger and step throug the code

Share this post


Link to post
Share on other sites
first error: in the line

if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
{
done == 1;
}
}



is done = 1

second error: in the line

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



is y*screen->pitch/4

i just modify the 32 bpp case, because you set your video mode at 640x480x32, i didn't look the other cases.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You left out 2 divisions in your DrawPixel routine (16 and 32 bpp code). That's all I found at a first glance, and I wouldn't be surprised if it was the cause of your problem, but of course I could be wrong.

Hope this helps.

Share this post


Link to post
Share on other sites
Thanks alot I can't see how I missed those division signs after all the times I looked at the code and compaired. As for the done == 1; I am not sure how that got by the compiler lol that is a syntax error for the particular context of the statement. Darn typos are the hardest to find especially when the debugger does not work like it should lol. Could not get it to stop at a breakpoint.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!