Godzil vor 3 Jahren
Ursprung
Commit
dc81b0aa1b
3 geänderte Dateien mit 35 neuen und 9 gelöschten Zeilen
  1. 6 6
      source/Game.cpp
  2. 15 1
      source/include/Components/Sprite.h
  3. 14 2
      source/include/Systems/Render.h

+ 6 - 6
source/Game.cpp

@@ -50,8 +50,8 @@ void Game::Initialize()
     }
 
     SDL_GetCurrentDisplayMode(0, &displayMode);
-    this->windowsWidth = displayMode.w;
-    this->windowsHeight = displayMode.h;
+    this->windowsWidth = 800;//displayMode.w;
+    this->windowsHeight = 600;//displayMode.h;
 
     this->window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                     this->windowsWidth, this->windowsHeight, SDL_WINDOW_BORDERLESS);
@@ -68,7 +68,7 @@ void Game::Initialize()
         return;
     }
 
-    SDL_SetWindowFullscreen(this->window, SDL_WINDOW_FULLSCREEN);
+    //SDL_SetWindowFullscreen(this->window, SDL_WINDOW_FULLSCREEN);
 
     this->isRunning = true;
 }
@@ -111,13 +111,13 @@ void Game::Setup()
 
     tank.addComponent<TransformComponent>(glm::vec2(10, 30), glm::vec2(1, 1), 0);
     tank.addComponent<RigidBodyComponent>(glm::vec2(40, 0));
-    tank.addComponent<SpriteComponent>("tank-image", 10, 10);
+    tank.addComponent<SpriteComponent>("tank-image", 32, 32);
 
     Entity truck = this->registry->createEntity();
 
     truck.addComponent<TransformComponent>(glm::vec2(50, 100), glm::vec2(1, 1), 0);
     truck.addComponent<RigidBodyComponent>(glm::vec2(0, 50));
-    truck.addComponent<SpriteComponent>("truck-image", 10, 50);
+    truck.addComponent<SpriteComponent>("truck-image", 32, 32);
 }
 
 void Game::Update()
@@ -167,7 +167,7 @@ void Game::Render()
     SDL_RenderClear(this->renderer);
 
     // Do something here
-    registry->getSystem<RenderSystem>().update(this->renderer);
+    registry->getSystem<RenderSystem>().update(this->renderer, this->assetStore);
 
     SDL_RenderPresent(this->renderer);
 }

+ 15 - 1
source/include/Components/Sprite.h

@@ -10,6 +10,8 @@
 #ifndef GAMEENGINE_SPRITE_H
 #define GAMEENGINE_SPRITE_H
 
+#include <SDL.h>
+
 #include <string>
 
 struct SpriteComponent
@@ -17,8 +19,20 @@ struct SpriteComponent
     std::string assetId;
     uint32_t width;
     uint32_t height;
+    SDL_Rect sourceRect;
 
-    explicit SpriteComponent(std::string assetId = "", uint32_t width = 0, uint32_t height = 0):assetId(assetId), width(width), height(height) {};
+    explicit SpriteComponent(std::string assetId = "",
+                             uint32_t width = 0, uint32_t height = 0,
+                             uint32_t sourceRectX = 0, uint32_t sourceRectY = 0): assetId(assetId),
+                                                                                  width(width),
+                                                                                  height(height)
+    {
+        this->sourceRect =
+        {
+            static_cast<int>(sourceRectX), static_cast<int>(sourceRectY),
+            static_cast<int>(width), static_cast<int>(height),
+        };
+    };
 };
 
 

+ 14 - 2
source/include/Systems/Render.h

@@ -12,6 +12,7 @@
 
 #include <SDL.h>
 
+#include <AssetStore.h>
 #include <ECS.h>
 #include <Components/Transform.h>
 #include <Components/Sprite.h>
@@ -25,13 +26,24 @@ public:
         this->requireComponent<SpriteComponent>();
     }
 
-    void update(SDL_Renderer *renderer)
+    void update(SDL_Renderer *renderer, std::unique_ptr<AssetStore> &assetStore)
     {
         for (auto entity: this->getSystemEntities())
         {
-            const auto tranform = entity.getComponent<TransformComponent>();
+            const auto transform = entity.getComponent<TransformComponent>();
             const auto sprite = entity.getComponent<SpriteComponent>();
 
+            SDL_Texture *texture = assetStore->getTexture(sprite.assetId);
+            SDL_Rect destRect =
+            {
+                static_cast<int>(transform.position.x),
+                static_cast<int>(transform.position.y),
+                static_cast<int>(sprite.width * transform.scale.x),
+                static_cast<int>(sprite.height * transform.scale.y)
+            };
+
+            SDL_RenderCopyEx(renderer, texture, &sprite.sourceRect, &destRect,
+                             transform.rotation, NULL, SDL_FLIP_NONE);
 
         }
     }