Added stringifying escape & F keys; add sound, sys FFI; unconflict time.h and timer.h

This commit is contained in:
John Alanbrook 2022-12-16 17:54:05 +00:00
parent bfddf39a38
commit 6227754925
11 changed files with 149 additions and 66 deletions

View file

@ -280,7 +280,7 @@ static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int
switch (key) { switch (key) {
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
quit = true; quit();
//editor_save_projects(); //editor_save_projects();
editor_save(); editor_save();
break; break;

View file

@ -3,19 +3,22 @@
#include <stdio.h> #include <stdio.h>
#include "script.h" #include "script.h"
#include "stb_ds.h" #include "stb_ds.h"
#include "log.h"
int32_t mouseWheelX = 0; int32_t mouseWheelX = 0;
int32_t mouseWheelY = 0; int32_t mouseWheelY = 0;
int ychange = 0; int ychange = 0;
int xchange = 0; int xchange = 0;
float deltaT = 0; float deltaT = 0;
int quit = 0;
static double c_xpos; static double c_xpos;
static double c_ypos; static double c_ypos;
static int *downkeys = NULL; static int *downkeys = NULL;
static int mquit = 0;
static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos) static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos)
{ {
xchange = (int)xpos - c_xpos; xchange = (int)xpos - c_xpos;
@ -66,7 +69,51 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods
{ {
char keystr[50] = {'\0'}; char keystr[50] = {'\0'};
strcat(keystr, "input_"); strcat(keystr, "input_");
strcat(keystr, glfwGetKeyName(key, 0)); const char *kkey = glfwGetKeyName(key, scancode);
if (!kkey) {
char keybuf[10];
if (key > 289 && key < 302) {
sprintf(keybuf, "f%d", key-289);
} else {
switch(key) {
case GLFW_KEY_ENTER:
kkey = "enter";
break;
case GLFW_KEY_ESCAPE:
kkey = "escape";
break;
case GLFW_KEY_TAB:
kkey = "tab";
break;
case GLFW_KEY_RIGHT:
kkey = "right";
break;
case GLFW_KEY_LEFT:
kkey = "left";
break;
case GLFW_KEY_UP:
kkey = "up";
break;
case GLFW_KEY_DOWN:
kkey = "down";
break;
}
strcat(keystr, kkey);
}
} else {
strcat(keystr, kkey);
}
switch (action) { switch (action) {
case GLFW_PRESS: case GLFW_PRESS:
strcat(keystr, "_pressed"); strcat(keystr, "_pressed");
@ -135,3 +182,12 @@ int action_up(int scancode)
return !found; return !found;
} }
int want_quit() {
return mquit;
}
void quit() {
YughInfo("Exiting game.");
mquit = 1;
}

View file

@ -9,7 +9,6 @@ extern int32_t mouseWheelY;
extern int ychange; extern int ychange;
extern int xchange; extern int xchange;
extern float deltaT; extern float deltaT;
extern int quit;
void input_init(); void input_init();
void input_poll(double wait); void input_poll(double wait);
@ -19,6 +18,9 @@ void cursor_show();
int action_down(int scancode); int action_down(int scancode);
int want_quit();
void quit();
void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods); void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods);
struct inputaction struct inputaction

View file

@ -9,6 +9,7 @@
#include "editor.h" #include "editor.h"
#include "engine.h" #include "engine.h"
#include "log.h" #include "log.h"
#include "input.h"
#include "s7.h" #include "s7.h"
@ -56,15 +57,15 @@ s7_pointer s7_settings_cmd(s7_scheme *sc, s7_pointer args) {
YughInfo("Changing a setting."); YughInfo("Changing a setting.");
switch(cmd) { switch(cmd) {
case 0: // render fps case 0: // render fps
renderMS = (double)val; renderMS = val;
break; break;
case 1: case 1:
updateMS = (double)val; updateMS = val;
break; break;
case 2: case 2:
physMS = (double)val; physMS = val;
break; break;
} }
@ -118,27 +119,67 @@ s7_pointer s7_win_cmd(s7_scheme *sc, s7_pointer args) {
return args; return args;
} }
s7_pointer s7_win_make(s7_scheme *sc, s7_pointer args) {
/* const char *title = s7_string(s7_car(args));
int w = s7_integer(s7_cadr(args));
mrb_value mrb_ui_begin(mrb_state *mrb, mrb_value self) { int h = s7_integer(s7_caddr(args));
char *title; struct window *win = MakeSDLWindow(title, w, h, 0);
mrb_float w, h; return s7_make_integer(sc, win->id);
mrb_get_args(mrb, "zff", &title, &w, &h);
return mrb_bool_value(nk_begin(ctx, title, nk_rect(0,0,w,h), NK_WINDOW_TITLE|NK_WINDOW_BORDER|NK_WINDOW_SCALABLE|NK_WINDOW_MOVABLE|NK_WINDOW_NO_SCROLLBAR));
} }
*/ s7_pointer s7_gen_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
const char *s = s7_string(s7_cadr(args));
/* /* Branch table for general commands from scheme */
mrb_value mrb_win_make(mrb_state *mrb, mrb_value self) { /* 0 : load level */
char name[50] = "New Window"; /* 1: load prefab */
struct window *new = MakeSDLWindow(name, 500, 500, 0);
return mrb_float_value(mrb, new->id); switch (cmd) {
case 0:
load_level(s);
break;
case 1:
gameobject_makefromprefab(s);
break;
}
return args;
} }
s7_pointer s7_sys_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
switch (cmd) {
case 0:
quit();
break;
}
}
s7_pointer s7_sound_cmd(s7_scheme *sc, s7_pointer args) {
int sound = s7_integer(s7_car(args));
int cmd = s7_integer(s7_cadr(args));
switch (cmd) {
case 0: // play
break;
case 1: // pause
break;
case 2: // stop
break;
case 3: // play from beginning
break;
}
return args;
}
/*
mrb_value mrb_nuke_cb(mrb_state *mrb, mrb_value self) { mrb_value mrb_nuke_cb(mrb_state *mrb, mrb_value self) {
mrb_float win; mrb_float win;
mrb_sym cb; mrb_sym cb;
@ -155,42 +196,6 @@ mrb_value mrb_gui_cb(mrb_state *mrb, mrb_value self) {
return self; return self;
} }
mrb_value mrb_sound_make(mrb_state *mrb, mrb_value self) {
mrb_value vals;
mrb_get_args(mrb, "H", &vals);
char *name = mrb_str_to_cstr(mrb, mrb_hash_fetch(mrb, vals, mrb_symbol_value(mrb_intern_cstr(mrb, "name")), mrb_str_new_cstr(mrb, "New Window")));
YughInfo("Window name is %s.", name);
return self;
}
mrb_value mrb_sound_cmd(mrb_state *mrb, mrb_value self) {
mrb_float sound, cmd;
mrb_get_args(mrb, "ff", &sound, &cmd);
switch ((int)cmd)
{
case 0: // play
break;
case 1: // pause
break;
case 2: // stop
break;
case 3: // play from beginning
break;
}
return self;
}
mrb_value mrb_editor_render(mrb_state *mrb, mrb_value self) {
editor_render();
return self;
}
*/ */
#define S7_FUNC(NAME, ARGS) s7_define_function(s7, #NAME, s7_ ##NAME, ARGS, 0, 0, "") #define S7_FUNC(NAME, ARGS) s7_define_function(s7, #NAME, s7_ ##NAME, ARGS, 0, 0, "")
@ -205,5 +210,9 @@ void ffi_load() {
S7_FUNC(win_cmd, 2); S7_FUNC(win_cmd, 2);
S7_FUNC(ui_rendertext, 3); S7_FUNC(ui_rendertext, 3);
S7_FUNC(log, 4); S7_FUNC(log, 4);
S7_FUNC(win_make, 3);
S7_FUNC(gen_cmd, 2);
S7_FUNC(sys_cmd, 1);
S7_FUNC(sound_cmd, 2);
} }

View file

@ -10,6 +10,8 @@
#include "stb_ds.h" #include "stb_ds.h"
#include "timer.h" #include "timer.h"
#include "time.h"
#define SHADER_BUF 10000 #define SHADER_BUF 10000
static struct shader *shaders; static struct shader *shaders;
@ -89,6 +91,7 @@ GLuint load_shader_from_file(const char *path, int type)
void shader_compile(struct shader *shader) void shader_compile(struct shader *shader)
{ {
YughInfo("Making shader with %s and %s.", shader->vertpath, shader->fragpath); YughInfo("Making shader with %s and %s.", shader->vertpath, shader->fragpath);
clock_t startt = clock();
GLuint vert = load_shader_from_file(shader->vertpath, GL_VERTEX_SHADER); GLuint vert = load_shader_from_file(shader->vertpath, GL_VERTEX_SHADER);
GLuint frag = load_shader_from_file(shader->fragpath, GL_FRAGMENT_SHADER); GLuint frag = load_shader_from_file(shader->fragpath, GL_FRAGMENT_SHADER);
@ -101,6 +104,9 @@ void shader_compile(struct shader *shader)
glDeleteShader(vert); glDeleteShader(vert);
glDeleteShader(frag); glDeleteShader(frag);
startt = clock() - startt;
YughInfo("Created shader in %d ticks (%f seconds).", startt, ((float)startt/CLOCKS_PER_SEC));
} }
void shader_use(struct shader *shader) void shader_use(struct shader *shader)

View file

@ -215,7 +215,7 @@ void anim_play(struct TexAnimation *anim)
if (anim->timer == NULL) if (anim->timer == NULL)
anim->timer = timer_make(1.f / anim->tex->anim.ms, tex_incr_anim, anim); anim->timer = timer_make(1.f / anim->tex->anim.ms, tex_incr_anim, anim);
else else
timer_settime(anim->timer, 1.f/anim->tex->anim.ms); timerr_settime(anim->timer, 1.f/anim->tex->anim.ms);
timer_start(anim->timer); timer_start(anim->timer);
} }

View file

@ -77,12 +77,12 @@ void timer_remove(struct timer *t) {
timers[i].timerid = i; timers[i].timerid = i;
} }
void timer_settime(struct timer *t, double interval) { void timerr_settime(struct timer *t, double interval) {
//double elapsed = time - (t->fire_time - t->interval); //double elapsed = time - (t->fire_time - t->interval);
t->interval = interval; t->interval = interval;
t->remain_time = t->interval; t->remain_time = t->interval;
// TODO: timer_settime reacts to elapsed time // TODO: timerr_settime reacts to elapsed time
} }
void *arrfind(void *arr, int (*valid)(void *arr, void *cmp), void *cmp) void *arrfind(void *arr, int (*valid)(void *arr, void *cmp), void *cmp)

View file

@ -19,7 +19,7 @@ void timer_start(struct timer *t);
void timer_pause(struct timer *t); void timer_pause(struct timer *t);
void timer_stop(struct timer *t); void timer_stop(struct timer *t);
void timer_update(double s); void timer_update(double s);
void timer_settime(struct timer *t, double interval); void timerr_settime(struct timer *t, double interval);

View file

@ -66,7 +66,7 @@ void window_framebuffer_size_cb(GLFWwindow *w, int width, int height)
void window_close_callback(GLFWwindow *w) void window_close_callback(GLFWwindow *w)
{ {
quit = 1; quit();
} }
@ -267,7 +267,6 @@ void window_seticon(struct window *w, struct Texture *icon)
images[0].height = icon->height; images[0].height = icon->height;
images[0].pixels = icon->data; images[0].pixels = icon->data;
glfwSetWindowIcon(w->window, 1, images); glfwSetWindowIcon(w->window, 1, images);
} }
int window_hasfocus(struct window *w) int window_hasfocus(struct window *w)

View file

@ -154,7 +154,7 @@ int main(int argc, char **args) {
double lastTick; double lastTick;
while (!quit) { while (!want_quit()) {
double elapsed; double elapsed;
elapsed = glfwGetTime() - lastTick; elapsed = glfwGetTime() - lastTick;
lastTick = glfwGetTime(); lastTick = glfwGetTime();

View file

@ -32,4 +32,15 @@
(define (win_fulltoggle w) (win_cmd w 0)) (define (win_fulltoggle w) (win_cmd w 0))
(define (win_fullscreen w) (win_cmd w 1)) (define (win_fullscreen w) (win_cmd w 1))
(define (win_unfullscreen w) (win_cmd w 2)) (define (win_unfullscreen w) (win_cmd w 2))
(define (win_title s) (win_cmd 0 3 s))
(define (load_level s) (gen_cmd 0 s))
(define (load_prefab s) (gen_cmd 1 s))
(define (quit) (sys_cmd 0))
(define (exit) (quit))
(define (sound_play sound) (sound_cmd sound 0))
(define (sound_pause sound) (sound_cmd sound 1))
(define (sound_stop sound) (sound_cmd sound 2))
(define (sound_restart sound) (sound_cmd sound 3))