Skip to content

Commit

Permalink
3.2.10
Browse files Browse the repository at this point in the history
- Completed issue #36 , adding a pause menu
- Modified mouse input scheme, hiding the glfw mouse functions from other classes behind a Engine::MouseInputMode enumeration.
  - Engine provides a mode for mouse controls, either normal (2d cursor) or free look.
  • Loading branch information
Yattabyte committed May 27, 2019
1 parent 94c103a commit c93e2ca
Show file tree
Hide file tree
Showing 25 changed files with 306 additions and 89 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
### reVision ###
################
cmake_minimum_required(VERSION 3.10)
project(reVision VERSION 3.2.9 DESCRIPTION "Video game engine project.")
project(reVision VERSION 3.2.10 DESCRIPTION "Video game engine project.")

# Get dependency directory locations from the user
set(ASSIMP_DIR "" CACHE PATH "ASSIMP root directory")
Expand Down
1 change: 1 addition & 0 deletions app/Configs/binds.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"ESCAPE" "256"
"FORWARD" "87"
"BACKWARD" "83"
"LEFT" "65"
Expand Down
26 changes: 24 additions & 2 deletions src/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,19 @@ void Engine::tick()
// Updated hard-coded bindings
double mouseX, mouseY;
glfwGetCursorPos(m_renderingContext.window, &mouseX, &mouseY);
m_actionState[ActionState::MOUSE_X] = (float)mouseX;
m_actionState[ActionState::MOUSE_Y] = (float)mouseY;
m_actionState[ActionState::MOUSE_L] = (float)glfwGetMouseButton(m_renderingContext.window, GLFW_MOUSE_BUTTON_LEFT);
m_actionState[ActionState::MOUSE_R] = (float)glfwGetMouseButton(m_renderingContext.window, GLFW_MOUSE_BUTTON_RIGHT);
switch (m_mouseInputMode) {
case NORMAL:
m_actionState[ActionState::MOUSE_X] = (float)mouseX;
m_actionState[ActionState::MOUSE_Y] = (float)mouseY;
break;
case FREE_LOOK:
m_actionState[ActionState::LOOK_X] = (float)mouseX;
m_actionState[ActionState::LOOK_Y] = (float)mouseY;
glfwSetCursorPos(m_renderingContext.window, 0, 0);
break;
};

// Update Engine State / Begin Frame
m_moduleGraphics.getCameraBuffer().waitFrame(m_frameCount);
Expand Down Expand Up @@ -266,6 +275,19 @@ void Engine::shutDown()
glfwSetWindowShouldClose(m_renderingContext.window, GLFW_TRUE);
}

void Engine::setMouseInputMode(const MouseInputMode & mode)
{
m_mouseInputMode = mode;
switch (mode) {
case NORMAL:
glfwSetInputMode(m_renderingContext.window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
break;
case FREE_LOOK:
glfwSetInputMode(m_renderingContext.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPos(m_renderingContext.window, m_actionState[ActionState::LOOK_X], m_actionState[ActionState::LOOK_Y]);
}
}

GLFWwindow * Engine::getRenderingContext() const
{
return m_renderingContext.window;
Expand Down
12 changes: 11 additions & 1 deletion src/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <string>


constexpr char ENGINE_VERSION[] = "3.2.9";
constexpr char ENGINE_VERSION[] = "3.2.10";

struct GLFWwindow;
class Engine;
Expand All @@ -44,6 +44,13 @@ struct Auxilliary_Context {
/** The main game engine object. Encapsulates the entire engine state. */
class Engine {
public:
/***/
enum MouseInputMode {
NORMAL,
FREE_LOOK
};


// Public (de)Constructors
/** Destroys the engine. */
~Engine();
Expand All @@ -62,6 +69,8 @@ class Engine {
bool shouldClose();
/** Tells the engine to shut down. */
void shutDown();
/***/
void setMouseInputMode(const MouseInputMode & mode);


// Public Accessors
Expand Down Expand Up @@ -124,6 +133,7 @@ class Engine {
float m_refreshRate = 120.0f;
float m_useFullscreen = 1.0f;
float m_vsync = 1.0f;
MouseInputMode m_mouseInputMode = MouseInputMode::NORMAL;
glm::ivec2 m_windowSize = glm::ivec2(1);
SoundManager m_soundManager;
AssetManager m_assetManager;
Expand Down
12 changes: 11 additions & 1 deletion src/Engine.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ struct Auxilliary_Context {
/** The main game engine object. Encapsulates the entire engine state. */
class Engine {
public:
/***/
enum MouseInputMode {
NORMAL,
FREE_LOOK
};


// Public (de)Constructors
/** Destroys the engine. */
~Engine();
Expand All @@ -62,6 +69,8 @@ public:
bool shouldClose();
/** Tells the engine to shut down. */
void shutDown();
/***/
void setMouseInputMode(const MouseInputMode & mode);


// Public Accessors
Expand Down Expand Up @@ -124,6 +133,7 @@ private:
float m_refreshRate = 120.0f;
float m_useFullscreen = 1.0f;
float m_vsync = 1.0f;
MouseInputMode m_mouseInputMode = MouseInputMode::NORMAL;
glm::ivec2 m_windowSize = glm::ivec2(1);
SoundManager m_soundManager;
AssetManager m_assetManager;
Expand All @@ -148,4 +158,4 @@ private:
EngineState * m_engineState = nullptr;
};

#endif // ENGINE_H
#endif // ENGINE_H
77 changes: 77 additions & 0 deletions src/Modules/UI/Macro Elements/PauseMenu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once
#ifndef PAUSEMENU_H
#define PAUSEMENU_H

#include "Modules/UI/Macro Elements/Menu.h"
#include "Modules/UI/Macro Elements/OptionsMenu.h"
#include "Engine.h"


/** A UI element serving as a pause menu. */
class PauseMenu : public Menu {
public:
// Public Interaction Enums
enum interact {
on_resume_game = last_interact_index,
on_options,
on_quit,
};


// Public (de)Constructors
/** Destroy the start menu. */
inline ~PauseMenu() = default;
/** Construct a start menu.
@param engine the engine to use. */
inline PauseMenu(Engine * engine) : Menu(engine) {
// Title
m_title->setText("PAUSE MENU");

// Add 'Start Game' button
auto startButton = std::make_shared<Button>(engine, "RESUME");
startButton->addCallback(Button::on_pressed, [&]() {
setVisible(false);
enactCallback(on_resume_game);
});
addButton(startButton);

// Add 'Options' button
auto optionsButton = std::make_shared<Button>(engine, " OPTIONS >");
m_optionsMenu = std::make_shared<OptionsMenu>(engine);
m_optionsMenu->setVisible(false);
optionsButton->addCallback(Button::on_pressed, [&]() {
m_backPanel->setVisible(false);
m_optionsMenu->setVisible(true);
enactCallback(on_options);
});
m_optionsMenu->addCallback(OptionsMenu::on_back, [&]() {
m_backPanel->setVisible(true);
m_optionsMenu->setVisible(false);
});
addButton(optionsButton);
addElement(m_optionsMenu);

// Add 'Quit' button
auto quitButton = std::make_shared<Button>(engine, "QUIT");
quitButton->addCallback(Button::on_pressed, [&, engine]() {
setVisible(false);
engine->shutDown();
enactCallback(on_quit);
});
addButton(quitButton);
}


// Public Interface Implementations
inline virtual void setScale(const glm::vec2 & scale) override {
m_optionsMenu->setScale(scale);
Menu::setScale(scale);
}


protected:
// Protected Attributes
std::shared_ptr<UI_Element> m_optionsMenu;
};

#endif // PAUSEMENU_H
72 changes: 35 additions & 37 deletions src/Modules/UI/UI_M.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,66 +20,64 @@ void UI_Module::initialize(Engine * engine)
preferences.addCallback(PreferenceState::C_WINDOW_WIDTH, m_aliveIndicator, [&](const float &f) {
m_renderSize.x = (int)f;
calcOthoProj(m_renderSize, m_projectionBuffer);
m_startMenu->setScale(m_renderSize);
if (m_uiElement)
m_uiElement->setScale(m_renderSize);
});
preferences.addCallback(PreferenceState::C_WINDOW_HEIGHT, m_aliveIndicator, [&](const float &f) {
m_renderSize.y = (int)f;
calcOthoProj(m_renderSize, m_projectionBuffer);
m_startMenu->setScale(m_renderSize);
});
calcOthoProj(m_renderSize, m_projectionBuffer);

// Create main menu
m_startMenu = std::make_shared<StartMenu>(m_engine);
m_startMenu->setScale(m_renderSize);
m_startMenu->addCallback(StartMenu::on_start_game, [&]() {
m_menuState = on_game;
});
m_startMenu->addCallback(StartMenu::on_start_puzzle, [&]() {
m_menuState = on_puzzle;
});
m_startMenu->addCallback(StartMenu::on_options, [&]() {
m_menuState = on_menu;
});
m_startMenu->addCallback(StartMenu::on_quit, [&]() {
m_menuState = on_exit;
if (m_uiElement)
m_uiElement->setScale(m_renderSize);
});
calcOthoProj(m_renderSize, m_projectionBuffer);
}

void UI_Module::frameTick(const float & deltaTime)
{
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
glViewport(0, 0, (GLsizei)m_renderSize.x, (GLsizei)m_renderSize.y);
glScissor(0, 0, (GLsizei)m_renderSize.x, (GLsizei)m_renderSize.y);

m_projectionBuffer.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 2);
m_startMenu->renderElement(deltaTime, glm::vec2(0.0f), m_renderSize);
if (m_uiElement) {
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
glViewport(0, 0, (GLsizei)m_renderSize.x, (GLsizei)m_renderSize.y);
glScissor(0, 0, (GLsizei)m_renderSize.x, (GLsizei)m_renderSize.y);

glDisable(GL_SCISSOR_TEST);
glDisable(GL_BLEND);
Shader::Release();
m_projectionBuffer.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 2);
m_uiElement->renderElement(deltaTime, glm::vec2(0.0f), m_renderSize);

glDisable(GL_SCISSOR_TEST);
glDisable(GL_BLEND);
Shader::Release();
}
}

void UI_Module::setRootElement(const std::shared_ptr<UI_Element>& rootElement)
{
m_uiElement = rootElement;
m_uiElement->setScale(m_renderSize);
}

UI_Module::MenuState UI_Module::getMenuState() const
void UI_Module::clearRootElement()
{
return m_menuState;
if (m_uiElement)
m_uiElement.reset();
}

void UI_Module::applyMouseEvent(const MouseEvent & mouseEvent)
{
m_startMenu->mouseAction(mouseEvent);
if (m_uiElement)
m_uiElement->mouseAction(mouseEvent);
}

void UI_Module::applyChar(const unsigned int & character)
{
m_startMenu->keyChar(character);
if (m_uiElement)
m_uiElement->keyChar(character);
}

void UI_Module::applyKey(const int & key, const int & scancode, const int & action, const int & mods)
{
m_startMenu->keyboardAction(key, scancode, action, mods);
if (m_uiElement)
m_uiElement->keyboardAction(key, scancode, action, mods);
}
20 changes: 6 additions & 14 deletions src/Modules/UI/UI_M.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@
/** A module responsible for the overall user interface. */
class UI_Module : public Engine_Module {
public:
// Public State Enumeration
enum MenuState {
on_menu,
on_game,
on_puzzle,
on_exit
};


// Public (de)Constructors
/** Destroy the UI module. */
inline ~UI_Module() = default;
Expand All @@ -35,9 +26,11 @@ class UI_Module : public Engine_Module {


// Public Methods
/** Return the current state of this menu.
@return enum representing the state of this menu. */
MenuState getMenuState() const;
/** Place a root UI element to receive input and be rendered.
@param rootElement the main element of focus for this UI system. */
void setRootElement(const std::shared_ptr<UI_Element> & rootElement);
/** Remove the root UI element from the UI system. */
void clearRootElement();
/** Propagates a mouse event to all UI elements.
@param mouseEvent the mouse event to apply. */
void applyMouseEvent(const MouseEvent & mouseEvent);
Expand All @@ -56,9 +49,8 @@ class UI_Module : public Engine_Module {
std::shared_ptr<bool> m_aliveIndicator = std::make_shared<bool>(true);
glm::ivec2 m_renderSize = glm::ivec2(1);
StaticBuffer m_projectionBuffer;
std::shared_ptr<UI_Element> m_startMenu, m_optionsMenu;
std::shared_ptr<UI_Element> m_uiElement;
MouseEvent m_mouseEvent;
MenuState m_menuState = on_menu;
};

#endif // UI_MODULE_H
4 changes: 2 additions & 2 deletions src/Modules/World/ECS/ecsComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct ECSComponent : public BaseECSComponent {
};

template <typename Component>
const uint32_t ECSComponentCreate(std::vector<uint8_t> & memory, const EntityHandle & entity, BaseECSComponent * comp) {
inline const uint32_t ECSComponentCreate(std::vector<uint8_t> & memory, const EntityHandle & entity, BaseECSComponent * comp) {
const size_t index = memory.size();
memory.resize(index+Component::SIZE);
Component * component = new(&memory[index])Component(*(Component*)comp);
Expand All @@ -55,7 +55,7 @@ const uint32_t ECSComponentCreate(std::vector<uint8_t> & memory, const EntityHan
}

template <typename Component>
void ECSComponentFree(BaseECSComponent * comp) {
inline void ECSComponentFree(BaseECSComponent * comp) {
Component * component = (Component*)comp;
component->~Component();
}
Expand Down
Loading

0 comments on commit c93e2ca

Please sign in to comment.