diff --git a/includes/mlx.h b/includes/mlx.h index 2619fd1..8da3d67 100644 --- a/includes/mlx.h +++ b/includes/mlx.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/src/core/application.h b/src/core/application.h index 0335fcc..daa6a39 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/src/core/application.inl b/src/core/application.inl index 5c65faa..b929f96 100644 --- a/src/core/application.inl +++ b/src/core/application.inl @@ -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; diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index ee502ef..c389455 100644 --- a/src/core/bridge.cpp +++ b/src/core/bridge.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#include extern "C" { @@ -68,6 +69,18 @@ extern "C" return 0; } + int mlx_do_key_autorepeaton(void* mlx) + { + static_cast(mlx)->enableAutoRepeat(); + return 0; + } + + int mlx_do_key_autorepeatoff(void* mlx) + { + static_cast(mlx)->disableAutoRepeat(); + return 0; + } + void* mlx_new_image(void* mlx, int width, int height) { return static_cast(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_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_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_ptr)->newStbTexture(filename, width, height); } diff --git a/src/core/graphics.h b/src/core/graphics.h index 8c0d3cb..f72b484 100644 --- a/src/core/graphics.h +++ b/src/core/graphics.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 #include +#include #include #include diff --git a/src/platform/inputs.cpp b/src/platform/inputs.cpp index fe36f10..fac051b 100644 --- a/src/platform/inputs.cpp +++ b/src/platform/inputs.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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)) { diff --git a/src/platform/inputs.h b/src/platform/inputs.h index 67c9a93..dde699a 100644 --- a/src/platform/inputs.h +++ b/src/platform/inputs.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; }; } diff --git a/src/renderer/pixel_put.cpp b/src/renderer/pixel_put.cpp index 4de8a6a..cfed47a 100644 --- a/src/renderer/pixel_put.cpp +++ b/src/renderer/pixel_put.cpp @@ -6,78 +6,72 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 #include #include 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(_impl->map) + (y * _impl->width * sizeof(uint32_t)) + (x * sizeof(uint32_t)); - *reinterpret_cast(mem) = color; + if(!_buffer.isMapped()) + _buffer.mapMem(&_map); + unsigned char* mem = static_cast(_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(mem) = new_color; + _has_been_modified = true; } void PixelPutPipeline::clear() { - if(!_impl->buffer.isMapped()) - _impl->buffer.mapMem(&_impl->map); - unsigned char* mem = static_cast(_impl->map); - std::memset(mem, 0, sizeof(uint32_t) * (_impl->width * _impl->height)); + if(!_buffer.isMapped()) + _buffer.mapMem(&_map); + unsigned char* mem = static_cast(_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() {} diff --git a/src/renderer/pixel_put.h b/src/renderer/pixel_put.h index c9a875c..d030b23 100644 --- a/src/renderer/pixel_put.h +++ b/src/renderer/pixel_put.h @@ -6,13 +6,14 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 #include 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; }; } diff --git a/src/renderer/renderer.h b/src/renderer/renderer.h index 2e90bfc..d8373f3 100644 --- a/src/renderer/renderer.h +++ b/src/renderer/renderer.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 #include -#include #include #include #include diff --git a/test/main.c b/test/main.c index 2382020..e03379b 100644 --- a/test/main.c +++ b/test/main.c @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; }