From e1dc054c0c558f403fabec7e2bddfa15c97bb896 Mon Sep 17 00:00:00 2001 From: kdx Date: Wed, 14 Feb 2024 16:33:16 +0100 Subject: gl mipmaps --- headers/TZR.h | 1 + headers/TZR_globals.h | 1 + headers/TZR_types.h | 1 + sources/TZR_DirectResourceLoad.c | 17 +++++++++++++++-- sources/TZR_Init.c | 41 +++++++++++++++++++++++++++++++++++++--- sources/TZR_Quit.c | 4 ++++ sources/globals.c | 1 + 7 files changed, 61 insertions(+), 5 deletions(-) diff --git a/headers/TZR.h b/headers/TZR.h index 2d9e7d4..3c6fbc4 100644 --- a/headers/TZR.h +++ b/headers/TZR.h @@ -28,6 +28,7 @@ .mixer=TZR_MIXER_ON, \ .basepath=NULL, \ .title="TZR", \ + .mipmaps=true, \ ._=0, __VA_ARGS__ }; \ _TZR_Init(&____arg); }) diff --git a/headers/TZR_globals.h b/headers/TZR_globals.h index 6c4bd04..bb4da02 100644 --- a/headers/TZR_globals.h +++ b/headers/TZR_globals.h @@ -32,6 +32,7 @@ extern size_t ___tzr_joysticks_size; extern SDL_BlendMode ___tzr_blendmode; extern int ___tzr_camera_x; extern int ___tzr_camera_y; +extern SDL_GLContext ___tzr_gl_ctx; #ifdef TZR_SOLOUD extern Soloud ___tzr_soloud; diff --git a/headers/TZR_types.h b/headers/TZR_types.h index 06d9d41..012fa71 100644 --- a/headers/TZR_types.h +++ b/headers/TZR_types.h @@ -65,6 +65,7 @@ struct TZR_Config { unsigned int mixer; const char *basepath; const char *title; + bool mipmaps; }; struct TZR_Point { diff --git a/sources/TZR_DirectResourceLoad.c b/sources/TZR_DirectResourceLoad.c index 16f0f64..4b010e1 100644 --- a/sources/TZR_DirectResourceLoad.c +++ b/sources/TZR_DirectResourceLoad.c @@ -1,6 +1,7 @@ #include "TZR_resource.h" #include "TZR_globals.h" #include "sdl_error.h" +#include #include #include #include @@ -79,12 +80,24 @@ TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size) SDL_Surface *const surf = SDL_LoadBMP_RW(rw, 1); if (surf == NULL) return sdl_error(-1); -#endif +#endif /* TZR_STB_IMAGE */ SDL_Texture *const tex = SDL_CreateTextureFromSurface(___tzr_renderer, surf); SDL_FreeSurface(surf); if (tex == NULL) return sdl_error(-1); +#ifdef TZR_OPENGL + if (___tzr_config.mipmaps) { + TZR_Log("generating mipmaps"); + SDL_GL_BindTexture(tex, NULL, NULL); + glGenerateMipmap(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + GL_LINEAR_MIPMAP_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, + -.5f); + SDL_GL_UnbindTexture(tex); + } +#endif /* TZR_OPENGL */ if (SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND) < 0) { SDL_DestroyTexture(tex); return sdl_error(-1); @@ -113,7 +126,7 @@ TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size) return -1; } res->sound.ptr = wav; -#else +#else /* TZR_SOLOUD */ SDL_RWops *const rw = SDL_RWFromConstMem(data, size); if (rw == NULL) return sdl_error(-1); diff --git a/sources/TZR_Init.c b/sources/TZR_Init.c index c6e3f7e..586b2cf 100644 --- a/sources/TZR_Init.c +++ b/sources/TZR_Init.c @@ -2,6 +2,7 @@ #include "TZR_globals.h" #include "sdl_error.h" #include +#include #include #include #include @@ -48,17 +49,51 @@ _TZR_Init(const TZR_Config *config) ___tzr_config.width * ___tzr_config.ratio * ___tzr_config.scale, ___tzr_config.height * ___tzr_config.scale, - SDL_WINDOW_RESIZABLE); + SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL); + if (___tzr_window == NULL) { + ___tzr_window = + SDL_CreateWindow(config->title, + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + ___tzr_config.width * ___tzr_config.ratio + * ___tzr_config.scale, + ___tzr_config.height * ___tzr_config.scale, + SDL_WINDOW_RESIZABLE ); + } if (___tzr_window == NULL) return _sdl_error(); +#ifdef TZR_OPENGL + ___tzr_gl_ctx = SDL_GL_CreateContext(___tzr_window); + if (___tzr_gl_ctx == NULL) { + TZR_Log("%s", SDL_GetError()); + ___tzr_config.mipmaps = false; + } else if (___tzr_config.mipmaps) do { + const char *gl_version = (const char *)glGetString(GL_VERSION); + if (gl_version == NULL || strlen(gl_version) < 3) { + ___tzr_config.mipmaps = false; + break; + } + const int major = atoi(gl_version); + const int minor = atoi(gl_version + 2); + if (major < 3) + ___tzr_config.mipmaps = false; + } while (0); +#endif /* TZR_OPENGL */ + #ifdef __EMSCRIPTEN__ + /* TODO: verify than this is unneeded */ ___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1, SDL_RENDERER_SOFTWARE); #else ___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1, SDL_RENDERER_ACCELERATED); -#endif + if (___tzr_renderer == NULL) { + TZR_Log("%s", SDL_GetError()); + ___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1, + SDL_RENDERER_SOFTWARE); + } +#endif /* __EMSCRIPTEN__ */ if (___tzr_renderer == NULL) return _sdl_error(); @@ -107,7 +142,7 @@ _TZR_Init(const TZR_Config *config) ___tzr_config.mixer = false; break; } -#endif +#endif /* TZR_SOLOUD */ } while (0); return 0; diff --git a/sources/TZR_Quit.c b/sources/TZR_Quit.c index 6925ac5..092e817 100644 --- a/sources/TZR_Quit.c +++ b/sources/TZR_Quit.c @@ -34,6 +34,10 @@ TZR_Quit(void) SDL_DestroyRenderer(___tzr_renderer); ___tzr_renderer = NULL; } + if (___tzr_gl_ctx != NULL) { + SDL_GL_DeleteContext(___tzr_gl_ctx); + ___tzr_gl_ctx = NULL; + } if (___tzr_window != NULL) { SDL_DestroyWindow(___tzr_window); ___tzr_window = NULL; diff --git a/sources/globals.c b/sources/globals.c index 3286499..a8e1383 100644 --- a/sources/globals.c +++ b/sources/globals.c @@ -32,6 +32,7 @@ size_t ___tzr_joysticks_size = 0; SDL_BlendMode ___tzr_blendmode = SDL_BLENDMODE_BLEND; int ___tzr_camera_x = 0; int ___tzr_camera_y = 0; +SDL_GLContext ___tzr_gl_ctx = NULL; #ifdef TZR_SOLOUD Soloud ___tzr_soloud = NULL; -- cgit v1.2.3