iDevGames Forums
Polling Events in SDL - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Polling Events in SDL (/thread-2144.html)



Polling Events in SDL - silver9172 - Nov 15, 2008 03:15 PM

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?


Polling Events in SDL - Wowbagger - Nov 15, 2008 05:30 PM

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.


Polling Events in SDL - Najdorf - Nov 15, 2008 07:11 PM

I suggest you start from a tutorial/working code, getting something working from the documentation alone is often close to impossible.


Polling Events in SDL - silver9172 - Nov 16, 2008 10:43 PM

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?


Polling Events in SDL - OneSadCookie - Nov 17, 2008 12:34 AM

OneSadCookie: Finding Your App's Files


Polling Events in SDL - silver9172 - Nov 17, 2008 08:40 AM

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.


Polling Events in SDL - Najdorf - Nov 17, 2008 09:09 AM

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);



Polling Events in SDL - OneSadCookie - Nov 17, 2008 12:07 PM

Why use the CF code when the NS code is much shorter?


Polling Events in SDL - Najdorf - Nov 17, 2008 01:01 PM

So that your code is 7 lines longer when bragging about your project's size, obviously Rasp