This commit is contained in:
John Alanbrook 2023-02-13 14:30:35 +00:00
parent 8155bddb5e
commit fceb68a910
10 changed files with 88 additions and 10 deletions

View file

@ -6,6 +6,7 @@
#include "log.h"
#include <assert.h>
#include "debug.h"
#include "window.h"
#include "stb_ds.h"
@ -69,6 +70,7 @@ void draw_edge(cpVect *points, int n, float *color)
shader_use(rectShader);
shader_setvec3(rectShader, "linecolor", color);
glLineWidth(20);
glBindBuffer(GL_ARRAY_BUFFER, rectVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * n * 2, points, GL_DYNAMIC_DRAW);
glBindVertexArray(rectVAO);
@ -77,6 +79,7 @@ void draw_edge(cpVect *points, int n, float *color)
shader_setfloat(rectShader, "alpha", 1.f);
glDrawArrays(GL_LINE_STRIP, 0, n);
glLineWidth(1);
}
void draw_circle(int x, int y, float radius, int pixels, float *color, int fill)
@ -130,10 +133,14 @@ void draw_grid(int width, int span)
shader_use(gridShader);
shader_setint(gridShader, "thickness", width);
shader_setint(gridShader, "span", span);
cpVect offset = cam_pos();
offset.x -= mainwin->width/2;
offset.y -= mainwin->height/2;
shader_setvec2(gridShader, "offset", &offset);
glBindVertexArray(gridVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void draw_point(int x, int y, float r, float *color)

View file

@ -636,6 +636,23 @@ duk_ret_t duk_cmd(duk_context *duk) {
if (!id2sprite(duk_to_int(duk, 1))) return 0;
id2sprite(duk_to_int(duk, 1))->layer = duk_to_int(duk, 2);
break;
case 61:
set_cam_body(id2body(duk_to_int(duk, 1)));
break;
case 62:
add_zoom(duk_to_number(duk, 1));
break;
case 63:
duk_push_number(duk, deltaT);
return 1;
case 64:
vect2duk(tex_get_dimensions(texture_pullfromfile(duk_to_string(duk, 1))));
return 1;
}
return 0;
@ -1023,6 +1040,10 @@ duk_ret_t duk_cmd_edge2d(duk_context *duk)
phys2d_edge_clearverts(edge);
phys2d_edge_addverts(edge, duk2cpvec2arr(duk, 2));
break;
case 1:
edge->thickness = duk_to_number(duk, 2);
break;
}
return 0;

View file

@ -38,6 +38,16 @@ int body2id(cpBody *body)
return cpBodyGetUserData(body);
}
cpBody *id2body(int id)
{
struct gameobject *go;
if (go = id2go(id))
return go->body;
return NULL;
}
int shape2gameobject(cpShape *shape)
{
struct phys2d_shape *s = cpShapeGetUserData(shape);

View file

@ -50,6 +50,7 @@ struct gameobject *get_gameobject_from_id(int id);
struct gameobject *id2go(int id);
int id_from_gameobject(struct gameobject *go);
int body2id(cpBody *body);
cpBody *id2body(int id);
void go_shape_apply(cpBody *body, cpShape *shape, struct gameobject *go);

View file

@ -62,6 +62,15 @@ static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos)
}
static void pawn_call_keydown(int key)
{
for (int i = 0; i < arrlen(pawns); i++) {
if (!pawns[i] || script_eval_setup("input_num_pressed", pawns[i])) continue;
duk_push_int(duk, key);
script_eval_exec(1);
}
}
static void scroll_cb(GLFWwindow *w, double xoffset, double yoffset)
{
mouseWheelY = yoffset;
@ -260,6 +269,10 @@ void input_poll(double wait)
call_input_down(&downkeys[i]);
}
int key_is_num(int key) {
return key <= 57 && key >= 48;
}
void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods)
{
char keystr[50] = {'\0'};
@ -270,6 +283,11 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods
snprintf(keystr, 50, "input_%s_pressed", kkey);
add_downkey(key);
call_input_signal("input_any_pressed");
if (key_is_num(key)) {
pawn_call_keydown(key-48);
}
break;
case GLFW_RELEASE:

View file

@ -124,10 +124,11 @@ void openglRender(struct window *window)
//////////// 2D projection
mfloat_t projection[16] = { 0.f };
cpVect pos = cam_pos();
mat4_ortho(projection, pos.x,
window->width*zoom + pos.x,
pos.y,
window->height*zoom + pos.y, -1.f, 1.f);
mat4_ortho(projection, pos.x - zoom*window->width/2,
pos.x + zoom*window->width/2,
pos.y - zoom*window->height/2,
pos.y + zoom*window->height/2, -1.f, 1.f);
mfloat_t ui_projection[16] = { 0.f };
mat4_ortho(ui_projection, 0,

View file

@ -197,6 +197,13 @@ void callee_dbl(struct callee c, double d)
exec_callee(1);
}
void callee_int(struct callee c, int i)
{
setup_callee(c);
duk_push_int(duk, i);
exec_callee(1);
}
void callee_vec2(struct callee c, cpVect vec)
{
setup_callee(c);

View file

@ -219,6 +219,15 @@ struct glrect tex_get_rect(struct Texture *tex)
return ST_UNIT;
}
cpVect tex_get_dimensions(struct Texture *tex)
{
if (!tex) return cpvzero;
cpVect d;
d.x = tex->width;
d.y = tex->height;
return d;
}
void tex_bind(struct Texture *tex)
{
glActiveTexture(GL_TEXTURE0);

View file

@ -2,6 +2,7 @@
#define TEXTURE_H
#include "timer.h"
#include <chipmunk/chipmunk.h>
#define TEX_SPEC 0
#define TEX_NORM 1
@ -67,6 +68,7 @@ struct Texture {
struct Texture *texture_pullfromfile(const char *path); // Create texture from image
struct Texture *texture_loadfromfile(const char *path); // Create texture & load to gpu
struct Texture *str2tex(const char *path);
void tex_gpu_reload(struct Texture *tex); // gpu_free then gpu_load
void tex_gpu_free(struct Texture *tex); // Remove texture data from gpu
void tex_bind(struct Texture *tex); // Bind to gl context
@ -88,6 +90,7 @@ void anim_incr(struct anim2d *anim);
void anim_decr(struct anim2d *anim);
struct glrect tex_get_rect(struct Texture *tex);
cpVect tex_get_dimensions(struct Texture *tex);
struct glrect anim_get_rect(struct anim2d *anim);
int anim_frames(struct TexAnim *a);

View file

@ -2,15 +2,16 @@
layout (location = 0) in vec2 pos;
out vec2 apos;
uniform vec2 offset;
layout (std140) uniform Projection {
mat4 projection;
};
void main()
{
mat4 iproj = inverse(projection);
vec4 ipos = iproj * vec4(pos, 0.f, 1.f);
apos = ipos.xy;
vec4 ipos = inverse(projection) * vec4(pos, 0.f, 1.f);
apos = ipos.xy + offset;
gl_Position = vec4(pos, 0.f, 1.f);
}