2010-10-27 19:51:49 +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-03-05 13:44:21 +01: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>.
|
2010-10-27 19:51:49 +02:00
|
|
|
*/
|
|
|
|
|
2021-08-12 23:09:01 +02:00
|
|
|
#pragma once
|
2017-11-25 20:45:11 +01:00
|
|
|
#include "taisei.h"
|
2010-10-27 19:51:49 +02:00
|
|
|
|
2019-09-24 21:58:05 +02:00
|
|
|
#if TAISEI_BUILDCONF_MALLOC_ALIGNMENT < 16
|
|
|
|
#define LIST_ALIGN alignas(TAISEI_BUILDCONF_MALLOC_ALIGNMENT)
|
|
|
|
#else
|
|
|
|
#define LIST_ALIGN alignas(16)
|
|
|
|
#endif
|
2019-02-02 12:25:06 +01:00
|
|
|
|
2017-12-24 07:16:25 +01:00
|
|
|
typedef struct ListInterface ListInterface;
|
|
|
|
typedef struct List List;
|
2018-06-01 20:40:18 +02:00
|
|
|
typedef struct ListAnchorInterface ListAnchorInterface;
|
|
|
|
typedef struct ListAnchor ListAnchor;
|
2017-12-24 07:16:25 +01:00
|
|
|
typedef struct ListContainer ListContainer;
|
|
|
|
|
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
|
|
|
#define LIST_INTERFACE_BASE(typename) struct { \
|
2018-01-12 19:26:07 +01:00
|
|
|
typename *next; \
|
|
|
|
typename *prev; \
|
2017-12-23 22:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define LIST_INTERFACE(typename) union { \
|
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
|
|
|
LIST_ALIGN ListInterface list_interface; \
|
2018-01-12 19:26:07 +01:00
|
|
|
LIST_INTERFACE_BASE(typename); \
|
2017-12-23 22:56:14 +01:00
|
|
|
}
|
|
|
|
|
2017-12-24 07:16:25 +01:00
|
|
|
struct ListInterface {
|
2018-01-12 19:26:07 +01:00
|
|
|
LIST_INTERFACE_BASE(ListInterface);
|
2017-12-24 07:16:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct List {
|
2018-01-12 19:26:07 +01:00
|
|
|
LIST_INTERFACE(List);
|
2017-12-24 07:16:25 +01:00
|
|
|
};
|
2017-09-29 21:03:49 +02:00
|
|
|
|
2018-06-01 20:40:18 +02:00
|
|
|
#define LIST_ANCHOR_INTERFACE_BASE(typename) struct { \
|
|
|
|
typename *first; \
|
|
|
|
typename *last; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LIST_ANCHOR_INTERFACE(typename) union { \
|
|
|
|
ListAnchorInterface list_anchor_interface; \
|
|
|
|
LIST_ANCHOR_INTERFACE_BASE(typename); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LIST_ANCHOR(typename) struct { \
|
|
|
|
LIST_ANCHOR_INTERFACE(typename); \
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ListAnchorInterface {
|
|
|
|
LIST_ANCHOR_INTERFACE_BASE(ListInterface);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ListAnchor {
|
|
|
|
LIST_ANCHOR_INTERFACE(List);
|
|
|
|
};
|
|
|
|
|
2017-12-24 07:16:25 +01:00
|
|
|
struct ListContainer {
|
2018-01-12 19:26:07 +01:00
|
|
|
LIST_INTERFACE(ListContainer);
|
|
|
|
void *data;
|
2017-12-24 07:16:25 +01:00
|
|
|
};
|
2017-03-02 11:23:30 +01:00
|
|
|
|
2017-11-21 15:45:01 +01:00
|
|
|
typedef void* (*ListForeachCallback)(List **head, List *elem, void *arg);
|
2018-06-01 20:40:18 +02:00
|
|
|
typedef void* (*ListAnchorForeachCallback)(ListAnchor *list, List *elem, void *arg);
|
2017-11-23 16:27:41 +01:00
|
|
|
typedef List* (*ListInsertionRule)(List **dest, List *elem);
|
2018-06-01 20:40:18 +02:00
|
|
|
typedef List* (*ListAnchorInsertionRule)(ListAnchor *dest, List *elem);
|
|
|
|
typedef int (*ListPriorityFunc)(List *elem);
|
2017-11-21 15:45:01 +01:00
|
|
|
|
2018-04-12 16:08:48 +02:00
|
|
|
List* list_insert(List **dest, List *elem) attr_nonnull(1, 2);
|
|
|
|
List* list_push(List **dest, List *elem) attr_nonnull(1, 2);
|
|
|
|
List* list_append(List **dest, List *elem) attr_nonnull(1, 2);
|
|
|
|
List* list_insert_at_priority_head(List **dest, List *elem, int prio, ListPriorityFunc prio_func) attr_hot attr_nonnull(1, 2, 4);
|
|
|
|
List* list_insert_at_priority_tail(List **dest, List *elem, int prio, ListPriorityFunc prio_func) attr_hot attr_nonnull(1, 2, 4);
|
|
|
|
List* list_pop(List **dest) attr_nonnull(1);
|
|
|
|
List* list_unlink(List **dest, List *elem) attr_nonnull(1, 2);
|
|
|
|
void* list_foreach(List **dest, ListForeachCallback callback, void *arg) attr_nonnull(1, 2);
|
2017-11-21 15:45:01 +01:00
|
|
|
void* list_callback_free_element(List **dest, List *elem, void *arg);
|
2018-04-12 16:08:48 +02:00
|
|
|
void list_free_all(List **dest) attr_nonnull(1);
|
2018-06-01 20:40:18 +02:00
|
|
|
|
|
|
|
List* alist_insert(ListAnchor *list, List *ref, List *elem) attr_nonnull(1, 3);
|
|
|
|
List* alist_push(ListAnchor *list, List *elem) attr_nonnull(1, 2);
|
|
|
|
List* alist_append(ListAnchor *list, List *elem) attr_nonnull(1, 2);
|
|
|
|
List* alist_insert_at_priority_head(ListAnchor *list, List *elem, int prio, ListPriorityFunc prio_func) attr_hot attr_nonnull(1, 2, 4);
|
|
|
|
List* alist_insert_at_priority_tail(ListAnchor *list, List *elem, int prio, ListPriorityFunc prio_func) attr_hot attr_nonnull(1, 2, 4);
|
|
|
|
List* alist_pop(ListAnchor *list) attr_nonnull(1);
|
|
|
|
List* alist_unlink(ListAnchor *list, List *elem) attr_nonnull(1, 2);
|
2020-03-31 21:09:06 +02:00
|
|
|
void alist_merge_tail(ListAnchor *dest, ListAnchor *src) attr_nonnull(1, 2);
|
2018-06-01 20:40:18 +02:00
|
|
|
void* alist_foreach(ListAnchor *list, ListAnchorForeachCallback callback, void *arg) attr_nonnull(1, 2);
|
|
|
|
void* alist_callback_free_element(ListAnchor *list, List *elem, void *arg);
|
|
|
|
void alist_free_all(ListAnchor *list) attr_nonnull(1);
|
|
|
|
|
2019-08-04 00:29:41 +02:00
|
|
|
ListContainer* list_wrap_container(void *data) attr_returns_allocated;
|
2017-11-21 15:45:01 +01:00
|
|
|
|
|
|
|
// type-generic macros
|
|
|
|
|
2021-08-12 16:12:40 +02:00
|
|
|
#define LIST_CAST(expr) ({ \
|
|
|
|
static_assert(__builtin_types_compatible_p( \
|
|
|
|
ListInterface, __typeof__((*(expr)).list_interface)), \
|
|
|
|
"struct must implement ListInterface (use the LIST_INTERFACE macro)"); \
|
|
|
|
static_assert(__builtin_offsetof(__typeof__(*(expr)), list_interface) == 0, \
|
|
|
|
"list_interface must be the first member in struct"); \
|
|
|
|
CASTPTR_ASSUME_ALIGNED((expr), List); \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define LIST_CAST_2(expr) ({ \
|
|
|
|
static_assert(__builtin_types_compatible_p(\
|
|
|
|
ListInterface, __typeof__((**(expr)).list_interface)), \
|
|
|
|
"struct must implement ListInterface (use the LIST_INTERFACE macro)"); \
|
|
|
|
static_assert(__builtin_offsetof(__typeof__(**(expr)), list_interface) == 0, \
|
|
|
|
"list_interface must be the first member in struct"); \
|
|
|
|
(void)ASSUME_ALIGNED(*(expr), alignof(List)); \
|
|
|
|
(List**)(expr); \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define LIST_ANCHOR_CAST(expr) ({ \
|
|
|
|
static_assert(__builtin_types_compatible_p(\
|
|
|
|
ListAnchorInterface, __typeof__((*(expr)).list_anchor_interface)), \
|
|
|
|
"struct must implement ListAnchorInterface (use the LIST_ANCHOR_INTERFACE macro)"); \
|
|
|
|
static_assert(__builtin_offsetof(__typeof__(*(expr)), list_anchor_interface) == 0, \
|
|
|
|
"list_anchor_interface must be the first member in struct"); \
|
|
|
|
CASTPTR_ASSUME_ALIGNED((expr), ListAnchor); \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define LIST_CAST_RETURN(e_typekey, e_return) \
|
|
|
|
CASTPTR_ASSUME_ALIGNED((e_return), __typeof__(*(e_typekey)))
|
2017-12-24 07:16:25 +01:00
|
|
|
|
|
|
|
#define list_insert(dest,elem) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, list_insert(LIST_CAST_2(dest), LIST_CAST(elem))))
|
2017-12-24 07:16:25 +01:00
|
|
|
|
2018-06-01 20:40:18 +02:00
|
|
|
#define alist_insert(dest,ref,elem) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, alist_insert(LIST_ANCHOR_CAST(dest), LIST_CAST(ref), LIST_CAST(elem))))
|
2018-06-01 20:40:18 +02:00
|
|
|
|
2017-12-24 07:16:25 +01:00
|
|
|
#define list_push(dest,elem) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, list_push(LIST_CAST_2(dest), LIST_CAST(elem))))
|
2017-12-24 07:16:25 +01:00
|
|
|
|
2018-06-01 20:40:18 +02:00
|
|
|
#define alist_push(dest,elem) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, alist_push(LIST_ANCHOR_CAST(dest), LIST_CAST(elem))))
|
2018-06-01 20:40:18 +02:00
|
|
|
|
2017-12-24 07:16:25 +01:00
|
|
|
#define list_append(dest,elem) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, list_append(LIST_CAST_2(dest), LIST_CAST(elem))))
|
2017-12-24 07:16:25 +01:00
|
|
|
|
2018-06-01 20:40:18 +02:00
|
|
|
#define alist_append(dest,elem) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, alist_append(LIST_ANCHOR_CAST(dest), LIST_CAST(elem))))
|
2018-06-01 20:40:18 +02:00
|
|
|
|
2018-01-09 20:52:20 +01:00
|
|
|
#define list_insert_at_priority_head(dest,elem,prio,prio_func) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, list_insert_at_priority_head(LIST_CAST_2(dest), LIST_CAST(elem), prio, prio_func)))
|
2018-01-09 20:52:20 +01:00
|
|
|
|
2018-06-01 20:40:18 +02:00
|
|
|
#define alist_insert_at_priority_head(dest,elem,prio,prio_func) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, alist_insert_at_priority_head(LIST_ANCHOR_CAST(dest), LIST_CAST(elem), prio, prio_func)))
|
2018-06-01 20:40:18 +02:00
|
|
|
|
2018-01-09 20:52:20 +01:00
|
|
|
#define list_insert_at_priority_tail(dest,elem,prio,prio_func) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, list_insert_at_priority_tail(LIST_CAST_2(dest), LIST_CAST(elem), prio, prio_func)))
|
2017-12-24 07:16:25 +01:00
|
|
|
|
2018-06-01 20:40:18 +02:00
|
|
|
#define alist_insert_at_priority_tail(dest,elem,prio,prio_func) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, alist_insert_at_priority_tail(LIST_ANCHOR_CAST(dest), LIST_CAST(elem), prio, prio_func)))
|
2018-06-01 20:40:18 +02:00
|
|
|
|
2017-12-24 07:16:25 +01:00
|
|
|
#define list_pop(dest) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(*(dest), list_pop(LIST_CAST_2(dest))))
|
2017-12-24 07:16:25 +01:00
|
|
|
|
2018-06-01 20:40:18 +02:00
|
|
|
#define alist_pop(dest) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN((dest)->first, alist_pop(LIST_ANCHOR_CAST(dest))))
|
2018-06-01 20:40:18 +02:00
|
|
|
|
2017-12-24 07:16:25 +01:00
|
|
|
#define list_unlink(dest,elem) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, list_unlink(LIST_CAST_2(dest), LIST_CAST(elem))))
|
2017-12-24 07:16:25 +01:00
|
|
|
|
2018-06-01 20:40:18 +02:00
|
|
|
#define alist_unlink(dest,elem) \
|
2019-02-02 12:25:06 +01:00
|
|
|
(LIST_CAST_RETURN(elem, alist_unlink(LIST_ANCHOR_CAST(dest), LIST_CAST(elem))))
|
2018-06-01 20:40:18 +02:00
|
|
|
|
2020-03-31 21:09:06 +02:00
|
|
|
#define alist_merge_tail(dest,src) \
|
|
|
|
alist_merge_tail(LIST_ANCHOR_CAST(dest), LIST_ANCHOR_CAST(src))
|
|
|
|
|
2017-12-24 07:16:25 +01:00
|
|
|
#define list_foreach(dest,callback,arg) \
|
2019-02-02 12:25:06 +01:00
|
|
|
list_foreach(LIST_CAST_2(dest), callback, arg)
|
2017-12-24 07:16:25 +01:00
|
|
|
|
2018-06-01 20:40:18 +02:00
|
|
|
#define alist_foreach(dest,callback,arg) \
|
2019-02-02 12:25:06 +01:00
|
|
|
alist_foreach(LIST_ANCHOR_CAST(dest), callback, arg)
|
2018-06-01 20:40:18 +02:00
|
|
|
|
2017-12-24 07:16:25 +01:00
|
|
|
#define list_free_all(dest) \
|
2019-02-02 12:25:06 +01:00
|
|
|
list_free_all(LIST_CAST_2(dest))
|
2017-11-21 15:45:01 +01:00
|
|
|
|
2018-06-01 20:40:18 +02:00
|
|
|
#define alist_free_all(dest) \
|
2019-02-02 12:25:06 +01:00
|
|
|
alist_free_all(LIST_ANCHOR_CAST(dest))
|