Polling Events in SDL

Member
Posts: 21
Joined: 2008.11
Post: #1
Hi,

I have been learning SDL and am now trying implement it by making a pong clone. I have drawn the two players and the ball and am now trying to use input to make the players move.

This is what my main loop looks like

Code:
// Main Loop
for(;;)
{
    playerAInput();
    updateScreen();
}

The playerAInput function lools like this

Code:
SDL_Event inputEvent;

void playerAInput()
{
    while (SDL_PollEvent(&inputEvent))
    {
        switch (inputEvent.type)
        {
            case SDL_KEYDOWN:
                switch(inputEvent.key.keysym.sym)
                {
                    case SDLK_UP:
                        playerAY -= 5;
                        break;
                    case SDLK_DOWN:
                        playerAY += 5;
                        break;
                    case SDLK_q:
                        SDL_QUIT;
                        break;
                    default:
                        break;
                }
        }
    }
}

and my updateScreen

Code:
void updateScreen()
{
    SDL_UpdateRect(displaySurface,0,0,0,0);
}

When I run my code it seems to get stuck and can't close normal or update the screen. I think it is the way that I am using the polling event. What am I doing wrong?
Quote this message in a reply
Member
Posts: 67
Joined: 2006.07
Post: #2
The reason you can't close the screen normally is because you didn't handle SDL_QUIT properly. SDL_QUIT is a type of event you get from SDL_PollEvent(), not a macro or anything. In your key handler, instead of using "SDL_QUIT;" you'd call SDL_Quit() along with an exit(0), because SQL_Quit() doesn't actually quit your app (it just shuts down SDL's subsystems). Just do the same thing in your SDL_QUIT handler.

I don't know why your SDL_UpdateRect() call isn't working. Try passing in SDL_GetVideoSurface() instead of your displaySurface variable and see if that works.

Since when was "Fred" a placeholder variable?
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #3
I suggest you start from a tutorial/working code, getting something working from the documentation alone is often close to impossible.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Member
Posts: 21
Joined: 2008.11
Post: #4
OK so I got the input funtion to work correct, but am still having problems with getting the screen to update. I found a tutorial and coppied it right out of the article and pasted it into xcode.

Code:
SDL_Surface *image;
    SDL_Surface *temp;
    
    temp = SDL_LoadBMP("image.bmp");
    if (temp == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        exit(0);
    }
    
    image = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);
    
    SDL_Rect src, dest;
    
    src.x = 0;
    src.y = 0;
    src.w = image->w;
    src.h = image->h;
    
    dest.x = 100;
    dest.y = 100;
    dest.w = image->w;
    dest.h = image->h;
    
    SDL_BlitSurface(image, &src, displaySurface, &dest);
    SDL_Flip(displaySurface);

I even got the same image they were using. When i build and go I get the error that it can't load the BMP file and app closes. I have the BMP file in my resources folder now but tried it everywhere i can imagine it being in the project. Do i have to put it somewhere special for SDL to pick it up?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
Quote this message in a reply
Member
Posts: 21
Joined: 2008.11
Post: #6
Awsome! I added the files in where the app is located in the debug folder. Is there a way that I can have it pick up the resources that are inside the app? When I "Show Package Contents" and open the resources i see all my resources that are in xcode.
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #7
try replacing the function "setupWorkingDirectory" in SDLMain.m with OSC's CF code i.e.

Code:
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
char path[PATH_MAX];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
{
    // error!
}
CFRelease(resourcesURL);
chdir(path);

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
Why use the CF code when the NS code is much shorter?
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #9
So that your code is 7 lines longer when bragging about your project's size, obviously Rasp

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Post Reply