Thread: Need help with this one line of code in SDL program

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    67

    Need help with this one line of code in SDL program

    Here's the code:

    Code:
     
    
    #include <SDL/SDL.h>
    SDL_Surface *screen = NULL;
    SDL_Surface *background = NULL;
    bool gameRunning = true;
    SDL_Event event;
    void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
    {
        SDL_Rect rect;
        rect.x = x;
        rect.y = y;
        SDL_BlitSurface(source,NULL,destination,&rect);
    }
    int main(int argc, char* argv[])
    {
    
    //initialize
    SDL_Init(SDL_INIT_VIDEO);
    //load
    screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    background = SDL_LoadBMP("background.bmp");
    
    //game loop
    while(gameRunning)
    if(SDL_PollEvent(&event))
    {
        if(event.type == SDL_Quit)
        {
            gameRunning = false;
        }
    }
    //apply surface
    apply_surface(0,0,background,screen);
    //update screen
    SDL_Flip(screen);
    SDL_Quit();
    return 0;
    }
    This is a simple SDL program designed to show a selected bmp file when running. When I try to compile this program in Code::Blocks, I recieve one error which occurs at line 34 ( ON THIS POST IT WILL SHOW LINE 22), stating "error: ISO C++ forbids comparision between pointer and integer." And it's the only error.

    Well, at line 34 is the code for the image to contionously loop until exit.
    Code:
    //game loop
    while(gameRunning)
    if(SDL_PollEvent(&event))
    {
        if(event.type == SDL_Quit)
        {
            gameRunning = false;
        }
    Any help would be GREATLY appreciated!

    P.S. Even though it didn't compile, I went to the C++ files for code::blocks, found my program, and double clicked on its exe. file and it opened up for a split second, showing the bmp file I selected, and exited. So I know for sure everything works, but it's just the loop that is failing.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    [QUOTE=cplusplusnoob;1095498]
    Code:
    //game loop
    while(gameRunning)
    if(SDL_PollEvent(&event))
    {
        if(event.type == SDL_Quit)
        {
            gameRunning = false;
        }
    I was able to fix this by changing "SDL_Quit" to "SDL_QUIT". So now it compiles with no errors, but when it compiles it still only appears for a split second and dissapears. Why isn't the loop working, making the box stay up??

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by cplusplusnoob View Post
    Here's the code:
    Code:
     
    
    #include <SDL/SDL.h>
    SDL_Surface *screen = NULL;
    SDL_Surface *background = NULL;
    bool gameRunning = true;
    SDL_Event event;
    void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
    {
        SDL_Rect rect;
        rect.x = x;
        rect.y = y;
        SDL_BlitSurface(source,NULL,destination,&rect);
    }
    int main(int argc, char* argv[])
    {
    
    //initialize
    SDL_Init(SDL_INIT_VIDEO);
    //load
    screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    background = SDL_LoadBMP("background.bmp");
    
    //game loop
    while(gameRunning)
    {
    if(SDL_PollEvent(&event))
    {
        if(event.type == SDL_Quit)
        {
            gameRunning = false;
        }
    }
    //apply surface
    apply_surface(0,0,background,screen);
    //update screen
    SDL_Flip(screen);
    }
    SDL_Quit();
    return 0;
    }
    I've fixed your loop for you. You also need to work on your indentation.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    Thanks for the help and advice lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comment each line and convert this C++ OOP code into C++ code.
    By Shayaan_Mustafa in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2011, 01:23 PM
  2. Run command line code from within my c code.
    By baikal_m in forum C Programming
    Replies: 6
    Last Post: 01-28-2010, 03:58 PM
  3. Why do I need this line of code?
    By Nextstopearth in forum C Programming
    Replies: 10
    Last Post: 09-05-2008, 04:54 AM
  4. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  5. Help with line of code
    By mcgeady in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2005, 01:31 PM