level saving and loading

This commit is contained in:
John Alanbrook 2023-02-03 19:41:53 +00:00
parent 7e1eaaae64
commit bea49b5e64
6 changed files with 49 additions and 41 deletions

View file

@ -54,8 +54,6 @@ cpShape *phys2d_query_pos(cpVect pos)
filter.categories = CP_ALL_CATEGORIES; filter.categories = CP_ALL_CATEGORIES;
cpShape *find = cpSpacePointQueryNearest(space, pos, 0.f, filter, NULL); cpShape *find = cpSpacePointQueryNearest(space, pos, 0.f, filter, NULL);
YughInfo("Hit a %p", find);
return find; return find;
} }

View file

@ -202,6 +202,10 @@ duk_ret_t duk_nuke(duk_context *duk)
case 5: case 5:
nuke_label(duk_to_string(duk, 1)); nuke_label(duk_to_string(duk, 1));
return 0; return 0;
case 6:
duk_push_boolean(duk, nuke_btn(duk_to_string(duk, 1)));
return 1;
} }
return 0; return 0;

View file

@ -75,8 +75,9 @@ void font_init(struct shader *textshader) {
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindVertexArray(0); glBindVertexArray(0);
@ -119,7 +120,7 @@ struct sFont *MakeFont(const char *fontfile, int height)
stbtt_PackFontRange(&pc, ttf_buffer, 0, height, 32, 95, glyphs); stbtt_PackFontRange(&pc, ttf_buffer, 0, height, 32, 95, glyphs);
stbtt_PackEnd(&pc); stbtt_PackEnd(&pc);
//stbi_write_png("packedfont.png", packsize, packsize, 1, bitmap, sizeof(char)*packsize); stbi_write_png("packedfont.png", packsize, packsize, 1, bitmap, sizeof(char) * packsize);
stbtt_fontinfo fontinfo; stbtt_fontinfo fontinfo;
if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0))) { if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0))) {
@ -162,6 +163,7 @@ struct sFont *MakeFont(const char *fontfile, int height)
} }
static int curchar = 0; static int curchar = 0;
static float *buffdraw;
void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct shader *shader, float color[3]) void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct shader *shader, float color[3])
{ {
@ -171,13 +173,14 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct
float xpos = cursor[0] + c.Bearing[0] * scale; float xpos = cursor[0] + c.Bearing[0] * scale;
float ypos = cursor[1] - c.Bearing[1] * scale; float ypos = cursor[1] - c.Bearing[1] * scale;
float verts[4 * 4] = { float verts[16] = {
xpos, ypos, c.rect.s0, c.rect.t1, xpos, ypos, c.rect.s0, c.rect.t1,
xpos+w, ypos, c.rect.s1, c.rect.t1, xpos+w, ypos, c.rect.s1, c.rect.t1,
xpos, ypos + h, c.rect.s0, c.rect.t0, xpos, ypos + h, c.rect.s0, c.rect.t0,
xpos + w, ypos + h, c.rect.s1, c.rect.t0 xpos + w, ypos + h, c.rect.s1, c.rect.t0
}; };
/* Check if the vertex is off screen */
if (verts[5] < 0 || verts[10] < 0 || verts[0] > window_i(0)->width || verts[1] > window_i(0)->height) if (verts[5] < 0 || verts[10] < 0 || verts[0] > window_i(0)->width || verts[1] > window_i(0)->height)
return; return;
@ -204,18 +207,17 @@ void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, font->texID); glBindTexture(GL_TEXTURE_2D, font->texID);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, len*16*sizeof(float), NULL, GL_STREAM_DRAW); glBufferData(GL_ARRAY_BUFFER, len*16*sizeof(float), NULL, GL_STREAM_DRAW);
glBindVertexArray(VAO);
const unsigned char *line, *wordstart; const unsigned char *line, *wordstart;
line = (unsigned char*)text; line = (unsigned char*)text;
curchar = 0; curchar = 0;
while (*line != '\0') { while (*line != '\0') {
switch (*line) { switch (*line) {
@ -252,5 +254,5 @@ void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3
} }
} }
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4*curchar); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4*curchar);
} }

View file

@ -149,7 +149,7 @@ void gameobject_clean(int id) {
void gameobjects_cleanup() { void gameobjects_cleanup() {
int clean = first; int clean = first;
while (clean > 0 && id2go(clean)->body) { while (clean >= 0 && id2go(clean)->body) {
gameobject_clean(clean); gameobject_clean(clean);
clean = id2go(clean)->next; clean = id2go(clean)->next;
} }

View file

@ -31,6 +31,21 @@ void remove_pawn(void *pawn) {
} }
} }
void add_downkey(int key) {
for (int i = 0; i < arrlen(downkeys); i++)
if (downkeys[i] == key) return;
arrput(downkeys, key);
}
void rm_downkey(int key) {
for (int i = 0; i < arrlen(downkeys); i++)
if (downkeys[i] == key) {
arrdelswap(downkeys, i);
return;
}
}
static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos) static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos)
{ {
mouse_delta.x = xpos - mouse_pos.x; mouse_delta.x = xpos - mouse_pos.x;
@ -60,10 +75,12 @@ static void mb_cb(GLFWwindow *w, int button, int action, int mods)
switch (action) { switch (action) {
case GLFW_PRESS: case GLFW_PRESS:
act = "pressed"; act = "pressed";
add_downkey(button);
break; break;
case GLFW_RELEASE: case GLFW_RELEASE:
act = "released"; act = "released";
rm_downkey(button);
break; break;
case GLFW_REPEAT: case GLFW_REPEAT:
@ -71,15 +88,7 @@ static void mb_cb(GLFWwindow *w, int button, int action, int mods)
break; break;
} }
switch (button) { btn = keyname_extd(button, button);
case GLFW_MOUSE_BUTTON_LEFT:
btn = "lmouse";
break;
case GLFW_MOUSE_BUTTON_RIGHT:
btn = "rmouse";
break;
}
if (!act || !btn) { if (!act || !btn) {
YughError("Tried to call a mouse action that doesn't exist."); YughError("Tried to call a mouse action that doesn't exist.");
@ -174,6 +183,18 @@ const char *keyname_extd(int key, int scancode) {
case GLFW_KEY_SPACE: case GLFW_KEY_SPACE:
kkey = "space"; kkey = "space";
break; break;
case GLFW_MOUSE_BUTTON_RIGHT:
kkey = "rmouse";
break;
case GLFW_MOUSE_BUTTON_LEFT:
kkey = "lmouse";
break;
case GLFW_MOUSE_BUTTON_MIDDLE:
kkey = "mmouse";
break;
} }
if (kkey) return kkey; if (kkey) return kkey;
@ -198,7 +219,6 @@ void input_poll(double wait)
glfwWaitEventsTimeout(wait); glfwWaitEventsTimeout(wait);
//editor_input(&e); //editor_input(&e);
for (int i = 0; i < arrlen(downkeys); i++) for (int i = 0; i < arrlen(downkeys); i++)
call_input_down(&downkeys[i]); call_input_down(&downkeys[i]);
@ -212,28 +232,12 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods
switch (action) { switch (action) {
case GLFW_PRESS: case GLFW_PRESS:
snprintf(keystr, 50, "input_%s_pressed", kkey); snprintf(keystr, 50, "input_%s_pressed", kkey);
add_downkey(key);
int found = 0;
for (int i = 0; i < arrlen(downkeys); i++) {
if (downkeys[i] == key)
goto SCRIPTCALL;
}
arrput(downkeys, key);
break; break;
case GLFW_RELEASE: case GLFW_RELEASE:
snprintf(keystr, 50, "input_%s_released", kkey); snprintf(keystr, 50, "input_%s_released", kkey);
rm_downkey(key);
for (int i = 0; i < arrlen(downkeys); i++) {
if (downkeys[i] == key) {
arrdelswap(downkeys, i);
goto SCRIPTCALL;
}
}
break; break;
case GLFW_REPEAT: case GLFW_REPEAT:
@ -241,7 +245,6 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods
break; break;
} }
SCRIPTCALL:
call_input_signal(keystr); call_input_signal(keystr);
} }

View file

@ -21,6 +21,7 @@ void cursor_hide();
void cursor_show(); void cursor_show();
void call_input_signal(char *signal); void call_input_signal(char *signal);
const char *keyname_extd(int key, int scancode);
int action_down(int scancode); int action_down(int scancode);
int want_quit(); int want_quit();