a-game

2D platformer written from scratch.
Log | Files | Refs | README | LICENSE

commit a01886f8d79f123cd140c0aaf00be5b77b1d19d0
parent eac9fbb97e04eaad748ea858c21e894abb1defe4
Author: Amin Mesbah <dev@aminmesbah.com>
Date:   Sat, 20 Jul 2019 19:21:42 -0700

Don't use glTexStorage3D to allocate space

We want to require OpenGL 3.3, not 4.2.

Diffstat:
Msrc/am_math.h | 12++++++++++++
Msrc/render.c | 11++++++++++-
2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/am_math.h b/src/am_math.h @@ -17,6 +17,12 @@ internal inline f32 math_max(f32 a, f32 b) return max; } +internal inline u32 math_pow_of_2(u32 exponent) +{ + u32 result = 1 << exponent; + return result; +} + internal inline i32 math_round(f32 n) { return n < 0.0f ? n - 0.5 : n + 0.5; @@ -210,6 +216,12 @@ internal inline bool math_v2i_eq(v2i vec1, v2i vec2) return result; } +internal inline bool math_v2u_eq(v2u vec1, v2u vec2) +{ + bool result = (vec1.x == vec2.x && vec1.y == vec2.y); + return result; +} + internal inline v3 math_v3_cross(v3 vec1, v3 vec2) { // cross product, a.k.a. outer product or vector product diff --git a/src/render.c b/src/render.c @@ -58,7 +58,16 @@ internal void renderer_init(struct RendererState *renderer, struct Image *images glGenTextures(1, &texture_id); glBindTexture(GL_TEXTURE_2D_ARRAY, texture_id); u32 mip_map_levels = 4; - glTexStorage3D(GL_TEXTURE_2D_ARRAY, mip_map_levels, GL_RGBA8, tile_dim.x, tile_dim.y, tileset_dim.x * tileset_dim.y); + // NOTE(amin): This allocates space for the 2D array texture. If we + // required OpenGL 4.2, we could instead use glTexStorage3D and no + // loop: + // glTexStorage3D(GL_TEXTURE_2D_ARRAY, mip_map_levels, GL_RGBA8, tile_dim.x, tile_dim.y, tileset_dim.x * tileset_dim.y); + for (u32 level = 0; level < mip_map_levels; level++) + { + v2u mip_dim = {tile_dim.x / math_pow_of_2(level), tile_dim.y / math_pow_of_2(level)}; + assert(!math_v2u_eq(mip_dim, (v2u) {0, 0})); + glTexImage3D(GL_TEXTURE_2D_ARRAY, level, GL_RGBA8, mip_dim.x, mip_dim.y, tileset_dim.x * tileset_dim.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + } size_t marker = mem_st_get_marker(allocator); {