]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/threads.cpp
b4471a4a875e2ffe143f497a23fa4a3fb1ea6419
[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 <iostream>
7 #include <algorithm>
8 #include "threads.h"
9 #include "graphics.h"
10 #include "game.h"
11
12
13 using namespace std;
14
15 void Threads::startMapBackupThread(){
16         Game::log("Starting Map Backup Thread");
17         pthread_t thread_id;
18         pthread_create(&thread_id, NULL, &mapBackupThread, NULL);
19 }
20 void *Threads::mapBackupThread(void *unused){
21         while(true){
22                 sleep(MAP_BACKUP_INTERVAL);
23                 Game::map -> save();
24         }
25         return NULL;
26 }
27 void Threads::startGraphicUpdateThread(){
28         Game::log("Starting Graphic Update Thread");
29         pthread_t thread_id;
30         pthread_create(&thread_id, NULL, &graphicUpdateThread, NULL);
31 }
32 void *Threads::graphicUpdateThread(void *unused){
33         while(true){
34                 usleep(1);
35                 if(glutGetWindow())
36                         glutPostRedisplay();
37         }
38         return NULL;
39 }
40 void Threads::addSignalHandlers(){
41 #ifndef _WIN32
42         struct sigaction sa_sigterm;
43         sa_sigterm.sa_handler = &signal_handler;
44         sigaction(SIGTERM, &sa_sigterm, NULL);
45         
46         struct sigaction sa_sigint;
47         sa_sigint.sa_handler = &signal_handler;
48         sigaction(SIGINT, &sa_sigint, NULL);
49 #endif
50 }
51 void Threads::signal_handler(int signal_number){
52 #ifndef _WIN32
53         Game::log((string)"Got "+sys_siglist[signal_number]+" Signal, Exiting.");
54         Game::map -> save();
55         exit(0);
56 #endif  
57 }