From 0f75f79498ebd13a78e7ca984ab8430f532c2692 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 20 Mar 2023 21:12:03 -0700 Subject: [PATCH] Testing UI without assets. --- src/dawn/display/font/CMakeLists.txt | 1 + src/dawn/display/font/ExampleFont.cpp | 104 ++++++++++++++++++ src/dawn/display/font/ExampleFont.hpp | 22 ++++ src/dawn/scene/components/ui/UILabel.cpp | 2 +- src/dawnhelloworld/scenes/HelloWorldScene.hpp | 22 ++++ src/dawnopengl/display/RenderManager.cpp | 4 +- src/dawnopengl/display/RenderManager.hpp | 2 + 7 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 src/dawn/display/font/ExampleFont.cpp create mode 100644 src/dawn/display/font/ExampleFont.hpp diff --git a/src/dawn/display/font/CMakeLists.txt b/src/dawn/display/font/CMakeLists.txt index ebbca010..598e1587 100644 --- a/src/dawn/display/font/CMakeLists.txt +++ b/src/dawn/display/font/CMakeLists.txt @@ -7,6 +7,7 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE BitmapFont.cpp + ExampleFont.cpp TrueTypeFont.cpp FontMeasure.cpp ) \ No newline at end of file diff --git a/src/dawn/display/font/ExampleFont.cpp b/src/dawn/display/font/ExampleFont.cpp new file mode 100644 index 00000000..48a07a31 --- /dev/null +++ b/src/dawn/display/font/ExampleFont.cpp @@ -0,0 +1,104 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "ExampleFont.hpp" + +using namespace Dawn; + +ExampleFont::ExampleFont() : realTilesetGrid(128/8, 64/8, 128, 64, 0, 0, 0, 0) { + this->texture = &this->realTexture; + this->tileset = &this->realTilesetGrid; +} + +struct Color ExampleFont::getColor(uint8_t n) { + if(n == 0) return COLOR_TRANSPARENT; + return COLOR_WHITE; +} + +void ExampleFont::init() { + struct Color pixels[128*64]; + uint8_t data[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x30, 0xD8, 0x50, 0x78, 0xC8, 0x70, 0x30, 0x30, 0x60, 0x48, 0x20, 0x00, 0x00, 0x00, 0x18, + 0x00, 0x30, 0xD8, 0xF8, 0xA0, 0xD0, 0xC8, 0x30, 0x60, 0x30, 0x30, 0x20, 0x00, 0x00, 0x00, 0x30, + 0x00, 0x30, 0x90, 0x50, 0x70, 0x20, 0x60, 0x60, 0x60, 0x30, 0x78, 0xF8, 0x00, 0xF8, 0x00, 0x60, + 0x00, 0x00, 0x00, 0xF8, 0x28, 0x58, 0xD4, 0x00, 0x60, 0x30, 0x30, 0x20, 0x30, 0x00, 0x30, 0xC0, + 0x00, 0x30, 0x00, 0x50, 0xF0, 0x98, 0xC8, 0x00, 0x30, 0x60, 0x48, 0x20, 0x30, 0x00, 0x30, 0x80, + 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x70, 0x10, 0xF0, 0xF0, 0x38, 0xF8, 0x70, 0xF8, 0x70, 0x70, 0x30, 0x30, 0x18, 0x00, 0x60, 0xF0, + 0x88, 0x30, 0x08, 0x08, 0x48, 0x80, 0x80, 0x08, 0x88, 0x88, 0x30, 0x30, 0x30, 0xF8, 0x30, 0x08, + 0x88, 0x10, 0x70, 0x30, 0x88, 0xF0, 0xF0, 0x10, 0x70, 0x78, 0x00, 0x00, 0x60, 0x00, 0x18, 0x70, + 0x88, 0x10, 0x80, 0x08, 0xF8, 0x08, 0x88, 0x20, 0x88, 0x08, 0x30, 0x30, 0x30, 0xF8, 0x30, 0x00, + 0x70, 0x10, 0xF8, 0xF0, 0x08, 0xF0, 0x70, 0x20, 0x70, 0x70, 0x30, 0x10, 0x18, 0x00, 0x60, 0x60, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x70, 0x70, 0xF0, 0x78, 0xF0, 0xF8, 0xF8, 0x78, 0x88, 0xF8, 0x08, 0x48, 0x80, 0x88, 0x88, 0x70, + 0x88, 0x88, 0x88, 0x80, 0x88, 0x80, 0x80, 0x80, 0x88, 0x20, 0x08, 0x50, 0x80, 0xD8, 0xC8, 0x88, + 0xB8, 0x88, 0xF0, 0x80, 0x88, 0xF0, 0xF0, 0xB8, 0xF8, 0x20, 0x08, 0x60, 0x80, 0xA8, 0xA8, 0x88, + 0x80, 0xF8, 0x88, 0x80, 0x88, 0x80, 0x80, 0x88, 0x88, 0x20, 0x88, 0x50, 0x80, 0x88, 0x98, 0x88, + 0x70, 0x88, 0xF0, 0x78, 0xF0, 0xF8, 0x80, 0x78, 0x88, 0xF8, 0x70, 0x48, 0xF8, 0x88, 0x88, 0x70, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xF0, 0x70, 0xF0, 0x78, 0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0xF8, 0xF0, 0x80, 0xF0, 0x20, 0x00, + 0x88, 0x88, 0x88, 0x80, 0x20, 0x88, 0x88, 0x88, 0x50, 0x50, 0x10, 0xC0, 0xC0, 0x30, 0x50, 0x00, + 0xF0, 0x88, 0xF0, 0x70, 0x20, 0x88, 0x88, 0xA8, 0x20, 0x20, 0x20, 0xC0, 0x60, 0x30, 0x88, 0x00, + 0x80, 0x88, 0xA0, 0x08, 0x20, 0x88, 0x50, 0xD8, 0x50, 0x20, 0x40, 0xC0, 0x30, 0x30, 0x00, 0x00, + 0x80, 0x70, 0x98, 0xF0, 0x20, 0x78, 0x20, 0x88, 0x88, 0x20, 0xF8, 0xF0, 0x18, 0xF0, 0x00, 0xF8, + 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x30, 0x70, 0xF0, 0x78, 0xF0, 0xF8, 0xF8, 0x78, 0x88, 0x70, 0x08, 0x48, 0x40, 0x88, 0x88, 0x70, + 0x30, 0x88, 0x88, 0x80, 0x88, 0x80, 0x80, 0x80, 0x88, 0x20, 0x08, 0x50, 0x40, 0xD8, 0xC8, 0x88, + 0x18, 0x88, 0xF0, 0x80, 0x88, 0xF0, 0xF0, 0xB8, 0xF8, 0x20, 0x08, 0x60, 0x40, 0xA8, 0xA8, 0x88, + 0x00, 0xF8, 0x88, 0x80, 0x88, 0x80, 0x80, 0x88, 0x88, 0x20, 0x88, 0x50, 0x40, 0x88, 0x98, 0x88, + 0x00, 0x88, 0xF0, 0x78, 0xF0, 0xF8, 0x80, 0x78, 0x88, 0x70, 0x70, 0x48, 0x78, 0x88, 0x88, 0x70, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xF0, 0x70, 0xF0, 0x78, 0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0xF8, 0x70, 0x30, 0x70, 0x48, 0x00, + 0x88, 0x88, 0x88, 0x80, 0x20, 0x88, 0x88, 0x88, 0x50, 0x50, 0x10, 0x60, 0x30, 0x30, 0xB0, 0x00, + 0xF0, 0x88, 0xF0, 0x70, 0x20, 0x88, 0x88, 0xA8, 0x20, 0x20, 0x20, 0xC0, 0x30, 0x18, 0x00, 0x00, + 0x80, 0x88, 0xA0, 0x08, 0x20, 0x88, 0x50, 0xD8, 0x50, 0x20, 0x40, 0x60, 0x30, 0x30, 0x00, 0x00, + 0x80, 0x70, 0x98, 0xF0, 0x20, 0x78, 0x20, 0x88, 0x88, 0x20, 0xF8, 0x70, 0x30, 0x70, 0x00, 0x00, + 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + int32_t j = 0; + for(int32_t i = 0; i < ((128/8) * 64); i++) { + uint8_t n = data[i]; + pixels[j++] = this->getColor(n & 0x80); + pixels[j++] = this->getColor(n & 0x40); + pixels[j++] = this->getColor(n & 0x20); + pixels[j++] = this->getColor(n & 0x10); + pixels[j++] = this->getColor(n & 0x08); + pixels[j++] = this->getColor(n & 0x04); + pixels[j++] = this->getColor(n & 0x02); + pixels[j++] = this->getColor(n & 0x01); + } + + this->realTexture.setSize(128, 64); + this->realTexture.buffer(pixels); +} diff --git a/src/dawn/display/font/ExampleFont.hpp b/src/dawn/display/font/ExampleFont.hpp new file mode 100644 index 00000000..dc0b68e9 --- /dev/null +++ b/src/dawn/display/font/ExampleFont.hpp @@ -0,0 +1,22 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "BitmapFont.hpp" + +namespace Dawn { + class ExampleFont : public BitmapFont { + protected: + Texture realTexture; + TilesetGrid realTilesetGrid; + + struct Color getColor(uint8_t n); + + public: + ExampleFont(); + + void init(); + }; +} \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UILabel.cpp b/src/dawn/scene/components/ui/UILabel.cpp index 2b1d46b2..68580e10 100644 --- a/src/dawn/scene/components/ui/UILabel.cpp +++ b/src/dawn/scene/components/ui/UILabel.cpp @@ -12,7 +12,7 @@ UILabel::UILabel(SceneItem *item) : UIComponent(item), text(""), fontSize(10.0f), - font(nullptr), + font(&item->scene->game->renderManager.defaultFont), maxWidth(UI_LABEL_MAX_WIDTH_NONE) { } diff --git a/src/dawnhelloworld/scenes/HelloWorldScene.hpp b/src/dawnhelloworld/scenes/HelloWorldScene.hpp index 4c6a9e90..279a7a5d 100644 --- a/src/dawnhelloworld/scenes/HelloWorldScene.hpp +++ b/src/dawnhelloworld/scenes/HelloWorldScene.hpp @@ -6,17 +6,39 @@ #pragma once #include "scene/Scene.hpp" #include "prefabs/SimpleSpinningCubePrefab.hpp" +#include "scene/components/ui/UILabel.hpp" +#include "scene/components/ui/UIImage.hpp" +#include "display/font/BitmapFont.hpp" namespace Dawn { class HelloWorldScene : public Scene { protected: Camera *camera; SimpleSpinningCubePrefab *cube; + UICanvas *canvas; + UILabel *label; + UIImage *image; + BitmapFont font; void stage() override { camera = Camera::create(this); camera->transform->lookAt(glm::vec3(3, 3, 3), glm::vec3(0, 0, 0)); + cube = SimpleSpinningCubePrefab::create(this); + + canvas = UICanvas::create(this); + + auto imageItem = this->createSceneItem(); + image = imageItem->addComponent(); + image->color = COLOR_BLACK; + imageItem->transform.setParent(canvas->transform); + + auto labelItem = this->createSceneItem(); + label = labelItem->addComponent(); + labelItem->transform.setParent(canvas->transform); + label->text = "Hello World, how are you today? I hope you are doing well. I really like the fact I can ramble in my text for once."; + label->maxWidth = 220; + image->alignment = glm::vec4(0, 0, label->getContentWidth(), label->getContentHeight()); } std::vector getRequiredAssets() override { diff --git a/src/dawnopengl/display/RenderManager.cpp b/src/dawnopengl/display/RenderManager.cpp index e123b5e7..d38fc59a 100644 --- a/src/dawnopengl/display/RenderManager.cpp +++ b/src/dawnopengl/display/RenderManager.cpp @@ -18,15 +18,15 @@ RenderManager::RenderManager(DawnGame *game) : void RenderManager::init() { this->renderPipeline.init(); - this->simpleShader.compile(); this->uiShaderProgram.compile(); - // Prepare the initial values glEnable(GL_TEXTURE_2D); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthMask(GL_TRUE); glDepthFunc(GL_LESS); + + this->defaultFont.init(); } RenderTarget * RenderManager::getBackBuffer() { diff --git a/src/dawnopengl/display/RenderManager.hpp b/src/dawnopengl/display/RenderManager.hpp index 48ba524a..a312a047 100644 --- a/src/dawnopengl/display/RenderManager.hpp +++ b/src/dawnopengl/display/RenderManager.hpp @@ -9,6 +9,7 @@ #include "display/shader/SimpleTexturedShader.hpp" #include "display/shader/UIShaderProgram.hpp" #include "display/RenderPipeline.hpp" +#include "display/font/ExampleFont.hpp" namespace Dawn { class RenderManager : public IRenderManager { @@ -19,6 +20,7 @@ namespace Dawn { BackBufferRenderTarget backBuffer; SimpleTexturedShader simpleShader; UIShaderProgram uiShaderProgram; + ExampleFont defaultFont; /** * Construct a new RenderManager for a game instance.