taisei/src/stage.c

1228 lines
29 KiB
C
Raw Normal View History

/*
* This software is licensed under the terms of the MIT License.
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
2019-07-03 20:00:56 +02:00
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
*/
#include "taisei.h"
#include "util.h"
#include "stage.h"
#include "global.h"
2012-07-28 22:53:53 +02:00
#include "video.h"
2017-01-24 14:40:57 +01:00
#include "resource/bgm.h"
#include "replay/state.h"
#include "replay/stage.h"
#include "replay/struct.h"
#include "config.h"
#include "player.h"
#include "menu/ingamemenu.h"
#include "menu/gameovermenu.h"
#include "audio/audio.h"
Implemented a simple and consistent logging subsystem The goal of this change is mainly to clean up Taisei's codebase and improve its console output. I've been frustrated by files littered with inconsistent printf/fprintf/warnx/errx calls for a long time, and now I actually did something about it. All the above functions are now considered deprecated and result in a compile-time warning when used. Instead, the following macros should be used: log_debug(format, ...) log_info(format, ...) log_warn(format, ...) log_err(format, ...) As you can see, all of them have the same printf-like interface. But they have different functionality and purpose: log_debug is intended for very verbose and specific information. It does nothing in release builds, much like assert(), so don't use expressions with side-effects in its arguments. log_info is for various status updates that are expected during normal operation of the program. log_warn is for non-critical failures or other things that may be worth investigating, but don't inherently render the program non-functional. log_err is for when the only choice is to give up. Like errx, it also terminates the program. Unlike errx, it actually calls abort(), which means the cleanup functions are not ran -- but on the other hand, you get a debuggable backtrace. However, if you're trying to catch programming errors, consider using assert() instead. All of them produce output that contains a timestamp, the log level identifier, the calling function's name, and the formatted message. The newline at the end of the format string is not required -- no, it is actually *prohibited*. The logging system will take care of the line breaks by itself, don't litter the code with that shit. Internally, the logging system is based on the SDL_RWops abstraction, and may have multiple, configurable destinations. This makes it easily extensible. Currently, log_debug and log_info are set to write to stdout, log_warn and log_err to stderr, and all of them also to the file log.txt in the Taisei config directory. Consequently, the nasty freopen hacks we used to make Taisei write to log files on Windows are no longer needed -- which is a very good thing, considering they probably would break if the configdir path contains UTF-8 characters. SDL_RWFromFile does not suffer this limitation. As an added bonus, it's also thread-safe. Note about printf and fprintf: in very few cases, the logging system is not a good substitute for these functions. That is, when you care about writing exactly to stdout/stderr and about exactly how the output looks. However, I insist on keeping the deprecation warnings on them to not tempt anyone to use them for logging/debugging out of habit and/or laziness. For this reason, I've added a tsfprintf function to util.c. It is functionally identical to fprintf, except it returns void. Yes, the name is deliberately ugly. Avoid using it if possible, but if you must, only use it to write to stdout or stderr. Do not write to actual files with it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
#include "log.h"
#include "stagetext.h"
#include "stagedraw.h"
2017-12-13 20:05:12 +01:00
#include "stageobjects.h"
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
#include "eventloop/eventloop.h"
#include "common_tasks.h"
#include "stageinfo.h"
#include "dynstage.h"
2017-02-11 12:38:50 +01:00
2020-06-24 19:51:00 +02:00
typedef struct StageFrameState {
StageInfo *stage;
CallChain cc;
CoSched sched;
Replay *quicksave;
bool quicksave_is_automatic;
bool quickload_requested;
uint32_t dynstage_generation;
2020-06-24 19:51:00 +02:00
int transition_delay;
int desync_check_freq;
2020-06-24 19:51:00 +02:00
uint16_t last_replay_fps;
float view_shake;
int bgm_start_time;
double bgm_start_pos;
2020-06-24 19:51:00 +02:00
} StageFrameState;
static StageFrameState *_current_stage_state; // TODO remove this shitty hack
#define BGM_FADE_LONG (2.0 * FADE_TIME / (double)FPS)
#define BGM_FADE_SHORT (FADE_TIME / (double)FPS)
static inline bool is_quickloading(StageFrameState *fstate) {
return fstate->quicksave && fstate->quicksave == global.replay.input.replay;
}
static void sync_bgm(StageFrameState *fstate) {
double t = fstate->bgm_start_pos + (global.frames - fstate->bgm_start_time) / (double)FPS;
audio_bgm_seek_realtime(t);
}
#ifdef HAVE_SKIP_MODE
static struct {
const char *skip_to_bookmark;
bool skip_to_dialog;
bool was_skip_mode;
} skip_state;
void _stage_bookmark(const char *name) {
log_debug("Bookmark [%s] reached at %i", name, global.frames);
if(skip_state.skip_to_bookmark && !strcmp(skip_state.skip_to_bookmark, name)) {
skip_state.skip_to_bookmark = NULL;
global.plr.iddqd = false;
}
}
DEFINE_EXTERN_TASK(stage_bookmark) {
_stage_bookmark(ARGS.name);
}
bool stage_is_skip_mode(void) {
return skip_state.skip_to_bookmark || skip_state.skip_to_dialog;
}
static void skipstate_init(void) {
skip_state.skip_to_dialog = env_get_int("TAISEI_SKIP_TO_DIALOG", 0);
skip_state.skip_to_bookmark = env_get_string_nonempty("TAISEI_SKIP_TO_BOOKMARK", NULL);
}
static LogicFrameAction skipstate_handle_frame(void) {
if(skip_state.skip_to_dialog && dialog_is_active(global.dialog)) {
skip_state.skip_to_dialog = false;
global.plr.iddqd = false;
}
bool skip_mode = stage_is_skip_mode();
if(!skip_mode && skip_state.was_skip_mode) {
sync_bgm(_current_stage_state);
}
skip_state.was_skip_mode = skip_mode;
if(skip_mode) {
return LFRAME_SKIP_ALWAYS;
}
if(gamekeypressed(KEY_SKIP)) {
return LFRAME_SKIP;
}
return LFRAME_WAIT;
}
static void skipstate_shutdown(void) {
memset(&skip_state, 0, sizeof(skip_state));
}
#else
INLINE LogicFrameAction skipstate_handle_frame(void) { return LFRAME_WAIT; }
INLINE void skipstate_init(void) { }
INLINE void skipstate_shutdown(void) { }
#endif
2017-02-26 13:17:48 +01:00
static void stage_start(StageInfo *stage) {
global.timer = 0;
global.frames = 0;
global.gameover = 0;
global.voltage_threshold = 0;
player_stage_pre_init(&global.plr);
2017-02-26 13:17:48 +01:00
2020-04-26 21:27:13 +02:00
stats_stage_reset(&global.plr.stats);
2017-02-26 13:17:48 +01:00
if(stage->type == STAGE_SPELL) {
global.is_practice_mode = true;
global.plr.lives = 0;
2017-02-26 13:17:48 +01:00
global.plr.bombs = 0;
2017-09-11 21:09:30 +02:00
} else if(global.is_practice_mode) {
global.plr.lives = PLR_STGPRACTICE_LIVES;
global.plr.bombs = PLR_STGPRACTICE_BOMBS;
}
if(global.is_practice_mode) {
global.plr.power_stored = config_get_int(CONFIG_PRACTICE_POWER);
}
global.plr.power_stored = iclamp(global.plr.power_stored, 0, PLR_MAX_POWER_STORED);
reset_all_sfx();
}
2017-10-10 16:06:46 +02:00
static bool ingame_menu_interrupts_bgm(void) {
return global.stage->type != STAGE_SPELL;
}
2020-06-24 19:51:00 +02:00
typedef struct IngameMenuContext {
CallChain next;
BGM *saved_bgm;
double saved_bgm_pos;
bool bgm_interrupted;
} IngameMenuContext;
static void setup_ingame_menu_bgm(IngameMenuContext *ctx, BGM *bgm) {
if(ingame_menu_interrupts_bgm()) {
ctx->bgm_interrupted = true;
if(bgm) {
ctx->saved_bgm = audio_bgm_current();
ctx->saved_bgm_pos = audio_bgm_tell();
audio_bgm_play(bgm, true, 0, 1);
} else {
audio_bgm_pause();
}
}
}
static void resume_bgm(IngameMenuContext *ctx) {
if(ctx->bgm_interrupted) {
if(ctx->saved_bgm) {
audio_bgm_play(ctx->saved_bgm, true, ctx->saved_bgm_pos, 0.5);
ctx->saved_bgm = NULL;
} else {
audio_bgm_resume();
}
ctx->bgm_interrupted = false;
}
2017-12-17 01:00:06 +01:00
}
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
static void stage_leave_ingame_menu(CallChainResult ccr) {
2020-06-24 19:51:00 +02:00
IngameMenuContext *ctx = ccr.ctx;
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
MenuData *m = ccr.result;
2017-10-10 16:06:46 +02:00
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
if(m->state != MS_Dead) {
return;
}
2017-10-10 16:06:46 +02:00
if(global.gameover > 0) {
stop_all_sfx();
2017-10-10 16:06:46 +02:00
2020-06-24 19:51:00 +02:00
if(ctx->bgm_interrupted) {
audio_bgm_stop(global.gameover == GAMEOVER_RESTART ? BGM_FADE_SHORT : BGM_FADE_LONG);
2017-10-10 16:06:46 +02:00
}
} else {
2020-06-24 19:51:00 +02:00
resume_bgm(ctx);
events_emit(TE_GAME_PAUSE_STATE_CHANGED, false, NULL, NULL);
2017-10-10 16:06:46 +02:00
}
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
resume_all_sfx();
2020-06-24 19:51:00 +02:00
run_call_chain(&ctx->next, NULL);
mem_free(ctx);
}
2020-06-24 19:51:00 +02:00
static void stage_enter_ingame_menu(MenuData *m, BGM *bgm, CallChain next) {
events_emit(TE_GAME_PAUSE_STATE_CHANGED, true, NULL, NULL);
auto ctx = ALLOC(IngameMenuContext, { .next = next });
2020-06-24 19:51:00 +02:00
setup_ingame_menu_bgm(ctx, bgm);
pause_all_sfx();
2020-06-24 19:51:00 +02:00
enter_menu(m, CALLCHAIN(stage_leave_ingame_menu, ctx));
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
}
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
void stage_pause(void) {
if(global.gameover == GAMEOVER_TRANSITIONING || stage_is_skip_mode()) {
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
return;
}
MenuData *m;
if(global.replay.input.replay) {
m = create_ingame_menu_replay();
} else {
m = create_ingame_menu();
}
2020-06-24 19:51:00 +02:00
stage_enter_ingame_menu(m, NULL, NO_CALLCHAIN);
}
void stage_gameover(void) {
if(global.stage->type == STAGE_SPELL && config_get_int(CONFIG_SPELLSTAGE_AUTORESTART)) {
global.gameover = GAMEOVER_RESTART;
return;
}
2020-06-24 19:51:00 +02:00
BGM *bgm = NULL;
if(ingame_menu_interrupts_bgm()) {
bgm = res_bgm("gameover");
progress_unlock_bgm("gameover");
}
stage_enter_ingame_menu(create_gameover_menu(), bgm, NO_CALLCHAIN);
}
2017-12-28 02:35:53 +01:00
static bool stage_input_common(SDL_Event *event, void *arg) {
TaiseiEvent type = TAISEI_EVENT(event->type);
int32_t code = event->user.code;
switch(type) {
case TE_GAME_KEY_DOWN:
switch(code) {
case KEY_STOP:
stage_finish(GAMEOVER_DEFEAT);
return true;
case KEY_RESTART:
stage_finish(GAMEOVER_RESTART);
return true;
}
break;
case TE_GAME_PAUSE:
stage_pause();
break;
default:
break;
}
return false;
}
static bool stage_input_key_filter(KeyIndex key, bool is_release) {
if(key == KEY_HAHAIWIN) {
IF_DEBUG(
if(!is_release) {
stage_finish(GAMEOVER_WIN);
}
);
return false;
}
IF_NOT_DEBUG(
if(
key == KEY_IDDQD ||
key == KEY_POWERUP ||
key == KEY_POWERDOWN
) {
return false;
}
);
if(stage_is_cleared()) {
if(key == KEY_SHOT) {
if(
global.gameover == GAMEOVER_SCORESCREEN &&
global.frames - global.gameover_time > GAMEOVER_SCORE_DELAY * 2
) {
if(!is_release) {
stage_finish(GAMEOVER_WIN);
}
}
}
if(key == KEY_BOMB || key == KEY_SPECIAL) {
return false;
}
}
return true;
}
attr_nonnull_all
static Replay *create_quicksave_replay(ReplayStage *rstg_src) {
ReplayStage *rstg = memdup(rstg_src, sizeof(*rstg));
rstg->num_events = 0;
memset(&rstg->events, 0, sizeof(rstg->events));
dynarray_ensure_capacity(&rstg->events, rstg_src->events.num_elements + 1);
dynarray_set_elements(&rstg->events, rstg_src->events.num_elements, rstg_src->events.data);
replay_stage_event(rstg, global.frames, EV_RESUME, 0);
auto rpy = ALLOC(Replay);
rpy->stages.num_elements = rpy->stages.capacity = 1;
rpy->stages.data = rstg;
log_info("Created quicksave replay on frame %i", global.frames);
return rpy;
}
static inline bool is_quicksave_allowed(void) {
#ifndef DEBUG
if(global.is_practice_mode) {
return false;
}
#endif
if(global.gameover != GAMEOVER_NONE) {
return false;
}
return true;
}
static void stage_do_quicksave(StageFrameState *fstate, bool isauto) {
if(isauto && fstate->quicksave && !fstate->quicksave_is_automatic) {
// Do not overwrite a manual quicksave with an auto quicksave
return;
}
if(fstate->quicksave) {
replay_reset(fstate->quicksave);
mem_free(fstate->quicksave);
}
fstate->quicksave = create_quicksave_replay(global.replay.output.stage);
fstate->quicksave_is_automatic = isauto;
}
static void stage_do_quickload(StageFrameState *fstate) {
if(fstate->quicksave) {
fstate->quickload_requested = true;
} else {
log_info("No active quicksave");
}
}
static bool stage_input_handler_gameplay(SDL_Event *event, void *arg) {
StageFrameState *fstate = NOT_NULL(arg);
if(event->type == SDL_KEYDOWN && !event->key.repeat && is_quicksave_allowed()) {
if(event->key.keysym.scancode == config_get_int(CONFIG_KEY_QUICKSAVE)) {
stage_do_quicksave(fstate, false);
} else if(event->key.keysym.scancode == config_get_int(CONFIG_KEY_QUICKLOAD)) {
stage_do_quickload(fstate);
}
return false;
}
TaiseiEvent type = TAISEI_EVENT(event->type);
int32_t code = event->user.code;
2017-12-28 02:35:53 +01:00
if(stage_input_common(event, arg)) {
return false;
}
if(
(type == TE_GAME_KEY_DOWN || type == TE_GAME_KEY_UP) &&
code == KEY_SHOT &&
config_get_int(CONFIG_SHOT_INVERTED)
) {
type = type == TE_GAME_KEY_DOWN ? TE_GAME_KEY_UP : TE_GAME_KEY_DOWN;
}
ReplayState *rpy = &global.replay.output;
2012-08-13 17:50:28 +02:00
switch(type) {
case TE_GAME_KEY_DOWN:
if(stage_input_key_filter(code, false)) {
player_event(&global.plr, NULL, rpy, EV_PRESS, code);
2017-02-15 18:10:56 +01:00
}
2012-08-13 17:50:28 +02:00
break;
case TE_GAME_KEY_UP:
if(stage_input_key_filter(code, true)) {
player_event(&global.plr, NULL, rpy, EV_RELEASE, code);
}
2012-08-07 05:28:41 +02:00
break;
case TE_GAME_AXIS_LR:
player_event(&global.plr, NULL, rpy, EV_AXIS_LR, (uint16_t)code);
2012-08-15 16:36:39 +02:00
break;
case TE_GAME_AXIS_UD:
player_event(&global.plr, NULL, rpy, EV_AXIS_UD, (uint16_t)code);
2012-08-15 16:36:39 +02:00
break;
2012-08-13 17:50:28 +02:00
default: break;
}
return false;
2012-08-13 17:50:28 +02:00
}
static bool stage_input_handler_replay(SDL_Event *event, void *arg) {
2017-12-28 02:35:53 +01:00
stage_input_common(event, arg);
return false;
2012-08-13 17:50:28 +02:00
}
struct replay_event_arg {
ReplayState *st;
ReplayEvent *resume_event;
};
static void handle_replay_event(ReplayEvent *e, void *arg) {
struct replay_event_arg *a = NOT_NULL(arg);
if(UNLIKELY(a->resume_event != NULL)) {
log_warn(
"Got replay event [%i:%02x:%04x] after resume event in the same frame, ignoring",
e->frame, e->type, e->value
);
return;
}
switch(e->type) {
case EV_OVER:
global.gameover = GAMEOVER_DEFEAT;
break;
case EV_RESUME:
a->resume_event = e;
break;
default:
player_event(&global.plr, a->st, &global.replay.output, e->type, e->value);
break;
}
}
static void leave_replay_mode(StageFrameState *fstate, ReplayState *rp_in) {
if(rp_in->replay == fstate->quicksave) {
audio_sfx_set_enabled(true);
sync_bgm(fstate);
}
replay_state_deinit(rp_in);
}
static void replay_input(StageFrameState *fstate) {
if(!is_quickloading(fstate)) {
events_poll((EventHandler[]){
{ .proc = stage_input_handler_replay },
{ NULL }
}, EFLAG_GAME);
}
ReplayState *rp_in = &global.replay.input;
if(UNLIKELY(rp_in->mode == REPLAY_NONE)) {
return;
}
struct replay_event_arg a = { .st = rp_in };
replay_state_play_advance(rp_in, global.frames, handle_replay_event, &a);
player_applymovement(&global.plr);
if(a.resume_event) {
leave_replay_mode(fstate, rp_in);
}
}
static void display_bgm_title(void) {
BGM *bgm = audio_bgm_current();
const char *title = bgm ? bgm_get_title(bgm) : NULL;
if(title) {
char txt[strlen(title) + 6];
snprintf(txt, sizeof(txt), "BGM: %s", title);
stagetext_add(txt, VIEWPORT_W-15 + I * (VIEWPORT_H-20), ALIGN_RIGHT, res_font("standard"), RGB(1, 1, 1), 30, 180, 35, 35);
}
}
static bool stage_handle_bgm_change(SDL_Event *evt, void *a) {
StageFrameState *fstate = NOT_NULL(a);
fstate->bgm_start_time = global.frames;
fstate->bgm_start_pos = audio_bgm_tell();
if(dialog_is_active(global.dialog)) {
INVOKE_TASK_WHEN(&global.dialog->events.fadeout_began, common_call_func, display_bgm_title);
} else {
display_bgm_title();
}
return false;
}
static void stage_input(StageFrameState *fstate) {
if(stage_is_skip_mode()) {
events_poll((EventHandler[]){
{
.proc = stage_handle_bgm_change,
.event_type = MAKE_TAISEI_EVENT(TE_AUDIO_BGM_STARTED),
.arg = fstate,
},
{NULL}
}, EFLAG_NOPUMP);
} else {
events_poll((EventHandler[]){
{ .proc = stage_input_handler_gameplay, .arg = fstate },
{
.proc = stage_handle_bgm_change,
.event_type = MAKE_TAISEI_EVENT(TE_AUDIO_BGM_STARTED),
.arg = fstate,
},
{NULL}
}, EFLAG_GAME);
}
player_fix_input(&global.plr, &global.replay.output);
player_applymovement(&global.plr);
}
2017-02-26 13:17:48 +01:00
static void stage_logic(void) {
process_boss(&global.boss);
process_enemies(&global.enemies);
process_projectiles(&global.projs, true);
2011-04-29 10:26:37 +02:00
process_items();
process_lasers();
process_projectiles(&global.particles, false);
if(global.dialog) {
dialog_update(global.dialog);
if((global.plr.inputflags & INFLAG_SKIP) && dialog_is_active(global.dialog)) {
dialog_page(global.dialog);
}
}
if(stage_is_skip_mode()) {
if(dialog_is_active(global.dialog)) {
dialog_page(global.dialog);
}
if(global.boss) {
ent_damage(&global.boss->ent, &(DamageInfo) { 400, DMG_PLAYER_SHOT } );
}
}
update_all_sfx();
global.frames++;
if(!dialog_is_active(global.dialog) && (!global.boss || boss_is_fleeing(global.boss))) {
global.timer++;
}
if(global.replay.input.replay && global.gameover != GAMEOVER_TRANSITIONING) {
ReplayStage *rstg = global.replay.input.stage;
ReplayEvent *last_event = dynarray_get_ptr(&rstg->events, rstg->events.num_elements - 1);
if(global.frames == last_event->frame - FADE_TIME && last_event->type != EV_RESUME) {
stage_finish(GAMEOVER_DEFEAT);
}
}
stagetext_update();
}
void stage_clear_hazards_predicate(bool (*predicate)(EntityInterface *ent, void *arg), void *arg, ClearHazardsFlags flags) {
2019-03-26 16:58:38 +01:00
bool force = flags & CLEAR_HAZARDS_FORCE;
if(flags & CLEAR_HAZARDS_BULLETS) {
for(Projectile *p = global.projs.first, *next; p; p = next) {
next = p->next;
2019-03-26 16:58:38 +01:00
if(!force && !projectile_is_clearable(p)) {
continue;
}
if(!predicate || predicate(&p->ent, arg)) {
clear_projectile(p, flags);
}
}
}
if(flags & CLEAR_HAZARDS_LASERS) {
for(Laser *l = global.lasers.first, *next; l; l = next) {
2018-01-06 19:23:38 +01:00
next = l->next;
2019-03-26 16:58:38 +01:00
if(!force && !laser_is_clearable(l)) {
continue;
}
if(!predicate || predicate(&l->ent, arg)) {
clear_laser(l, flags);
}
}
}
}
void stage_clear_hazards(ClearHazardsFlags flags) {
stage_clear_hazards_predicate(NULL, NULL, flags);
}
static bool proximity_predicate(EntityInterface *ent, void *varg) {
Circle *area = varg;
switch(ent->type) {
case ENT_TYPE_ID(Projectile): {
Projectile *p = ENT_CAST(ent, Projectile);
return cabs(p->pos - area->origin) < area->radius;
}
case ENT_TYPE_ID(Laser): {
Laser *l = ENT_CAST(ent, Laser);
return laser_intersects_circle(l, *area);
}
default: UNREACHABLE;
}
}
2019-03-26 16:58:38 +01:00
static bool ellipse_predicate(EntityInterface *ent, void *varg) {
Ellipse *e = varg;
switch(ent->type) {
case ENT_TYPE_ID(Projectile): {
2019-03-26 16:58:38 +01:00
Projectile *p = ENT_CAST(ent, Projectile);
return point_in_ellipse(p->pos, *e);
}
case ENT_TYPE_ID(Laser): {
2019-03-26 16:58:38 +01:00
Laser *l = ENT_CAST(ent, Laser);
return laser_intersects_ellipse(l, *e);
}
default: UNREACHABLE;
}
}
void stage_clear_hazards_at(cmplx origin, double radius, ClearHazardsFlags flags) {
Circle area = { origin, radius };
stage_clear_hazards_predicate(proximity_predicate, &area, flags);
}
2019-03-26 16:58:38 +01:00
void stage_clear_hazards_in_ellipse(Ellipse e, ClearHazardsFlags flags) {
stage_clear_hazards_predicate(ellipse_predicate, &e, flags);
}
2021-08-12 22:31:06 +02:00
TASK(clear_dialog) {
assert(global.dialog != NULL);
// dialog_deinit() should've been called by dialog_end() at this point
global.dialog = NULL;
}
2021-08-12 22:31:06 +02:00
TASK(dialog_fixup_timer) {
// HACK: remove when global.timer is gone
global.timer++;
}
void stage_begin_dialog(Dialog *d) {
assert(global.dialog == NULL);
global.dialog = d;
dialog_init(d);
INVOKE_TASK_WHEN(&d->events.fadeout_began, dialog_fixup_timer);
INVOKE_TASK_WHEN(&d->events.fadeout_ended, clear_dialog);
}
2017-02-26 13:17:48 +01:00
static void stage_free(void) {
delete_enemies(&global.enemies);
2011-04-29 10:26:37 +02:00
delete_items();
delete_lasers();
delete_projectiles(&global.projs);
delete_projectiles(&global.particles);
if(global.dialog) {
dialog_deinit(global.dialog);
global.dialog = NULL;
}
if(global.boss) {
free_boss(global.boss);
global.boss = NULL;
}
lasers_shutdown();
Lots of disorganized (mostly) visual overhaul (#156) * WIP some projectile effects * fix segfault * Laser smoothing and glow via post-processing blur magic TODO: make it optional * fix memory corruption * fix memory corruption for realsies now * fix color_get_hsl for out-of-range colors * some more bullet flare tweaks * some lame clear effect workarounds * spawn bullet flares after frame 0; looks better and fixes some problems * New baryon explosion; fix petal_explosion; leanify everything * Add missing bullet flare sprite, rebuild main atlas * improve batching efficiency with bullet spawn flares * forgot git add * Group projectiles/particles by shader where possible * Another take on baryon explosion; make fg framebuffers 16bit * WIP some settings for toasters * remove stupid debug log * microoptimization that probably does nothing anyway * somewhat more intuitive quality settings * Whitelist more particles (MarisaB is on hold) * Whitelist (and fix) some more stage6 particles (mostly ToE) * Add a spell name background * Experimental radial healthbar for bosses * healthbar tweaks * thiccer healthbars in response to feedback * remove healthbar survival timer; just fade out on survivals * Add linear healthbars option; WIP other boss HUD tweaks * Use the proper spell card name format * New font and some random garbage to go along with it * Generate static font outlines for use in text shaders * Use outlines in overlay text shader * Complete boss HUD/healthbar fading logic * fix boss timer limit * stage5 bombs explosion effect * split PFLAG_NOSPAWNZOOM into PFLAG_NOSPAWNFLARE and PFLAG_NOSPAWNFADE; introduce PFLAG_NOSPAWNEFFECTS which disables both (it's just the two values OR'd together) simplify vampiric vapor bullet spawning effect * Remove spawn fade-in from super-fast stage5 fairy projectiles (limiters) * lower particle density in v.vapor in minimal mode * graze effect tweaks * fix text shortening, tweak replay menu layout * stupid debug spam * revisit grazing effects again * dumb debug spam again * improve boss attack timer * overlay effect for boss deaths (similar to the player one) * spice up spellcard declaration (HUD) * don't spawn boss death overlay if fleed * modify Exo2 font to use tabular figures * adjust replay menu for the font change * draw timer & power with standard font (phasing out the numbers font) * WIP new HUD; random fixes/tweaks * hud: move difficulty indicator * hud: move debug stuff around * preloads, mostly * fix youmuA batching conflict * shitty workaround for the shitty screenshake shit * remove extraspell lag by stopping to draw stagebg sooner which is possible because extra spells have a different spellcard_intro timing. Fun fact of the day: the duration of spellcard_intro is always ATTACK_START_DELAY_EXTRA even for normal spells! * new stain particle * i disabled background rendering… * "batch" marisa_b masterspark draws * remove these once a new atlas is generated * make toe quick again * hopefully fix all occurences of changed stain and ScaleFade behavior * tweaking reimu_a and toe boson launch effects * make lhc fast again * softer involnerability effect * fix stage 1 snow on the water bug (and improve performance) translated the time to the future a bit because it only seemed to be an issue for small time values * remove unnecessary spawnflare from toe * tone down extra spell start effect * experimental ReimuB gap shader optimization * fix python3 shebangs * generate simple blur shaders w/ hardcoded kernels * New loading screen * lasers: fix incorrect draw hook registration * add webp support for atlas generator * Use ImageMagick for atlas composition (adds 16-bit support) * Atlas maintenance * make the vampiric vapor bullets less prone to invisibility * Revert a few particles to the quadratic fade curve * experimental baryon effect * improve baryon sprites * disable the baryon effect on minimal postprocessing setting
2019-01-04 23:59:39 +01:00
projectiles_free();
stagetext_free();
}
static void stage_finalize(CallChainResult ccr) {
global.gameover = (intptr_t)ccr.ctx;
2017-02-26 13:17:48 +01:00
}
void stage_finish(int gameover) {
if(global.gameover == GAMEOVER_TRANSITIONING) {
return;
}
int prev_gameover = global.gameover;
global.gameover_time = global.frames;
if(gameover == GAMEOVER_SCORESCREEN) {
global.gameover = GAMEOVER_SCORESCREEN;
} else {
global.gameover = GAMEOVER_TRANSITIONING;
CallChain cc = CALLCHAIN(stage_finalize, (void*)(intptr_t)gameover);
set_transition(TransFadeBlack, FADE_TIME, FADE_TIME*2, cc);
2020-06-24 19:51:00 +02:00
audio_bgm_stop(BGM_FADE_LONG);
}
if(
global.replay.input.replay == NULL &&
prev_gameover != GAMEOVER_SCORESCREEN &&
(gameover == GAMEOVER_SCORESCREEN || gameover == GAMEOVER_WIN)
) {
StageProgress *p = stageinfo_get_progress(global.stage, global.diff, true);
if(p) {
++p->num_cleared;
2019-04-06 20:27:39 +02:00
log_debug("Stage cleared %u times now", p->num_cleared);
}
}
if(gameover == GAMEOVER_SCORESCREEN && stage_is_skip_mode()) {
// don't get stuck in an infinite loop
taisei_quit();
}
2017-02-26 13:17:48 +01:00
}
static void stage_preload(void) {
difficulty_preload();
projectiles_preload();
player_preload();
items_preload();
boss_preload();
laserdraw_preload();
enemies_preload();
2019-09-12 17:33:08 +02:00
if(global.stage->type != STAGE_SPELL) {
2020-06-24 19:51:00 +02:00
preload_resource(RES_BGM, "gameover", RESF_DEFAULT);
2019-09-12 17:33:08 +02:00
dialog_preload();
}
global.stage->procs->preload();
}
static void display_stage_title(StageInfo *info) {
stagetext_add(info->title, VIEWPORT_W/2 + I * (VIEWPORT_H/2-40), ALIGN_CENTER, res_font("big"), RGB(1, 1, 1), 50, 85, 35, 35);
stagetext_add(info->subtitle, VIEWPORT_W/2 + I * (VIEWPORT_H/2), ALIGN_CENTER, res_font("standard"), RGB(1, 1, 1), 60, 85, 35, 35);
}
TASK(start_bgm, { BGM *bgm; }) {
audio_bgm_play(ARGS.bgm, true, 0, 0);
}
void stage_start_bgm(const char *bgm) {
INVOKE_TASK_DELAYED(1, start_bgm, res_bgm(bgm));
}
void stage_set_voltage_thresholds(uint easy, uint normal, uint hard, uint lunatic) {
switch(global.diff) {
case D_Easy: global.voltage_threshold = easy; return;
case D_Normal: global.voltage_threshold = normal; return;
case D_Hard: global.voltage_threshold = hard; return;
case D_Lunatic: global.voltage_threshold = lunatic; return;
default: UNREACHABLE;
}
}
bool stage_is_cleared(void) {
return global.gameover == GAMEOVER_SCORESCREEN || global.gameover == GAMEOVER_TRANSITIONING;
}
static void stage_update_fps(StageFrameState *fstate) {
if(global.replay.output.replay) {
2017-12-26 12:07:40 +01:00
uint16_t replay_fps = (uint16_t)rint(global.fps.logic.fps);
if(replay_fps != fstate->last_replay_fps) {
replay_stage_event(global.replay.output.stage, global.frames, EV_FPS, replay_fps);
fstate->last_replay_fps = replay_fps;
}
}
}
static void stage_give_clear_bonus(const StageInfo *stage, StageClearBonus *bonus) {
memset(bonus, 0, sizeof(*bonus));
// FIXME: this is clunky...
if(!global.is_practice_mode && stage->type == STAGE_STORY) {
StageInfo *next = stageinfo_get_by_id(stage->id + 1);
if(next == NULL || next->type != STAGE_STORY) {
bonus->all_clear = true;
}
}
if(stage->type == STAGE_STORY) {
bonus->base = stage->id * 1000000;
}
if(bonus->all_clear) {
bonus->base += global.plr.point_item_value * 100;
// TODO redesign this
// bonus->graze = global.plr.graze * (global.plr.point_item_value / 10);
}
bonus->voltage = imax(0, (int)global.plr.voltage - (int)global.voltage_threshold) * (global.plr.point_item_value / 25);
bonus->lives = global.plr.lives * global.plr.point_item_value * 5;
// TODO: maybe a difficulty multiplier?
bonus->total = bonus->base + bonus->voltage + bonus->lives + bonus->graze;
2019-04-07 00:55:13 +02:00
player_add_points(&global.plr, bonus->total, global.plr.pos);
}
2020-02-28 12:36:22 +01:00
INLINE bool stage_should_yield(void) {
return (global.boss && !boss_is_fleeing(global.boss)) || dialog_is_active(global.dialog);
}
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
static LogicFrameAction stage_logic_frame(void *arg) {
StageFrameState *fstate = arg;
StageInfo *stage = fstate->stage;
stage_update_fps(fstate);
2018-10-02 02:03:51 +02:00
if(stage_is_skip_mode()) {
global.plr.iddqd = true;
}
2020-05-16 22:42:22 +02:00
fapproach_p(&fstate->view_shake, 0, 1);
fapproach_asymptotic_p(&fstate->view_shake, 0, 0.05, 1e-2);
2018-10-02 02:03:51 +02:00
if(
global.replay.input.replay == NULL &&
fstate->dynstage_generation != dynstage_get_generation()
) {
log_info("Stages library updated, attempting to hot-reload");
stage_do_quicksave(fstate, true); // no-op if user has a manual save
stage_do_quickload(fstate);
}
if(global.gameover == GAMEOVER_TRANSITIONING) {
// Usually stage_comain will do this
events_poll(NULL, 0);
} else {
cosched_run_tasks(&fstate->sched);
if(global.gameover == GAMEOVER_SCORESCREEN && global.frames - global.gameover_time == GAMEOVER_SCORE_DELAY) {
StageClearBonus b;
stage_give_clear_bonus(stage, &b);
stage_display_clear_screen(&b);
}
2018-07-24 20:30:45 +02:00
if(stage->type == STAGE_SPELL && !global.boss && !fstate->transition_delay) {
fstate->transition_delay = 120;
}
}
uint16_t desync_check = (rng_u64() ^ global.plr.points) & 0xFFFF;
ReplaySyncStatus rpsync = replay_state_check_desync(&global.replay.input, global.frames, desync_check);
if(
global.replay.output.stage &&
fstate->desync_check_freq > 0 &&
!(global.frames % fstate->desync_check_freq)
) {
replay_stage_event(global.replay.output.stage, global.frames, EV_CHECK_DESYNC, desync_check);
}
if(rpsync == REPLAY_SYNC_FAIL) {
if(
global.is_replay_verification &&
!global.replay.output.stage
) {
exit(1);
}
if(fstate->quicksave && fstate->quicksave == global.replay.input.replay) {
log_warn("Quicksave replay desynced; resuming prematurely!");
leave_replay_mode(fstate, &global.replay.input);
}
}
2017-12-28 11:46:08 +01:00
if(fstate->transition_delay) {
2018-07-24 20:30:45 +02:00
if(!--fstate->transition_delay) {
stage_finish(GAMEOVER_WIN);
}
2017-12-28 11:46:08 +01:00
} else {
update_transition();
}
if(global.replay.input.replay == NULL && global.plr.points > progress.hiscore) {
progress.hiscore = global.plr.points;
}
if(fstate->quickload_requested) {
log_info("Quickload initiated");
return LFRAME_STOP;
}
if(global.gameover > 0) {
2017-12-26 12:07:40 +01:00
return LFRAME_STOP;
}
if(is_quickloading(fstate)) {
return LFRAME_SKIP_ALWAYS;
}
LogicFrameAction skipmode = skipstate_handle_frame();
if(skipmode != LFRAME_WAIT) {
return skipmode;
}
2021-08-19 00:58:08 +02:00
if(global.replay.input.replay && gamekeypressed(KEY_SKIP)) {
2017-12-26 12:07:40 +01:00
return LFRAME_SKIP;
}
return LFRAME_WAIT;
}
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
static RenderFrameAction stage_render_frame(void *arg) {
2017-12-26 12:07:40 +01:00
StageFrameState *fstate = arg;
StageInfo *stage = fstate->stage;
if(stage_is_skip_mode()) {
return RFRAME_DROP;
}
rng_lock(&global.rand_game);
rng_make_active(&global.rand_visual);
BEGIN_DRAW_CODE();
stage_draw_scene(stage);
END_DRAW_CODE();
rng_unlock(&global.rand_game);
rng_make_active(&global.rand_game);
draw_transition();
2017-12-26 12:07:40 +01:00
return RFRAME_SWAP;
}
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
static void stage_end_loop(void *ctx);
static void stage_stub_proc(void) { }
TASK(stage_comain, { StageFrameState *fstate; }) {
StageFrameState *fstate = ARGS.fstate;
StageInfo *stage = fstate->stage;
stage->procs->begin();
player_stage_post_init(&global.plr);
if(global.stage->type != STAGE_SPELL) {
display_stage_title(stage);
}
for(;;YIELD) {
if(global.replay.input.replay != NULL) {
replay_input(fstate);
} else {
stage_input(fstate);
}
if(!stage_should_yield()) {
stage->procs->event();
}
stage->procs->update();
stage_logic();
}
}
static void _stage_enter(
StageInfo *stage, CallChain next, Replay *quickload, bool quicksave_is_automatic
) {
2017-02-26 13:17:48 +01:00
assert(stage);
assert(stage->procs);
if(global.gameover == GAMEOVER_WIN) {
global.gameover = 0;
} else if(global.gameover) {
run_call_chain(&next, NULL);
return;
}
uint32_t dynstage_generation = dynstage_get_generation();
stageinfo_reload();
#define STUB_PROC(proc, stub) do {\
if(!stage->procs->proc) { \
stage->procs->proc = stub; \
log_debug(#proc " proc is missing"); \
} \
} while(0)
static const ShaderRule shader_rules_stub[1] = { NULL };
STUB_PROC(preload, stage_stub_proc);
STUB_PROC(begin, stage_stub_proc);
STUB_PROC(end, stage_stub_proc);
STUB_PROC(draw, stage_stub_proc);
STUB_PROC(event, stage_stub_proc);
STUB_PROC(update, stage_stub_proc);
STUB_PROC(shader_rules, (ShaderRule*)shader_rules_stub);
2017-02-26 13:17:48 +01:00
if(quickload) {
assert(global.replay.input.stage == NULL);
ReplayStage *qload_stage = dynarray_get_ptr(&quickload->stages, 0);
assert(qload_stage->stage == stage->id);
replay_state_init_play(&global.replay.input, quickload, qload_stage);
}
// I really want to separate all of the game state from the global struct sometime
2017-02-26 13:17:48 +01:00
global.stage = stage;
Lots of disorganized (mostly) visual overhaul (#156) * WIP some projectile effects * fix segfault * Laser smoothing and glow via post-processing blur magic TODO: make it optional * fix memory corruption * fix memory corruption for realsies now * fix color_get_hsl for out-of-range colors * some more bullet flare tweaks * some lame clear effect workarounds * spawn bullet flares after frame 0; looks better and fixes some problems * New baryon explosion; fix petal_explosion; leanify everything * Add missing bullet flare sprite, rebuild main atlas * improve batching efficiency with bullet spawn flares * forgot git add * Group projectiles/particles by shader where possible * Another take on baryon explosion; make fg framebuffers 16bit * WIP some settings for toasters * remove stupid debug log * microoptimization that probably does nothing anyway * somewhat more intuitive quality settings * Whitelist more particles (MarisaB is on hold) * Whitelist (and fix) some more stage6 particles (mostly ToE) * Add a spell name background * Experimental radial healthbar for bosses * healthbar tweaks * thiccer healthbars in response to feedback * remove healthbar survival timer; just fade out on survivals * Add linear healthbars option; WIP other boss HUD tweaks * Use the proper spell card name format * New font and some random garbage to go along with it * Generate static font outlines for use in text shaders * Use outlines in overlay text shader * Complete boss HUD/healthbar fading logic * fix boss timer limit * stage5 bombs explosion effect * split PFLAG_NOSPAWNZOOM into PFLAG_NOSPAWNFLARE and PFLAG_NOSPAWNFADE; introduce PFLAG_NOSPAWNEFFECTS which disables both (it's just the two values OR'd together) simplify vampiric vapor bullet spawning effect * Remove spawn fade-in from super-fast stage5 fairy projectiles (limiters) * lower particle density in v.vapor in minimal mode * graze effect tweaks * fix text shortening, tweak replay menu layout * stupid debug spam * revisit grazing effects again * dumb debug spam again * improve boss attack timer * overlay effect for boss deaths (similar to the player one) * spice up spellcard declaration (HUD) * don't spawn boss death overlay if fleed * modify Exo2 font to use tabular figures * adjust replay menu for the font change * draw timer & power with standard font (phasing out the numbers font) * WIP new HUD; random fixes/tweaks * hud: move difficulty indicator * hud: move debug stuff around * preloads, mostly * fix youmuA batching conflict * shitty workaround for the shitty screenshake shit * remove extraspell lag by stopping to draw stagebg sooner which is possible because extra spells have a different spellcard_intro timing. Fun fact of the day: the duration of spellcard_intro is always ATTACK_START_DELAY_EXTRA even for normal spells! * new stain particle * i disabled background rendering… * "batch" marisa_b masterspark draws * remove these once a new atlas is generated * make toe quick again * hopefully fix all occurences of changed stain and ScaleFade behavior * tweaking reimu_a and toe boson launch effects * make lhc fast again * softer involnerability effect * fix stage 1 snow on the water bug (and improve performance) translated the time to the future a bit because it only seemed to be an issue for small time values * remove unnecessary spawnflare from toe * tone down extra spell start effect * experimental ReimuB gap shader optimization * fix python3 shebangs * generate simple blur shaders w/ hardcoded kernels * New loading screen * lasers: fix incorrect draw hook registration * add webp support for atlas generator * Use ImageMagick for atlas composition (adds 16-bit support) * Atlas maintenance * make the vampiric vapor bullets less prone to invisibility * Revert a few particles to the quadratic fade curve * experimental baryon effect * improve baryon sprites * disable the baryon effect on minimal postprocessing setting
2019-01-04 23:59:39 +01:00
ent_init();
stage_objpools_init();
stage_draw_pre_init();
stage_preload();
stage_draw_init();
lasers_init();
rng_make_active(&global.rand_game);
2017-02-26 13:17:48 +01:00
stage_start(stage);
uint64_t start_time, seed;
if(global.replay.input.replay) {
ReplayStage *rstg = global.replay.input.stage;
assert(rstg != NULL);
assert(stageinfo_get_by_id(rstg->stage) == stage);
start_time = rstg->start_time;
seed = rstg->rng_seed;
global.diff = rstg->diff;
log_debug("REPLAY_PLAY mode: %d events, stage: \"%s\"", rstg->events.num_elements, stage->title);
} else {
start_time = (uint64_t)time(0);
seed = makeseed();
StageProgress *p = stageinfo_get_progress(stage, global.diff, true);
if(p) {
log_debug("You played this stage %u times", p->num_played);
log_debug("You cleared this stage %u times", p->num_cleared);
++p->num_played;
p->unlocked = true;
}
}
rng_seed(&global.rand_game, seed);
if(global.replay.input.replay) {
player_init(&global.plr);
replay_stage_sync_player_state(global.replay.input.stage, &global.plr);
}
if(global.replay.output.replay) {
global.replay.output.stage = replay_stage_new(
global.replay.output.replay,
stage,
start_time,
seed,
global.diff,
&global.plr
);
player_init(&global.plr);
replay_stage_sync_player_state(global.replay.output.stage, &global.plr);
}
auto fstate = ALLOC(StageFrameState, {
.stage = stage,
.cc = next,
.quicksave = quickload,
.quicksave_is_automatic = quicksave_is_automatic,
.desync_check_freq = env_get("TAISEI_REPLAY_DESYNC_CHECK_FREQUENCY", FPS * 5),
.dynstage_generation = dynstage_generation,
});
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
cosched_init(&fstate->sched);
2020-05-16 22:42:22 +02:00
_current_stage_state = fstate;
skipstate_init();
if(is_quickloading(fstate)) {
audio_sfx_set_enabled(false);
}
SCHED_INVOKE_TASK(&fstate->sched, stage_comain, fstate);
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
eventloop_enter(fstate, stage_logic_frame, stage_render_frame, stage_end_loop, FPS);
}
void stage_enter(StageInfo *stage, CallChain next) {
_stage_enter(stage, next, NULL, false);
}
void stage_end_loop(void *ctx) {
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
StageFrameState *s = ctx;
2020-05-16 22:42:22 +02:00
assert(s == _current_stage_state);
Replay *quicksave = s->quicksave;
bool quicksave_is_automatic = s->quicksave_is_automatic;
bool is_quickload = s->quickload_requested;
if(is_quickload) {
assume(quicksave != NULL);
}
if(global.replay.output.replay) {
if(is_quickload) {
// rollback this stage, as we're about to replay it
global.replay.output.replay->stages.num_elements--;
replay_stage_destroy_events(global.replay.output.stage);
} else {
replay_stage_event(global.replay.output.stage, global.frames, EV_OVER, 0);
global.replay.output.stage->plr_points_final = global.plr.points;
if(global.gameover == GAMEOVER_WIN) {
global.replay.output.stage->flags |= REPLAY_SFLAG_CLEAR;
}
}
}
if(quicksave && !is_quickload) {
replay_reset(quicksave);
mem_free(quicksave);
}
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
s->stage->procs->end();
stage_draw_shutdown();
cosched_finish(&s->sched);
2017-02-26 13:17:48 +01:00
stage_free();
2017-11-25 16:51:43 +01:00
player_free(&global.plr);
ent_shutdown();
rng_make_active(&global.rand_visual);
stop_all_sfx();
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
taisei_commit_persistent_data();
skipstate_shutdown();
if(taisei_quit_requested()) {
global.gameover = GAMEOVER_ABORT;
}
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
2020-05-16 22:42:22 +02:00
_current_stage_state = NULL;
StageInfo *stginfo = s->stage;
2020-05-16 22:42:22 +02:00
CallChain cc = s->cc;
mem_free(s);
2020-05-16 22:42:22 +02:00
if(is_quickload) {
_stage_enter(stginfo, cc, quicksave, quicksave_is_automatic);
} else {
run_call_chain(&cc, NULL);
}
}
2019-03-11 00:21:43 +01:00
void stage_unlock_bgm(const char *bgm) {
if(global.replay.input.replay == NULL && !global.plr.stats.total.continues_used) {
2019-03-11 00:21:43 +01:00
progress_unlock_bgm(bgm);
}
}
2020-05-16 22:42:22 +02:00
void stage_shake_view(float strength) {
assume(strength >= 0);
_current_stage_state->view_shake += strength;
}
float stage_get_view_shake_strength(void) {
if(_current_stage_state) {
return _current_stage_state->view_shake;
}
return 0;
}
void stage_load_quicksave(void) {
stage_do_quickload(NOT_NULL(_current_stage_state));
}
CoSched *stage_get_sched(void) {
return &NOT_NULL(_current_stage_state)->sched;
}