-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgamethread.cpp
140 lines (117 loc) · 3.37 KB
/
gamethread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "gamethread.h"
#include "events/lua_event.h"
#include "work_queue.h"
#ifdef USE_ANDROID
#include "android_fopen.h"
#endif // USE_ANDROID
#include <pthread.h>
extern "C" {
#include "lauxlib.h"
#include "lualib.h"
#include "lapi.h"
}
#include "lua_util.h"
#include "lualibs/characterlib.h"
#include "lualibs/animationlib.h"
#include "lualibs/lua_promise.h"
#include "lualibs/bitlib.h"
#include "lualibs/gamelib.h"
#include "lualibs/timerlib.h"
#include "lualibs/triggerlib.h"
#include "lualibs/variablelib.h"
static uint8_t thread_shutdown = 1;
static pthread_mutex_t luastate_mutex = PTHREAD_MUTEX_INITIALIZER;
bool getIsShutdown() {
bool isShutdown = false;
pthread_mutex_lock(&luastate_mutex);
isShutdown = thread_shutdown;
pthread_mutex_unlock(&luastate_mutex);
return isShutdown;
}
static void setIsShutdown(uint8_t shutdown) {
pthread_mutex_lock(&luastate_mutex);
thread_shutdown = shutdown;
pthread_mutex_unlock(&luastate_mutex);
return;
}
void * lua_game_thread(void * p_engine) {
setIsShutdown(0);
engine * gameEngine = (engine *) p_engine;
lua_State* L=luaL_newstate();
luaL_openlibs(L);
/**
* Load all the lua libraries
*/
load_character_lib(L, gameEngine);
load_animation_lib(L, gameEngine);
load_bit_lib(L, gameEngine);
load_game_lib(L, gameEngine);
load_timer_lib(L, gameEngine);
load_trigger_lib(L, gameEngine);
load_variable_lib(L, gameEngine);
if (luaL_dofile(L, gameEngine->scriptLocation))
{
ALOGE("Lua error: %s\n", lua_tostring(L, -1));
}
/*
* Pull new out of the game module's new.
*/
lua_getfield(L, -1, "new");
if (!lua_isfunction(L, -1)) {
lua_close(L);
ALOGE("ERROR could not instantiate the game script! You must create a constructor.");
return NULL;
}
/*
* Move the counter module to be the first argument of new.
*/
lua_insert(L, -1);
if (lua_pcall(L, 1, 2, 0) != 0) {
ALOGE("Lua error: %s\n", lua_tostring(L, -1));
lua_close(L);
return NULL;
}
/*
*
* Remove the empty filler nil from the top of the stack. The
* lua_pcall stated 2 return values but on success we only
* get one so we have nil filler after.
*
*/
lua_pop(L, 1);
if (lua_type(L, -1) != LUA_TTABLE) {
ALOGE("Invalid type (%d) returned by new\n", lua_type(L, -1));
lua_close(L);
return NULL;
}
lua_insert(L,-1);
lua_setglobal(L, NYAN_MODULE);
getPromise(L);
while(true) {
LuaEvent * currentEvent = gameEngine->workQueue->getEventBlocking();
currentEvent->runEvent(L);
bool shouldShutdown = currentEvent->getShouldShutdown();
delete currentEvent;
if (shouldShutdown) {
break;
}
}
lua_close(L);
Event * e = NULL;
while ((e = gameEngine->workQueue->getEvent())) {
ALOGE("RELEASING EVENT: %p",e);
delete e;
}
delete gameEngine->workQueue;
gameEngine->workQueue = NULL;
setIsShutdown(1);
return NULL;
}
void start_game_thread(engine * engine) {
pthread_t game_thread;
int pthread_creation_result = pthread_create(&game_thread, NULL, lua_game_thread,(void *) engine);
ALOGE("pthread_creation_result: %d", pthread_creation_result);
if(pthread_creation_result) {
ALOGE("Error creating thread\n");
}
}