在FPS风格游戏中使用鼠标输入时,运动意外加速

时间:2013-01-21 17:34:12

标签: c++ opengl sdl

我正在使用鼠标和键盘输入在OpenGL(使用SDL作为框架)编写FPS风格的游戏。出于某种原因,每当我使用鼠标改变方向(旋转)时,同时用键盘移动“摄像机位置”,移动速度会急剧加快(而不是在不使用鼠标时用键盘移动)。

我的代码出了什么问题?

的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;
}

1 个答案:

答案 0 :(得分:2)

尝试在轮询循环外移动EventHandler()

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

否则将为每个调用EventHandler()事件。你知道什么会产生一堆事件吗?鼠标动作:)