cross-platform code

leggo
Unregistered
 
Post: #31
I'm making a little game to try SDL and implementaton files

I've created a "ship" class with a function handling its movements

Code:
void astronave::controllanave() {
    SDL_Event event;
            
    glLoadIdentity();
    glTranslatef(x, y, -10.0f);
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_LINE_LOOP);
        glVertex2f(0.0f, 1.0f);
        glVertex2f(-1.0f, 0.0f);
        glVertex2f(1.0f, 0.0f);
    glEnd();
    x += vx;
    y += vy;
            
    while ( SDL_PollEvent (&event) ) {
        switch (event.type) {
            case SDLK_UP:
                vy = 0.2f;
            break;
            case SDLK_DOWN:
                vy = -0.2f;
            break;
            case SDLK_LEFT:
                vx = -0.2f;
            break;
            case SDLK_RIGHT:
                vx = 0.2f;
            break;
            default:
            break;
        }
    }
}


the problem is that my wonderful triangle won't move from the start point! Mad
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #32
If I'm not entirely mistaken, you're getting stuck in an infinite loop. I don't know how the rest of your code looks, but I suggest that you re-build it a little bit.

You already have the concept of a "main loop" down, but in the wrong place. When you start the game, you should trap yourself in that while() loop you've written. From within it, you should update and draw the ship as needed.

More like:
Code:
while (SDL_PollEvent (&event))
{
       switch_and_stuff();
      update_ship();
      draw_ship();
}

You see, what happens there is that you enter the movement function, draw the ship, and then get stuck receiving events. But, you never exit that while loop, so you never redraw.

Check out some of the SDL tutorials on libsdl.org! Smile
Quote this message in a reply
leggo
Unregistered
 
Post: #33
Fenris Wrote:If I'm not entirely mistaken, you're getting stuck in an infinite loop. I don't know how the rest of your code looks, but I suggest that you re-build it a little bit.

You already have the concept of a "main loop" down, but in the wrong place. When you start the game, you should trap yourself in that while() loop you've written. From within it, you should update and draw the ship as needed.

More like:
Code:
while (SDL_PollEvent (&event))
{
       switch_and_stuff();
      update_ship();
      draw_ship();
}

You see, what happens there is that you enter the movement function, draw the ship, and then get stuck receiving events. But, you never exit that while loop, so you never redraw.

Check out some of the SDL tutorials on libsdl.org! Smile

ah ok! tnx for the help! Wow
I'll check the tutorials as soon as I have more time!
Quote this message in a reply
Member
Posts: 70
Joined: 2004.06
Post: #34
Blush wrong thread Blush
Quote this message in a reply
leggo
Unregistered
 
Post: #35
umm I was wondering if SDL_GetTick() is precise enough, or if it's better to use OS' specific code... what do you think?
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #36
SDL_GetTick() is at least 1/60 seconds fidelity. For fast, fast games, that might be a little low, but I can almost guarantee you that it is above millisec fidelity on Mac OS X... So I wouldn't worry. Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Cross platform question kordeul 13 5,044 Aug 22, 2007 03:09 AM
Last Post: kordeul
  Cross-platform gui? Duane 13 5,377 Jul 6, 2007 06:38 PM
Last Post: mac_girl
  Best cross platform API for PC & MAC Tarek Demiati 6 3,205 Apr 16, 2006 03:07 PM
Last Post: Dan Potter
  Cross platform game code on a budget Carlos Camacho 7 3,685 Apr 19, 2003 09:29 PM
Last Post: Mars_999
  Cross-platform Solutions DJBlufire 13 4,923 Feb 16, 2003 02:40 PM
Last Post: athomson