Godzil 3 gadi atpakaļ
vecāks
revīzija
f512d7d69e

+ 1 - 1
source/AssetStore.cpp

@@ -59,7 +59,7 @@ void AssetStore::addFont(const std::string &assetId, const std::string &filePath
     TTF_Font *font = TTF_OpenFont(filePath.c_str(), fontSize);
     if (!font)
     {
-        Logger::Error("Error opening texture '%s' (path: '%s')", assetId.c_str(), filePath.c_str());
+        Logger::Error("Error opening font '%s' (path: '%s') Error: %d", assetId.c_str(), filePath.c_str(), TTF_GetError);
         return;
     }
 

+ 25 - 15
source/Game.cpp

@@ -28,6 +28,7 @@
 #include <Components/CameraFollow.h>
 #include <Components/ProjectileEmitter.h>
 #include <Components/Heakth.h>
+#include <Components/TextLabel.h>
 
 #include <Systems/Movement.h>
 #include <Systems/Render.h>
@@ -38,6 +39,7 @@
 #include <Systems/CameraMovement.h>
 #include <Systems/ProjectileEmit.h>
 #include <Systems/ProjectileLifeCycle.h>
+#include <Systems/RenderText.h>
 
 uint32_t Game::windowsWidth = 0;
 uint32_t Game::windowsHeight = 0;
@@ -70,6 +72,13 @@ void Game::Initialize()
         return;
     }
 
+    ret = TTF_Init();
+    if (ret)
+    {
+        Logger::Critical("SDL_ttf Initialisation error.");
+        return;
+    }
+
     SDL_GetCurrentDisplayMode(0, &displayMode);
     Game::windowsWidth = 800; //displayMode.w;
     Game::windowsHeight = 600; //displayMode.h;
@@ -145,6 +154,7 @@ void Game::LoadLevel(int level)
     this->registry->addSystem<CameraMovementSystem>();
     this->registry->addSystem<ProjectileEmitSystem>();
     this->registry->addSystem<ProjectileLifeCycleSystem>();
+    this->registry->addSystem<RenderTextSystem>();
 
     this->assetStore->addTexture(this->renderer, "tank-image", "assets/images/tank-panther-right.png");
     this->assetStore->addTexture(this->renderer, "truck-image", "assets/images/truck-ford-right.png");
@@ -153,9 +163,9 @@ void Game::LoadLevel(int level)
     this->assetStore->addTexture(this->renderer, "radar-image", "assets/images/radar.png");
     this->assetStore->addTexture(this->renderer, "bullet-image", "assets/images/bullet.png");
 
-    this->assetStore->addFont("charriot-font", "assets/fonts/charriot.ttf", 14);
-    this->assetStore->addFont("arial-font", "assets/fonts/charriot.ttf", 14);
-    this->assetStore->addFont("pico8-font", "assets/fonts/charriot.ttf", 14);
+    this->assetStore->addFont("charriot-font", "assets/fonts/charriot.ttf", 20);
+    this->assetStore->addFont("arial-font", "assets/fonts/charriot.ttf", 20);
+    this->assetStore->addFont("pico8-font", "assets/fonts/charriot.ttf", 20);
 
     Logger::Debug("struct SpriteComponent size is %d", sizeof(struct SpriteComponent));
 
@@ -193,8 +203,8 @@ void Game::LoadLevel(int level)
     Game::mapHeight = mapNumRows * tileSize *  tileScale;
 
     Entity chopper = this->registry->createEntity();
-    chopper.addComponent<TransformComponent>(glm::vec2(10, 100), glm::vec2(1, 1), 0);
-    chopper.addComponent<SpriteComponent>("chopper-image", 2, 32, 32);
+    chopper.addComponent<TransformComponent>(glm::vec2(240, 110), glm::vec2(1, 1), 0);
+    chopper.addComponent<SpriteComponent>("chopper-image", 1, 32, 32);
     chopper.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
     chopper.addComponent<BoxColliderComponent>(32, 32);
     chopper.addComponent<AnimationComponent>(2, 15, true);
@@ -213,26 +223,26 @@ void Game::LoadLevel(int level)
     radar.addComponent<AnimationComponent>(8, 5, true);
 
     Entity tank = this->registry->createEntity();
-    tank.addComponent<TransformComponent>(glm::vec2(500, 10), glm::vec2(1, 1), 0);
-    tank.addComponent<RigidBodyComponent>(glm::vec2(-3, 0));
+    tank.addComponent<TransformComponent>(glm::vec2(500, 500), glm::vec2(1, 1), 0);
+    tank.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
     tank.addComponent<SpriteComponent>("tank-image", 1, 32, 32);
     tank.addComponent<BoxColliderComponent>(32, 32);
-    tank.addComponent<ProjectileEmitterComponent>(glm::vec2(100, 0), 5000, 3000, 10, false);
+    tank.addComponent<ProjectileEmitterComponent>(glm::vec2(100, 0), 3000, 5000, 10, false);
     tank.addComponent<HealthComponent>(100);
     tank.group("enemies");
 
     Entity truck = this->registry->createEntity();
-    truck.addComponent<TransformComponent>(glm::vec2(10, 10), glm::vec2(1, 1), 0);
-    truck.addComponent<RigidBodyComponent>(glm::vec2(2, 0));
-    truck.addComponent<SpriteComponent>("truck-image", 1, 32, 32);
+    truck.addComponent<TransformComponent>(glm::vec2(120, 500), glm::vec2(1, 1), 0);
+    truck.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
+    truck.addComponent<SpriteComponent>("truck-image", 2, 32, 32);
     truck.addComponent<BoxColliderComponent>(32, 32);
-    truck.addComponent<ProjectileEmitterComponent>(glm::vec2(0, 100), 2000, 5000, 10, false);
+    truck.addComponent<ProjectileEmitterComponent>(glm::vec2(0, -100), 2000, 5000, 10, false);
     truck.addComponent<HealthComponent>(100);
     truck.group("enemies");
 
+    SDL_Color green = { 64, 255, 127, 255 };
     Entity label = this->registry->createEntity();
-    label.addComponent<TransformComponent>(10, 10, glm::vec2(0, 0), 0);
-    //label.addComponent<TextLabelComponent>( ... );
+    label.addComponent<TextLabelComponent>(glm::vec2(this->windowsWidth/2 - 60, 10), "SUPER CHOPPER BROS 1.0", "charriot-font", green);
 }
 
 void Game::Setup()
@@ -307,7 +317,7 @@ void Game::Render()
     SDL_RenderClear(this->renderer);
 
     registry->getSystem<RenderSystem>().update(this->renderer, this->assetStore, this->camera);
-
+    registry->getSystem<RenderTextSystem>().update(this->renderer, this->assetStore, this->camera);
     if (this->isDebug)
     {
         registry->getSystem<CollisionSystem>().debugRender(this->renderer, this->camera);

+ 34 - 0
source/include/Components/TextLabel.h

@@ -0,0 +1,34 @@
+/*
+ * 2D Game Engine 
+ * TextLabel.h: 
+ * Based on pikuma.com 2D game engine in C++ and Lua course
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 27/02/2021.
+ */
+
+#ifndef GAMEENGINE_SOURCE_INCLUDE_COMPONENTS_TEXTLABEL_H
+#define GAMEENGINE_SOURCE_INCLUDE_COMPONENTS_TEXTLABEL_H
+
+#include <SDL.h>
+#include <glm/glm.hpp>
+#include <string>
+
+struct TextLabelComponent
+{
+    glm::vec2 position;
+    std::string text;
+    std::string fontId;
+    SDL_Color fgColor;
+    bool isFixed;
+
+    TextLabelComponent(glm::vec2 position = glm::vec2(0, 0),
+                       std::string text = "",
+                       std::string fontId = "",
+                       SDL_Color fgColor = {0, 0, 0, 0},
+                       bool isFixed = true) : position(position), text(text), fontId(fontId), fgColor(fgColor), isFixed(isFixed) {};
+
+
+};
+
+#endif /* GAMEENGINE_SOURCE_INCLUDE_COMPONENTS_TEXTLABEL_H */

+ 54 - 0
source/include/Systems/RenderText.h

@@ -0,0 +1,54 @@
+/*
+ * 2D Game Engine 
+ * RenderText.h: 
+ * Based on pikuma.com 2D game engine in C++ and Lua course
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 27/02/2021.
+ */
+
+#ifndef GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERTEXT_H
+#define GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERTEXT_H
+
+
+#include <SDL.h>
+#include <SDL_ttf.h>
+
+#include <AssetStore.h>
+#include <ECS.h>
+#include <Components/TextLabel.h>
+
+class RenderTextSystem: public System
+{
+public:
+    RenderTextSystem()
+    {
+        this->requireComponent<TextLabelComponent>();
+    }
+
+    void update(SDL_Renderer *renderer, std::unique_ptr<AssetStore> &assetStore, SDL_Rect &camera)
+    {
+        for(auto entity: this->getSystemEntities())
+        {
+            const auto textLabel = entity.getComponent<TextLabelComponent>();
+            TTF_Font *font = assetStore->getFont(textLabel.fontId);
+            SDL_Surface *surface = TTF_RenderText_Blended(font, textLabel.text.c_str(), textLabel.fgColor);
+            SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
+
+            int labelWidth, labelHeight;
+
+            SDL_QueryTexture(texture, NULL, NULL, &labelWidth, &labelHeight);
+
+            SDL_Rect destRect =
+                {
+                    static_cast<int>(textLabel.position.x - (textLabel.isFixed?0.:camera.x)),
+                    static_cast<int>(textLabel.position.y - (textLabel.isFixed?0.:camera.y)),
+                    labelWidth, labelHeight
+                };
+            SDL_FreeSurface(surface);
+            SDL_RenderCopyEx(renderer, texture, NULL, &destRect,0, NULL, SDL_FLIP_NONE);
+        }
+    }
+};
+
+#endif /* GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERTEXT_H */