working on window manager

This commit is contained in:
2022-10-05 20:37:04 +02:00
parent 3ee3a5899b
commit b275918de6
10 changed files with 83 additions and 24 deletions

View File

@@ -6,7 +6,7 @@
# By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ # # By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/10/04 16:43:41 by maldavid #+# #+# # # Created: 2022/10/04 16:43:41 by maldavid #+# #+# #
# Updated: 2022/10/04 22:08:52 by maldavid ### ########.fr # # Updated: 2022/10/05 19:25:51 by maldavid ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */ /* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */
/* Updated: 2022/10/04 22:27:45 by maldavid ### ########.fr */ /* Updated: 2022/10/05 18:42:22 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -21,6 +21,11 @@ void mlx_init();
void mlx_new_window(int w, int h, const char* title); void mlx_new_window(int w, int h, const char* title);
int mlx_loop(); int mlx_loop();
int mlx_mouse_show();
int mlx_mouse_hide();
int mlx_mouse_move(void* win_ptr, int x, int y);
int mlx_mouse_get_pos(void* win_ptr, int* x, int* y);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */ /* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
/* Updated: 2022/10/05 16:58:25 by maldavid ### ########.fr */ /* Updated: 2022/10/05 19:23:15 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -17,6 +17,8 @@
#include <memory> #include <memory>
#include <utility> #include <utility>
#include "errors.h"
#include <platform/inputs.h> #include <platform/inputs.h>
#include <platform/window.h> #include <platform/window.h>
@@ -25,11 +27,21 @@ namespace mlx::core
class Application class Application
{ {
public: public:
Application() = default; Application() : _in(_wins) {}
inline void new_window(std::size_t w, std::size_t h, std::string title) inline void* new_window(std::size_t w, std::size_t h, std::string title)
{ {
_wins.emplace_back(std::make_unique<Window>(w, h, std::move(title))); _wins.emplace_back(std::make_shared<Window>(w, h, std::move(title), _wins.size()));
return reinterpret_cast<void*>(&_wins.back()->get_id());
}
inline int get_mouse_pos(void* win_ptr, int* x, int* y) noexcept
{
if(*reinterpret_cast<int*>(win_ptr) > _wins.size())
{
error::report(e_kind::error, "Invalid window pointer");
return -1;
}
} }
void run() noexcept; void run() noexcept;
@@ -38,7 +50,7 @@ namespace mlx::core
private: private:
Input _in; Input _in;
std::vector<std::unique_ptr<Window>> _wins; std::vector<std::shared_ptr<Window>> _wins;
bool _is_loop_running = false; bool _is_loop_running = false;
}; };
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */ /* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2022/10/05 14:02:27 by maldavid ### ########.fr */ /* Updated: 2022/10/05 18:45:52 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -26,11 +26,11 @@ extern "C"
__main_app.reset(new mlx::core::Application()); __main_app.reset(new mlx::core::Application());
} }
void mlx_new_window(int w, int h, const char* title) void* mlx_new_window(int w, int h, const char* title)
{ {
if(__main_app == nullptr) if(__main_app == nullptr)
mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can create new windows"); mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can create new windows");
__main_app->new_window(w, h, title); return __main_app->new_window(w, h, title);
} }
int mlx_loop() int mlx_loop()
@@ -39,4 +39,30 @@ extern "C"
mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can run the main loop"); mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can run the main loop");
__main_app->run(); __main_app->run();
} }
int mlx_mouse_show()
{
if(__main_app == nullptr)
mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can modify the cursor");
return SDL_ShowCursor(SDL_ENABLE);
}
int mlx_mouse_hide()
{
if(__main_app == nullptr)
mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can modify the cursor");
return SDL_ShowCursor(SDL_DISABLE);
}
int mlx_mouse_move(void* win_ptr, int x, int y)
{
if(__main_app == nullptr)
mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can modify the mouse");
}
int mlx_mouse_get_pos(void* win_ptr, int* x, int* y)
{
if(__main_app == nullptr)
mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can access to the mouse");
}
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */ /* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */
/* Updated: 2022/10/05 16:39:42 by maldavid ### ########.fr */ /* Updated: 2022/10/05 19:24:30 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -15,7 +15,7 @@
namespace mlx namespace mlx
{ {
Input::Input() Input::Input(const std::vector<std::shared_ptr<Window>>& wins) : _wins(wins)
{ {
std::memset(_keys.data(), 0, SDL_NUM_SCANCODES); std::memset(_keys.data(), 0, SDL_NUM_SCANCODES);
std::memset(_mouse.data(), 0, 8); std::memset(_mouse.data(), 0, 8);

View File

@@ -6,13 +6,17 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */ /* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */
/* Updated: 2022/10/05 16:45:55 by maldavid ### ########.fr */ /* Updated: 2022/10/05 19:53:18 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <SDL2/SDL.h>
#include <cstdint>
#include <array> #include <array>
#include <memory>
#include <vector>
#include <cstdint>
#include <SDL2/SDL.h>
#include "window.h"
namespace mlx namespace mlx
{ {
@@ -21,7 +25,7 @@ namespace mlx
class Input class Input
{ {
public: public:
Input(); Input(const std::vector<std::shared_ptr<Window>>& wins);
void update(); void update();
@@ -37,14 +41,15 @@ namespace mlx
inline int getYRel() const noexcept { return _yRel; } inline int getYRel() const noexcept { return _yRel; }
inline bool is_running() const noexcept { return !_end; } inline bool is_running() const noexcept { return !_end; }
inline void finish() noexcept { _end = true; } inline constexpr void finish() noexcept { _end = true; }
~Input() = default; ~Input() = default;
private: private:
SDL_Event _event;
std::array<uint8_t, SDL_NUM_SCANCODES> _keys; std::array<uint8_t, SDL_NUM_SCANCODES> _keys;
std::array<uint8_t, 8> _mouse; std::array<uint8_t, 8> _mouse;
SDL_Event _event; std::vector<std::shared_ptr<Window>> _wins;
int _x = 0; int _x = 0;
int _y = 0; int _y = 0;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */ /* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */
/* Updated: 2022/10/04 22:09:27 by maldavid ### ########.fr */ /* Updated: 2022/10/05 18:39:11 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -15,7 +15,7 @@
namespace mlx namespace mlx
{ {
Window::Window(std::size_t w, std::size_t h, std::string title) Window::Window(std::size_t w, std::size_t h, std::string title, int id) : _id(id)
{ {
_win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, /*SDL_WINDOW_VULKAN |*/ SDL_WINDOW_SHOWN); _win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, /*SDL_WINDOW_VULKAN |*/ SDL_WINDOW_SHOWN);
if(!_win) if(!_win)

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */ /* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */
/* Updated: 2022/10/04 21:59:54 by maldavid ### ########.fr */ /* Updated: 2022/10/05 19:10:35 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -21,12 +21,15 @@ namespace mlx
class Window class Window
{ {
public: public:
Window(std::size_t w, std::size_t h, std::string title); Window(std::size_t w, std::size_t h, std::string title, int id);
inline int& get_id() noexcept { return _id; }
~Window(); ~Window();
private: private:
SDL_Window* _win = nullptr; SDL_Window* _win = nullptr;
int _id;
}; };
} }

View File

@@ -6,16 +6,23 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
/* Updated: 2022/10/05 16:58:46 by maldavid ### ########.fr */ /* Updated: 2022/10/05 19:25:46 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdio.h>
#include "../includes/mlx.h" #include "../includes/mlx.h"
int main(void) int main(void)
{ {
void *win_ptr;
int x;
int y;
mlx_init(); mlx_init();
mlx_new_window(400, 400, "My window"); win_ptr = mlx_new_window(400, 400, "My window");
mlx_mouse_get_pos(win_ptr, &x, &y);
printf("%d, %d\n", x, y);
mlx_loop(); mlx_loop();
return (0); return (0);
} }

1
test/run.sh git.filemode.executable_file
View File

@@ -0,0 +1 @@
clear && gcc main.c ../libMicroX.so `sdl2-config --cflags --libs` -lSDL2 && ./a.out