diff --git a/game.cxx b/game.cxx index 4f1fd6e..c3a4fc7 100644 --- a/game.cxx +++ b/game.cxx @@ -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(); @@ -63,7 +74,7 @@ static const types::u8* loadMap() } } -static void gameInputHdl() +static void gameInputHdl_KB() { if (!window.hasFocus()) { @@ -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; } } @@ -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)) { @@ -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; diff --git a/inc/prefs.hxx b/inc/prefs.hxx index 015c2ae..771e55c 100755 --- a/inc/prefs.hxx +++ b/inc/prefs.hxx @@ -1,6 +1,6 @@ #pragma once -extern types::u8* menuIndex; +extern types::u8 menuIndex; extern bool player; void prefsScreen(); \ No newline at end of file diff --git a/inc/utils.hxx b/inc/utils.hxx index 7829338..57148aa 100755 --- a/inc/utils.hxx +++ b/inc/utils.hxx @@ -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, @@ -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); \ No newline at end of file diff --git a/jukebox.cxx b/jukebox.cxx index 142baa5..9872443 100755 --- a/jukebox.cxx +++ b/jukebox.cxx @@ -35,8 +35,8 @@ static void jukeboxBack() } sf::Event e; volume = music.getVolume(); - fadeMusic(true,volFadeSpeed); - screenFade(volFadeSpeed,false); + fadeMusic(true,volFadeSpeed,volMin); + screenFade(volFadeSpeed,false,fadeDark); window.draw(fadeRect); window.display(); while (window.pollEvent(e)) @@ -77,14 +77,14 @@ void jukebox() while(window.isOpen()) { sf::Event e; - songLabel.setString("Title: " + musHdr[*menuIndex].songTitle); - songAlbumLabel.setString("Album: " + musHdr[*menuIndex].songAlbum); - songArtistLabel.setString("Artist(s): " + musHdr[*menuIndex].songArtist); - songLoopLabel.setString("Will Audio Loop: " + boolStrings[musHdr[*menuIndex].loop]); - songTypeLabel.setString("Audio Type: " + musType[musHdr[*menuIndex].isSFX]); + songLabel.setString("Title: " + musHdr[menuIndex].songTitle); + songAlbumLabel.setString("Album: " + musHdr[menuIndex].songAlbum); + songArtistLabel.setString("Artist(s): " + musHdr[menuIndex].songArtist); + songLoopLabel.setString("Will Audio Loop: " + boolStrings[musHdr[menuIndex].loop]); + songTypeLabel.setString("Audio Type: " + musType[musHdr[menuIndex].isSFX]); songTimeElapsed.setString("Elapsed Time: " + std::to_string(music.getPlayingOffset().asSeconds()) + timeMetric); songTimeTotal.setString("Total Time: " + std::to_string(music.getDuration().asSeconds()) + timeMetric); - screenFade(volFadeSpeed,true); + screenFade(volFadeSpeed,true,fadeLight); window.clear(); window.draw(songLabel); window.draw(songAlbumLabel); @@ -113,40 +113,40 @@ void jukebox() if (e.key.scancode == sf::Keyboard::Scan::Left) { sndHvr.play(); - if (*menuIndex == 0) + if (menuIndex == 0) { - *menuIndex = sndAmnt-1; + menuIndex = sndAmnt-1; } else { - --*menuIndex; + menuIndex--; } } else if (e.key.scancode == sf::Keyboard::Scan::Right) { sndHvr.play(); - if (*menuIndex == sndAmnt-1) + if (menuIndex == sndAmnt-1) { - *menuIndex = 0; + menuIndex = 0; } else { - ++*menuIndex; + menuIndex++; } } if (e.key.scancode == sf::Keyboard::Scan::Enter) { - if (!musHdr[*menuIndex].isSFX) + if (!musHdr[menuIndex].isSFX) { - music.openFromFile(*musHdr[*menuIndex].songPath); - music.setLoop(musHdr[*menuIndex].loop); + music.openFromFile(*musHdr[menuIndex].songPath); + music.setLoop(musHdr[menuIndex].loop); music.play(); } else { - sb.loadFromFile(*musHdr[*menuIndex].songPath); + sb.loadFromFile(*musHdr[menuIndex].songPath); snd.setBuffer(sb); - snd.setLoop(musHdr[*menuIndex].loop); + snd.setLoop(musHdr[menuIndex].loop); snd.play(); } } diff --git a/main.cxx b/main.cxx index 3ee0298..fd3cfa1 100755 --- a/main.cxx +++ b/main.cxx @@ -17,7 +17,6 @@ int main(int argc, char** argv) font.loadFromFile(blazeTTF); font.setSmooth(false); window.setFramerateLimit(60); - menuIndex = new types::u8(0); fadeRect.setSize(sf::Vector2f(1280,720)); fadeRect.setFillColor(sf::Color::Black); sbHvr.loadFromFile(hoverSFX); diff --git a/prefs.cxx b/prefs.cxx index d1414e4..b52c323 100755 --- a/prefs.cxx +++ b/prefs.cxx @@ -1,6 +1,6 @@ #include "inc/includes.hxx" -types::u8* menuIndex; +types::u8 menuIndex; const types::u8 prefsX = 10; const types::u8 prefsY = 12; const types::u8 diffXDelta = 9; @@ -40,7 +40,7 @@ static void updConfOutline() static void selectMenuPrefs() { - if (*menuIndex >= 6) + if (menuIndex >= 6) { float volume = music.getVolume(); while (window.isOpen()) @@ -53,8 +53,8 @@ static void selectMenuPrefs() } sf::Event e; volume = music.getVolume(); - fadeMusic(true,volFadeSpeed); - screenFade(volFadeSpeed,false); + fadeMusic(true,volFadeSpeed,volMin); + screenFade(volFadeSpeed,false,fadeDark); window.draw(fadeRect); window.display(); while (window.pollEvent(e)) @@ -67,7 +67,7 @@ static void selectMenuPrefs() } } } - switch (*menuIndex) + switch (menuIndex) { case 0: { @@ -101,13 +101,13 @@ static void selectMenuPrefs() } case 6: { - *menuIndex = 0; + menuIndex = 0; jukebox(); break; } case 7: { - *menuIndex = 3; + menuIndex = 3; title(); break; } @@ -123,7 +123,7 @@ void prefsScreen() { music.openFromFile(lsTrack); music.play(); - *menuIndex = 0; + menuIndex = 0; fadeRect.setFillColor(sf::Color::Black); sf::Text diffStr(templateText); diffStr.setString("Difficulty:"); @@ -134,7 +134,7 @@ void prefsScreen() while (window.isOpen()) { sf::Event e; - screenFade(volFadeSpeed,true); + screenFade(volFadeSpeed,true,fadeLight); window.clear(sf::Color::Black); window.draw(diffStr); window.draw(plrStr); @@ -160,25 +160,25 @@ void prefsScreen() if (e.key.scancode == sf::Keyboard::Scan::Left) { sndHvr.play(); - if (*menuIndex == 0) + if (menuIndex == 0) { - *menuIndex = prefsMenuAmnt-1; + menuIndex = prefsMenuAmnt-1; } else { - --*menuIndex; + menuIndex--; } } else if (e.key.scancode == sf::Keyboard::Scan::Right) { sndHvr.play(); - if (*menuIndex < prefsMenuAmnt-1) + if (menuIndex < prefsMenuAmnt-1) { - ++*menuIndex; + menuIndex++; } else { - *menuIndex = 0; + menuIndex = 0; } } if (e.key.scancode == sf::Keyboard::Scan::Enter) @@ -189,7 +189,7 @@ void prefsScreen() else if (e.key.scancode == sf::Keyboard::Scan::Escape) { sndBack.play(); - *menuIndex = 7; + menuIndex = 7; selectMenuPrefs(); } break; diff --git a/res/imgs/icon2.png b/res/imgs/icon2.png index bad6a17..749f7eb 100644 Binary files a/res/imgs/icon2.png and b/res/imgs/icon2.png differ diff --git a/title.cxx b/title.cxx index 094ab3b..ef3d67b 100755 --- a/title.cxx +++ b/title.cxx @@ -26,8 +26,8 @@ static void selectMenuTitle() } sf::Event e; volume = music.getVolume(); - fadeMusic(true,volFadeSpeed); - screenFade(volFadeSpeed,false); + fadeMusic(true,volFadeSpeed,volMin); + screenFade(volFadeSpeed,false,fadeDark); window.draw(fadeRect); window.display(); while (window.pollEvent(e)) @@ -39,7 +39,7 @@ static void selectMenuTitle() } } } - switch (*menuIndex) + switch (menuIndex) { case 0: { @@ -89,7 +89,7 @@ void title() while (window.isOpen()) { sf::Event event; - screenFade(volFadeSpeed,true); + screenFade(volFadeSpeed,true,fadeLight); window.clear(sf::Color::Black); window.draw(copyInfo); window.draw(versionText); @@ -115,25 +115,25 @@ void title() if (event.key.scancode == sf::Keyboard::Scan::Left) { sndHvr.play(); - if (*menuIndex == 0) + if (menuIndex == 0) { - *menuIndex = titleOptAmnt-1; + menuIndex = titleOptAmnt-1; } else { - --*menuIndex; + menuIndex--; } } else if (event.key.scancode == sf::Keyboard::Scan::Right) { sndHvr.play(); - if (*menuIndex >= titleOptAmnt-1) + if (menuIndex >= titleOptAmnt-1) { - *menuIndex = 0; + menuIndex = 0; } else { - ++*menuIndex; + menuIndex++; } } if (event.key.scancode == sf::Keyboard::Scan::Enter) diff --git a/utils.cxx b/utils.cxx index 7bb80f4..a6a8950 100755 --- a/utils.cxx +++ b/utils.cxx @@ -15,27 +15,32 @@ sf::Text templateText; const float volFadeSpeed = 1.75; const float scrnFadeSpeed = 2; const sf::Color playerColors[] = {sf::Color(0xAA22EEFF),sf::Color(0x00CC66FF)}; +const types::u8 volMin = 0; +const types::u8 volMax = 100; +const types::u8 fadeDark = 0xFF; +const types::u8 fadeLight = 0x00; /// @brief Fades the screen in or out. /// @param speed Speed of the fade. Bigger value = faster fade. /// @param direction Direction of the fade. True for in, false for out. -void screenFade(float speed, bool direction) +/// @param fadeTarget Alpha value (0 - 255) that the fade routine stops at. +void screenFade(float speed, bool direction, float fadeTarget) { float alpha = fadeRect.getFillColor().a; if (!direction) { alpha += speed; - if (alpha >= 255) + if (alpha >= fadeTarget) { - alpha = 255; + alpha = fadeTarget; } } else { alpha -= speed*3; - if (alpha <= 0) + if (alpha <= fadeTarget) { - alpha = 0; + alpha = fadeTarget; } } fadeRect.setFillColor(sf::Color(0,0,0,alpha)); @@ -54,8 +59,8 @@ void drawMenu(const structs::Option* option, types::u8 length) optionLabel.setPosition(pixelToTile(o.x),pixelToTile(o.y)); window.draw(optionLabel); sf::Text selectedLabel(templateText); - selectedLabel.setString(option[*menuIndex].label); - selectedLabel.setPosition(pixelToTile(option[*menuIndex].x),pixelToTile(option[*menuIndex].y)); + selectedLabel.setString(option[menuIndex].label); + selectedLabel.setPosition(pixelToTile(option[menuIndex].x),pixelToTile(option[menuIndex].y)); selectedLabel.setOutlineThickness(3.5); selectedLabel.setOutlineColor(playerColors[player]); window.draw(selectedLabel); @@ -65,23 +70,24 @@ void drawMenu(const structs::Option* option, types::u8 length) /// @brief Fades music in or out. /// @param direction False = in, True = out /// @param speed Smaller = slower -void fadeMusic(bool direction, float speed) +/// @param targetVolume The volume that the function stops at. +void fadeMusic(bool direction, float speed, float targetVolume) { float volume = music.getVolume(); if (direction) { volume -= speed; - if (volume <= 0) + if (volume <= targetVolume) { - volume = 0; + volume = targetVolume; } } else { volume += speed; - if (volume >= 100) + if (volume >= targetVolume) { - volume = 100; + volume = targetVolume; } } music.setVolume(volume); diff --git a/version.cxx b/version.cxx index d906f8e..f03965f 100755 --- a/version.cxx +++ b/version.cxx @@ -6,5 +6,5 @@ const std::string programTitle = "Project Daisy"; const std::string buildTitle = "Build 20231004-PC"; const types::u8 versionMajor = 1; const types::u8 versionMinor = 2; -const types::u8 versionRevision = 0; +const types::u8 versionRevision = 1; const types::u8 releaseStage = 0; \ No newline at end of file