twn_timer.c: time limiter for game ticks
This commit is contained in:
parent
16bd49b42e
commit
9ddc5c4a66
@ -68,6 +68,7 @@ add_subdirectory(third-party/libxm ${CMAKE_CURRENT_BINARY_DIR}/third-party/libxm
|
|||||||
if(LINUX)
|
if(LINUX)
|
||||||
set(SYSTEM_SOURCE_FILES
|
set(SYSTEM_SOURCE_FILES
|
||||||
src/system/linux/twn_elf.c
|
src/system/linux/twn_elf.c
|
||||||
|
src/system/linux/twn_timer.c
|
||||||
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:src/game_object/twn_linux_game_object.c>)
|
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:src/game_object/twn_linux_game_object.c>)
|
||||||
elseif(WIN32)
|
elseif(WIN32)
|
||||||
set(SYSTEM_SOURCE_FILES
|
set(SYSTEM_SOURCE_FILES
|
||||||
|
71
src/system/linux/twn_timer.c
Normal file
71
src/system/linux/twn_timer.c
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#include "../twn_timer.h"
|
||||||
|
#include "twn_engine_context_c.h"
|
||||||
|
|
||||||
|
#include <SDL_messagebox.h>
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
|
static timer_t timerid;
|
||||||
|
static sigset_t mask;
|
||||||
|
static struct sigaction sa;
|
||||||
|
static struct sigevent sev;
|
||||||
|
|
||||||
|
static bool created;
|
||||||
|
|
||||||
|
|
||||||
|
/* stop application */
|
||||||
|
static void sanity_timer_handler(int sig, siginfo_t *si, void *uc) {
|
||||||
|
(void)uc; (void)sig; (void)si;
|
||||||
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
||||||
|
"sanity timer", "Your game tick exceeded its allocated time, application is closing",
|
||||||
|
ctx.window);
|
||||||
|
raise(SIGKILL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool start_sanity_timer(uint64_t milliseconds_to_expire) {
|
||||||
|
if (!created) {
|
||||||
|
sa.sa_flags = SA_SIGINFO;
|
||||||
|
sa.sa_sigaction = sanity_timer_handler;
|
||||||
|
sigemptyset(&sa.sa_mask);
|
||||||
|
if (sigaction(SIGRTMIN, &sa, NULL) == -1)
|
||||||
|
goto ERR_SIGACTION;
|
||||||
|
|
||||||
|
sev.sigev_notify = SIGEV_SIGNAL;
|
||||||
|
sev.sigev_signo = SIGRTMIN;
|
||||||
|
sev.sigev_value.sival_ptr = &timerid;
|
||||||
|
if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) == -1)
|
||||||
|
goto ERR_TIMERCREATE;
|
||||||
|
|
||||||
|
created = true;
|
||||||
|
|
||||||
|
ERR_TIMERCREATE:
|
||||||
|
// ERR_SIGPROCMASK:
|
||||||
|
ERR_SIGACTION:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct itimerspec its = {0};
|
||||||
|
its.it_value.tv_sec = milliseconds_to_expire / 1000;
|
||||||
|
its.it_value.tv_nsec = (milliseconds_to_expire * 1000000 % 1000000000);
|
||||||
|
if (timer_settime(timerid, 0, &its, NULL) == -1)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool end_sanity_timer(void) {
|
||||||
|
struct itimerspec its = {0};
|
||||||
|
its.it_value.tv_sec = 0;
|
||||||
|
its.it_value.tv_nsec = 0;
|
||||||
|
if (timer_settime(timerid, 0, &its, NULL) == -1)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
10
src/system/twn_timer.h
Normal file
10
src/system/twn_timer.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef TWN_TIMER_H
|
||||||
|
#define TWN_TIMER_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdbit.h>
|
||||||
|
|
||||||
|
bool start_sanity_timer(uint64_t milliseconds_to_expire);
|
||||||
|
bool end_sanity_timer(void);
|
||||||
|
|
||||||
|
#endif // TWN_TIMER_H
|
@ -5,6 +5,7 @@
|
|||||||
#include "twn_util_c.h"
|
#include "twn_util_c.h"
|
||||||
#include "twn_game_object_c.h"
|
#include "twn_game_object_c.h"
|
||||||
#include "twn_textures_c.h"
|
#include "twn_textures_c.h"
|
||||||
|
#include "system/twn_timer.h"
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <physfs.h>
|
#include <physfs.h>
|
||||||
@ -216,7 +217,9 @@ static void main_loop(void) {
|
|||||||
input_state_update(&ctx.input);
|
input_state_update(&ctx.input);
|
||||||
|
|
||||||
profile_start("game tick");
|
profile_start("game tick");
|
||||||
|
start_sanity_timer(2000);
|
||||||
game_object_tick();
|
game_object_tick();
|
||||||
|
end_sanity_timer();
|
||||||
profile_end("game tick");
|
profile_end("game tick");
|
||||||
|
|
||||||
input_state_update_postframe(&ctx.input);
|
input_state_update_postframe(&ctx.input);
|
||||||
|
Loading…
Reference in New Issue
Block a user