From 934ca543d230e3bd3b97e9cc43ebe5ce7aefc42a Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Tue, 9 Sep 2025 00:24:39 +0300 Subject: [PATCH] Switch to SDL3 --- CMakeLists.txt | 8 +++++--- examples/sdl/SdlGamepadController.cpp | 11 +++++++++++ examples/sdl/SdlGamepadController.h | 17 +++++++++++++++++ examples/sdl/SdlKeyboardController.cpp | 2 +- examples/sdl/Window.cpp | 12 ++++++------ examples/sdl/Window.h | 2 +- examples/sdl/main.cpp | 6 +++--- src/Cartridge.cpp | 1 + src/Controller.h | 2 ++ src/Utils.h | 1 + vcpkg.json | 15 --------------- 11 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 examples/sdl/SdlGamepadController.cpp create mode 100644 examples/sdl/SdlGamepadController.h delete mode 100644 vcpkg.json diff --git a/CMakeLists.txt b/CMakeLists.txt index cdf6412..27bed26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,8 +33,10 @@ add_executable(nes src/Mapper/Mapper1.h src/Bus.cpp src/Bus.h - src/Utils.h) + src/Utils.h + examples/sdl/SdlGamepadController.cpp + examples/sdl/SdlGamepadController.h) -find_package(SDL2 CONFIG REQUIRED) +find_package(SDL3 CONFIG REQUIRED) find_package(fmt REQUIRED) -target_link_libraries(nes PRIVATE SDL2::SDL2 fmt::fmt) \ No newline at end of file +target_link_libraries(nes PRIVATE SDL3::SDL3 fmt::fmt) \ No newline at end of file diff --git a/examples/sdl/SdlGamepadController.cpp b/examples/sdl/SdlGamepadController.cpp new file mode 100644 index 0000000..9cac10f --- /dev/null +++ b/examples/sdl/SdlGamepadController.cpp @@ -0,0 +1,11 @@ +// +// Created by selim on 09.09.2025. +// + +#include "SdlGamepadController.h" + +void SdlGamepadController::poll() +{ + //SDL_GetJoysticks() + //SDL_GetJoystickButton() +} diff --git a/examples/sdl/SdlGamepadController.h b/examples/sdl/SdlGamepadController.h new file mode 100644 index 0000000..8b88a3c --- /dev/null +++ b/examples/sdl/SdlGamepadController.h @@ -0,0 +1,17 @@ +// +// Created by selim on 09.09.2025. +// + +#ifndef NES_SDLGAMEPADCONTROLLER_H +#define NES_SDLGAMEPADCONTROLLER_H + +#include "../../src/Controller.h" + +class SdlGamepadController: public nes::Controller +{ +public: + void poll() override; +}; + + +#endif //NES_SDLGAMEPADCONTROLLER_H \ No newline at end of file diff --git a/examples/sdl/SdlKeyboardController.cpp b/examples/sdl/SdlKeyboardController.cpp index db0ec95..f445072 100644 --- a/examples/sdl/SdlKeyboardController.cpp +++ b/examples/sdl/SdlKeyboardController.cpp @@ -3,7 +3,7 @@ // #include "SdlKeyboardController.h" -#include +#include void SdlKeyboardController::poll() { auto state = SDL_GetKeyboardState(nullptr); diff --git a/examples/sdl/Window.cpp b/examples/sdl/Window.cpp index cb9e86a..7c57a20 100644 --- a/examples/sdl/Window.cpp +++ b/examples/sdl/Window.cpp @@ -7,16 +7,16 @@ namespace nes { SdlWindow::SdlWindow(uint16_t width, uint16_t height): _width(width), _height(height) { - int res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER); + int res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD); if(res < 0) throw std::runtime_error("Error initializing SDL"); - Uint32 flags = SDL_WINDOW_ALLOW_HIGHDPI; //| SDL_WINDOW_VULKAN | SDL_WINDOW_METAL; - _wnd.reset(SDL_CreateWindow("NES", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, flags)); + Uint32 flags = SDL_WINDOW_HIGH_PIXEL_DENSITY; //| SDL_WINDOW_VULKAN | SDL_WINDOW_METAL; + _wnd.reset(SDL_CreateWindow("NES", width, height, flags)); if(!_wnd) throw std::runtime_error("Error creating SDL window"); - SDL_SetWindowResizable(_wnd.get(), SDL_TRUE); + SDL_SetWindowResizable(_wnd.get(), true); - _renderer.reset(SDL_CreateRenderer(_wnd.get(), -1, 0)); + _renderer.reset(SDL_CreateRenderer(_wnd.get(), nullptr)); if(!_renderer) throw std::runtime_error("Error creating SDL renderer"); _texture.reset(SDL_CreateTexture(_renderer.get(), SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, width, height)); @@ -27,7 +27,7 @@ namespace nes { int pitch = static_cast(_width*sizeof(Pixel)); SDL_UpdateTexture(_texture.get(), nullptr, buffer, pitch); SDL_RenderClear(_renderer.get()); - SDL_RenderCopy(_renderer.get(), _texture.get(), nullptr, nullptr); + SDL_RenderTexture(_renderer.get(), _texture.get(), nullptr, nullptr); SDL_RenderPresent(_renderer.get()); } diff --git a/examples/sdl/Window.h b/examples/sdl/Window.h index 3cb8816..2a88c3e 100644 --- a/examples/sdl/Window.h +++ b/examples/sdl/Window.h @@ -7,7 +7,7 @@ #include "../../src/Ppu.h" -#include +#include #include namespace nes { diff --git a/examples/sdl/main.cpp b/examples/sdl/main.cpp index 3d53d68..3b4cc36 100644 --- a/examples/sdl/main.cpp +++ b/examples/sdl/main.cpp @@ -21,9 +21,9 @@ int main() { frameRendered = true; }); device.connect(std::make_shared()); - //device.insertCartridge("/home/selim/Downloads/dk.nes"); - device.insertCartridge("/Users/selim/Documents/nes/ppu_tests/power_up_palette.nes"); - device.insertCartridge("/Users/selim/Documents/nes/ff.nes"); + device.insertCartridge("/home/selim/Documents/nes/smb.nes"); + //device.insertCartridge("/Users/selim/Documents/nes/ppu_tests/power_up_palette.nes"); + //device.insertCartridge("/Users/selim/Documents/nes/ff.nes"); //device.insertCartridge("C:\\Users\\selim\\Documents\\nestest.nes"); auto frameStart = std::chrono::steady_clock::now(); diff --git a/src/Cartridge.cpp b/src/Cartridge.cpp index 4d1857d..37bbdd2 100644 --- a/src/Cartridge.cpp +++ b/src/Cartridge.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace nes { diff --git a/src/Controller.h b/src/Controller.h index 038214b..c5df0de 100644 --- a/src/Controller.h +++ b/src/Controller.h @@ -11,6 +11,8 @@ namespace nes { class Controller { public: + virtual ~Controller() = default; + enum Key: uint8_t { A = 7, B = 6, diff --git a/src/Utils.h b/src/Utils.h index 48446c7..e03c99f 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -6,6 +6,7 @@ #define UTILS_H #include +#include namespace nes { diff --git a/vcpkg.json b/vcpkg.json deleted file mode 100644 index 9d47d8f..0000000 --- a/vcpkg.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name" : "nes", - "version-string" : "1.0.0", - "builtin-baseline" : "c95000e1b5bb62884de08d5e952993c8bced9db6", - "dependencies": [ - { - "name": "sdl2", - "version>=": "2.26.5" - }, - { - "name": "fmt", - "version>=": "10.0.0" - } - ] -} \ No newline at end of file