Add movement relative to time
Previously movement was based on frames rather than time. I added time_get_dt() function for getting time since last frame and changed rotation speed to use this function. Signed-off-by: Piotr Krygier <piotrkrygier@everyonecancode@xyz>
This commit is contained in:
@@ -28,7 +28,6 @@ struct rse_graphics_context_t
|
|||||||
struct graphics_context_t* context;
|
struct graphics_context_t* context;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define NS_TO_S(time) (time/1000000000)
|
|
||||||
static void model_view_projection_update(struct graphics_context_t* context)
|
static void model_view_projection_update(struct graphics_context_t* context)
|
||||||
{
|
{
|
||||||
vec3 eye = {0};
|
vec3 eye = {0};
|
||||||
@@ -38,6 +37,7 @@ static void model_view_projection_update(struct graphics_context_t* context)
|
|||||||
float fovy;
|
float fovy;
|
||||||
float z_near;
|
float z_near;
|
||||||
float z_far;
|
float z_far;
|
||||||
|
static float rotation = 0.0;
|
||||||
|
|
||||||
eye[2] = -5.0f;
|
eye[2] = -5.0f;
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ static void model_view_projection_update(struct graphics_context_t* context)
|
|||||||
|
|
||||||
glm_mat4_identity(context->mvp_data.model);
|
glm_mat4_identity(context->mvp_data.model);
|
||||||
|
|
||||||
glm_rotate(context->mvp_data.model, time_get_sec_from_start() * glm_rad(90.0f), rotation_vec);
|
glm_rotate(context->mvp_data.model, rotation, rotation_vec);
|
||||||
glm_lookat(eye, center, up, context->mvp_data.view);
|
glm_lookat(eye, center, up, context->mvp_data.view);
|
||||||
glm_perspective(
|
glm_perspective(
|
||||||
fovy,
|
fovy,
|
||||||
@@ -57,6 +57,8 @@ static void model_view_projection_update(struct graphics_context_t* context)
|
|||||||
z_near,
|
z_near,
|
||||||
z_far,
|
z_far,
|
||||||
context->mvp_data.proj);
|
context->mvp_data.proj);
|
||||||
|
|
||||||
|
rotation += time_get_dt() * glm_rad(90.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static rse_err_t render_static_mesh(struct graphics_context_t* context, uint32_t renderer_id)
|
static rse_err_t render_static_mesh(struct graphics_context_t* context, uint32_t renderer_id)
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ rse_err_t window_loop(struct graphics_context_t* context)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time_update();
|
||||||
draw_frame(context);
|
draw_frame(context);
|
||||||
fps_print(&start_time);
|
fps_print(&start_time);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,5 +14,8 @@ executable(
|
|||||||
dependencies : [
|
dependencies : [
|
||||||
sdl3_dep,
|
sdl3_dep,
|
||||||
],
|
],
|
||||||
link_with: rse_graphics_lib,
|
link_with: [
|
||||||
|
rse_graphics_lib,
|
||||||
|
rse_utilities_lib,
|
||||||
|
],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "graphics/rse_graphics.h"
|
#include "graphics/rse_graphics.h"
|
||||||
#include "utilities/errors_common.h"
|
#include "utilities/errors_common.h"
|
||||||
#include "SDL3/SDL_thread.h"
|
#include "SDL3/SDL_thread.h"
|
||||||
|
#include "utilities/time_utils.h"
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
@@ -15,6 +16,7 @@ int main(int argc, char** argv)
|
|||||||
|
|
||||||
SDL_SetLogPriorities(SDL_LOG_PRIORITY_VERBOSE);
|
SDL_SetLogPriorities(SDL_LOG_PRIORITY_VERBOSE);
|
||||||
|
|
||||||
|
STATUS_CHECK(time_init());
|
||||||
STATUS_CHECK(rse_graphics_init(&context));
|
STATUS_CHECK(rse_graphics_init(&context));
|
||||||
STATUS_CHECK(rse_graphics_test_function(context));
|
STATUS_CHECK(rse_graphics_test_function(context));
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,33 @@
|
|||||||
#include "time_utils.h"
|
#include "time_utils.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "SDL3/SDL_timer.h"
|
#include "SDL3/SDL_timer.h"
|
||||||
|
#include "commons.h"
|
||||||
|
#include "errors_common.h"
|
||||||
|
|
||||||
#define NS_TO_S(time) (time/1000000000)
|
#define NS_TO_S(time) (time/1000000000)
|
||||||
|
double g_last_time = 0.0;
|
||||||
|
|
||||||
|
rse_err_t time_init(void)
|
||||||
|
{
|
||||||
|
g_last_time = NS_TO_S((double)SDL_GetTicksNS());
|
||||||
|
|
||||||
|
return RSE_ERROR_NO_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
double time_get_sec_from_start(void)
|
double time_get_sec_from_start(void)
|
||||||
{
|
{
|
||||||
return NS_TO_S((double)SDL_GetTicksNS());
|
return NS_TO_S((double)SDL_GetTicksNS());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void time_update(void)
|
||||||
|
{
|
||||||
|
g_last_time = NS_TO_S((double)SDL_GetTicksNS());
|
||||||
|
}
|
||||||
|
|
||||||
|
double time_get_dt(void)
|
||||||
|
{
|
||||||
|
float time = NS_TO_S((double)SDL_GetTicksNS());
|
||||||
|
float dt = time - g_last_time;
|
||||||
|
|
||||||
|
return dt;
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,6 +12,13 @@
|
|||||||
#ifndef TIME_UTILS_H_
|
#ifndef TIME_UTILS_H_
|
||||||
#define TIME_UTILS_H_
|
#define TIME_UTILS_H_
|
||||||
|
|
||||||
|
#include "commons.h"
|
||||||
|
|
||||||
|
rse_err_t time_init(void);
|
||||||
|
void time_update(void);
|
||||||
|
|
||||||
double time_get_sec_from_start(void);
|
double time_get_sec_from_start(void);
|
||||||
|
|
||||||
|
double time_get_dt(void);
|
||||||
|
|
||||||
#endif // !TIME_UTILS_H_
|
#endif // !TIME_UTILS_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user