Remove vec.h; add editor start, step, pause controls

This commit is contained in:
John Alanbrook 2023-08-28 02:57:19 +00:00
parent 2a79182cac
commit aad89926d9
9 changed files with 147 additions and 110 deletions

View file

@ -78,13 +78,13 @@ COMPILER_FLAGS = $(includeflag) $(QFLAGS) -MD $(WARNING_FLAGS) -I. -DCP_USE_DOUB
LIBPATH = -L$(BIN) LIBPATH = -L$(BIN)
ifeq ($(OS), WIN32) ifeq ($(OS), WIN32)
LINKER_FLAGS = $(QFLAGS) -static LINKER_FLAGS = $(QFLAGS) -static
ELIBS = engine ucrt yughc glfw3 opengl32 gdi32 ws2_32 ole32 winmm setupapi m ELIBS = engine glfw3 opengl32 quickjs gdi32 ws2_32 ole32 winmm setupapi m
CLIBS = CLIBS =
EXT = .exe EXT = .exe
else else
LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -pthread -rdynamic LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -pthread -rdynamic
ELIBS = engine pthread yughc quickjs glfw3 GL c m dl ELIBS = engine pthread quickjs glfw3 GL c m dl
CLIBS = CLIBS =
endif endif

View file

@ -2,7 +2,6 @@
#include "config.h" #include "config.h"
#include "log.h" #include "log.h"
#include "vec.h"
#include <dirent.h> #include <dirent.h>
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h> #include <stddef.h>
@ -20,7 +19,7 @@
char *DATA_PATH = NULL; char *DATA_PATH = NULL;
char *PREF_PATH = NULL; char *PREF_PATH = NULL;
struct vec *prefabs = NULL; char *prefabs;
const char *EXT_PREFAB = ".prefab"; const char *EXT_PREFAB = ".prefab";
const char *EXT_LEVEL = ".level"; const char *EXT_LEVEL = ".level";
@ -29,13 +28,10 @@ int stemlen = 0;
static const char *cur_ext = NULL; static const char *cur_ext = NULL;
struct dirent *c_dirent = NULL; struct dirent *c_dirent = NULL;
struct vec *c_vec = NULL;
char pathbuf[MAXPATH + 1]; char pathbuf[MAXPATH + 1];
void resources_init() { void resources_init() {
prefabs = vec_make(MAXNAME, 25);
DATA_PATH = malloc(MAXPATH); DATA_PATH = malloc(MAXPATH);
getcwd(DATA_PATH, MAXPATH); getcwd(DATA_PATH, MAXPATH);
strncat(DATA_PATH, "/", MAXPATH); strncat(DATA_PATH, "/", MAXPATH);
@ -80,17 +76,19 @@ FILE *res_open(char *path, const char *tag) {
static int ext_check(const char *path, const struct stat *sb, int typeflag) { static int ext_check(const char *path, const struct stat *sb, int typeflag) {
if (typeflag == FTW_F) { if (typeflag == FTW_F) {
const char *ext = strrchr(path, '.'); const char *ext = strrchr(path, '.');
if (ext != NULL && !strcmp(ext, cur_ext)) if (ext != NULL && !strcmp(ext, cur_ext)) {
vec_add(c_vec, path); char newstr[255];
strncpy(newstr, path, 255);
arrput(prefabs, newstr);
}
} }
return 0; return 0;
} }
void fill_extensions(struct vec *vec, const char *path, const char *ext) { void fill_extensions(char *paths, const char *path, const char *ext) {
c_vec = vec;
cur_ext = ext; cur_ext = ext;
vec_clear(c_vec); arrfree(paths);
ftw(".", ext_check, 10); ftw(".", ext_check, 10);
} }

View file

@ -3,8 +3,6 @@
#include <stdio.h> #include <stdio.h>
struct vec;
extern char *DATA_PATH; extern char *DATA_PATH;
extern char *PREF_PATH; extern char *PREF_PATH;
@ -15,9 +13,9 @@ extern int stemlen;
void resources_init(); void resources_init();
extern struct vec *prefabs; extern char *prefabs;
void findPrefabs(); void findPrefabs();
void fill_extensions(struct vec *vec, const char *path, const char *ext); void fill_extensions(char *paths, const char *path, const char *ext);
char *get_filename_from_path(char *path, int extension); char *get_filename_from_path(char *path, int extension);
char *get_directory_from_path(char *path); char *get_directory_from_path(char *path);
char *str_replace_ext(const char *s, const char *newext); char *str_replace_ext(const char *s, const char *newext);

View file

@ -1,5 +1,7 @@
#include "script.h" #include "script.h"
#include "stdarg.h"
#include "log.h" #include "log.h"
#include "stdio.h" #include "stdio.h"

View file

@ -3,7 +3,6 @@
#include "log.h" #include "log.h"
#include "render.h" #include "render.h"
#include "sokol/sokol_gfx.h" #include "sokol/sokol_gfx.h"
#include "util.h"
#include <math.h> #include <math.h>
#include <stb_ds.h> #include <stb_ds.h>
#include <stb_image.h> #include <stb_image.h>

View file

@ -35,7 +35,7 @@
#define SOKOL_TRACE_HOOKS #define SOKOL_TRACE_HOOKS
#define SOKOL_GFX_IMPL #define SOKOL_GFX_IMPL
#define SOKOL_GLES3 #define SOKOL_GLCORE33
#include "sokol/sokol_gfx.h" #include "sokol/sokol_gfx.h"
int physOn = 0; int physOn = 0;
@ -297,7 +297,7 @@ void sim_stop() {
sim_play = SIM_STOP; sim_play = SIM_STOP;
} }
int phys_stepping() { return phys_step; } int phys_stepping() { return sim_play == SIM_STEP; }
void sim_step() { void sim_step() {
if (sim_paused()) { if (sim_paused()) {

View file

@ -100,10 +100,11 @@ var Debug = {
}); });
gui_text(Game.playing() ? "PLAYING" gui_text(Game.playing() ? "PLAYING"
: Game.paused() ? : Game.stepping() ?
"PAUSED" : "STEP" :
Game.paused() ?
"PAUSED" :
"STOPPED", [0, 0], 1); "STOPPED", [0, 0], 1);
}, },
}; };
@ -235,24 +236,10 @@ DebugControls.inputs.f4 = function() {
DebugControls.inputs.f4.doc = "Toggle drawing gizmos and names of objects."; DebugControls.inputs.f4.doc = "Toggle drawing gizmos and names of objects.";
DebugControls.inputs.f10 = function() { Time.timescale = 0.1; }; DebugControls.inputs.f10 = function() { Time.timescale = 0.1; };
DebugControls.inputs.f10.doc = "Toggle timescale to 1/10."; DebugControls.inputs.f10.doc = "Toggle timescale to 1/10.";
DebugControls.inputs.f10.released = function () { Time.timescale = 1.0; Log.warn("SET TIMESCALE");}; DebugControls.inputs.f10.released = function () { Time.timescale = 1.0; };
DebugControls.inputs.f12 = function() { GUI.defaults.debug = !GUI.defaults.debug; Log.warn("GUI toggle debug");}; DebugControls.inputs.f12 = function() { GUI.defaults.debug = !GUI.defaults.debug; Log.warn("GUI toggle debug");};
DebugControls.inputs.f12.doc = "Toggle drawing GUI debugging aids."; DebugControls.inputs.f12.doc = "Toggle drawing GUI debugging aids.";
DebugControls.inputs.f5 = function() {
if (Game.paused())
Game.play();
else
Game.pause();
};
DebugControls.inputs.f5.doc = "Pause or play game simulation."
DebugControls.inputs.f6 = function() {
if (Game.paused())
Game.step();
};
DebugControls.inputs.f6.doc = "Do one step through game while paused.";
DebugControls.inputs['M-1'] = Render.normal; DebugControls.inputs['M-1'] = Render.normal;
Render.normal.doc = "Render mode for enabling all shaders and lighting effects."; Render.normal.doc = "Render mode for enabling all shaders and lighting effects.";
DebugControls.inputs['M-2'] = Render.wireframe; DebugControls.inputs['M-2'] = Render.wireframe;

View file

@ -264,7 +264,7 @@ var editor = {
return; return;
} }
this.selectlist = []; editor.selectlist = [];
selects.forEach(function(x) { selects.forEach(function(x) {
if (x !== null) if (x !== null)
this.selectlist.push(x); this.selectlist.push(x);
@ -353,7 +353,7 @@ var editor = {
}, },
unselect() { unselect() {
this.selectlist = []; editor.selectlist = [];
this.grabselect = null; this.grabselect = null;
this.sel_comp = null; this.sel_comp = null;
}, },
@ -363,6 +363,22 @@ var editor = {
brush_obj: null, brush_obj: null,
camera_recalls: {}, camera_recalls: {},
camera_recall_stack: [],
camera_recall_store() {
this.camera_recall_stack.push({
pos:this.camera.pos,
zoom:this.camera.zoom
});
},
camera_recall_pop() {
Object.assign(this.camera, this.camera_recalls.pop());
},
camera_recall_clear() {
this.camera_recall_stack = [];
},
input_num_pressed(num) { input_num_pressed(num) {
if (Keys.ctrl()) { if (Keys.ctrl()) {
@ -373,20 +389,6 @@ var editor = {
return; return;
} }
if (Keys.alt()) {
switch(num) {
case 0:
Render.normal();
break;
case 2:
Render.wireframe();
break;
}
return;
}
if (num === 0) { if (num === 0) {
this.camera.pos = [0,0]; this.camera.pos = [0,0];
this.camera.zoom = 1; this.camera.zoom = 1;
@ -490,8 +492,8 @@ var editor = {
this.grabselect = grabobj; this.grabselect = grabobj;
if (!this.selectlist.includes(grabobj)) { if (!this.selectlist.includes(grabobj)) {
this.selectlist = []; editor.selectlist = [];
this.selectlist.push(grabobj); editor.selectlist.push(grabobj);
} }
this.moveoffset = this.grabselect.pos.sub(screen2world(Mouse.pos)); this.moveoffset = this.grabselect.pos.sub(screen2world(Mouse.pos));
@ -528,7 +530,7 @@ var editor = {
this.stash = this.edit_level.save(); this.stash = this.edit_level.save();
this.edit_level.kill(); this.edit_level.kill();
load_configs("game.config"); load_configs("game.config");
game.start(); Game.play();
unset_pawn(this); unset_pawn(this);
set_pawn(limited_editor); set_pawn(limited_editor);
Register.unregister_obj(this); Register.unregister_obj(this);
@ -997,8 +999,8 @@ var editor = {
Log.info("adding file " + file + " at pos " + pos); Log.info("adding file " + file + " at pos " + pos);
var newlvl = this.edit_level.addfile(file); var newlvl = this.edit_level.addfile(file);
newlvl.pos = pos; newlvl.pos = pos;
this.selectlist = []; editor.selectlist = [];
this.selectlist.push(newlvl); editor.selectlist.push(newlvl);
return; return;
}, },
@ -1094,14 +1096,14 @@ editor.inputs['C-y'].doc = "Open the object explorer for a selected object.";
editor.inputs['M-y'] = function() { editor.openpanel(protoexplorer); }; editor.inputs['M-y'] = function() { editor.openpanel(protoexplorer); };
editor.inputs['M-y'].doc = "Open the prototype explorer."; editor.inputs['M-y'].doc = "Open the prototype explorer.";
editor.inputs['C-S-p'] = function() { editor.inputs['C-S-t'] = function() {
if (editor.selectlist.length !== 1) return; if (editor.selectlist.length !== 1) return;
editor.openpanel(saveprototypeas); editor.openpanel(saveprototypeas);
}; };
editor.inputs['C-S-p'].doc = "Save prototype as. Ie, to a new prototype."; editor.inputs['C-S-t'].doc = "Save prototype as. Ie, to a new prototype.";
editor.inputs['C-p'] = function() { editor.save_proto(); }; editor.inputs['C-t'] = function() { editor.save_proto(); };
editor.inputs['C-p'].doc = "Save the selected prototype to disk."; editor.inputs['C-t'].doc = "Save the selected prototype to disk.";
editor.inputs['M-p'] = function() { editor.openpanel(prefabpanel); }; editor.inputs['M-p'] = function() { editor.openpanel(prefabpanel); };
editor.inputs['M-p'].doc = "Open the prefab panel."; editor.inputs['M-p'].doc = "Open the prefab panel.";
@ -1197,26 +1199,56 @@ editor.inputs.r = function() {
}; };
editor.inputs.r.doc = "Rotate selected using the mouse while held down."; editor.inputs.r.doc = "Rotate selected using the mouse while held down.";
editor.inputs.f5 = function() { editor.inputs['C-p'] = function() {
if (!Game.playing()) { if (!Game.playing()) {
editor.start_play_ed(); editor.start_play_ed();
Level.loadlevel("debug_start.lvl"); // if (!Level.loadlevel("debug"))
World.loadlevel("game");
} else {
Game.pause();
} }
}; };
editor.inputs.f5.doc = "Start level 'debug_start.lvl'."; editor.inputs['C-p'].doc = "Start game from 'debug' if it exists; otherwise, from 'game'.";
editor.inputs.f6 = function() { editor.inputs['M-p'] = function() {
if (Game.playing()) return; if (Game.playing())
Game.pause();
// Level.loadlevel Game.step();
}
editor.inputs['M-p'].doc = "Do one time step, pausing if necessary.";
editor.inputs['C-M-p'] = function() {
Log.warn(`Starting edited level ...`);
}; };
editor.inputs.f6.doc = "Load game from currently edited level."; editor.inputs['C-M-p'].doc = "Start game from currently edited level.";
editor.inputs.f7 = function() { editor.inputs['C-q'] = function() {
if (Game.playing()) return;
Level.loadlevel("game.lvl");
}; };
editor.inputs.f7.doc = "Start game from the beginning."; editor.inputs['C-q'].doc = "Quit simulation and return to editor.";
var rebinder = {};
rebinder.inputs = {};
rebinder.inputs.any = function(cmd) {
};
editor.inputs['C-space'] = function() {
};
editor.inputs['C-space'].doc = "Search to execute a specific command.";
editor.inputs['M-m'] = function() {
// set_pawn(rebinder);
};
editor.inputs['M-m'].doc = "Rebind a shortcut. Usage: M-m SHORTCUT TARGET";
editor.inputs['M-S-8'] = function() {
editor.camera_recall_pop();
};
editor.inputs['M-S-8'].doc = "Jump to last location.";
editor.inputs.escape = function() { editor.openpanel(quitpanel); } editor.inputs.escape = function() { editor.openpanel(quitpanel); }
editor.inputs.escape.doc = "Quit editor."; editor.inputs.escape.doc = "Quit editor.";
@ -1310,12 +1342,6 @@ editor.inputs['C-S-o'].doc = "Open previous level.";
editor.inputs['C-l'] = function() { editor.inputs['C-l'] = function() {
texteditor.on_close = function() { editor.edit_level.script = texteditor.value;}; texteditor.on_close = function() { editor.edit_level.script = texteditor.value;};
texteditor.input_s_pressed = function() {
if (!Keys.ctrl()) return;
editor.edit_level.script = texteditor.value;
editor.save_current();
texteditor.startbuffer = texteditor.value.slice();
};
editor.openpanel(texteditor); editor.openpanel(texteditor);
if (!editor.edit_level.script) if (!editor.edit_level.script)
@ -1858,7 +1884,11 @@ var texteditor = clone(inputpanel, {
}); });
texteditor.inputs = {}; texteditor.inputs = {};
texteditor.inputs['C-s'] = function() { editor.save_current(); }; texteditor.inputs['C-s'] = function() {
editor.edit_level.script = texteditor.value;
editor.save_current();
texteditor.startbuffer = texteditor.value.slice();
};
texteditor.inputs['C-s'].doc = "Save script to file."; texteditor.inputs['C-s'].doc = "Save script to file.";
texteditor.inputs['C-u'] = function() { this.popstate(); }; texteditor.inputs['C-u'] = function() { this.popstate(); };
@ -1999,8 +2029,6 @@ texteditor.inputs['M-n'] = function() {
texteditor.inputs['M-n'].doc = "Go down to next line with text on it."; texteditor.inputs['M-n'].doc = "Go down to next line with text on it.";
texteditor.inputs['M-n'].rep = true; texteditor.inputs['M-n'].rep = true;
var protoexplorer = copy(inputpanel, { var protoexplorer = copy(inputpanel, {
title: "prototype explorer", title: "prototype explorer",
waitclose:false, waitclose:false,
@ -2361,8 +2389,11 @@ var gen_notify = function(val, fn) {
var panel = Object.create(notifypanel); var panel = Object.create(notifypanel);
panel.msg = val; panel.msg = val;
panel.yes = fn; panel.yes = fn;
panel.input_y_pressed = function() { panel.yes(); panel.close(); }; panel.inputs = {};
panel.input_enter_pressed = function() { panel.close(); }; panel.inputs.y = function() { panel.yes(); panel.close(); };
panel.inputs.y.doc = "Confirm yes.";
panel.inputs.enter = function() { panel.close(); };
panel.inputs.enter.doc = "Close.";
return panel; return panel;
}; };
@ -2467,31 +2498,38 @@ var prefabpanel = copy(openlevelpanel, {
}, },
}); });
var limited_editor = { var limited_editor = {};
input_f1_pressed() { editor.input_f1_pressed(); },
input_f5_pressed() { limited_editor.inputs = {};
/* Pause, and resume editor */
}, limited_editor.inputs['C-p'] = function()
input_f7_pressed() { {
if (sim_playing()) { if (Game.playing())
sim_stop(); Game.pause();
game.stop(); else
Sound.killall(); Game.play();
unset_pawn(limited_editor); }
set_pawn(editor);
register_gui(editor.ed_gui, editor); limited_editor.inputs['M-p'] = function()
Debug.register_call(editor.ed_debug, editor); {
Level.kill(); Game.pause();
Level.clear_all(); Game.step();
editor.load_json(editor.stash); }
Yugine.view_camera(editor_camera);
} limited_editor.inputs['C-q'] = function()
}, {
input_f8_pressed() { sim_step(); }, Game.stop();
input_f10_pressed() { editor.input_f10_pressed(); }, game.stop();
input_f10_released() { editor.input_f10_released(); }, Sound.killall();
}; unset_pawn(limited_editor);
set_pawn(editor);
register_gui(editor.ed_gui, editor);
Debug.register_call(editor.ed_debug, editor);
World.kill();
World.clear_all();
editor.load_json(editor.stash);
Yugine.view_camera(editor_camera);
}
set_pawn(editor); set_pawn(editor);
register_gui(editor.ed_gui, editor); register_gui(editor.ed_gui, editor);

View file

@ -840,6 +840,10 @@ var Keys = {
alt() { alt() {
return cmd(50, 342) || cmd(50, 346); return cmd(50, 342) || cmd(50, 346);
}, },
super() {
return cmd(50, 343) || cmd(50, 347);
},
}; };
var Input = { var Input = {
@ -870,6 +874,10 @@ Input.print_md_kbm = function(pawn) {
return str; return str;
}; };
Input.has_bind = function(pawn, bind) {
return (typeof pawn.inputs?.[bind] === 'function');
};
function screen2world(screenpos) { return Yugine.camera.view2world(screenpos); } function screen2world(screenpos) { return Yugine.camera.view2world(screenpos); }
function world2screen(worldpos) { return Yugine.camera.world2view(worldpos); } function world2screen(worldpos) { return Yugine.camera.world2view(worldpos); }
@ -917,6 +925,10 @@ var Player = {
raw_input(cmd, state, ...args) { raw_input(cmd, state, ...args) {
for (var pawn of this.pawns.reverse()) { for (var pawn of this.pawns.reverse()) {
if (typeof pawn.inputs?.any === 'function') {
pawn.inputs.any(cmd);
return;
}
if (!pawn.inputs?.[cmd]) continue; if (!pawn.inputs?.[cmd]) continue;
var fn = null; var fn = null;
@ -1012,9 +1024,11 @@ var Register = {
if (btn === 'rmouse') if (btn === 'rmouse')
btn = 'rm'; btn = 'rm';
var e_str = ""; var e_str = "";
if (Keys.ctrl()) e_str += "C-"; if (Keys.ctrl()) e_str += "C-";
if (Keys.alt()) e_str += "M-"; if (Keys.alt()) e_str += "M-";
if (Keys.super()) e_str += "Sp-";
e_str += btn; e_str += btn;
Player.players[0].raw_input(e_str, state, ...args); Player.players[0].raw_input(e_str, state, ...args);
}, },
@ -1341,7 +1355,8 @@ var Game = {
playing() { return sys_cmd(5); }, playing() { return sys_cmd(5); },
paused() { return sys_cmd(6); }, paused() { return sys_cmd(6); },
stepping() { return cmd(79); }, stepping() {
return cmd(79); },
play() play()
{ {