Thread: SDL C Problem

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    SDL C Problem

    I've just recently started fooling with SDL, yet I'm having troubles.

    Some Info:
    - compiling under mingw32
    - using C::B and DevC++ as my IDE of choice
    - 32-bit Vista, newest service pack updated
    - nVidia GT 130M Graphic Card, newest drivers updated

    First of all, whenever i try to compile my code under, DevCPP I'm getting a BSoD, however C::B doesnt seem to have this problem. Anyone know or heard what might be the issue?

    Second, this is a basic code that is suppose to blink 3 times an image on the screen, when i compile and run the code however, i just get black screen. Ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "SDL/SDL.h"
    
    #define TRUE 0
    #define FALSE 1
    #define SCREEN_WIDTH 640
    #define SCREEN_HEIGHT 480
    #define SCREEN_BPP 32
    
    SDL_Surface *image = NULL;
    SDL_Surface *screen = NULL;
    
    signed int initEngine();
    void quitEngine();
    void updateScreen();
    SDL_Surface* loadImage(char* filename);
    void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination);
    
    int main (int argc, char* args[])
    {
        initEngine();
        image = loadImage("1.bmp");
        applySurface(40, 40, image, screen);
            applySurface(100, 100, image, screen);
                applySurface(200, 200, image, screen);
        updateScreen();
        SDL_Delay(2000);
        quitEngine();
    
        return TRUE;
    }
    
    signed int initEngine()
    {
        if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
        {
            return FALSE;
        }
    
        screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    
        if(screen == NULL)
        {
            return FALSE;
        }
        SDL_WM_SetCaption("Engine", NULL);
        return TRUE;
    }
    
    void quitEngine()
    {
        SDL_FreeSurface(image);
        SDL_FreeSurface(screen);
        SDL_Quit();
    }
    
    void updateScreen()
    {
        SDL_Flip(screen);
    }
    
    SDL_Surface* loadImage(char* filename)
    {
        SDL_Surface* loadedImage = NULL;
        SDL_Surface* optimizedImage = NULL;
    
        loadedImage = SDL_LoadBMP(&filename);
    
            if(loadedImage != NULL)
            {
                optimizedImage = SDL_DisplayFormat(loadedImage);
                SDL_FreeSurface(loadedImage);
            }
        return optimizedImage;
    }
    
    void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
    {
        SDL_Rect offset;
    
        offset.x = x;
        offset.y = y;
        SDL_BlitSurface(source, NULL, destination, &offset);
    }
    Cheers,
    Last edited by newBorn; 03-17-2012 at 06:04 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by newBorn View Post
    I've just recently started fooling with SDL, yet I'm having troubles.

    Some Info:
    - compiling under mingw32
    - using C::B and DevC++ as my IDE of choice
    - 32-bit Vista, newest service pack updated
    - nVidia GT 130M Graphic Card, newest drivers updated

    First of all, whenever i try to compile my code under, DevCPP I'm getting a BSoD, however C::B doesnt seem to have this problem. Anyone know or heard what might be the issue?
    Perhaps because Dev-C++ is outdated and unmaintained? Also, Vista is not Microsoft's best work, it may not play nicely with Dev-C++. Just ditch it, and stick with C::B and MinGW.

    Second, this is a basic code that is suppose to blink 3 times an image on the screen, when i compile and run the code however, i just get black screen. Ideas?
    This I can't really help you with, I've never used SDL. Somebody else here may know, or you may want to ask on an SDL specific forum.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    You don't check the return of initEngine, so you may not even have SDL loaded or available at all - that's before you even BEGIN to draw to the screen using it. However, you blithely ignore that fact and carry on anyway.

    Similarly for loadImage - the image may not even exist or be where the program expects it, or completely corrupt, but it carries along merrily anyway, trying to blit it to the screen.

    You should also be checking return values in applySurface. (EDIT: I take back what I said about dstrect in an earlier revision of this post, though, because I was working on something else very similar at the time and it doesn't apply for your code).

    That's honestly just from a 20-second skim to see what the program does. I suspect there may be other problems too.

    P.S. I use a 130M too, on XP though, and I prefer Eclipse for my C-and-SDL projects. Specifically, I use Wascana, which bundles a pre-configured version of MinGW into an Eclipse install. You'll probably want to update both the version of MinGW in that environment, AND the version of Eclipse too after installation (I use Helios, dunno if the newer versions work with Wascana). That said, if something BSoD's on you, ever, you should really find an alternative immediately. It's not suggestive of good code and you don't want to spend your life wondering if your program's crashed or your OS or your IDE.
    Last edited by ledow; 03-19-2012 at 05:54 AM.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Apart from the other observations it may be that your code is actually ' working ' You call applySurface() three times, and then updateScreen() once, followed by your SDL pause. It is unsurprising in this case that you are going to see a black screen, as it has completed before you can see it.

    Try just displaying the image once, the pause is not neccesary unless you put your code in a loop. Like with a counter and switch statement.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    It is clear that you have been following the Lazy Foo tutorials, they are good If you are willing to do more than copy paste and hope, He has in my opinion put in a huge amount of work to publish useful tuts, and continues to try and bug hunt and improve them, certainly they helped me a lot, I would recommend you start from the easiest examples, get a grasp of what is going on then work from there.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Why are you using a signed int to indicate a boolean value?

    You should be #include-ing <stdbool.h> and using an actual bool type. Along with the already-defined true and false macros that come with the header.

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    is there a bool type in C ?

    EDIT ahh, behind on the standards i am it seems
    Last edited by rogster001; 03-19-2012 at 12:51 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  8. #8
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by rogster001 View Post
    Apart from the other observations it may be that your code is actually ' working ' You call applySurface() three times, and then updateScreen() once, followed by your SDL pause. It is unsurprising in this case that you are going to see a black screen, as it has completed before you can see it.
    This isn't entirely true. He blits three surfaces to different locations on the screen. Then he calls SDL_Flip (which actually just calls SDL_UpdateRect because he hasn't requested SDL_DOUBLEBUF surfaces). This will update the screen, and then the application will pause for 2 seconds and then quit. Although you might not see anything because of other problems, it's not necessary to call SDL_Flip every 60th of a second or whatever - it will just update the screen when called and not otherwise. If you dragged a window over the SDL window, it won't update the image, but so long as you don't do that, it should display the three images for 2 seconds before quitting.

    The fact that it doesn't display ANYTHING is probably down to failing to check the returns - I bet the image isn't in the program directory or wherever the program is looking for it, for example, but in some subdirectory or in the source directory instead of where the final binary is run from. Even on the fastest of machines you should have at least seen the flicker of any SDL_Flip with blits pending.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by rogster001 View Post
    is there a bool type in C ?

    EDIT ahh, behind on the standards i am it seems
    Indeed there is the obscure bool in stdbool but nobody uses it probably because most of the standard library has no use for it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    it should display the three images for 2 seconds before quitting.
    after looking again i see this is true , but if the image was not present does SDL not output an error anyway? Regardless of return codes? I can't remember, its a long time since i used it.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  11. #11
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by rogster001 View Post
    after looking again i see this is true , but if the image was not present does SDL not output an error anyway? Regardless of return codes? I can't remember, its a long time since i used it.
    No. Otherwise what would be the point of error codes.

    SDL has SDL_GetError(). Programs are expected to catch poor return codes and print the result of SDL_GetError() to the user. Not all of them do and anything that isn't console-based (i.e. almost all SDL apps) won't see them anyway.

    Personally, I check for returns and on failure jump to an error function that printf()'s SDL_GetError and also MessageBox's it (on Windows) if it's critical. At least then the user can do some diagnosis on what happened rather than being rudely thrown out of the program or experiencing symptoms like this.

    Hell, most SDL programs I know even redirect stdout/stderr to files so that users aren't bothered by consoles popping up and the programmer can use them to aid diagnosis in the case of a program.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM