Draw sprite to GUI

This commit is contained in:
John Alanbrook 2022-12-24 19:18:06 +00:00
parent 06b8bba27f
commit 147cf1b05d
12 changed files with 99 additions and 61 deletions

View file

@ -64,7 +64,7 @@ eobjects != $(call rm,$(eobjects),sqlite pl_mpeg_extract_frames pl_mpeg_player y
engincs != find source/engine -maxdepth 1 -type d engincs != find source/engine -maxdepth 1 -type d
includeflag != find source -type d -name include includeflag != find source -type d -name include
includeflag += engincs includeflag += $(engincs) source/engine/thirdparty/Nuklear
includeflag := $(addprefix -I, $(includeflag)) includeflag := $(addprefix -I, $(includeflag))
WARNING_FLAGS = -Wall# -pedantic -Wextra -Wwrite-strings -Wno-incompatible-function-pointer-types -Wno-incompatible-pointer-types -Wno-unused-function WARNING_FLAGS = -Wall# -pedantic -Wextra -Wwrite-strings -Wno-incompatible-function-pointer-types -Wno-incompatible-pointer-types -Wno-unused-function

View file

@ -183,11 +183,9 @@ 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] + campos.x; cursor[0] = pos[0];
cursor[1] = pos[1] + campos.y; cursor[1] = pos[1];
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO); glBindVertexArray(VAO);

View file

@ -13,6 +13,7 @@
#include "gameobject.h" #include "gameobject.h"
#include "openglrender.h" #include "openglrender.h"
#include "2dphysics.h" #include "2dphysics.h"
#include "sprite.h"
#include "yugine.h" #include "yugine.h"
@ -70,6 +71,16 @@ s7_pointer s7_gui_text(s7_scheme *sc, s7_pointer args) {
return s7_car(args); return s7_car(args);
} }
s7_pointer s7_gui_img(s7_scheme *sc, s7_pointer args) {
const char *img = s7_string(s7_car(args));
float x = s7_real(s7_cadr(args));
float y = s7_real(s7_caddr(args));
gui_draw_img(img, x, y);
return args;
}
s7_pointer s7_settings_cmd(s7_scheme *sc, s7_pointer args) { s7_pointer s7_settings_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args)); int cmd = s7_integer(s7_car(args));
double val = s7_real(s7_cadr(args)); double val = s7_real(s7_cadr(args));
@ -132,18 +143,32 @@ s7_pointer s7_win_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_cadr(args)); int cmd = s7_integer(s7_cadr(args));
struct window *w = window_i(win); struct window *w = window_i(win);
/*
3: return win width
4: return win height
*/
switch (cmd) { switch (cmd) {
case 0: // toggle fullscreen case 0: /* toggle fullscreen */
window_togglefullscreen(w); window_togglefullscreen(w);
break; break;
case 1: // Fullscreen on case 1: /* Fullscreen on */
window_makefullscreen(w); window_makefullscreen(w);
break; break;
case 2: // Fullscreen off case 2: /* Fullscreen off */
window_unfullscreen(w); window_unfullscreen(w);
break; break;
case 3:
return s7_make_integer(sc, w->width);
break;
case 4:
return s7_make_integer(sc, w->height);
break;
} }
return args; return args;
@ -365,6 +390,7 @@ void ffi_load() {
S7_FUNC(ui_rendertext, 3); S7_FUNC(ui_rendertext, 3);
S7_FUNC(gui_text, 4); S7_FUNC(gui_text, 4);
S7_FUNC(gui_img, 3);
S7_FUNC(gen_cmd, 2); S7_FUNC(gen_cmd, 2);
S7_FUNC(sys_cmd, 1); S7_FUNC(sys_cmd, 1);

View file

@ -123,9 +123,6 @@ void add_zoom(float val) { zoom = val; }
void openglRender(struct window *window) void openglRender(struct window *window)
{ {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.3f, 0.3f, 0.3f, 1.f);
//////////// 2D projection //////////// 2D projection
mfloat_t projection[16] = { 0.f }; mfloat_t projection[16] = { 0.f };
@ -135,31 +132,36 @@ void openglRender(struct window *window)
pos.y, pos.y,
window->height*zoom + pos.y, -1.f, 1.f); window->height*zoom + pos.y, -1.f, 1.f);
glBindBuffer(GL_UNIFORM_BUFFER, projUBO); mfloat_t ui_projection[16] = { 0.f };
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection); mat4_ortho(ui_projection, 0,
window->width,
//shader_setmat4(vid_shader, "projection", projection); 0,
glEnable(GL_DEPTH_TEST); window->height, -1.f, 1.f);
// Clear color and depth // Clear color and depth
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 glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
call_gui(); glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection);
//// DEBUG
if (debugDrawPhysics)
gameobject_draw_debugs();
glEnable(GL_DEPTH_TEST);
///// Sprites ///// Sprites
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
shader_use(spriteShader); shader_use(spriteShader);
sprite_draw_all(); sprite_draw_all();
glDepthFunc(GL_ALWAYS);
shader_use(textShader);
shader_setmat4(textShader, "projection", projection);
glDisable(GL_DEPTH_TEST);
//// DEBUG
if (debugDrawPhysics)
gameobject_draw_debugs();
////// TEXT && GUI
glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, ui_projection);
call_gui();
} }
void BindUniformBlock(GLuint shaderID, const char *bufferName, GLuint bufferBind) void BindUniformBlock(GLuint shaderID, const char *bufferName, GLuint bufferBind)

View file

@ -133,13 +133,7 @@ void sprite_initialize()
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
} }
void sprite_draw(struct sprite *sprite) void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], float offset[2]) {
{
if (sprite->tex != NULL) {
//shader_use(spriteShader);
mfloat_t model[16] = { 0.f }; mfloat_t model[16] = { 0.f };
mfloat_t r_model[16] = { 0.f }; mfloat_t r_model[16] = { 0.f };
mfloat_t s_model[16] = { 0.f }; mfloat_t s_model[16] = { 0.f };
@ -147,39 +141,49 @@ void sprite_draw(struct sprite *sprite)
memcpy(r_model, UNITMAT4, sizeof(UNITMAT4)); memcpy(r_model, UNITMAT4, sizeof(UNITMAT4));
memcpy(s_model, UNITMAT4, sizeof(UNITMAT4)); memcpy(s_model, UNITMAT4, sizeof(UNITMAT4));
mfloat_t t_scale[2] = { size[0] * tex->width, size[1] * tex->height };
mfloat_t t_offset[2] = { offset[0] * t_scale[0], offset[1] * t_scale[1] };
mat4_translate_vec2(model, t_offset);
mfloat_t t_move[2] = { 0.f };
mfloat_t t_scale[2] = { 0.f };
t_scale[0] = sprite->size[0] * sprite->tex->width * sprite->go->scale;
t_scale[1] = sprite->size[1] * sprite->tex->height * sprite->go->scale;
t_move[0] = sprite->pos[0] * t_scale[0];
t_move[1] = sprite->pos[1] * t_scale[1];
mat4_translate_vec2(model, t_move);
mat4_scale_vec2(model, t_scale); mat4_scale_vec2(model, t_scale);
mat4_rotation_z(r_model, cpBodyGetAngle(sprite->go->body));
mat4_rotation_z(r_model, angle);
mat4_multiply(model, r_model, model); mat4_multiply(model, r_model, model);
cpVect pos = cpBodyGetPosition(sprite->go->body); mat4_translate_vec2(model, pos);
t_move[0] = pos.x;
t_move[1] = pos.y;
mat4_translate_vec2(model, t_move);
float white[3] = { 1.f, 1.f, 1.f };
shader_setmat4(spriteShader, "model", model); shader_setmat4(spriteShader, "model", model);
shader_setvec3(spriteShader, "spriteColor", sprite->color); shader_setvec3(spriteShader, "spriteColor", white);
//tex_bind(sprite->tex); //tex_bind(sprite->tex);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sprite->tex->id); glBindTexture(GL_TEXTURE_2D, tex->id);
glBindVertexArray(quadVAO); glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
} }
void sprite_draw(struct sprite *sprite)
{
if (sprite->tex) {
cpVect cpos = cpBodyGetPosition(sprite->go->body);
float pos[2] = {cpos.x, cpos.y};
float size[2] = { sprite->size[0] * sprite->go->scale, sprite->size[1] * sprite->go->scale };
tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos);
}
}
void gui_draw_img(const char *img, float x, float y) {
shader_use(spriteShader);
struct Texture *tex = texture_loadfromfile(img);
float pos[2] = {x, y};
float size[2] = {1.f, 1.f};
float offset[2] = { 0.f, 0.f };
tex_draw(tex, pos, 0.f, size, offset);
} }
void spriteanim_draw(struct sprite *sprite) void spriteanim_draw(struct sprite *sprite)

View file

@ -46,4 +46,7 @@ void sprite_draw_all();
unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite); unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite);
void gui_draw_img(const char *img, float x, float y);
#endif #endif

View file

@ -29,7 +29,7 @@ struct Texture *texture_pullfromfile(const char *path)
tex->anim.ms = 1; tex->anim.ms = 1;
int n; int n;
stbi_set_flip_vertically_on_load(0); stbi_set_flip_vertically_on_load(1);
unsigned char *data = stbi_load(path, &tex->width, &tex->height, &n, 4); unsigned char *data = stbi_load(path, &tex->width, &tex->height, &n, 4);
while (data == NULL) { while (data == NULL) {
@ -250,3 +250,5 @@ void anim_bkwd(struct TexAnimation *anim)
{ {
anim_decr(anim); anim_decr(anim);
} }

View file

@ -76,4 +76,6 @@ void tex_anim_calc_uv(struct TexAnimation *anim);
void tex_anim_set(struct TexAnimation *anim); void tex_anim_set(struct TexAnimation *anim);
#endif #endif

View file

@ -154,9 +154,6 @@ void window_handle_event(struct window *w)
"Changed size of window %d: width %d, height %d.", "Changed size of window %d: width %d, height %d.",
w->id, w->width, w->height); w->id, w->width, w->height);
window_makecurrent(w); window_makecurrent(w);
w.projection =
glm::ortho(0.f, (float) width, 0.f, (float) height, -1.f,
1.f);
w->render = true; w->render = true;
break; break;

View file

@ -18,7 +18,6 @@ struct window {
bool minimized; bool minimized;
bool iconified; bool iconified;
bool shown; bool shown;
float projection[16];
void (*nuke_gui)(); void (*nuke_gui)();
}; };

View file

@ -40,10 +40,11 @@
(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 (zoom! amt) (settings_cmd 5 amt))
(define (win_fulltoggle w) (win_cmd w 0)) (define (win_fulltoggle) (win_cmd 0 0))
(define (win_fullscreen w) (win_cmd w 1)) (define (win_fullscreen) (win_cmd 0 1))
(define (win_unfullscreen w) (win_cmd w 2)) (define (win_unfullscreen) (win_cmd 0 2))
(define (win_title s) (win_cmd 0 3 s)) (define (win_w) (win_cmd 0 3))
(define (win_h) (win_cmd 0 4))
(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))
@ -71,7 +72,8 @@
(register 0 ,f)))) (register 0 ,f))))
(define-macro (gui . expr) (define-macro (gui . expr)
`(registertype gui ,@expr)) `(registertype gui
(let ((x 0) (y 0)) ,@expr)))
(define-macro (while condition . body) (define-macro (while condition . body)
(let ((loop (gensym))) (let ((loop (gensym)))

View file

@ -2,7 +2,10 @@
layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex> layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex>
out vec2 TexCoords; out vec2 TexCoords;
uniform mat4 projection; layout (std140) uniform Projection
{
mat4 projection;
};
void main() void main()
{ {