Skip to content

Commit

Permalink
yes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Oct 18, 2024
1 parent 0ea0cde commit 632117a
Show file tree
Hide file tree
Showing 19 changed files with 108 additions and 173 deletions.
72 changes: 10 additions & 62 deletions example/main.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
/* Updated: 2024/10/03 06:39:01 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdio.h>
#include "../includes/mlx.h"

/*
typedef struct
{
void* mlx;
Expand All @@ -24,8 +11,6 @@ typedef struct
void* img;
} mlx_t;

//void* img = NULL;
int update(void* param)
{
static int i = 0;
Expand All @@ -34,34 +19,35 @@ int update(void* param)
if(i == 200)
mlx_clear_window(mlx->mlx, mlx->win);

// if(img)
// mlx_destroy_image(mlx->mlx,img);
// img = mlx_new_image(mlx->mlx, 800, 800);
// mlx_set_image_pixel(mlx->mlx, img, 4, 4, 0xFF00FF00);
// mlx_put_image_to_window(mlx->mlx, mlx->win, img, 0, 0);
if(i >= 250)
mlx_set_font_scale(mlx->mlx, mlx->win, "default", 16.f);
else
mlx_set_font_scale(mlx->mlx, mlx->win, "default", 6.f);

mlx_string_put(mlx->mlx, mlx->win, 160, 120, 0xFFFF2066, "this text should be hidden");

mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_png, 100, 100);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_bmp, 220, 40);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->img, 150, 60);

mlx_set_font(mlx->mlx, mlx->win, "default");
mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text");

int color = 0;
for(int j = 0; j < 400; j++)
for(int j = 0, color = 0; j < 400; j++)
{
mlx_pixel_put(mlx->mlx, mlx->win, j, j, 0xFFFF0000 + color);
mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, 0xFF0000FF);
color += (color < 255);
}

mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150);

for(int j = 0; j < 20; j++)
{
for(int k = 0; k < 20; k++)
mlx_pixel_put(mlx->mlx, mlx->win, 220 + j, 160 + k, 0xFFFF0000);
}

i++;
return 0;
}
Expand Down Expand Up @@ -174,44 +160,6 @@ int main(void)
mlx_destroy_image(mlx.mlx, mlx.img);
mlx_destroy_window(mlx.mlx, mlx.win);
mlx_destroy_display(mlx.mlx);
return 0;
}
*/

int window_hook(int event, void* param)
{
if(event == 0)
mlx_loop_end(param);
return 0;
}

int main(void)
{
void* mlx;
void* win;

mlx = mlx_init();
win = mlx_new_window(mlx, 400, 400, "My window");

mlx_set_fps_goal(mlx, 60);

mlx_on_event(mlx, win, MLX_WINDOW_EVENT, window_hook, mlx);

int dummy;
void* logo_png = mlx_png_file_to_image(mlx, "42_logo.png", &dummy, &dummy);

mlx_put_image_to_window(mlx, win, logo_png, 10, 190);
mlx_put_image_to_window(mlx, win, logo_png, 11, 190);
mlx_put_image_to_window(mlx, win, logo_png, 10, 190);
mlx_put_image_to_window(mlx, win, logo_png, 100, 190);
mlx_put_image_to_window(mlx, win, logo_png, 10, 190);

mlx_loop(mlx);

mlx_destroy_image(mlx, logo_png);
mlx_destroy_window(mlx, win);
mlx_destroy_display(mlx);

return 0;
}
4 changes: 2 additions & 2 deletions runtime/Includes/Core/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ namespace mlx
std::shared_ptr<Window> p_window;
std::unique_ptr<Scene> p_scene;

std::uint64_t m_current_depth = 0;

int m_id;

bool m_has_window;

bool m_insert_new_pixel_put_texture = false;
};
}

Expand Down
16 changes: 7 additions & 9 deletions runtime/Includes/Core/Graphics.inl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include <Core/Graphics.h>
#include <iostream>

namespace mlx
{
Expand All @@ -9,20 +8,19 @@ namespace mlx
MLX_PROFILE_FUNCTION();
p_scene->ResetSprites();
m_put_pixel_manager.ResetRenderData();
m_current_depth = 0;
m_insert_new_pixel_put_texture = true;
}

void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept
{
MLX_PROFILE_FUNCTION();
/*
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_current_depth, color);
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_insert_new_pixel_put_texture, color);
if(texture)
{
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetPosition(Vec3f{ 0.0f, 0.0f, static_cast<float>(m_current_depth) });
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
}
*/
m_insert_new_pixel_put_texture = false;
}

void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str)
Expand All @@ -41,11 +39,11 @@ namespace mlx
if(!sprite)
{
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetPosition(Vec3f{ static_cast<float>(x), static_cast<float>(y), static_cast<float>(m_current_depth) });
m_current_depth++;
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
m_insert_new_pixel_put_texture = true;
}
else
sprite->SetPosition(Vec3f{ static_cast<float>(x), static_cast<float>(y), static_cast<float>(m_current_depth) });
p_scene->BringToFront(std::move(sprite));
}

void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale)
Expand Down
26 changes: 12 additions & 14 deletions runtime/Includes/Embedded/2DFragment.spv.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
3,2,35,7,0,0,1,0,39,0,0,0,51,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0,
3,2,35,7,0,0,1,0,39,0,0,0,48,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0,
3,0,0,0,0,0,1,0,0,0,15,0,8,0,4,0,0,0,28,0,0,0,109,97,105,110,0,0,0,0,
10,0,0,0,16,0,0,0,22,0,0,0,16,0,3,0,28,0,0,0,7,0,0,0,3,0,3,0,0,0,
0,0,100,0,0,0,5,0,4,0,19,0,0,0,86,101,114,116,79,117,116,0,6,0,5,0,19,0,0,0,
Expand Down Expand Up @@ -29,16 +29,14 @@
7,0,0,0,248,0,2,0,29,0,0,0,59,0,4,0,24,0,0,0,30,0,0,0,7,0,0,0,59,0,
4,0,20,0,0,0,31,0,0,0,7,0,0,0,65,0,5,0,13,0,0,0,32,0,0,0,31,0,0,0,
12,0,0,0,63,0,3,0,32,0,0,0,10,0,0,0,65,0,5,0,18,0,0,0,33,0,0,0,31,0,
0,0,17,0,0,0,63,0,3,0,33,0,0,0,16,0,0,0,65,0,5,0,13,0,0,0,34,0,0,0,
31,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0,35,0,0,0,34,0,0,0,61,0,4,0,3,0,
0,0,36,0,0,0,5,0,0,0,65,0,5,0,18,0,0,0,37,0,0,0,31,0,0,0,17,0,0,0,
61,0,4,0,14,0,0,0,38,0,0,0,37,0,0,0,87,0,5,0,8,0,0,0,39,0,0,0,36,0,
0,0,38,0,0,0,133,0,5,0,8,0,0,0,40,0,0,0,35,0,0,0,39,0,0,0,65,0,5,0,
13,0,0,0,41,0,0,0,30,0,0,0,12,0,0,0,62,0,3,0,41,0,0,0,40,0,0,0,65,0,
5,0,13,0,0,0,45,0,0,0,30,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0,46,0,0,0,
45,0,0,0,81,0,5,0,1,0,0,0,47,0,0,0,46,0,0,0,3,0,0,0,180,0,5,0,27,0,
0,0,48,0,0,0,47,0,0,0,26,0,0,0,247,0,3,0,42,0,0,0,0,0,0,0,250,0,4,0,
48,0,0,0,43,0,0,0,44,0,0,0,248,0,2,0,43,0,0,0,252,0,1,0,248,0,2,0,44,0,
0,0,249,0,2,0,42,0,0,0,248,0,2,0,42,0,0,0,61,0,4,0,23,0,0,0,49,0,0,0,
30,0,0,0,81,0,5,0,8,0,0,0,50,0,0,0,49,0,0,0,0,0,0,0,62,0,3,0,22,0,
0,0,50,0,0,0,253,0,1,0,56,0,1,0
0,0,17,0,0,0,63,0,3,0,33,0,0,0,16,0,0,0,61,0,4,0,3,0,0,0,34,0,0,0,
5,0,0,0,65,0,5,0,18,0,0,0,35,0,0,0,31,0,0,0,17,0,0,0,61,0,4,0,14,0,
0,0,36,0,0,0,35,0,0,0,87,0,5,0,8,0,0,0,37,0,0,0,34,0,0,0,36,0,0,0,
65,0,5,0,13,0,0,0,38,0,0,0,30,0,0,0,12,0,0,0,62,0,3,0,38,0,0,0,37,0,
0,0,65,0,5,0,13,0,0,0,42,0,0,0,30,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0,
43,0,0,0,42,0,0,0,81,0,5,0,1,0,0,0,44,0,0,0,43,0,0,0,3,0,0,0,180,0,
5,0,27,0,0,0,45,0,0,0,44,0,0,0,26,0,0,0,247,0,3,0,39,0,0,0,0,0,0,0,
250,0,4,0,45,0,0,0,40,0,0,0,41,0,0,0,248,0,2,0,40,0,0,0,252,0,1,0,248,0,
2,0,41,0,0,0,249,0,2,0,39,0,0,0,248,0,2,0,39,0,0,0,61,0,4,0,23,0,0,0,
46,0,0,0,30,0,0,0,81,0,5,0,8,0,0,0,47,0,0,0,46,0,0,0,0,0,0,0,62,0,
3,0,22,0,0,0,47,0,0,0,253,0,1,0,56,0,1,0
4 changes: 2 additions & 2 deletions runtime/Includes/Graphics/PutPixelManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace mlx
PutPixelManager(NonOwningPtr<class Renderer> renderer) : p_renderer(renderer) {}

// Return a valid pointer when a new texture has been created
NonOwningPtr<Texture> DrawPixel(int x, int y, std::uint64_t z, std::uint32_t color);
NonOwningPtr<Texture> DrawPixel(int x, int y, bool insert_new_texture, std::uint32_t color);
void ResetRenderData();

~PutPixelManager();

private:
std::map<std::uint64_t, Texture> m_textures;
std::list<Texture> m_textures;
NonOwningPtr<class Renderer> p_renderer;
};
}
Expand Down
5 changes: 2 additions & 3 deletions runtime/Includes/Graphics/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ namespace mlx

Sprite& CreateSprite(NonOwningPtr<class Texture> texture) noexcept;
NonOwningPtr<Sprite> GetSpriteFromTextureAndPosition(NonOwningPtr<Texture> texture, const Vec2f& position) const;
void BringToFront(NonOwningPtr<Sprite> sprite);
void TryEraseSpriteFromTexture(NonOwningPtr<Texture> texture);

inline void ResetSprites() { m_sprites.clear(); }

[[nodiscard]] MLX_FORCEINLINE const std::vector<std::shared_ptr<Sprite>>& GetSprites() const noexcept { return m_sprites; }
[[nodiscard]] MLX_FORCEINLINE const SceneDescriptor& GetDescription() const noexcept { return m_descriptor; }
[[nodiscard]] MLX_FORCEINLINE DepthImage& GetDepth() noexcept { return m_depth; }
[[nodiscard]] MLX_FORCEINLINE ViewerData& GetViewerData() noexcept { return m_viewer_data; }

~Scene();
~Scene() = default;

private:
SceneDescriptor m_descriptor;
std::vector<std::shared_ptr<Sprite>> m_sprites;
DepthImage m_depth;
ViewerData m_viewer_data;
};
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/Includes/Graphics/Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ namespace mlx
Sprite(class Renderer& renderer, NonOwningPtr<Texture> texture);

inline void SetColor(Vec4f color) noexcept { m_color = color; }
inline void SetPosition(Vec3f position) noexcept { m_position = position; }
inline void SetPosition(Vec2f position) noexcept { m_position = position; }

[[nodiscard]] MLX_FORCEINLINE const Vec4f& GetColor() const noexcept { return m_color; }
[[nodiscard]] MLX_FORCEINLINE const Vec3f& GetPosition() const noexcept { return m_position; }
[[nodiscard]] MLX_FORCEINLINE const Vec2f& GetPosition() const noexcept { return m_position; }
[[nodiscard]] MLX_FORCEINLINE std::shared_ptr<Mesh> GetMesh() const { return p_mesh; }
[[nodiscard]] MLX_FORCEINLINE NonOwningPtr<Texture> GetTexture() const { return p_texture; }

Expand All @@ -46,7 +46,7 @@ namespace mlx
NonOwningPtr<Texture> p_texture;
std::shared_ptr<Mesh> p_mesh;
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };
Vec3f m_position = Vec3f{ 0.0f, 0.0f, 0.0f };
Vec2f m_position = Vec2f{ 0.0f, 0.0f };
};
}

Expand Down
1 change: 1 addition & 0 deletions runtime/Includes/PreCompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <string_view>
#include <sstream>
#include <ostream>
#include <ranges>

#ifndef MLX_PLAT_WINDOWS
#include <dlfcn.h>
Expand Down
16 changes: 0 additions & 16 deletions runtime/Includes/Renderer/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,6 @@ namespace mlx
bool m_is_multisampled = false;
};

class DepthImage : public Image
{
public:
DepthImage() = default;
inline void Init(std::uint32_t width, std::uint32_t height, bool is_multisampled, [[maybe_unused]] std::string_view debug_name)
{
MLX_PROFILE_FUNCTION();
std::vector<VkFormat> candidates = { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT };
VkFormat format = kvfFindSupportFormatInCandidates(RenderCore::Get().GetDevice(), candidates.data(), candidates.size(), VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT);
Image::Init(ImageType::Depth, width, height, format, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, is_multisampled, std::move(debug_name));
Image::CreateImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_DEPTH_BIT);
Image::TransitionLayout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
}
~DepthImage() = default;
};

class Texture : public Image
{
public:
Expand Down
2 changes: 0 additions & 2 deletions runtime/Includes/Renderer/Pipelines/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace mlx
std::shared_ptr<Shader> fragment_shader;
std::vector<NonOwningPtr<Texture>> color_attachments;
NonOwningPtr<class Renderer> renderer = nullptr;
NonOwningPtr<DepthImage> depth = nullptr;
bool clear_color_attachments = true;
bool no_vertex_inputs = false;
};
Expand Down Expand Up @@ -55,7 +54,6 @@ namespace mlx
VkPipeline m_pipeline = VK_NULL_HANDLE;
VkPipelineLayout m_pipeline_layout = VK_NULL_HANDLE;
NonOwningPtr<class Renderer> p_renderer;
NonOwningPtr<DepthImage> p_depth;
};
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/Sources/Core/SDLManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ namespace mlx
#endif

#ifdef VK_USE_PLATFORM_WAYLAND_KHR
extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
// extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
#endif

#ifdef VK_USE_PLATFORM_WIN32_KHR
Expand Down
20 changes: 12 additions & 8 deletions runtime/Sources/Graphics/PutPixelManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@

namespace mlx
{
NonOwningPtr<Texture> PutPixelManager::DrawPixel(int x, int y, std::uint64_t z, std::uint32_t color)
NonOwningPtr<Texture> PutPixelManager::DrawPixel(int x, int y, bool insert_new_texture, std::uint32_t color)
{
Verify((bool)p_renderer, "invalid renderer pointer");

VkExtent2D swapchain_extent = kvfGetSwapchainImagesSize(p_renderer->GetSwapchain());
#ifdef DEBUG
auto res = m_textures.try_emplace(z, CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(z));
#else
auto res = m_textures.try_emplace(z, CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{});
#endif
res.first->second.SetPixel(x, y, color);
return (res.second ? &res.first->second : nullptr);
if(insert_new_texture)
{
#ifdef DEBUG
Texture& texture = m_textures.emplace_back(CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(m_textures.size()));
#else
Texture& texture = m_textures.emplace_back(CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{});
#endif
texture.Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
}
m_textures.back().SetPixel(x, y, color);
return (insert_new_texture ? &m_textures.back() : nullptr);
}

void PutPixelManager::ResetRenderData()
Expand Down
18 changes: 12 additions & 6 deletions runtime/Sources/Graphics/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace mlx
{
MLX_PROFILE_FUNCTION();
Verify((bool)m_descriptor.renderer, "invalid renderer");
m_depth.Init(m_descriptor.renderer->GetSwapchainImages().back().GetWidth(), m_descriptor.renderer->GetSwapchainImages().back().GetHeight(), false, "mlx_scene_depth");
}

Sprite& Scene::CreateSprite(NonOwningPtr<Texture> texture) noexcept
Expand All @@ -31,6 +30,18 @@ namespace mlx
return (it != m_sprites.end() ? it->get() : nullptr);
}

void Scene::BringToFront(NonOwningPtr<Sprite> sprite)
{
MLX_PROFILE_FUNCTION();
auto it = std::find_if(m_sprites.begin(), m_sprites.end(), [&sprite](std::shared_ptr<Sprite> sprite_ptr)
{
return sprite_ptr.get() == sprite.Get();
});
if(it == m_sprites.end())
return;
std::rotate(it, it + 1, m_sprites.end());
}

void Scene::TryEraseSpriteFromTexture(NonOwningPtr<Texture> texture)
{
MLX_PROFILE_FUNCTION();
Expand All @@ -44,9 +55,4 @@ namespace mlx
m_sprites.erase(it);
} while(it != m_sprites.end());
}

Scene::~Scene()
{
m_depth.Destroy();
}
}
Loading

0 comments on commit 632117a

Please sign in to comment.