From d69c8e3a5e131797ed3f761bfed321f375172994 Mon Sep 17 00:00:00 2001 From: zlago <104219492+zlago@users.noreply.github.com> Date: Mon, 17 Nov 2025 20:56:24 +0100 Subject: [PATCH] a shallow game will never be good, it takes depth --- src/assets.c++ | 2 +- src/batch.c | 19 ++++++++++--------- src/batch.h | 6 +++--- src/main.c++ | 6 ++++-- src/main.h++ | 2 +- src/spr2D.glsl | 4 ++-- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/assets.c++ b/src/assets.c++ index 904c9bd..32764f4 100644 --- a/src/assets.c++ +++ b/src/assets.c++ @@ -9,7 +9,7 @@ #include "rect-pack.h" #include "batch.h" -#include +#include "glad/gles2.h" #include "main.h++" diff --git a/src/batch.c b/src/batch.c index 2c0d7c3..7e62e15 100644 --- a/src/batch.c +++ b/src/batch.c @@ -3,13 +3,14 @@ #include #include #include -#include +#include +#include "glad/gles2.h" #include "batch.h" struct batch { - struct vertices { // 8 bytes - int16_t x, y; // 4 bytes + struct vertices { // 10 bytes + int16_t x, y, z; // 6 bytes uint16_t s, t; // 4 bytes // must subtract 1 after multiplying } (*vertices)[4]; @@ -38,17 +39,17 @@ struct batch { static void batch_insert(struct vertices v[4], GLuint texture); -void batch_blit_flippable(int x, int y, struct texture *tex, struct crop *crop) { +void batch_blit_flippable(int x, int y, int z, struct texture *tex, struct crop *crop) { if (crop->w < 0) { x -= crop->w; } if (crop->h < 0) { y -= crop->h; } - batch_blit(x, y, tex, crop); + batch_blit(x, y, z, tex, crop); } -void batch_blit(int x, int y, struct texture *tex, struct crop *crop) { +void batch_blit(int x, int y, int z, struct texture *tex, struct crop *crop) { unsigned s1, s2, t1, t2; if (tex->flipped) { s1 = tex->x + crop->y; @@ -63,7 +64,7 @@ void batch_blit(int x, int y, struct texture *tex, struct crop *crop) { } struct vertices vi = { - .x = x, .y = y, + .x = x, .y = y, .z = z, .s = s1 * tex->size, .t = t1 * tex->size, }; @@ -126,8 +127,8 @@ void batch_flush(void) { glUseProgram(batch.program); - glVertexAttribPointer(batch.aVertCoord, 2, GL_SHORT, GL_FALSE, sizeof (struct vertices), (void *) 0); - glVertexAttribPointer(batch.aTexCoord, 2, GL_UNSIGNED_SHORT, GL_TRUE, sizeof (struct vertices), (void *) 4); + glVertexAttribPointer(batch.aVertCoord, 3, GL_SHORT, GL_FALSE, sizeof (struct vertices), (void *) offsetof (struct vertices, x)); + glVertexAttribPointer(batch.aTexCoord, 2, GL_UNSIGNED_SHORT, GL_TRUE, sizeof (struct vertices), (void *) offsetof (struct vertices, s)); glEnableVertexAttribArray(batch.aVertCoord); glEnableVertexAttribArray(batch.aTexCoord); diff --git a/src/batch.h b/src/batch.h index 475fe42..1fcc44e 100644 --- a/src/batch.h +++ b/src/batch.h @@ -7,7 +7,7 @@ extern "C" { #include #include #include -#include +#include "glad/gles2.h" struct texture { GLuint texture; @@ -21,8 +21,8 @@ struct crop { int w, h; }; -void batch_blit(int x, int y, struct texture *tex, struct crop *crop); -void batch_blit_flippable(int x, int y, struct texture *tex, struct crop *crop); +void batch_blit(int x, int y, int z, struct texture *tex, struct crop *crop); +void batch_blit_flippable(int x, int y, int z, struct texture *tex, struct crop *crop); void batch_flush(void); diff --git a/src/main.c++ b/src/main.c++ index e7d1806..eb669a5 100644 --- a/src/main.c++ +++ b/src/main.c++ @@ -1,4 +1,4 @@ -#include +#include "glad/gles2.h" #include #define SDL_MAIN_USE_CALLBACKS 1 #include @@ -157,9 +157,10 @@ SDL_AppResult SDL_AppIterate(void *) { glViewport(0, 0, WINDOW_VIRTUAL, WINDOW_VIRTUAL); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_DEPTH_TEST); struct crop c = {0, 0, 64, 64}; - batch_blit(-32, -32, &textures.at("turret"), &c); + batch_blit(-32, -32, 0, &textures.at("turret"), &c); batch_flush(); glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -185,6 +186,7 @@ SDL_AppResult SDL_AppIterate(void *) { } glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); + glDisable(GL_DEPTH_TEST); glBindBuffer(GL_ARRAY_BUFFER, fb.vbo); glUseProgram(fb.program); diff --git a/src/main.h++ b/src/main.h++ index dbfd4d0..fbc4862 100644 --- a/src/main.h++ +++ b/src/main.h++ @@ -1,6 +1,6 @@ #pragma once -#include +#include "glad/gles2.h" #include #include diff --git a/src/spr2D.glsl b/src/spr2D.glsl index 33dafcd..fa3176b 100644 --- a/src/spr2D.glsl +++ b/src/spr2D.glsl @@ -10,12 +10,12 @@ varying vec2 vTexCoord; %% -attribute vec2 aVertCoord; +attribute vec3 aVertCoord; attribute vec2 aTexCoord; void main(void) { vTexCoord = aTexCoord; - gl_Position = vec4(aVertCoord * vec2(1.0 / 64.0), 0.0, 1.0); + gl_Position = vec4(aVertCoord * (1.0 / 64.0), 1.0); } ##