Skip to content

Commit

Permalink
fixing things
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Jan 7, 2025
1 parent aa345c7 commit 54b4582
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 199 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*.pdb
*.gch
*.pch
*.sym
*.exe
*vgcore.*
*.gdb_history
Expand Down
21 changes: 9 additions & 12 deletions example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ typedef struct

static mlx_color pixels_circle[CIRCLE_DIAMETER * CIRCLE_DIAMETER] = { 0 };

#define THRESHOLD 200

void update(void* param)
{
static int i = 0;
mlx_t* mlx = (mlx_t*)param;

if(i > 200)
if(i > THRESHOLD)
{
mlx_clear_window(mlx->mlx, mlx->win, (mlx_color){ .rgba = 0x334D4DFF });
mlx_put_transformed_image_to_window(mlx->mlx, mlx->win, mlx->logo_bmp, 220, 40, 0.5f, 0.5f, i);
}

if(i >= 250)
if(i >= THRESHOLD + THRESHOLD / 4)
mlx_set_font_scale(mlx->mlx, "default", 16.f);
else
mlx_set_font_scale(mlx->mlx, "default", 6.f);

mlx_string_put(mlx->mlx, mlx->win, 160, 120, (mlx_color){ .rgba = 0xFF2066FF }, "this text should be hidden");
mlx_string_put(mlx->mlx, mlx->win, 160, 120, (mlx_color){ .rgba = 0xFF2066FF }, "this text should be behind");

mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_png, 100, 100);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->img, 150, 60);
Expand All @@ -50,7 +52,7 @@ void update(void* param)
color += (color < 255);
}

if(i < 200)
if(i < THRESHOLD)
mlx_put_transformed_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150, 0.5f, 2.0f, 0.0f);
else
mlx_put_transformed_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150, fabs(sin(i / 100.0f)), fabs(cos(i / 100.0f) * 2.0f), 0.0f);
Expand All @@ -59,7 +61,7 @@ void update(void* param)

mlx_pixel_put_region(mlx->mlx, mlx->win, 200, 170, CIRCLE_DIAMETER, CIRCLE_DIAMETER, pixels_circle);

i++;
i++; // Will overflow and I don't care
}

mlx_image create_image(mlx_t* mlx)
Expand Down Expand Up @@ -166,14 +168,9 @@ int main(void)
mlx_on_event(mlx.mlx, mlx.win, MLX_KEYDOWN, key_hook, &mlx);
mlx_on_event(mlx.mlx, mlx.win, MLX_WINDOW_EVENT, window_hook, &mlx);

mlx.logo_png = mlx_new_image_from_file(mlx.mlx, "42_logo.png", &dummy, &dummy);
mlx.logo_bmp = mlx_new_image_from_file(mlx.mlx, "42_logo.bmp", &dummy, &dummy);
//mlx.logo_jpg = mlx_new_image_from_file(mlx.mlx, "42_logo.jpg", &dummy, &dummy);
mlx.logo_jpg = mlx_new_image(mlx.mlx, dummy, dummy);

mlx_color* data = (mlx_color*)malloc(dummy * dummy * sizeof(mlx_color));
mlx_get_image_region(mlx.mlx, mlx.logo_png, 0, 0, dummy, dummy, data);
mlx_set_image_region(mlx.mlx, mlx.logo_jpg, 0, 0, dummy, dummy, data);
mlx.logo_png = mlx_new_image_from_file(mlx.mlx, "42_logo.png", &dummy, &dummy);
mlx.logo_jpg = mlx_new_image_from_file(mlx.mlx, "42_logo.jpg", &dummy, &dummy);

mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, (mlx_color){ .rgba = 0xFF00FFFF });
mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo_png, 0, 0);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions includes/mlx.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */
/* Updated: 2024/12/20 00:42:01 by maldavid ### ########.fr */
/* Updated: 2025/01/05 22:44:22 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -79,7 +79,7 @@ MLX_API mlx_context mlx_init();
* @brief Caps the FPS
*
* @param mlx Internal MLX application
* @param fps The FPS cap
* @param fps The FPS cap or 0 for vsync
*/
MLX_API void mlx_set_fps_goal(mlx_context mlx, int fps);

Expand Down
6 changes: 3 additions & 3 deletions includes/mlx_profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 08:49:17 by maldavid #+# #+# */
/* Updated: 2024/12/17 00:35:35 by maldavid ### ########.fr */
/* Updated: 2025/01/07 00:17:45 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef __MLX_PROFILE__
#define __MLX_PROFILE__
#ifndef MACROLIBX_PROFILE_H
#define MACROLIBX_PROFILE_H

// Try to identify the compiler
#if defined(__BORLANDC__)
Expand Down
7 changes: 7 additions & 0 deletions runtime/Includes/Core/Application.inl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ namespace mlx

void Application::SetFPSCap(std::uint32_t fps) noexcept
{
if(fps == 0)
{
SDL_DisplayMode mode;
if(!SDL_GetCurrentDisplayMode(1, &mode))
return;
fps = mode.refresh_rate;
}
m_fps.SetMaxFPS(fps);
}

Expand Down
12 changes: 6 additions & 6 deletions runtime/Includes/Core/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ namespace mlx

void Render() noexcept;

inline void ResetRenderData(mlx_color color) noexcept;
void ResetRenderData(mlx_color color) noexcept;

inline void PixelPut(int x, int y, mlx_color color) noexcept;
inline void PixelPutArray(int x, int y, mlx_color* color, std::size_t pixels_size) noexcept;
inline void PixelPutRegion(int x, int y, int w, int h, mlx_color* color) noexcept;
inline void StringPut(int x, int y, mlx_color color, std::string str);
inline void TexturePut(NonOwningPtr<class Texture> texture, int x, int y, float scale_x, float scale_y, float angle);
void PixelPut(int x, int y, mlx_color color) noexcept;
void PixelPutArray(int x, int y, mlx_color* color, std::size_t pixels_size) noexcept;
void PixelPutRegion(int x, int y, int w, int h, mlx_color* color) noexcept;
void StringPut(int x, int y, mlx_color color, std::string str);
void TexturePut(NonOwningPtr<class Texture> texture, int x, int y, float scale_x, float scale_y, float angle);

inline void TryEraseSpritesInScene(NonOwningPtr<Texture> texture) noexcept;

Expand Down
101 changes: 0 additions & 101 deletions runtime/Includes/Core/Graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,6 @@

namespace mlx
{
void GraphicsSupport::ResetRenderData(mlx_color color) noexcept
{
MLX_PROFILE_FUNCTION();
Vec4f vec_color = {
static_cast<float>(color.r) / 255.0f,
static_cast<float>(color.g) / 255.0f,
static_cast<float>(color.b) / 255.0f,
static_cast<float>(color.a) / 255.0f
};
p_scene->ResetScene(std::move(vec_color));
m_put_pixel_manager.ResetRenderData();
m_draw_layer = 0;
m_pixelput_called = false;
}

void GraphicsSupport::PixelPut(int x, int y, mlx_color color) noexcept
{
MLX_PROFILE_FUNCTION();
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_draw_layer, color);
if(texture)
{
m_pixelput_called = true;
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
}
}

void GraphicsSupport::PixelPutArray(int x, int y, mlx_color* pixels, std::size_t pixels_size) noexcept
{
MLX_PROFILE_FUNCTION();
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixelsArray(x, y, m_draw_layer, pixels, pixels_size);
if(texture)
{
m_pixelput_called = true;
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
}
}

void GraphicsSupport::PixelPutRegion(int x, int y, int w, int h, mlx_color* pixels) noexcept
{
MLX_PROFILE_FUNCTION();
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixelsRegion(x, y, w, h, m_draw_layer, pixels);
if(texture)
{
m_pixelput_called = true;
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
}
}

void GraphicsSupport::StringPut(int x, int y, mlx_color color, std::string str)
{
MLX_PROFILE_FUNCTION();
if(str.empty())
return;

Vec4f vec_color = {
static_cast<float>(color.r) / 255.0f,
static_cast<float>(color.g) / 255.0f,
static_cast<float>(color.b) / 255.0f,
static_cast<float>(color.a) / 255.0f,
};

NonOwningPtr<Text> text = p_scene->GetTextFromPositionAndColor(str, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, vec_color);
if(!text)
{
if(m_pixelput_called)
{
m_draw_layer++;
m_pixelput_called = false;
}
Text& new_text = p_scene->CreateText(str);
new_text.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
new_text.SetColor(std::move(vec_color));
}
else if(!p_scene->IsTextAtGivenDrawLayer(str, m_draw_layer))
p_scene->BringToDrawLayer(text.Get(), m_draw_layer);
}

void GraphicsSupport::TexturePut(NonOwningPtr<Texture> texture, int x, int y, float scale_x, float scale_y, float angle)
{
MLX_PROFILE_FUNCTION();
NonOwningPtr<Sprite> sprite = p_scene->GetSpriteFromTexturePositionScaleRotation(texture, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, scale_x, scale_y, angle);
if(!sprite)
{
if(m_pixelput_called)
{
m_draw_layer++;
m_pixelput_called = false;
}
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetCenter(Vec2f{ texture->GetWidth() / 2.0f, texture->GetHeight() / 2.0f });
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
new_sprite.SetScale(Vec2f{ scale_x, scale_y });
new_sprite.SetRotation(angle);
}
else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer))
p_scene->BringToDrawLayer(sprite.Get(), m_draw_layer);
}

void GraphicsSupport::TryEraseSpritesInScene(NonOwningPtr<Texture> texture) noexcept
{
MLX_PROFILE_FUNCTION();
Expand Down
6 changes: 5 additions & 1 deletion runtime/Includes/Graphics/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,21 @@ namespace mlx

void BringToDrawLayer(NonOwningPtr<Drawable> drawable, std::uint64_t draw_layer);

inline void ResetScene(Vec4f clear) { m_drawables.clear(); m_clear_color = std::move(clear); }
inline void ResetScene(Vec4f clear) { m_drawables.clear(); m_clear_color = std::move(clear); m_has_scene_changed = true; }
inline const Vec4f& GetClearColor() const noexcept { return m_clear_color; }

[[nodiscard]] MLX_FORCEINLINE const std::vector<std::shared_ptr<Drawable>>& GetDrawables() const noexcept { return m_drawables; }

inline void ResetChangeChecker() noexcept { m_has_scene_changed = false; }
inline bool HasSceneChanged() const noexcept { return m_has_scene_changed; }

~Scene() = default;

private:
std::vector<std::shared_ptr<Drawable>> m_drawables;
std::shared_ptr<Font> p_bound_font;
Vec4f m_clear_color = { 0.0f, 0.0f, 0.0f, 1.0f };
bool m_has_scene_changed = false;
};
}

Expand Down
1 change: 1 addition & 0 deletions runtime/Sources/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace mlx
catch(...) { return nullptr; }
m_image_registry.RegisterTexture(texture);
image->texture = texture;
texture->Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
return image;
}

Expand Down
2 changes: 0 additions & 2 deletions runtime/Sources/Core/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ extern "C"
MLX_CHECK_APPLICATION_POINTER(mlx);
if(fps < 0)
mlx::Error("You cannot set a negative FPS cap (nice try)");
else if(fps == 0)
mlx::Error("You cannot set a FPS cap to 0 (nice try)");
else
mlx->app->SetFPSCap(static_cast<std::uint32_t>(fps));
}
Expand Down
101 changes: 101 additions & 0 deletions runtime/Sources/Core/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,107 @@ namespace mlx
#endif
}

void GraphicsSupport::ResetRenderData(mlx_color color) noexcept
{
MLX_PROFILE_FUNCTION();
Vec4f vec_color = {
static_cast<float>(color.r) / 255.0f,
static_cast<float>(color.g) / 255.0f,
static_cast<float>(color.b) / 255.0f,
static_cast<float>(color.a) / 255.0f
};
p_scene->ResetScene(std::move(vec_color));
m_put_pixel_manager.ResetRenderData();
m_draw_layer = 0;
m_pixelput_called = false;
}

void GraphicsSupport::PixelPut(int x, int y, mlx_color color) noexcept
{
MLX_PROFILE_FUNCTION();
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_draw_layer, color);
if(texture)
{
m_pixelput_called = true;
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
}
}

void GraphicsSupport::PixelPutArray(int x, int y, mlx_color* pixels, std::size_t pixels_size) noexcept
{
MLX_PROFILE_FUNCTION();
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixelsArray(x, y, m_draw_layer, pixels, pixels_size);
if(texture)
{
m_pixelput_called = true;
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
}
}

void GraphicsSupport::PixelPutRegion(int x, int y, int w, int h, mlx_color* pixels) noexcept
{
MLX_PROFILE_FUNCTION();
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixelsRegion(x, y, w, h, m_draw_layer, pixels);
if(texture)
{
m_pixelput_called = true;
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
}
}

void GraphicsSupport::StringPut(int x, int y, mlx_color color, std::string str)
{
MLX_PROFILE_FUNCTION();
if(str.empty())
return;

Vec4f vec_color = {
static_cast<float>(color.r) / 255.0f,
static_cast<float>(color.g) / 255.0f,
static_cast<float>(color.b) / 255.0f,
static_cast<float>(color.a) / 255.0f,
};

NonOwningPtr<Text> text = p_scene->GetTextFromPositionAndColor(str, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, vec_color);
if(!text)
{
if(m_pixelput_called)
{
m_draw_layer++;
m_pixelput_called = false;
}
Text& new_text = p_scene->CreateText(str);
new_text.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
new_text.SetColor(std::move(vec_color));
}
else if(!p_scene->IsTextAtGivenDrawLayer(str, m_draw_layer))
p_scene->BringToDrawLayer(text.Get(), m_draw_layer);
}

void GraphicsSupport::TexturePut(NonOwningPtr<Texture> texture, int x, int y, float scale_x, float scale_y, float angle)
{
MLX_PROFILE_FUNCTION();
NonOwningPtr<Sprite> sprite = p_scene->GetSpriteFromTexturePositionScaleRotation(texture, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, scale_x, scale_y, angle);
if(!sprite)
{
if(m_pixelput_called)
{
m_draw_layer++;
m_pixelput_called = false;
}
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetCenter(Vec2f{ texture->GetWidth() / 2.0f, texture->GetHeight() / 2.0f });
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
new_sprite.SetScale(Vec2f{ scale_x, scale_y });
new_sprite.SetRotation(angle);
}
else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer))
p_scene->BringToDrawLayer(sprite.Get(), m_draw_layer);
}

GraphicsSupport::~GraphicsSupport()
{
MLX_PROFILE_FUNCTION();
Expand Down
Loading

0 comments on commit 54b4582

Please sign in to comment.