Skip to content

Commit

Permalink
fixing inputs bug, fixing missing depth image destruction in scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Oct 17, 2024
1 parent 74dd8a0 commit 0ea0cde
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/fetch_dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Fetch Dependencies

on:
schedule:
- cron: '0 0 * * *' # Runs daily
- cron: '0 0 * * 0' # Runs weekly

jobs:
update-dependencies:
Expand Down
4 changes: 2 additions & 2 deletions includes/mlx.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* mlx.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* By: maldavid <contact@kbz8.me> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */
/* Updated: 2024/09/15 09:23:48 by maldavid ### ########.fr */
/* Updated: 2024/10/17 17:51:28 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
8 changes: 3 additions & 5 deletions runtime/Includes/Core/SDLManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ namespace mlx
public:
SDLManager();

Handle CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden);
Handle CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden, std::int32_t& id);
void DestroyWindow(Handle window) noexcept;

void InputsFetcher(func::function<void(mlx_event_type, int, int)> functor);

VkSurfaceKHR CreateVulkanSurface(Handle window, VkInstance instance) const noexcept;
std::vector<const char*> GetRequiredVulkanInstanceExtentions() const noexcept;
Vec2ui GetVulkanDrawableSize(Handle window) const noexcept;
void MoveMouseOnWindow(Handle window, int x, int y) const noexcept;
void GetScreenSizeWindowIsOn(Handle window, int* x, int* y) const noexcept;
void SetWindowPosition(Handle window, int x, int y) const noexcept;

inline void SetEventCallback(func::function<void(mlx_event_type, int, int, void*)> functor, void* userdata) { f_callback = std::move(functor); p_callback_data = userdata; }

std::int32_t GetX() const noexcept;
std::int32_t GetY() const noexcept;
std::int32_t GetXRel() const noexcept;
Expand All @@ -36,8 +36,6 @@ namespace mlx
static SDLManager* s_instance;

std::unordered_set<Handle> m_windows_registry;
func::function<void(mlx_event_type, int, int, void*)> f_callback;
void* p_callback_data = nullptr;
bool m_drop_sdl_responsability = false;
};
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/Includes/Embedded/2DVertex.nzsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ external
[entry(vert)]
fn main(input: VertIn) -> VertOut
{
let position: vec2[f32] = input.pos.xy + model.position.xy;
let position: vec4[f32] = vec4[f32](input.pos.xyz + model.position.xyz, 1.0);
input.uv *= -1.0;
let output: VertOut;
output.uv = input.uv;
output.color = model.color;
output.pos = viewer_data.projection_matrix * vec4[f32](position, 0.0, 1.0);
output.pos = viewer_data.projection_matrix * position;
return output;
}
54 changes: 27 additions & 27 deletions runtime/Includes/Embedded/2DVertex.spv.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,37 @@
21,0,0,0,7,0,0,0,17,0,0,0,30,0,4,0,22,0,0,0,2,0,0,0,17,0,0,0,32,0,
4,0,23,0,0,0,7,0,0,0,22,0,0,0,32,0,4,0,24,0,0,0,3,0,0,0,2,0,0,0,
32,0,4,0,26,0,0,0,3,0,0,0,17,0,0,0,30,0,5,0,29,0,0,0,2,0,0,0,17,0,
0,0,2,0,0,0,43,0,4,0,1,0,0,0,30,0,0,0,0,0,128,191,32,0,4,0,31,0,0,0,
7,0,0,0,29,0,0,0,43,0,4,0,14,0,0,0,32,0,0,0,2,0,0,0,43,0,4,0,1,0,
0,0,33,0,0,0,0,0,0,0,43,0,4,0,1,0,0,0,34,0,0,0,0,0,128,63,32,0,4,0,
45,0,0,0,9,0,0,0,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,43,0,0,0,0,0,0,0,1,0,0,0,65,0,5,0,45,0,0,0,46,0,0,0,9,0,0,0,
20,0,0,0,61,0,4,0,2,0,0,0,47,0,0,0,46,0,0,0,79,0,7,0,17,0,0,0,48,0,
0,0,47,0,0,0,47,0,0,0,0,0,0,0,1,0,0,0,129,0,5,0,17,0,0,0,49,0,0,0,
44,0,0,0,48,0,0,0,62,0,3,0,37,0,0,0,49,0,0,0,65,0,5,0,21,0,0,0,50,0,
0,0,39,0,0,0,20,0,0,0,61,0,4,0,17,0,0,0,51,0,0,0,50,0,0,0,142,0,5,0,
17,0,0,0,52,0,0,0,51,0,0,0,30,0,0,0,65,0,5,0,21,0,0,0,53,0,0,0,39,0,
0,0,20,0,0,0,62,0,3,0,53,0,0,0,52,0,0,0,65,0,5,0,21,0,0,0,54,0,0,0,
39,0,0,0,20,0,0,0,61,0,4,0,17,0,0,0,55,0,0,0,54,0,0,0,65,0,5,0,21,0,
0,0,56,0,0,0,38,0,0,0,20,0,0,0,62,0,3,0,56,0,0,0,55,0,0,0,65,0,5,0,
45,0,0,0,57,0,0,0,9,0,0,0,15,0,0,0,61,0,4,0,2,0,0,0,58,0,0,0,57,0,
0,0,65,0,5,0,16,0,0,0,59,0,0,0,38,0,0,0,15,0,0,0,62,0,3,0,59,0,0,0,
58,0,0,0,65,0,5,0,60,0,0,0,61,0,0,0,6,0,0,0,15,0,0,0,61,0,4,0,3,0,
0,0,62,0,0,0,61,0,0,0,61,0,4,0,17,0,0,0,63,0,0,0,37,0,0,0,80,0,6,0,
2,0,0,0,64,0,0,0,63,0,0,0,33,0,0,0,34,0,0,0,145,0,5,0,2,0,0,0,65,0,
0,0,62,0,0,0,64,0,0,0,65,0,5,0,16,0,0,0,66,0,0,0,38,0,0,0,32,0,0,0,
62,0,3,0,66,0,0,0,65,0,0,0,61,0,4,0,29,0,0,0,67,0,0,0,38,0,0,0,81,0,
5,0,2,0,0,0,68,0,0,0,67,0,0,0,0,0,0,0,62,0,3,0,25,0,0,0,68,0,0,0,
81,0,5,0,17,0,0,0,69,0,0,0,67,0,0,0,1,0,0,0,62,0,3,0,27,0,0,0,69,0,
0,0,81,0,5,0,2,0,0,0,70,0,0,0,67,0,0,0,2,0,0,0,62,0,3,0,28,0,0,0,
70,0,0,0,253,0,1,0,56,0,1,0
61,0,4,0,2,0,0,0,43,0,0,0,42,0,0,0,79,0,8,0,31,0,0,0,44,0,0,0,43,0,
0,0,43,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,65,0,5,0,45,0,0,0,46,0,0,0,
9,0,0,0,20,0,0,0,61,0,4,0,2,0,0,0,47,0,0,0,46,0,0,0,79,0,8,0,31,0,
0,0,48,0,0,0,47,0,0,0,47,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,129,0,5,0,
31,0,0,0,49,0,0,0,44,0,0,0,48,0,0,0,80,0,5,0,2,0,0,0,50,0,0,0,49,0,
0,0,32,0,0,0,62,0,3,0,37,0,0,0,50,0,0,0,65,0,5,0,21,0,0,0,51,0,0,0,
39,0,0,0,20,0,0,0,61,0,4,0,17,0,0,0,52,0,0,0,51,0,0,0,142,0,5,0,17,0,
0,0,53,0,0,0,52,0,0,0,33,0,0,0,65,0,5,0,21,0,0,0,54,0,0,0,39,0,0,0,
20,0,0,0,62,0,3,0,54,0,0,0,53,0,0,0,65,0,5,0,21,0,0,0,55,0,0,0,39,0,
0,0,20,0,0,0,61,0,4,0,17,0,0,0,56,0,0,0,55,0,0,0,65,0,5,0,21,0,0,0,
57,0,0,0,38,0,0,0,20,0,0,0,62,0,3,0,57,0,0,0,56,0,0,0,65,0,5,0,45,0,
0,0,58,0,0,0,9,0,0,0,15,0,0,0,61,0,4,0,2,0,0,0,59,0,0,0,58,0,0,0,
65,0,5,0,16,0,0,0,60,0,0,0,38,0,0,0,15,0,0,0,62,0,3,0,60,0,0,0,59,0,
0,0,65,0,5,0,61,0,0,0,62,0,0,0,6,0,0,0,15,0,0,0,61,0,4,0,3,0,0,0,
63,0,0,0,62,0,0,0,61,0,4,0,2,0,0,0,64,0,0,0,37,0,0,0,145,0,5,0,2,0,
0,0,65,0,0,0,63,0,0,0,64,0,0,0,65,0,5,0,16,0,0,0,66,0,0,0,38,0,0,0,
30,0,0,0,62,0,3,0,66,0,0,0,65,0,0,0,61,0,4,0,29,0,0,0,67,0,0,0,38,0,
0,0,81,0,5,0,2,0,0,0,68,0,0,0,67,0,0,0,0,0,0,0,62,0,3,0,25,0,0,0,
68,0,0,0,81,0,5,0,17,0,0,0,69,0,0,0,67,0,0,0,1,0,0,0,62,0,3,0,27,0,
0,0,69,0,0,0,81,0,5,0,2,0,0,0,70,0,0,0,67,0,0,0,2,0,0,0,62,0,3,0,
28,0,0,0,70,0,0,0,253,0,1,0,56,0,1,0
2 changes: 1 addition & 1 deletion runtime/Includes/Graphics/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace mlx
[[nodiscard]] MLX_FORCEINLINE DepthImage& GetDepth() noexcept { return m_depth; }
[[nodiscard]] MLX_FORCEINLINE ViewerData& GetViewerData() noexcept { return m_viewer_data; }

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

private:
SceneDescriptor m_descriptor;
Expand Down
4 changes: 3 additions & 1 deletion runtime/Includes/Platform/Inputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ namespace mlx
};

public:
Inputs();
Inputs() = default;

void FetchInputs();

inline void RegisterWindow(std::shared_ptr<Window> window) { m_windows[window->GetID()] = window; }

Expand Down
2 changes: 1 addition & 1 deletion runtime/Includes/Platform/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace mlx

private:
Handle p_window = nullptr;
std::uint32_t m_id = -1;
std::int32_t m_id;
int m_width = 0;
int m_height = 0;
};
Expand Down
2 changes: 2 additions & 0 deletions runtime/Sources/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace mlx
if(!m_fps.Update())
continue;

m_in.FetchInputs();

if(f_loop_hook)
f_loop_hook(p_param);

Expand Down
109 changes: 50 additions & 59 deletions runtime/Sources/Core/SDLManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,68 +44,10 @@ namespace mlx

if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
FatalError("SDL : unable to init all subsystems; %", SDL_GetError());

struct WatcherData
{
func::function<void(mlx_event_type, int, int, void*)> callback;
NonOwningPtr<SDLManager> manager;
void* userdata;
};

WatcherData watcher_data;
watcher_data.callback = f_callback;
watcher_data.userdata = p_callback_data;

SDL_AddEventWatch([](void* userdata, SDL_Event* event) -> int
{
WatcherData* data = static_cast<WatcherData*>(userdata);

std::uint32_t id = event->window.windowID;
switch(event->type)
{
case SDL_KEYUP: data->callback(MLX_KEYUP, id, event->key.keysym.scancode, data->userdata); break;
case SDL_KEYDOWN: data->callback(MLX_KEYDOWN, id, event->key.keysym.scancode, data->userdata); break;
case SDL_MOUSEBUTTONUP: data->callback(MLX_MOUSEUP, id, event->button.button, data->userdata); break;
case SDL_MOUSEBUTTONDOWN: data->callback(MLX_MOUSEDOWN, id, event->button.button, data->userdata); break;
case SDL_MOUSEWHEEL:
{
if(event->wheel.y > 0) // scroll up
data->callback(MLX_MOUSEWHEEL, id, 1, data->userdata);
else if(event->wheel.y < 0) // scroll down
data->callback(MLX_MOUSEWHEEL, id, 2, data->userdata);
if(event->wheel.x > 0) // scroll right
data->callback(MLX_MOUSEWHEEL, id, 3, data->userdata);
else if(event->wheel.x < 0) // scroll left
data->callback(MLX_MOUSEWHEEL, id, 4, data->userdata);
break;
}
case SDL_WINDOWEVENT:
{
switch(event->window.event)
{
case SDL_WINDOWEVENT_CLOSE: data->callback(MLX_WINDOW_EVENT, id, 0, data->userdata); break;
case SDL_WINDOWEVENT_MOVED: data->callback(MLX_WINDOW_EVENT, id, 1, data->userdata); break;
case SDL_WINDOWEVENT_MINIMIZED: data->callback(MLX_WINDOW_EVENT, id, 2, data->userdata); break;
case SDL_WINDOWEVENT_MAXIMIZED: data->callback(MLX_WINDOW_EVENT, id, 3, data->userdata); break;
case SDL_WINDOWEVENT_ENTER: data->callback(MLX_WINDOW_EVENT, id, 4, data->userdata); break;
case SDL_WINDOWEVENT_FOCUS_GAINED: data->callback(MLX_WINDOW_EVENT, id, 5, data->userdata); break;
case SDL_WINDOWEVENT_LEAVE: data->callback(MLX_WINDOW_EVENT, id, 6, data->userdata); break;
case SDL_WINDOWEVENT_FOCUS_LOST: data->callback(MLX_WINDOW_EVENT, id, 7, data->userdata); break;

default : break;
}
break;
}

default: break;
}

return 0;
}, &watcher_data);
DebugLog("SDL Manager initialized");
}

Handle SDLManager::CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden)
Handle SDLManager::CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden, std::int32_t& id)
{
Internal::WindowInfos* infos = new Internal::WindowInfos;
Verify(infos != nullptr, "SDL : window allocation failed");
Expand All @@ -118,6 +60,8 @@ namespace mlx

m_windows_registry.insert(infos);

id = SDL_GetWindowID(infos->window);

return infos;
}

Expand Down Expand Up @@ -228,6 +172,53 @@ namespace mlx
return y;
}

void SDLManager::InputsFetcher(func::function<void(mlx_event_type, int, int)> functor)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
std::uint32_t id = event.window.windowID;
switch(event.type)
{
case SDL_KEYUP: functor(MLX_KEYUP, id, event.key.keysym.scancode); break;
case SDL_KEYDOWN: functor(MLX_KEYDOWN, id, event.key.keysym.scancode); break;
case SDL_MOUSEBUTTONUP: functor(MLX_MOUSEUP, id, event.button.button); break;
case SDL_MOUSEBUTTONDOWN: functor(MLX_MOUSEDOWN, id, event.button.button); break;
case SDL_MOUSEWHEEL:
{
if(event.wheel.y > 0) // scroll up
functor(MLX_MOUSEWHEEL, id, 1);
else if(event.wheel.y < 0) // scroll down
functor(MLX_MOUSEWHEEL, id, 2);
if(event.wheel.x > 0) // scroll right
functor(MLX_MOUSEWHEEL, id, 3);
else if(event.wheel.x < 0) // scroll left
functor(MLX_MOUSEWHEEL, id, 4);
break;
}
case SDL_WINDOWEVENT:
{
switch(event.window.event)
{
case SDL_WINDOWEVENT_CLOSE: functor(MLX_WINDOW_EVENT, id, 0); break;
case SDL_WINDOWEVENT_MOVED: functor(MLX_WINDOW_EVENT, id, 1); break;
case SDL_WINDOWEVENT_MINIMIZED: functor(MLX_WINDOW_EVENT, id, 2); break;
case SDL_WINDOWEVENT_MAXIMIZED: functor(MLX_WINDOW_EVENT, id, 3); break;
case SDL_WINDOWEVENT_ENTER: functor(MLX_WINDOW_EVENT, id, 4); break;
case SDL_WINDOWEVENT_FOCUS_GAINED: functor(MLX_WINDOW_EVENT, id, 5); break;
case SDL_WINDOWEVENT_LEAVE: functor(MLX_WINDOW_EVENT, id, 6); break;
case SDL_WINDOWEVENT_FOCUS_LOST: functor(MLX_WINDOW_EVENT, id, 7); break;

default : break;
}
break;
}

default: break;
}
}
}

SDLManager::~SDLManager()
{
if(m_drop_sdl_responsability)
Expand Down
5 changes: 5 additions & 0 deletions runtime/Sources/Graphics/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ namespace mlx
m_sprites.erase(it);
} while(it != m_sprites.end());
}

Scene::~Scene()
{
m_depth.Destroy();
}
}
10 changes: 6 additions & 4 deletions runtime/Sources/Platform/Inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

namespace mlx
{
Inputs::Inputs()
void Inputs::FetchInputs()
{
SDLManager::Get().SetEventCallback([this](mlx_event_type event, int window_id, int code, [[maybe_unused]] void* userdata)
SDLManager::Get().InputsFetcher([this](mlx_event_type event, int window_id, int code)
{
if(m_windows.find(window_id) == m_windows.end())
if(!m_windows.contains(window_id))
return;
if(!m_events_hooks.contains(window_id) || !m_events_hooks[window_id][event].hook)
return;
m_events_hooks[window_id][event].hook(code, m_events_hooks[window_id][event].param);
}, nullptr);
});
}

std::int32_t Inputs::GetX() const noexcept
Expand Down
4 changes: 1 addition & 3 deletions runtime/Sources/Platform/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ namespace mlx
{
Window::Window(std::size_t w, std::size_t h, const std::string& title, bool hidden) : m_width(w), m_height(h)
{
static std::uint64_t ids = 0;
p_window = SDLManager::Get().CreateWindow(title, w, h, hidden);
m_id = ids++;
p_window = SDLManager::Get().CreateWindow(title, w, h, hidden, m_id);
}

void Window::Destroy() noexcept
Expand Down
1 change: 0 additions & 1 deletion runtime/Sources/Renderer/Pipelines/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <Renderer/Renderer.h>
#include <Renderer/Vertex.h>
#include <Core/EventBus.h>
#include <iostream>

namespace mlx
{
Expand Down
1 change: 0 additions & 1 deletion runtime/Sources/Renderer/RenderPasses/2DPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <Renderer/Renderer.h>
#include <Graphics/Scene.h>
#include <Maths/Mat4.h>
#include <iostream>

namespace mlx
{
Expand Down

0 comments on commit 0ea0cde

Please sign in to comment.