From f8f6d0d2e3e2a21bf48e0ae131015e2542f0f840 Mon Sep 17 00:00:00 2001 From: arngo <27396817+arngo@users.noreply.github.com> Date: Wed, 2 Nov 2022 14:28:51 -0400 Subject: implement resource loader --- assets/textures/spaceShooter2_spritesheet_2X.png | Bin 0 -> 769635 bytes src/main.cpp | 46 +++++++++++++++++++++++ src/resources.cpp | 27 +++++++++++++ src/resources.h | 7 ++++ 4 files changed, 80 insertions(+) create mode 100644 assets/textures/spaceShooter2_spritesheet_2X.png create mode 100644 src/main.cpp create mode 100644 src/resources.cpp create mode 100644 src/resources.h diff --git a/assets/textures/spaceShooter2_spritesheet_2X.png b/assets/textures/spaceShooter2_spritesheet_2X.png new file mode 100644 index 0000000..3c03400 Binary files /dev/null and b/assets/textures/spaceShooter2_spritesheet_2X.png differ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..d4b1bc3 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,46 @@ +#include "raylib.h" +#include "resources.h" +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // TODO: Update your variables here + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); + ; + DrawTexture(Resources::useTexture("ship"), 0, 0, WHITE); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/src/resources.cpp b/src/resources.cpp new file mode 100644 index 0000000..141e4ca --- /dev/null +++ b/src/resources.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include "raylib.h" +#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; + } +} diff --git a/src/resources.h b/src/resources.h new file mode 100644 index 0000000..eef1883 --- /dev/null +++ b/src/resources.h @@ -0,0 +1,7 @@ +#include +#include +#include "raylib.h" + +namespace Resources { + Texture useTexture(std::string id); +} -- cgit v1.2.3