resize support
This commit is contained in:
parent
472b439bed
commit
549214014d
@ -23,3 +23,46 @@ std::vector<SDL_Surface *> 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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -11,4 +11,6 @@ constexpr size_t array_len(T *array[]) {
|
||||
return i;
|
||||
}
|
||||
|
||||
std::vector<SDL_Surface*> get_hills();
|
||||
std::vector<SDL_Surface *> get_hills();
|
||||
|
||||
SDL_Surface *resize_surface(SDL_Surface *t_surface, int t_width, int t_height);
|
||||
|
Loading…
x
Reference in New Issue
Block a user