2012-12-23 14:29:54 +01:00
|
|
|
/*! \file main.c
|
|
|
|
* \brief Main file of SuperDerpy engine.
|
2012-02-28 13:09:12 +01:00
|
|
|
*
|
2012-12-23 14:29:54 +01:00
|
|
|
* Contains basic functions shared by all views.
|
2012-02-28 13:09:12 +01:00
|
|
|
*/
|
2012-03-04 13:32:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) Sebastian Krzyszkowiak <dos@dosowisko.net>
|
|
|
|
*
|
|
|
|
* 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
|
2017-07-22 18:22:12 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2012-03-04 13:32:42 +01:00
|
|
|
* (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
|
2017-07-22 18:22:12 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-09-19 01:20:24 +02:00
|
|
|
*
|
|
|
|
* Also, ponies.
|
2012-03-04 13:32:42 +01:00
|
|
|
*/
|
2016-11-06 03:10:43 +01:00
|
|
|
#ifdef LIBSUPERDERPY_MOUSE_EMULATION
|
|
|
|
#define ALLEGRO_UNSTABLE
|
|
|
|
#endif
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
#include "libsuperderpy.h"
|
|
|
|
#include "internal.h"
|
2012-12-26 02:25:56 +01:00
|
|
|
#include <dlfcn.h>
|
2016-07-05 01:30:06 +02:00
|
|
|
#include <libgen.h>
|
2017-09-10 21:35:14 +02:00
|
|
|
#include <locale.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
2016-09-08 00:32:21 +02:00
|
|
|
#include <sys/param.h>
|
2017-09-10 21:35:14 +02:00
|
|
|
#include <unistd.h>
|
2016-07-02 23:23:08 +02:00
|
|
|
#ifdef ALLEGRO_MACOSX
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#endif
|
2012-02-15 23:57:06 +01:00
|
|
|
|
2016-09-08 00:32:21 +02:00
|
|
|
SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char* name, struct Viewport viewport) {
|
2017-09-10 21:35:14 +02:00
|
|
|
struct Game* game = calloc(1, sizeof(struct Game));
|
2012-08-04 21:58:31 +02:00
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
game->name = name;
|
2016-08-23 02:13:15 +02:00
|
|
|
game->viewport_config = viewport;
|
2012-07-03 23:44:03 +02:00
|
|
|
|
2016-07-02 23:23:08 +02:00
|
|
|
#ifdef ALLEGRO_MACOSX
|
2016-09-08 00:32:21 +02:00
|
|
|
getcwd(game->_priv.cwd, MAXPATHLEN);
|
2016-07-02 23:23:08 +02:00
|
|
|
char exe_path[MAXPATHLEN];
|
|
|
|
uint32_t size = sizeof(exe_path);
|
|
|
|
_NSGetExecutablePath(exe_path, &size);
|
2016-07-05 01:30:06 +02:00
|
|
|
chdir(dirname(exe_path));
|
2016-07-02 23:23:08 +02:00
|
|
|
#endif
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!al_init()) {
|
2012-09-03 02:25:32 +02:00
|
|
|
fprintf(stderr, "failed to initialize allegro!\n");
|
2017-08-16 01:47:07 +02:00
|
|
|
free(game);
|
2016-07-03 22:38:36 +02:00
|
|
|
return NULL;
|
2012-09-03 02:25:32 +02:00
|
|
|
}
|
2012-09-03 02:08:12 +02:00
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
InitConfig(game);
|
2012-02-16 12:48:48 +01:00
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
game->_priv.fps_count.frames_done = 0;
|
|
|
|
game->_priv.fps_count.fps = 0;
|
|
|
|
game->_priv.fps_count.old_time = 0;
|
2012-12-23 14:29:54 +01:00
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
game->_priv.font_console = NULL;
|
2016-07-03 22:38:36 +02:00
|
|
|
game->_priv.font_bsod = NULL;
|
2017-09-09 00:11:43 +02:00
|
|
|
game->_priv.console_pos = 0;
|
2017-09-10 21:35:14 +02:00
|
|
|
for (unsigned int i = 0; i < (sizeof(game->_priv.console) / sizeof(game->_priv.console[0])); i++) {
|
2017-09-09 00:11:43 +02:00
|
|
|
game->_priv.console[i][0] = '\0';
|
|
|
|
}
|
2012-12-24 19:41:12 +01:00
|
|
|
|
2016-08-15 04:37:27 +02:00
|
|
|
game->_priv.garbage = NULL;
|
2017-08-07 02:26:36 +02:00
|
|
|
game->_priv.timelines = NULL;
|
2016-08-15 04:37:27 +02:00
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
game->handlers.event = NULL;
|
|
|
|
game->handlers.destroy = NULL;
|
2017-11-23 22:08:05 +01:00
|
|
|
game->handlers.compositor = NULL;
|
2018-02-03 02:13:24 +01:00
|
|
|
game->handlers.prelogic = NULL;
|
|
|
|
game->handlers.postlogic = NULL;
|
|
|
|
game->handlers.predraw = NULL;
|
|
|
|
game->handlers.postdraw = NULL;
|
2016-11-08 22:11:10 +01:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
game->config.fullscreen = strtol(GetConfigOptionDefault(game, "SuperDerpy", "fullscreen", "1"), NULL, 10);
|
|
|
|
game->config.music = strtol(GetConfigOptionDefault(game, "SuperDerpy", "music", "10"), NULL, 10);
|
|
|
|
game->config.voice = strtol(GetConfigOptionDefault(game, "SuperDerpy", "voice", "10"), NULL, 10);
|
|
|
|
game->config.fx = strtol(GetConfigOptionDefault(game, "SuperDerpy", "fx", "10"), NULL, 10);
|
|
|
|
game->config.debug = strtol(GetConfigOptionDefault(game, "SuperDerpy", "debug", "0"), NULL, 10);
|
|
|
|
game->config.width = strtol(GetConfigOptionDefault(game, "SuperDerpy", "width", "1280"), NULL, 10);
|
|
|
|
if (game->config.width < 320) { game->config.width = 320; }
|
|
|
|
game->config.height = strtol(GetConfigOptionDefault(game, "SuperDerpy", "height", "720"), NULL, 10);
|
|
|
|
if (game->config.height < 180) { game->config.height = 180; }
|
2012-02-26 12:59:45 +01:00
|
|
|
|
2016-08-16 18:01:12 +02:00
|
|
|
game->_priv.showconsole = game->config.debug;
|
2017-08-07 02:26:36 +02:00
|
|
|
game->_priv.showtimeline = false;
|
2016-08-16 18:01:12 +02:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!al_init_image_addon()) {
|
2012-02-25 22:26:31 +01:00
|
|
|
fprintf(stderr, "failed to initialize image addon!\n");
|
2012-04-14 22:26:33 +02:00
|
|
|
/*al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!",
|
2016-07-02 23:23:08 +02:00
|
|
|
NULL, ALLEGRO_MESSAGEBOX_ERROR);*/
|
2016-07-03 22:38:36 +02:00
|
|
|
return NULL;
|
2012-02-25 22:26:31 +01:00
|
|
|
}
|
2012-02-15 23:57:06 +01:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!al_install_audio()) {
|
2017-08-19 01:27:34 +02:00
|
|
|
fprintf(stderr, "failed to initialize audio!\n");
|
2016-07-03 22:38:36 +02:00
|
|
|
return NULL;
|
2012-02-25 22:26:31 +01:00
|
|
|
}
|
2012-02-19 19:17:04 +01:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!al_init_acodec_addon()) {
|
2017-08-19 01:27:34 +02:00
|
|
|
fprintf(stderr, "failed to initialize audio codecs!\n");
|
2016-07-03 22:38:36 +02:00
|
|
|
return NULL;
|
2012-02-25 22:26:31 +01:00
|
|
|
}
|
2012-02-15 23:57:06 +01:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!al_install_keyboard()) {
|
2012-02-25 22:26:31 +01:00
|
|
|
fprintf(stderr, "failed to initialize keyboard!\n");
|
2016-07-03 22:38:36 +02:00
|
|
|
return NULL;
|
2012-02-25 22:26:31 +01:00
|
|
|
}
|
2012-02-15 23:57:06 +01:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!al_init_primitives_addon()) {
|
2012-05-09 10:58:45 +02:00
|
|
|
fprintf(stderr, "failed to initialize primitives!\n");
|
2016-07-03 22:38:36 +02:00
|
|
|
return NULL;
|
2012-05-09 10:58:45 +02:00
|
|
|
}
|
2016-06-27 21:21:59 +02:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!al_install_mouse()) {
|
2016-07-02 23:23:08 +02:00
|
|
|
fprintf(stderr, "failed to initialize the mouse!\n");
|
2016-07-03 22:38:36 +02:00
|
|
|
return NULL;
|
2016-07-02 23:23:08 +02:00
|
|
|
}
|
|
|
|
|
2012-02-25 22:26:31 +01:00
|
|
|
al_init_font_addon();
|
2012-02-15 23:57:06 +01:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!al_init_ttf_addon()) {
|
2012-02-25 22:26:31 +01:00
|
|
|
fprintf(stderr, "failed to initialize fonts!\n");
|
2016-07-03 22:38:36 +02:00
|
|
|
return NULL;
|
2012-02-25 22:26:31 +01:00
|
|
|
}
|
2012-02-16 12:48:48 +01:00
|
|
|
|
2016-11-08 22:11:10 +01:00
|
|
|
game->touch = false;
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!strtol(GetConfigOptionDefault(game, "SuperDerpy", "disableTouch", "0"), NULL, 10)) {
|
2016-11-08 22:11:10 +01:00
|
|
|
game->touch = al_install_touch_input();
|
|
|
|
}
|
2016-11-06 03:10:43 +01:00
|
|
|
|
|
|
|
#ifdef LIBSUPERDERPY_MOUSE_EMULATION
|
2016-11-08 22:11:10 +01:00
|
|
|
if (game->touch) {
|
2016-11-08 17:56:19 +01:00
|
|
|
al_set_mouse_emulation_mode(ALLEGRO_MOUSE_EMULATION_TRANSPARENT);
|
|
|
|
}
|
2016-11-06 03:10:43 +01:00
|
|
|
#endif
|
|
|
|
|
2016-12-05 23:56:41 +01:00
|
|
|
al_install_joystick();
|
|
|
|
|
2018-02-03 02:13:24 +01:00
|
|
|
al_set_new_display_flags((game->config.fullscreen ? (ALLEGRO_FULLSCREEN_WINDOW | ALLEGRO_FRAMELESS) : ALLEGRO_WINDOWED) | ALLEGRO_RESIZABLE | ALLEGRO_OPENGL_3_0 | ALLEGRO_PROGRAMMABLE_PIPELINE);
|
2017-08-22 21:54:58 +02:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
al_set_new_display_flags((al_get_new_display_flags() | ALLEGRO_WINDOWED) ^ ALLEGRO_FULLSCREEN_WINDOW);
|
|
|
|
#endif
|
2017-09-10 21:35:14 +02:00
|
|
|
al_set_new_display_option(ALLEGRO_VSYNC, 2 - strtol(GetConfigOptionDefault(game, "SuperDerpy", "vsync", "1"), NULL, 10), ALLEGRO_SUGGEST);
|
2017-06-26 03:18:54 +02:00
|
|
|
|
|
|
|
#ifdef LIBSUPERDERPY_ORIENTATION_LANDSCAPE
|
2017-07-22 21:01:29 +02:00
|
|
|
al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS, ALLEGRO_DISPLAY_ORIENTATION_LANDSCAPE, ALLEGRO_SUGGEST);
|
2017-06-26 03:18:54 +02:00
|
|
|
#elif defined(LIBSUPERDERPY_ORIENTATION_PORTRAIT)
|
|
|
|
al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS, ALLEGRO_DISPLAY_ORIENTATION_PORTRAIT, ALLEGRO_SUGGEST);
|
|
|
|
#endif
|
|
|
|
|
2018-02-03 02:13:24 +01:00
|
|
|
al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_SUGGEST);
|
|
|
|
|
2016-06-27 21:20:02 +02:00
|
|
|
#ifdef ALLEGRO_WINDOWS
|
|
|
|
al_set_new_window_position(20, 40); // workaround nasty Windows bug with window being created off-screen
|
|
|
|
#endif
|
2012-05-10 20:02:22 +02:00
|
|
|
|
2016-08-23 02:13:15 +02:00
|
|
|
al_set_new_window_title(game->name);
|
2016-07-03 22:38:36 +02:00
|
|
|
game->display = al_create_display(game->config.width, game->config.height);
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!game->display) {
|
2018-02-03 02:13:24 +01:00
|
|
|
fprintf(stderr, "Failed to create display!\n");
|
2016-07-03 22:38:36 +02:00
|
|
|
return NULL;
|
2012-02-25 22:26:31 +01:00
|
|
|
}
|
2012-12-24 19:41:12 +01:00
|
|
|
|
2016-11-06 03:10:43 +01:00
|
|
|
#ifdef ALLEGRO_ANDROID
|
|
|
|
al_android_set_apk_file_interface();
|
|
|
|
al_android_set_apk_fs_interface();
|
|
|
|
#endif
|
|
|
|
|
2016-08-23 02:13:15 +02:00
|
|
|
SetupViewport(game, viewport);
|
2012-12-28 02:55:52 +01:00
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
PrintConsole(game, "Viewport %dx%d", game->viewport.width, game->viewport.height);
|
2018-02-03 02:13:24 +01:00
|
|
|
PrintConsole(game, "Max bitmap size: %d", al_get_display_option(game->display, ALLEGRO_MAX_BITMAP_SIZE));
|
|
|
|
PrintConsole(game, "Color buffer bits: %d", al_get_display_option(game->display, ALLEGRO_COLOR_SIZE));
|
|
|
|
PrintConsole(game, "Depth buffer bits: %d", al_get_display_option(game->display, ALLEGRO_DEPTH_SIZE));
|
|
|
|
PrintConsole(game, "Stencil buffer bits: %d", al_get_display_option(game->display, ALLEGRO_STENCIL_SIZE));
|
|
|
|
PrintConsole(game, "NPOT bitmaps: %d", al_get_display_option(game->display, ALLEGRO_SUPPORT_NPOT_BITMAP));
|
|
|
|
PrintConsole(game, "Separate alpha: %d", al_get_display_option(game->display, ALLEGRO_SUPPORT_SEPARATE_ALPHA));
|
|
|
|
|
|
|
|
if (!al_get_display_option(game->display, ALLEGRO_COMPATIBLE_DISPLAY)) {
|
|
|
|
al_destroy_display(game->display);
|
|
|
|
fprintf(stderr, "Created display is Allegro incompatible!\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!al_get_display_option(game->display, ALLEGRO_CAN_DRAW_INTO_BITMAP)) {
|
|
|
|
FatalError(game, true, "The created display does not support drawing into bitmaps.");
|
|
|
|
al_destroy_display(game->display);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!al_get_display_option(game->display, ALLEGRO_RENDER_METHOD)) {
|
|
|
|
FatalError(game, true, "Failed to create hardware accelerated display.");
|
|
|
|
al_destroy_display(game->display);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintConsole(game, "libsuperderpy display created :)");
|
2012-12-28 02:55:52 +01:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
ALLEGRO_BITMAP* icon = al_load_bitmap(GetDataFilePath(game, GetGameName(game, "icons/%s.png")));
|
2016-07-03 22:38:36 +02:00
|
|
|
al_set_display_icon(game->display, icon);
|
2012-09-19 01:20:24 +02:00
|
|
|
al_destroy_bitmap(icon);
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if (game->config.fullscreen) { al_hide_mouse_cursor(game->display); }
|
2016-07-02 23:23:08 +02:00
|
|
|
al_inhibit_screensaver(true);
|
2012-02-29 23:00:59 +01:00
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
al_add_new_bitmap_flag(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR);
|
2012-02-29 23:00:59 +01:00
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
game->_priv.gamestates = NULL;
|
2016-08-16 18:41:50 +02:00
|
|
|
game->_priv.gamestate_scheduled = false;
|
2012-12-25 00:22:03 +01:00
|
|
|
|
2016-08-29 22:47:55 +02:00
|
|
|
al_init_user_event_source(&(game->event_source));
|
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
game->_priv.event_queue = al_create_event_queue();
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!game->_priv.event_queue) {
|
2016-07-03 22:38:36 +02:00
|
|
|
FatalError(game, true, "Failed to create event queue.");
|
|
|
|
al_destroy_display(game->display);
|
|
|
|
return NULL;
|
2012-02-25 22:26:31 +01:00
|
|
|
}
|
2012-02-15 23:57:06 +01:00
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
ALLEGRO_AUDIO_DEPTH depth = ALLEGRO_AUDIO_DEPTH_FLOAT32;
|
|
|
|
#ifdef ALLEGRO_ANDROID
|
|
|
|
depth = ALLEGRO_AUDIO_DEPTH_INT16;
|
|
|
|
#endif
|
|
|
|
game->audio.v = al_create_voice(44100, depth, ALLEGRO_CHANNEL_CONF_2);
|
2017-09-05 03:01:18 +02:00
|
|
|
if (!game->audio.v) {
|
2017-09-09 00:11:43 +02:00
|
|
|
// fallback
|
|
|
|
depth = (depth == ALLEGRO_AUDIO_DEPTH_FLOAT32) ? ALLEGRO_AUDIO_DEPTH_INT16 : ALLEGRO_AUDIO_DEPTH_FLOAT32;
|
|
|
|
game->audio.v = al_create_voice(44100, depth, ALLEGRO_CHANNEL_CONF_2);
|
2017-09-05 03:01:18 +02:00
|
|
|
}
|
2016-07-03 22:38:36 +02:00
|
|
|
game->audio.mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
|
|
|
|
game->audio.fx = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
|
|
|
|
game->audio.music = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
|
|
|
|
game->audio.voice = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
|
|
|
|
al_attach_mixer_to_voice(game->audio.mixer, game->audio.v);
|
|
|
|
al_attach_mixer_to_mixer(game->audio.fx, game->audio.mixer);
|
|
|
|
al_attach_mixer_to_mixer(game->audio.music, game->audio.mixer);
|
|
|
|
al_attach_mixer_to_mixer(game->audio.voice, game->audio.mixer);
|
2017-09-10 21:35:14 +02:00
|
|
|
al_set_mixer_gain(game->audio.fx, game->config.fx / 10.0);
|
|
|
|
al_set_mixer_gain(game->audio.music, game->config.music / 10.0);
|
|
|
|
al_set_mixer_gain(game->audio.voice, game->config.voice / 10.0);
|
2016-07-03 22:38:36 +02:00
|
|
|
|
|
|
|
setlocale(LC_NUMERIC, "C");
|
2016-06-27 21:20:02 +02:00
|
|
|
|
2016-07-04 00:06:50 +02:00
|
|
|
game->_priv.argc = argc;
|
|
|
|
game->_priv.argv = argv;
|
2012-02-24 13:08:44 +01:00
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
game->data = NULL;
|
2012-05-07 22:44:39 +02:00
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
game->shutting_down = false;
|
2016-07-03 22:38:36 +02:00
|
|
|
game->restart = false;
|
2012-12-24 19:41:12 +01:00
|
|
|
|
2016-08-20 03:32:32 +02:00
|
|
|
game->show_loading_on_launch = false;
|
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
game->loading_progress = 0;
|
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
return game;
|
|
|
|
}
|
2012-04-09 16:41:10 +02:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT int libsuperderpy_run(struct Game* game) {
|
2016-07-03 22:38:36 +02:00
|
|
|
al_register_event_source(game->_priv.event_queue, al_get_display_event_source(game->display));
|
|
|
|
al_register_event_source(game->_priv.event_queue, al_get_mouse_event_source());
|
|
|
|
al_register_event_source(game->_priv.event_queue, al_get_keyboard_event_source());
|
2016-12-05 23:56:41 +01:00
|
|
|
al_register_event_source(game->_priv.event_queue, al_get_joystick_event_source());
|
2016-11-08 22:11:10 +01:00
|
|
|
if (game->touch) {
|
2016-11-08 17:56:19 +01:00
|
|
|
al_register_event_source(game->_priv.event_queue, al_get_touch_input_event_source());
|
2016-11-06 03:10:43 +01:00
|
|
|
#ifdef LIBSUPERDERPY_MOUSE_EMULATION
|
2016-11-08 17:56:19 +01:00
|
|
|
al_register_event_source(game->_priv.event_queue, al_get_touch_input_mouse_emulation_event_source());
|
2016-11-06 03:10:43 +01:00
|
|
|
#endif
|
2016-11-08 17:56:19 +01:00
|
|
|
}
|
2016-08-29 22:47:55 +02:00
|
|
|
al_register_event_source(game->_priv.event_queue, &(game->event_source));
|
2012-04-09 16:41:10 +02:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
al_clear_to_color(al_map_rgb(0, 0, 0));
|
2016-07-03 22:38:36 +02:00
|
|
|
game->_priv.timer = al_create_timer(ALLEGRO_BPS_TO_SECS(60)); // logic timer
|
2017-09-10 21:35:14 +02:00
|
|
|
if (!game->_priv.timer) {
|
2016-07-03 22:38:36 +02:00
|
|
|
FatalError(game, true, "Failed to create logic timer.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
al_register_event_source(game->_priv.event_queue, al_get_timer_event_source(game->_priv.timer));
|
|
|
|
|
|
|
|
al_flip_display();
|
|
|
|
al_start_timer(game->_priv.timer);
|
|
|
|
|
2017-09-10 22:07:02 +02:00
|
|
|
{
|
|
|
|
struct Gamestate* tmp = game->_priv.gamestates;
|
|
|
|
while (tmp) {
|
|
|
|
// don't show loading screen on init if requested
|
|
|
|
tmp->showLoading = game->show_loading_on_launch;
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
2016-07-03 22:38:36 +02:00
|
|
|
}
|
2012-12-24 19:41:12 +01:00
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
game->_priv.loading.gamestate = AllocateGamestate(game, "loading");
|
|
|
|
if (!OpenGamestate(game, game->_priv.loading.gamestate) || !LinkGamestate(game, game->_priv.loading.gamestate)) {
|
|
|
|
// TODO: support loading-less scenario
|
2016-07-03 22:38:36 +02:00
|
|
|
return 2;
|
2012-12-28 03:42:59 +01:00
|
|
|
}
|
2017-09-09 00:11:43 +02:00
|
|
|
game->_priv.loading.gamestate->data = (*game->_priv.loading.gamestate->api->Gamestate_Load)(game, NULL);
|
|
|
|
PrintConsole(game, "Loading screen registered.");
|
2012-12-28 03:42:59 +01:00
|
|
|
|
2017-11-23 22:08:05 +01:00
|
|
|
game->_priv.timestamp = al_get_time();
|
2016-11-06 03:10:43 +01:00
|
|
|
game->_priv.draw = true;
|
2017-08-22 21:54:58 +02:00
|
|
|
#ifdef __EMSCRIPTEN__
|
2017-09-10 21:35:14 +02:00
|
|
|
void libsuperderpy_mainloop(void* game);
|
2017-08-22 21:54:58 +02:00
|
|
|
emscripten_set_main_loop_arg(libsuperderpy_mainloop, game, 0, true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool redraw = false;
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_INTERNAL void libsuperderpy_mainloop_exit(struct Game* game) {
|
2017-08-22 21:54:58 +02:00
|
|
|
libsuperderpy_destroy(game);
|
|
|
|
free(game);
|
|
|
|
printf("Halted.\n");
|
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_INTERNAL void libsuperderpy_mainloop(void* g) {
|
|
|
|
struct Game* game = (struct Game*)g;
|
2017-12-15 01:49:07 +01:00
|
|
|
redraw = true;
|
2017-08-22 21:54:58 +02:00
|
|
|
while (!al_is_event_queue_empty(game->_priv.event_queue) || redraw) {
|
|
|
|
#else
|
|
|
|
bool redraw = false;
|
|
|
|
while (1) {
|
|
|
|
#endif
|
2016-11-08 22:11:10 +01:00
|
|
|
ClearGarbage(game);
|
|
|
|
|
2016-09-08 00:32:21 +02:00
|
|
|
// TODO: split mainloop to functions to make it readable
|
2012-02-25 22:26:31 +01:00
|
|
|
ALLEGRO_EVENT ev;
|
2017-11-23 22:08:05 +01:00
|
|
|
if (game->_priv.draw && (((redraw || true) && al_is_event_queue_empty(game->_priv.event_queue)) || (game->_priv.gamestate_scheduled))) {
|
2016-08-16 18:41:50 +02:00
|
|
|
game->_priv.gamestate_scheduled = false;
|
2017-09-10 21:35:14 +02:00
|
|
|
struct Gamestate* tmp = game->_priv.gamestates;
|
2016-08-13 18:01:46 +02:00
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
game->_priv.loading.toLoad = 0;
|
|
|
|
game->_priv.loading.loaded = 0;
|
|
|
|
game->loading_progress = 0;
|
2012-12-28 00:00:37 +01:00
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
// TODO: support gamestate dependences/ordering
|
2012-12-25 00:22:03 +01:00
|
|
|
while (tmp) {
|
2016-08-13 20:03:58 +02:00
|
|
|
if (tmp->pending_stop) {
|
2016-07-03 22:38:36 +02:00
|
|
|
PrintConsole(game, "Stopping gamestate \"%s\"...", tmp->name);
|
2016-08-16 18:41:50 +02:00
|
|
|
game->_priv.current_gamestate = tmp;
|
2016-08-20 03:02:39 +02:00
|
|
|
(*tmp->api->Gamestate_Stop)(game, tmp->data);
|
2012-12-26 20:02:40 +01:00
|
|
|
tmp->started = false;
|
2016-08-13 20:03:58 +02:00
|
|
|
tmp->pending_stop = false;
|
2012-12-27 21:05:28 +01:00
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if (tmp->pending_load) { game->_priv.loading.toLoad++; }
|
|
|
|
tmp = tmp->next;
|
2012-12-27 21:05:28 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
tmp = game->_priv.gamestates;
|
2012-12-28 00:00:37 +01:00
|
|
|
|
2012-12-27 21:05:28 +01:00
|
|
|
while (tmp) {
|
2016-08-13 20:03:58 +02:00
|
|
|
if (tmp->pending_unload) {
|
2016-07-03 22:38:36 +02:00
|
|
|
PrintConsole(game, "Unloading gamestate \"%s\"...", tmp->name);
|
|
|
|
al_stop_timer(game->_priv.timer);
|
2012-12-26 20:02:40 +01:00
|
|
|
tmp->loaded = false;
|
2016-08-13 20:03:58 +02:00
|
|
|
tmp->pending_unload = false;
|
2016-08-16 18:41:50 +02:00
|
|
|
game->_priv.current_gamestate = tmp;
|
2016-08-20 03:02:39 +02:00
|
|
|
(*tmp->api->Gamestate_Unload)(game, tmp->data);
|
2016-08-21 21:58:47 +02:00
|
|
|
al_resume_timer(game->_priv.timer);
|
2016-08-13 20:03:58 +02:00
|
|
|
}
|
|
|
|
if (tmp->pending_load) {
|
2016-07-03 22:38:36 +02:00
|
|
|
al_stop_timer(game->_priv.timer);
|
2017-09-10 21:35:14 +02:00
|
|
|
if (tmp->showLoading) {
|
|
|
|
(*game->_priv.loading.gamestate->api->Gamestate_Start)(game, game->_priv.loading.gamestate->data);
|
|
|
|
}
|
2012-12-28 02:55:52 +01:00
|
|
|
|
2016-08-20 03:02:39 +02:00
|
|
|
if (!tmp->api) {
|
2017-09-09 00:11:43 +02:00
|
|
|
if (!OpenGamestate(game, tmp) || !LinkGamestate(game, tmp)) {
|
2016-08-20 03:02:39 +02:00
|
|
|
tmp->pending_load = false;
|
|
|
|
tmp->pending_start = false;
|
2017-09-09 00:11:43 +02:00
|
|
|
tmp->next = tmp;
|
|
|
|
continue;
|
2016-08-20 03:02:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tmp->api) {
|
|
|
|
PrintConsole(game, "Loading gamestate \"%s\"...", tmp->name);
|
2017-09-09 00:11:43 +02:00
|
|
|
game->_priv.loading.progress = -1;
|
2012-12-28 00:00:37 +01:00
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
game->_priv.loading.current = tmp;
|
|
|
|
game->_priv.current_gamestate = tmp;
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
struct GamestateLoadingThreadData data = {.game = game, .gamestate = tmp, .bitmap_flags = al_get_new_bitmap_flags()};
|
2017-09-09 00:11:43 +02:00
|
|
|
game->_priv.loading.inProgress = true;
|
|
|
|
|
|
|
|
#ifndef LIBSUPERDERPY_SINGLE_THREAD
|
|
|
|
al_run_detached_thread(GamestateLoadingThread, &data);
|
|
|
|
while (game->_priv.loading.inProgress) {
|
|
|
|
DrawGamestates(game);
|
2018-02-03 02:13:24 +01:00
|
|
|
al_set_target_backbuffer(game->display);
|
2017-09-10 21:35:14 +02:00
|
|
|
if (tmp->showLoading) {
|
|
|
|
(*game->_priv.loading.gamestate->api->Gamestate_Draw)(game, game->_priv.loading.gamestate->data);
|
|
|
|
}
|
2017-09-09 00:11:43 +02:00
|
|
|
DrawConsole(game);
|
2016-06-27 21:21:59 +02:00
|
|
|
al_flip_display();
|
|
|
|
}
|
2017-09-09 00:11:43 +02:00
|
|
|
#else
|
|
|
|
GamestateLoadingThread(&data);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
al_set_new_bitmap_flags(data.bitmap_flags);
|
|
|
|
// TODO: compile shaders
|
|
|
|
al_convert_memory_bitmaps();
|
|
|
|
game->_priv.loading.loaded++;
|
2012-12-26 02:25:56 +01:00
|
|
|
|
|
|
|
tmp->loaded = true;
|
|
|
|
tmp->pending_load = false;
|
|
|
|
}
|
2017-09-10 21:35:14 +02:00
|
|
|
if (tmp->showLoading) {
|
|
|
|
(*game->_priv.loading.gamestate->api->Gamestate_Stop)(game, game->_priv.loading.gamestate->data);
|
|
|
|
}
|
2016-08-21 21:58:47 +02:00
|
|
|
al_resume_timer(game->_priv.timer);
|
2017-11-23 22:08:05 +01:00
|
|
|
game->_priv.timestamp = al_get_time();
|
2012-12-25 00:22:03 +01:00
|
|
|
}
|
2012-12-26 13:38:15 +01:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
tmp = tmp->next;
|
2012-12-27 21:05:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool gameActive = false;
|
2017-09-10 21:35:14 +02:00
|
|
|
tmp = game->_priv.gamestates;
|
2012-12-27 21:05:28 +01:00
|
|
|
|
|
|
|
while (tmp) {
|
2017-09-10 21:35:14 +02:00
|
|
|
if ((tmp->pending_start) && (tmp->loaded)) {
|
2016-07-03 22:38:36 +02:00
|
|
|
PrintConsole(game, "Starting gamestate \"%s\"...", tmp->name);
|
|
|
|
al_stop_timer(game->_priv.timer);
|
2016-08-16 18:41:50 +02:00
|
|
|
game->_priv.current_gamestate = tmp;
|
2012-12-27 21:05:28 +01:00
|
|
|
tmp->started = true;
|
|
|
|
tmp->pending_start = false;
|
2016-09-04 01:26:41 +02:00
|
|
|
(*tmp->api->Gamestate_Start)(game, tmp->data);
|
|
|
|
al_resume_timer(game->_priv.timer);
|
2012-12-27 21:05:28 +01:00
|
|
|
}
|
2012-12-26 13:38:15 +01:00
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
if ((tmp->started) || (tmp->pending_start) || (tmp->pending_load)) {
|
|
|
|
gameActive = true;
|
|
|
|
}
|
|
|
|
tmp = tmp->next;
|
2012-12-25 00:22:03 +01:00
|
|
|
}
|
2012-12-24 19:41:12 +01:00
|
|
|
|
2012-12-26 13:38:15 +01:00
|
|
|
if (!gameActive) {
|
2016-07-03 22:38:36 +02:00
|
|
|
PrintConsole(game, "No gamestates left, exiting...");
|
2017-08-22 21:54:58 +02:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
libsuperderpy_mainloop_exit(game);
|
|
|
|
#endif
|
2012-12-26 13:38:15 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
al_convert_memory_bitmaps();
|
|
|
|
|
2017-11-23 22:08:05 +01:00
|
|
|
double delta = al_get_time() - game->_priv.timestamp;
|
|
|
|
game->_priv.timestamp += delta;
|
|
|
|
delta /= ALLEGRO_BPS_TO_SECS(al_get_timer_speed(game->_priv.timer) / (1 / 60.f));
|
|
|
|
LogicGamestates(game, delta);
|
|
|
|
redraw = true;
|
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
DrawGamestates(game);
|
|
|
|
DrawConsole(game);
|
2018-02-03 02:13:24 +01:00
|
|
|
//al_wait_for_vsync();
|
2012-02-25 22:19:12 +01:00
|
|
|
al_flip_display();
|
2012-12-25 00:22:03 +01:00
|
|
|
redraw = false;
|
2012-12-24 19:41:12 +01:00
|
|
|
|
2012-09-26 03:19:10 +02:00
|
|
|
} else {
|
2017-08-22 21:54:58 +02:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
if (al_is_event_queue_empty(game->_priv.event_queue)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
al_wait_for_event(game->_priv.event_queue, &ev);
|
2012-12-24 19:41:12 +01:00
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
if (game->handlers.event) {
|
|
|
|
if ((*game->handlers.event)(game, &ev)) {
|
2016-11-08 22:11:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->_priv.timer)) {
|
2017-11-23 22:08:05 +01:00
|
|
|
/* double delta = al_get_time() - game->_priv.timestamp;
|
|
|
|
game->_priv.timestamp += delta;
|
|
|
|
LogicGamestates(game, delta);
|
|
|
|
redraw = true;*/
|
2017-09-10 21:35:14 +02:00
|
|
|
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
|
2017-08-22 21:54:58 +02:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
libsuperderpy_mainloop_exit(game);
|
|
|
|
#endif
|
2012-09-26 03:19:10 +02:00
|
|
|
break;
|
2017-09-10 21:35:14 +02:00
|
|
|
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_HALT_DRAWING) {
|
2016-11-11 19:38:26 +01:00
|
|
|
PrintConsole(game, "Engine halted.");
|
2016-11-06 03:10:43 +01:00
|
|
|
game->_priv.draw = false;
|
2016-11-08 10:34:32 +01:00
|
|
|
al_stop_timer(game->_priv.timer);
|
2016-11-06 03:10:43 +01:00
|
|
|
al_detach_voice(game->audio.v);
|
2016-11-09 00:40:13 +01:00
|
|
|
FreezeGamestates(game);
|
2016-11-06 03:10:43 +01:00
|
|
|
al_acknowledge_drawing_halt(game->display);
|
2017-09-10 21:35:14 +02:00
|
|
|
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING) {
|
2016-11-08 17:42:23 +01:00
|
|
|
game->_priv.draw = true;
|
2016-11-11 19:38:26 +01:00
|
|
|
al_acknowledge_drawing_resume(game->display);
|
|
|
|
PrintConsole(game, "Engine resumed.");
|
|
|
|
ReloadGamestates(game);
|
2016-11-09 00:40:13 +01:00
|
|
|
UnfreezeGamestates(game);
|
2016-11-08 17:42:23 +01:00
|
|
|
al_attach_mixer_to_voice(game->audio.mixer, game->audio.v);
|
|
|
|
al_resume_timer(game->_priv.timer);
|
2017-09-10 21:35:14 +02:00
|
|
|
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
|
2017-12-15 01:49:07 +01:00
|
|
|
al_acknowledge_resize(game->display);
|
2016-11-11 19:38:26 +01:00
|
|
|
SetupViewport(game, game->viewport_config);
|
2017-11-23 22:08:05 +01:00
|
|
|
ResizeGamestates(game);
|
2016-09-02 00:07:43 +02:00
|
|
|
}
|
2017-06-25 17:37:41 +02:00
|
|
|
#ifdef ALLEGRO_ANDROID
|
2017-09-09 02:46:07 +02:00
|
|
|
else if ((ev.type == ALLEGRO_EVENT_KEY_CHAR) && ((ev.keyboard.keycode == ALLEGRO_KEY_MENU) || (ev.keyboard.keycode == ALLEGRO_KEY_TILDE) || (ev.keyboard.keycode == ALLEGRO_KEY_BACKQUOTE))) {
|
2016-06-27 21:20:02 +02:00
|
|
|
#else
|
2017-08-07 02:26:36 +02:00
|
|
|
else if ((ev.type == ALLEGRO_EVENT_KEY_CHAR) && ((ev.keyboard.keycode == ALLEGRO_KEY_TILDE) || (ev.keyboard.keycode == ALLEGRO_KEY_BACKQUOTE))) {
|
2016-06-27 21:20:02 +02:00
|
|
|
#endif
|
2016-07-03 22:38:36 +02:00
|
|
|
game->_priv.showconsole = !game->_priv.showconsole;
|
2017-08-07 02:26:36 +02:00
|
|
|
if (ev.keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) {
|
|
|
|
game->_priv.showtimeline = game->_priv.showconsole;
|
|
|
|
}
|
2017-09-10 21:35:14 +02:00
|
|
|
} else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (game->config.debug) && (ev.keyboard.keycode == ALLEGRO_KEY_F1)) {
|
2012-12-27 01:02:47 +01:00
|
|
|
int i;
|
2017-09-10 21:35:14 +02:00
|
|
|
for (i = 0; i < 512; i++) {
|
2017-11-23 22:08:05 +01:00
|
|
|
LogicGamestates(game, ALLEGRO_BPS_TO_SECS(al_get_timer_speed(game->_priv.timer)));
|
2012-09-26 03:19:10 +02:00
|
|
|
}
|
2016-07-03 22:38:36 +02:00
|
|
|
game->_priv.showconsole = true;
|
|
|
|
PrintConsole(game, "DEBUG: 512 frames skipped...");
|
2017-09-10 21:35:14 +02:00
|
|
|
} else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (game->config.debug) && (ev.keyboard.keycode == ALLEGRO_KEY_F10)) {
|
2016-07-03 22:38:36 +02:00
|
|
|
double speed = ALLEGRO_BPS_TO_SECS(al_get_timer_speed(game->_priv.timer)); // inverting
|
2012-12-27 01:02:47 +01:00
|
|
|
speed -= 10;
|
2017-09-10 21:35:14 +02:00
|
|
|
if (speed < 10) { speed = 10; }
|
2016-07-03 22:38:36 +02:00
|
|
|
al_set_timer_speed(game->_priv.timer, ALLEGRO_BPS_TO_SECS(speed));
|
|
|
|
game->_priv.showconsole = true;
|
2017-09-10 21:35:14 +02:00
|
|
|
PrintConsole(game, "DEBUG: Gameplay speed: %.2fx", speed / 60.0);
|
|
|
|
} else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (game->config.debug) && (ev.keyboard.keycode == ALLEGRO_KEY_F11)) {
|
2016-07-03 22:38:36 +02:00
|
|
|
double speed = ALLEGRO_BPS_TO_SECS(al_get_timer_speed(game->_priv.timer)); // inverting
|
2012-12-27 01:02:47 +01:00
|
|
|
speed += 10;
|
2017-09-10 21:35:14 +02:00
|
|
|
if (speed > 600) { speed = 600; }
|
2016-07-03 22:38:36 +02:00
|
|
|
al_set_timer_speed(game->_priv.timer, ALLEGRO_BPS_TO_SECS(speed));
|
|
|
|
game->_priv.showconsole = true;
|
2017-09-10 21:35:14 +02:00
|
|
|
PrintConsole(game, "DEBUG: Gameplay speed: %.2fx", speed / 60.0);
|
2017-06-25 17:37:41 +02:00
|
|
|
} else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (ev.keyboard.keycode == ALLEGRO_KEY_F12)) {
|
2017-09-09 00:42:57 +02:00
|
|
|
DrawGamestates(game);
|
|
|
|
int flags = al_get_new_bitmap_flags();
|
|
|
|
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
|
2017-09-10 21:35:14 +02:00
|
|
|
ALLEGRO_BITMAP* bitmap = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display));
|
2017-09-09 00:42:57 +02:00
|
|
|
al_set_new_bitmap_flags(flags);
|
2017-09-10 21:35:14 +02:00
|
|
|
ALLEGRO_BITMAP* target = al_get_target_bitmap();
|
2017-09-09 00:42:57 +02:00
|
|
|
al_set_target_bitmap(bitmap);
|
|
|
|
al_draw_bitmap(al_get_backbuffer(game->display), 0, 0, 0);
|
|
|
|
al_set_target_bitmap(target);
|
|
|
|
PrintConsole(game, "Screenshot made! Storing...");
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
struct ScreenshotThreadData* data = malloc(sizeof(struct ScreenshotThreadData));
|
2017-09-09 00:42:57 +02:00
|
|
|
data->game = game;
|
|
|
|
data->bitmap = bitmap;
|
|
|
|
#ifndef LIBSUPERDERPY_SINGLE_THREAD
|
|
|
|
al_run_detached_thread(ScreenshotThread, data);
|
|
|
|
#else
|
|
|
|
ScreenshotThread(data);
|
|
|
|
#endif
|
2016-07-02 23:23:08 +02:00
|
|
|
}
|
2016-11-08 17:42:23 +01:00
|
|
|
EventGamestates(game, &ev);
|
2012-02-25 22:26:31 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-03 22:38:36 +02:00
|
|
|
|
2017-08-22 21:54:58 +02:00
|
|
|
#ifndef __EMSCRIPTEN__
|
2017-09-09 00:11:43 +02:00
|
|
|
if (game->handlers.destroy) {
|
2017-08-22 21:54:58 +02:00
|
|
|
libsuperderpy_destroy(game);
|
|
|
|
}
|
2016-07-03 22:38:36 +02:00
|
|
|
return 0;
|
2017-08-22 21:54:58 +02:00
|
|
|
#endif
|
2016-07-03 22:38:36 +02:00
|
|
|
}
|
|
|
|
|
2017-09-10 21:35:14 +02:00
|
|
|
SYMBOL_EXPORT void libsuperderpy_destroy(struct Game* game) {
|
2017-09-09 00:11:43 +02:00
|
|
|
game->shutting_down = true;
|
2012-12-24 19:41:12 +01:00
|
|
|
|
2016-08-16 18:01:12 +02:00
|
|
|
ClearGarbage(game);
|
|
|
|
|
|
|
|
struct Gamestate *tmp = game->_priv.gamestates, *pom;
|
2012-12-26 13:38:15 +01:00
|
|
|
while (tmp) {
|
|
|
|
if (tmp->started) {
|
2016-07-03 22:38:36 +02:00
|
|
|
PrintConsole(game, "Stopping gamestate \"%s\"...", tmp->name);
|
2016-08-16 18:41:50 +02:00
|
|
|
game->_priv.current_gamestate = tmp;
|
2016-08-20 03:02:39 +02:00
|
|
|
(*tmp->api->Gamestate_Stop)(game, tmp->data);
|
2012-12-26 13:38:15 +01:00
|
|
|
tmp->started = false;
|
|
|
|
}
|
|
|
|
if (tmp->loaded) {
|
2016-07-03 22:38:36 +02:00
|
|
|
PrintConsole(game, "Unloading gamestate \"%s\"...", tmp->name);
|
2016-08-16 18:41:50 +02:00
|
|
|
game->_priv.current_gamestate = tmp;
|
2016-08-20 03:02:39 +02:00
|
|
|
(*tmp->api->Gamestate_Unload)(game, tmp->data);
|
2012-12-26 13:38:15 +01:00
|
|
|
tmp->loaded = false;
|
|
|
|
}
|
2017-09-09 00:11:43 +02:00
|
|
|
CloseGamestate(game, tmp);
|
2016-08-16 18:01:12 +02:00
|
|
|
pom = tmp->next;
|
|
|
|
free(tmp);
|
2017-09-10 21:35:14 +02:00
|
|
|
tmp = pom;
|
2012-12-26 13:38:15 +01:00
|
|
|
}
|
|
|
|
|
2017-09-09 00:11:43 +02:00
|
|
|
(*game->_priv.loading.gamestate->api->Gamestate_Unload)(game, game->_priv.loading.gamestate->data);
|
|
|
|
CloseGamestate(game, game->_priv.loading.gamestate);
|
|
|
|
free(game->_priv.loading.gamestate);
|
|
|
|
|
|
|
|
if (game->handlers.destroy) {
|
|
|
|
(*game->handlers.destroy)(game);
|
2017-08-22 21:54:58 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 10:34:32 +01:00
|
|
|
ClearScreen(game);
|
2017-08-22 21:54:58 +02:00
|
|
|
#ifdef __EMSCRIPTEN__
|
2017-09-09 00:11:43 +02:00
|
|
|
{
|
2017-09-10 21:35:14 +02:00
|
|
|
ALLEGRO_BITMAP* bmp = al_create_bitmap(320, 180);
|
2017-09-09 00:11:43 +02:00
|
|
|
al_set_target_bitmap(bmp);
|
2017-09-10 21:35:14 +02:00
|
|
|
al_clear_to_color(al_map_rgb(0, 0, 0));
|
|
|
|
ALLEGRO_FONT* font = al_create_builtin_font();
|
|
|
|
al_draw_text(font, al_map_rgb(228, 127, 59), 320 / 2, 180 / 2 - 8 - 6, ALLEGRO_ALIGN_CENTER, "It's now safe to turn off");
|
|
|
|
al_draw_text(font, al_map_rgb(228, 127, 59), 320 / 2, 180 / 2 - 8 + 6, ALLEGRO_ALIGN_CENTER, "your browser.");
|
2017-09-09 00:11:43 +02:00
|
|
|
al_set_target_backbuffer(game->display);
|
2017-09-10 21:35:14 +02:00
|
|
|
al_draw_scaled_bitmap(bmp, 0, 0, 320, 180, 0, -game->viewport.height * 0.2, game->viewport.width, game->viewport.height * 1.4, 0);
|
2017-09-09 00:11:43 +02:00
|
|
|
al_flip_display();
|
|
|
|
al_destroy_bitmap(bmp);
|
|
|
|
al_destroy_font(font);
|
|
|
|
}
|
2017-08-22 21:54:58 +02:00
|
|
|
#endif
|
2017-09-09 00:11:43 +02:00
|
|
|
|
2016-07-03 22:38:36 +02:00
|
|
|
PrintConsole(game, "Shutting down...");
|
|
|
|
DrawConsole(game);
|
2012-02-17 13:25:06 +01:00
|
|
|
al_flip_display();
|
2016-08-16 18:01:12 +02:00
|
|
|
while (game->_priv.garbage) {
|
|
|
|
free(game->_priv.garbage->data);
|
|
|
|
game->_priv.garbage = game->_priv.garbage->next;
|
|
|
|
}
|
2016-07-03 22:38:36 +02:00
|
|
|
al_destroy_timer(game->_priv.timer);
|
|
|
|
Console_Unload(game);
|
|
|
|
al_destroy_display(game->display);
|
2016-08-29 22:47:55 +02:00
|
|
|
al_destroy_user_event_source(&(game->event_source));
|
2016-07-03 22:38:36 +02:00
|
|
|
al_destroy_event_queue(game->_priv.event_queue);
|
|
|
|
al_destroy_mixer(game->audio.fx);
|
|
|
|
al_destroy_mixer(game->audio.music);
|
|
|
|
al_destroy_mixer(game->audio.mixer);
|
|
|
|
al_destroy_voice(game->audio.v);
|
2012-02-25 22:26:31 +01:00
|
|
|
al_uninstall_audio();
|
2016-07-03 22:38:36 +02:00
|
|
|
DeinitConfig(game);
|
2017-08-22 21:54:58 +02:00
|
|
|
#ifndef __EMSCRIPTEN__
|
2012-12-26 02:25:56 +01:00
|
|
|
al_uninstall_system();
|
2016-07-04 00:06:50 +02:00
|
|
|
char** argv = game->_priv.argv;
|
2016-08-16 18:01:12 +02:00
|
|
|
bool restart = game->restart;
|
2016-07-03 22:38:36 +02:00
|
|
|
free(game);
|
2016-08-16 18:01:12 +02:00
|
|
|
if (restart) {
|
2016-09-08 00:32:21 +02:00
|
|
|
#ifdef ALLEGRO_MACOSX
|
|
|
|
chdir(game->_priv.cwd);
|
|
|
|
#endif
|
2017-08-19 01:26:53 +02:00
|
|
|
execv(argv[0], argv);
|
2012-03-08 22:21:02 +01:00
|
|
|
}
|
2017-08-22 21:54:58 +02:00
|
|
|
#endif
|
2012-02-15 23:57:06 +01:00
|
|
|
}
|