Add camera

This commit is contained in:
John Alanbrook 2022-12-22 22:58:06 +00:00
parent efaf8736d8
commit 8729892939
13 changed files with 188 additions and 77 deletions

View file

@ -10,6 +10,8 @@
#include <window.h> #include <window.h>
#include "log.h" #include "log.h"
#include "openglrender.h"
#include <stb_truetype.h> #include <stb_truetype.h>
static uint32_t VBO = 0; static uint32_t VBO = 0;
@ -181,9 +183,11 @@ void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3
shader_use(shader); shader_use(shader);
shader_setvec3(shader, "textColor", color); shader_setvec3(shader, "textColor", color);
cpVect campos = cam_pos();
mfloat_t cursor[2] = { 0.f }; mfloat_t cursor[2] = { 0.f };
cursor[0] = pos[0]; cursor[0] = pos[0] + campos.x;
cursor[1] = pos[1]; cursor[1] = pos[1] + campos.y;
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO); glBindVertexArray(VAO);

View file

@ -44,8 +44,10 @@ static void gameobject_setpickcolor(struct gameobject *go)
go->editor.color[2] = b; go->editor.color[2] = b;
} }
struct gameobject *MakeGameobject() int MakeGameobject()
{ {
if (gameobjects == NULL) arrsetcap(gameobjects, 100);
YughInfo("Making new gameobject"); YughInfo("Making new gameobject");
struct gameobject go = { struct gameobject go = {
.editor.id = arrlen(gameobjects), .editor.id = arrlen(gameobjects),
@ -62,7 +64,7 @@ struct gameobject *MakeGameobject()
arrput(gameobjects, go); arrput(gameobjects, go);
return &arrlast(gameobjects); return arrlen(gameobjects)-1;
} }
void gameobject_addcomponent(struct gameobject *go, struct component *c) void gameobject_addcomponent(struct gameobject *go, struct component *c)
@ -131,7 +133,7 @@ int gameobject_makefromprefab(char *path)
return; return;
} }
struct gameobject *new = MakeGameobject(); struct gameobject *new = get_gameobject_from_id(MakeGameobject());
fread(new, sizeof(*new), 1, fprefab); fread(new, sizeof(*new), 1, fprefab);
new->components = NULL; new->components = NULL;
@ -217,8 +219,8 @@ void toggleprefab(struct gameobject *go)
void gameobject_move(struct gameobject *go, float xs, float ys) void gameobject_move(struct gameobject *go, float xs, float ys)
{ {
cpVect p = cpBodyGetPosition(go->body); cpVect p = cpBodyGetPosition(go->body);
p.x += xs * deltaT; p.x += xs;
p.y += ys * deltaT; p.y += ys;
cpBodySetPosition(go->body, p); cpBodySetPosition(go->body, p);
phys2d_reindex_body(go->body); phys2d_reindex_body(go->body);
@ -326,3 +328,34 @@ void gameobject_draw_debugs() {
} }
} }
} }
static struct {struct gameobject go; cpVect pos; float angle; } *saveobjects = NULL;
void gameobject_saveall() {
arrfree(saveobjects);
arrsetlen(saveobjects, arrlen(gameobjects));
for (int i = 0; i < arrlen(gameobjects); i++) {
saveobjects[i].go = gameobjects[i];
saveobjects[i].pos = cpBodyGetPosition(gameobjects[i].body);
saveobjects[i].angle = cpBodyGetAngle(gameobjects[i].body);
}
}
void gameobject_loadall() {
YughInfo("N gameobjects: %d, N saved: %d", arrlen(gameobjects), arrlen(saveobjects));
for (int i = 0; i < arrlen(saveobjects); i++) {
gameobjects[i] = saveobjects[i].go;
cpBodySetPosition(gameobjects[i].body, saveobjects[i].pos);
cpBodySetAngle(gameobjects[i].body, saveobjects[i].angle);
cpBodySetVelocity(gameobjects[i].body, cpvzero);
cpBodySetAngularVelocity(gameobjects[i].body, 0.f);
}
arrfree(saveobjects);
}
int gameobjects_saved() {
return arrlen(saveobjects);
}

View file

@ -43,7 +43,7 @@ struct gameobject {
extern struct gameobject *gameobjects; extern struct gameobject *gameobjects;
struct gameobject *MakeGameobject(); int MakeGameobject();
void init_gameobjects(); void init_gameobjects();
void gameobject_delete(int id); void gameobject_delete(int id);
void clear_gameobjects(); void clear_gameobjects();
@ -76,4 +76,8 @@ void gameobject_draw_debugs();
void object_gui(struct gameobject *go); void object_gui(struct gameobject *go);
void gameobject_saveall();
void gameobject_loadall();
int gameobjects_saved();
#endif #endif

View file

@ -45,8 +45,6 @@ void input_init()
{ {
glfwSetCursorPosCallback(mainwin->window, cursor_pos_cb); glfwSetCursorPosCallback(mainwin->window, cursor_pos_cb);
glfwSetScrollCallback(mainwin->window, scroll_cb); glfwSetScrollCallback(mainwin->window, scroll_cb);
} }
void call_input_signal(char *signal) { void call_input_signal(char *signal) {
@ -54,40 +52,15 @@ void call_input_signal(char *signal) {
script_eval_w_env(signal, pawns[i]); script_eval_w_env(signal, pawns[i]);
} }
void call_input_down(int *key) { const char *keyname_extd(int key, int scancode) {
const char *keyname = glfwGetKeyName(*key, 0); char keybuf[50];
char keystr[50] = {'\0'};
snprintf(keystr, 50, "input_%s_down", keyname);
call_input_signal(keystr);
}
/* This is called once every frame - or more if we want it more! */
void input_poll(double wait)
{
ychange = 0;
xchange = 0;
mouseWheelX = 0;
mouseWheelY = 0;
glfwWaitEventsTimeout(wait);
//editor_input(&e);
for (int i = 0; i < arrlen(downkeys); i++)
call_input_down(&downkeys[i]);
}
void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods)
{
char keystr[50] = {'\0'};
strcat(keystr, "input_");
const char *kkey = glfwGetKeyName(key, scancode); const char *kkey = glfwGetKeyName(key, scancode);
if (!kkey) { if (kkey) return kkey;
char keybuf[10];
if (key > 289 && key < 302) { if (key > 289 && key < 302) {
sprintf(keybuf, "f%d", key-289); sprintf(keybuf, "f%d", key-289);
strcat(keystr, keybuf); return keybuf;
} else { } else {
switch(key) { switch(key) {
case GLFW_KEY_ENTER: case GLFW_KEY_ENTER:
@ -143,20 +116,43 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods
break; break;
} }
if (kkey) if (kkey) return kkey;
strcat(keystr, kkey);
else
YughWarn("Could not get key string for key %d, scancode %d.", key, scancode);
} }
return "NULL";
} else {
strcat(keystr, kkey);
} }
void call_input_down(int *key) {
char keystr[50] = {'\0'};
snprintf(keystr, 50, "input_%s_down", keyname_extd(*key, 0));
call_input_signal(keystr);
}
/* This is called once every frame - or more if we want it more! */
void input_poll(double wait)
{
ychange = 0;
xchange = 0;
mouseWheelX = 0;
mouseWheelY = 0;
glfwWaitEventsTimeout(wait);
//editor_input(&e);
for (int i = 0; i < arrlen(downkeys); i++)
call_input_down(&downkeys[i]);
}
void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods)
{
char keystr[50] = {'\0'};
const char *kkey = keyname_extd(key, scancode);
switch (action) { switch (action) {
case GLFW_PRESS: case GLFW_PRESS:
strcat(keystr, "_pressed"); snprintf(keystr, 50, "input_%s_pressed", kkey);
int found = 0; int found = 0;
@ -170,7 +166,7 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods
break; break;
case GLFW_RELEASE: case GLFW_RELEASE:
strcat(keystr, "_released"); snprintf(keystr, 50, "input_%s_released", kkey);
for (int i = 0; i < arrlen(downkeys); i++) { for (int i = 0; i < arrlen(downkeys); i++) {
if (downkeys[i] == key) { if (downkeys[i] == key) {
@ -182,7 +178,7 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods
break; break;
case GLFW_REPEAT: case GLFW_REPEAT:
strcat(keystr, "_rep"); snprintf(keystr, 50, "input_%s_rep", kkey);
break; break;
} }

View file

@ -90,6 +90,14 @@ s7_pointer s7_settings_cmd(s7_scheme *sc, s7_pointer args) {
case 3: case 3:
debug_draw_phys(val); debug_draw_phys(val);
break; break;
case 4:
set_timescale(val);
break;
case 5:
add_zoom(val);
break;
} }
return args; return args;
@ -202,6 +210,9 @@ s7_pointer s7_sys_cmd(s7_scheme *sc, s7_pointer args) {
case 6: case 6:
return s7_make_boolean(sc, sim_paused()); return s7_make_boolean(sc, sim_paused());
case 7:
return s7_make_integer(sc, MakeGameobject());
} }
return args; return args;
@ -337,6 +348,17 @@ s7_pointer s7_phys_set(s7_scheme *sc, s7_pointer args) {
phys2d_set_gravity(x, y); phys2d_set_gravity(x, y);
} }
s7_pointer s7_int_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
int val = s7_integer(s7_cadr(args));
switch (cmd) {
case 0:
set_cam_body(get_gameobject_from_id(val)->body);
break;
}
}
#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, "")
void ffi_load() { void ffi_load() {
@ -362,5 +384,6 @@ void ffi_load() {
S7_FUNC(phys_cmd, 3); S7_FUNC(phys_cmd, 3);
S7_FUNC(phys_q, 2); S7_FUNC(phys_q, 2);
S7_FUNC(phys_set, 3); S7_FUNC(phys_set, 3);
S7_FUNC(int_cmd, 2);
} }

View file

@ -12,6 +12,8 @@
#include "log.h" #include "log.h"
#include "datastream.h" #include "datastream.h"
int renderMode = 0; int renderMode = 0;
static GLuint UBO; static GLuint UBO;
@ -107,7 +109,18 @@ void openglInit()
} }
static struct mCamera mcamera = {0}; static cpBody *camera = NULL;
void set_cam_body(cpBody *body) {
camera = body;
}
cpVect cam_pos() {
return camera ? cpBodyGetPosition(camera) : cpvzero;
}
static float zoom = 1.f;
void add_zoom(float val) { zoom = val; }
void openglRender(struct window *window) void openglRender(struct window *window)
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@ -116,10 +129,12 @@ void openglRender(struct window *window)
//////////// 2D projection //////////// 2D projection
mfloat_t projection[16] = { 0.f }; mfloat_t projection[16] = { 0.f };
mat4_ortho(projection, mcamera.transform.position[0], cpVect pos = cam_pos();
window->width + mcamera.transform.position[0], mat4_ortho(projection, pos.x,
mcamera.transform.position[1], window->width*zoom + pos.x,
window->height + mcamera.transform.position[1], -1.f, 1.f); pos.y,
window->height*zoom + pos.y, -1.f, 1.f);
glBindBuffer(GL_UNIFORM_BUFFER, projUBO); glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection); glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection);
@ -130,7 +145,6 @@ void openglRender(struct window *window)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
////// TEXT && GUI ////// TEXT && GUI
//script_call_sym(window->gui_cb);
call_gui(); call_gui();
//// DEBUG //// DEBUG

View file

@ -28,6 +28,8 @@ extern GLuint debugColorPickBO;
extern struct gameobject *selectedobject; extern struct gameobject *selectedobject;
#include <chipmunk/chipmunk.h>
enum RenderMode { enum RenderMode {
LIT, LIT,
UNLIT, UNLIT,
@ -46,4 +48,8 @@ void debug_draw_phys(int draw);
void BindUniformBlock(GLuint shaderID, const char *bufferName, GLuint bufferBind); void BindUniformBlock(GLuint shaderID, const char *bufferName, GLuint bufferBind);
void set_cam_body(cpBody *body);
cpVect cam_pos();
void add_zoom(float val);
#endif #endif

View file

@ -108,9 +108,9 @@ void register_update(s7_pointer sym) {
arrput(updates, sym); arrput(updates, sym);
} }
void call_updates() { void call_updates(double dt) {
for (int i = 0; i < arrlen(updates); i++) for (int i = 0; i < arrlen(updates); i++)
script_call_sym(updates[i]); s7_call(s7, updates[i], s7_cons(s7, s7_make_real(s7, dt), s7_nil(s7)));
} }
void register_gui(s7_pointer sym) { void register_gui(s7_pointer sym) {
@ -126,7 +126,7 @@ void register_physics(s7_pointer sym) {
arrput(physics, sym); arrput(physics, sym);
} }
void call_physics() { void call_physics(double dt) {
for (int i = 0; i < arrlen(physics); i++) for (int i = 0; i < arrlen(physics); i++)
script_call_sym(physics[i]); s7_call(s7, physics[i], s7_cons(s7, s7_make_real(s7, dt), s7_nil(s7)));
} }

View file

@ -15,12 +15,12 @@ int script_has_sym(s7_pointer sym);
void script_eval_w_env(const char *s, s7_pointer env); void script_eval_w_env(const char *s, s7_pointer env);
void register_update(s7_pointer sym); void register_update(s7_pointer sym);
void call_updates(); void call_updates(double dt);
void register_gui(s7_pointer sym); void register_gui(s7_pointer sym);
void call_gui(); void call_gui();
void register_physics(s7_pointer sym); void register_physics(s7_pointer sym);
void call_physics(); void call_physics(double dt);
#endif #endif

View file

@ -10,11 +10,9 @@ struct mTransform {
}; };
struct mTransform MakeTransform(mfloat_t pos[3], mfloat_t rotation[3], struct mTransform MakeTransform(mfloat_t pos[3], mfloat_t rotation[3], float scale);
float scale);
mfloat_t *trans_forward(mfloat_t * res, mfloat_t *trans_forward(mfloat_t * res, const struct mTransform *const trans);
const struct mTransform *const trans);
mfloat_t *trans_back(mfloat_t * res, const struct mTransform *trans); mfloat_t *trans_back(mfloat_t * res, const struct mTransform *trans);
mfloat_t *trans_up(mfloat_t * res, const struct mTransform *trans); mfloat_t *trans_up(mfloat_t * res, const struct mTransform *trans);
mfloat_t *trans_down(mfloat_t * res, const struct mTransform *trans); mfloat_t *trans_down(mfloat_t * res, const struct mTransform *trans);

View file

@ -38,6 +38,8 @@ static int ed = 1;
static int sim_play = 0; static int sim_play = 0;
static double lastTick; static double lastTick;
static float timescale = 1.f;
void seghandle(int sig) { void seghandle(int sig) {
#ifdef __linux__ #ifdef __linux__
void *ents[512]; void *ents[512];
@ -164,11 +166,13 @@ int main(int argc, char **args) {
if (sim_play) { if (sim_play) {
physlag += elapsed; physlag += elapsed;
call_updates(); call_updates(elapsed * timescale);
if (physlag >= physMS) {
while (physlag >= physMS) {
physlag -= physMS; physlag -= physMS;
phys2d_update(physMS); phys2d_update(physMS * timescale);
call_physics(); call_physics(physMS * timescale);
if (sim_play == 2) sim_pause();
} }
} }
@ -191,8 +195,13 @@ int main(int argc, char **args) {
int sim_playing() { return sim_play; } int sim_playing() { return sim_play; }
int sim_paused() { return (!sim_play && gameobjects_saved()); }
void sim_start() { void sim_start() {
/* Save starting state of everything */ /* Save starting state of everything */
if (!gameobjects_saved())
gameobject_saveall();
sim_play = 1; sim_play = 1;
} }
@ -200,15 +209,18 @@ void sim_pause() {
sim_play = 0; sim_play = 0;
} }
int sim_paused() {
return sim_play;
}
void sim_stop() { void sim_stop() {
/* Revert starting state of everything from sim_start */ /* Revert starting state of everything from sim_start */
sim_play = 0; sim_play = 0;
gameobject_loadall();
} }
void sim_step() { void sim_step() {
if (sim_paused()) {
sim_play = 2;
}
}
void set_timescale(float val) {
timescale = val;
} }

View file

@ -7,6 +7,7 @@ void sim_start();
void sim_pause(); void sim_pause();
void sim_stop(); void sim_stop();
void sim_step(); void sim_step();
void set_timescale(float val);
#endif #endif

View file

@ -38,6 +38,7 @@
(define (set_fps fps) (settings_cmd 0 (/ 1 fps))) (define (set_fps fps) (settings_cmd 0 (/ 1 fps)))
(define (set_update fps) (settings_cmd 1 (/ 1 fps))) (define (set_update fps) (settings_cmd 1 (/ 1 fps)))
(define (set_phys fps) (settings_cmd 2 (/ 1 fps))) (define (set_phys fps) (settings_cmd 2 (/ 1 fps)))
(define (zoom! amt) (settings_cmd 5 amt))
(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))
@ -46,6 +47,7 @@
(define (load_level s) (gen_cmd 0 s)) (define (load_level s) (gen_cmd 0 s))
(define (load_prefab s) (gen_cmd 1 s)) (define (load_prefab s) (gen_cmd 1 s))
(define (newobject) (sys_cmd 7))
(define (quit) (sys_cmd 0)) (define (quit) (sys_cmd 0))
(define (exit) (quit)) (define (exit) (quit))
@ -63,7 +65,10 @@
((gui) 1)) ,f)))) ((gui) 1)) ,f))))
(define-macro (update . expr) (define-macro (update . expr)
`(registertype update ,@expr)) (let ((f (gensym)))
`(begin
(define (,f dt) (begin . ,expr))
(register 0 ,f))))
(define-macro (gui . expr) (define-macro (gui . expr)
`(registertype gui ,@expr)) `(registertype gui ,@expr))
@ -94,6 +99,7 @@
(define (b2i val) (if (eq? val #f) 0 1)) (define (b2i val) (if (eq? val #f) 0 1))
(define (dbg_draw_phys val) (settings_cmd 3 (b2i val))) (define (dbg_draw_phys val) (settings_cmd 3 (b2i val)))
(define (timescale! val) (settings_cmd 4 val))
(define (sim_play) (sys_cmd 1)) (define (sim_play) (sys_cmd 1))
(define (sim_stop) (sys_cmd 2)) (define (sim_stop) (sys_cmd 2))
(define (sim_pause) (sys_cmd 3)) (define (sim_pause) (sys_cmd 3))
@ -101,6 +107,8 @@
(define (sim_play?) (sys_cmd 5)) (define (sim_play?) (sys_cmd 5))
(define (sim_pause?) (sys_cmd 6)) (define (sim_pause?) (sys_cmd 6))
(define (camera! body) (int_cmd 0 body))
(define (bodytype? body) (phys_q body 0)) (define (bodytype? body) (phys_q body 0))
(define-macro (register-phys type body . expr) (define-macro (register-phys type body . expr)
@ -126,3 +134,15 @@
,@lets) ,@lets)
,@expr ,@expr
(curlet)))) (curlet))))
(define-macro (input key . expr)
`(define (,(symbol "input_" (symbol->string key))) (begin . ,expr)))
(define-macro (+= var amt)
`(set! ,var (+ ,var ,amt)))
(define-macro (-= var amt)
`(set! ,var (- ,var ,amt)))
(define (attach-script script object)
(let-set! script 'body object))