From 6936cd871bd99f702e6180b169785b814cfa00f3 Mon Sep 17 00:00:00 2001 From: realtradam Date: Fri, 4 Nov 2022 16:32:10 -0400 Subject: implemented sprites --- orbital_game_doc/.obsidian/workspace.json | 5 ++- src/main.cpp | 6 ++- src/resources.cpp | 65 ++++++++++++++++++++++--------- src/resources.h | 15 ++++++- 4 files changed, 67 insertions(+), 24 deletions(-) diff --git a/orbital_game_doc/.obsidian/workspace.json b/orbital_game_doc/.obsidian/workspace.json index 998ad1d..3c57dec 100644 --- a/orbital_game_doc/.obsidian/workspace.json +++ b/orbital_game_doc/.obsidian/workspace.json @@ -69,7 +69,8 @@ } ], "direction": "horizontal", - "width": 300 + "width": 300, + "collapsed": true }, "right": { "id": "4eafccb3ae5c4cab", @@ -136,7 +137,7 @@ "width": 300, "collapsed": true }, - "active": "387ea3eb3fadc8cb", + "active": "5a480a8cd616d9c5", "lastOpenFiles": [ "Example Doc.md", "Design Doc.md" diff --git a/src/main.cpp b/src/main.cpp index d4b1bc3..827368d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,8 @@ int main(void) SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- + + auto ship = Resources::Sprite("ship", (Rectangle){1365,1696,198,188}); // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key @@ -30,8 +32,8 @@ int main(void) ClearBackground(RAYWHITE); DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); - ; - DrawTexture(Resources::useTexture("ship"), 0, 0, WHITE); + //DrawTexture(Resources::useTexture("ship"), 0, 0, WHITE); + ship.draw(100,100); EndDrawing(); //---------------------------------------------------------------------------------- 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); + } } diff --git a/src/resources.h b/src/resources.h index eef1883..edc1fec 100644 --- a/src/resources.h +++ b/src/resources.h @@ -3,5 +3,18 @@ #include "raylib.h" namespace Resources { - Texture useTexture(std::string id); + Texture useTexture(std::string id); + class Sprite { + public: + std::string texture_name; + Rectangle source_rectangle; + Vector2 origin; + + Sprite(std::string texture_name, + Rectangle source_rectangle, + Vector2 origin = (Vector2){0,0} + ); + void drawPro(Rectangle dest_rectangle, float rotation = 0, Color color = WHITE); + void draw(float x, float y, float scale = 1.0, float rotation = 0, Color color = WHITE); + }; } -- cgit v1.2.3