简体   繁体   中英

Movement Unexpectedly Speeds Up When Using Mouse Input On FPS Style Game

I'm writing a FPS style game in OpenGL (with SDL as a framework) utilizing mouse and keyboard input. For some reason whenever I change direction (rotate) using the mouse WHILE moving the "camera position" with the keyboard simultaneously, the movement drastically speeds up (as opposed to moving with keyboard when the mouse isn't being used).

What's wrong with my code?

main.cpp

#define PI 3.14159265
#include <sdl.h>
#include <string>
#include "SDL_opengl.h"
#include <gl\gl.h>
#include <gl\glu.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
float urad(0), drad(0), slrad(0), srrad(0);
int mousex(0), mousey(0);
float transx(0), transy(-.3), transz(0), yrot(0), luad(0);
float rotx(0), roty(0);
bool up,down,right,left,tr,tl, sl,sr,lu,ld;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Event event;
GLfloat zpos(0);
GLfloat rot(0);
bool exited(false);
void drawstuff(); //Function Prototype
void startGL()
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_SetVideoMode(800, 500, 32, SDL_OPENGL | SDL_HWPALETTE);
    glViewport(0,0,800,500);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f,(GLfloat)800/(GLfloat)500,0.1f,100.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_POLYGON_SMOOTH);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void drawstuff()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glRotatef(roty, 0, 1, 0);
    glRotatef(luad, 1, 0, 0);
    glTranslatef(transx, transy, transz);
    glBegin(GL_QUADS);                                  // Draw A Quad
        glColor3f(0.0f,1.0f,0.0f);                      // Set The Color To Green
        glVertex3f( 1.0f, 1.0f,-1.0f);                  // Top Right Of The Quad (Top)
        glVertex3f(-1.0f, 1.0f,-1.0f);                  // Top Left Of The Quad (Top)
        glVertex3f(-1.0f, 1.0f, 1.0f);                  // Bottom Left Of The Quad (Top)
        glVertex3f( 1.0f, 1.0f, 1.0f);                  // Bottom Right Of The Quad (Top)
        glColor3f(1.0f,0.5f,0.0f);                      // Set The Color To Orange
        glVertex3f( 1.0f,-1.0f, 1.0f);                  // Top Right Of The Quad (Bottom)
        glVertex3f(-1.0f,-1.0f, 1.0f);                  // Top Left Of The Quad (Bottom)
        glVertex3f(-1.0f,-1.0f,-1.0f);                  // Bottom Left Of The Quad (Bottom)
        glVertex3f( 1.0f,-1.0f,-1.0f);                  // Bottom Right Of The Quad (Bottom)
        glColor3f(1.0f,0.0f,0.0f);                      // Set The Color To Red
        glVertex3f( 1.0f, 1.0f, 1.0f);                  // Top Right Of The Quad (Front)
        glVertex3f(-1.0f, 1.0f, 1.0f);                  // Top Left Of The Quad (Front)
        glVertex3f(-1.0f,-1.0f, 1.0f);                  // Bottom Left Of The Quad (Front)
        glVertex3f( 1.0f,-1.0f, 1.0f);                  // Bottom Right Of The Quad (Front)
        glColor3f(1.0f,1.0f,0.0f);                      // Set The Color To Yellow
        glVertex3f( 1.0f,-1.0f,-1.0f);                  // Top Right Of The Quad (Back)
        glVertex3f(-1.0f,-1.0f,-1.0f);                  // Top Left Of The Quad (Back)
        glVertex3f(-1.0f, 1.0f,-1.0f);                  // Bottom Left Of The Quad (Back)
        glVertex3f( 1.0f, 1.0f,-1.0f);                  // Bottom Right Of The Quad (Back)
        glColor3f(0.0f,0.0f,1.0f);                      // Set The Color To Blue
        glVertex3f(-1.0f, 1.0f, 1.0f);                  // Top Right Of The Quad (Left)
        glVertex3f(-1.0f, 1.0f,-1.0f);                  // Top Left Of The Quad (Left)
        glVertex3f(-1.0f,-1.0f,-1.0f);                  // Bottom Left Of The Quad (Left)
        glVertex3f(-1.0f,-1.0f, 1.0f);                  // Bottom Right Of The Quad (Left)
        glColor3f(1.0f,0.0f,1.0f);                      // Set The Color To Violet
        glVertex3f( 1.0f, 1.0f,-1.0f);                  // Top Right Of The Quad (Right)
        glVertex3f( 1.0f, 1.0f, 1.0f);                  // Top Left Of The Quad (Right)
        glVertex3f( 1.0f,-1.0f, 1.0f);                  // Bottom Left Of The Quad (Right)
        glVertex3f( 1.0f,-1.0f,-1.0f);                  // Bottom Right Of The Quad (Right)
    glEnd();                                            // Done Drawing The Quad
}

void EventListener()
{
    Uint8 *keystate = SDL_GetKeyState(NULL);
    SDL_PumpEvents();
    if (keystate[SDLK_LEFT]) tl=true;
    else tl=false;
    if (keystate[SDLK_RIGHT]) tr=true;
    else tr=false;
    if (keystate['w'] ) up=true;
    else up=false;
    if (keystate['s'] ) down=true;
    else down=false;
    if (keystate['a'] ) sl=true;
    else sl=false;
    if (keystate['d'] ) sr=true;
    else sr=false;
}
void EventHandler()
{
    if (tl)
    {
        roty-=2;
        if (roty >360)
        {
            roty -= 360;
        }
    }
    if (tr)
    {
        roty+=2;
        if (roty <-360)
        {
            roty += 360;
        }
    }
    if (up)
    {
        urad = roty*PI/180;
        transx+=sin(-urad) * .06;
        transz+=cos(-urad) * .06;
    }
    if (down)
    {
        drad = roty*PI/180;
        transx-=sin(-drad) * .06;
        transz-=cos(-drad) * .06;
    }
    if (sl)
    {
        slrad = roty*PI/180;
        transx += cos(slrad) * .04;
        transz += sin(slrad) * .04;
    }
    if (sr)
    {

        srrad = roty*PI/180;
        transx -= cos(srrad) * .04;
        transz -= sin(srrad) * .04;
    }
    SDL_PumpEvents();
    SDL_GetMouseState(&mousex, &mousey);
    roty=mousex*.5;
}
int main (int argc, char* args[])
{
    SDL_Surface* screen = NULL;
    SDL_Init( SDL_INIT_EVERYTHING );
    screen = SDL_SetVideoMode( 800, 500, 32, SDL_SWSURFACE);
    SDL_Flip( screen );
    SDL_WM_SetCaption("OpenGL", NULL);
    startGL();
    SDL_EnableKeyRepeat(1, 10);
    while(exited == false)
    {
        while( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_QUIT )
            {
                exited = true;
            }
            EventListener();
            EventHandler();

        }
        glClear(GL_COLOR_BUFFER_BIT);

        drawstuff();
        SDL_GL_SwapBuffers();
        SDL_Delay(16);
    }
    SDL_Quit();
    return 1;
}

Try moving the EventHandler() outside the polling loop:

while( SDL_PollEvent( &event ) )
{
    if( event.type == SDL_QUIT )
    {
        exited = true;
    }
    EventListener();
}
EventHandler();

Otherwise EventHandler() will be called for every . single . event . You know what generates a bunch of events? Mouse motion :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM