Skip to content

Commit

Permalink
feat: Update demo to use previously redundant code
Browse files Browse the repository at this point in the history
Updated the demo to use the GameStage and PausedStage classes.
Tidied up some of the code and got the button rendering working.
  • Loading branch information
ThePythonator committed Sep 16, 2024
1 parent f932581 commit 64740ad
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/game/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ namespace FONTS {

namespace COLOURS {
const Framework::Colour BLACK{ 0x00, 0x00, 0x00 };
const Framework::Colour BLUE { 0xA7, 0xC7, 0xE7 };
const Framework::Colour WHITE{ 0xFF, 0xFF, 0xFF };
}

Expand Down Expand Up @@ -133,7 +134,6 @@ namespace BUTTONS {
namespace TITLE {
enum TITLE {
PLAY,
SETTINGS,
QUIT,

TOTAL
Expand Down
4 changes: 4 additions & 0 deletions include/game/Stages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class TitleStage : public Framework::BaseStage {

class GameStage : public Framework::BaseStage {
public:
void start();

bool update(float dt);
void render();
};
Expand All @@ -25,6 +27,8 @@ class PausedStage : public Framework::BaseStage {
public:
PausedStage(BaseStage* background_stage);

void start();

bool update(float dt);
void render();

Expand Down
86 changes: 71 additions & 15 deletions src/game/Stages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@

void TitleStage::start() {
// Create buttons
buttons = {
Framework::Button(
Framework::Rect(128, 0, 256, 64),
graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD],
Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Play", COLOURS::BLACK, 4.0f)
)
};
buttons.clear();
buttons.emplace_back(
Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, 32), Framework::Vec(256, 64)),
graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD],
Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Play", COLOURS::BLACK, 4.0f),
BUTTONS::TITLE::PLAY
);
buttons.emplace_back(
Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, -64), Framework::Vec(256, 64)),
graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD],
Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Quit", COLOURS::BLACK, 4.0f),
BUTTONS::TITLE::QUIT
);

// Set transition
set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]);

// Start transition
set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]);
transition->open();

// Start timer used for rotations
Expand All @@ -38,11 +46,7 @@ bool TitleStage::update(float dt) {
// Next stage!
switch (button_selected) {
case BUTTONS::TITLE::PLAY:
//finish(new GameStage());
break;

case BUTTONS::TITLE::SETTINGS:
//finish(new SettingsStage());
finish(new GameStage());
break;

case BUTTONS::TITLE::QUIT:
Expand All @@ -59,7 +63,7 @@ bool TitleStage::update(float dt) {
}

void TitleStage::render() {
graphics_objects->graphics_ptr->fill(COLOURS::WHITE);
graphics_objects->graphics_ptr->fill(COLOURS::BLUE);

graphics_objects->graphics_ptr->render_circle(Framework::Vec(20, 20), 10, COLOURS::BLACK);
graphics_objects->graphics_ptr->render_rect(Framework::Rect(40, 20, 10, 30), COLOURS::BLACK);
Expand All @@ -80,13 +84,29 @@ void TitleStage::render() {

// GameStage

void GameStage::start() {
// Set transition
set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]);

// Start transition
transition->open();
}

bool GameStage::update(float dt) {
transition->update(dt);

if (input->just_down(Framework::KeyHandler::Key::ESCAPE) || input->just_down(Framework::KeyHandler::Key::P)) {
finish(new PausedStage(this), false);
}

return true;
}

void GameStage::render() {
graphics_objects->graphics_ptr->fill(COLOURS::BLUE);

graphics_objects->spritesheet_ptrs[GRAPHICS_OBJECTS::SPRITESHEETS::MAIN_SPRITESHEET]->sprite(0, Framework::Vec(128, 64));

transition->render();
}

Expand All @@ -97,16 +117,45 @@ PausedStage::PausedStage(BaseStage* background_stage) : BaseStage() {
_background_stage = background_stage;
}

void PausedStage::start() {
// Create buttons
buttons.clear();
buttons.emplace_back(
Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, 32), Framework::Vec(256, 64)),
graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD],
Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Resume", COLOURS::BLACK, 4.0f),
BUTTONS::PAUSED::RESUME
);
buttons.emplace_back(
Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, -64), Framework::Vec(256, 64)),
graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD],
Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Exit", COLOURS::BLACK, 4.0f),
BUTTONS::PAUSED::EXIT
);

// Set transition
set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]);
}

bool PausedStage::update(float dt) {
transition->update(dt);

if (input->just_down(Framework::KeyHandler::Key::ESCAPE) || input->just_down(Framework::KeyHandler::Key::P)) {
transition->close();
}

// Update buttons
for (Framework::Button& button : buttons) {
button.update(input);

if (button.pressed() && transition->is_open()) {
button_selected = button.get_id();
transition->close();
}
}

if (transition->is_closed()) {
if (button_selected == BUTTONS::PAUSED::EXIT) {
delete _background_stage;
finish(new TitleStage());
}
else {
Expand All @@ -123,4 +172,11 @@ void PausedStage::render() {
_background_stage->render();

// Render pause menu
graphics_objects->graphics_ptr->fill(COLOURS::BLACK, 0x7f);

for (const Framework::Button& button : buttons) {
button.render();
}

transition->render();
}

0 comments on commit 64740ad

Please sign in to comment.