Font working
This commit is contained in:
		@@ -33,7 +33,7 @@ float_t TrueTypeFont::getSpaceSize(float_t fontSize) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t TrueTypeFont::getInitialLineHeight(float_t fontSize) {
 | 
			
		||||
  return mathRoundFloat(this->getScale(fontSize) * 11.0f);
 | 
			
		||||
  return 8.0f;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void TrueTypeFont::buffer(
 | 
			
		||||
@@ -185,7 +185,7 @@ void TrueTypeFont::draw(Mesh &mesh, int32_t startchar, int32_t length) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t TrueTypeFont::getLineHeight(float_t fontSize) {
 | 
			
		||||
  return mathRoundFloat(this->getScale(fontSize) * 14.0f);
 | 
			
		||||
  return 13.0f;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t TrueTypeFont::getDefaultFontSize() {
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,7 @@ namespace Dawn {
 | 
			
		||||
  #define TRUETYPE_NUM_CHARS 96
 | 
			
		||||
 | 
			
		||||
  /** Refer to STBTT docs for OpenGL Fill Mode v d3d Fill Modes */
 | 
			
		||||
  #define TRUETYPE_FILL_MODE 1
 | 
			
		||||
  #define TRUETYPE_FILL_MODE 0
 | 
			
		||||
 | 
			
		||||
  typedef stbtt_bakedchar truetypechar_t;
 | 
			
		||||
  typedef stbtt_aligned_quad truetypequad_t;
 | 
			
		||||
 
 | 
			
		||||
@@ -101,6 +101,6 @@ namespace Dawn {
 | 
			
		||||
       * @param parameter parameter to set the texture on to.
 | 
			
		||||
       * @param texture Texture to bind to the parameter.
 | 
			
		||||
       */
 | 
			
		||||
      virtual void setTexture(T parameter, std::shared_ptr<Texture> texture)=0;
 | 
			
		||||
      virtual void setTexture(T parameter, Texture *texture) = 0;
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
@@ -26,7 +26,7 @@ namespace Dawn {
 | 
			
		||||
      std::map<shaderparameter_t, bool_t> boolValues;
 | 
			
		||||
      std::map<shaderparameter_t, glm::mat4> matrixValues;
 | 
			
		||||
      std::map<shaderparameter_t, glm::vec3> vec3Values;
 | 
			
		||||
      std::map<shaderparameter_t, std::shared_ptr<Texture>> textureValues;
 | 
			
		||||
      std::map<shaderparameter_t, Texture*> textureValues;
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Material component constructor. 
 | 
			
		||||
 
 | 
			
		||||
@@ -7,5 +7,6 @@
 | 
			
		||||
target_sources(${DAWN_TARGET_NAME}
 | 
			
		||||
  PRIVATE
 | 
			
		||||
    UIComponent.cpp
 | 
			
		||||
    UILabel.cpp
 | 
			
		||||
    UISprite.cpp
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										57
									
								
								src/dawn/ui/UILabel.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/dawn/ui/UILabel.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,57 @@
 | 
			
		||||
// Copyright (c) 2022 Dominic Masters
 | 
			
		||||
// 
 | 
			
		||||
// This software is released under the MIT License.
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#include "UILabel.hpp"
 | 
			
		||||
 | 
			
		||||
using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
UILabel::UILabel(UICanvas &canvas) : UIComponent(canvas) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabel::updateMesh() {
 | 
			
		||||
  if(!this->needsRebuffering) return;
 | 
			
		||||
  if(this->font == nullptr) return;
 | 
			
		||||
 | 
			
		||||
  this->font->buffer(
 | 
			
		||||
    this->text,
 | 
			
		||||
    this->fontSize,
 | 
			
		||||
    -1,
 | 
			
		||||
    this->mesh,
 | 
			
		||||
    &this->measure
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  this->needsRebuffering = false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::string UILabel::getText() {
 | 
			
		||||
  return this->text;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabel::setFont(Font *font) {
 | 
			
		||||
  this->font = font;
 | 
			
		||||
  this->needsRebuffering = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabel::setText(std::string text) {
 | 
			
		||||
  this->text = text;
 | 
			
		||||
  this->needsRebuffering = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabel::setFontSize(float_t fontSize) {
 | 
			
		||||
  this->fontSize = fontSize;
 | 
			
		||||
  this->needsRebuffering = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabel::drawSelf(UIShader &shader, glm::mat4 selfTransform) {
 | 
			
		||||
  if(this->font == nullptr) return;
 | 
			
		||||
 | 
			
		||||
  this->updateMesh();
 | 
			
		||||
  shader.setUIColor(this->textColor);
 | 
			
		||||
  shader.setUIModel(selfTransform);
 | 
			
		||||
  shader.setUITexture(&this->font->getTexture());
 | 
			
		||||
  
 | 
			
		||||
  this->font->draw(this->mesh, 0, -1);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								src/dawn/ui/UILabel.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/dawn/ui/UILabel.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
// Copyright (c) 2022 Dominic Masters
 | 
			
		||||
// 
 | 
			
		||||
// This software is released under the MIT License.
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "UIComponent.hpp"
 | 
			
		||||
#include "display/mesh/QuadMesh.hpp"
 | 
			
		||||
#include "display/font/Font.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  class UILabel : public UIComponent {
 | 
			
		||||
    private:
 | 
			
		||||
      Mesh mesh;
 | 
			
		||||
      struct FontMeasure measure;
 | 
			
		||||
      bool_t needsRebuffering = true;
 | 
			
		||||
      Font *font = nullptr;
 | 
			
		||||
      std::string text = "";
 | 
			
		||||
      float_t fontSize = 10.0f;
 | 
			
		||||
 | 
			
		||||
      void updateMesh();
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      struct Color textColor = COLOR_MAGENTA;
 | 
			
		||||
      
 | 
			
		||||
      UILabel(UICanvas &canvas);
 | 
			
		||||
 | 
			
		||||
      std::string getText();
 | 
			
		||||
 | 
			
		||||
      void setFont(Font *font);
 | 
			
		||||
      void setText(std::string text);
 | 
			
		||||
      void setFontSize(float_t fontSize);
 | 
			
		||||
 | 
			
		||||
      void drawSelf(UIShader &shader, glm::mat4 selfTransform);
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
@@ -29,6 +29,7 @@ void UISprite::drawSelf(UIShader &uiShader, glm::mat4 selfTransform) {
 | 
			
		||||
  uiShader.setUIModel(selfTransform);
 | 
			
		||||
  uiShader.setUIModel(glm::mat4(1.0f));
 | 
			
		||||
  uiShader.setUIColor(COLOR_WHITE);
 | 
			
		||||
  uiShader.setUITexture(this->texture);
 | 
			
		||||
 | 
			
		||||
  this->mesh.draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
 | 
			
		||||
}
 | 
			
		||||
@@ -6,6 +6,7 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "UIComponent.hpp"
 | 
			
		||||
#include "display/mesh/QuadMesh.hpp"
 | 
			
		||||
#include "display/Texture.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  class UISprite : public UIComponent {
 | 
			
		||||
@@ -14,6 +15,7 @@ namespace Dawn {
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      Mesh mesh;
 | 
			
		||||
      Texture *texture;
 | 
			
		||||
 | 
			
		||||
      UISprite(UICanvas &canvas);
 | 
			
		||||
      
 | 
			
		||||
 
 | 
			
		||||
@@ -87,7 +87,7 @@ void Shader::setVector3(shaderparameter_t uniform, glm::vec3 vector) {
 | 
			
		||||
 | 
			
		||||
void Shader::setTexture(
 | 
			
		||||
  shaderparameter_t param,
 | 
			
		||||
  std::shared_ptr<Texture> texture
 | 
			
		||||
  Texture *texture
 | 
			
		||||
) {
 | 
			
		||||
  if(texture == nullptr || !texture->isReady()) {
 | 
			
		||||
    this->bindTexture(param, nullptr);
 | 
			
		||||
 
 | 
			
		||||
@@ -51,7 +51,7 @@ namespace Dawn {
 | 
			
		||||
       */
 | 
			
		||||
      virtual void bindTexture(
 | 
			
		||||
        shaderparameter_t param,
 | 
			
		||||
        std::shared_ptr<Texture> texture
 | 
			
		||||
        Texture *texture
 | 
			
		||||
      ) = 0;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -75,10 +75,7 @@ namespace Dawn {
 | 
			
		||||
      void setBoolean(shaderparameter_t parameter, bool_t value) override;
 | 
			
		||||
      void setColor(shaderparameter_t parameter, struct Color color) override;
 | 
			
		||||
      void setVector3(shaderparameter_t parameter, glm::vec3 vector) override;
 | 
			
		||||
      void setTexture(
 | 
			
		||||
        shaderparameter_t parameter,
 | 
			
		||||
        std::shared_ptr<Texture> texture
 | 
			
		||||
      ) override;
 | 
			
		||||
      void setTexture(shaderparameter_t parameter, Texture *texture) override;
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Destroys and deletes the shader from the GPU.
 | 
			
		||||
 
 | 
			
		||||
@@ -43,7 +43,7 @@ namespace Dawn {
 | 
			
		||||
 | 
			
		||||
      void bindTexture(
 | 
			
		||||
        shaderparameter_t param,
 | 
			
		||||
        std::shared_ptr<Texture> texture
 | 
			
		||||
        Texture *texture
 | 
			
		||||
      ) override {
 | 
			
		||||
        if(texture == nullptr) {
 | 
			
		||||
          this->setBoolean(this->paramHasTexture, false);
 | 
			
		||||
 
 | 
			
		||||
@@ -43,7 +43,7 @@ namespace Dawn {
 | 
			
		||||
 | 
			
		||||
      void bindTexture(
 | 
			
		||||
        shaderparameter_t param,
 | 
			
		||||
        std::shared_ptr<Texture> texture
 | 
			
		||||
        Texture *texture
 | 
			
		||||
      ) override {
 | 
			
		||||
        if(texture == nullptr) {
 | 
			
		||||
          this->setBoolean(this->paramHasTexture, false);
 | 
			
		||||
@@ -108,7 +108,7 @@ namespace Dawn {
 | 
			
		||||
        this->setMatrix(this->paramModel, model);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      void setUITexture(std::shared_ptr<Texture> texture) {
 | 
			
		||||
      void setUITexture(Texture *texture) {
 | 
			
		||||
        this->bindTexture(this->paramTexture, texture);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,8 @@ using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
std::shared_ptr<TrueTypeAsset> asset;
 | 
			
		||||
std::shared_ptr<TextureAsset> assetTexture;
 | 
			
		||||
std::shared_ptr<UILabel> label;
 | 
			
		||||
float_t fs = 1.0f;
 | 
			
		||||
 | 
			
		||||
DawnGame::DawnGame(DawnHost &host) :
 | 
			
		||||
  host(host),
 | 
			
		||||
@@ -25,16 +27,9 @@ int32_t DawnGame::init() {
 | 
			
		||||
 | 
			
		||||
  auto cameraObject = this->scene->createSceneItem();
 | 
			
		||||
  auto camera = cameraObject->addComponent<Camera>();
 | 
			
		||||
  camera->transform.lookAt(glm::vec3(5, 5, 5), glm::vec3(0, 0, 0));
 | 
			
		||||
  camera->transform.lookAt(glm::vec3(50, 50, 50), glm::vec3(0, 0, 0));
 | 
			
		||||
  
 | 
			
		||||
  // auto canvas = UICanvas::createCanvas(this->scene);
 | 
			
		||||
  // auto test = canvas->addElement<UISprite>();
 | 
			
		||||
  // test->setTransform(
 | 
			
		||||
  //   UI_COMPONENT_ALIGN_START,
 | 
			
		||||
  //   UI_COMPONENT_ALIGN_START,
 | 
			
		||||
  //   glm::vec4(0, 0, 32, 32),
 | 
			
		||||
  //   0
 | 
			
		||||
  // );
 | 
			
		||||
  auto canvas = UICanvas::createCanvas(this->scene);
 | 
			
		||||
  
 | 
			
		||||
  asset = this->assetManager.load<TrueTypeAsset>("truetype_ark");
 | 
			
		||||
  assetTexture = this->assetManager.load<TextureAsset>("texture_test");
 | 
			
		||||
@@ -42,17 +37,25 @@ int32_t DawnGame::init() {
 | 
			
		||||
    this->assetManager.update();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // auto sprite = canvas->addElement<UISprite>();
 | 
			
		||||
  // sprite->setTransform(
 | 
			
		||||
  //   UI_COMPONENT_ALIGN_START,
 | 
			
		||||
  //   UI_COMPONENT_ALIGN_START,
 | 
			
		||||
  //   glm::vec4(0, 0, 200, 200),
 | 
			
		||||
  //   0
 | 
			
		||||
  // );
 | 
			
		||||
  // sprite->texture = &asset->font.getTexture();
 | 
			
		||||
 | 
			
		||||
  auto text = this->scene->createSceneItem();
 | 
			
		||||
  auto meshRenderer = text->addComponent<MeshRenderer>();
 | 
			
		||||
  auto material = text->addComponent<Material>();
 | 
			
		||||
  meshRenderer->mesh = std::make_shared<Mesh>();
 | 
			
		||||
  // meshRenderer->mesh->createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
 | 
			
		||||
  // QuadMesh::bufferQuadMesh(*meshRenderer->mesh, glm::vec2(0, 0), glm::vec2(0, 0), glm::vec2(1, 1), glm::vec2(1, 1), 0, 0);
 | 
			
		||||
 | 
			
		||||
  struct FontMeasure measure;
 | 
			
		||||
  asset->font.buffer("Test", 16.0f, -1, *meshRenderer->mesh, &measure);
 | 
			
		||||
  material->textureValues[material->getShader()->getParameterByName("u_Text")] = std::shared_ptr<Texture>(&asset->font.getTexture());
 | 
			
		||||
  label = canvas->addElement<UILabel>();
 | 
			
		||||
  label->setTransform(
 | 
			
		||||
    UI_COMPONENT_ALIGN_START,
 | 
			
		||||
    UI_COMPONENT_ALIGN_START,
 | 
			
		||||
    glm::vec4(0, 0, 200, 200),
 | 
			
		||||
    0
 | 
			
		||||
  );
 | 
			
		||||
  label->setText("Hello World\nHow are you today?");
 | 
			
		||||
  label->setFont(&asset->font);
 | 
			
		||||
 | 
			
		||||
  return DAWN_GAME_INIT_RESULT_SUCCESS;
 | 
			
		||||
}
 | 
			
		||||
@@ -61,6 +64,8 @@ int32_t DawnGame::update(float_t delta) {
 | 
			
		||||
  this->assetManager.update();
 | 
			
		||||
 | 
			
		||||
  if(this->scene != nullptr) this->scene->update();
 | 
			
		||||
 | 
			
		||||
  label->setFontSize(fs += delta);
 | 
			
		||||
  
 | 
			
		||||
  this->renderManager.update();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -6,5 +6,6 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "game/DawnGame.hpp"
 | 
			
		||||
#include "scene/components/Components.hpp"
 | 
			
		||||
#include "ui/UILabel.hpp"
 | 
			
		||||
#include "ui/UISprite.hpp"
 | 
			
		||||
#include "asset/assets/TrueTypeAsset.hpp"
 | 
			
		||||
		Reference in New Issue
	
	Block a user