get the flycam going already
This commit is contained in:
15
src/input.c
15
src/input.c
@ -1,5 +1,6 @@
|
||||
#include "input.h"
|
||||
#include "util.h"
|
||||
#include "context.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stb_ds.h>
|
||||
@ -190,6 +191,9 @@ void input_state_update(struct input_state *input) {
|
||||
input->mouse_state = SDL_GetMouseState(&input->mouse_window_position.x,
|
||||
&input->mouse_window_position.y);
|
||||
|
||||
SDL_GetRelativeMouseState(&input->mouse_relative_position.x,
|
||||
&input->mouse_relative_position.y);
|
||||
|
||||
for (size_t i = 0; i < shlenu(input->action_hash); ++i) {
|
||||
struct action *action = &input->action_hash[i].value;
|
||||
update_action_pressed_state(input, action);
|
||||
@ -296,3 +300,14 @@ t_fvec2 input_get_action_position(struct input_state *input, char *action_name)
|
||||
|
||||
return action->value.position;
|
||||
}
|
||||
|
||||
|
||||
void input_set_mouse_captured(struct input_state *input, bool enabled) {
|
||||
/* TODO: returns -1 if not supported, but like... do we care? */
|
||||
SDL_SetRelativeMouseMode(enabled);
|
||||
}
|
||||
|
||||
|
||||
bool input_is_mouse_captured(struct input_state *input) {
|
||||
return SDL_GetRelativeMouseMode();
|
||||
}
|
||||
|
11
src/input.h
11
src/input.h
@ -2,6 +2,7 @@
|
||||
#define INPUT_H
|
||||
|
||||
#include "config.h"
|
||||
#include "src/vec.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
@ -39,8 +40,12 @@ struct button {
|
||||
/* that is, changes in the states of any of the bound buttons will affect it */
|
||||
struct action {
|
||||
size_t num_bindings;
|
||||
|
||||
/* if you bind more than NUM_KEYBIND_SLOTS (set in config.h) */
|
||||
/* it forgets the first button to add the new one at the end */
|
||||
struct button bindings[NUM_KEYBIND_SLOTS];
|
||||
t_fvec2 position; /* set if applicable */
|
||||
|
||||
t_fvec2 position; /* set if applicable, e.g. mouse click */
|
||||
bool is_pressed;
|
||||
bool just_changed;
|
||||
};
|
||||
@ -57,6 +62,7 @@ struct input_state {
|
||||
const uint8_t *keyboard_state; /* array of booleans indexed by scancode */
|
||||
uint32_t mouse_state; /* SDL mouse button bitmask */
|
||||
t_vec2 mouse_window_position;
|
||||
t_vec2 mouse_relative_position;
|
||||
enum button_source last_active_source;
|
||||
bool is_anything_just_pressed;
|
||||
};
|
||||
@ -85,7 +91,10 @@ void input_delete_action(struct input_state *input, char *action_name);
|
||||
bool input_is_action_pressed(struct input_state *input, char *action_name);
|
||||
bool input_is_action_just_pressed(struct input_state *input, char *action_name);
|
||||
bool input_is_action_just_released(struct input_state *input, char *action_name);
|
||||
|
||||
t_fvec2 input_get_action_position(struct input_state *input, char *action_name);
|
||||
|
||||
void input_set_mouse_captured(struct input_state *input, bool value);
|
||||
bool input_is_mouse_captured(struct input_state *input);
|
||||
|
||||
#endif
|
||||
|
23
src/util.c
23
src/util.c
@ -101,6 +101,25 @@ void *ccalloc(size_t num, size_t size) {
|
||||
}
|
||||
|
||||
|
||||
double clamp(double d, double min, double max) {
|
||||
const double t = d < min ? min : d;
|
||||
return t > max ? max : t;
|
||||
}
|
||||
|
||||
|
||||
float clampf(float f, float min, float max) {
|
||||
const float t = f < min ? min : f;
|
||||
return t > max ? max : t;
|
||||
}
|
||||
|
||||
|
||||
int clampi(int i, int min, int max) {
|
||||
const int t = i < min ? min : i;
|
||||
return t > max ? max : t;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int64_t file_to_bytes(const char *path, unsigned char **buf_out) {
|
||||
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
|
||||
|
||||
@ -135,7 +154,7 @@ char *file_to_str(const char *path) {
|
||||
SDL_RWread(handle, str_out, sizeof *str_out, len);
|
||||
SDL_RWclose(handle); /* we got all we needed from the stream */
|
||||
|
||||
str_out[len] = '\0';
|
||||
str_out[len] = '\0';
|
||||
|
||||
return str_out;
|
||||
}
|
||||
@ -147,7 +166,7 @@ bool strends(const char *str, const char *suffix) {
|
||||
|
||||
if (suffix_length > str_length)
|
||||
return false;
|
||||
|
||||
|
||||
return memcmp((str + str_length) - suffix_length, suffix, suffix_length) == 0;
|
||||
}
|
||||
|
||||
|
13
src/util.h
13
src/util.h
@ -18,8 +18,8 @@
|
||||
/* */
|
||||
|
||||
void cry_impl(const char *file, const int line, const char *title, const char *text);
|
||||
#define CRY(title, text) cry_impl(__FILE__, __LINE__, title, text)
|
||||
#define CRY_SDL(title) cry_impl(__FILE__, __LINE__, title, SDL_GetError())
|
||||
#define CRY(title, text) cry_impl(__FILE__, __LINE__, title, text)
|
||||
#define CRY_SDL(title) cry_impl(__FILE__, __LINE__, title, SDL_GetError())
|
||||
#define CRY_PHYSFS(title) \
|
||||
cry_impl(__FILE__, __LINE__, title, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))
|
||||
|
||||
@ -53,6 +53,15 @@ void *ccalloc(size_t num, size_t size);
|
||||
#define M_PI 3.14159265358979323846264338327950288 /**< pi */
|
||||
#endif
|
||||
|
||||
/* multiply by these to convert degrees <---> radians */
|
||||
#define DEG2RAD (M_PI / 180)
|
||||
#define RAD2DEG (180 / M_PI)
|
||||
|
||||
/* TODO: this is why generics were invented. sorry, i'm tired today */
|
||||
double clamp(double d, double min, double max);
|
||||
float clampf(float f, float min, float max);
|
||||
int clampi(int i, int min, int max);
|
||||
|
||||
/* sets buf_out to a pointer to a byte buffer which must be freed. */
|
||||
/* returns the size of this buffer. */
|
||||
int64_t file_to_bytes(const char *path, unsigned char **buf_out);
|
||||
|
30
src/vec.h
30
src/vec.h
@ -66,6 +66,10 @@ static inline t_fvec2 fvec2_from_shvec2(t_shvec2 vec) {
|
||||
};
|
||||
}
|
||||
|
||||
static inline t_fvec3 fvec3_add(t_fvec3 a, t_fvec3 b) {
|
||||
return (t_fvec3) { a.x + b.x, a.y + b.y, a.z + b.z };
|
||||
}
|
||||
|
||||
static inline t_fvec3 fvec3_sub(t_fvec3 a, t_fvec3 b) {
|
||||
return (t_fvec3) { a.x - b.x, a.y - b.y, a.z - b.z };
|
||||
}
|
||||
@ -97,6 +101,32 @@ static inline t_fvec3 fvec3_norm(t_fvec3 a) {
|
||||
return fvec3_scale(a, 1.0f / n);
|
||||
}
|
||||
|
||||
static inline t_fvec3 fvec3_rotate(t_fvec3 v, float angle, t_fvec3 axis) {
|
||||
/* from cglm */
|
||||
t_fvec3 v1, v2, k;
|
||||
float c, s;
|
||||
|
||||
c = cosf(angle);
|
||||
s = sinf(angle);
|
||||
|
||||
k = fvec3_norm(axis);
|
||||
|
||||
/* Right Hand, Rodrigues' rotation formula:
|
||||
v = v*cos(t) + (kxv)sin(t) + k*(k.v)(1 - cos(t))
|
||||
*/
|
||||
v1 = fvec3_scale(v, c);
|
||||
|
||||
v2 = fvec3_cross(k, v);
|
||||
v2 = fvec3_scale(v2, s);
|
||||
|
||||
v1 = fvec3_add(v1, v2);
|
||||
|
||||
v2 = fvec3_scale(k, fvec3_dot(k, v) * (1.0f - c));
|
||||
v = fvec3_add(v1, v2);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
#define m_to_fvec2(p_any_vec2) (_Generic((p_any_vec2), \
|
||||
t_vec2: fvec2_from_vec2, \
|
||||
t_shvec2: fvec2_from_shvec2 \
|
||||
|
Reference in New Issue
Block a user