2023-01-04 18:09:42 -06:00
|
|
|
#include "yugine.h"
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
#include "render.h"
|
2022-02-04 11:36:24 -06:00
|
|
|
#include "camera.h"
|
|
|
|
#include "window.h"
|
2022-01-19 16:43:21 -06:00
|
|
|
#include "engine.h"
|
2022-02-04 11:36:24 -06:00
|
|
|
#include "input.h"
|
|
|
|
#include "openglrender.h"
|
2023-02-02 17:52:15 -06:00
|
|
|
#include "gameobject.h"
|
|
|
|
|
|
|
|
#include "timer.h"
|
|
|
|
|
2022-08-14 18:10:29 -05:00
|
|
|
#include "script.h"
|
2023-02-02 17:52:15 -06:00
|
|
|
#include "ffi.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>
|
|
|
|
|
2023-01-10 07:13:00 -06:00
|
|
|
#include "duktape.h"
|
2023-01-04 18:09:42 -06:00
|
|
|
|
2022-12-22 03:50:40 -06:00
|
|
|
#include "2dphysics.h"
|
|
|
|
|
2022-12-23 13:48:29 -06:00
|
|
|
#if ED
|
|
|
|
#include "editor.h"
|
|
|
|
#endif
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
#ifdef __linux__
|
2023-03-01 14:42:44 -06:00
|
|
|
//#include <execinfo.h>
|
2022-12-12 10:48:21 -06: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"
|
|
|
|
|
2022-02-04 11:36:24 -06:00
|
|
|
int physOn = 0;
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
double renderlag = 0;
|
2022-08-12 14:03:56 -05:00
|
|
|
double physlag = 0;
|
|
|
|
double updatelag = 0;
|
|
|
|
|
2022-12-20 08:16:26 -06:00
|
|
|
double renderMS = 1/165.f;
|
2022-08-12 14:03:56 -05:00
|
|
|
double physMS = 1/120.f;
|
|
|
|
double updateMS = 1/60.f;
|
2022-02-04 11:36:24 -06:00
|
|
|
|
2022-08-14 14:19:36 -05:00
|
|
|
static int ed = 1;
|
2022-12-22 03:50:40 -06:00
|
|
|
static int sim_play = 0;
|
|
|
|
static double lastTick;
|
2022-08-14 14:19:36 -05:00
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
static float timescale = 1.f;
|
|
|
|
|
2022-12-29 04:26:21 -06:00
|
|
|
#define FPSBUF 10
|
|
|
|
static double framems[FPSBUF];
|
|
|
|
int framei = 0;
|
|
|
|
int fps;
|
2022-12-27 17:54:39 -06:00
|
|
|
|
2023-02-28 17:03:28 -06:00
|
|
|
#define SIM_STOP 0
|
|
|
|
#define SIM_PLAY 1
|
|
|
|
#define SIM_PAUSE 2
|
|
|
|
#define SIM_STEP 3
|
|
|
|
|
2022-11-24 01:54:17 -06:00
|
|
|
void seghandle(int sig) {
|
2022-12-12 10:48:21 -06:00
|
|
|
#ifdef __linux__
|
2023-03-01 14:42:44 -06:00
|
|
|
/* void *ents[512];
|
2022-11-24 01:54:17 -06:00
|
|
|
size_t size;
|
|
|
|
|
|
|
|
size = backtrace(ents, 512);
|
|
|
|
if (strsignal(sig)) {
|
|
|
|
YughCritical("CRASH! Signal: %s.", strsignal(sig));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
YughCritical("CRASH! Signal: %d.", sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
YughCritical("====================BACKTRACE====================");
|
|
|
|
char **stackstr = backtrace_symbols(ents, size);
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
YughInfo("Stack size is %d.", size);
|
|
|
|
|
2022-11-24 01:54:17 -06:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
YughCritical(stackstr[i]);
|
|
|
|
}
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
duk_dump_stack(duk);
|
|
|
|
|
2023-03-01 14:42:44 -06: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
|
|
|
|
2022-08-07 01:43:45 -05:00
|
|
|
int main(int argc, char **args) {
|
2022-12-15 17:30:22 -06:00
|
|
|
int logout = 1;
|
|
|
|
|
2022-08-14 14:19:36 -05:00
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
if (args[i][0] == '-') {
|
2022-11-24 01:54:17 -06:00
|
|
|
switch(args[i][1]) {
|
|
|
|
case 'p':
|
|
|
|
if (strncmp(&args[i][2], "lay", 3))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ed = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
if (i+1 < argc && args[i+1][0] != '-') {
|
|
|
|
log_setfile(args[i+1]);
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
YughError("Expected a file for command line arg '-l'.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:58:36 -06:00
|
|
|
case 'v':
|
|
|
|
printf("Yugine version %s, %s build.\n", VER, INFO);
|
2023-02-23 17:03:58 -06:00
|
|
|
printf("Copyright 2022-2023 odplot productions LLC.\n");
|
2022-11-24 10:58:36 -06:00
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
|
2022-12-14 13:01:42 -06:00
|
|
|
case 'h':
|
|
|
|
printf("-l Set log file\n");
|
|
|
|
printf("-play Launch engine in play mode instead of editor mode\n");
|
|
|
|
printf("-v Display engine info\n");
|
2023-02-23 17:03:58 -06:00
|
|
|
printf("-c Redirect logging to console\n");
|
2022-12-14 13:01:42 -06:00
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
|
2022-12-15 17:30:22 -06:00
|
|
|
case 'c':
|
|
|
|
logout = 0;
|
|
|
|
break;
|
|
|
|
|
2022-08-14 14:19:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 09:40:53 -06:00
|
|
|
ed = 0;
|
|
|
|
|
2023-01-10 07:13:00 -06:00
|
|
|
|
2022-12-23 13:48:29 -06:00
|
|
|
#if DBG
|
|
|
|
if (logout) {
|
2022-11-24 01:54:17 -06:00
|
|
|
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);
|
|
|
|
}
|
2022-12-23 13:48:29 -06:00
|
|
|
signal(SIGSEGV, seghandle);
|
2023-02-23 17:03:58 -06:00
|
|
|
signal(SIGABRT, seghandle);
|
|
|
|
signal(SIGFPE, seghandle);
|
2022-12-23 13:48:29 -06:00
|
|
|
#endif
|
|
|
|
|
2022-11-24 10:58:36 -06:00
|
|
|
FILE *gameinfo = NULL;
|
|
|
|
gameinfo = fopen("game.info", "w");
|
|
|
|
fprintf(gameinfo, "Yugine v. %s, sys %s.", VER, INFO);
|
|
|
|
fclose(gameinfo);
|
|
|
|
|
2022-01-19 16:43:21 -06:00
|
|
|
engine_init();
|
|
|
|
|
2022-08-15 16:38:21 -05:00
|
|
|
const GLFWvidmode *vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
|
2022-11-18 12:03:07 -06:00
|
|
|
YughInfo("Refresh rate is %d", vidmode->refreshRate);
|
2022-08-15 16:38:21 -05:00
|
|
|
|
|
|
|
renderMS = 1.0/vidmode->refreshRate;
|
|
|
|
|
2022-08-07 01:43:45 -05:00
|
|
|
window_set_icon("icon.png");
|
|
|
|
|
2022-08-14 14:19:36 -05:00
|
|
|
if (ed) {
|
|
|
|
editor_init(MakeSDLWindow("Editor", 600, 600, 0));
|
|
|
|
} else {
|
2023-01-10 14:02:24 -06:00
|
|
|
script_dofile("game.js");
|
2022-08-14 14:19:36 -05:00
|
|
|
}
|
2022-01-19 16:43:21 -06:00
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
input_init();
|
2022-02-04 11:36:24 -06:00
|
|
|
openglInit();
|
2022-12-22 03:50:40 -06:00
|
|
|
sim_stop();
|
2022-12-16 11:54:05 -06:00
|
|
|
while (!want_quit()) {
|
2022-12-20 08:16:26 -06:00
|
|
|
double elapsed = glfwGetTime() - lastTick;
|
|
|
|
deltaT = elapsed;
|
2022-08-12 14:03:56 -05:00
|
|
|
lastTick = glfwGetTime();
|
2022-12-29 04:26:21 -06:00
|
|
|
double wait = fmax(0, renderMS-elapsed);
|
|
|
|
input_poll(wait);
|
|
|
|
window_all_handle_events();
|
2022-01-19 16:43:21 -06:00
|
|
|
|
2022-12-29 04:26:21 -06:00
|
|
|
framems[framei++] = elapsed;
|
2023-02-03 22:18:19 -06:00
|
|
|
|
|
|
|
if (framei == FPSBUF) framei = 0;
|
2022-12-20 08:16:26 -06:00
|
|
|
|
2023-02-28 17:03:28 -06:00
|
|
|
if (sim_play == SIM_PLAY || sim_play == SIM_STEP) {
|
2023-01-19 10:44:29 -06:00
|
|
|
timer_update(elapsed);
|
2022-12-22 03:50:40 -06:00
|
|
|
physlag += elapsed;
|
2022-12-22 16:58:06 -06:00
|
|
|
call_updates(elapsed * timescale);
|
|
|
|
|
|
|
|
while (physlag >= physMS) {
|
2022-12-22 03:50:40 -06:00
|
|
|
physlag -= physMS;
|
2022-12-22 16:58:06 -06:00
|
|
|
phys2d_update(physMS * timescale);
|
|
|
|
call_physics(physMS * timescale);
|
2023-02-28 17:03:28 -06:00
|
|
|
if (sim_play == SIM_STEP) sim_pause();
|
2022-12-22 03:50:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 22:18:19 -06:00
|
|
|
renderlag += elapsed;
|
|
|
|
|
2022-08-07 01:43:45 -05:00
|
|
|
if (renderlag >= renderMS) {
|
|
|
|
renderlag -= renderMS;
|
2022-12-20 08:16:26 -06:00
|
|
|
window_renderall();
|
2022-08-07 01:43:45 -05:00
|
|
|
}
|
2022-01-19 16:43:21 -06:00
|
|
|
|
2023-01-13 08:05:36 -06:00
|
|
|
gameobjects_cleanup();
|
|
|
|
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
2022-11-03 16:58:03 -05:00
|
|
|
|
|
|
|
return 0;
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
2022-12-22 03:50:40 -06:00
|
|
|
|
2022-12-29 04:26:21 -06:00
|
|
|
int frame_fps()
|
|
|
|
{
|
|
|
|
double fpsms = 0;
|
2023-02-03 22:18:19 -06:00
|
|
|
for (int i = 0; i < FPSBUF; i++) {
|
|
|
|
fpsms += framems[i];
|
|
|
|
}
|
2022-12-29 04:26:21 -06:00
|
|
|
|
2023-02-03 22:18:19 -06:00
|
|
|
return FPSBUF / fpsms;
|
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; }
|
|
|
|
int sim_stopped() { return sim_play == SIM_STOP; }
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void sim_stop() {
|
|
|
|
/* Revert starting state of everything from sim_start */
|
2023-02-28 17:03:28 -06:00
|
|
|
sim_play = SIM_STOP;
|
2022-12-22 03:50:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void sim_step() {
|
2023-02-06 17:21:00 -06:00
|
|
|
if (sim_paused()) {
|
2023-02-28 17:03:28 -06:00
|
|
|
YughInfo("Step");
|
|
|
|
sim_play = SIM_STEP;
|
2023-02-06 17:21:00 -06:00
|
|
|
}
|
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) {
|
|
|
|
timescale = val;
|
2023-02-02 17:52:15 -06:00
|
|
|
}
|