我正在使用OpenGL创建一个游戏并且SDL_PollEvent没有拾取所有事件的问题,例如,如果我按住一个键,我必须等待~1-3秒,然后程序才能实现我所拥有的完成。这是我的 main.cpp :
unsigned char command[] = {0x1B, 0x2A, 0x72, 0x44, 0x01, 0x00};
uint bytesWritten = 0;
StarPrinterStatus_2 starPrinterStatus;
SMPort *port = nil;
@try
{
port = [SMPort getPort:@"BT:" :@"" :10000];
//Start checking the completion of printing
[port beginCheckedBlock:&starPrinterStatus :2];
if (starPrinterStatus.offline == SM_TRUE)
{
//There was an error writing to the port
}
while (bytesWritten < sizeof (command)) {
bytesWritten += [port writePort: command : bytesWritten : sizeof (command) - bytesWritten];
}
//End checking the completion of printing
[port endCheckedBlock:&starPrinterStatus :2];
if (starPrinterStatus.offline == SM_TRUE)
{
//There was an error writing to the port
}
}
@catch (PortException)
{
//There was an error writing to the port
}
@finally
{
[SMPort releasePort:port];
}
所有功能都按照他们的名字命名并正常工作。
编辑:
我已完成一些测试并删除了#include <GL/glew.h>
#include <GL/GL.h>
#include <SDL\SDL.h>
#include "Display.h"
#include "Mesh.h"
#include "Shader.h"
#include "Texture.h"
#include "Transform.h"
#include "Camera.h"
#include <iostream>
#define WIDTH 800
#define HEIGHT 600
int main(int argc, char *argv[])
{
Display Display(800, 600, "OpenGL");
Vertex vertices[] = { Vertex(glm::vec3(-0.5, -0.5, 0), glm::vec2(0.0, 0.0)),
Vertex(glm::vec3(0, 0.5, 0), glm::vec2(0.5, 1.0)),
Vertex(glm::vec3(0.5, -0.5, 0), glm::vec2(1.0, 0.0)), };
unsigned int indices[] = { 0,1,2 };
SDL_Init(SDL_INIT_EVERYTHING);
// Mesh Car("./res/Test2.obj");
// Mesh Test(vertices, sizeof(vertices) / sizeof(vertices[0]), indices, sizeof(indices)/sizeof(indices[0]));
Mesh Ground("./res/Ground2.obj");
Shader shader("./res/basicShader");
// Texture texture("./res/CarUV.png");
Texture test("./res/ground.jpg");
Camera camera(glm::vec3(0, 0, -14), 70.f, (float)WIDTH / (float)HEIGHT, 0.01f, 1000.0f);
Transform transform;
const Uint8 *keyState;
transform.GetPos().x = 0;
transform.GetPos().y = 0;
transform.GetPos().z = 0;
transform.GetRot().x = 80;
transform.GetRot().y = 0;
transform.GetRot().z = 0;
float counter = 0.0f;
while (!Display.IsClosed())
{
Display.Clear(0.0f, 0.15f, 0.3f, 1.0f);
float sinCounter = sinf(counter);
float cosCounter = cosf(counter);
SDL_Event ev;
while (SDL_PollEvent(&ev) != 0)
{
keyState = SDL_GetKeyboardState(NULL);
if (keyState[SDL_SCANCODE_W])
{
camera.MoveForward(1);
std::cout << "keypress" << std::endl;
}
else if (keyState[SDL_SCANCODE_S])
{
camera.MoveBackward(1);
}
if (keyState[SDL_SCANCODE_A])
{
camera.MoveRight(1);
}
else if (keyState[SDL_SCANCODE_D])
{
camera.MoveLeft(1);
}
if (keyState[SDL_SCANCODE_Y])
{
camera.RotateY(0.1);
}
if (keyState[SDL_SCANCODE_X])
{
camera.RotateX(0.1);
}
}
//transform.SetScale(glm::vec3(cosCounter, cosCounter, cosCounter));
shader.Bind();
test.Bind(0);
shader.Update(transform, camera);
//Car.draw();
Ground.draw();
Display.Update();
counter += 1.0f;
}
return 0;
}
和Display.clear();
修复了问题,但这不是一个可行的解决方案。
编辑2:
就像程序最初工作的额外信息一样,然后我开始摆弄它并且事件处理程序搞砸了。
答案 0 :(得分:1)
正如@HolyBlackCat在评论中所说,在这部分代码中:
while (SDL_PollEvent(&ev) != 0)
{
//if statements here
}
while循环是不必要的,因为你已经有了以下形式的循环:
while (!Display.IsClosed())
{
//main game loop
}
在内部使用另一个while循环只会导致问题。
无论如何希望这会有所帮助,请不要将其标记为答案 @HolyBlackCat创建一个或这个不适合你。欢呼声。