aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-12-16 03:23:01 +0100
committerkdx <kikoodx@paranoici.org>2023-12-16 03:23:03 +0100
commit221a7d0618b4f8b6af19b089f49818806fe1e1b3 (patch)
tree688211a87d03ba3a1acbb527b0f434aa3533aa45
parent1546f59de4e15fd6653cce36b8719c1c9c0fdd8d (diff)
downloadtzr-221a7d0618b4f8b6af19b089f49818806fe1e1b3.tar.gz
resizable viewport
-rw-r--r--headers/TZR.h3
-rw-r--r--main.c2
-rw-r--r--sources/TZR_Init.c24
-rw-r--r--sources/TZR_SetViewportSize.c40
4 files changed, 48 insertions, 21 deletions
diff --git a/headers/TZR.h b/headers/TZR.h
index fab1c55..2d9e7d4 100644
--- a/headers/TZR.h
+++ b/headers/TZR.h
@@ -50,3 +50,6 @@ int TZR_MainLoop(int (*main_loop)(void *udata), void *udata);
unsigned long TZR_GetTick(void);
void TZR_ToggleFullscreen(void);
+
+/* NEVER call this during draw */
+int TZR_SetViewportSize(int width, int height);
diff --git a/main.c b/main.c
index 9bd302b..8d06221 100644
--- a/main.c
+++ b/main.c
@@ -60,6 +60,8 @@ main_loop(void *udata)
if (TZR_IsKeyPressed(SDL_SCANCODE_SPACE))
TZR_PlaySound(id2);
+ TZR_SetViewportSize(256 + x, 256 + y);
+
if (TZR_DrawBegin()
|| TZR_DrawSetColor(0.0f, 0.0f, 0.0f)
|| TZR_DrawClear()
diff --git a/sources/TZR_Init.c b/sources/TZR_Init.c
index 5c9241b..ea0eaba 100644
--- a/sources/TZR_Init.c
+++ b/sources/TZR_Init.c
@@ -63,27 +63,9 @@ _TZR_Init(const TZR_Config *config)
if (___tzr_renderer == NULL)
return _sdl_error();
- if (___tzr_config.hd_render) {
- if (SDL_RenderSetLogicalSize(___tzr_renderer,
- ___tzr_config.width,
- ___tzr_config.height) < 0)
- return _sdl_error();
- } else {
- ___tzr_target = SDL_CreateTexture(___tzr_renderer,
- SDL_PIXELFORMAT_RGB888,
- SDL_TEXTUREACCESS_TARGET,
- ___tzr_config.width,
- ___tzr_config.height);
- if (___tzr_target == NULL)
- return _sdl_error();
- if (___tzr_config.interlace) {
- ___tzr_target_pre = SDL_CreateTexture(
- ___tzr_renderer,
- SDL_PIXELFORMAT_RGB888,
- SDL_TEXTUREACCESS_TARGET,
- ___tzr_config.width,
- ___tzr_config.height);
- }
+ if (TZR_SetViewportSize(___tzr_config.width, ___tzr_config.height)) {
+ TZR_Quit();
+ return -1;
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,
diff --git a/sources/TZR_SetViewportSize.c b/sources/TZR_SetViewportSize.c
new file mode 100644
index 0000000..fee70db
--- /dev/null
+++ b/sources/TZR_SetViewportSize.c
@@ -0,0 +1,40 @@
+#include "TZR.h"
+#include "TZR_globals.h"
+#include "sdl_error.h"
+
+int
+TZR_SetViewportSize(int width, int height)
+{
+ const int pw = ___tzr_config.width;
+ const int ph = ___tzr_config.height;
+ ___tzr_config.width = width;
+ ___tzr_config.height = height;
+ if (___tzr_config.hd_render) {
+ if (SDL_RenderSetLogicalSize(___tzr_renderer,
+ ___tzr_config.width,
+ ___tzr_config.height) < 0)
+ return sdl_error(-1);
+ } else {
+ if (___tzr_target != NULL && pw == width && ph == height)
+ return 0;
+ if (___tzr_target != NULL)
+ SDL_DestroyTexture(___tzr_target);
+ ___tzr_target = SDL_CreateTexture(___tzr_renderer,
+ SDL_PIXELFORMAT_RGB888,
+ SDL_TEXTUREACCESS_TARGET,
+ ___tzr_config.width,
+ ___tzr_config.height);
+ if (___tzr_target == NULL)
+ return sdl_error(-1);
+ if (___tzr_config.interlace) {
+ ___tzr_target_pre = SDL_CreateTexture(
+ ___tzr_renderer,
+ SDL_PIXELFORMAT_RGB888,
+ SDL_TEXTUREACCESS_TARGET,
+ ___tzr_config.width,
+ ___tzr_config.height);
+ }
+ }
+ return 0;
+}
+