Skip to content

Commit

Permalink
Pause Screen
Browse files Browse the repository at this point in the history
the only notable part of this commit

and also, pointers
  • Loading branch information
TheWindowsPro98 committed Oct 4, 2023
1 parent de9b9fe commit fd9a722
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 99 deletions.
217 changes: 180 additions & 37 deletions game.cxx
Original file line number Diff line number Diff line change
@@ -1,53 +1,64 @@
#include "inc/includes.hxx"

sf::RectangleShape lvlFG(sf::Vector2f(window.getSize()));
sf::RectangleShape lvlBG(lvlFG);
sf::Texture lvlPxBG, lvlPxFG;
sf::Color bgColor(0x000000FF);
sf::RectangleShape* lvlFG;
sf::RectangleShape* lvlBG;
sf::Texture* lvlPxBG;
sf::Texture* lvlPxFG;
sf::Color* bgColor;
types::u8 level = 0;
float cam_x = 0;
float cam_y = 0;
float* cam_x;
float* cam_y;
const sf::Vector2f mapSizes[] =
{
{3840,2160},
};
bool* isPaused;
const types::u8 pauseX = 20;
const types::u8 pauseY = 13;
const types::u8 pauseOptsAmnt = 3;
const structs::Option pauseMenu[] =
{
{pauseX,pauseY,"Return to Game"},
{pauseX,pauseY+2,"Save and Exit"},
{pauseX,pauseY+4,"Exit Without Saving"}
};

static void camPos()
{
if (cam_x < 0)
if (*cam_x < 0)
{
cam_x = 0;
*cam_x = 0;
}
else if (cam_x > mapSizes[level].x - window.getSize().x)
else if (*cam_x > mapSizes[level].x - window.getSize().x)
{
cam_x = mapSizes[level].x - window.getSize().x;
*cam_x = mapSizes[level].x - window.getSize().x;
}
if (cam_y < 0)
if (*cam_y < 0)
{
cam_y = 0;
*cam_y = 0;
}
else if (cam_y > mapSizes[level].y - window.getSize().y)
else if (*cam_y > mapSizes[level].y - window.getSize().y)
{
cam_y = mapSizes[level].y - window.getSize().y;
*cam_y = mapSizes[level].y - window.getSize().y;
}
lvlFG.setPosition(sf::Vector2f(-cam_x,-cam_y));
lvlBG.setPosition(sf::Vector2f(-cam_x/2,-cam_y/2));
lvlFG->setPosition(sf::Vector2f(-*cam_x,-*cam_y));
lvlBG->setPosition(sf::Vector2f(-*cam_x/2,-*cam_y/2));
}

static const types::u8* loadMap()
{
lvlPxBG.setSmooth(true);
lvlPxFG.setSmooth(true);
lvlPxBG->setSmooth(true);
lvlPxFG->setSmooth(true);
switch (level)
{
case 0:
{
lvlPxBG.loadFromFile(testLvlBG);
lvlPxFG.loadFromFile(testLvlFG);
lvlBG.setTexture(&lvlPxBG);
lvlFG.setTexture(&lvlPxFG);
lvlFG.setSize(mapSizes[level]);
lvlBG.setSize(mapSizes[level]);
lvlPxBG->loadFromFile(testLvlBG);
lvlPxFG->loadFromFile(testLvlFG);
lvlBG->setTexture(lvlPxBG);
lvlFG->setTexture(lvlPxFG);
lvlFG->setSize(mapSizes[level]);
lvlBG->setSize(mapSizes[level]);
music.openFromFile(lfTrack);
music.setLoop(true);
music.play();
Expand All @@ -63,7 +74,7 @@ static const types::u8* loadMap()
}
}

static void gameInputHdl()
static void gameInputHdl_KB()
{
if (!window.hasFocus())
{
Expand All @@ -75,19 +86,19 @@ static void gameInputHdl()
bool downPressed = sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Down);
if (leftPressed)
{
cam_x -= 2.5;
*cam_x -= 2.5;
}
else if (rightPressed)
{
cam_x += 2.5;
*cam_x += 2.5;
}
if (upPressed)
{
cam_y -= 2.5;
*cam_y -= 2.5;
}
else if (downPressed)
{
cam_y += 2.5;
*cam_y += 2.5;
}
}

Expand All @@ -96,29 +107,107 @@ static void drawHUD()
sf::Text camXLabel(templateText);
sf::Text camYLabel(templateText);
camYLabel.setPosition(0, 27.5);
std::string cxStr = std::to_string(cam_x);
std::string cyStr = std::to_string(cam_y);
std::string cxStr = std::to_string(*cam_x);
std::string cyStr = std::to_string(*cam_y);
camXLabel.setString("CX: " + cxStr);
camYLabel.setString("CY: " + cyStr);
window.draw(camXLabel);
window.draw(camYLabel);
}

static void selectMenuPause()
{
if (menuIndex >= 1)
{
sf::Texture windowCapture;
float volume = music.getVolume();
while (window.isOpen())
{
if (volume == 0)
{
music.stop();
music.setVolume(100);
break;
}
sf::Event e;
volume = music.getVolume();
fadeMusic(true,volFadeSpeed,volMin);
screenFade(volFadeSpeed,false,fadeDark);
window.draw(fadeRect);
window.display();
while (window.pollEvent(e))
{
if (e.type == sf::Event::Closed)
{
window.close();
return;
}
}
}
}
switch (menuIndex)
{
case 0:
{
*isPaused = false;
break;
}
case 2:
{
menuIndex = 0;
delete cam_x;
delete cam_y;
delete isPaused;
delete bgColor;
delete lvlPxBG;
delete lvlPxFG;
title();
break;
}
default:
{
printerr(missingFuncErr);
break;
}
}
}

void gameInit()
{
fadeRect.setFillColor(sf::Color::Black);
cam_x = new float(0);
cam_y = new float(0);
isPaused = new bool(false);
bgColor = new sf::Color(0x000000FF);
lvlPxBG = new sf::Texture;
lvlPxFG = new sf::Texture;
lvlBG = new sf::RectangleShape(sf::Vector2f(window.getSize()));
lvlFG = new sf::RectangleShape(*lvlBG);
const types::u8* collisionArray = loadMap();
while (window.isOpen())
{
sf::Event e;
screenFade(volFadeSpeed,true);
gameInputHdl();
camPos();
window.clear(bgColor);
window.draw(lvlBG);
window.draw(lvlFG);
if (!*isPaused)
{
gameInputHdl_KB();
camPos();
fadeMusic(false,volFadeSpeed,volMax);
screenFade(volFadeSpeed,true,fadeLight);
}
else
{
fadeMusic(true,volFadeSpeed,50);
screenFade(volFadeSpeed*3,false,0x7F);
}
window.clear(*bgColor);
window.draw(*lvlBG);
window.draw(*lvlFG);
drawHUD();
window.draw(fadeRect);
if (*isPaused)
{
drawMenu(pauseMenu,pauseOptsAmnt);
}
window.display();
while (window.pollEvent(e))
{
Expand All @@ -129,6 +218,60 @@ void gameInit()
window.close();
break;
}
case sf::Event::KeyPressed:
{
if (!window.hasFocus())
{
break;
}
if (!*isPaused)
{
if (e.key.scancode == sf::Keyboard::Scan::Enter)
{
sndCnf.play();
*isPaused = true;
}
}
else
{
if (e.key.scancode == sf::Keyboard::Scan::Up)
{
sndHvr.play();
if (menuIndex <= 0)
{
menuIndex = pauseOptsAmnt - 1;
}
else
{
menuIndex--;
}
}
else if (e.key.scancode == sf::Keyboard::Scan::Down)
{
sndHvr.play();
if (menuIndex >= pauseOptsAmnt - 1)
{
menuIndex = 0;
}
else
{
menuIndex++;
}
}
if (e.key.scancode == sf::Keyboard::Scan::Enter)
{
sndCnf.play();
selectMenuPause();
}
else if (e.key.scancode == sf::Keyboard::Scan::Escape)
{
sndBack.play();
menuIndex = 0;
selectMenuPause();
}
}
break;
}
default:
{
break;
Expand Down
2 changes: 1 addition & 1 deletion inc/prefs.hxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

extern types::u8* menuIndex;
extern types::u8 menuIndex;
extern bool player;

void prefsScreen();
8 changes: 6 additions & 2 deletions inc/utils.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ extern sf::Sound sndBack;
extern sf::Text templateText;
extern const float volFadeSpeed;
extern const sf::Color playerColors[];
extern const types::u8 volMin;
extern const types::u8 volMax;
extern const types::u8 fadeDark;
extern const types::u8 fadeLight;
enum errorCodes
{
genericErr,
Expand All @@ -24,8 +28,8 @@ enum errorCodes
missingAssetsErr,
};

void screenFade(float speed, bool direction);
void screenFade(float speed, bool direction, float fadeTarget);
void drawMenu(const structs::Option* option, types::u8 length);
void fadeMusic(bool direction, float speed);
void fadeMusic(bool direction, float speed, float targetVolume);
float pixelToTile(float pos);
void printerr(types::u8 error);
Loading

0 comments on commit fd9a722

Please sign in to comment.