From 19b91b78d29674d2f5be47de607531f0f6e8656d Mon Sep 17 00:00:00 2001 From: TriantaTV <56975502+TriantaTV@users.noreply.github.com> Date: Fri, 29 Jul 2022 15:53:59 -0500 Subject: [PATCH 1/2] GameState now only defines window limits GameState now only defines window limits. RenderWindow is now set to be a separate object left in the main method. Noted issues if put into GameState: Window never opens and the program just ends. --- src/GameState.cpp | 12 ++++++------ src/GameState.h | 7 ++++--- src/main.cpp | 15 ++++++++++----- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/GameState.cpp b/src/GameState.cpp index 24f2310..66b7f23 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -7,9 +7,9 @@ GameState::GameState() { videoSizeHorizontal = 1024; videoSizeVertical = 725; - sf::Vector2u newVideoSize(videoSizeHorizontal, videoSizeVertical); - window.setSize(newVideoSize); - window.setTitle("SnakePlusPlus"); + // sf::Vector2u newVideoSize(videoSizeHorizontal, videoSizeVertical); + // window.setSize(newVideoSize); + // window.setTitle("SnakePlusPlus"); return; } @@ -17,8 +17,8 @@ GameState::GameState(int newHorizontal, int newVertical) { videoSizeHorizontal = newHorizontal; videoSizeVertical = newVertical; - sf::Vector2u newVideoSize(videoSizeHorizontal, videoSizeVertical); - window.setSize(newVideoSize); - window.setTitle("SnakePlusPlus"); + // sf::Vector2u newVideoSize(videoSizeHorizontal, videoSizeVertical); + // window.setSize(newVideoSize); + // window.setTitle("SnakePlusPlus"); return; } diff --git a/src/GameState.h b/src/GameState.h index 5381228..e0eb3d5 100644 --- a/src/GameState.h +++ b/src/GameState.h @@ -5,12 +5,13 @@ class GameState { private: - int videoSizeHorizontal; - int videoSizeVertical; - sf::RenderWindow window; public: + unsigned int videoSizeHorizontal; + unsigned int videoSizeVertical; + // sf::RenderWindow window; GameState(); GameState(int newHorizontal, int newVertical); + // sf::Vector2f GetGameBoundaries(); }; #endif diff --git a/src/main.cpp b/src/main.cpp index ba99147..f41f899 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,24 @@ #include +#include #include #include +#include "GameState.h" #include "Snake.h" int main() { - int videoSizeHorizontal, videoSizeVertical; - videoSizeHorizontal = 1024; - videoSizeVertical = 725; + // int videoSizeHorizontal, videoSizeVertical; + // videoSizeHorizontal = 1024; + // videoSizeVertical = 725; /* gameGridHorizontal = (videoSizeHorizontal // 25) * 25; gameGridVertical = (videoSizeVertical // 25) * 25; */ - sf::RenderWindow window(sf::VideoMode(videoSizeHorizontal, videoSizeVertical), "SnakePlusPlus"); - sf::Time delay = sf::milliseconds(50); + // GameState newGame(1200, 1000); + GameState newGame; + sf::RenderWindow window(sf::VideoMode(newGame.videoSizeHorizontal, newGame.videoSizeVertical), "SnakePlusPlus"); + // window = newGame.window; + sf::Time delay = sf::milliseconds(25); int snakeDirection = 0; Snake Player(sf::Vector2f(25,25)); From f64577d2cd9cc62928aa2f12b200ea7f3711db72 Mon Sep 17 00:00:00 2001 From: TriantaTV <56975502+TriantaTV@users.noreply.github.com> Date: Fri, 29 Jul 2022 16:27:18 -0500 Subject: [PATCH 2/2] Moved render window into GameState --- src/GameState.cpp | 16 ++++++---------- src/GameState.h | 9 ++++++--- src/main.cpp | 28 +++++++++------------------- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/GameState.cpp b/src/GameState.cpp index 66b7f23..a81ae0b 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -5,20 +5,16 @@ GameState::GameState() { - videoSizeHorizontal = 1024; - videoSizeVertical = 725; - // sf::Vector2u newVideoSize(videoSizeHorizontal, videoSizeVertical); - // window.setSize(newVideoSize); - // window.setTitle("SnakePlusPlus"); + sf::VideoMode tempVideoMode(1024, 725); + gameVideoMode = tempVideoMode; + sf::RenderWindow gameWindow(gameVideoMode, "SnakePlusPlus"); return; } GameState::GameState(int newHorizontal, int newVertical) { - videoSizeHorizontal = newHorizontal; - videoSizeVertical = newVertical; - // sf::Vector2u newVideoSize(videoSizeHorizontal, videoSizeVertical); - // window.setSize(newVideoSize); - // window.setTitle("SnakePlusPlus"); + sf::VideoMode tempVideoMode(newHorizontal, newVertical); + gameVideoMode = tempVideoMode; + sf::RenderWindow tempWindow(gameVideoMode, "SnakePlusPlus"); return; } diff --git a/src/GameState.h b/src/GameState.h index e0eb3d5..eec85ae 100644 --- a/src/GameState.h +++ b/src/GameState.h @@ -6,9 +6,12 @@ class GameState { private: public: - unsigned int videoSizeHorizontal; - unsigned int videoSizeVertical; - // sf::RenderWindow window; + sf::VideoMode gameVideoMode; + sf::RenderWindow gameWindow; + /* + gameGridHorizontal = (videoSizeHorizontal // 25) * 25; + gameGridVertical = (videoSizeVertical // 25) * 25; + */ GameState(); GameState(int newHorizontal, int newVertical); // sf::Vector2f GetGameBoundaries(); diff --git a/src/main.cpp b/src/main.cpp index f41f899..e6ed2ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,34 +7,24 @@ int main() { - // int videoSizeHorizontal, videoSizeVertical; - // videoSizeHorizontal = 1024; - // videoSizeVertical = 725; - /* - gameGridHorizontal = (videoSizeHorizontal // 25) * 25; - gameGridVertical = (videoSizeVertical // 25) * 25; - */ - // GameState newGame(1200, 1000); GameState newGame; - sf::RenderWindow window(sf::VideoMode(newGame.videoSizeHorizontal, newGame.videoSizeVertical), "SnakePlusPlus"); - // window = newGame.window; + newGame.gameWindow.create(newGame.gameVideoMode, "SnakePlusPlus"); + sf::RenderWindow *window = &newGame.gameWindow; sf::Time delay = sf::milliseconds(25); - int snakeDirection = 0; Snake Player(sf::Vector2f(25,25)); sf::RectangleShape snakeHead(sf::Vector2f(25,25)); sf::RectangleShape snakeFood(sf::Vector2f(25,25)); snakeFood.setFillColor(sf::Color::Red); - snakeFood.setPosition(25,25); - while (window.isOpen()) + while (window->isOpen()) { sf::Event event; - while (window.pollEvent(event)) + while (window->pollEvent(event)) { if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) - window.close(); + window->close(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) snakeDirection = 1; @@ -45,10 +35,10 @@ int main() if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) snakeDirection = 4; Player.MoveSnake(snakeFood); - window.clear(); - window.draw(snakeFood); - Player.DisplaySnake(window); - window.display(); + window->clear(); + window->draw(snakeFood); + Player.DisplaySnake(*window); + window->display(); sf::sleep(delay); } }