opengl moment #1

Merged
veclavtalica merged 27 commits from opengl into main 2024-07-28 14:50:35 +00:00
4 changed files with 24 additions and 10 deletions
Showing only changes of commit 910e45a44b - Show all commits

View File

@ -11,6 +11,21 @@ static void ingame_tick(struct state *state) {
world_drawdef(scn->world); world_drawdef(scn->world);
player_calc(scn->player); player_calc(scn->player);
push_sprite_ex((t_frect){ .x = 32, .y = 64, .w = 64, .h = 64 }, (t_push_sprite_args){
.path = "/assets/light.png",
.color = (t_color){255, 0, 0, 255},
.blend = true });
push_sprite_ex((t_frect){ .x = 48, .y = 64, .w = 64, .h = 64 }, (t_push_sprite_args){
.path = "/assets/light.png",
.color = (t_color){0, 255, 0, 255},
.blend = true });
push_sprite_ex((t_frect){ .x = 64, .y = 64, .w = 64, .h = 64 }, (t_push_sprite_args){
.path = "/assets/light.png",
.color = (t_color){0, 0, 255, 255},
.blend = true });
push_sprite_ex((t_frect){ .x = 32, .y = 32, .w = 64, .h = 64 }, (t_push_sprite_args){ push_sprite_ex((t_frect){ .x = 32, .y = 32, .w = 64, .h = 64 }, (t_push_sprite_args){
.path = "/assets/player/baron-walk.png", .path = "/assets/player/baron-walk.png",
.color = (t_color){255, 255, 255, 255}, .color = (t_color){255, 255, 255, 255},

View File

@ -71,7 +71,8 @@ static void render_2d(void) {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
glDepthFunc(GL_LESS); glDisable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);
const size_t render_queue_len = arrlenu(ctx.render_queue_2d); const size_t render_queue_len = arrlenu(ctx.render_queue_2d);
@ -85,21 +86,17 @@ static void render_2d(void) {
const struct sprite_batch batch = const struct sprite_batch batch =
collect_sprite_batch(current, render_queue_len - i); collect_sprite_batch(current, render_queue_len - i);
glDepthRange((double)batch_count / UINT16_MAX, 1.0);
if (batch.blend) { if (batch.blend) {
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS); glDepthFunc(GL_ALWAYS);
glDepthRange((double)batch_count / UINT16_MAX, 1.0);
glDisable(GL_ALPHA_TEST);
render_sprites(current, batch.size, false); render_sprites(current, batch.size, false);
} else { } else {
glDisable(GL_BLEND); glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
glDepthRange((double)batch_count / UINT16_MAX, 1.0);
glDisable(GL_ALPHA_TEST);
render_sprites(current, batch.size, true); render_sprites(current, batch.size, true);
} }

View File

@ -115,8 +115,6 @@ static void render_sprites(const struct primitive_2d primitives[],
NULL, NULL,
GL_STREAM_DRAW); GL_STREAM_DRAW);
const t_rect srcrect =
textures_get_srcrect(&ctx.texture_cache, primitives->sprite.texture_key);
const t_rect dims = const t_rect dims =
textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key); textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key);
@ -130,6 +128,9 @@ static void render_sprites(const struct primitive_2d primitives[],
const size_t cur = reversed ? len - i - 1: i; const size_t cur = reversed ? len - i - 1: i;
const struct sprite_primitive sprite = primitives[cur].sprite; const struct sprite_primitive sprite = primitives[cur].sprite;
const t_rect srcrect =
textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key);
const float wr = (float)srcrect.w / (float)dims.w; const float wr = (float)srcrect.w / (float)dims.w;
const float hr = (float)srcrect.h / (float)dims.h; const float hr = (float)srcrect.h / (float)dims.h;
const float xr = (float)srcrect.x / (float)dims.w; const float xr = (float)srcrect.x / (float)dims.w;

View File

@ -75,6 +75,7 @@ static void add_new_atlas(struct texture_cache *cache) {
a_mask); a_mask);
SDL_FreeFormat(native_format); SDL_FreeFormat(native_format);
SDL_SetSurfaceBlendMode(new_atlas, SDL_BLENDMODE_NONE);
SDL_SetSurfaceRLE(new_atlas, true); SDL_SetSurfaceRLE(new_atlas, true);
arrput(cache->atlas_surfaces, new_atlas); arrput(cache->atlas_surfaces, new_atlas);
arrput(cache->atlas_textures, new_gl_texture()); arrput(cache->atlas_textures, new_gl_texture());
@ -108,7 +109,7 @@ static void upload_texture_from_surface(GLuint texture, SDL_Surface *surface) {
glTexImage2D(GL_TEXTURE_2D, glTexImage2D(GL_TEXTURE_2D,
0, 0,
GL_RGBA, GL_RGBA8,
surface->w, surface->w,
surface->h, surface->h,
0, 0,