]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/threads.cpp
Upload Files
[dragonblocks-bedrock.git] / src / threads.cpp
1 #include <pthread.h>
2 #include <unistd.h>
3 #include <signal.h>
4 #include <sys/types.h>
5 #include <GL/glut.h>
6 #include "threads.h"
7 #include "graphics.h"
8 #include "game.h"
9 using namespace std;
10 void Threads::add_signal_handlers(){
11         Game::log("Adding Signal Handlers");
12         struct sigaction sa_sigterm;
13         sa_sigterm.sa_handler = &signal_handler;
14         sigaction(SIGTERM, &sa_sigterm, NULL);
15         struct sigaction sa_sigint;
16         sa_sigint.sa_handler = &signal_handler;
17         sigaction(SIGINT, &sa_sigint, NULL);
18 }
19 void Threads::signal_handler(int signal_number){
20         Game::log((string)"Got "+sys_siglist[signal_number]+" Signal, Exiting.");
21         Game::save();
22         exit(0);
23 }
24 void Threads::start(void *(*callback)(void *)){
25         pthread_t thread_id;
26         pthread_create(&thread_id, NULL, callback, NULL);
27 }
28 void *Threads::worldBackupThread(void *unused){
29         Game::log("Starting World Backup");
30         while(true){
31                 sleep(BACKUP_INTERVAL);
32                 Game::save();
33         }
34         return NULL;
35 }
36 void *Threads::graphicRedrawThread(void *unused){
37         Game::log("Starting Graphic Redraw");
38         while(true){
39                 usleep(1);
40                 if(glutGetWindow())
41                         glutPostRedisplay();
42         }
43         return NULL;
44 }
45 void *Threads::modRuntimeThread(void *unused){
46         Game::log("Starting Mod Runtime");
47         while(true){
48                 usleep(10);
49                 // Mods::runtime();
50         }
51         return NULL;
52 }
53 void *Threads::entityPhysicsThread(void *unused){
54         Game::log("Starting Entity Physics");
55         while(true){
56                 usleep(PHYSIC_TIME_TICK);
57                 Entity::t += (double)PHYSIC_TIME_TICK / 1000000;
58                 Entity::physics_all();
59                 Graphics::ajustScroll();
60         }
61         return NULL;
62 }