FPS Label Implemented

This commit is contained in:
2023-03-21 07:50:21 -07:00
parent 9ea6d600b4
commit cc502591bd
4 changed files with 44 additions and 27 deletions

View File

@ -44,7 +44,10 @@ void BitmapFont::buffer(
size_t j = 0; size_t j = 0;
size_t wordStart = 0; size_t wordStart = 0;
glm::vec2 xy0(0, 0); glm::vec2 xy0(0, 0);
glm::vec2 tileSize = glm::vec2(tileset->getTileWidth(), tileset->getTileHeight()); glm::vec2 tileSize =
glm::vec2(tileset->getTileWidth(), tileset->getTileHeight()) *
(fontSize / this->getDefaultFontSize())
;
// Buffer quads // Buffer quads
while(c = text[i++]) { while(c = text[i++]) {
@ -139,9 +142,9 @@ void BitmapFont::draw(Mesh *mesh, int32_t start, int32_t len) {
} }
float_t BitmapFont::getLineHeight(float_t fontSize) { float_t BitmapFont::getLineHeight(float_t fontSize) {
return 16.0f; return tileset->getTileHeight();
} }
float_t BitmapFont::getDefaultFontSize() { float_t BitmapFont::getDefaultFontSize() {
return 16.0f; return tileset->getTileHeight();
} }

View File

@ -0,0 +1,32 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "prefab/SceneItemPrefab.hpp"
#include "scene/components/ui/UILabel.hpp"
namespace Dawn {
class FPSLabel : public SceneItemPrefab<FPSLabel>, public StateOwner {
public:
static std::vector<Asset*> prefabAssets(AssetManager *man) {
return {};
}
UILabel *label;
FPSLabel(Scene *s, sceneitemid_t i) : SceneItemPrefab(s, i) {}
void prefabInit(AssetManager *man) override {
label = this->addComponent<UILabel>();
label->text = "No Frame";
useEvent([&](float_t delta){
std::string strFps = std::to_string((int32_t)(1.0f / delta));
std::string strTick = std::to_string((int32_t)(delta * 1000.0f));
label->text = strFps + "FPS (" + strTick + "ms)";
}, scene->eventSceneUnpausedUpdate);
}
};
}

View File

@ -48,7 +48,7 @@ int32_t DawnHost::init(DawnGame *game) {
// Load GLAD // Load GLAD
glfwMakeContextCurrent(this->data->window); glfwMakeContextCurrent(this->data->window);
glfwSwapInterval(0); glfwSwapInterval(1);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
// Override the defaults // Override the defaults

View File

@ -6,7 +6,7 @@
#pragma once #pragma once
#include "scene/Scene.hpp" #include "scene/Scene.hpp"
#include "prefabs/SimpleSpinningCubePrefab.hpp" #include "prefabs/SimpleSpinningCubePrefab.hpp"
#include "scene/components/ui/UILabel.hpp" #include "prefabs/FPSLabel.hpp"
#include "scene/components/ui/UIImage.hpp" #include "scene/components/ui/UIImage.hpp"
#include "display/font/BitmapFont.hpp" #include "display/font/BitmapFont.hpp"
@ -17,38 +17,20 @@ namespace Dawn {
SimpleSpinningCubePrefab *cube; SimpleSpinningCubePrefab *cube;
UICanvas *canvas; UICanvas *canvas;
UILabel *label; UILabel *label;
UIImage *image;
BitmapFont font;
Texture test;
void stage() override { void stage() override {
camera = Camera::create(this); camera = Camera::create(this);
camera->transform->lookAt(glm::vec3(3, 3, 3), glm::vec3(0, 0, 0)); camera->transform->lookAt(glm::vec3(3, 3, 3), glm::vec3(0, 0, 0));
cube = SimpleSpinningCubePrefab::create(this); cube = SimpleSpinningCubePrefab::create(this);
cube->material->texture = &test;
struct Color colors[] = {
COLOR_RED, COLOR_BLUE, COLOR_GREEN,
COLOR_MAGENTA, COLOR_DARK_GREY, COLOR_WHITE,
COLOR_BLACK, COLOR_CORNFLOWER_BLUE, COLOR_RED
};
test.setSize(3, 3);
test.buffer(colors);
canvas = UICanvas::create(this); canvas = UICanvas::create(this);
auto imageItem = this->createSceneItem(); auto labelItem = FPSLabel::create(this);
image = imageItem->addComponent<UIImage>();
image->color = COLOR_BLACK;
imageItem->transform.setParent(canvas->transform);
auto labelItem = this->createSceneItem();
label = labelItem->addComponent<UILabel>();
labelItem->transform.setParent(canvas->transform); 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."; labelItem->label->alignX = UI_COMPONENT_ALIGN_END;
label->maxWidth = 220; labelItem->label->alignment = glm::vec4(0, 0, 0, 0);
image->alignment = glm::vec4(0, 0, label->getContentWidth(), label->getContentHeight()); labelItem->label->fontSize = 16;
} }
std::vector<Asset*> getRequiredAssets() override { std::vector<Asset*> getRequiredAssets() override {