Archived

This topic is now archived and is closed to further replies.

repeated release message

This topic is 5131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

C++, SDL, Dev-C++, win2k I am writing a simple program using SDL. It goes into fullscreen and draws a bitmap each frame. Supposedly, the bitmap should move when I press the arrows. I have an ordered pair vVel containing the velocity. When I noticed that it doesn''t move, I started a logfile to record the velocity and position every frame and any keypresses or releases. Here are the contents of the file after one press and release: Vel: [0,0] Pos: [384,284] Key pressed. New velocity: [0,-1] Key released. New velocity: [0,0] Vel: [0,0] Pos: [384,284] . . . Vel: [0,0] Pos: [384,284] Key released. New velocity: [0,0] Vel: [0,0] Pos: [384,284] Complete Logfile It seems like a release is detected immediately, and also when the key is actually released. (I limited it to 30 fps so it wouldn''t be insanely fast if it worked) Here is the message handler:
    while(SDL_PollEvent(&event)) {
      switch(event.type) {
        //irrelevant code removed (quit clause)

        case SDL_KEYDOWN:
          switch(event.key.keysym.sym) {
            case SDLK_ESCAPE:
              done=true;
              break;
            case SDLK_UP:
              vVel.y=-1;
              printf("Key pressed.  New velocity: [%d,%d]\n",vVel.x,vVel.y);
              break;
            //irrelevant code removed (other 3 cases, almost identical to case UP

            default:
              break;
          }
        case SDL_KEYUP:
          switch(event.key.keysym.sym) {
            case SDLK_UP:
              vVel.y=0;
              printf("Key released.  New velocity: [%d,%d]\n",vVel.x,vVel.y);
              break;
            //irrelevant code removed (other 3 cases)

          }
          break;
        //other events

        
        default:
          break;
      }
    }
Here is the complete source of the main loop:
  while(!done) {
    nTime=SDL_GetTicks();
    
    //draw frame

    cSmile.Blit();
    
    
    //flip screen

    SDL_Flip(hScreen);
    
    //message handling

    while(SDL_PollEvent(&event)) {
      switch(event.type) {
        case SDL_QUIT:
          done=true;
          break;
        case SDL_KEYDOWN:
          switch(event.key.keysym.sym) {
            case SDLK_ESCAPE:
              done=true;
              break;
            case SDLK_UP:
              vVel.y=-1;
              printf("Key pressed.  New velocity: [%d,%d]\n",vVel.x,vVel.y);
              break;
            case SDLK_DOWN:
              vVel.y=1;
              printf("Key pressed.  New velocity: [%d,%d]\n",vVel.x,vVel.y);
              break;
            case SDLK_LEFT:
              vVel.x=-1;
              printf("Key pressed.  New velocity: [%d,%d]\n",vVel.x,vVel.y);
              break;
            case SDLK_RIGHT:
              vVel.x=1;
              printf("Key pressed.  New velocity: [%d,%d]\n",vVel.x,vVel.y);
              break;
            default:
              break;
          }
          
        case SDL_KEYUP:
          switch(event.key.keysym.sym) {
            case SDLK_UP:
              vVel.y=0;
              printf("Key released.  New velocity: [%d,%d]\n",vVel.x,vVel.y);
              break;
            case SDLK_DOWN:
              vVel.y=0;
              printf("Key released.  New velocity: [%d,%d]\n",vVel.x,vVel.y);
              break;
            case SDLK_LEFT:
              vVel.x=0;
              printf("Key released.  New velocity: [%d,%d]\n",vVel.x,vVel.y);
              break;
            case SDLK_RIGHT:
              vVel.x=0;
              printf("Key released.  New velocity: [%d,%d]\n",vVel.x,vVel.y);
              break;
            default:
              break;
          }
          break;
        //other events

        
        default:
          break;
      }
    }
    //game logic

    printf("Vel: [%d,%d] Pos: [%d,%d]\n",vVel.x,vVel.y,cSmile.GetX(),cSmile.GetY());
    cSmile.AddPos(vVel);
    
    
    /*if(cSmile.GetX()<0) cSmile.SetX(0);
    if(cSmile.GetX()>MAX_X-32) cSmile.SetX(MAX_X-32);
    if(cSmile.GetY()<0) cSmile.SetY(0);
    if(cSmile.GetY()>MAX_Y-32) cSmile.SetY(MAX_Y-32);*/
    
    
    //timer delay- limit program to 30 fps

    while((nTime - SDL_GetTicks()) < 33);
  }

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you forgot to break out of the switch

you fell thorugh to the release code

- C-Junkie too lazy to log in

Share this post


Link to post
Share on other sites
Thanks, that fixed it! Here is a link to the executable and image. I know it is simple, but the Character class will allow for easy implementation of new features. Included is the windows version of SDL.dll; you may have to dl the version for your OS. For that matter, it probably will only work on windows without being recompiled.

[edited by - nagromo on November 29, 2003 11:42:39 PM]

Share this post


Link to post
Share on other sites