From 549214014de4659b5e44f9f2b71c1d5d025ca1e2 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Sun, 4 Aug 2024 15:59:32 +0200 Subject: [PATCH] resize support --- src/utils.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/utils.hpp | 4 +++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/utils.cpp b/src/utils.cpp index 2fb7807..59d28ec 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -23,3 +23,46 @@ std::vector get_hills() { return surfaces; } + +SDL_Surface *resize_surface(SDL_Surface *t_surface, int t_width, int t_height) { + if (!t_surface) + throw std::runtime_error("Original surface is null."); + + if (t_surface->w == t_width && t_surface->h == t_height) + return t_surface; + + SDL_Surface *resizedSurface = SDL_CreateRGBSurface( + 0, + t_width, + t_height, + t_surface->format->BitsPerPixel, + t_surface->format->Rmask, + t_surface->format->Gmask, + t_surface->format->Bmask, + t_surface->format->Amask + ); + + if (!resizedSurface) + throw std::runtime_error("Failed to create resized surface: " + std::string(SDL_GetError())); + + SDL_Rect src_rect, dest_rect; + for (int y = 0; y < t_height; ++y) { + for (int x = 0; x < t_width; ++x) { + src_rect.x = x * t_surface->w / t_width; + src_rect.y = y * t_surface->h / t_height; + src_rect.w = 1; + src_rect.h = 1; + + dest_rect.x = x; + dest_rect.y = y; + dest_rect.w = 1; + dest_rect.h = 1; + + SDL_BlitSurface(t_surface, &src_rect, resizedSurface, &dest_rect); + } + } + + return resizedSurface; +} + + diff --git a/src/utils.hpp b/src/utils.hpp index 828c1c4..715c90e 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -11,4 +11,6 @@ constexpr size_t array_len(T *array[]) { return i; } -std::vector get_hills(); +std::vector get_hills(); + +SDL_Surface *resize_surface(SDL_Surface *t_surface, int t_width, int t_height);