Working on time and input
This commit is contained in:
		| @@ -21,5 +21,6 @@ add_subdirectory(asset) | |||||||
| add_subdirectory(display) | add_subdirectory(display) | ||||||
| add_subdirectory(input) | add_subdirectory(input) | ||||||
| add_subdirectory(scene) | add_subdirectory(scene) | ||||||
|  | add_subdirectory(time) | ||||||
| add_subdirectory(ui) | add_subdirectory(ui) | ||||||
| add_subdirectory(visualnovel) | add_subdirectory(visualnovel) | ||||||
| @@ -9,6 +9,8 @@ | |||||||
| #include "display/RenderManager.hpp" | #include "display/RenderManager.hpp" | ||||||
| #include "asset/AssetManager.hpp" | #include "asset/AssetManager.hpp" | ||||||
| #include "input/InputManager.hpp" | #include "input/InputManager.hpp" | ||||||
|  | #include "time/TimeManager.hpp" | ||||||
|  | #include "input/InputBinds.hpp" | ||||||
|  |  | ||||||
| #define DAWN_GAME_INIT_RESULT_SUCCESS 0 | #define DAWN_GAME_INIT_RESULT_SUCCESS 0 | ||||||
| #define DAWN_GAME_UPDATE_RESULT_SUCCESS 0 | #define DAWN_GAME_UPDATE_RESULT_SUCCESS 0 | ||||||
| @@ -24,6 +26,7 @@ namespace Dawn { | |||||||
|       RenderManager renderManager; |       RenderManager renderManager; | ||||||
|       AssetManager assetManager; |       AssetManager assetManager; | ||||||
|       InputManager inputManager; |       InputManager inputManager; | ||||||
|  |       TimeManager timeManager; | ||||||
|  |  | ||||||
|       /** |       /** | ||||||
|        * Construct a new instance of the DawnGame. |        * Construct a new instance of the DawnGame. | ||||||
|   | |||||||
| @@ -25,6 +25,8 @@ void Scene::update() { | |||||||
|   // TODO: Cleanup old scene items |   // TODO: Cleanup old scene items | ||||||
|  |  | ||||||
|   // TODO: Tick scene items(?) |   // TODO: Tick scene items(?) | ||||||
|  |   this->eventSceneUpdate.invoke(); | ||||||
|  |   if(!this->game.timeManager.isPaused) this->eventSceneUnpausedUpdate.invoke(); | ||||||
| } | } | ||||||
|  |  | ||||||
| std::shared_ptr<SceneItem> Scene::createSceneItem() { | std::shared_ptr<SceneItem> Scene::createSceneItem() { | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ | |||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
| #include "SceneItem.hpp" | #include "SceneItem.hpp" | ||||||
|  | #include "event/Event.hpp" | ||||||
|  |  | ||||||
| namespace Dawn { | namespace Dawn { | ||||||
|   class DawnGame; |   class DawnGame; | ||||||
| @@ -17,6 +18,8 @@ namespace Dawn { | |||||||
|  |  | ||||||
|     public: |     public: | ||||||
|       DawnGame &game; |       DawnGame &game; | ||||||
|  |       Event<> eventSceneUpdate; | ||||||
|  |       Event<> eventSceneUnpausedUpdate; | ||||||
|  |  | ||||||
|       /** |       /** | ||||||
|        * Construct a new Scene instance. |        * Construct a new Scene instance. | ||||||
|   | |||||||
							
								
								
									
										10
									
								
								src/dawn/time/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/dawn/time/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | # Copyright (c) 2022 Dominic Masters | ||||||
|  | #  | ||||||
|  | # This software is released under the MIT License. | ||||||
|  | # https://opensource.org/licenses/MIT | ||||||
|  |  | ||||||
|  | # Sources | ||||||
|  | target_sources(${DAWN_TARGET_NAME} | ||||||
|  |   PRIVATE | ||||||
|  |     TimeManager.cpp | ||||||
|  | ) | ||||||
							
								
								
									
										32
									
								
								src/dawn/time/TimeManager.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/dawn/time/TimeManager.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | |||||||
|  | // Copyright (c) 2022 Dominic Masters | ||||||
|  | //  | ||||||
|  | // This software is released under the MIT License. | ||||||
|  | // https://opensource.org/licenses/MIT | ||||||
|  |  | ||||||
|  | #include "TimeManager.hpp" | ||||||
|  |  | ||||||
|  | using namespace Dawn; | ||||||
|  |  | ||||||
|  | TimeManager::TimeManager() { | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void TimeManager::update(float_t delta) { | ||||||
|  |   this->delta = delta; | ||||||
|  |   this->time += delta; | ||||||
|  |   if(!this->isPaused) { | ||||||
|  |     this->unpausedTime += delta; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void TimeManager::pause() { | ||||||
|  |   if(this->isPaused) return; | ||||||
|  |   this->isPaused = true; | ||||||
|  |   this->eventTimePaused.invoke(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void TimeManager::resume() { | ||||||
|  |   if(!this->isPaused) return; | ||||||
|  |   this->isPaused = false; | ||||||
|  |   this->eventTimeResumed.invoke(); | ||||||
|  | } | ||||||
							
								
								
									
										43
									
								
								src/dawn/time/TimeManager.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/dawn/time/TimeManager.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | |||||||
|  | // Copyright (c) 2022 Dominic Masters | ||||||
|  | //  | ||||||
|  | // This software is released under the MIT License. | ||||||
|  | // https://opensource.org/licenses/MIT | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  | #include "dawnlibs.hpp" | ||||||
|  | #include "event/Event.hpp" | ||||||
|  |  | ||||||
|  | namespace Dawn { | ||||||
|  |   class TimeManager { | ||||||
|  |     public: | ||||||
|  |       float_t time = 0.0f; | ||||||
|  |       float_t unpausedTime = 0.0f; | ||||||
|  |       float_t delta = 0.016f; | ||||||
|  |       bool_t isPaused = false; | ||||||
|  |  | ||||||
|  |       Event<> eventTimePaused; | ||||||
|  |       Event<> eventTimeResumed; | ||||||
|  |  | ||||||
|  |       /** | ||||||
|  |        * Constructor for the Time Manager. | ||||||
|  |        */ | ||||||
|  |       TimeManager(); | ||||||
|  |  | ||||||
|  |       /** | ||||||
|  |        * Updates / Ticks the time manager instance. | ||||||
|  |        *  | ||||||
|  |        * @param delta Time in seconds to tick the instance by. | ||||||
|  |        */ | ||||||
|  |       void update(float_t delta); | ||||||
|  |  | ||||||
|  |       /** | ||||||
|  |        * Pauses the game. | ||||||
|  |        */ | ||||||
|  |       void pause(); | ||||||
|  |  | ||||||
|  |       /** | ||||||
|  |        * Resumes the game. | ||||||
|  |        */ | ||||||
|  |       void resume(); | ||||||
|  |   }; | ||||||
|  | } | ||||||
| @@ -13,7 +13,6 @@ UILabel::UILabel(UICanvas &canvas) : UIComponent(canvas) { | |||||||
|  |  | ||||||
| void UILabel::updatePositions() { | void UILabel::updatePositions() { | ||||||
|   UIComponent::updatePositions(); |   UIComponent::updatePositions(); | ||||||
|  |  | ||||||
|   this->updateMesh(); |   this->updateMesh(); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -59,5 +58,5 @@ void UILabel::drawSelf(UIShader &shader, glm::mat4 selfTransform) { | |||||||
|   shader.setUIModel(selfTransform); |   shader.setUIModel(selfTransform); | ||||||
|   shader.setUITexture(&this->font->getTexture()); |   shader.setUITexture(&this->font->getTexture()); | ||||||
|    |    | ||||||
|   this->font->draw(this->mesh, 0, -1); |   this->font->draw(this->mesh, this->startQuad, this->endQuad); | ||||||
| } | } | ||||||
| @@ -12,7 +12,6 @@ namespace Dawn { | |||||||
|   class UILabel : public UIComponent { |   class UILabel : public UIComponent { | ||||||
|     private: |     private: | ||||||
|       Mesh mesh; |       Mesh mesh; | ||||||
|       struct FontMeasure measure; |  | ||||||
|       bool_t needsRebuffering = true; |       bool_t needsRebuffering = true; | ||||||
|       Font *font = nullptr; |       Font *font = nullptr; | ||||||
|       std::string text = ""; |       std::string text = ""; | ||||||
| @@ -26,6 +25,10 @@ namespace Dawn { | |||||||
|       void updateMesh(); |       void updateMesh(); | ||||||
|  |  | ||||||
|     public: |     public: | ||||||
|  |       struct FontMeasure measure; | ||||||
|  |       int32_t startQuad = 0; | ||||||
|  |       int32_t endQuad = -1; | ||||||
|  |  | ||||||
|       /** The colour of this label */ |       /** The colour of this label */ | ||||||
|       struct Color textColor = COLOR_MAGENTA; |       struct Color textColor = COLOR_MAGENTA; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ | |||||||
| // https://opensource.org/licenses/MIT | // https://opensource.org/licenses/MIT | ||||||
|  |  | ||||||
| #include "VisualNovelTextbox.hpp" | #include "VisualNovelTextbox.hpp" | ||||||
|  | #include "game/DawnGame.hpp" | ||||||
|  |  | ||||||
| using namespace Dawn; | using namespace Dawn; | ||||||
|  |  | ||||||
| @@ -12,10 +13,58 @@ VisualNovelTextbox::VisualNovelTextbox(UICanvas &canvas) : | |||||||
|   border(canvas), |   border(canvas), | ||||||
|   label(canvas) |   label(canvas) | ||||||
| { | { | ||||||
|  |   // Border | ||||||
|   this->addChild(&this->border); |   this->addChild(&this->border); | ||||||
|  |  | ||||||
|  |   // Label | ||||||
|   this->addChild(&this->label); |   this->addChild(&this->label); | ||||||
|  |   this->label.setText("The quick brown fox jumps over the lazy dog."); | ||||||
|  |   this->label.startQuad = 0; | ||||||
|  |   this->label.endQuad = 1; | ||||||
|  |  | ||||||
|  |   this->updatePositions(); | ||||||
|  |  | ||||||
|  |   this->canvas.getScene().eventSceneUnpausedUpdate.addListener( | ||||||
|  |     this, &VisualNovelTextbox::textboxOnSceneUpdate | ||||||
|  |   ); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void VisualNovelTextbox::textboxOnSceneUpdate() { | ||||||
|  |   DawnGame &game = this->canvas.getGame(); | ||||||
|  |   if(game.inputManager.isDown(INPUT_BIND_ACCEPT)) { | ||||||
|  |     this->timeCharacter += game.timeManager.delta * VISUAL_NOVEL_TEXTBOX_SPEED_FASTER; | ||||||
|  |   } else { | ||||||
|  |     this->timeCharacter += game.timeManager.delta * VISUAL_NOVEL_TEXTBOX_SPEED; | ||||||
|  |   } | ||||||
|  |   this->label.endQuad = (int32_t)mathFloorFloat(this->timeCharacter); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void VisualNovelTextbox::updatePositions() { | ||||||
|  |   UIComponent::updatePositions(); | ||||||
|  |  | ||||||
|  |   this->border.setTransform( | ||||||
|  |     UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_STRETCH, | ||||||
|  |     glm::vec4(0, 0, 0, 0), | ||||||
|  |     0.0f | ||||||
|  |   ); | ||||||
|  |  | ||||||
|  |   this->label.setTransform( | ||||||
|  |     UI_COMPONENT_ALIGN_STRETCH, | ||||||
|  |     UI_COMPONENT_ALIGN_STRETCH, | ||||||
|  |     glm::vec4( | ||||||
|  |       this->border.getBorderSize() + this->labelPadding, | ||||||
|  |       this->border.getBorderSize() + this->labelPadding | ||||||
|  |     ), | ||||||
|  |     1.0f | ||||||
|  |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| void VisualNovelTextbox::drawSelf(UIShader &shader, glm::mat4 self) { | void VisualNovelTextbox::drawSelf(UIShader &shader, glm::mat4 self) { | ||||||
|    |    | ||||||
| } | } | ||||||
|  |  | ||||||
|  | VisualNovelTextbox::~VisualNovelTextbox() { | ||||||
|  |   this->canvas.getScene().eventSceneUnpausedUpdate.removeListener( | ||||||
|  |     this, &VisualNovelTextbox::textboxOnSceneUpdate | ||||||
|  |   ); | ||||||
|  | } | ||||||
| @@ -7,16 +7,28 @@ | |||||||
| #include "ui/UISprite.hpp" | #include "ui/UISprite.hpp" | ||||||
| #include "ui/UIBorder.hpp" | #include "ui/UIBorder.hpp" | ||||||
| #include "ui/UILabel.hpp" | #include "ui/UILabel.hpp" | ||||||
|  | #include "util/mathutils.hpp" | ||||||
|  |  | ||||||
|  | #define VISUAL_NOVEL_TEXTBOX_SPEED 25.0f | ||||||
|  | #define VISUAL_NOVEL_TEXTBOX_SPEED_FASTER 40.0f | ||||||
|  |  | ||||||
| namespace Dawn { | namespace Dawn { | ||||||
|   class VisualNovelTextbox : public UIComponent { |   class VisualNovelTextbox : public UIComponent { | ||||||
|     private: |     private: | ||||||
|       UIBorder border; |  | ||||||
|       UILabel label; |  | ||||||
|       glm::vec2 labelPadding = glm::vec2(0, 0); |       glm::vec2 labelPadding = glm::vec2(0, 0); | ||||||
|  |  | ||||||
|  |       void updatePositions() override; | ||||||
|  |  | ||||||
|  |       void textboxOnSceneUpdate(); | ||||||
|  |  | ||||||
|     public: |     public: | ||||||
|  |       float_t timeCharacter = 0.0f; | ||||||
|  |       UIBorder border; | ||||||
|  |       UILabel label; | ||||||
|  |        | ||||||
|       VisualNovelTextbox(UICanvas &canvas); |       VisualNovelTextbox(UICanvas &canvas); | ||||||
|       void drawSelf(UIShader &shader, glm::mat4 selfTransform) override; |       void drawSelf(UIShader &shader, glm::mat4 selfTransform) override; | ||||||
|  |  | ||||||
|  |       ~VisualNovelTextbox(); | ||||||
|   }; |   }; | ||||||
| } | } | ||||||
| @@ -56,6 +56,11 @@ int32_t DawnHost::init(DawnGame &game) { | |||||||
|     DAWN_GLFW_WINDOW_HEIGHT_DEFAULT |     DAWN_GLFW_WINDOW_HEIGHT_DEFAULT | ||||||
|   ); |   ); | ||||||
|  |  | ||||||
|  |   // Default keybinds | ||||||
|  |   game.inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_ENTER); | ||||||
|  |   game.inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_E); | ||||||
|  |   game.inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_SPACE); | ||||||
|  |  | ||||||
|   // Initialize the game |   // Initialize the game | ||||||
|   auto result = game.init(); |   auto result = game.init(); | ||||||
|   if(result != DAWN_GAME_INIT_RESULT_SUCCESS) return result; |   if(result != DAWN_GAME_INIT_RESULT_SUCCESS) return result; | ||||||
|   | |||||||
| @@ -9,5 +9,11 @@ set(DAWN_TARGET_NAME "PokerGame" CACHE INTERNAL ${DAWN_CACHE_TARGET}) | |||||||
| # Build Project | # Build Project | ||||||
| add_executable(${DAWN_TARGET_NAME}) | add_executable(${DAWN_TARGET_NAME}) | ||||||
|  |  | ||||||
|  | # Includes | ||||||
|  | target_include_directories(${DAWN_TARGET_NAME} | ||||||
|  |   PUBLIC | ||||||
|  |     ${CMAKE_CURRENT_LIST_DIR} | ||||||
|  | ) | ||||||
|  |  | ||||||
| # Subdirs | # Subdirs | ||||||
| add_subdirectory(game) | add_subdirectory(game) | ||||||
| @@ -5,11 +5,12 @@ | |||||||
|  |  | ||||||
| #include "DawnPokerGame.hpp" | #include "DawnPokerGame.hpp" | ||||||
| #include "asset/assets/TextureAsset.hpp" | #include "asset/assets/TextureAsset.hpp" | ||||||
| #include "ui/UIBorder.hpp" | #include "asset/assets/TrueTypeAsset.hpp" | ||||||
|  | #include "visualnovel/ui/VisualNovelTextbox.hpp" | ||||||
|  |  | ||||||
| using namespace Dawn; | using namespace Dawn; | ||||||
|  |  | ||||||
| std::shared_ptr<TrueTypeAsset> asset; | std::shared_ptr<TrueTypeAsset> assetFont; | ||||||
| std::shared_ptr<TextureAsset> assetTexture; | std::shared_ptr<TextureAsset> assetTexture; | ||||||
|  |  | ||||||
| DawnGame::DawnGame(DawnHost &host) : | DawnGame::DawnGame(DawnHost &host) : | ||||||
| @@ -31,17 +32,19 @@ int32_t DawnGame::init() { | |||||||
|    |    | ||||||
|   auto canvas = UICanvas::createCanvas(this->scene); |   auto canvas = UICanvas::createCanvas(this->scene); | ||||||
|    |    | ||||||
|   asset = this->assetManager.load<TrueTypeAsset>("truetype_ark"); |   assetFont = this->assetManager.load<TrueTypeAsset>("truetype_ark"); | ||||||
|   assetTexture = this->assetManager.load<TextureAsset>("texture_test"); |   assetTexture = this->assetManager.load<TextureAsset>("texture_test"); | ||||||
|   while(!asset->loaded || !assetTexture->loaded) { |   while(!assetFont->loaded || !assetTexture->loaded) { | ||||||
|     this->assetManager.update(); |     this->assetManager.update(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   auto border = canvas->addElement<UIBorder>(); |   auto textbox = canvas->addElement<VisualNovelTextbox>(); | ||||||
|   border->texture = assetTexture->texture.get(); |   textbox->border.texture = assetTexture->texture.get(); | ||||||
|   border->setTransform( |   textbox->label.setFont(&assetFont->font); | ||||||
|     UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_STRETCH, |   textbox->label.setFontSize(40); | ||||||
|     glm::vec4(32, 32, 32, 32), 0.0f |   textbox->setTransform( | ||||||
|  |     UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_END, | ||||||
|  |     glm::vec4(0, 250, 0, 0), 0.0f | ||||||
|   ); |   ); | ||||||
|  |  | ||||||
|   // auto sprite = canvas->addElement<UISprite>(); |   // auto sprite = canvas->addElement<UISprite>(); | ||||||
| @@ -59,6 +62,7 @@ int32_t DawnGame::init() { | |||||||
| int32_t DawnGame::update(float_t delta) { | int32_t DawnGame::update(float_t delta) { | ||||||
|   this->assetManager.update(); |   this->assetManager.update(); | ||||||
|   this->inputManager.update(); |   this->inputManager.update(); | ||||||
|  |   this->timeManager.update(delta); | ||||||
|  |  | ||||||
|   if(this->scene != nullptr) this->scene->update(); |   if(this->scene != nullptr) this->scene->update(); | ||||||
|    |    | ||||||
|   | |||||||
| @@ -6,6 +6,3 @@ | |||||||
| #pragma once | #pragma once | ||||||
| #include "game/DawnGame.hpp" | #include "game/DawnGame.hpp" | ||||||
| #include "scene/components/Components.hpp" | #include "scene/components/Components.hpp" | ||||||
| #include "ui/UILabel.hpp" |  | ||||||
| #include "ui/UISprite.hpp" |  | ||||||
| #include "asset/assets/TrueTypeAsset.hpp" |  | ||||||
							
								
								
									
										9
									
								
								src/dawnpokergame/input/InputBinds.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/dawnpokergame/input/InputBinds.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | // Copyright (c) 2022 Dominic Masters | ||||||
|  | //  | ||||||
|  | // This software is released under the MIT License. | ||||||
|  | // https://opensource.org/licenses/MIT | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  | #include "input/InputManager.hpp" | ||||||
|  |  | ||||||
|  | #define INPUT_BIND_ACCEPT ((inputbind_t)1) | ||||||
		Reference in New Issue
	
	Block a user