aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKikooDX <kikoodx@paranoici.org>2022-02-28 22:15:00 +0100
committerKikooDX <kikoodx@paranoici.org>2022-02-28 22:15:00 +0100
commit9793cc16677226372d4b0b37dd11a35a73d860fb (patch)
treee04618844f2ebbffa2230e59eed0c1e53ce594b7
parent426a48adb9c94a9246f41135a24df3578eb218de (diff)
downloadlzy-9793cc16677226372d4b0b37dd11a35a73d860fb.tar.gz
scale sdl window
-rw-r--r--inc/lzy.h72
1 files changed, 69 insertions, 3 deletions
diff --git a/inc/lzy.h b/inc/lzy.h
index cfbef1c..1ec1fc9 100644
--- a/inc/lzy.h
+++ b/inc/lzy.h
@@ -160,6 +160,7 @@ const char *LZY_GetError(void) {
static const char *error = NULL;
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
+static SDL_Texture *target = NULL;
static int should_quit = 0;
static int input[LZYK_COUNT] = {0};
static int fps_limiter = 0;
@@ -176,7 +177,7 @@ int LZY_Init(const char *title, int target_fps) {
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, LZY_DISPLAY_WIDTH,
- LZY_DISPLAY_HEIGHT, SDL_WINDOW_SHOWN);
+ LZY_DISPLAY_HEIGHT, SDL_WINDOW_RESIZABLE);
if (window == NULL) {
error = SDL_GetError();
return -2;
@@ -190,6 +191,14 @@ int LZY_Init(const char *title, int target_fps) {
return -3;
}
+ target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888,
+ SDL_TEXTUREACCESS_TARGET, LZY_DISPLAY_WIDTH,
+ LZY_DISPLAY_HEIGHT);
+ if (target == NULL) {
+ error = SDL_GetError();
+ return -4;
+ }
+
fps_limiter = target_fps > 0;
if (fps_limiter) {
min_dt = 1000 / target_fps;
@@ -200,6 +209,11 @@ int LZY_Init(const char *title, int target_fps) {
}
void LZY_Quit(void) {
+ if (target != NULL) {
+ SDL_DestroyTexture(target);
+ target = NULL;
+ }
+
if (renderer != NULL) {
SDL_DestroyRenderer(renderer);
renderer = NULL;
@@ -216,10 +230,50 @@ void LZY_Quit(void) {
int LZY_DrawBegin(void) {
if (fps_limiter)
next_time += min_dt;
+
+ if (SDL_SetRenderTarget(renderer, target) < 0) {
+ error = SDL_GetError();
+ return -1;
+ }
return 0;
}
int LZY_DrawEnd(void) {
+ int win_w, win_h, off_x, off_y;
+ float ratio_w, ratio_h, scale;
+ SDL_Rect src, dst;
+
+ if (SDL_SetRenderTarget(renderer, NULL)) {
+ error = SDL_GetError();
+ return -1;
+ }
+
+ LZY_DrawSetColor(0x00, 0x00, 0x00);
+ if (LZY_DrawClear())
+ return -2;
+
+ SDL_GetWindowSize(window, &win_w, &win_h);
+ ratio_w = (float)win_w / LZY_DISPLAY_WIDTH;
+ ratio_h = (float)win_h / LZY_DISPLAY_HEIGHT;
+ scale = (ratio_w < ratio_h) ? (ratio_w) : (ratio_h);
+ off_x = (win_w - LZY_DISPLAY_WIDTH * scale) / 2;
+ off_y = (win_h - LZY_DISPLAY_HEIGHT * scale) / 2;
+
+ src.x = 0;
+ src.y = 0;
+ src.w = LZY_DISPLAY_WIDTH;
+ src.h = LZY_DISPLAY_HEIGHT;
+
+ dst.x = off_x;
+ dst.y = off_y;
+ dst.w = LZY_DISPLAY_WIDTH * scale;
+ dst.h = LZY_DISPLAY_HEIGHT * scale;
+
+ if (SDL_RenderCopy(renderer, target, &src, &dst) < 0) {
+ error = SDL_GetError();
+ return -3;
+ }
+
SDL_RenderPresent(renderer);
if (fps_limiter) {
@@ -250,18 +304,30 @@ int LZY_DrawClear(void) {
int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h) {
const SDL_Rect rect = {x, y, w, h};
+
+ if (w == 0 || h == 0) {
+ error = "rectangle dimensions cannot be 0";
+ return -1;
+ }
+
if (SDL_RenderDrawRect(renderer, &rect) < 0) {
error = SDL_GetError();
- return -1;
+ return -2;
}
return 0;
}
int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h) {
const SDL_Rect rect = {x, y, w, h};
+
+ if (w == 0 || h == 0) {
+ error = "rectangle dimensions cannot be 0";
+ return -1;
+ }
+
if (SDL_RenderFillRect(renderer, &rect) < 0) {
error = SDL_GetError();
- return -1;
+ return -2;
}
return 0;
}