2023-01-04 18:09:42 -06:00
|
|
|
#include "yugine.h"
|
2023-12-04 13:38:37 -06:00
|
|
|
#include "transform.h"
|
2022-02-04 11:36:24 -06:00
|
|
|
#include "input.h"
|
2023-09-04 09:48:44 -05:00
|
|
|
#include "render.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "window.h"
|
2023-08-31 17:23:24 -05:00
|
|
|
#include "sound.h"
|
|
|
|
#include "resources.h"
|
2023-12-04 13:38:37 -06:00
|
|
|
#include "spline.h"
|
2023-09-18 07:36:07 -05:00
|
|
|
#include <stdio.h>
|
2024-01-02 07:55:22 -06:00
|
|
|
#include "simplex.h"
|
2024-04-05 23:03:10 -05:00
|
|
|
#include <wctype.h>
|
2024-01-02 07:55:22 -06:00
|
|
|
|
2023-08-30 18:22:32 -05:00
|
|
|
#include "datastream.h"
|
2023-03-24 14:01:01 -05:00
|
|
|
|
2023-04-22 14:07:37 -05:00
|
|
|
#include "quickjs/quickjs.h"
|
|
|
|
|
2023-11-03 22:01:30 -05:00
|
|
|
#include "jsffi.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "script.h"
|
2022-12-23 13:48:29 -06:00
|
|
|
|
2022-11-18 12:03:07 -06:00
|
|
|
#include "log.h"
|
2022-11-24 01:54:17 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-12-22 03:50:40 -06:00
|
|
|
#include "2dphysics.h"
|
|
|
|
|
2022-11-24 01:54:17 -06:00
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
2022-08-14 14:19:36 -05:00
|
|
|
#include "string.h"
|
|
|
|
|
2023-09-18 21:55:37 -05:00
|
|
|
#include "render.h"
|
2023-09-15 03:37:07 -05:00
|
|
|
|
2023-08-22 22:44:09 -05:00
|
|
|
#include "sokol/sokol_app.h"
|
2023-08-31 03:10:30 -05:00
|
|
|
#include "sokol/sokol_audio.h"
|
2023-09-02 06:53:52 -05:00
|
|
|
#include "sokol/sokol_time.h"
|
2023-09-18 07:36:07 -05:00
|
|
|
#include "sokol/sokol_args.h"
|
2024-03-04 11:15:55 -06:00
|
|
|
#include "sokol/sokol_fetch.h"
|
2023-08-31 13:00:33 -05:00
|
|
|
#include <stb_ds.h>
|
|
|
|
#include <stb_truetype.h>
|
|
|
|
#include "stb_image.h"
|
|
|
|
#include "stb_image_write.h"
|
|
|
|
#include <pl_mpeg.h>
|
|
|
|
|
2024-06-05 16:07:44 -05:00
|
|
|
#include "gui.h"
|
|
|
|
|
2023-10-30 17:41:32 -05:00
|
|
|
uint64_t start_t;
|
|
|
|
uint64_t frame_t;
|
2022-12-22 16:58:06 -06:00
|
|
|
|
2023-08-29 17:11:36 -05:00
|
|
|
#define SIM_PLAY 0
|
|
|
|
#define SIM_PAUSE 1
|
|
|
|
#define SIM_STEP 2
|
|
|
|
|
|
|
|
static int sim_play = SIM_PLAY;
|
2023-09-19 12:35:12 -05:00
|
|
|
|
2023-08-31 03:10:30 -05:00
|
|
|
static int argc;
|
|
|
|
static char **args;
|
2022-08-14 14:19:36 -05:00
|
|
|
|
2024-05-21 18:50:53 -05:00
|
|
|
static JSValue c_start;
|
|
|
|
static JSValue c_process_fn;
|
2024-04-17 06:32:25 -05:00
|
|
|
|
|
|
|
static int PLAYSTART = 0;
|
2024-03-15 10:51:04 -05:00
|
|
|
|
2024-02-23 16:05:30 -06:00
|
|
|
void c_init() {
|
2024-03-13 16:30:55 -05:00
|
|
|
mainwin.start = 1;
|
2024-03-15 11:21:36 -05:00
|
|
|
window_resize(sapp_width(), sapp_height());
|
2024-03-21 11:33:36 -05:00
|
|
|
phys2d_init();
|
2023-09-04 09:48:44 -05:00
|
|
|
render_init();
|
2024-04-17 06:32:25 -05:00
|
|
|
if (!JS_IsUndefined(c_start)) {
|
|
|
|
script_call_sym(c_start,0,NULL);
|
|
|
|
JS_FreeValue(js, c_start);
|
|
|
|
}
|
2023-08-22 22:44:09 -05:00
|
|
|
}
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2024-04-02 07:41:46 -05:00
|
|
|
void c_frame() {
|
2024-04-17 06:32:25 -05:00
|
|
|
sfetch_dowork();
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
if (PLAYSTART)
|
|
|
|
script_call_sym(c_process_fn,0,NULL);
|
|
|
|
else if (LOADED_GAME) {
|
|
|
|
PLAYSTART = 1;
|
|
|
|
printf("LOADED GAME\n");
|
|
|
|
script_evalf("cmd_args('play');");
|
|
|
|
script_call_sym(c_start,0,NULL);
|
|
|
|
JS_FreeValue(js, c_start);
|
|
|
|
window_resize(sapp_width(), sapp_height());
|
|
|
|
}
|
|
|
|
#else
|
2024-04-02 07:41:46 -05:00
|
|
|
script_call_sym(c_process_fn,0,NULL);
|
2024-04-17 06:32:25 -05:00
|
|
|
#endif
|
2024-04-02 07:41:46 -05:00
|
|
|
}
|
2023-09-19 01:10:00 -05:00
|
|
|
|
2024-03-11 15:11:39 -05:00
|
|
|
void cleanup()
|
|
|
|
{
|
2024-02-23 16:05:30 -06:00
|
|
|
out_memusage(".prosperon/jsmem.txt");
|
2023-10-03 17:16:38 -05:00
|
|
|
script_stop();
|
2024-03-11 15:11:39 -05:00
|
|
|
}
|
|
|
|
|
2024-03-14 09:33:15 -05:00
|
|
|
void seghandle()
|
|
|
|
{
|
|
|
|
js_stacktrace();
|
|
|
|
cleanup();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2024-03-11 15:11:39 -05:00
|
|
|
void c_clean() {
|
2024-04-16 07:48:34 -05:00
|
|
|
JS_FreeValue(js, c_process_fn);
|
2024-03-11 15:11:39 -05:00
|
|
|
cleanup();
|
2023-10-03 17:16:38 -05:00
|
|
|
saudio_shutdown();
|
|
|
|
sg_shutdown();
|
2023-09-12 17:19:46 -05:00
|
|
|
};
|
2022-12-22 03:50:40 -06:00
|
|
|
|
2023-08-22 22:44:09 -05:00
|
|
|
void c_event(const sapp_event *e)
|
|
|
|
{
|
2024-09-03 14:08:46 -05:00
|
|
|
gui_input(e);
|
2024-04-03 17:17:32 -05:00
|
|
|
char lcfmt[5];
|
2023-08-22 22:44:09 -05:00
|
|
|
switch (e->type) {
|
|
|
|
case SAPP_EVENTTYPE_MOUSE_MOVE:
|
2024-09-03 14:08:46 -05:00
|
|
|
if (gui_wantmouse()) return;
|
2024-05-16 14:50:18 -05:00
|
|
|
script_evalf("prosperon.mousemove([%g, %g], [%g, %g]);", e->mouse_x, e->mouse_y, e->mouse_dx, -e->mouse_dy);
|
2023-08-22 22:44:09 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_MOUSE_SCROLL:
|
2024-09-03 14:08:46 -05:00
|
|
|
if (gui_wantmouse()) return;
|
2024-03-11 22:23:02 -05:00
|
|
|
script_evalf("prosperon.mousescroll([%g, %g]);", e->scroll_x, e->scroll_y);
|
2023-08-22 22:44:09 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_KEY_DOWN:
|
2024-09-03 14:08:46 -05:00
|
|
|
if (gui_wantkeys()) return;
|
2024-03-11 22:23:02 -05:00
|
|
|
script_evalf("prosperon.keydown(%d, %d);", e->key_code, e->key_repeat);
|
2023-08-22 22:44:09 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_KEY_UP:
|
2024-09-03 14:08:46 -05:00
|
|
|
if (gui_wantkeys()) return;
|
2024-03-11 22:23:02 -05:00
|
|
|
script_evalf("prosperon.keyup(%d);", e->key_code);
|
2023-08-31 02:05:06 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_MOUSE_UP:
|
2024-09-03 14:08:46 -05:00
|
|
|
if (gui_wantmouse()) return;
|
2024-03-11 22:23:02 -05:00
|
|
|
script_evalf("prosperon.mouseup(%d);", e->mouse_button);
|
2023-08-31 02:05:06 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_MOUSE_DOWN:
|
2024-09-03 14:08:46 -05:00
|
|
|
if (gui_wantmouse()) return;
|
2024-03-11 22:23:02 -05:00
|
|
|
script_evalf("prosperon.mousedown(%d);", e->mouse_button);
|
2023-08-22 22:44:09 -05:00
|
|
|
break;
|
2022-12-29 04:26:21 -06:00
|
|
|
|
2023-08-22 22:44:09 -05:00
|
|
|
case SAPP_EVENTTYPE_CHAR:
|
2024-09-03 14:08:46 -05:00
|
|
|
if (gui_wantkeys()) return;
|
2024-04-05 23:03:10 -05:00
|
|
|
if (iswcntrl(e->char_code)) break;
|
2024-04-03 17:17:32 -05:00
|
|
|
snprintf(lcfmt, 5, "%lc", e->char_code);
|
|
|
|
script_evalf("prosperon.textinput(`%s`);", lcfmt);
|
2023-08-22 22:44:09 -05:00
|
|
|
break;
|
2023-08-31 02:05:06 -05:00
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_RESIZED:
|
|
|
|
window_resize(e->window_width, e->window_height);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_ICONIFIED:
|
|
|
|
window_iconified(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_RESTORED:
|
|
|
|
window_iconified(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_FOCUSED:
|
|
|
|
window_focused(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_UNFOCUSED:
|
|
|
|
window_focused(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_SUSPENDED:
|
|
|
|
window_suspended(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_QUIT_REQUESTED:
|
2024-03-11 15:11:39 -05:00
|
|
|
quit();
|
2023-08-31 02:05:06 -05:00
|
|
|
break;
|
2023-12-20 09:19:04 -06:00
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_FILES_DROPPED:
|
|
|
|
input_dropped_files(sapp_get_num_dropped_files());
|
|
|
|
break;
|
2024-03-12 09:01:52 -05:00
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_MOUSE_ENTER:
|
2024-09-03 14:08:46 -05:00
|
|
|
if (gui_wantmouse()) return;
|
2024-03-12 09:01:52 -05:00
|
|
|
script_evalf("prosperon.mouseenter();");
|
|
|
|
break;
|
|
|
|
case SAPP_EVENTTYPE_MOUSE_LEAVE:
|
2024-09-03 14:08:46 -05:00
|
|
|
if (gui_wantmouse()) return;
|
2024-03-12 09:01:52 -05:00
|
|
|
script_evalf("prosperon.mouseleave();");
|
|
|
|
break;
|
|
|
|
case SAPP_EVENTTYPE_TOUCHES_BEGAN:
|
|
|
|
touch_start(e->touches, e->num_touches);
|
|
|
|
break;
|
|
|
|
case SAPP_EVENTTYPE_TOUCHES_MOVED:
|
|
|
|
touch_move(e->touches, e->num_touches);
|
|
|
|
break;
|
|
|
|
case SAPP_EVENTTYPE_TOUCHES_ENDED:
|
|
|
|
touch_end(e->touches, e->num_touches);
|
|
|
|
break;
|
|
|
|
case SAPP_EVENTTYPE_TOUCHES_CANCELLED:
|
|
|
|
touch_cancelled(e->touches, e->num_touches);
|
|
|
|
break;
|
|
|
|
case SAPP_EVENTTYPE_CLIPBOARD_PASTED:
|
|
|
|
input_clipboard_paste(sapp_get_clipboard_string());
|
|
|
|
break;
|
2023-12-21 17:21:01 -06:00
|
|
|
default:
|
|
|
|
break;
|
2023-05-12 13:22:05 -05:00
|
|
|
}
|
2022-12-29 04:26:21 -06:00
|
|
|
}
|
|
|
|
|
2023-09-06 12:17:16 -05:00
|
|
|
static sapp_desc start_desc = {
|
|
|
|
.width = 720,
|
|
|
|
.height = 1080,
|
|
|
|
.high_dpi = 0,
|
|
|
|
.sample_count = 1,
|
2024-09-29 06:10:42 -05:00
|
|
|
.swap_interval = 0,
|
2023-09-06 12:17:16 -05:00
|
|
|
.fullscreen = 1,
|
2024-03-13 16:30:55 -05:00
|
|
|
.window_title = NULL,
|
2023-09-06 12:17:16 -05:00
|
|
|
.enable_clipboard = false,
|
|
|
|
.clipboard_size = 0,
|
|
|
|
.enable_dragndrop = true,
|
|
|
|
.max_dropped_files = 1,
|
|
|
|
.max_dropped_file_path_length = 2048,
|
|
|
|
.init_cb = c_init,
|
|
|
|
.frame_cb = c_frame,
|
|
|
|
.cleanup_cb = c_clean,
|
|
|
|
.event_cb = c_event,
|
2024-04-16 22:27:29 -05:00
|
|
|
.win32_console_create = false,
|
2024-04-16 07:51:22 -05:00
|
|
|
.logger.func = sg_logging
|
2023-09-06 12:17:16 -05:00
|
|
|
};
|
|
|
|
|
2024-04-16 22:27:29 -05:00
|
|
|
sapp_desc sokol_main(int argc, char **argv) {
|
2023-09-04 09:48:44 -05:00
|
|
|
#ifndef NDEBUG
|
2023-09-13 07:31:00 -05:00
|
|
|
log_init();
|
2023-12-22 07:14:44 -06:00
|
|
|
signal(SIGSEGV, seghandle);
|
|
|
|
signal(SIGABRT, seghandle);
|
|
|
|
signal(SIGFPE, seghandle);
|
2023-11-20 07:49:14 -06:00
|
|
|
#endif
|
2024-03-21 11:33:36 -05:00
|
|
|
|
2023-10-23 08:08:11 -05:00
|
|
|
resources_init();
|
2024-03-14 14:10:06 -05:00
|
|
|
stm_setup(); /* time */
|
2024-02-23 16:05:30 -06:00
|
|
|
script_startup();
|
|
|
|
|
2024-04-17 06:32:25 -05:00
|
|
|
#ifndef __EMSCRIPTEN__
|
2023-09-04 09:48:44 -05:00
|
|
|
int argsize = 0;
|
2023-09-06 17:48:08 -05:00
|
|
|
for (int i = 0; i < argc; i++) {
|
2023-09-05 09:38:52 -05:00
|
|
|
argsize += strlen(argv[i]);
|
2023-09-04 09:48:44 -05:00
|
|
|
if (argc > i+1) argsize++;
|
|
|
|
}
|
|
|
|
|
2023-12-18 17:12:05 -06:00
|
|
|
char cmdstr[argsize+1];
|
2023-09-04 09:48:44 -05:00
|
|
|
cmdstr[0] = '\0';
|
2023-08-22 22:44:09 -05:00
|
|
|
|
2024-04-17 06:32:25 -05:00
|
|
|
for (int i = 0; i < argc; i++) {
|
2023-09-05 09:38:52 -05:00
|
|
|
strcat(cmdstr, argv[i]);
|
2023-09-04 09:48:44 -05:00
|
|
|
if (argc > i+1) strcat(cmdstr, " ");
|
|
|
|
}
|
2024-04-17 06:32:25 -05:00
|
|
|
|
2024-03-04 11:15:55 -06:00
|
|
|
script_evalf("cmd_args('%s');", cmdstr);
|
2024-04-17 06:32:25 -05:00
|
|
|
#endif
|
2024-04-16 07:51:22 -05:00
|
|
|
|
2024-04-16 22:27:29 -05:00
|
|
|
return start_desc;
|
2024-02-23 16:05:30 -06:00
|
|
|
}
|
|
|
|
|
2024-05-16 14:50:18 -05:00
|
|
|
void engine_start(JSValue start, JSValue procfn, float x, float y)
|
2024-02-23 16:05:30 -06:00
|
|
|
{
|
2024-03-28 17:40:14 -05:00
|
|
|
c_start = JS_DupValue(js,start);
|
|
|
|
c_process_fn = JS_DupValue(js,procfn);
|
2024-03-14 14:10:06 -05:00
|
|
|
|
2024-02-23 16:05:30 -06:00
|
|
|
sound_init();
|
2024-04-16 22:27:29 -05:00
|
|
|
|
2024-05-16 14:50:18 -05:00
|
|
|
start_desc.width = x;
|
|
|
|
start_desc.height = y;
|
2024-03-13 16:30:55 -05:00
|
|
|
start_desc.window_title = mainwin.title;
|
|
|
|
start_desc.fullscreen = mainwin.fullscreen;
|
2024-04-10 16:21:46 -05:00
|
|
|
start_desc.swap_interval = mainwin.vsync;
|
|
|
|
start_desc.enable_dragndrop = mainwin.enable_dragndrop;
|
|
|
|
start_desc.enable_clipboard = mainwin.enable_clipboard;
|
|
|
|
start_desc.high_dpi = mainwin.high_dpi;
|
|
|
|
start_desc.sample_count = mainwin.sample_count;
|
2023-08-22 22:44:09 -05:00
|
|
|
}
|
2023-10-30 17:41:32 -05:00
|
|
|
|
2024-03-15 11:21:36 -05:00
|
|
|
double apptime() { return stm_sec(stm_now()); }
|
2024-03-11 15:11:39 -05:00
|
|
|
|
|
|
|
void quit() {
|
2024-03-13 16:30:55 -05:00
|
|
|
if (mainwin.start)
|
2024-03-11 15:11:39 -05:00
|
|
|
sapp_quit();
|
|
|
|
else {
|
|
|
|
cleanup();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|