2023-01-04 18:09:42 -06:00
|
|
|
#include "yugine.h"
|
|
|
|
|
2022-02-04 11:36:24 -06:00
|
|
|
#include "camera.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "font.h"
|
|
|
|
#include "gameobject.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-02-02 17:52:15 -06:00
|
|
|
|
2023-08-30 18:22:32 -05:00
|
|
|
#include "datastream.h"
|
2023-03-24 14:01:01 -05:00
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
#include "timer.h"
|
|
|
|
|
2023-04-22 14:07:37 -05:00
|
|
|
#include "quickjs/quickjs.h"
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
#include "ffi.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"
|
|
|
|
|
2023-09-03 12:50:02 -05:00
|
|
|
#ifdef __GLIBC__
|
2023-05-12 13:22:05 -05:00
|
|
|
#include <execinfo.h>
|
2023-05-30 11:39:22 -05:00
|
|
|
#endif
|
|
|
|
|
2022-11-24 01:54:17 -06:00
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
2022-02-04 11:36:24 -06:00
|
|
|
|
2022-08-14 14:19:36 -05:00
|
|
|
#include "string.h"
|
|
|
|
|
2023-05-04 17:07:00 -05:00
|
|
|
#define SOKOL_TRACE_HOOKS
|
2023-08-31 02:05:06 -05:00
|
|
|
#define SOKOL_IMPL
|
2023-08-31 17:23:24 -05:00
|
|
|
|
2023-05-04 17:07:00 -05:00
|
|
|
#include "sokol/sokol_gfx.h"
|
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-05-04 17:07:00 -05:00
|
|
|
|
2023-08-31 13:00:33 -05:00
|
|
|
#define STB_DS_IMPLEMENTATION
|
|
|
|
#include <stb_ds.h>
|
|
|
|
|
|
|
|
#define STB_TRUETYPE_IMPLEMENTATION
|
|
|
|
#include <stb_truetype.h>
|
|
|
|
|
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
|
|
#define STBI_FAILURE_USERMSG
|
2023-09-02 06:53:52 -05:00
|
|
|
#define STBI_NO_SIMD
|
2023-08-31 13:00:33 -05:00
|
|
|
#include "stb_image.h"
|
|
|
|
|
|
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
|
|
#include "stb_image_write.h"
|
|
|
|
|
|
|
|
#define PL_MPEG_IMPLEMENTATION
|
|
|
|
#include <pl_mpeg.h>
|
|
|
|
|
2023-09-02 06:53:52 -05:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
static struct d_prof prof_draw;
|
|
|
|
static struct d_prof prof_update;
|
|
|
|
static struct d_prof prof_input;
|
|
|
|
static struct d_prof prof_physics;
|
2023-08-31 13:00:33 -05:00
|
|
|
|
2022-08-12 14:03:56 -05:00
|
|
|
double physlag = 0;
|
2023-09-06 17:48:08 -05:00
|
|
|
int render_dirty = 0;
|
2022-08-12 14:03:56 -05:00
|
|
|
|
2023-09-06 17:48:08 -05:00
|
|
|
double physMS = 1 / 60.f;
|
2022-02-04 11:36:24 -06:00
|
|
|
|
2023-03-10 13:13:48 -06:00
|
|
|
static int phys_step = 0;
|
2022-08-14 14:19:36 -05:00
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
double appTime = 0;
|
2022-12-22 16:58:06 -06:00
|
|
|
|
|
|
|
static float timescale = 1.f;
|
2022-12-27 17:54:39 -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-02-28 17:03:28 -06:00
|
|
|
|
2023-05-27 10:13:20 -05:00
|
|
|
#ifdef __TINYC__
|
|
|
|
int backtrace(void **buffer, int size) {
|
|
|
|
extern uint64_t *__libc_stack_end;
|
|
|
|
uint64_t **p, *bp, *frame;
|
|
|
|
asm ("mov %%rbp, %0;" : "=r" (bp));
|
|
|
|
p = (uint64_t**) bp;
|
|
|
|
int i = 0;
|
|
|
|
while (i < size) {
|
|
|
|
frame = p[0];
|
|
|
|
if (frame < bp || frame > __libc_stack_end) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
buffer[i++] = p[1];
|
|
|
|
p = (uint64_t**) frame;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void print_stacktrace() {
|
2023-09-03 12:50:02 -05:00
|
|
|
#ifdef __GLIBC__
|
2023-05-12 13:22:05 -05:00
|
|
|
void *ents[512];
|
2023-05-27 10:13:20 -05:00
|
|
|
size_t size = backtrace(ents, 512);
|
2022-11-24 01:54:17 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
YughCritical("====================BACKTRACE====================");
|
|
|
|
char **stackstr = backtrace_symbols(ents, size);
|
2022-11-24 01:54:17 -06:00
|
|
|
|
2023-05-27 10:13:20 -05:00
|
|
|
YughCritical("Stack size is %d.", size);
|
2023-02-02 17:52:15 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
YughCritical(stackstr[i]);
|
2022-11-24 01:54:17 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
js_stacktrace();
|
2023-05-30 11:39:22 -05:00
|
|
|
#endif
|
2023-04-24 17:22:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void seghandle(int sig) {
|
2023-09-03 12:50:02 -05:00
|
|
|
#ifdef __GLIBC__
|
2023-05-12 13:22:05 -05:00
|
|
|
if (strsignal(sig))
|
|
|
|
YughCritical("CRASH! Signal: %s.", strsignal(sig));
|
2023-04-24 17:22:18 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
print_stacktrace();
|
2023-02-02 17:52:15 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
exit(1);
|
2022-12-12 10:48:21 -06:00
|
|
|
#endif
|
2022-11-24 01:54:17 -06:00
|
|
|
}
|
2022-02-04 11:36:24 -06:00
|
|
|
|
2023-06-28 11:35:41 -05:00
|
|
|
const char *engine_info()
|
|
|
|
{
|
2023-09-04 01:20:55 -05:00
|
|
|
static char str[100];
|
2023-06-28 11:35:41 -05:00
|
|
|
snprintf(str, 100, "Yugine version %s, %s build.\nCopyright 2022-2023 odplot productions LLC.\n", VER, INFO);
|
|
|
|
return str;
|
|
|
|
}
|
2023-04-22 14:07:37 -05:00
|
|
|
|
2023-08-31 03:10:30 -05:00
|
|
|
static int argc;
|
|
|
|
static char **args;
|
2022-08-14 14:19:36 -05:00
|
|
|
|
2023-08-22 22:44:09 -05:00
|
|
|
void c_init() {
|
2023-09-04 09:48:44 -05:00
|
|
|
render_init();
|
2023-09-05 17:09:25 -05:00
|
|
|
|
|
|
|
script_evalf("initialize();");
|
2023-08-22 22:44:09 -05:00
|
|
|
}
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2023-08-22 22:44:09 -05:00
|
|
|
int frame_fps() {
|
|
|
|
return 1.0/sapp_frame_duration();
|
|
|
|
}
|
2023-05-24 20:45:50 -05:00
|
|
|
|
2023-08-22 22:44:09 -05:00
|
|
|
void c_frame()
|
|
|
|
{
|
2023-08-31 02:05:06 -05:00
|
|
|
double elapsed = sapp_frame_duration();
|
|
|
|
appTime += elapsed;
|
|
|
|
|
2023-09-06 17:48:08 -05:00
|
|
|
input_poll(0);
|
2023-08-23 17:18:34 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
if (sim_play == SIM_PLAY || sim_play == SIM_STEP) {
|
2023-09-02 06:53:52 -05:00
|
|
|
prof_start(&prof_update);
|
2023-05-12 13:22:05 -05:00
|
|
|
timer_update(elapsed * timescale);
|
|
|
|
call_updates(elapsed * timescale);
|
2023-09-02 06:53:52 -05:00
|
|
|
prof(&prof_update);
|
|
|
|
|
|
|
|
physlag += elapsed;
|
|
|
|
while (physlag >= physMS) {
|
|
|
|
prof_start(&prof_physics);
|
2023-05-12 13:22:05 -05:00
|
|
|
phys_step = 1;
|
|
|
|
physlag -= physMS;
|
|
|
|
phys2d_update(physMS * timescale);
|
2023-05-27 07:01:17 -05:00
|
|
|
call_physics(physMS * timescale);
|
2023-05-12 13:22:05 -05:00
|
|
|
if (sim_play == SIM_STEP) sim_pause();
|
|
|
|
phys_step = 0;
|
2023-09-02 06:53:52 -05:00
|
|
|
prof(&prof_physics);
|
|
|
|
}
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2023-09-06 17:48:08 -05:00
|
|
|
if (sim_play == SIM_STEP) {
|
|
|
|
sim_pause();
|
|
|
|
render_dirty = 1;
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 16:43:21 -06:00
|
|
|
|
2023-09-06 17:48:08 -05:00
|
|
|
if (sim_play == SIM_PLAY || render_dirty) {
|
2023-09-02 06:53:52 -05:00
|
|
|
prof_start(&prof_draw);
|
2023-08-31 02:05:06 -05:00
|
|
|
window_render(&mainwin);
|
2023-09-02 06:53:52 -05:00
|
|
|
prof(&prof_draw);
|
2023-09-06 17:48:08 -05:00
|
|
|
render_dirty = 0;
|
|
|
|
}
|
2023-09-02 06:53:52 -05:00
|
|
|
|
2023-09-06 17:48:08 -05:00
|
|
|
gameobjects_cleanup();
|
2023-08-22 22:44:09 -05:00
|
|
|
}
|
|
|
|
|
2023-09-06 17:48:08 -05:00
|
|
|
void c_clean() {};
|
2023-05-12 13:22:05 -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)
|
|
|
|
{
|
2023-09-06 17:48:08 -05:00
|
|
|
render_dirty = 1;
|
2023-08-22 22:44:09 -05:00
|
|
|
switch (e->type) {
|
|
|
|
case SAPP_EVENTTYPE_MOUSE_MOVE:
|
2023-08-31 02:05:06 -05:00
|
|
|
input_mouse_move(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:
|
2023-08-31 02:05:06 -05:00
|
|
|
input_mouse_scroll(e->scroll_x, e->scroll_y);
|
2023-08-22 22:44:09 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_KEY_DOWN:
|
2023-08-31 02:05:06 -05:00
|
|
|
input_btn(e->key_code, e->key_repeat ? INPUT_REPEAT : INPUT_DOWN, e->modifiers);
|
2023-08-22 22:44:09 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_KEY_UP:
|
2023-08-31 02:05:06 -05:00
|
|
|
input_btn(e->key_code, INPUT_UP, e->modifiers);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_MOUSE_UP:
|
|
|
|
input_mouse(e->mouse_button, INPUT_UP);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_EVENTTYPE_MOUSE_DOWN:
|
|
|
|
input_mouse(e->mouse_button, INPUT_DOWN);
|
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:
|
2023-08-31 03:10:30 -05:00
|
|
|
input_key(e->char_code, e->modifiers);
|
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:
|
|
|
|
window_quit();
|
|
|
|
break;
|
2023-05-12 13:22:05 -05:00
|
|
|
}
|
2022-12-29 04:26:21 -06:00
|
|
|
}
|
|
|
|
|
2023-02-28 17:03:28 -06:00
|
|
|
int sim_playing() { return sim_play == SIM_PLAY; }
|
|
|
|
int sim_paused() { return sim_play == SIM_PAUSE; }
|
2022-12-22 16:58:06 -06:00
|
|
|
|
2022-12-22 03:50:40 -06:00
|
|
|
void sim_start() {
|
2023-02-28 17:03:28 -06:00
|
|
|
sim_play = SIM_PLAY;
|
2022-12-22 03:50:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void sim_pause() {
|
2023-02-28 17:03:28 -06:00
|
|
|
sim_play = SIM_PAUSE;
|
2022-12-22 03:50:40 -06:00
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:19 -05:00
|
|
|
int phys_stepping() { return sim_play == SIM_STEP; }
|
2023-03-10 13:13:48 -06:00
|
|
|
|
2022-12-22 03:50:40 -06:00
|
|
|
void sim_step() {
|
2023-09-06 17:48:08 -05:00
|
|
|
sim_play = SIM_STEP;
|
2022-12-22 16:58:06 -06:00
|
|
|
}
|
2022-12-22 03:50:40 -06:00
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
void set_timescale(float val) {
|
2023-05-12 13:22:05 -05:00
|
|
|
timescale = val;
|
2023-02-02 17:52:15 -06:00
|
|
|
}
|
2023-08-28 17:00:53 -05:00
|
|
|
|
|
|
|
double get_timescale()
|
|
|
|
{
|
|
|
|
return timescale;
|
|
|
|
}
|
2023-08-22 22:44:09 -05:00
|
|
|
|
2023-09-06 12:17:16 -05:00
|
|
|
static sapp_desc start_desc = {
|
|
|
|
.width = 720,
|
|
|
|
.height = 1080,
|
|
|
|
.high_dpi = 0,
|
|
|
|
.sample_count = 1,
|
|
|
|
.fullscreen = 1,
|
|
|
|
.window_title = "Yugine",
|
|
|
|
.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,
|
|
|
|
.logger.func = sg_logging,
|
|
|
|
};
|
|
|
|
|
2023-09-05 09:38:52 -05:00
|
|
|
sapp_desc sokol_main(int argc, char **argv) {
|
2023-09-04 09:48:44 -05:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#ifdef __linux__
|
|
|
|
int logout = 0;
|
|
|
|
if (logout) {
|
|
|
|
time_t now = time(NULL);
|
|
|
|
char fname[100];
|
|
|
|
snprintf(fname, 100, "yugine-%d.log", now);
|
|
|
|
log_setfile(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
YughInfo("Starting yugine version %s.", VER);
|
|
|
|
|
|
|
|
FILE *sysinfo = NULL;
|
|
|
|
/* sysinfo = popen("uname -a", "r");
|
|
|
|
if (!sysinfo) {
|
|
|
|
YughWarn("Failed to get sys info.");
|
|
|
|
} else {
|
|
|
|
log_cat(sysinfo);
|
|
|
|
pclose(sysinfo);
|
|
|
|
}*/
|
|
|
|
signal(SIGSEGV, seghandle);
|
|
|
|
signal(SIGABRT, seghandle);
|
|
|
|
signal(SIGFPE, seghandle);
|
|
|
|
signal(SIGBUS, seghandle);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
stm_setup(); /* time */
|
|
|
|
resources_init();
|
|
|
|
|
|
|
|
phys2d_init();
|
2023-09-02 06:53:52 -05:00
|
|
|
|
2023-08-31 03:10:30 -05:00
|
|
|
script_startup();
|
2023-09-06 12:17:16 -05:00
|
|
|
|
|
|
|
script_dofile("scripts/engine.js");
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
char cmdstr[argsize];
|
|
|
|
cmdstr[0] = '\0';
|
2023-08-22 22:44:09 -05:00
|
|
|
|
2023-09-04 09:48:44 -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, " ");
|
|
|
|
}
|
2023-09-05 09:38:52 -05:00
|
|
|
|
2023-09-04 09:48:44 -05:00
|
|
|
script_evalf("cmd_args('%s');", cmdstr);
|
|
|
|
|
|
|
|
sound_init();
|
|
|
|
input_init();
|
2023-09-04 01:20:55 -05:00
|
|
|
|
2023-09-06 12:17:16 -05:00
|
|
|
start_desc.width = mainwin.width;
|
|
|
|
start_desc.height = mainwin.height;
|
|
|
|
start_desc.fullscreen = 0;
|
|
|
|
|
|
|
|
return start_desc;
|
2023-08-22 22:44:09 -05:00
|
|
|
}
|