Skip to content

Commit

Permalink
Added methods to set scaling mode for textures.
Browse files Browse the repository at this point in the history
  • Loading branch information
bXi committed Apr 4, 2024
1 parent cbd3fd0 commit 9debf4e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
13 changes: 12 additions & 1 deletion assethandler/assethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Texture AssetHandler::_loadTexture(const std::string &fileName) {
SDL_Texture *tex = SDL_CreateTextureFromSurface(Window::GetRenderer(), surface);
texture.texture = tex;


SDL_SetTextureScaleMode(tex, (SDL_ScaleMode)defaultMode);

if (!tex)
SDL_Log("Texture failed: %s", SDL_GetError());

Expand Down Expand Up @@ -176,4 +179,12 @@ Font AssetHandler::_getFont(const std::string &fileName, const int fontSize) {
} else {
return _fonts[index];
}
}
}

void AssetHandler::_setDefaultTextureScaleMode(ScaleMode mode) {
defaultMode = mode;
}

ScaleMode AssetHandler::_getDefaultTextureScaleMode() {
return defaultMode;
}
32 changes: 30 additions & 2 deletions assethandler/assethandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <unordered_map>
#include <string>
#include <utility>

#include "SDL3/SDL.h"
#include "SDL3_image/SDL_image.h"
Expand All @@ -16,6 +17,12 @@
#include "assettypes/sound.h"
#include "assettypes/texture.h"

enum class ScaleMode {
NEAREST,
LINEAR,
BEST,
};

/**
* @brief Manages assets and provides utility functions for working with assets.
*/
Expand All @@ -36,21 +43,36 @@ class AssetHandler {
*/
static void LoadTexture(const char *fileName) { get()._loadTexture(fileName); }

/**
* @brief Sets a scaling mode for all textures loaded after this call.
*
* @param mode The new ScaleMode to set as default.
*/
static void SetDefaultTextureScaleMode(ScaleMode mode) { get()._setDefaultTextureScaleMode(mode); }

/**
* @brief Gets the current scaling mode.
*
* @return The current ScaleMode.
*/
static ScaleMode GetDefaultTextureScaleMode() { return get()._getDefaultTextureScaleMode(); }


/**
* @brief Saves the given texture as a PNG file.
*
* @param texture The texture to save.
* @param fileName The name of the PNG file to save.
*/
static void SaveTextureAsPNG(Texture texture, const char *fileName) { get()._saveTextureAsPNG(texture, fileName); }
static void SaveTextureAsPNG(Texture texture, const char *fileName) { get()._saveTextureAsPNG(std::move(texture), fileName); }

/**
* @brief Creates an empty texture with the specified size.
*
* @param size The size of the empty texture.
* @return The created empty texture.
*/
static Texture CreateEmptyTexture(vf2d size) { return get()._createEmptyTexture(size); }
static Texture CreateEmptyTexture(const vf2d& size) { return get()._createEmptyTexture(size); }

/**
* @brief Retrieves a map of all loaded textures.
Expand Down Expand Up @@ -103,6 +125,10 @@ class AssetHandler {

Texture _loadTexture(const std::string &fileName);

void _setDefaultTextureScaleMode(ScaleMode mode);

ScaleMode _getDefaultTextureScaleMode();

Texture _createEmptyTexture(const vf2d &size);

void _saveTextureAsPNG(Texture texture, const char *fileName);
Expand All @@ -124,6 +150,8 @@ class AssetHandler {
std::unordered_map<std::string, Sound> _sounds;
std::unordered_map<std::string, Texture> _textures;

ScaleMode defaultMode = ScaleMode::LINEAR;

public:
AssetHandler(const AssetHandler &) = delete;

Expand Down

0 comments on commit 9debf4e

Please sign in to comment.