mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 22:53:34 +00:00
adding texture atlas class
This commit is contained in:
12
README.md
12
README.md
@@ -5,8 +5,7 @@ A rewrite of School 42's MiniLibX using SDL2 and Vulkan. The goal of this versio
|
|||||||
# Installation
|
# Installation
|
||||||
|
|
||||||
## Linux
|
## Linux
|
||||||
|
Dependances :
|
||||||
1. Necessary packages :
|
|
||||||
|
|
||||||
For Ubuntu/Debian
|
For Ubuntu/Debian
|
||||||
```bash
|
```bash
|
||||||
@@ -19,7 +18,12 @@ For Arch based distros
|
|||||||
~ sudo pacman -S sdl2
|
~ sudo pacman -S sdl2
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Get MacroLibX
|
### MacOS
|
||||||
|
Dependances :
|
||||||
|
* [MoltenVK](https://github.com/KhronosGroup/MoltenVK/)
|
||||||
|
* SDL2 `brew install SDL2`
|
||||||
|
|
||||||
|
# Get MacroLibX
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
~ git clone https://github.com/420verfl0w/MacroLibX.git
|
~ git clone https://github.com/420verfl0w/MacroLibX.git
|
||||||
@@ -27,7 +31,7 @@ For Arch based distros
|
|||||||
~ make
|
~ make
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Compile your project
|
# Compile your project
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
clang myApp.c MacroLibX/libmlx.so -lSDL2
|
clang myApp.c MacroLibX/libmlx.so -lSDL2
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */
|
/* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */
|
||||||
/* Updated: 2023/04/02 23:50:31 by maldavid ### ########.fr */
|
/* Updated: 2023/04/07 14:23:01 by maldavid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ namespace mlx
|
|||||||
if(!_cpu_map->isMapped())
|
if(!_cpu_map->isMapped())
|
||||||
_cpu_map->mapMem(&_cpu_map_adress);
|
_cpu_map->mapMem(&_cpu_map_adress);
|
||||||
if(_cpu_map_adress == nullptr)
|
if(_cpu_map_adress == nullptr)
|
||||||
core::error::report(e_kind::fatal_error, "Texture : CPU memory mappind failed");
|
core::error::report(e_kind::fatal_error, "Texture : CPU memory mapping failed");
|
||||||
return _cpu_map_adress;
|
return _cpu_map_adress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* texture.h :+: :+: :+: */
|
/* texture.h :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: maldavid <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
|
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
|
||||||
/* Updated: 2023/04/02 22:36:52 by maldavid ### ########.fr */
|
/* Updated: 2023/04/07 16:39:35 by maldavid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <array>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <renderer/images/vk_image.h>
|
#include <renderer/images/vk_image.h>
|
||||||
#include <renderer/descriptors/vk_descriptor_set.h>
|
#include <renderer/descriptors/vk_descriptor_set.h>
|
||||||
|
|||||||
75
src/renderer/images/texture_atlas.cpp
git.filemode.normal_file
75
src/renderer/images/texture_atlas.cpp
git.filemode.normal_file
@@ -0,0 +1,75 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* texture_atlas.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/07 16:40:09 by maldavid #+# #+# */
|
||||||
|
/* Updated: 2023/04/07 17:07:46 by maldavid ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <renderer/images/texture_atlas.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
void TextureAtlas::create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format, uint32_t render_width, uint32_t rendre_height)
|
||||||
|
{
|
||||||
|
Image::create(width, height, format,
|
||||||
|
VK_IMAGE_TILING_OPTIMAL,
|
||||||
|
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
|
||||||
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
||||||
|
);
|
||||||
|
|
||||||
|
Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||||
|
Image::createSampler();
|
||||||
|
|
||||||
|
std::vector<Vertex> vertexData = {
|
||||||
|
{{0, 0}, {1.f, 1.f, 1.f, 1.f}, {0.0f, 0.0f}},
|
||||||
|
{{render_width, 0}, {1.f, 1.f, 1.f, 1.f}, {1.0f, 0.0f}},
|
||||||
|
{{render_width, render_height}, {1.f, 1.f, 1.f, 1.f}, {1.0f, 1.0f}},
|
||||||
|
{{0, render_height}, {1.f, 1.f, 1.f, 1.f}, {0.0f, 1.0f}}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<uint16_t> indexData = { 0, 1, 2, 2, 3, 0 };
|
||||||
|
|
||||||
|
_vbo.create(sizeof(Vertex) * vertexData.size(), vertexData.data());
|
||||||
|
_ibo.create(sizeof(uint16_t) * indexData.size(), indexData.data());
|
||||||
|
|
||||||
|
if(pixels != nullptr)
|
||||||
|
{
|
||||||
|
Buffer staging_buffer;
|
||||||
|
std::size_t size = width * height * (format == VK_FORMAT_R32G32B32A32_SFLOAT ? 16 : 4);
|
||||||
|
staging_buffer.create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, pixels);
|
||||||
|
Image::copyFromBuffer(staging_buffer);
|
||||||
|
staging_buffer.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureAtlas::render(Renderer& renderer, int x, int y, std::array<glm::vec2, 4> uv)
|
||||||
|
{
|
||||||
|
auto cmd = renderer.getActiveCmdBuffer().get();
|
||||||
|
|
||||||
|
std::vector<Vertex> vertexData = {
|
||||||
|
{{0, 0}, {1.f, 1.f, 1.f, 1.f}, uv[0]},
|
||||||
|
{{render_width, 0}, {1.f, 1.f, 1.f, 1.f}, uv[1]},
|
||||||
|
{{render_width, render_height}, {1.f, 1.f, 1.f, 1.f}, uv[2]},
|
||||||
|
{{0, render_height}, {1.f, 1.f, 1.f, 1.f}, uv[3]}
|
||||||
|
};
|
||||||
|
_vbo.setData(sizeof(Vertex) * vertexData.size(), vertexData.data());
|
||||||
|
|
||||||
|
_vbo.bind(renderer);
|
||||||
|
_ibo.bind(renderer);
|
||||||
|
glm::vec2 translate(x, y);
|
||||||
|
vkCmdPushConstants(cmd, renderer.getPipeline().getPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(translate), &translate);
|
||||||
|
vkCmdDrawIndexed(cmd, static_cast<uint32_t>(_ibo.getSize() / sizeof(uint16_t)), 1, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureAtlas::destroy() noexcept
|
||||||
|
{
|
||||||
|
Image::destroy();
|
||||||
|
_vbo.destroy();
|
||||||
|
_ibo.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
46
src/renderer/images/texture_atlas.h
git.filemode.normal_file
46
src/renderer/images/texture_atlas.h
git.filemode.normal_file
@@ -0,0 +1,46 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* texture_atlas.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */
|
||||||
|
/* Updated: 2023/04/07 16:44:43 by maldavid ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef __MLX_TEXTURE_ATLAS__
|
||||||
|
#define __MLX_TEXTURE_ATLAS__
|
||||||
|
|
||||||
|
#include <renderer/images/texture.h>
|
||||||
|
#include <array>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
class TextureAtlas : public Image
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TextureAtlas() = default;
|
||||||
|
|
||||||
|
void create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format, uint32_t render_width, uint32_t rendre_height);
|
||||||
|
void render(class Renderer& renderer, int x, int y, std::array<glm::vec2, 4> uv);
|
||||||
|
void destroy() noexcept override;
|
||||||
|
|
||||||
|
inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); }
|
||||||
|
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
|
||||||
|
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); }
|
||||||
|
|
||||||
|
~TextureAtlas() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
VBO _vbo;
|
||||||
|
C_IBO _ibo;
|
||||||
|
DescriptorSet _set;
|
||||||
|
uint32_t _render_width;
|
||||||
|
uint32_t _render_height;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -6,24 +6,33 @@
|
|||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
|
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
|
||||||
/* Updated: 2023/04/07 00:13:50 by maldavid ### ########.fr */
|
/* Updated: 2023/04/07 17:24:33 by maldavid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <renderer/text_pipeline.h>
|
#include <renderer/text_pipeline.h>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#define STB_TRUETYPE_IMPLEMENTATION
|
#define STB_TRUETYPE_IMPLEMENTATION
|
||||||
#include <std_truetype.h>
|
#include <stb_truetype.h>
|
||||||
|
|
||||||
namespace mlx
|
namespace mlx
|
||||||
{
|
{
|
||||||
void TextPutPipeline::init(Renderer& renderer) noexcept
|
static uint8_t ttf_buffer[1 << 20];
|
||||||
{
|
static uint8_t tmp_bitmap[512 * 512];
|
||||||
|
|
||||||
|
void TextPutPipeline::init(Renderer* renderer) noexcept
|
||||||
|
{
|
||||||
|
_renderer = renderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextPutPipeline::put(int x, int y, int color, std::string str)
|
void TextPutPipeline::put(int x, int y, int color, std::string str)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextPutPipeline::destroy() noexcept
|
||||||
|
{
|
||||||
|
_atlas.destroy();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */
|
/* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */
|
||||||
/* Updated: 2023/04/07 00:11:25 by maldavid ### ########.fr */
|
/* Updated: 2023/04/07 17:11:08 by maldavid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
#define __MLX_TEXT_PIPELINE__
|
#define __MLX_TEXT_PIPELINE__
|
||||||
|
|
||||||
#include <renderer/renderer.h>
|
#include <renderer/renderer.h>
|
||||||
#include <renderer/images/texture.h>
|
#include <renderer/images/texture_atlas.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <stb_truetype.h>
|
#include <stb_truetype.h>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@@ -26,16 +26,16 @@ namespace mlx
|
|||||||
public:
|
public:
|
||||||
TextPutPipeline() = default;
|
TextPutPipeline() = default;
|
||||||
|
|
||||||
void init(Renderer& renderer) noexcept;
|
void init(Renderer* renderer) noexcept;
|
||||||
|
|
||||||
void put(int x, int y, int color, std::string str);
|
void put(int x, int y, int color, std::string str);
|
||||||
|
void destroy() noexcept;
|
||||||
|
|
||||||
~TextPutPipeline() = default;
|
~TextPutPipeline() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t _ttf_buffer[1 << 20];
|
|
||||||
stbtt_bakedchar _cdata[96];
|
stbtt_bakedchar _cdata[96];
|
||||||
Texture _atlas;
|
TextureAtlas _atlas;
|
||||||
|
Renderer* _renderer = nullptr;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
BIN
src/utils/opensans-regular.ttf
git.filemode.normal_file
BIN
src/utils/opensans-regular.ttf
git.filemode.normal_file
Binary file not shown.
27702
third_party/stb_truetype.h
vendored
27702
third_party/stb_truetype.h
vendored
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user