Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
rework skin: port into spreadsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Estrol committed Feb 13, 2024
1 parent 744bdfd commit 4271000
Show file tree
Hide file tree
Showing 43 changed files with 1,497 additions and 558 deletions.
4 changes: 3 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_library(EstEngine STATIC ${HEADERS}
# Main Graphics
"src/Graphics/NativeWindow.cpp"
"src/Graphics/Renderer.cpp"
"src/Graphics/SpriteBatch.cpp"

# Screens
"src/Screens/Base.cpp"
Expand All @@ -26,7 +27,8 @@ add_library(EstEngine STATIC ${HEADERS}
"src/UI/Image.cpp"
"src/UI/Text.cpp"
"src/UI/TextBox.cpp"
"src/UI/Dialog.cpp"
"src/UI/Dialog.cpp"
"src/UI/Sprite.cpp"

# Vulkan backends
"src/Graphics/Backends/Vulkan/VulkanBackend.cpp"
Expand Down
2 changes: 1 addition & 1 deletion lib/include/Fonts/FontManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Fonts {

struct FontAtlas
{
std::unique_ptr<Graphics::Texture2D> Texture;
std::shared_ptr<Graphics::Texture2D> Texture;
std::vector<Glyph> Glyphs;
glm::vec2 TexSize;

Expand Down
13 changes: 8 additions & 5 deletions lib/include/Graphics/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "GraphicsBackendBase.h"
#include "GraphicsTexture2D.h"
#include <map>
#include <string>
#include <vector>

Expand Down Expand Up @@ -46,9 +47,9 @@ namespace Graphics {
Make sure delete the texture before Renderer is destroyed
*/

Texture2D *LoadTexture(std::filesystem::path path);
Texture2D *LoadTexture(const unsigned char *buf, size_t size);
Texture2D *LoadTexture(const unsigned char *pixbuf, uint32_t width, uint32_t height);
std::shared_ptr<Texture2D> LoadTexture(std::filesystem::path path);
std::shared_ptr<Texture2D> LoadTexture(const unsigned char *buf, size_t size);
std::shared_ptr<Texture2D> LoadTexture(const unsigned char *pixbuf, uint32_t width, uint32_t height);

Graphics::Backends::BlendHandle CreateBlendState(Graphics::Backends::TextureBlendInfo info);

Expand All @@ -65,8 +66,10 @@ namespace Graphics {
API m_API;
TextureSamplerInfo m_Sampler;

Backends::Base *m_Backend;
bool m_onFrame = false;
std::shared_ptr<Backends::Base> m_Backend;
bool m_onFrame = false;

std::map<std::string, std::shared_ptr<Texture2D>> m_textures_caches;
};
} // namespace Graphics

Expand Down
51 changes: 51 additions & 0 deletions lib/include/Graphics/SpriteBatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* MIT License
*
* Copyright (c) 2023 Estrol Mendex
* See the LICENSE file in the root of this project for details.
*/

#ifndef __SPRITEBATCH_H__
#define __SPRITEBATCH_H__

#include "GraphicsBackendBase.h"
#include "GraphicsTexture2D.h"

#include <map>
#include <vector>

namespace Graphics {
enum class SpriteBatchMode {
Deferred,
Immediate
};

enum class SpriteBatchSortMode {
BackToFront,
FrontToBack,
};

class SpriteBatch
{
public:
SpriteBatch();
~SpriteBatch();

void Begin();
void Draw(Backends::SubmitInfo info);
void Draw(std::vector<Backends::SubmitInfo> infos);
void End();

SpriteBatchMode RenderMode;
SpriteBatchSortMode SortMode;

private:
int m_CurrentQueue;

bool m_DrawingMode;
std::map<int, std::vector<Backends::SubmitInfo>> m_Queue;
const void *m_Image;
};
} // namespace Graphics

#endif
8 changes: 6 additions & 2 deletions lib/include/UI/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ namespace UI {
public:
Image();
Image(std::filesystem::path path);
Image(Graphics::Texture2D *texture);
Image(std::shared_ptr<Graphics::Texture2D> texture);
Image(const char *buf, size_t size);
Image(const char *pixbuf, uint32_t width, uint32_t height);

Rect GetImageSize() const;
ImageScaleMode ScaleMode = ImageScaleMode::Stretch;

void SetTexCoord(std::vector<glm::vec2> texCoord);

protected:
void OnDraw() override;
std::vector<glm::vec2> texCoord;

void CalculateSize() override;
void
CalculateSize() override;
};
} // namespace UI

Expand Down
61 changes: 61 additions & 0 deletions lib/include/UI/Sprite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* MIT License
*
* Copyright (c) 2023 Estrol Mendex
* See the LICENSE file in the root of this project for details.
*/

#ifndef __SPRITE_H_
#define __SPRITE_H_

#include <vector>

#include "Image.h"
#include <glm/glm.hpp>

namespace UI {
class Sprite : public Image
{
public:
Sprite();
Sprite(
std::filesystem::path path,
std::vector<std::vector<glm::vec2>> texCoords,
double frameTime);

Sprite(
std::shared_ptr<Graphics::Texture2D> texture,
std::vector<std::vector<glm::vec2>> texCoords,
double frameTime);

Sprite(
const char *buf,
size_t size,
std::vector<std::vector<glm::vec2>> texCoords,
double frameTime);

Sprite(
const char *pixbuf,
uint32_t width,
uint32_t height,
std::vector<std::vector<glm::vec2>> texCoords,
double frameTime);

void Draw(double delta);

void SetSpriteIndex(int index);
int GetSpriteIndex() const;

void SetFrameTime(double time);
double GetFrameTime() const;

protected:
void OnDraw() override;
double m_frameTime;
double m_elapsedFrameTime;
int m_spriteIndex;
std::vector<std::vector<glm::vec2>> m_texCoords;
};
} // namespace UI

#endif
6 changes: 4 additions & 2 deletions lib/include/UI/UIBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <Graphics/GraphicsBackendBase.h>
#include <Graphics/GraphicsTexture2D.h>
#include <Graphics/SpriteBatch.h>
#include <Graphics/Utils/Rect.h>
#include <Math/Color3.h>
#include <Math/UDim2.h>
Expand Down Expand Up @@ -59,7 +60,8 @@ namespace UI {
Vector2 AnchorPoint;
Vector4 CornerRadius;

Graphics::Backends::BlendHandle BlendState;
std::shared_ptr<Graphics::SpriteBatch> SpriteBatch;
Graphics::Backends::BlendHandle BlendState;

float Transparency;
float Rotation;
Expand Down Expand Up @@ -87,7 +89,7 @@ namespace UI {
std::vector<Graphics::Backends::Vertex> m_vertices;
std::vector<uint16_t> m_indices;

std::unique_ptr<Graphics::Texture2D> m_texture;
std::shared_ptr<Graphics::Texture2D> m_texture;
Graphics::Texture2D *m_texturePtr = nullptr;
RenderMode m_renderMode = RenderMode::Normal;

Expand Down
12 changes: 12 additions & 0 deletions lib/src/Audio/AudioSample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ void Sample::Load(std::filesystem::path path)
fs.close();

Preload();

SetVolume(100.0f);
SetPan(0.0f);
SetRate(1.0f);
}

void Sample::Load(const char *buf, size_t size)
Expand All @@ -59,6 +63,10 @@ void Sample::Load(const char *buf, size_t size)
memcpy(m_Buffer.data(), buf, size);

Preload();

SetVolume(100.0f);
SetPan(0.0f);
SetRate(1.0f);
}

void Sample::Load(ECHANDLE encoder)
Expand All @@ -73,6 +81,10 @@ void Sample::Load(ECHANDLE encoder)
if (result != EST_OK) {
throw Exceptions::EstException("Failed to load audio sample: %s", EST_GetError());
}

SetVolume(100.0f);
SetPan(0.0f);
SetRate(1.0f);
}

void Sample::Play()
Expand Down
13 changes: 6 additions & 7 deletions lib/src/Fonts/FontManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ FontAtlas *FontManager::LoadFont(FontLoadBufferInfo &info)

glyph.Rect = glm::vec4(x0, y0, x1, y1);

glyph.UV[0] = glm::vec2(glyph.Top.x / tex_width, glyph.Top.y / tex_height);
glyph.UV[1] = glm::vec2(glyph.Bottom.x / tex_width, glyph.Top.y / tex_height);
glyph.UV[2] = glm::vec2(glyph.Bottom.x / tex_width, glyph.Bottom.y / tex_height);
glyph.UV[3] = glm::vec2(glyph.Top.x / tex_width, glyph.Bottom.y / tex_height);
glyph.UV[0] = glm::vec2(glyph.Top.x / tex_width, glyph.Top.y / tex_height); // Top left
glyph.UV[1] = glm::vec2(glyph.Bottom.x / tex_width, glyph.Top.y / tex_height); // Top right
glyph.UV[2] = glm::vec2(glyph.Bottom.x / tex_width, glyph.Bottom.y / tex_height); // Bottom right
glyph.UV[3] = glm::vec2(glyph.Top.x / tex_width, glyph.Bottom.y / tex_height); // Bottom left

if (c == '?') {
Invalid = glyph;
Expand All @@ -223,13 +223,12 @@ FontAtlas *FontManager::LoadFont(FontLoadBufferInfo &info)
rgba_data[i * 4 + 3] = pixel & 0xFF; // Alpha
}

auto tex = Graphics::Renderer::Get()->LoadTexture(
auto atlas = std::make_unique<FontAtlas>();
atlas->Texture = Graphics::Renderer::Get()->LoadTexture(
(const unsigned char *)rgba_data.data(),
tex_width,
tex_height);

auto atlas = std::make_unique<FontAtlas>();
atlas->Texture = std::unique_ptr<Graphics::Texture2D>(tex);
atlas->Glyphs = std::move(glyphs);
atlas->NewlineHeight = faceHeight;
atlas->TexSize = glm::vec2(tex_width, tex_height);
Expand Down
8 changes: 8 additions & 0 deletions lib/src/Graphics/Backends/Vulkan/VulkanBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,10 @@ void Vulkan::Push(SubmitInfo &info)
return;
}

if (info.uiSize.x == 0 || info.uiSize.y == 0) {
return;
}

uint32_t maxSize = (m_DrawData.vertexSize + static_cast<uint32_t>(info.vertices.size())) * sizeof(info.vertices[0]);
uint32_t inMaxSize = (m_DrawData.indiceSize + static_cast<uint32_t>(info.indices.size())) * sizeof(info.indices[0]);

Expand Down Expand Up @@ -1344,6 +1348,10 @@ void Vulkan::Push(std::vector<SubmitInfo> &infos)
return;
}

if (infos[0].uiSize.x == 0 || infos[0].uiSize.y == 0) {
return;
}

size_t size = infos.size();

uint32_t maxSize = (m_DrawData.vertexSize + static_cast<uint32_t>(infos[0].vertices.size() * size)) * sizeof(infos[0].vertices[0]);
Expand Down
Loading

0 comments on commit 4271000

Please # to comment.