3 \details initializes necessary subsystems before invoking the preloader,
4 which loads initial game data, before finally invoking the
5 main loop gameloop(void)
8 ------------------------------------------------------------------------------*/
13 #include <emscripten/emscripten.h>
15 /* Traditional Environment */
19 #include <SDL2/SDL_ttf.h>
21 /* ENVIRONMENT-AGNOSTIC DEFINES */
23 #include <SDL2/SDL_image.h>
27 #define TRIGGERS quit_trigger
28 #include <core/trigger.h>
29 #include <core/engine.h>
30 #include <wolfssl/wolfcrypt/sha256.h>
32 /* exposed functions */
35 /* private functions */
36 static int main_init(void);
38 /* unexposed externs *
39 extern int state_init(void);
40 extern void state_tick(uint32_t delta_ticks);
41 extern const char* state_get_error(void);
42 extern void state_quit(void);
43 extern void state_handle_event(SDL_Event event);
44 extern int io_init(void);
45 extern const char* io_get_error(void);
46 extern void io_quit(void);*/
48 /* main jump buffer */
52 initializes subsystems and calls main_loop(void)
54 main sets a jump buffer for its primary switch, which may be jumped to
55 at any time. Jumping with a 0 return value is equivalent to calling
56 setjmp directly, which initializes the system and begins the main loop.
57 Jumping with any other value will process one of the directives associated
58 with the exit codes in core.h
62 emscripten_set_main_loop(main_loop,0,0);\
63 TRIGGER_SET(quit_trigger, emscripten_cancel_main_loop);\
68 main (int argc
, char** argv
)
70 wc_InitSha256(&shstr
);
72 printf("%d %d\n", sizeof(long), sizeof(long long));
74 switch(setjmp(jmp_main
))
83 //dump some debug info
86 TRIGGER(quit_trigger
);
94 /** subsystem initializer
95 Calling main_init() bootstraps the system, and may be called multiple
96 times to cause a system-wide reboot.
97 @return 0 if successful, -1 SDL, -2 IMG, -3 TTF, -4 STATE.
98 SDL and logging is available after this is called
99 ******************************************************************************/
100 #define INIT(_subsys_id,_cond,_errorstring,_quit)\
102 { fprintf(stderr, #_cond " failed: %s\n", _errorstring());\
105 TRIGGER_SET(quit_trigger, _quit)
107 #define SDL_FLAGS SDL_INIT_EVERYTHING & ~(SDL_INIT_TIMER | SDL_INIT_HAPTIC)
112 { static char bInitialized
= 0;
114 { TRIGGER(quit_trigger
);
115 SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION
, "Resetting [%d]\n",
119 INIT(1, SDL_Init(SDL_FLAGS
) < 0, SDL_GetError
, SDL_Quit
);
120 INIT(2, IMG_Init(IMG_INIT_PNG
) != IMG_INIT_PNG
, IMG_GetError
, IMG_Quit
);
121 INIT(3, TTF_Init() == -1, TTF_GetError
, TTF_Quit
);
124 INIT(4, io_init(), io_get_error, io_quit);
125 INIT(5, state_init(), state_get_error, state_quit);
128 SDL_Log("Initialization Complete.");
133 *******************************************************************************/
136 { static uint32_t main_loop_last_ticks
= 0;
138 uint32_t delta_ticks
;
140 #ifdef LOOP_YIELD_OPTIONAL
143 /* Poll events (user/system inputs) */
144 while (SDL_PollEvent(&event
))
145 //state_handle_event(&event);
148 /* change in time since last loop */
149 delta_ticks
= SDL_GetTicks() - main_loop_last_ticks
;
151 /* handle breakpoints (long pause likely a breakpoint) */
152 if (delta_ticks
> 1000)
153 { SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM
, "Recovering from long pause.");
154 delta_ticks
= TARGET_DT
;
157 /* tick the state manager forward by the change in time */
158 //state_tick(delta_ticks);
159 main_loop_last_ticks
= SDL_GetTicks();
161 /* render the scene to backbuffer */
162 ////TODO: state_tick(delta_ticks);
163 /* swap in the backbuffer for display */
164 ////TODO: state_render();
166 #ifdef LOOP_YIELD_OPTIONAL
167 #define DIV_UP(x,y) ((x + (y / 2)) / y)
169 /* cap the framerate if we're handling the loop mechanism directly, but only
170 yield if we have a substantial portion of time to yield (1/10th of the
172 delta_ticks
= SDL_GetTicks() - main_loop_last_ticks
;
173 if ((delta_ticks
+ DIV_UP(TARGET_DT
, 10)) < TARGET_DT
)
174 SDL_Delay(TARGET_DT
- delta_ticks
);