ProtoTank/ProtoTank.cpp
JackCarterSmith db830a2a4e
Window size handling
Do the job, but need more control over resize limit
2024-10-29 19:14:17 +01:00

156 lines
5.1 KiB
C++

#include <stdexcept>
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/ContextSettings.hpp>
#include <SFML/System/Vector2.hpp>
#include "icon.h"
#include "Engine/Misc/Logger.hpp"
#include "Game.hpp"
struct MainWindowParams {
sf::Vector2u wndSize = {0, 0}; // Ignored on fullscreen mode
sf::ContextSettings wndContextSettings;
bool wndFullscreen = false;
};
static void CreateWindow(std::shared_ptr<sf::RenderWindow> wnd, MainWindowParams& params);
//static void ForceWindowRatio(sf::RenderWindow* wnd);
int main(int argc, char** argv) {
Logger& log = Logger::getInstance();
log.PrintInfo(LOGGER_MSG_FT("Create main window"));
MainWindowParams wndParams;
Game::GetDefaultWindowSize(wndParams.wndSize.x, wndParams.wndSize.y);
wndParams.wndContextSettings.depthBits = 0;
wndParams.wndContextSettings.stencilBits = 0;
wndParams.wndContextSettings.antialiasingLevel = 8;
wndParams.wndContextSettings.sRgbCapable = true;
auto mainWindow = std::make_shared<sf::RenderWindow>();
try {
CreateWindow(mainWindow, wndParams);
} catch (const std::exception& ex) {
log.PrintCritical(std::string(ex.what()));
return EXIT_FAILURE;
}
log.PrintInfo(LOGGER_MSG_FT("Create game instance"));
#ifdef DEBUG
Game& arcadeGame = Game::getInstance(mainWindow, true);
#else
Game& arcadeGame = Game::getInstance(mainWindow, false);
#endif
GAME_STATUS status = GAME_INIT;
sf::FloatRect visibleArea(0, 0, WNDSIZE_DEFAULT_W, WNDSIZE_DEFAULT_H);
for ( ;; ) {
sf::Event event;
while (mainWindow->pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
mainWindow->close();
break;
case sf::Event::Resized:
visibleArea.width = event.size.width;
visibleArea.height = event.size.height;
mainWindow->setView(sf::View(visibleArea)); // Adapt the view resolution
arcadeGame.EventWindowSizeChanged(event.size.width, event.size.height);
//ForceWindowRatio(mainWindow.get());
break;
//case sf::Event::LostFocus:
// EventSuspending();
// break;
//case sf::Event::GainedFocus:
// EventResuming();
// break;
default:
break;
}
// Focus only actions
if (mainWindow->hasFocus()) {
switch (event.type) {
case sf::Event::MouseMoved:
arcadeGame.EventMouseMoved(event.mouseMove.x, event.mouseMove.y);
break;
default:
break;
}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
mainWindow->close();
status = GAME_QUIT;
} else if ((sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt) || sf::Keyboard::isKeyPressed(sf::Keyboard::RAlt)) && sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) {
wndParams.wndFullscreen = !wndParams.wndFullscreen;
try {
CreateWindow(mainWindow, wndParams);
} catch (const std::exception& ex) {
log.PrintCritical(std::string(ex.what()));
return EXIT_FAILURE;
}
arcadeGame.EventWindowSizeChanged(mainWindow->getSize().x, mainWindow->getSize().y);
}
status = arcadeGame.Tick();
if (status == GAME_QUIT)
break;
}
log.PrintInfo(LOGGER_MSG_FT("Bye bye!"));
return EXIT_SUCCESS;
}
static void CreateWindow(std::shared_ptr<sf::RenderWindow> wnd, MainWindowParams& params) {
sf::Uint32 wndStyle = sf::Style::Close | sf::Style::Titlebar;
sf::VideoMode vMode;
if (wnd == nullptr)
throw std::invalid_argument("Failed to create the window");
if (params.wndFullscreen) {
vMode = sf::VideoMode::getFullscreenModes().front();
wndStyle = sf::Style::Fullscreen;
} else {
vMode = sf::VideoMode(params.wndSize.x, params.wndSize.y);
wndStyle = sf::Style::Close | sf::Style::Titlebar | sf::Style::Resize;
}
/*if(mode == BORDERLESS) {
//vMode = sf::VideoMode::getFullscreenModes().front();
//vMode = sf::VideoMode(params.wndSize.x, params.wndSize.y);
vMode = sf::VideoMode::getDesktopMode();
wndStyle = sf::Style::None;
}*/
wnd->create(vMode, "ProtoTank", wndStyle, params.wndContextSettings);
wnd->setVerticalSyncEnabled(false); // Never use simultaneously with framerate limiter
wnd->setFramerateLimit(60); // Never use simultaneously with VSync
wnd->setKeyRepeatEnabled(false);
wnd->setMouseCursorVisible(true);
wnd->setIcon(64, 64, _aicon_bin);
}
/*
inline static void ForceWindowRatio(sf::RenderWindow* wnd) {
auto wndSize = wnd->getSize();
if (wndSize.y * W_RATIO <= wndSize.x)
wndSize.x = wndSize.y * W_RATIO;
else if (wndSize.x * H_RATIO <= wndSize.y)
wndSize.y = wndSize.x * H_RATIO;
wnd->setSize(wndSize);
}
*/