diff --git a/EightBit.sln b/EightBit.sln index fe8aee1..ed6432c 100644 --- a/EightBit.sln +++ b/EightBit.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27130.2010 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29306.81 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "EightBit", "src\EightBit.vcxproj", "{A9C24BD9-0CB4-4C84-B09B-46B815F9DA47}" EndProject @@ -35,6 +35,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "unittest_MC6809", "MC6809\u EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "M6532", "M6532\src\M6532.vcxproj", "{61ACB9AF-314F-4D9E-BFF4-96BC85F38278}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Gaming", "Gaming\src\Gaming.vcxproj", "{BC526300-E3D0-44B1-B57B-312BAA6566AC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -171,6 +173,14 @@ Global {61ACB9AF-314F-4D9E-BFF4-96BC85F38278}.Release|x64.Build.0 = Release|x64 {61ACB9AF-314F-4D9E-BFF4-96BC85F38278}.Release|x86.ActiveCfg = Release|Win32 {61ACB9AF-314F-4D9E-BFF4-96BC85F38278}.Release|x86.Build.0 = Release|Win32 + {BC526300-E3D0-44B1-B57B-312BAA6566AC}.Debug|x64.ActiveCfg = Debug|x64 + {BC526300-E3D0-44B1-B57B-312BAA6566AC}.Debug|x64.Build.0 = Debug|x64 + {BC526300-E3D0-44B1-B57B-312BAA6566AC}.Debug|x86.ActiveCfg = Debug|Win32 + {BC526300-E3D0-44B1-B57B-312BAA6566AC}.Debug|x86.Build.0 = Debug|Win32 + {BC526300-E3D0-44B1-B57B-312BAA6566AC}.Release|x64.ActiveCfg = Release|x64 + {BC526300-E3D0-44B1-B57B-312BAA6566AC}.Release|x64.Build.0 = Release|x64 + {BC526300-E3D0-44B1-B57B-312BAA6566AC}.Release|x86.ActiveCfg = Release|Win32 + {BC526300-E3D0-44B1-B57B-312BAA6566AC}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Gaming/inc/Game.h b/Gaming/inc/Game.h new file mode 100644 index 0000000..d69c113 --- /dev/null +++ b/Gaming/inc/Game.h @@ -0,0 +1,87 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include + +#include + +#include "GameController.h" + +class Configuration; + +namespace Gaming { + class Game : public EightBit::Device { + public: + + static void throwSDLException(std::string failure) { + throw std::runtime_error(failure + ::SDL_GetError()); + } + + static void verifySDLCall(int returned, std::string failure) { + if (returned < 0) + throwSDLException(failure); + } + + Game(); + virtual ~Game(); + + virtual void runLoop(); + virtual void raisePOWER() override; + + protected: + virtual int fps() const = 0; + virtual bool useVsync() const = 0; + + virtual int displayWidth() const { return rasterWidth(); } + virtual int displayHeight() const { return rasterHeight(); } + virtual int displayScale() const = 0; + + virtual int rasterWidth() const = 0; + virtual int rasterHeight() const = 0; + + virtual std::string title() const = 0; + + virtual void runFrame() = 0; + + void addJoystick(SDL_Event& e); + void removeJoystick(SDL_Event& e); + + virtual void updateTexture(); + virtual void copyTexture(); + virtual void displayTexture(); + + virtual const uint32_t* pixels() const = 0; + + std::shared_ptr m_pixelFormat; + + private: + std::shared_ptr m_window; + std::shared_ptr m_renderer; + std::shared_ptr m_bitmapTexture; + Uint32 m_pixelType = SDL_PIXELFORMAT_ARGB8888; + + bool m_vsync = false; + Uint32 m_startTicks = 0; + Uint32 m_frames = 0; + + std::map> m_gameControllers; + std::map m_mappedControllers; + + void configureBackground() const; + void createBitmapTexture(); + + virtual void handleKeyDown(SDL_Keycode key) {} + virtual void handleKeyUp(SDL_Keycode key) {} + + virtual void handleJoyButtonDown(SDL_JoyButtonEvent event) {} + virtual void handleJoyButtonUp(SDL_JoyButtonEvent event) {} + + int chooseControllerIndex(int who) const; + std::shared_ptr chooseController(int who) const; + }; +} diff --git a/Gaming/inc/GameController.h b/Gaming/inc/GameController.h new file mode 100644 index 0000000..a2e79c9 --- /dev/null +++ b/Gaming/inc/GameController.h @@ -0,0 +1,36 @@ +#pragma once + +#include + +namespace Gaming { + class GameController final { + public: + GameController(int index); + virtual ~GameController(); + + void startRumble() noexcept; + void stopRumble() noexcept; + + static auto buildJoystickId(SDL_GameController* controller) noexcept { + auto joystick = ::SDL_GameControllerGetJoystick(controller); + return ::SDL_JoystickInstanceID(joystick); + } + + auto getJoystickId() const noexcept { + return buildJoystickId(m_gameController); + } + + private: + int m_index; + SDL_GameController* m_gameController = nullptr; + + void open(); + void close() noexcept; + + SDL_Haptic* m_hapticController = nullptr; + bool m_hapticRumbleSupported = false; + + void openHapticController(); + void closeHapticController() noexcept; + }; +} \ No newline at end of file diff --git a/Gaming/src/Game.cpp b/Gaming/src/Game.cpp new file mode 100644 index 0000000..56489fd --- /dev/null +++ b/Gaming/src/Game.cpp @@ -0,0 +1,182 @@ +#include "pch.h" +#include "Game.h" + +namespace Gaming { + +Game::Game() { + verifySDLCall(::SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC), "Failed to initialise SDL: "); +} + +Game::~Game() { + ::SDL_Quit(); +} + +void Game::raisePOWER() { + + Device::raisePOWER(); + + m_window.reset(::SDL_CreateWindow( + title().c_str(), + SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + displayWidth() * displayScale(), displayHeight() * displayScale(), + SDL_WINDOW_SHOWN), ::SDL_DestroyWindow); + if (m_window == nullptr) + throwSDLException("Unable to create window: "); + + ::SDL_DisplayMode mode; + verifySDLCall(::SDL_GetWindowDisplayMode(m_window.get(), &mode), "Unable to obtain window information"); + + Uint32 rendererFlags = 0; + m_vsync = useVsync(); + if (m_vsync) { + const auto required = fps(); + if (required == mode.refresh_rate) { + rendererFlags |= SDL_RENDERER_PRESENTVSYNC; + ::SDL_Log("Attempting to use SDL_RENDERER_PRESENTVSYNC"); + } else { + m_vsync = false; + ::SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Display refresh rate is incompatible with required rate (%d)", required); + } + } + m_renderer.reset(::SDL_CreateRenderer(m_window.get(), -1, rendererFlags), ::SDL_DestroyRenderer); + if (m_renderer == nullptr) + throwSDLException("Unable to create renderer: "); + + ::SDL_RendererInfo info; + verifySDLCall(::SDL_GetRendererInfo(m_renderer.get(), &info), "Unable to obtain renderer information"); + + if (m_vsync) { + if ((info.flags & SDL_RENDERER_PRESENTVSYNC) == 0) { + ::SDL_LogWarn(::SDL_LOG_CATEGORY_APPLICATION, "Renderer does not support VSYNC, reverting to timed delay loop."); + m_vsync = false; + } + } + + m_pixelFormat.reset(::SDL_AllocFormat(m_pixelType), ::SDL_FreeFormat); + if (m_pixelFormat == nullptr) + throwSDLException("Unable to allocate pixel format: "); + + configureBackground(); + createBitmapTexture(); +} + +void Game::configureBackground() const { + verifySDLCall(::SDL_SetRenderDrawColor(m_renderer.get(), 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE), "Unable to set render draw colour"); +} + +void Game::createBitmapTexture() { + m_bitmapTexture.reset(::SDL_CreateTexture(m_renderer.get(), m_pixelType, SDL_TEXTUREACCESS_STREAMING, rasterWidth(), rasterHeight()), ::SDL_DestroyTexture); + if (m_bitmapTexture == nullptr) + throwSDLException("Unable to create bitmap texture"); +} + +void Game::runLoop() { + + m_frames = 0UL; + m_startTicks = ::SDL_GetTicks(); + + while (powered()) { + ::SDL_Event e; + while (::SDL_PollEvent(&e)) { + switch (e.type) { + case SDL_QUIT: + lowerPOWER(); + break; + case SDL_KEYDOWN: + handleKeyDown(e.key.keysym.sym); + break; + case SDL_KEYUP: + handleKeyUp(e.key.keysym.sym); + break; + case SDL_JOYBUTTONDOWN: + handleJoyButtonDown(e.jbutton); + break; + case SDL_JOYBUTTONUP: + handleJoyButtonUp(e.jbutton); + break; + case SDL_JOYDEVICEADDED: + addJoystick(e); + break; + case SDL_JOYDEVICEREMOVED: + removeJoystick(e); + break; + } + } + + runFrame(); + + updateTexture(); + copyTexture(); + displayTexture(); + + ++m_frames; + + if (!m_vsync) { + const auto elapsedTicks = ::SDL_GetTicks() - m_startTicks; + const auto neededTicks = (m_frames / (float)fps()) * 1000.0; + const auto sleepNeeded = (int)(neededTicks - elapsedTicks); + if (sleepNeeded > 0) { + ::SDL_Delay(sleepNeeded); + } + } + } +} + +void Game::removeJoystick(SDL_Event& e) { + const auto which = e.jdevice.which; + const auto found = m_gameControllers.find(which); + SDL_assert(found != m_gameControllers.end()); + auto controller = found->second; + const auto joystickId = controller->getJoystickId(); + m_mappedControllers.erase(joystickId); + m_gameControllers.erase(which); + SDL_Log("Joystick device %d removed (%zd controllers)", which, m_gameControllers.size()); +} + +void Game::addJoystick(SDL_Event& e) { + const auto which = e.jdevice.which; + SDL_assert(m_gameControllers.find(which) == m_gameControllers.end()); + auto controller = std::make_shared(which); + const auto joystickId = controller->getJoystickId(); + m_gameControllers[which] = controller; + SDL_assert(m_mappedControllers.find(joystickId) == m_mappedControllers.end()); + m_mappedControllers[joystickId] = which; + SDL_Log("Joystick device %d added (%zd controllers)", which, m_gameControllers.size()); +} + +// -1 if no controllers, otherwise index +int Game::chooseControllerIndex(const int who) const { + const auto count = m_gameControllers.size(); + if (count == 0) + return -1; + auto firstController = m_gameControllers.cbegin(); + if (count == 1 || (who == 1)) + return firstController->first; + auto secondController = (++firstController)->first; + return secondController; +} + +std::shared_ptr Game::chooseController(const int who) const { + const auto which = chooseControllerIndex(who); + if (which == -1) + return nullptr; + const auto found = m_gameControllers.find(which); + SDL_assert(found != m_gameControllers.cend()); + return found->second; +} + +void Game::updateTexture() { + verifySDLCall(::SDL_UpdateTexture(m_bitmapTexture.get(), nullptr, pixels(), displayWidth() * sizeof(Uint32)), "Unable to update texture: "); +} + +void Game::copyTexture() { + verifySDLCall( + ::SDL_RenderCopy(m_renderer.get(), m_bitmapTexture.get(), nullptr, nullptr), + "Unable to copy texture to renderer"); +} + +void Game::displayTexture() { + ::SDL_RenderPresent(m_renderer.get()); +} + +} \ No newline at end of file diff --git a/Gaming/src/GameController.cpp b/Gaming/src/GameController.cpp new file mode 100644 index 0000000..1ea8345 --- /dev/null +++ b/Gaming/src/GameController.cpp @@ -0,0 +1,73 @@ +#include "pch.h" +#include "GameController.h" +#include "Game.h" + +namespace Gaming { + + GameController::GameController(int index) + : m_index(index) { + open(); + } + + GameController::~GameController() { + close(); + } + + void GameController::open() { + SDL_assert(::SDL_NumJoysticks() > 0); + if (::SDL_IsGameController(m_index)) { + m_gameController = ::SDL_GameControllerOpen(m_index); + if (m_gameController == nullptr) { + Game::throwSDLException("Unable to open game controller: "); + } + openHapticController(); + auto name = ::SDL_GameControllerName(m_gameController); + ::SDL_Log("Game controller name: %s", name); + } + else { + ::SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Joystick is not a game controller!!"); + } + } + + void GameController::openHapticController() { + m_hapticController = ::SDL_HapticOpen(m_index); + if (m_hapticController == nullptr) { + Game::throwSDLException("Unable to open haptic controller: "); + } + Game::verifySDLCall(::SDL_HapticRumbleInit(m_hapticController), "Unable to initialise haptic controller: "); + m_hapticRumbleSupported = ::SDL_HapticRumbleSupported(m_hapticController) != SDL_FALSE; + } + + void GameController::closeHapticController() noexcept { + if (m_hapticController != nullptr) { + ::SDL_HapticClose(m_hapticController); + m_hapticController = nullptr; + } + m_hapticRumbleSupported = false; + } + + void GameController::close() noexcept { + if (m_gameController != nullptr) { + ::SDL_GameControllerClose(m_gameController); + m_gameController = nullptr; + } + closeHapticController(); + } + + void GameController::startRumble() noexcept { + if (m_hapticRumbleSupported) { + if (::SDL_HapticRumblePlay(m_hapticController, 1.0, 1000) < 0) { + ::SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Unable to start haptic rumble: %s", ::SDL_GetError()); + } + } + } + + void GameController::stopRumble() noexcept { + if (m_hapticRumbleSupported) { + if (::SDL_HapticRumbleStop(m_hapticController) < 0) { + ::SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Unable to stop haptic rumble: %s", ::SDL_GetError()); + } + } + } + +} \ No newline at end of file diff --git a/Gaming/src/Gaming.vcxproj b/Gaming/src/Gaming.vcxproj new file mode 100644 index 0000000..67e3ed5 --- /dev/null +++ b/Gaming/src/Gaming.vcxproj @@ -0,0 +1,178 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {BC526300-E3D0-44B1-B57B-312BAA6566AC} + Win32Proj + Gaming + + + + StaticLibrary + true + v142 + Unicode + + + StaticLibrary + v142 + true + Unicode + + + StaticLibrary + true + v142 + Unicode + + + StaticLibrary + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + false + ..\inc;..\..\inc;C:\Libraries\SDL2-2.0.7\include;$(IncludePath) + + + true + ..\inc;..\..\inc;C:\Libraries\SDL2-2.0.7\include;$(IncludePath) + + + true + ..\inc;..\..\inc;C:\Libraries\SDL2-2.0.7\include;$(IncludePath) + + + false + ..\inc;..\..\inc;C:\Libraries\SDL2-2.0.7\include;$(IncludePath) + + + + Use + Level3 + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + pch.h + stdcpp17 + Speed + true + false + false + true + AdvancedVectorExtensions + + + Windows + true + true + true + + + + + Use + Level3 + Disabled + true + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + pch.h + stdcpp17 + + + Windows + true + + + + + Use + Level3 + Disabled + true + _DEBUG;_LIB;%(PreprocessorDefinitions) + pch.h + stdcpp17 + + + Windows + true + + + + + Use + Level3 + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + pch.h + stdcpp17 + Speed + true + false + false + true + AdvancedVectorExtensions + + + Windows + true + true + true + + + + + + + Create + Create + Create + Create + + + + + + + + + + + \ No newline at end of file diff --git a/Gaming/src/Gaming.vcxproj.filters b/Gaming/src/Gaming.vcxproj.filters new file mode 100644 index 0000000..10b1d21 --- /dev/null +++ b/Gaming/src/Gaming.vcxproj.filters @@ -0,0 +1,35 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/Gaming/src/pch.cpp b/Gaming/src/pch.cpp new file mode 100644 index 0000000..64b7eef --- /dev/null +++ b/Gaming/src/pch.cpp @@ -0,0 +1,5 @@ +// pch.cpp: source file corresponding to the pre-compiled header + +#include "pch.h" + +// When you are using pre-compiled headers, this source file is necessary for compilation to succeed. diff --git a/Gaming/src/pch.h b/Gaming/src/pch.h new file mode 100644 index 0000000..49df2b3 --- /dev/null +++ b/Gaming/src/pch.h @@ -0,0 +1,20 @@ +// pch.h: This is a precompiled header file. +// Files listed below are compiled only once, improving build performance for future builds. +// This also affects IntelliSense performance, including code completion and many code browsing features. +// However, files listed here are ALL re-compiled if any one of them is updated between builds. +// Do not add files here that you will be updating frequently as this negates the performance advantage. + +#ifndef PCH_H +#define PCH_H + +#include +#include +#include +#include +#include + +#include + +#include + +#endif //PCH_H