Skip to content

Commit

Permalink
[GameState|HUD|InterfaceState] Fullscreen and resize handling added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jan 27, 2020
1 parent 54493e3 commit 2d417f6
Show file tree
Hide file tree
Showing 22 changed files with 189 additions and 82 deletions.
2 changes: 2 additions & 0 deletions client/include/core/ClientApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ClientApplication : public gk::CoreApplication {
void init() override;

private:
void handleEvents() override;

void initOpenGL();

gk::KeyboardHandler m_keyboardHandler;
Expand Down
2 changes: 2 additions & 0 deletions client/include/hud/Crosshair.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Crosshair : public gk::Drawable {
public:
Crosshair();

void setup();

private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

Expand Down
2 changes: 2 additions & 0 deletions client/include/hud/HUD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class HUD : public gk::Transformable, public gk::Drawable {
public:
HUD(ClientPlayer &player, ClientWorld &world, ClientCommandHandler &client);

void setup();

void onEvent(const SDL_Event &event);

void update();
Expand Down
6 changes: 6 additions & 0 deletions client/include/states/InterfaceState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@

#include <gk/core/ApplicationState.hpp>
#include <gk/gl/Shader.hpp>
#include <gk/graphics/RectangleShape.hpp>

class InterfaceState : public gk::ApplicationState {
public:
InterfaceState(gk::ApplicationState *parent = nullptr);

protected:
void setup();
void onEvent(const SDL_Event &event) override;

void prepareDraw(gk::RenderTarget &target, gk::RenderStates &states) const;

private:
gk::Shader m_shader;
// gk::View m_view;

glm::mat4 m_projectionMatrix;

gk::RectangleShape m_background;
};

#endif // INTERFACESTATE_HPP_
2 changes: 0 additions & 2 deletions client/include/states/InventoryState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class InventoryState : public InterfaceState {
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

std::unique_ptr<Widget> m_widget;

gk::RectangleShape m_background;
};

#endif // INVENTORYSTATE_HPP_
2 changes: 0 additions & 2 deletions client/include/states/LuaGUIState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class LuaGUIState : public InterfaceState {
std::deque<InventoryWidget> m_inventoryWidgets;
std::vector<std::unique_ptr<Widget>> m_widgets;
std::vector<std::unique_ptr<gk::Drawable>> m_drawables;

gk::RectangleShape m_background;
};

#endif // LUAGUISTATE_HPP_
2 changes: 0 additions & 2 deletions client/include/states/PauseMenuState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class PauseMenuState : public InterfaceState {

MenuWidget m_menuWidget{1, 4};

gk::RectangleShape m_background;

Client &m_client;
};

Expand Down
2 changes: 0 additions & 2 deletions client/include/states/SettingsMenuState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class SettingsMenuState : public InterfaceState {

void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

gk::RectangleShape m_background;

MenuWidget m_menuWidget;
TextButton m_doneButton;

Expand Down
9 changes: 9 additions & 0 deletions client/source/core/ClientApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ void ClientApplication::init() {
// m_stateStack.push<ServerLoadingState>(game);
}

void ClientApplication::handleEvents() {
gk::CoreApplication::handleEvents();

if ((Config::isFullscreenModeEnabled && m_window.getWindowMode() != gk::Window::Mode::Fullscreen)
|| (!Config::isFullscreenModeEnabled && m_window.getWindowMode() != gk::Window::Mode::Windowed)) {
m_window.setWindowMode(Config::isFullscreenModeEnabled ? gk::Window::Mode::Fullscreen : gk::Window::Mode::Windowed);
}
}

void ClientApplication::initOpenGL() {
// Enable transparency
glCheck(glEnable(GL_BLEND));
Expand Down
12 changes: 10 additions & 2 deletions client/source/gui/MenuWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ void MenuWidget::reset(u16 width, u16 height) {
}

void MenuWidget::onEvent(const SDL_Event &event) {
for (TextButton &button : m_buttons) {
button.onEvent(event);
for (std::size_t i = 0 ; i < m_buttons.size() ; ++i) {
m_buttons.at(i).onEvent(event);

if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
int x = i % m_width;
int y = i / m_width;

m_buttons.at(i).setPosition(SCREEN_WIDTH / getScale().x / 2 - (m_width * (m_buttons.at(i).width() + s_horizontalSpacing) - s_horizontalSpacing) / 2 + x * (m_buttons.at(i).width() + s_horizontalSpacing),
SCREEN_HEIGHT / getScale().y / 2 - (m_height * (m_buttons.at(i).height() + s_verticalSpacing) - s_verticalSpacing) / 2 + y * (m_buttons.at(i).height() + s_verticalSpacing), 0);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions client/source/hud/Crosshair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include "Crosshair.hpp"

Crosshair::Crosshair() {
setup();
}

void Crosshair::setup() {
float xFactor = SCREEN_WIDTH * SCREEN_HEIGHT / 100;
float yFactor = SCREEN_HEIGHT * SCREEN_WIDTH / 100;

Expand Down
6 changes: 6 additions & 0 deletions client/source/hud/HUD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ HUD::HUD(ClientPlayer &player, ClientWorld &world, ClientCommandHandler &client)
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/basic.f.glsl");
m_shader.linkProgram();

setup();
}

void HUD::setup() {
m_orthoMatrix = glm::ortho(0.0f, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT, 0.0f);

m_hotbar.setPosition(SCREEN_WIDTH / getScale().x / 2 - m_hotbar.width() / 2, SCREEN_HEIGHT / getScale().y - m_hotbar.height(), 0);

m_blockInfoWidget.setPosition(SCREEN_WIDTH / getScale().x / 2 - m_blockInfoWidget.width() / 2, 2, 0);

m_crosshair.setup();
}

void HUD::onEvent(const SDL_Event &event) {
Expand Down
52 changes: 31 additions & 21 deletions client/source/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,43 @@ GameState::GameState(const std::string &host, int port) {
}

void GameState::onEvent(const SDL_Event &event) {
if (event.type == SDL_MOUSEMOTION) {
if(SCREEN_WIDTH / 2 != event.motion.x || SCREEN_HEIGHT / 2 != event.motion.y) {
m_player.turnH(event.motion.xrel * 0.01 * Config::mouseSensitivity);
m_player.turnV(-event.motion.yrel * 0.01 * Config::mouseSensitivity);
if (&m_stateStack->top() == this) {
if (event.type == SDL_MOUSEMOTION) {
if(SCREEN_WIDTH / 2 != event.motion.x || SCREEN_HEIGHT / 2 != event.motion.y) {
m_player.turnH(event.motion.xrel * 0.01 * Config::mouseSensitivity);
m_player.turnV(-event.motion.yrel * 0.01 * Config::mouseSensitivity);

gk::Mouse::resetToWindowCenter();
gk::Mouse::resetToWindowCenter();
}
}
}
else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE && &m_stateStack->top() == this) {
m_stateStack->push<PauseMenuState>(m_client, this);
}
else if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
// FIXME
// m_stateStack->push<PauseMenuState>(this);

gk::Mouse::setCursorGrabbed(false);
gk::Mouse::setCursorVisible(true);
else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
m_stateStack->push<PauseMenuState>(m_client, this);
}
else if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
gk::Mouse::setCursorGrabbed(true);
gk::Mouse::setCursorVisible(false);
else if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
// FIXME
// m_stateStack->push<PauseMenuState>(this);

gk::Mouse::setCursorGrabbed(false);
gk::Mouse::setCursorVisible(true);
}
else if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
gk::Mouse::setCursorGrabbed(true);
gk::Mouse::setCursorVisible(false);
}
}

if (m_clientCommandHandler.isRegistryInitialized())
m_hud.onEvent(event);
}

if (m_clientCommandHandler.isRegistryInitialized())
m_hud.onEvent(event);
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
SCREEN_WIDTH = event.window.data1;
SCREEN_HEIGHT = event.window.data2;

m_camera.setAspectRatio((float)SCREEN_WIDTH / SCREEN_HEIGHT);
m_hud.setup();
}
}

void GameState::update() {
Expand Down
26 changes: 25 additions & 1 deletion client/source/states/InterfaceState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,43 @@ InterfaceState::InterfaceState(gk::ApplicationState *parent) : gk::ApplicationSt
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/basic.f.glsl");
m_shader.linkProgram();

m_background.setFillColor(gk::Color{0, 0, 0, 127});

setup();
}

void InterfaceState::setup() {
m_projectionMatrix = glm::ortho(0.0f, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT, 0.0f);

m_background.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);

// m_view.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
// m_view.setCenter(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
}

void InterfaceState::prepareDraw(gk::RenderTarget &, gk::RenderStates &states) const {
void InterfaceState::onEvent(const SDL_Event &event) {
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
if (m_parent)
m_parent->onEvent(event);
else {
SCREEN_WIDTH = event.window.data1;
SCREEN_HEIGHT = event.window.data2;
}

setup();
}
}

void InterfaceState::prepareDraw(gk::RenderTarget &target, gk::RenderStates &states) const {
states.transform *= getTransform();
states.shader = &m_shader;
// states.vertexAttributes = gk::VertexAttribute::Only2d;

states.projectionMatrix = m_projectionMatrix;

// target.setView(m_view);

if (m_parent)
target.draw(m_background, states);
}

15 changes: 8 additions & 7 deletions client/source/states/InventoryState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ InventoryState::InventoryState(gk::ApplicationState *parent) : InterfaceState(pa
gk::Mouse::setCursorGrabbed(false);
gk::Mouse::setCursorVisible(true);
gk::Mouse::resetToWindowCenter();

m_background.setFillColor(gk::Color{0, 0, 0, 127});
m_background.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
}

void InventoryState::onEvent(const SDL_Event &event) {
// if (m_parent)
// m_parent->onEvent(event);
InterfaceState::onEvent(event);

if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
if (m_widget) {
m_widget->setPosition(SCREEN_WIDTH / 2.0 - m_widget->getGlobalBounds().width / 2.0,
SCREEN_HEIGHT / 2.0 - m_widget->getGlobalBounds().height / 2.0, 0);
}
}

if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
gk::Mouse::setCursorGrabbed(true);
Expand Down Expand Up @@ -57,8 +60,6 @@ void InventoryState::draw(gk::RenderTarget &target, gk::RenderStates states) con

prepareDraw(target, states);

target.draw(m_background, states);

if (m_widget)
target.draw(*m_widget, states);
}
Expand Down
7 changes: 2 additions & 5 deletions client/source/states/LuaGUIState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ LuaGUIState::LuaGUIState(ClientCommandHandler &client, ClientPlayer &player, Cli
gk::Mouse::setCursorVisible(true);
gk::Mouse::resetToWindowCenter();

m_background.setFillColor(gk::Color{0, 0, 0, 127});
m_background.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);

m_mainWidget.setScale(GUI_SCALE, GUI_SCALE);

while (!packet.endOfPacket())
loadGUI(player, world, packet);
}

void LuaGUIState::onEvent(const SDL_Event &event) {
InterfaceState::onEvent(event);

if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
gk::Mouse::setCursorGrabbed(true);
gk::Mouse::setCursorVisible(false);
Expand Down Expand Up @@ -98,8 +97,6 @@ void LuaGUIState::draw(gk::RenderTarget &target, gk::RenderStates states) const
states.transform *= m_mainWidget.getTransform();
states.viewMatrix = gk::Transform::Identity;

target.draw(m_background, states);

for (auto &it : m_drawables)
target.draw(*it, states);

Expand Down
44 changes: 30 additions & 14 deletions client/source/states/PauseMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ PauseMenuState::PauseMenuState(Client &client, gk::ApplicationState *parent)
gk::Mouse::setCursorVisible(true);
gk::Mouse::resetToWindowCenter();

m_background.setFillColor(gk::Color{0, 0, 0, 127});
m_background.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);

m_menuWidget.setScale(GUI_SCALE, GUI_SCALE, 1);
m_menuWidget.addButton("Back to Game", [this] (TextButton &) { gk::Mouse::setCursorGrabbed(true); gk::Mouse::setCursorVisible(false); m_stateStack->pop(); });
m_menuWidget.addButton("Options...", [this] (TextButton &) { m_stateStack->push<SettingsMenuState>(m_parent); });

m_menuWidget.addButton("Back to Game", [this] (TextButton &) {
gk::Mouse::setCursorGrabbed(true);
gk::Mouse::setCursorVisible(false);

m_stateStack->pop();
});

m_menuWidget.addButton("Options...", [this] (TextButton &) {
m_stateStack->push<SettingsMenuState>(this);
});

m_menuWidget.addButton("Title Screen", [this] (TextButton &) {
// m_client.disconnect();
Expand All @@ -52,14 +58,23 @@ PauseMenuState::PauseMenuState(Client &client, gk::ApplicationState *parent)
}

void PauseMenuState::onEvent(const SDL_Event &event) {
m_menuWidget.onEvent(event);
InterfaceState::onEvent(event);

if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
gk::Mouse::setCursorGrabbed(true);
gk::Mouse::setCursorVisible(false);
gk::Mouse::resetToWindowCenter();
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
if (&m_stateStack->top() != this)
m_menuWidget.onEvent(event);
}

m_stateStack->pop();
if (&m_stateStack->top() == this) {
m_menuWidget.onEvent(event);

if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
gk::Mouse::setCursorGrabbed(true);
gk::Mouse::setCursorVisible(false);
gk::Mouse::resetToWindowCenter();

m_stateStack->pop();
}
}
}

Expand All @@ -70,9 +85,10 @@ void PauseMenuState::draw(gk::RenderTarget &target, gk::RenderStates states) con
if (m_parent)
target.draw(*m_parent, states);

prepareDraw(target, states);
if (&m_stateStack->top() == this) {
prepareDraw(target, states);

target.draw(m_background, states);
target.draw(m_menuWidget, states);
target.draw(m_menuWidget, states);
}
}

Loading

0 comments on commit 2d417f6

Please sign in to comment.