Smooth rotation speed & only poll keyState once per frame.

This commit is contained in:
2023-11-20 15:07:32 -05:00
parent f10a2fc9ef
commit 579444c4de
3 changed files with 36 additions and 41 deletions

View File

@@ -37,9 +37,7 @@ public:
float farPlane = 100.0f;
float fov = 72.5f;
SDL_Event event;
void keyState () {
const Uint8* ks = SDL_GetKeyboardState(nullptr);
}
Uint8* keyState;
[[nodiscard]] float framerate() const {
return 1.f / this->frameDelta;

View File

@@ -48,9 +48,9 @@ void pre_render() {
auto skybox = new(Skybox);
entityList.storeEntity(skybox);
}
process_sdl_events();
engine->frameCount++;
process_sdl_events();
engine->keyState = const_cast<Uint8 *>(SDL_GetKeyboardState(nullptr));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
@@ -118,7 +118,6 @@ void post_render() {
//Resets all of the transformations for the next frame.
glPopMatrix();
glPushMatrix();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
[[noreturn]] void render_loop() {
@@ -129,6 +128,13 @@ void post_render() {
post_render();
auto stop = std::chrono::high_resolution_clock::now();
float dT = (std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count());
//If we have more than 1000 fps.
if (dT < 1000){
int remaining = 1000 - dT;
std::this_thread::sleep_for(std::chrono::microseconds(remaining));
dT = 1000;
}
engine->frameDelta = dT / 1000000;
}
}

View File

@@ -14,55 +14,46 @@ public:
void update() {
//TODO: move this to render next.
//I suspect this will be more complicated.
const Uint8* keyState = SDL_GetKeyboardState(nullptr);
if (keyState[SDL_SCANCODE_SPACE] == 1) {
this->position.y -= 0.025;
}
if (keyState[SDL_SCANCODE_LSHIFT] == 1) {
this->position.y += 0.025;
}
if (keyState[SDL_SCANCODE_LEFT] == 1) {
this->angles.y +=2.0f;
}
if (keyState[SDL_SCANCODE_RIGHT] == 1) {
this->angles.y -=2.0f;
//std::cout << this->angles.y << std::endl;
}
if (keyState[SDL_SCANCODE_UP] == 1) {
this->angles.x +=2.0f;
//std::cout << this->angles.x << std::endl;
}
if (keyState[SDL_SCANCODE_DOWN] == 1) {
this->angles.x -=2.0f;
//std::cout << this->angles.x << std::endl;
}
}
void render() {
if (mode == CameraMode::FREECAM) {
//TODO: check this once every frame instead of here.
const Uint8* keyState = SDL_GetKeyboardState(nullptr);
if (keyState[SDL_SCANCODE_0] == 1) {
if (engine->keyState[SDL_SCANCODE_0] == 1) {
this->takingScreenshot = true;
}
if (keyState[SDL_SCANCODE_S] == 1) {
if (engine->keyState[SDL_SCANCODE_S] == 1) {
move(bAngle(),2);
}
if (keyState[SDL_SCANCODE_W] == 1) {
//this->position.z = this->position.z-0.1f;
if (engine->keyState[SDL_SCANCODE_W] == 1) {
move(fAngle(),2);
}
if (keyState[SDL_SCANCODE_A] == 1) {
if (engine->keyState[SDL_SCANCODE_A] == 1) {
move(lAngle(), 2);
}
if (keyState[SDL_SCANCODE_D] == 1) {
if (engine->keyState[SDL_SCANCODE_D] == 1) {
move(rAngle(),2);
}
if (engine->keyState[SDL_SCANCODE_SPACE] == 1) {
this->position.y -= 5*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_LSHIFT] == 1) {
this->position.y += 5*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_LEFT] == 1) {
this->angles.y +=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_RIGHT] == 1) {
this->angles.y -=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_UP] == 1) {
this->angles.x +=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_DOWN] == 1) {
this->angles.x -=75.0f*engine->frameDelta;
}
}
this->angles.clamp();
glRotatef(-angles.x,1.0f, 0.0f, 0.0f);