mirror of
https://github.com/kuhyx/testsAndMisc-archive.git
synced 2026-07-04 15:43:11 +02:00
fix: left button drift
This commit is contained in:
parent
f97a06f440
commit
4125436fec
33
C/fps/main.c
33
C/fps/main.c
@ -13,6 +13,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#ifndef M_PI
|
#ifndef M_PI
|
||||||
#define M_PI 3.14159265358979323846
|
#define M_PI 3.14159265358979323846
|
||||||
@ -364,14 +365,19 @@ static void display()
|
|||||||
static void update(float dt)
|
static void update(float dt)
|
||||||
{
|
{
|
||||||
if (g_state == GAME_RUNNING) {
|
if (g_state == GAME_RUNNING) {
|
||||||
float speed = g_move_speed * ((g_keys['\t'] || g_keys['Q'] || g_keys['q']) ? g_sprint_mul : 1.0f); // Tab/Q to sprint
|
float speed = g_move_speed * ((g_keys['\t'] || g_keys['q']) ? g_sprint_mul : 1.0f); // Tab/q to sprint
|
||||||
vec3 f = cam_front(); f.y = 0.0f; f = v3_norm(f);
|
vec3 f = cam_front(); f.y = 0.0f; f = v3_norm(f);
|
||||||
vec3 r = cam_right(); r.y = 0.0f; r = v3_norm(r);
|
vec3 r = cam_right(); r.y = 0.0f; r = v3_norm(r);
|
||||||
|
// Accumulate movement and apply once to avoid drift artifacts
|
||||||
if (g_keys['W'] || g_keys['w']) g_cam_pos = v3_add(g_cam_pos, v3_scale(f, speed*dt));
|
vec3 mv = v3(0,0,0);
|
||||||
if (g_keys['S'] || g_keys['s']) g_cam_pos = v3_sub(g_cam_pos, v3_scale(f, speed*dt));
|
if (g_keys['w']) mv = v3_add(mv, f);
|
||||||
if (g_keys['A'] || g_keys['a']) g_cam_pos = v3_sub(g_cam_pos, v3_scale(r, speed*dt));
|
if (g_keys['s']) mv = v3_sub(mv, f);
|
||||||
if (g_keys['D'] || g_keys['d']) g_cam_pos = v3_add(g_cam_pos, v3_scale(r, speed*dt));
|
if (g_keys['a']) mv = v3_sub(mv, r);
|
||||||
|
if (g_keys['d']) mv = v3_add(mv, r);
|
||||||
|
if (v3_len(mv) > 0.0f) {
|
||||||
|
mv = v3_norm(mv);
|
||||||
|
g_cam_pos = v3_add(g_cam_pos, v3_scale(mv, speed*dt));
|
||||||
|
}
|
||||||
|
|
||||||
// Keep feet on ground
|
// Keep feet on ground
|
||||||
g_cam_pos.y = 1.6f;
|
g_cam_pos.y = 1.6f;
|
||||||
@ -436,7 +442,8 @@ static void reshape(int w, int h)
|
|||||||
static void keyboard_down(unsigned char key, int x, int y)
|
static void keyboard_down(unsigned char key, int x, int y)
|
||||||
{
|
{
|
||||||
(void)x; (void)y;
|
(void)x; (void)y;
|
||||||
g_keys[key] = true;
|
unsigned char k = (unsigned char)tolower(key);
|
||||||
|
g_keys[k] = true;
|
||||||
if (key == 27) { // Esc
|
if (key == 27) { // Esc
|
||||||
exit(0);
|
exit(0);
|
||||||
} else if (key == ' ') {
|
} else if (key == ' ') {
|
||||||
@ -459,7 +466,8 @@ static void keyboard_down(unsigned char key, int x, int y)
|
|||||||
static void keyboard_up(unsigned char key, int x, int y)
|
static void keyboard_up(unsigned char key, int x, int y)
|
||||||
{
|
{
|
||||||
(void)x; (void)y;
|
(void)x; (void)y;
|
||||||
g_keys[key] = false;
|
unsigned char k = (unsigned char)tolower(key);
|
||||||
|
g_keys[k] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mouse_button(int button, int state, int x, int y)
|
static void mouse_button(int button, int state, int x, int y)
|
||||||
@ -500,6 +508,13 @@ static void init_gl()
|
|||||||
glLineWidth(1.0f);
|
glLineWidth(1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void on_entry(int state)
|
||||||
|
{
|
||||||
|
if (state != GLUT_ENTERED) {
|
||||||
|
for (int i = 0; i < 256; ++i) g_keys[i] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
(void)argv;
|
(void)argv;
|
||||||
@ -518,6 +533,8 @@ int main(int argc, char** argv)
|
|||||||
glutReshapeFunc(reshape);
|
glutReshapeFunc(reshape);
|
||||||
glutKeyboardFunc(keyboard_down);
|
glutKeyboardFunc(keyboard_down);
|
||||||
glutKeyboardUpFunc(keyboard_up);
|
glutKeyboardUpFunc(keyboard_up);
|
||||||
|
// Clear keys on focus leave to avoid stuck inputs
|
||||||
|
glutEntryFunc(on_entry);
|
||||||
glutPassiveMotionFunc(passive_motion);
|
glutPassiveMotionFunc(passive_motion);
|
||||||
glutMouseFunc(mouse_button);
|
glutMouseFunc(mouse_button);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user