Browse Source

Add pause and play buttons

master
C. J. Howard 7 years ago
parent
commit
88243f4c31
10 changed files with 203 additions and 36 deletions
  1. +1
    -1
      data
  2. +79
    -7
      src/application.cpp
  3. +10
    -0
      src/application.hpp
  4. +2
    -1
      src/render-passes.cpp
  5. +5
    -3
      src/render-passes.hpp
  6. +2
    -0
      src/states/main-menu-state.cpp
  7. +100
    -2
      src/states/play-state.cpp
  8. +4
    -0
      src/states/play-state.hpp
  9. +0
    -16
      src/states/title-state.cpp
  10. +0
    -6
      src/states/title-state.hpp

+ 1
- 1
data

@ -1 +1 @@
Subproject commit bca60d7e0fe476dcf32de8ab4f17d08326ed37b9
Subproject commit c11a70bf013de27252281cbbd80788ad3a1b5eaf

+ 79
- 7
src/application.cpp View File

@ -26,6 +26,7 @@
#include "states/title-state.hpp" #include "states/title-state.hpp"
#include "states/main-menu-state.hpp" #include "states/main-menu-state.hpp"
#include "states/play-state.hpp" #include "states/play-state.hpp"
#include "game/colony.hpp"
#include "debug.hpp" #include "debug.hpp"
#include "camera-controller.hpp" #include "camera-controller.hpp"
#include <cstdlib> #include <cstdlib>
@ -349,6 +350,7 @@ int Application::execute()
t = 0.0f; t = 0.0f;
dt = 1.0f / 60.0f; dt = 1.0f / 60.0f;
float accumulator = 0.0f; float accumulator = 0.0f;
float maxFrameTime = 0.25f;
int performanceSampleSize = 15; // Number of frames to sample int performanceSampleSize = 15; // Number of frames to sample
int performanceSampleFrame = 0; // Current sample frame int performanceSampleFrame = 0; // Current sample frame
@ -364,7 +366,7 @@ int Application::execute()
frameTimer.reset(); frameTimer.reset();
// Add frame time (in seconds) to accumulator // Add frame time (in seconds) to accumulator
accumulator += frameTime / 1000.0f;
accumulator += std::min(frameTime / 1000.0f, maxFrameTime);
// If the user tried to close the application // If the user tried to close the application
if (inputManager->wasClosed() || escape.isTriggered()) if (inputManager->wasClosed() || escape.isTriggered())
@ -433,11 +435,15 @@ int Application::execute()
performanceSampleFrame = 0; performanceSampleFrame = 0;
// Update frame time label // Update frame time label
std::string frameTimeString;
std::stringstream stream;
stream << meanFrameTime;
stream >> frameTimeString;
frameTimeLabel->setText(frameTimeString);
if (frameTimeLabel->isVisible())
{
std::string frameTimeString;
std::stringstream stream;
stream.precision(2);
stream << std::fixed << meanFrameTime;
stream >> frameTimeString;
frameTimeLabel->setText(frameTimeString);
}
} }
// Update UI // Update UI
@ -586,6 +592,9 @@ bool Application::loadModels()
antHillModelInstance.setRotation(glm::angleAxis(glm::radians(90.0f), Vector3(1, 0, 0))); antHillModelInstance.setRotation(glm::angleAxis(glm::radians(90.0f), Vector3(1, 0, 0)));
nestModelInstance.setModel(nestModel); nestModelInstance.setModel(nestModel);
// Create terrain
terrain.create(255, 255, Vector3(50, 20, 50));
return true; return true;
} }
@ -623,9 +632,12 @@ bool Application::loadScene()
lightingPass.setRenderTarget(&defaultRenderTarget); lightingPass.setRenderTarget(&defaultRenderTarget);
lightingPass.setShadowMap(0); lightingPass.setShadowMap(0);
lightingPass.setShadowCamera(&camera); lightingPass.setShadowCamera(&camera);
lightingPass.setModelLoader(modelLoader);
defaultCompositor.addPass(&lightingPass); defaultCompositor.addPass(&lightingPass);
// Setup debug pass
debugPass.setRenderTarget(&defaultRenderTarget);
defaultCompositor.addPass(&debugPass);
// Load compositor // Load compositor
defaultCompositor.load(nullptr); defaultCompositor.load(nullptr);
@ -678,6 +690,8 @@ bool Application::loadUI()
levelActiveTexture = textureLoader->load("data/textures/ui-level-active.png"); levelActiveTexture = textureLoader->load("data/textures/ui-level-active.png");
levelInactiveTexture = textureLoader->load("data/textures/ui-level-inactive.png"); levelInactiveTexture = textureLoader->load("data/textures/ui-level-inactive.png");
levelConnectorTexture = textureLoader->load("data/textures/ui-level-connector.png"); levelConnectorTexture = textureLoader->load("data/textures/ui-level-connector.png");
pauseButtonTexture = textureLoader->load("data/textures/pause-button.png");
playButtonTexture = textureLoader->load("data/textures/play-button.png");
// Get strings // Get strings
std::string pressAnyKeyString; std::string pressAnyKeyString;
@ -781,6 +795,10 @@ bool Application::loadUI()
frameTimeLabel->setText(""); frameTimeLabel->setText("");
uiRootElement->addChild(frameTimeLabel); uiRootElement->addChild(frameTimeLabel);
bool frameTimeLabelVisible = false;
settings.get("show_frame_time", &frameTimeLabelVisible);
frameTimeLabel->setVisible(frameTimeLabelVisible);
// Create "Press any key" element // Create "Press any key" element
anyKeyLabel = new UILabel(); anyKeyLabel = new UILabel();
anyKeyLabel->setAnchor(Vector2(0.5f, 1.0f)); anyKeyLabel->setAnchor(Vector2(0.5f, 1.0f));
@ -958,6 +976,25 @@ bool Application::loadUI()
} }
} }
// Create pause/play button elements
pauseButtonImage = new UIImage();
pauseButtonImage->setAnchor(Vector2(0.0f, 1.0f));
pauseButtonImage->setDimensions(Vector2(pauseButtonTexture->getWidth(), pauseButtonTexture->getHeight()));
pauseButtonImage->setTranslation(Vector2(16.0f, -16.0f));
pauseButtonImage->setTexture(pauseButtonTexture);
pauseButtonImage->setVisible(false);
pauseButtonImage->setActive(false);
uiRootElement->addChild(pauseButtonImage);
playButtonImage = new UIImage();
playButtonImage->setAnchor(Vector2(0.0f, 1.0f));
playButtonImage->setDimensions(Vector2(playButtonTexture->getWidth(), playButtonTexture->getHeight()));
playButtonImage->setTranslation(Vector2(16.0f, -16.0f));
playButtonImage->setTexture(playButtonTexture);
playButtonImage->setVisible(false);
playButtonImage->setActive(false);
uiRootElement->addChild(playButtonImage);
// Create tweener // Create tweener
tweener = new Tweener(); tweener = new Tweener();
@ -1047,6 +1084,11 @@ bool Application::loadUI()
antHillFadeOutTween->setEndCallback(std::bind(Application::changeState, this, mainMenuState)); antHillFadeOutTween->setEndCallback(std::bind(Application::changeState, this, mainMenuState));
tweener->addTween(antHillFadeOutTween); tweener->addTween(antHillFadeOutTween);
playButtonFadeTween = new Tween<Vector4>(EaseFunction::OUT_CUBIC, 0.0f, 1.0f, Vector4(1.0f, 1.0f, 1.0f, 1.0f), Vector4(0.0f, 0.0f, 0.0f, -1.0f));
playButtonFadeTween->setUpdateCallback(std::bind(UIElement::setTintColor, playButtonImage, std::placeholders::_1));
playButtonFadeTween->setEndCallback(std::bind(UIElement::setVisible, playButtonImage, false));
tweener->addTween(playButtonFadeTween);
// Build menu system // Build menu system
selectedMenuItemIndex = 0; selectedMenuItemIndex = 0;
mainMenu = new Menu(); mainMenu = new Menu();
@ -1241,6 +1283,7 @@ bool Application::loadControls()
gameControlProfile->registerControl("walk-back", &walkBack); gameControlProfile->registerControl("walk-back", &walkBack);
gameControlProfile->registerControl("turn-left", &turnLeft); gameControlProfile->registerControl("turn-left", &turnLeft);
gameControlProfile->registerControl("turn-right", &turnRight); gameControlProfile->registerControl("turn-right", &turnRight);
gameControlProfile->registerControl("toggle-pause", &togglePause);
cameraMoveForward.bindKey(keyboard, SDL_SCANCODE_W); cameraMoveForward.bindKey(keyboard, SDL_SCANCODE_W);
cameraMoveBack.bindKey(keyboard, SDL_SCANCODE_S); cameraMoveBack.bindKey(keyboard, SDL_SCANCODE_S);
@ -1258,6 +1301,7 @@ bool Application::loadControls()
walkBack.bindKey(keyboard, SDL_SCANCODE_DOWN); walkBack.bindKey(keyboard, SDL_SCANCODE_DOWN);
turnLeft.bindKey(keyboard, SDL_SCANCODE_LEFT); turnLeft.bindKey(keyboard, SDL_SCANCODE_LEFT);
turnRight.bindKey(keyboard, SDL_SCANCODE_RIGHT); turnRight.bindKey(keyboard, SDL_SCANCODE_RIGHT);
togglePause.bindKey(keyboard, SDL_SCANCODE_SPACE);
return true; return true;
} }
@ -1273,6 +1317,11 @@ bool Application::loadGame()
campaign.load("data/levels/"); campaign.load("data/levels/");
currentWorld = 1; currentWorld = 1;
currentLevel = 1; currentLevel = 1;
simulationPaused = false;
// Create colony
colony = new Colony();
colony->setAntModel(antModel);
return true; return true;
} }
@ -1426,4 +1475,27 @@ void Application::loadLevel()
std::string heightmap = std::string("data/textures/") + level->heightmap; std::string heightmap = std::string("data/textures/") + level->heightmap;
terrain.load(heightmap); terrain.load(heightmap);
changeState(playState);
}
void Application::pauseSimulation()
{
simulationPaused = true;
playButtonImage->setVisible(false);
playButtonImage->setActive(false);
pauseButtonImage->setVisible(true);
pauseButtonImage->setActive(true);
playButtonFadeTween->stop();
}
void Application::unpauseSimulation()
{
simulationPaused = false;
pauseButtonImage->setVisible(false);
pauseButtonImage->setActive(false);
playButtonImage->setTintColor(Vector4(1.0f));
playButtonImage->setVisible(true);
playButtonImage->setActive(true);
playButtonFadeTween->start();
} }

+ 10
- 0
src/application.hpp View File

@ -96,6 +96,9 @@ public:
void loadLevel(); void loadLevel();
void pauseSimulation();
void unpauseSimulation();
private: private:
ApplicationState* state; ApplicationState* state;
ApplicationState* nextState; ApplicationState* nextState;
@ -202,6 +205,7 @@ public:
Control walkBack; Control walkBack;
Control turnLeft; Control turnLeft;
Control turnRight; Control turnRight;
Control togglePause;
Arcball arcball; Arcball arcball;
// Misc // Misc
@ -223,6 +227,8 @@ public:
Texture* levelActiveTexture; Texture* levelActiveTexture;
Texture* levelInactiveTexture; Texture* levelInactiveTexture;
Texture* levelConnectorTexture; Texture* levelConnectorTexture;
Texture* pauseButtonTexture;
Texture* playButtonTexture;
// UI elements // UI elements
Vector4 selectedColor; Vector4 selectedColor;
@ -266,6 +272,8 @@ public:
UIContainer* levelSelectorContainer; UIContainer* levelSelectorContainer;
UIImage* levelSelections[10]; UIImage* levelSelections[10];
UIImage* levelConnectors[9]; UIImage* levelConnectors[9];
UIImage* pauseButtonImage;
UIImage* playButtonImage;
// Animation // Animation
Tweener* tweener; Tweener* tweener;
@ -287,6 +295,7 @@ public:
Tween<float>* antHillZoomInTween; Tween<float>* antHillZoomInTween;
Tween<Vector4>* antHillFadeOutTween; Tween<Vector4>* antHillFadeOutTween;
Tween<Vector4>* playButtonFadeTween;
// Menus // Menus
std::size_t menuCount; std::size_t menuCount;
@ -318,6 +327,7 @@ public:
bool cameraOverheadView; bool cameraOverheadView;
bool cameraNestView; bool cameraNestView;
int toolIndex; int toolIndex;
bool simulationPaused;
// Debug // Debug
LineBatcher* lineBatcher; LineBatcher* lineBatcher;

+ 2
- 1
src/render-passes.cpp View File

@ -295,7 +295,6 @@ void SoilRenderPass::render(const RenderContext* renderContext)
LightingRenderPass::LightingRenderPass(): LightingRenderPass::LightingRenderPass():
shadowMap(0), shadowMap(0),
shadowCamera(nullptr), shadowCamera(nullptr),
modelLoader(nullptr),
treeShadow(nullptr), treeShadow(nullptr),
diffuseCubemap(nullptr) diffuseCubemap(nullptr)
{ {
@ -1021,6 +1020,7 @@ void DebugRenderPass::render(const RenderContext* renderContext)
{ {
const Camera& camera = *(renderContext->camera); const Camera& camera = *(renderContext->camera);
/*
// Bind framebuffer and setup viewport // Bind framebuffer and setup viewport
glBindFramebuffer(GL_FRAMEBUFFER, renderTarget->framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, renderTarget->framebuffer);
glViewport(0, 0, renderTarget->width, renderTarget->height); glViewport(0, 0, renderTarget->width, renderTarget->height);
@ -1034,6 +1034,7 @@ void DebugRenderPass::render(const RenderContext* renderContext)
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE); glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
*/
// Disable backface culling // Disable backface culling
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);

+ 5
- 3
src/render-passes.hpp View File

@ -126,7 +126,6 @@ public:
inline void setShadowMap(GLuint shadowMap) { this->shadowMap = shadowMap; } inline void setShadowMap(GLuint shadowMap) { this->shadowMap = shadowMap; }
inline void setShadowCamera(const Camera* camera) { this->shadowCamera = camera; } inline void setShadowCamera(const Camera* camera) { this->shadowCamera = camera; }
inline void setModelLoader(ModelLoader* modelLoader) { this->modelLoader = modelLoader; }
private: private:
bool loadShader(const RenderOperation& operation); bool loadShader(const RenderOperation& operation);
@ -158,8 +157,6 @@ private:
Texture* specularCubemap; Texture* specularCubemap;
const Camera* shadowCamera; const Camera* shadowCamera;
float time; float time;
ModelLoader* modelLoader;
}; };
/** /**
@ -173,6 +170,11 @@ public:
virtual void unload(); virtual void unload();
virtual void render(const RenderContext* renderContext); virtual void render(const RenderContext* renderContext);
//void setDrawBounds(bool enabled);
//void setDrawSkeletons(bool enabled);
//void setDrawCameras(bool enabled);
//void setDrawLights(bool enabled);
private: private:
ShaderParameterSet parameterSet; ShaderParameterSet parameterSet;
const ShaderParameter* modelViewProjectionParam; const ShaderParameter* modelViewProjectionParam;

+ 2
- 0
src/states/main-menu-state.cpp View File

@ -277,6 +277,8 @@ void MainMenuState::execute()
void MainMenuState::exit() void MainMenuState::exit()
{ {
// Remove nest
application->defaultLayer->removeObject(&application->nestModelInstance);
} }
void MainMenuState::mouseButtonPressed(int button, int x, int y) void MainMenuState::mouseButtonPressed(int button, int x, int y)

+ 100
- 2
src/states/play-state.cpp View File

@ -19,6 +19,9 @@
#include "play-state.hpp" #include "play-state.hpp"
#include "../application.hpp" #include "../application.hpp"
#include "../camera-controller.hpp"
#include "../game/colony.hpp"
#include "../game/ant.hpp"
PlayState::PlayState(Application* application): PlayState::PlayState(Application* application):
ApplicationState(application) ApplicationState(application)
@ -29,12 +32,107 @@ PlayState::~PlayState()
void PlayState::enter() void PlayState::enter()
{ {
// Start fade-in
application->fadeInTween->start();
// Hide level selector
application->levelSelectorContainer->setVisible(false);
application->levelSelectorContainer->setActive(false);
// Setup HUD
application->pauseButtonImage->setVisible(false);
application->pauseButtonImage->setActive(false);
application->playButtonImage->setVisible(false);
application->playButtonImage->setActive(false);
// Create terrain model instances
application->terrain.getSurfaceModel()->getGroup(0)->material = application->materialLoader->load("data/materials/debug-terrain-surface.mtl");
terrainSurface.setModel(application->terrain.getSurfaceModel());
terrainSurface.setTranslation(Vector3(0, 0, 0));
terrainSubsurface.setModel(application->terrain.getSubsurfaceModel());
terrainSubsurface.setTranslation(Vector3(0, 0, 0));
// Add terrain to scene
application->defaultLayer->addObject(&terrainSurface);
application->defaultLayer->addObject(&terrainSubsurface);
// Spawn ants
Navmesh* navmesh = application->terrain.getSurfaceNavmesh();
for (int i = 0; i < 50; ++i)
{
Navmesh::Triangle* triangle = (*navmesh->getTriangles())[0];
Ant* ant = application->colony->spawn(navmesh, triangle, normalize_barycentric(Vector3(0.5f)));
Vector3 forward = glm::normalize(triangle->edge->vertex->position - triangle->edge->next->vertex->position);
Vector3 up = triangle->normal;
ant->setOrientation(forward, up);
application->defaultLayer->addObject(ant->getModelInstance());
ant->setState(Ant::State::WANDER);
}
// Setup camera controller
application->surfaceCam->setCamera(&application->camera);
application->surfaceCam->setFocalPoint(Vector3(0.0f));
application->surfaceCam->setFocalDistance(250.0f);
application->surfaceCam->setElevation(glm::radians(35.0f));
application->surfaceCam->setAzimuth(glm::radians(-45.0f));
application->surfaceCam->setTargetFocalPoint(application->surfaceCam->getFocalPoint());
application->surfaceCam->setTargetFocalDistance(application->surfaceCam->getFocalDistance());
application->surfaceCam->setTargetElevation(application->surfaceCam->getElevation());
application->surfaceCam->setTargetAzimuth(application->surfaceCam->getAzimuth());
application->surfaceCam->update(0.0f);
application->simulationPaused = false;
} }
void PlayState::execute() void PlayState::execute()
{ {
// Update colony
if (!application->simulationPaused)
{
application->colony->update(application->dt);
}
// Move camera
Vector2 movementVector(0.0f);
if (application->cameraMoveLeft.isTriggered())
movementVector.x -= application->cameraMoveLeft.getCurrentValue();
if (application->cameraMoveRight.isTriggered())
movementVector.x += application->cameraMoveRight.getCurrentValue();
if (application->cameraMoveForward.isTriggered())
movementVector.y -= application->cameraMoveForward.getCurrentValue();
if (application->cameraMoveBack.isTriggered())
movementVector.y += application->cameraMoveBack.getCurrentValue();
if (movementVector.x != 0.0f || movementVector.y != 0.0f)
{
movementVector *= 0.005f * application->surfaceCam->getFocalDistance() * application->dt / (1.0f / 60.0f);
application->surfaceCam->move(movementVector);
Vector3 focal = application->surfaceCam->getFocalPoint();
}
// Zoom camera
float zoomFactor = application->surfaceCam->getFocalDistance() / 20.0f * application->dt / (1.0f / 60.0f);
if (application->cameraZoomIn.isTriggered())
application->surfaceCam->zoom(zoomFactor * application->cameraZoomIn.getCurrentValue());
if (application->cameraZoomOut.isTriggered())
application->surfaceCam->zoom(-zoomFactor * application->cameraZoomOut.getCurrentValue());
application->surfaceCam->update(application->dt);
// Update camera
application->surfaceCam->update(application->dt);
// Pause simulation
if (application->togglePause.isTriggered() && !application->togglePause.wasTriggered())
{
if (application->simulationPaused)
{
application->unpauseSimulation();
}
else
{
application->pauseSimulation();
}
}
} }
void PlayState::exit() void PlayState::exit()

+ 4
- 0
src/states/play-state.hpp View File

@ -38,6 +38,10 @@ public:
virtual void mouseButtonPressed(int button, int x, int y); virtual void mouseButtonPressed(int button, int x, int y);
virtual void mouseButtonReleased(int button, int x, int y); virtual void mouseButtonReleased(int button, int x, int y);
private:
ModelInstance terrainSurface;
ModelInstance terrainSubsurface;
}; };
#endif // PLAY_STATE_HPP #endif // PLAY_STATE_HPP

+ 0
- 16
src/states/title-state.cpp View File

@ -52,22 +52,6 @@ void TitleState::enter()
// Title ant hill // Title ant hill
application->defaultLayer->addObject(&application->antHillModelInstance); application->defaultLayer->addObject(&application->antHillModelInstance);
// Create terrain
application->terrain.create(255, 255, Vector3(50, 20, 50));
application->terrain.getSurfaceModel()->getGroup(0)->material = application->materialLoader->load("data/materials/debug-terrain-surface.mtl");
terrainSurface.setModel(application->terrain.getSurfaceModel());
terrainSurface.setTranslation(Vector3(0, 0, 0));
terrainSubsurface.setModel(application->terrain.getSubsurfaceModel());
terrainSubsurface.setTranslation(Vector3(0, 0, 0));
//application->defaultLayer->addObject(&terrainSurface);
//application->defaultLayer->addObject(&terrainSubsurface);
navmesh = application->terrain.getSurfaceNavmesh();
// Load level
application->loadLevel();
application->inputManager->addWindowObserver(this); application->inputManager->addWindowObserver(this);
windowResized(application->width, application->height); windowResized(application->width, application->height);

+ 0
- 6
src/states/title-state.hpp View File

@ -50,12 +50,6 @@ private:
bool fadeIn; bool fadeIn;
bool fadeOut; bool fadeOut;
int substate; int substate;
Colony colony;
Ant* ant;
Navmesh* navmesh;
ModelInstance terrainSurface;
ModelInstance terrainSubsurface;
}; };
#endif // TITLE_STATE_HPP #endif // TITLE_STATE_HPP

Loading…
Cancel
Save