From c055eeef6d41269d11b2ddf7f9aba6f8867da65d Mon Sep 17 00:00:00 2001 From: Nikita Kostovsky Date: Tue, 23 Dec 2025 15:28:05 +0100 Subject: initial commit --- core/src/cpp/sprite.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 core/src/cpp/sprite.cpp (limited to 'core/src/cpp/sprite.cpp') diff --git a/core/src/cpp/sprite.cpp b/core/src/cpp/sprite.cpp new file mode 100644 index 0000000..afd633d --- /dev/null +++ b/core/src/cpp/sprite.cpp @@ -0,0 +1,49 @@ +#include "sprite.h" + +#include + +#include +#include + +Sprite::Sprite( + SDL_Renderer* renderer, + const std::string& filename, + const int width +) + : width{width} +{ + SDL_Surface* surface = + SDL_LoadBMP((std::string(RESOURCES_DIR) + filename).c_str()); + + if (!surface) + { + std::cerr << "Error in SDL_LoadBMP: " << SDL_GetError() << std::endl; + return; + } + + if (!(surface->w % width) && surface->w / width) + { // image width must be a multiple of sprite width + height = surface->h; + nframes = surface->w / width; + texture = SDL_CreateTextureFromSurface(renderer, surface); + } + else + { + std::cerr << "Incorrect sprite size" << std::endl; + } + + SDL_FreeSurface(surface); +} + +Sprite::~Sprite() +{ + if (texture) + { + SDL_DestroyTexture(texture); + } +} + +SDL_Rect Sprite::rect(const int idx) const +{ + return {idx * width, 0, width, height}; +} -- cgit v1.2.3-70-g09d2