Newer
Older
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Nicolas Casalini "DarkGod"
darkgod@te4.org
*/
#include "display.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <sys/time.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "luasocket.h"
#include "luasocket/mime.h"
#include "physfsrwops.h"
#define DEFAULT_IDLE_FPS (2)
#define WINDOW_ICON_PATH ("/engines/default/data/gfx/te4-icon.png")
#define JOY_DEADZONE 0.21
char *override_home = NULL;
int g_argc = 0;
char **g_argv;
float screen_zoom = 1;
dg
committed
SDL_Window *window = NULL;
SDL_Surface *windowIconSurface = NULL;
dg
committed
SDL_GLContext maincontext; /* Our opengl context handle */
SDL_Joystick* gamepad = NULL;
int nb_cpus;
bool safe_mode = FALSE;
bool tickPaused = FALSE;
int mouse_cursor_ox, mouse_cursor_oy;
int mouse_drag_w = 32, mouse_drag_h = 32;
int mouse_drag_tex = 0, mouse_drag_tex_ref = LUA_NOREF;
int mousex = 0, mousey = 0;
/* The currently requested fps for the program */
/* The requested fps for when the program is idle (i.e., doesn't have focus) */
int requested_fps_idle = DEFAULT_IDLE_FPS;
/* The currently "saved" fps, used for idle transitions. */
int requested_fps_idle_saved = 0;
SDL_TimerID display_timer_id = 0;
SDL_TimerID realtime_timer_id = 0;
/* Error handling */
lua_err_type *last_lua_error_head = NULL, *last_lua_error_tail = NULL;
/*
* Locks for thread safety with respect to the rendering and realtime timers.
* The locks are used to control access to each timer's respective id and flag.
*/
SDL_mutex *renderingLock;
SDL_mutex *realtimeLock;
int redraw_pending = 0;
int realtime_pending = 0;
/*
* Used to clean up a lock and its corresponding timer/flag.
*
* @param lock
* The lock which is used by the timer and its event handler.
*
* @param timer
* The id of the timer to clean up.
*
* @param timerFlag
* The flag variable that timer and its events use.
*
*/
static void cleanupTimerLock(SDL_mutex *lock, SDL_TimerID *timer, int *timerFlag);
/*
* Handles transitions to and from idle mode.
*
* A transition is only performed if the game already has a running render timer
* and there is an actual idle->normal or normal->idle transition.
*
* @param goIdle
* Return to normal game rendering speed if zero, go idle otherwise.
*/
static void handleIdleTransition(int goIdle);
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
void del_lua_error()
{
lua_err_type *cur = last_lua_error_head;
while (cur)
{
if (cur->err_msg) free(cur->err_msg);
if (cur->file) free(cur->file);
if (cur->func) free(cur->func);
lua_err_type *ocur = cur;
cur = cur->next;
free(ocur);
}
last_lua_error_head = NULL;
last_lua_error_tail = NULL;
}
static void new_lua_error(const char *err)
{
del_lua_error();
lua_err_type *cur = calloc(1, sizeof(lua_err_type));
cur->err_msg = strdup(err);
cur->next = NULL;
last_lua_error_head = cur;
last_lua_error_tail = cur;
}
static void add_lua_error(const char *file, int line, const char *func)
{
lua_err_type *cur = calloc(1, sizeof(lua_err_type));
cur->err_msg = NULL;
cur->file = strdup(file);
cur->line = line;
cur->func = strdup(func);
cur->next = NULL;
last_lua_error_tail->next = cur;
last_lua_error_tail = cur;
}
printf("Lua Error: %s\n", lua_tostring(L, 1));
while(lua_getstack(L, n++, &ar)) {
lua_getinfo(L, "nSl", &ar);
printf("\tAt %s:%d %s\n", ar.short_src, ar.currentline, ar.name?ar.name:"");
}
// Do it again for the lua error popup, if needed
if (1)
{
n = 0;
new_lua_error(lua_tostring(L, 1));
while(lua_getstack(L, n++, &ar)) {
lua_getinfo(L, "nSl", &ar);
add_lua_error(ar.short_src, ar.currentline, ar.name?ar.name:"");
}
}
fflush(stdout);
void stackDump (lua_State *L) {
int i=lua_gettop(L);
printf(" ---------------- Stack Dump ----------------\n" );
while( i ) {
int t = lua_type(L, i);
switch (t) {
case LUA_TSTRING:
printf("%d:`%s'\n", i, lua_tostring(L, i));
break;
case LUA_TBOOLEAN:
printf("%d: %s\n",i,lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER:
printf("%d: %g\n", i, lua_tonumber(L, i));
break;
default:
#if defined(__PTRDIFF_TYPE__)
if((sizeof(__PTRDIFF_TYPE__) == sizeof(long int)))
{ printf("%d: %s // %lx\n", i, lua_typename(L, t), (unsigned long int)lua_topointer(L, i)); }
{ printf("%d: %s // %x\n", i, lua_typename(L, t), (unsigned int)lua_topointer(L, i)); }
#else
printf("%d: %s // %x\n", i, lua_typename(L, t), lua_topointer(L, i));
#endif
break;
}
i--;
}
printf("--------------- Stack Dump Finished ---------------\n" );
fflush(stdout);
int status;
int base = lua_gettop(L) - narg; /* function index */
lua_pushcfunction(L, traceback); /* push traceback function */
lua_insert(L, base); /* put it under chunk and args */
status = lua_pcall(L, narg, nret, base);
lua_remove(L, base); /* remove traceback function */
/* force a complete garbage collection in case of errors */
if (status != 0) { lua_pop(L, 1); lua_gc(L, LUA_GCCOLLECT, 0); }
// printf(">===%d (%d) [%d]\n", lua_gettop(L), nret, status);
if (lua_gettop(L) != nret + (base - 1))
{
stackDump(L);
// assert(0);
#else
int status=0;
int base = lua_gettop(L) - narg; /* function index */
lua_call(L, narg, nret);
return status;
#endif
/* No print function, does .. nothing */
int noprint(lua_State *L)
{
return 0;
}
// define our data that is passed to our redraw function
typedef struct {
Uint32 color;
} MainStateData;
dg
committed
int event_filter(void *userdata, SDL_Event* event)
{
// Do not allow the user to close without asking the game to know about it
if (event->type == SDL_QUIT && (current_game != LUA_NOREF))
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
lua_pushstring(L, "onQuit");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
docall(L, 1, 0);
return 0;
}
return 1;
}
#define MIN(a,b) ((a < b) ? a : b)
#define MAX(a,b) ((a < b) ? b : a)
dg
committed
extern SDL_Cursor *mouse_cursor;
extern SDL_Cursor *mouse_cursor_down;
bool on_event(SDL_Event *event)
if (current_keyhandler != LUA_NOREF)
{
static Uint32 lastts = 0;
static char lastc = 0;
if (browsers_count) { // Somehow CEF3 makes keys sometime arrive duplicated, so prevent that here
if (event->text.timestamp == lastts) break;
if ((event->text.timestamp - lastts < 3) && (lastc == event->text.text[0])) break;
}
lastts = event->text.timestamp;
lastc = event->text.text[0];
lua_rawgeti(L, LUA_REGISTRYINDEX, current_keyhandler);
lua_pushstring(L, "receiveKey");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_keyhandler);
lua_pushnumber(L, 0);
SDL_Keymod _pKeyState = SDL_GetModState();
lua_pushboolean(L, (_pKeyState & KMOD_CTRL) ? TRUE : FALSE);
lua_pushboolean(L, (_pKeyState & KMOD_SHIFT) ? TRUE : FALSE);
lua_pushboolean(L, (_pKeyState & KMOD_ALT) ? TRUE : FALSE);
lua_pushboolean(L, (_pKeyState & KMOD_GUI) ? TRUE : FALSE);
lua_pushstring(L, event->text.text);
lua_pushboolean(L, FALSE);
lua_pushnil(L);
lua_pushnil(L);
lua_pushnumber(L, 0);
return TRUE;
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_keyhandler);
lua_pushstring(L, "receiveKey");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_keyhandler);
dg
committed
SDL_Keymod _pKeyState = SDL_GetModState();
lua_pushboolean(L, (_pKeyState & KMOD_CTRL) ? TRUE : FALSE);
lua_pushboolean(L, (_pKeyState & KMOD_SHIFT) ? TRUE : FALSE);
lua_pushboolean(L, (_pKeyState & KMOD_ALT) ? TRUE : FALSE);
lua_pushboolean(L, (_pKeyState & KMOD_GUI) ? TRUE : FALSE);
/* Convert unicode UCS-2 to UTF8 string */
if (event->key.keysym.sym)
{
wchar_t wc = event->key.keysym.sym;
char buf[4] = {0,0,0,0};
if (wc < 0x80)
{
buf[0] = wc;
}
else if (wc < 0x800)
{
buf[0] = (0xC0 | wc>>6);
buf[1] = (0x80 | (wc>>6 & 0x3F));
buf[2] = (0x80 | (wc & 0x3F));
}
lua_pushstring(L, buf);
}
else
lua_pushnil(L);
lua_pushnil(L);
lua_pushnumber(L, event->key.keysym.sym);
docall(L, 11, 0);
return TRUE;
dg
committed
if (event->type == SDL_MOUSEBUTTONDOWN) SDL_SetCursor(mouse_cursor_down);
else SDL_SetCursor(mouse_cursor);
if (current_mousehandler != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushstring(L, "receiveMouse");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
switch (event->button.button)
{
case SDL_BUTTON_LEFT:
#if 1
{
SDL_Keymod _pKeyState = SDL_GetModState();
if (_pKeyState & KMOD_ALT) lua_pushstring(L, "right");
else lua_pushstring(L, "left");
}
#else
#endif
break;
case SDL_BUTTON_MIDDLE:
lua_pushstring(L, "middle");
break;
case SDL_BUTTON_RIGHT:
lua_pushstring(L, "right");
break;
default:
lua_pushstring(L, "button");
lua_pushnumber(L, event->button.button);
lua_concat(L, 2);
break;
lua_pushnumber(L, event->button.x / screen_zoom);
lua_pushnumber(L, event->button.y / screen_zoom);
lua_pushboolean(L, (event->type == SDL_MOUSEBUTTONUP) ? TRUE : FALSE);
docall(L, 5, 0);
return TRUE;
dg
committed
case SDL_MOUSEWHEEL:
if (current_mousehandler != LUA_NOREF)
{
int x = 0, y = 0;
SDL_GetMouseState(&x, &y);
int i;
for (i = 0; i <= 1; i++)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushstring(L, "receiveMouse");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
if (event->wheel.y > 0) lua_pushstring(L, "wheelup");
else if (event->wheel.y < 0) lua_pushstring(L, "wheeldown");
else if (event->wheel.x > 0) lua_pushstring(L, "wheelleft");
else if (event->wheel.x < 0) lua_pushstring(L, "wheelright");
else lua_pushstring(L, "wheelnone");
lua_pushnumber(L, x / screen_zoom);
lua_pushnumber(L, y / screen_zoom);
dg
committed
lua_pushboolean(L, i);
docall(L, 5, 0);
}
}
return TRUE;
mousex = event->motion.x / screen_zoom;
mousey = event->motion.y / screen_zoom;
if (current_mousehandler != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushstring(L, "receiveMouseMotion");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
if (event->motion.state & SDL_BUTTON(1)) lua_pushstring(L, "left");
else if (event->motion.state & SDL_BUTTON(2)) lua_pushstring(L, "middle");
else if (event->motion.state & SDL_BUTTON(3)) lua_pushstring(L, "right");
else if (event->motion.state & SDL_BUTTON(4)) lua_pushstring(L, "wheelup");
else if (event->motion.state & SDL_BUTTON(5)) lua_pushstring(L, "wheeldown");
else lua_pushstring(L, "none");
lua_pushnumber(L, event->motion.x / screen_zoom);
lua_pushnumber(L, event->motion.y / screen_zoom);
lua_pushnumber(L, event->motion.xrel);
lua_pushnumber(L, event->motion.yrel);
docall(L, 6, 0);
}
return TRUE;
case SDL_FINGERDOWN:
case SDL_FINGERUP:
if (current_mousehandler != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushstring(L, "receiveTouch");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushnumber(L, event->tfinger.fingerId);
lua_pushnumber(L, event->tfinger.x / screen_zoom);
lua_pushnumber(L, event->tfinger.y / screen_zoom);
lua_pushnumber(L, event->tfinger.dx);
lua_pushnumber(L, event->tfinger.dy);
lua_pushnumber(L, event->tfinger.pressure);
lua_pushboolean(L, (event->type == SDL_FINGERUP) ? TRUE : FALSE);
docall(L, 8, 0);
}
return TRUE;
case SDL_FINGERMOTION:
if (current_mousehandler != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushstring(L, "receiveTouchMotion");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushnumber(L, event->tfinger.fingerId);
lua_pushnumber(L, event->tfinger.x / screen_zoom);
lua_pushnumber(L, event->tfinger.y / screen_zoom);
lua_pushnumber(L, event->tfinger.dx);
lua_pushnumber(L, event->tfinger.dy);
lua_pushnumber(L, event->tfinger.pressure);
docall(L, 7, 0);
}
return TRUE;
case SDL_MULTIGESTURE:
if (current_mousehandler != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushstring(L, "receiveTouchGesture");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushnumber(L, event->mgesture.numFingers);
lua_pushnumber(L, event->mgesture.x / screen_zoom);
lua_pushnumber(L, event->mgesture.y / screen_zoom);
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
lua_pushnumber(L, event->mgesture.dTheta);
lua_pushnumber(L, event->mgesture.dDist);
docall(L, 6, 0);
}
return TRUE;
case SDL_JOYAXISMOTION:
if (current_mousehandler != LUA_NOREF)
{
float v = (float)event->jaxis.value / 32770;
if (v > -JOY_DEADZONE && v < JOY_DEADZONE) return FALSE;
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushstring(L, "receiveJoyAxis");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushnumber(L, event->jaxis.axis);
lua_pushnumber(L, MIN(1, MAX(-1, v)));
docall(L, 3, 0);
}
return TRUE;
case SDL_JOYBALLMOTION:
if (current_mousehandler != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushstring(L, "receiveJoyBall");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushnumber(L, event->jball.ball);
lua_pushnumber(L, event->jball.xrel);
lua_pushnumber(L, event->jball.yrel);
docall(L, 4, 0);
}
return TRUE;
case SDL_JOYHATMOTION:
if (current_mousehandler != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushstring(L, "receiveJoyHat");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_mousehandler);
lua_pushnumber(L, event->jhat.hat);
switch (event->jhat.value) {
case SDL_HAT_UP: lua_pushnumber(L, 8); break;
case SDL_HAT_DOWN: lua_pushnumber(L, 2); break;
case SDL_HAT_LEFT: lua_pushnumber(L, 4); break;
case SDL_HAT_RIGHT: lua_pushnumber(L, 6); break;
case SDL_HAT_LEFTUP: lua_pushnumber(L, 7); break;
case SDL_HAT_LEFTDOWN: lua_pushnumber(L, 1); break;
case SDL_HAT_RIGHTUP: lua_pushnumber(L, 9); break;
case SDL_HAT_RIGHTDOWN: lua_pushnumber(L, 3); break;
}
docall(L, 3, 0);
}
return TRUE;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
if (current_mousehandler != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_keyhandler);
lua_pushstring(L, "receiveJoyButton");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_keyhandler);
lua_pushnumber(L, event->jbutton.button);
lua_pushboolean(L, event->jbutton.state == SDL_RELEASED ? TRUE : FALSE);
docall(L, 3, 0);
}
return TRUE;
return FALSE;
static int Frames = 0;
static int T0 = 0;
lua_pushstring(L, "tick");
lua_gettable(L, -2);
lua_remove(L, -2);
docall(L, 1, 1);
tickPaused = lua_toboolean(L, -1);
/* Gather our frames per second */
Frames++;
{
int t = SDL_GetTicks();
float seconds = (t - T0) / 1000.0;
float fps = Frames / seconds;
// printf("%d ticks in %g seconds = %g TPS\n", Frames, seconds, fps);
void call_draw(int nb_keyframes)
// Notify the particles threads that there are new keyframes
if (!anims_paused) thread_particle_new_keyframes(nb_keyframes);
lua_pushstring(L, "display");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_pushnumber(L, (nb_keyframes < 0) ? 0 : nb_keyframes);
docall(L, 2, 0);
/* Mouse pointer */
GLfloat texcoords[2*4] = {
0, 0,
0, 1,
GLfloat colors[4*4] = {
1, 1, 1, 0.6,
1, 1, 1, 0.6,
1, 1, 1, 0.6,
1, 1, 1, 0.6,
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
glColorPointer(4, GL_FLOAT, 0, colors);
int x = mousex;
int y = mousey;
int w = mouse_drag_w / 2;
int h = mouse_drag_h / 2;
tglBindTexture(GL_TEXTURE_2D, mouse_drag_tex);
GLfloat vertices[2*4] = {
};
glVertexPointer(2, GL_FLOAT, 0, vertices);
glDrawArrays(GL_QUADS, 0, 4);
long total_keyframes = 0;
void on_redraw()
{
static int Frames = 0;
static int T0 = 0;
static float nb_keyframes = 0;
static int last_keyframe = 0;
static float reference_fps = 30;
static int count_keyframes = 0;
if (!is_waiting()) {
int t = SDL_GetTicks();
if (!anims_paused) cur_frame_tick = t - frame_tick_paused_time;
if (t - T0 >= 1000) {
reference_fps = fps;
// printf("%d frames in %g seconds = %g FPS (%d keyframes)\n", Frames, seconds, fps, count_keyframes);
last_keyframe = 0;
nb_keyframes = 0;
count_keyframes = 0;
else
{
// If we are waiting we ignore the fact that we are losing time, this way we never try to "catch up" later
T0 = SDL_GetTicks();
Frames = 0;
last_keyframe = 0;
nb_keyframes = 0;
count_keyframes = 0;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
float step = 30 / reference_fps;
nb_keyframes += step;
int nb = ceilf(nb_keyframes);
count_keyframes += nb - last_keyframe;
total_keyframes += nb - last_keyframe;
// printf("keyframes: %f / %f by %f => %d\n", nb_keyframes, reference_fps, step, nb - (last_keyframe));
call_draw(nb - last_keyframe);
dg
committed
//SDL_GL_SwapBuffers();
SDL_GL_SwapWindow(window);
last_keyframe = nb;
#ifdef STEAM_TE4
#endif
#ifdef DISCORD_TE4
extern void te4_discord_update();
te4_discord_update();
void pass_command_args(int argc, char *argv[])
{
int i;
if (current_game != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
lua_pushstring(L, "commandLineArgs");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
lua_newtable(L);
for (i = 1; i <= argc; i++)
{
lua_pushnumber(L, i);
lua_pushstring(L, argv[i]);
lua_settable(L, -3);
}
docall(L, 2, 0);
}
}
Uint32 redraw_timer(Uint32 interval, void *param)
{
SDL_Event event;
SDL_UserEvent userevent;
/* In this example, our callback pushes an SDL_USEREVENT event
into the queue, and causes ourself to be called again at the
same interval: */
userevent.type = SDL_USEREVENT;
userevent.code = 0;
userevent.data1 = NULL;
userevent.data2 = NULL;
event.type = SDL_USEREVENT;
event.user = userevent;
// Grab the rendering lock and see if a redraw should be requested.
SDL_mutexP(renderingLock);
// If there is no redraw pending, request one. Otherwise, ignore.
if (!redraw_pending && isActive) {
quicksilver
committed
SDL_PushEvent(&event);
redraw_pending = 1;
}
SDL_mutexV(renderingLock);
Uint32 realtime_timer(Uint32 interval, void *param)
{
SDL_Event event;
SDL_UserEvent userevent;
/* In this example, our callback pushes an SDL_USEREVENT event
into the queue, and causes ourself to be called again at the
same interval: */
userevent.type = SDL_USEREVENT;
userevent.code = 2;
userevent.data1 = NULL;
userevent.data2 = NULL;
event.type = SDL_USEREVENT;
event.user = userevent;
// Grab the realtime lock and see if a tick should be requested.
SDL_mutexP(realtimeLock);
// If there is no realtime tick pending, request one. Otherwise, ignore.
if (!realtime_pending && isActive) {
SDL_PushEvent(&event);
realtime_pending = 1;
SDL_mutexV(realtimeLock);
return(interval);
}
// Calls the lua music callback
void on_music_stop()
{
if (current_game != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
lua_pushstring(L, "onMusicStop");
lua_gettable(L, -2);
lua_remove(L, -2);
if (lua_isfunction(L, -1))
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
docall(L, 1, 0);
}
else
lua_pop(L, 1);
}
}
// Setup realtime
void setupRealtime(float freq)
{
SDL_mutexP(realtimeLock);
if (!freq)
{
if (realtime_timer_id) SDL_RemoveTimer(realtime_timer_id);
printf("[ENGINE] Switching to turn based\n");
}
else
{
float interval = 1000 / freq;
if (realtime_timer_id) SDL_RemoveTimer(realtime_timer_id);
realtime_timer_id = SDL_AddTimer((int)interval, realtime_timer, NULL);
printf("[ENGINE] Switching to realtime, interval %d ms\n", (int)interval);
}
SDL_mutexV(realtimeLock);
void setupDisplayTimer(int fps)
{
SDL_mutexP(renderingLock);
if (display_timer_id) SDL_RemoveTimer(display_timer_id);
display_timer_id = SDL_AddTimer(1000 / fps, redraw_timer, NULL);
printf("[ENGINE] Setting requested FPS to %d (%d ms)\n", fps, 1000 / fps);
SDL_mutexV(renderingLock);
}
/* general OpenGL initialization function */
int initGL()
{
/* Set the background black */
glClearDepth( 1.0f );
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
return( TRUE );
}
int resizeWindow(int width, int height)
{
/* Height / width ration */
GLfloat ratio;
/* Protect against a divide by zero */
if ( height == 0 )
height = 1;
ratio = ( GLfloat )width / ( GLfloat )height;
/* Setup our viewport. */
glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );
/* change to the projection matrix and set our viewing volume. */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* Set our perspective */
//gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
glOrtho(0, width / screen_zoom, height / screen_zoom, 0, -1001, 1001);
/* Make sure we're chaning the model view and not the projection */
glMatrixMode( GL_MODELVIEW );
/* Reset The View */
glLoadIdentity( );
//TSDL2 SDL_SetGamma(gamma_correction, gamma_correction, gamma_correction);
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
printf("OpenGL max texture size: %d\n", max_texture_size);
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* @see main.h#resizeNeedsNewWindow */
extern bool resizeNeedsNewWindow(int w, int h, bool fullscreen, bool borderless)
{
/* Note: w and h currently not a factor */
bool newWindowNeeded = window && ( (is_borderless && !borderless)
|| (!is_borderless && borderless) );
return (newWindowNeeded);
}
/* @see main.h#do_move */
void do_move(int w, int h) {
/* Save the origin in case a window needs to be remade later. */
start_xpos = w;
start_ypos = h;
/* Can't move a fullscreen SDL window in one go.*/
if (is_fullscreen) {
/* Drop out of fullscreen so we can move the window. */
SDL_SetWindowFullscreen(window, SDL_FALSE);
}
/* Move the window */
SDL_SetWindowPosition(window, w, h);
/* Jump back into fullscreen if necessary */
if (is_fullscreen) {
if (!SDL_SetWindowFullscreen(window, SDL_TRUE)) {
/* Fullscreen change successful */
is_fullscreen = SDL_TRUE;
} else {
/* Error switching fullscreen mode */
printf("[DO MOVE] Unable to return window"
" to fullscreen mode: %s\n", SDL_GetError());
SDL_ClearError();
}
}