Skip to main content
GameDev.net gamedev.net
🔒 Locked

Question Regarding SDL

Started by Shaneoneal Sep 17, 2005 at 7:37 PM 3 replies 1.4k views
Original Post
Shaneoneal
Shaneoneal
Hello everyone, I am having a problem. The code below compiles, but when I run it, the window flashes quickly and then disappears. To be quite honest, I am very new to the whole game loop thing as I have never attempted game programming before. There is probably a simple solution, and I am sorry if this is a stupid question. Maybe I am tired and just can't see the problem. I am new here too, so Hello! (BTW, I know the way the paddle is moving is pointless, but i believe it should still work, I was just messing around.) /********************************************************************** * Name: A Program Version 1.0 * * Author: Shane O'Neal * * SDK: SDL * **********************************************************************/ #include #include "SDL.h" #include "SDL_image.h" using namespace std; // Defines for screen height, width and depth #define SCREEN_WIDTH 500 #define SCREEN_HEIGHT 400 #define SCREEN_DEPTH 16 int main(int args, char **argv) { // Set up the variables SDL_Event event; SDL_Surface *screen; SDL_Surface *ball; SDL_Surface *paddle; SDL_Surface *background; int paddle_movey, paddle_movex; // Initialize SDL SDL_Init(SDL_INIT_EVERYTHING); // Set up "screen" as the screen screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_DOUBLEBUF); background = IMG_Load("sunset.jpg"); // Blit the background surface SDL_Rect bg_dest; bg_dest.w = SCREEN_WIDTH; bg_dest.h = SCREEN_HEIGHT; bg_dest.x = 0; bg_dest.y = 0; SDL_BlitSurface(background, NULL, screen, &bg_dest); // Set up the paddle surface and rect paddle = IMG_Load("paddle.bmp"); SDL_Rect paddle_dest, paddle_src; paddle_dest.w = paddle->w; paddle_dest.h = paddle->h; paddle_dest.x = 200; paddle_dest.y = 350; paddle_src = paddle_dest; // The Main Message Loop bool running = true; while (running) { SDL_PollEvent(&event); switch (event.type) { case SDL_QUIT: running = false; } SDL_FillRect(screen, NULL, 0); SDL_BlitSurface(paddle, &paddle_src, screen, &paddle_dest); paddle_dest.x += 1; if (paddle_dest.x == 0 || paddle_dest.x == SCREEN_WIDTH - paddle_dest.w) paddle_dest.x -= 1; SDL_UpdateRect(screen, 0, 0 ,0 ,0); SDL_Flip(screen); } return 0; }
Drew_Benton
Drew_Benton
When posting code, you can use the [source] code goes here [/source] tags to make your code formatted and appear in a nice white box. You can click on the edit button on the top right of your original post and add those tags in.

Now onto your code.

  • You will either use SDL_Flip or SDL_UpdateRect, but not both. If you are using double buffering, then you will want to just use SDL_Flip.
  • When you poll for events, you will want to poll for all events and process per a frame in your program. I.E.
    while( running ){   while( SDL_PollEvent(&event) )   {      switch(event.type)      {         ...      }   }   ... Other draw/update code}

  • Whenever you load an image with SDL_Image, or SDL, you will always want to check the result to see if it was actually loaded. I.E.
    background = IMG_Load("sunset.jpg");if( !background ){   // Handle error here, display a message   exit(0);}


    That should get you started, so start making those changes. I think your program quits so fast becuase the background surface is failing to load, so add that code in first to see if the image is being loaded or not.
  • Lazy Foo
    Lazy Foo
    I think the problem is that the image files aren't loading.

    I have some SDL tutorials that I shamelessly spam. I cover getting a basic image to show on the screen in tutorial 2, and I cover how a basic game loop and event handling in tutorial 4. I think they might be useful to you.

    Lazy Foo's SDL tutorials

    [Edited by - Lazy Foo on August 9, 2007 7:22:24 PM]
    Learn to make games with my SDL 2 Tutorials
    deavik
    deavik
    Check in the program directory if there is a stderr.txt file. And I, Seer extraordinaire, am going to make a prediction: the stderr.txt file says (something like):
    Fatal Segmentation Fault (SDL Parachute deployed)
    Seriously, it is most probably the image loading that is failing. Try some debugging by commenting out code and seeing if you can get it to run.
    Chad Smith
    Chad Smith
    Ok I have looked at your code and it is the same way I write mine, except just a little different.

    My code runs fine, so that belives me to think that the image is not loading properly. Check to see if it is.


    Also since it is loading an image as the background, then does it actually come up and you see the image as the background then it disapear, or is it that it does it black real fast. If it does it black real fast, then yeah the image isn't loading properly. If it is showing the image, then something else is wrong. What is it?



    Oh yeah, a little advice. Why don't you use Functions to load and display images? It will save you time. You wont have to write down the code alot. Just the function. Just a little advice. That is what I do. It makes it alot easier to read my code and of course to understand it.



    Chad.

    Topic Locked

    This topic has been locked by a moderator. New replies are not allowed.

    Sign in to reply to this topic.