prosperon/source/engine/yugine.c

340 lines
7.1 KiB
C
Raw Normal View History

2023-01-04 18:09:42 -06:00
#include "yugine.h"
2023-05-12 13:22:05 -05:00
#include "font.h"
2023-12-04 13:38:37 -06:00
#include "transform.h"
2023-05-12 13:22:05 -05:00
#include "gameobject.h"
2022-02-04 11:36:24 -06:00
#include "input.h"
#include "render.h"
2023-05-12 13:22:05 -05:00
#include "window.h"
#include "sound.h"
#include "resources.h"
2023-12-04 13:38:37 -06:00
#include "spline.h"
#include <stdio.h>
2023-12-29 19:08:53 -06:00
#include "particle.h"
2024-01-02 07:55:22 -06:00
#include "simplex.h"
2024-01-16 07:29:27 -06:00
#include "nota.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
#include "timer.h"
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
#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include "2dphysics.h"
#include <signal.h>
#include <time.h>
2022-02-04 11:36:24 -06:00
2023-11-20 07:49:14 -06:00
#ifdef STEAM
#include "steamffi.h"
2023-11-20 07:49:14 -06:00
#endif
#ifdef DISCORD
#include "discord.h"
#endif
2022-08-14 14:19:36 -05:00
#include "string.h"
#include "render.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"
#include "sokol/sokol_time.h"
#include "sokol/sokol_args.h"
#include <stb_ds.h>
#include <stb_truetype.h>
#include "stb_image.h"
#include "stb_image_write.h"
#include <pl_mpeg.h>
#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;
2022-08-12 14:03:56 -05:00
double physlag = 0;
double physMS = 1 / 60.f;
uint64_t physlast = 0;
double updateMS = 1/60.f;
uint64_t updatelast = 0;
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
uint64_t start_t;
uint64_t frame_t;
2022-12-22 16:58:06 -06:00
static float timescale = 1.f;
2022-12-27 17:54:39 -06:00
#define SIM_PLAY 0
#define SIM_PAUSE 1
#define SIM_STEP 2
static int sim_play = SIM_PLAY;
int editor_mode = 0;
2023-12-20 17:20:29 -06:00
#define ENGINEINFO "Yugine version " VER ", " INFO " build.\nCopyright 2022-2024."
const char *engine_info() { return ENGINEINFO; }
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-12-22 07:14:44 -06:00
void seghandle()
{
js_stacktrace();
exit(1);
}
2023-11-27 14:29:55 -06:00
2023-08-22 22:44:09 -05:00
void c_init() {
2023-11-27 14:29:55 -06:00
input_init();
script_evalf("world_start();");
render_init();
window_set_icon("icons/moon.gif");
window_resize(sapp_width(), sapp_height());
script_evalf("Game.init();");
2023-12-29 19:08:53 -06:00
2024-01-01 17:30:42 -06:00
particle_init();
2023-08-22 22:44:09 -05:00
}
2023-05-12 13:22:05 -05:00
2023-12-12 08:46:27 -06:00
int frame_fps() { return 1.0/sapp_frame_duration(); }
2023-05-24 20:45:50 -05:00
static void process_frame()
2023-08-22 22:44:09 -05:00
{
double elapsed = stm_sec(stm_laptime(&frame_t));
2023-11-29 17:31:41 -06:00
script_evalf("Register.appupdate.broadcast(%g);", elapsed);
2023-11-27 17:04:04 -06:00
call_stack();
input_poll(0);
/* Timers all update every frame - once per monitor refresh */
timer_update(elapsed, timescale);
2023-12-29 19:08:53 -06:00
emitters_step(elapsed);
2023-11-30 10:47:59 -06:00
if (sim_play == SIM_PLAY || sim_play == SIM_STEP) {
if (stm_sec(stm_diff(frame_t, updatelast)) > updateMS) {
double dt = stm_sec(stm_diff(frame_t, updatelast));
updatelast = frame_t;
2023-12-24 09:14:46 -06:00
// prof_start(&prof_update);
call_updates(dt * timescale);
// prof_lap(&prof_update);
if (sim_play == SIM_STEP)
sim_pause();
}
2022-01-19 16:43:21 -06:00
physlag += elapsed;
while (physlag > physMS) {
physlag -= physMS;
// prof_start(&prof_physics);
phys_step = 1;
phys2d_update(physMS * timescale);
call_physics(physMS * timescale);
phys_step = 0;
// prof_lap(&prof_physics);
}
}
// prof_start(&prof_draw);
window_render(&mainwin);
// prof_lap(&prof_draw);
2023-08-22 22:44:09 -05:00
}
void c_frame()
{
if (editor_mode) return;
process_frame();
}
void c_clean() {
gif_rec_end("out.gif");
out_memusage("jsmem.txt");
script_stop();
saudio_shutdown();
sg_shutdown();
};
2023-08-22 22:44:09 -05:00
void c_event(const sapp_event *e)
{
switch (e->type) {
case SAPP_EVENTTYPE_MOUSE_MOVE:
input_mouse_move(e->mouse_x, e->mouse_y, e->mouse_dx, e->mouse_dy, e->modifiers);
2023-08-22 22:44:09 -05:00
break;
case SAPP_EVENTTYPE_MOUSE_SCROLL:
input_mouse_scroll(e->scroll_x, e->scroll_y, e->modifiers);
2023-08-22 22:44:09 -05:00
break;
case SAPP_EVENTTYPE_KEY_DOWN:
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:
input_btn(e->key_code, INPUT_UP, e->modifiers);
break;
case SAPP_EVENTTYPE_MOUSE_UP:
input_mouse(e->mouse_button, INPUT_UP, e->modifiers);
break;
case SAPP_EVENTTYPE_MOUSE_DOWN:
input_mouse(e->mouse_button, INPUT_DOWN, e->modifiers);
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;
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-12-20 09:19:04 -06:00
case SAPP_EVENTTYPE_FILES_DROPPED:
input_mouse_move(e->mouse_x, e->mouse_y, e->mouse_dx, e->mouse_dy, e->modifiers);
input_dropped_files(sapp_get_num_dropped_files());
break;
default:
break;
2023-05-12 13:22:05 -05:00
}
if (editor_mode)
process_frame();
2022-12-29 04:26:21 -06:00
}
int sim_playing() { return sim_play == SIM_PLAY; }
int sim_paused() { return sim_play == SIM_PAUSE; }
2023-12-11 08:36:45 -06:00
void sim_start() { sim_play = SIM_PLAY; }
void sim_pause() { sim_play = SIM_PAUSE; }
int phys_stepping() { return sim_play == SIM_STEP; }
2023-12-11 08:36:45 -06:00
void sim_step() { sim_play = SIM_STEP; }
void set_timescale(float val) { timescale = val; }
double get_timescale() { return timescale; }
2023-08-22 22:44:09 -05:00
static sapp_desc start_desc = {
.width = 720,
.height = 1080,
.high_dpi = 0,
.sample_count = 1,
.fullscreen = 1,
.window_title = "Primum Machinam",
.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,
};
void app_name(const char *name) { start_desc.window_title = strdup(name); }
int main(int argc, char **argv) {
#ifndef NDEBUG
log_init();
int logout = 0;
if (logout) {
time_t now = time(NULL);
char fname[100];
snprintf(fname, 100, "yugine-%ld.log", now);
log_setfile(fname);
}
2023-12-28 07:57:22 -06:00
2023-12-22 07:14:44 -06:00
signal(SIGSEGV, seghandle);
signal(SIGABRT, seghandle);
signal(SIGFPE, seghandle);
2024-01-03 08:38:17 -06:00
// signal(SIGBUS, seghandle);
2023-12-28 07:57:22 -06:00
#endif
2023-11-20 07:49:14 -06:00
#ifdef STEAM
steaminit();
#endif
#ifdef DISCORD
struct IDiscordCore *core;
DiscordCreate(DISCORD_VERSION, &(struct DiscordCreateParams){
.client_id = 1176355046590533714,
.flags = DiscordCreateFlags_Default
}, &core);
struct IDiscordUserManager *dum;
struct IDiscordActivityManager *dam;
dam = core->get_activity_manager(core);
struct DiscordActivity da;
sprintf(da.state, "Playing Solo Pinball");
sprintf(da.details, "COMPetitive");
dam->update_activity(dam, &da, NULL, NULL);
2023-11-20 07:49:14 -06:00
#endif
stm_setup(); /* time */
start_t = frame_t = stm_now();
physlast = updatelast = start_t;
2023-11-27 14:29:55 -06:00
sound_init();
resources_init();
phys2d_init();
2023-08-31 03:10:30 -05:00
script_startup();
2023-12-04 13:38:37 -06:00
int argsize = 0;
for (int i = 0; i < argc; i++) {
2023-09-05 09:38:52 -05:00
argsize += strlen(argv[i]);
if (argc > i+1) argsize++;
}
2023-12-18 17:12:05 -06:00
char cmdstr[argsize+1];
cmdstr[0] = '\0';
2023-08-22 22:44:09 -05:00
for (int i = 0; i < argc; i++) {
2023-09-05 09:38:52 -05:00
strcat(cmdstr, argv[i]);
if (argc > i+1) strcat(cmdstr, " ");
}
2023-09-05 09:38:52 -05:00
script_evalf("cmd_args('%s');", cmdstr);
start_desc.width = mainwin.width;
start_desc.height = mainwin.height;
start_desc.fullscreen = 0;
sapp_run(&start_desc);
return 0;
2023-08-22 22:44:09 -05:00
}
2023-12-12 08:46:27 -06:00
double apptime() { return stm_sec(stm_diff(stm_now(), start_t)); }