简体   繁体   English

在FPS样式游戏上使用鼠标输入时,移动速度意外加快

[英]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. 我正在使用鼠标和键盘输入在OpenGL中(以SDL作为框架)编写FPS风格的游戏。 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 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: 尝试将EventHandler()移到轮询循环之外:

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

Otherwise EventHandler() will be called for every . 否则,将为every调用EventHandler() single . 单身 event . 事件 You know what generates a bunch of events? 您知道什么会产生一系列事件吗? Mouse motion :) 鼠标动作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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