2011-06-13 18:48:36 +02:00
|
|
|
/*
|
2019-08-03 19:43:48 +02:00
|
|
|
* This software is licensed under the terms of the MIT License.
|
2017-02-11 04:52:08 +01:00
|
|
|
* See COPYING for further information.
|
2011-06-13 18:48:36 +02:00
|
|
|
* ---
|
2024-05-16 23:30:41 +02:00
|
|
|
* Copyright (c) 2011-2024, Lukas Weber <laochailan@web.de>.
|
|
|
|
* Copyright (c) 2012-2024, Andrei Alexeyev <akari@taisei-project.org>.
|
2011-06-13 18:48:36 +02:00
|
|
|
*/
|
|
|
|
|
2021-08-12 23:09:01 +02:00
|
|
|
#pragma once
|
2017-11-25 20:45:11 +01:00
|
|
|
#include "taisei.h"
|
2011-06-13 18:48:36 +02:00
|
|
|
|
2012-08-14 17:56:53 +02:00
|
|
|
#include "transition.h"
|
2023-04-07 05:27:56 +02:00
|
|
|
#include "util/callchain.h"
|
2020-04-05 04:51:00 +02:00
|
|
|
#include "dynarray.h"
|
2012-08-14 17:56:53 +02:00
|
|
|
|
2024-05-17 04:41:28 +02:00
|
|
|
#include <SDL_events.h>
|
|
|
|
|
2011-06-13 18:48:36 +02:00
|
|
|
#define IMENU_BLUR 0.05
|
|
|
|
|
2017-02-11 04:52:08 +01:00
|
|
|
enum {
|
2012-08-12 16:54:48 +02:00
|
|
|
FADE_TIME = 15
|
|
|
|
};
|
|
|
|
|
2017-02-16 17:55:46 +01:00
|
|
|
typedef struct MenuData MenuData;
|
|
|
|
|
|
|
|
typedef void (*MenuAction)(MenuData*, void*);
|
|
|
|
typedef bool (*MenuCallback)(MenuData*);
|
2017-02-24 22:58:27 +01:00
|
|
|
typedef void (*MenuProc)(MenuData*);
|
2012-08-12 20:16:40 +02:00
|
|
|
|
2017-02-18 10:24:45 +01:00
|
|
|
typedef struct MenuEntry {
|
2011-06-13 18:48:36 +02:00
|
|
|
char *name;
|
2012-08-12 20:16:40 +02:00
|
|
|
MenuAction action;
|
2011-06-13 18:48:36 +02:00
|
|
|
void *arg;
|
2012-08-14 17:56:53 +02:00
|
|
|
float drawdata;
|
|
|
|
TransitionRule transition;
|
2011-06-13 18:48:36 +02:00
|
|
|
} MenuEntry;
|
|
|
|
|
2017-02-11 04:52:08 +01:00
|
|
|
enum MenuFlag {
|
2018-01-12 19:26:07 +01:00
|
|
|
MF_Transient = 1, // the menu will be automatically closed on selection
|
|
|
|
MF_Abortable = 2, // the menu can be closed with the escape key
|
2023-06-08 01:45:48 +02:00
|
|
|
MF_AlwaysProcessInput = 4, // the menu will process input when it's fading out
|
|
|
|
MF_NoDemo = 8, // demo playback is disabled while in this menu
|
2012-08-14 17:56:53 +02:00
|
|
|
};
|
2011-06-13 18:48:36 +02:00
|
|
|
|
2017-02-16 17:55:46 +01:00
|
|
|
enum MenuState {
|
2012-08-12 16:54:48 +02:00
|
|
|
MS_Normal = 0,
|
|
|
|
MS_FadeOut,
|
|
|
|
MS_Dead
|
|
|
|
};
|
|
|
|
|
2017-02-24 22:58:27 +01:00
|
|
|
struct MenuData {
|
2012-08-12 16:54:48 +02:00
|
|
|
int flags;
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2011-06-13 18:48:36 +02:00
|
|
|
int cursor;
|
|
|
|
int selected;
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2020-04-05 04:51:00 +02:00
|
|
|
DYNAMIC_ARRAY(MenuEntry) entries;
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2011-06-13 18:48:36 +02:00
|
|
|
int frames;
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2012-08-12 16:54:48 +02:00
|
|
|
int state;
|
2017-02-24 22:58:27 +01:00
|
|
|
int transition_in_time;
|
|
|
|
int transition_out_time;
|
|
|
|
float fade;
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2012-08-14 17:56:53 +02:00
|
|
|
TransitionRule transition;
|
2017-02-11 04:52:08 +01:00
|
|
|
|
|
|
|
float drawdata[4];
|
|
|
|
|
2011-07-03 15:11:18 +02:00
|
|
|
void *context;
|
2017-02-24 22:58:27 +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
|
|
|
MenuProc begin;
|
2017-02-24 22:58:27 +01:00
|
|
|
MenuProc draw;
|
|
|
|
MenuProc input;
|
|
|
|
MenuProc logic;
|
|
|
|
MenuProc end;
|
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
|
|
|
|
|
|
|
CallChain cc;
|
2017-02-18 10:24:45 +01:00
|
|
|
};
|
2011-06-13 18:48:36 +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
|
|
|
MenuData *alloc_menu(void);
|
|
|
|
void free_menu(MenuData *menu);
|
|
|
|
void close_menu(MenuData *menu);
|
|
|
|
void kill_menu(MenuData *menu);
|
|
|
|
|
|
|
|
// You will get a pointer to the MenuData in callchain result.
|
|
|
|
// WARNING: Unless the menu state is MS_Dead at the time your callback is invoked, it may be called again!
|
|
|
|
// This must be accounted for, otherwise demons will literally fly out of your nose.
|
|
|
|
// No joke. Not *might*, they actually *will*. I wouldn't recommend testing that out.
|
|
|
|
void enter_menu(MenuData *menu, CallChain next);
|
|
|
|
|
2018-05-08 10:21:01 +02:00
|
|
|
MenuEntry *add_menu_entry(MenuData *menu, const char *name, MenuAction action, void *arg);
|
2012-08-14 17:56:53 +02:00
|
|
|
MenuEntry *add_menu_entry_f(MenuData *menu, char *name, MenuAction action, void *arg, int flags);
|
2012-08-10 22:08:51 +02:00
|
|
|
void add_menu_separator(MenuData *menu);
|
2011-06-13 18:48:36 +02:00
|
|
|
|
|
|
|
void menu_input(MenuData *menu);
|
2017-02-24 22:58:27 +01:00
|
|
|
void menu_no_input(MenuData *menu);
|
2011-06-13 18:48:36 +02:00
|
|
|
|
2012-08-12 16:54:48 +02:00
|
|
|
float menu_fade(MenuData *menu);
|
2011-07-05 15:20:19 +02:00
|
|
|
|
2017-09-29 21:03:49 +02:00
|
|
|
bool menu_input_handler(SDL_Event *event, void *arg);
|