From 6936cd871bd99f702e6180b169785b814cfa00f3 Mon Sep 17 00:00:00 2001 From: realtradam Date: Fri, 4 Nov 2022 16:32:10 -0400 Subject: implemented sprites --- src/resources.cpp | 65 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 19 deletions(-) (limited to 'src/resources.cpp') diff --git a/src/resources.cpp b/src/resources.cpp index 141e4ca..8a820b5 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -5,23 +5,50 @@ #include "resources.h" namespace Resources { - static std::unordered_map textureFiles = { - {"ship", "./assets/textures/spaceShooter2_spritesheet_2X.png"} - }; - static std::unordered_map textures; - Texture useTexture(std::string id) { - Texture texture; - auto texSearch = textures.find(id); - if (texSearch != textures.end()) { - return texSearch->second; - } - auto texPathSearch = textureFiles.find(id); - if (texPathSearch != textureFiles.end()) { - texture = LoadTexture(texPathSearch->second.c_str()); - textures.insert({id, texture}); - } else { - throw std::invalid_argument("Texture id '" + id + "' has no assigned path."); - } - return texture; - } + static std::unordered_map textureFiles = { + {"ship", "./assets/textures/spaceShooter2_spritesheet_2X.png"} + }; + static std::unordered_map textures; + Texture useTexture(std::string id) { + Texture texture; + auto texSearch = textures.find(id); + if (texSearch != textures.end()) { + return texSearch->second; + } + auto texPathSearch = textureFiles.find(id); + if (texPathSearch != textureFiles.end()) { + texture = LoadTexture(texPathSearch->second.c_str()); + textures.insert({id, texture}); + } else { + throw std::invalid_argument("Texture id '" + id + "' has no assigned path."); + } + return texture; + } + + Sprite::Sprite(std::string texture_name, + Rectangle source_rectangle, + Vector2 origin + ):texture_name(texture_name), + source_rectangle(source_rectangle), + origin(origin){ + + } + + void Sprite::drawPro(Rectangle dest_rectangle, float rotation, Color color) { + DrawTexturePro(useTexture(texture_name), + source_rectangle, + dest_rectangle, + origin, + rotation, + color); + } + + void Sprite::draw(float x, float y, float scale, float rotation, Color color) { + DrawTexturePro(useTexture(texture_name), + source_rectangle, + (Rectangle){x,y,source_rectangle.width * scale,source_rectangle.height * scale}, + origin, + rotation, + color); + } } -- cgit v1.2.3