diff --git a/src/rendering/twn_gl_any_rendering.c b/src/rendering/twn_gl_any_rendering.c index 03b99f7..72b9e76 100644 --- a/src/rendering/twn_gl_any_rendering.c +++ b/src/rendering/twn_gl_any_rendering.c @@ -208,7 +208,7 @@ void delete_gpu_texture(GPUTexture texture) { } -void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int width, int height) { +void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int width, int height, int xoffset, int yoffset) { glBindTexture(GL_TEXTURE_2D, texture); int format; @@ -223,6 +223,12 @@ void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int widt return; } + if (xoffset != 0 || yoffset != 0) + glPixelStorei(GL_UNPACK_ROW_LENGTH, width + xoffset * 2); + + void *start = xoffset != 0 || yoffset != 0 + ? &(((uint8_t *)pixels)[xoffset * channels + yoffset * (width + xoffset * 2) * channels]) : pixels; + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, @@ -231,7 +237,10 @@ void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int widt height, format, GL_UNSIGNED_BYTE, - pixels); + start); + + if (xoffset != 0 || yoffset != 0) + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/src/rendering/twn_gpu_texture_c.h b/src/rendering/twn_gpu_texture_c.h index 91add2e..0b9aa90 100644 --- a/src/rendering/twn_gpu_texture_c.h +++ b/src/rendering/twn_gpu_texture_c.h @@ -16,7 +16,7 @@ GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps, int c void delete_gpu_texture(GPUTexture texture); -void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int width, int height); +void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int width, int height, int xoffset, int yoffset); void bind_gpu_texture(GPUTexture texture); diff --git a/src/rendering/twn_quads.c b/src/rendering/twn_quads.c index 81b7012..8e8a7be 100644 --- a/src/rendering/twn_quads.c +++ b/src/rendering/twn_quads.c @@ -239,10 +239,15 @@ void finally_draw_space_quads_batch(const MeshBatch *batch, const Rect srcrect = textures_get_srcrect(&ctx.texture_cache, texture_key); const Rect dims = textures_get_dims(&ctx.texture_cache, texture_key); - const float wr = srcrect.w / dims.w; - const float hr = srcrect.h / dims.h; - const float xr = srcrect.x / dims.w; - const float yr = srcrect.y / dims.h; + // const float wr = srcrect.w / dims.w; + // const float hr = srcrect.h / dims.h; + // const float xr = srcrect.x / dims.w; + // const float yr = srcrect.y / dims.h; + + const float wr = 1; + const float hr = 1; + const float xr = 1; + const float yr = 1; /* update pixel-based uvs to correspond with texture atlases */ for (size_t i = 0; i < primitives_len; ++i) { @@ -293,6 +298,7 @@ void finally_draw_space_quads_batch(const MeshBatch *batch, }; command.texture_key = texture_key; + command.texture_repeat = true; command.textured = true; command.element_buffer = get_quad_element_buffer(); diff --git a/src/rendering/twn_text.c b/src/rendering/twn_text.c index 37799b4..cee245b 100644 --- a/src/rendering/twn_text.c +++ b/src/rendering/twn_text.c @@ -174,7 +174,8 @@ static FontData *text_load_font_data(const char *path, int height_px) { bitmap, 1, (int)ctx.font_texture_size, - (int)ctx.font_texture_size + (int)ctx.font_texture_size, + 0, 0 ); SDL_free(bitmap); diff --git a/src/twn_textures.c b/src/twn_textures.c index fadeb25..6540047 100644 --- a/src/twn_textures.c +++ b/src/twn_textures.c @@ -73,7 +73,7 @@ static SDL_Surface *gen_missing_texture_surface(void) { SDL_LockMutex(textures_load_mutex); if (!missing_texture_surface) { - int const dim = + TEXTURE_BORDER_REPEAT_SIZE * 2; + int const dim = 64 + TEXTURE_BORDER_REPEAT_SIZE * 2; uint8_t *data = SDL_malloc(dim * dim * 3); for (int y = 0; y < dim; ++y) { for (int x = 0; x < dim; ++x) { @@ -250,7 +250,7 @@ static void add_new_atlas(TextureCache *cache) { static void upload_texture_from_surface(GPUTexture texture, SDL_Surface *surface) { SDL_LockSurface(surface); - upload_gpu_texture(texture, surface->pixels, surface->format->BytesPerPixel, surface->w, surface->h); + upload_gpu_texture(texture, surface->pixels, surface->format->BytesPerPixel, surface->w, surface->h, 0, 0); SDL_UnlockSurface(surface); } @@ -411,8 +411,10 @@ void textures_cache_deinit(TextureCache *cache) { /* free cache hashes */ for (size_t i = 0; i < shlenu(cache->hash); ++i) { /* TODO: better to have field that stores the source of memory directly, ugh */ - if (missing_texture_surface != NULL && cache->hash[i].value.data->pixels == missing_texture_surface->pixels) + if (missing_texture_surface != NULL && cache->hash[i].value.data->pixels == missing_texture_surface->pixels) { + missing_texture_surface = NULL; SDL_free(cache->hash[i].value.data->pixels); + } else if (cache->hash[i].value.srcrect.w < 2048 && cache->hash[i].value.srcrect.h < 2048) (void)0; /* do nothing, memory owned by surface */ else @@ -706,8 +708,8 @@ void textures_bind_repeating(const TextureCache *cache, TextureKey key) { const GPUTexture repeating_texture = create_gpu_texture(TEXTURE_FILTER_NEAREAST, false, texture.data->format->BytesPerPixel, - texture.data->w, - texture.data->h); + texture.data->w - TEXTURE_BORDER_REPEAT_SIZE * 2, + texture.data->h - TEXTURE_BORDER_REPEAT_SIZE * 2); SDL_LockSurface(texture.data); @@ -715,8 +717,10 @@ void textures_bind_repeating(const TextureCache *cache, TextureKey key) { upload_gpu_texture(repeating_texture, texture.data->pixels, texture.data->format->BytesPerPixel, - texture.data->w, - texture.data->h); + texture.data->w - TEXTURE_BORDER_REPEAT_SIZE * 2, + texture.data->h - TEXTURE_BORDER_REPEAT_SIZE * 2, + TEXTURE_BORDER_REPEAT_SIZE, + TEXTURE_BORDER_REPEAT_SIZE); SDL_UnlockSurface(texture.data);