working on event system

This commit is contained in:
2023-04-04 12:40:47 +02:00
parent 21962dda90
commit d02f04c294
11 changed files with 122 additions and 57 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */
/* Updated: 2023/04/02 22:23:25 by maldavid ### ########.fr */
/* Updated: 2023/04/03 10:49:01 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,21 +26,26 @@ int mlx_loop_end(void* mlx);
int mlx_mouse_show();
int mlx_mouse_hide();
int mlx_mouse_move(void* mlx, void* win_ptr, int x, int y);
int mlx_mouse_move(void* mlx, void* win, int x, int y);
int mlx_mouse_get_pos(void* mlx, int* x, int* y);
int mlx_pixel_put(void* mlx, void* win_ptr, int x, int y, int color);
int mlx_do_key_autorepeaton(void* mlx);
int mlx_do_key_autorepeatoff(void* mlx);
int mlx_pixel_put(void* mlx, void* win, int x, int y, int color);
void* mlx_new_image(void* mlx, int width, int height);
char* mlx_get_data_addr(void* mlx, void* img_ptr, int* bits_per_pixel, int* size_line, int* endian);
int mlx_put_image_to_window(void* mlx, void* win_ptr, void* img_ptr, int x, int y);
int mlx_destroy_image(void* mlx, void* img_ptr);
char* mlx_get_data_addr(void* mlx, void* img, int* bits_per_pixel, int* size_line, int* endian);
int mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y);
int mlx_destroy_image(void* mlx, void* img);
void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height);
void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height);
void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height);
int mlx_clear_window(void* mlx, void* win_ptr);
int mlx_clear_window(void* mlx, void* win);
int mlx_destroy_window(void* mlx, void* win_ptr);
int mlx_destroy_window(void* mlx, void* win);
int mlx_destroy_display(void* mlx);
int mlx_get_screens_size(void* mlx, int* w, int* h);

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
/* Updated: 2023/04/02 23:38:05 by maldavid ### ########.fr */
/* Updated: 2023/04/03 11:18:41 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,6 +35,9 @@ namespace mlx::core
inline void getMousePos(int* x, int* y) noexcept;
inline void mouseMove(void* win_ptr, int x, int y) noexcept;
inline constexpr void enableAutoRepeat() noexcept;
inline constexpr void disableAutoRepeat() noexcept;
inline void getScreenSize(int* w, int* h) noexcept;
inline void* newGraphicsSuport(std::size_t w, std::size_t h, std::string title);

View File

@@ -27,6 +27,16 @@ namespace mlx::core
SDL_FlushEvent(SDL_MOUSEMOTION);
}
constexpr void Application::enableAutoRepeat() noexcept
{
_in.enableAutoRepeat();
}
constexpr void Application::disableAutoRepeat() noexcept
{
_in.disableAutoRepeat();
}
void Application::getScreenSize(int* w, int* h) noexcept
{
SDL_DisplayMode DM;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2023/04/02 22:23:40 by maldavid ### ########.fr */
/* Updated: 2023/04/03 14:18:50 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,7 @@
#include "errors.h"
#include "application.h"
#include <renderer/core/render_core.h>
#include <filesystem>
extern "C"
{
@@ -68,6 +69,18 @@ extern "C"
return 0;
}
int mlx_do_key_autorepeaton(void* mlx)
{
static_cast<mlx::core::Application*>(mlx)->enableAutoRepeat();
return 0;
}
int mlx_do_key_autorepeatoff(void* mlx)
{
static_cast<mlx::core::Application*>(mlx)->disableAutoRepeat();
return 0;
}
void* mlx_new_image(void* mlx, int width, int height)
{
return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height);
@@ -92,6 +105,34 @@ extern "C"
void* mlx_png_file_to_image(void* mlx_ptr, char* filename, int* width, int* height)
{
std::filesystem::path file(filename);
if(file.extension() != ".png")
{
mlx::core::error::report(e_kind::error, "PNG loader : not a png file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx_ptr)->newStbTexture(filename, width, height);
}
void* mlx_jpg_file_to_image(void* mlx_ptr, char* filename, int* width, int* height)
{
std::filesystem::path file(filename);
if(file.extension() != ".jpg" && file.extension() != ".jpeg")
{
mlx::core::error::report(e_kind::error, "PNG loader : not a jpg file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx_ptr)->newStbTexture(filename, width, height);
}
void* mlx_bmp_file_to_image(void* mlx_ptr, char* filename, int* width, int* height)
{
std::filesystem::path file(filename);
if(file.extension() != ".bmp" && file.extension() != ".dib")
{
mlx::core::error::report(e_kind::error, "PNG loader : not a jpg file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx_ptr)->newStbTexture(filename, width, height);
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */
/* Updated: 2023/04/02 15:36:49 by maldavid ### ########.fr */
/* Updated: 2023/04/03 14:23:57 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,6 +21,7 @@
#include <platform/window.h>
#include <renderer/renderer.h>
#include <renderer/pixel_put.h>
#include <utils/non_copyable.h>
#include <renderer/images/texture.h>

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */
/* Updated: 2023/04/01 16:46:13 by maldavid ### ########.fr */
/* Updated: 2023/04/03 12:40:10 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -25,6 +25,9 @@ namespace mlx
{
_xRel = 0;
_yRel = 0;
if(!_auto_repeat)
std::memset(_keys.data(), 0, SDL_NUM_SCANCODES);
while(SDL_PollEvent(&_event))
{

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */
/* Updated: 2022/12/18 23:09:41 by maldavid ### ########.fr */
/* Updated: 2023/04/03 14:19:11 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,6 +43,9 @@ namespace mlx
inline bool is_running() const noexcept { return !_end; }
inline constexpr void finish() noexcept { _end = true; }
inline constexpr void enableAutoRepeat() noexcept { _auto_repeat = true; }
inline constexpr void disableAutoRepeat() noexcept { _auto_repeat = false; }
~Input() = default;
private:
@@ -56,5 +59,6 @@ namespace mlx
int _yRel = 0;
bool _end = false;
bool _auto_repeat = true;
};
}

View File

@@ -6,78 +6,72 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 15:14:50 by maldavid #+# #+# */
/* Updated: 2023/04/02 20:02:30 by maldavid ### ########.fr */
/* Updated: 2023/04/03 14:22:27 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <renderer/images/texture.h>
#include <renderer/pixel_put.h>
#include <cstring>
namespace mlx
{
struct PixelPutPipeline::_Pimpl
{
Texture texture;
Buffer buffer;
void* map = nullptr;
uint32_t width = 0;
uint32_t height = 0;
};
PixelPutPipeline::PixelPutPipeline() : _impl(std::make_unique<_Pimpl>()) {}
void PixelPutPipeline::init(uint32_t width, uint32_t height, Renderer& renderer) noexcept
{
_impl->texture.create(nullptr, width, height, VK_FORMAT_R8G8B8A8_UNORM);
_impl->texture.setDescriptor(renderer.getFragDescriptorSet().duplicate());
_texture.create(nullptr, width, height, VK_FORMAT_R8G8B8A8_UNORM);
_texture.setDescriptor(renderer.getFragDescriptorSet().duplicate());
_impl->buffer.create(Buffer::kind::dynamic, sizeof(uint32_t) * (width * height), VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
_impl->width = width;
_impl->height = height;
_buffer.create(Buffer::kind::dynamic, sizeof(uint32_t) * (width * height), VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
_width = width;
_height = height;
}
VkDescriptorSet PixelPutPipeline::getDescriptorSet() noexcept
{
return _impl->texture.getSet();
return _texture.getSet();
}
void PixelPutPipeline::setPixel(uint32_t x, uint32_t y, int color) noexcept
{
if(x < 0 || y < 0 || x > _impl->width || y > _impl->height)
if(x < 0 || y < 0 || x > _width || y > _height)
return;
if(!_impl->buffer.isMapped())
_impl->buffer.mapMem(&_impl->map);
unsigned char* mem = static_cast<unsigned char*>(_impl->map) + (y * _impl->width * sizeof(uint32_t)) + (x * sizeof(uint32_t));
*reinterpret_cast<uint32_t*>(mem) = color;
if(!_buffer.isMapped())
_buffer.mapMem(&_map);
unsigned char* mem = static_cast<unsigned char*>(_map) + (y * _width * sizeof(uint32_t)) + (x * sizeof(uint32_t));
int new_color = color & 0xFFFFFF00;
new_color >>= 8;
new_color |= (color << 24) & 0xFF000000;
*reinterpret_cast<uint32_t*>(mem) = new_color;
_has_been_modified = true;
}
void PixelPutPipeline::clear()
{
if(!_impl->buffer.isMapped())
_impl->buffer.mapMem(&_impl->map);
unsigned char* mem = static_cast<unsigned char*>(_impl->map);
std::memset(mem, 0, sizeof(uint32_t) * (_impl->width * _impl->height));
if(!_buffer.isMapped())
_buffer.mapMem(&_map);
unsigned char* mem = static_cast<unsigned char*>(_map);
std::memset(mem, 0, sizeof(uint32_t) * (_width * _height));
_has_been_modified = true;
}
void PixelPutPipeline::present() noexcept
{
if(_impl->buffer.isMapped())
_impl->buffer.unmapMem();
_impl->texture.copyFromBuffer(_impl->buffer);
_impl->texture.updateSet(0);
if(_has_been_modified)
{
_texture.copyFromBuffer(_buffer);
_has_been_modified = false;
}
_texture.updateSet(0);
}
void PixelPutPipeline::render(Renderer& renderer) noexcept
{
_impl->texture.render(renderer, 0, 0);
_texture.render(renderer, 0, 0);
}
void PixelPutPipeline::destroy() noexcept
{
_impl->buffer.destroy();
_impl->texture.destroy();
_buffer.destroy();
_texture.destroy();
}
PixelPutPipeline::~PixelPutPipeline() {}

View File

@@ -6,13 +6,14 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 13:18:50 by maldavid #+# #+# */
/* Updated: 2023/04/01 17:26:46 by maldavid ### ########.fr */
/* Updated: 2023/04/03 14:22:35 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_PIXEL_PUT__
#define __MLX_PIXEL_PUT__
#include <renderer/images/texture.h>
#include <renderer/descriptors/vk_descriptor_set.h>
namespace mlx
@@ -20,7 +21,7 @@ namespace mlx
class PixelPutPipeline
{
public:
PixelPutPipeline();
PixelPutPipeline() = default;
void init(uint32_t width, uint32_t height, class Renderer& renderer) noexcept;
@@ -36,8 +37,12 @@ namespace mlx
~PixelPutPipeline();
private:
struct _Pimpl;
std::unique_ptr<_Pimpl> _impl;
Texture _texture;
Buffer _buffer;
void* _map = nullptr;
uint32_t _width = 0;
uint32_t _height = 0;
bool _has_been_modified = true;
};
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/18 17:14:45 by maldavid #+# #+# */
/* Updated: 2023/04/02 18:09:14 by maldavid ### ########.fr */
/* Updated: 2023/04/03 14:23:46 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,7 +17,6 @@
#include <vector>
#include <memory>
#include <renderer/pixel_put.h>
#include <renderer/buffers/vk_ubo.h>
#include <renderer/core/vk_surface.h>
#include <renderer/core/render_core.h>

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
/* Updated: 2023/04/02 23:58:57 by maldavid ### ########.fr */
/* Updated: 2023/04/03 00:05:08 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -56,7 +56,7 @@ void *create_image(t_mlx *mlx)
if (i < 10000 || i > 20000)
{
addr[i + 0] = 0xFF;
addr[i + 1] = 0xFF;
addr[i + 1] = i;
addr[i + 2] = 0x00;
addr[i + 3] = 0xFF;
}