Skip to content

Commit

Permalink
fix(graphics:sdl): sdl library switching
Browse files Browse the repository at this point in the history
  • Loading branch information
flavien-chenu committed Apr 7, 2024
1 parent 90bc0c2 commit 574f4b1
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(ARCADE_BIN_DIR ${CMAKE_CURRENT_LIST_DIR})
set(ARCADE_LIB_DIR ${CMAKE_CURRENT_LIST_DIR}/lib)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -fno-gnu-unique")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ARCADE_BIN_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ARCADE_LIB_DIR})
Expand Down
6 changes: 5 additions & 1 deletion core/src/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,13 @@ void Core::_handleWindowClose()
{
if (this->_window && this->_window->isOpen()) {
this->_stopAllGraphicsSounds();
this->_textures.clear();
this->_fonts.clear();
this->_window->close();
this->_sounds.clear();
this->_menu.updateScore(this->_game);
this->_sceneStage = MENU;
this->_window.reset();
}
}

Expand Down Expand Up @@ -592,7 +596,7 @@ void Core::run()
this->_game->compute(deltaTime);
this->_gameEntities = this->_game->getEntities();
this->_handleEvents();
if (this->_window->isOpen()) {
if (this->_window && this->_window->isOpen()) {
this->_renderEntities();
}
}
Expand Down
19 changes: 13 additions & 6 deletions core/src/menu/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,10 @@ void Menu::_changeGraphics(const std::shared_ptr<CheckBox>& checkBox)
void Menu::_exitAndPlayOldGame()
{
this->_sceneStage = RESUME;
this->_window->close();
if (this->_window) {
this->_clearLists();
this->_window.reset();
}
}

void Menu::_exitWithNewGame()
Expand All @@ -464,8 +467,11 @@ void Menu::_exitWithNewGame()
}
}
this->_sceneStage = NEWGAME;
if (this->_window)
if (this->_window) {
this->_window->close();
this->_clearLists();
this->_window.reset();
}
}

void Menu::_handleMouseMoveEvents(const std::shared_ptr<events::IMouseEvent>& mouse)
Expand Down Expand Up @@ -522,7 +528,7 @@ void Menu::_handleMouseButtonEvents(const std::shared_ptr<events::IMouseButtonEv
this->_selectGame();
}
}
for (auto checkBox : this->_graphicsCheckBoxes) {
for (auto &checkBox : this->_graphicsCheckBoxes) {
if (checkBox->isHovered(position)) {
this->_selectGame();
}
Expand Down Expand Up @@ -660,13 +666,14 @@ void Menu::run()
this->_previousSelectedGraphics();
if (!this->_window)
throw ArcadeError("Can't create window");
while (this->_window->isOpen()) {
while (this->_window && this->_window->isOpen()) {
this->_render();
this->_handleEvents();
if (this->_window->isOpen())
this->_render();
}
if (this->_music)
this->_music->setState(ISound::SoundState::STOP);
this->_clearLists();
this->_window.reset();
}

void Menu::_readScores()
Expand Down
6 changes: 3 additions & 3 deletions graphics/ncurses/src/window/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ Window::Window(const IWindow::WindowInitProps &props):
noecho();
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
mousemask(ALL_MOUSE_EVENTS, NULL);
mousemask(ALL_MOUSE_EVENTS, nullptr);
curs_set(0);
_window = std::unique_ptr<WINDOW, decltype(&delwin)>(newwin(_size.y, _size.x, 0, 0), &delwin);
}

Window::~Window()
{
wclear(stdscr);
close();
Window::close();
endwin();
}

Expand Down Expand Up @@ -89,7 +89,7 @@ void Window::render(const shared::graphics::TextProps &props) {
}

void Window::clear() {
wclear(_window.get());
werase(_window.get());
}

void Window::display() {
Expand Down
1 change: 1 addition & 0 deletions graphics/sdl2/sdl/text/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bool Font::load(const std::string &path, int ptSize) {
Font::~Font() {
if (_font)
TTF_CloseFont(_font);
_font = nullptr;
}

TTF_Font *Font::_safeFont() const {
Expand Down
4 changes: 2 additions & 2 deletions graphics/sdl2/sdl/window/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
** Window.cpp
*/

#include <iostream>
#include "Window.hpp"
#include "sdl/exception/Exception.hpp"
#include "sdl/initializer/Initializer.hpp"

using namespace sdl;

Expand All @@ -21,6 +21,7 @@ Window &Window::create(
const Vector2i &size,
Uint32 flags
) {
Initializer::init();
_window = SDL_CreateWindow(
title.c_str(),
position.x,
Expand Down Expand Up @@ -84,7 +85,6 @@ Renderer &Window::getRenderer() {
void Window::close()
{
if (_window) {
_renderer.reset();
SDL_DestroyWindow(_window);
}
_window = nullptr;
Expand Down
6 changes: 4 additions & 2 deletions graphics/sdl2/src/GraphicsProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ const GraphicsManifest GraphicsProvider::_manifest = {
}
};

GraphicsProvider::GraphicsProvider() {
sdl::Initializer::init();
GraphicsProvider::GraphicsProvider() = default;

GraphicsProvider::~GraphicsProvider() {
sdl::Initializer::quit();
}

const shared::graphics::GraphicsManifest &GraphicsProvider::getManifest() const noexcept {
Expand Down
2 changes: 1 addition & 1 deletion graphics/sdl2/src/GraphicsProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class arcade::graphics::sdl2::GraphicsProvider : public IGraphicsProvider
{
public:
GraphicsProvider();
~GraphicsProvider() override = default;
~GraphicsProvider() override;

/**
* @brief Get the manifest of the graphics library
Expand Down

0 comments on commit 574f4b1

Please sign in to comment.