From 93f3e4476bc2889a7b9892f0eeca8ad2cca58369 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Sun, 28 Jul 2024 14:25:36 +0530 Subject: [PATCH 01/28] Added my own window file --- Space-Invaders/main.cpp | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 7d5f90dff..194234723 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,5 +1,30 @@ - -int main() -{ - return 0; -} \ No newline at end of file +#include + +int main() +{ + // Define the video mode (dimensions) + sf::VideoMode videoMode = sf::VideoMode(800, 600); + + // Create a window object with specific dimensions and a title + sf::RenderWindow window(videoMode, "My SFML Window"); + + + + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + // Check for window closure + if (event.type == sf::Event::Closed) + window.close(); + } + + + // Clear the window--- + window.clear(sf::Color::Blue); + + // Display whatever you draw + window.display(); + } + + return 0; +} From 676acb27083c928d83d919a1cc580247443014e5 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 13:17:15 +0530 Subject: [PATCH 02/28] Added new Main.cpp --- Space-Invaders/main.cpp | 55 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 7d5f90dff..2963054ca 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,5 +1,50 @@ - -int main() -{ - return 0; -} \ No newline at end of file +#include + +int main() +{ + // Define the video mode (dimensions) + sf::VideoMode videoMode = sf::VideoMode(800, 600); + + // Create a window object with specific dimensions and a title + sf::RenderWindow window(videoMode, "My SFML Window"); + + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + // Check for window closure + if (event.type == sf::Event::Closed) + window.close(); + } + + + // Clear the window--- + window.clear(sf::Color::Magenta); + + //Start + + // Draw a circle + sf::CircleShape circle(50); // Radius 50 + circle.setFillColor(sf::Color::Green); + circle.setPosition(100, 100); // Set position + window.draw(circle); + + //Draw a Red Square + sf::RectangleShape square(sf::Vector2f(50, 50)); + square.setFillColor(sf::Color::Red); + square.setPosition(200, 200); // Set position + window.draw(square); + + //Draw a Blue Triangle + sf::CircleShape Triangle(50, 3); + Triangle.setFillColor(sf::Color::Blue); + Triangle.setPosition(300, 300); // Set position + window.draw(Triangle); + + //End + + // Display whatever you draw + window.display(); + } + + return 0; +} From fe06d7c4cc2e706476aceb5d6dd8a83d9ad15db2 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 13:27:28 +0530 Subject: [PATCH 03/28] Shapes added --- Space-Invaders/main.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 194234723..2963054ca 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -8,8 +8,6 @@ int main() // Create a window object with specific dimensions and a title sf::RenderWindow window(videoMode, "My SFML Window"); - - while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { @@ -20,7 +18,29 @@ int main() // Clear the window--- - window.clear(sf::Color::Blue); + window.clear(sf::Color::Magenta); + + //Start + + // Draw a circle + sf::CircleShape circle(50); // Radius 50 + circle.setFillColor(sf::Color::Green); + circle.setPosition(100, 100); // Set position + window.draw(circle); + + //Draw a Red Square + sf::RectangleShape square(sf::Vector2f(50, 50)); + square.setFillColor(sf::Color::Red); + square.setPosition(200, 200); // Set position + window.draw(square); + + //Draw a Blue Triangle + sf::CircleShape Triangle(50, 3); + Triangle.setFillColor(sf::Color::Blue); + Triangle.setPosition(300, 300); // Set position + window.draw(Triangle); + + //End // Display whatever you draw window.display(); From a096bda7a4ac996f110d5cde921f1b003a79d54f Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 13:46:20 +0530 Subject: [PATCH 04/28] Logo and Sprites --- Space-Invaders/main.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 2963054ca..c7eaf2cf6 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -18,7 +18,7 @@ int main() // Clear the window--- - window.clear(sf::Color::Magenta); + window.clear(sf::Color::Black); //Start @@ -40,6 +40,26 @@ int main() Triangle.setPosition(300, 300); // Set position window.draw(Triangle); + //Drawing a sprite + sf::Texture outscal_texture; + outscal_texture.loadFromFile("assets/textures/outscal_logo.png"); + + sf::Sprite outscal_sprite; + outscal_sprite.setTexture(outscal_texture); + + outscal_sprite.setPosition(500, 200); // Position + outscal_sprite.setRotation(45); // Rotation in degrees + outscal_sprite.setScale(0.5, 0.5); // Scale factor + + window.draw(outscal_sprite); + + //Drawing a text + sf::Font font; + font.loadFromFile("assets/fonts/OpenSans.ttf"); + sf::Text text("SFML is Awesome", font, 50); + text.setFillColor(sf::Color::White); + window.draw(text); + //End // Display whatever you draw From fcc675e3a76c97449b5e3a7f2cd29e189606ddff Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 13:49:23 +0530 Subject: [PATCH 05/28] added text and sprites --- Space-Invaders/main.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 2963054ca..c7eaf2cf6 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -18,7 +18,7 @@ int main() // Clear the window--- - window.clear(sf::Color::Magenta); + window.clear(sf::Color::Black); //Start @@ -40,6 +40,26 @@ int main() Triangle.setPosition(300, 300); // Set position window.draw(Triangle); + //Drawing a sprite + sf::Texture outscal_texture; + outscal_texture.loadFromFile("assets/textures/outscal_logo.png"); + + sf::Sprite outscal_sprite; + outscal_sprite.setTexture(outscal_texture); + + outscal_sprite.setPosition(500, 200); // Position + outscal_sprite.setRotation(45); // Rotation in degrees + outscal_sprite.setScale(0.5, 0.5); // Scale factor + + window.draw(outscal_sprite); + + //Drawing a text + sf::Font font; + font.loadFromFile("assets/fonts/OpenSans.ttf"); + sf::Text text("SFML is Awesome", font, 50); + text.setFillColor(sf::Color::White); + window.draw(text); + //End // Display whatever you draw From 9b096bbaab273403773ebc492aa7d97b6def8962 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 15:11:02 +0530 Subject: [PATCH 06/28] OOP --- Space-Invaders/main.cpp | 75 +++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index c7eaf2cf6..a92576c58 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,5 +1,38 @@ +#include #include + +class Player +{ +private: + + // Private Properties + int health = 3; + sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); + int movement_speed = 5; + int player_score = 0; + +public: + + // Public Properties + sf::Texture player_texture; + sf::Sprite player_sprite; + + //Public Getter & Setter methods + int getScore() { + return player_score; + }; + + void setScore(int newScore) { + player_score = newScore; + }; + + //New methods + void takeDamage() {}; + void move() {}; + void shootBullets() {}; +}; + int main() { // Define the video mode (dimensions) @@ -20,47 +53,7 @@ int main() // Clear the window--- window.clear(sf::Color::Black); - //Start - - // Draw a circle - sf::CircleShape circle(50); // Radius 50 - circle.setFillColor(sf::Color::Green); - circle.setPosition(100, 100); // Set position - window.draw(circle); - - //Draw a Red Square - sf::RectangleShape square(sf::Vector2f(50, 50)); - square.setFillColor(sf::Color::Red); - square.setPosition(200, 200); // Set position - window.draw(square); - - //Draw a Blue Triangle - sf::CircleShape Triangle(50, 3); - Triangle.setFillColor(sf::Color::Blue); - Triangle.setPosition(300, 300); // Set position - window.draw(Triangle); - - //Drawing a sprite - sf::Texture outscal_texture; - outscal_texture.loadFromFile("assets/textures/outscal_logo.png"); - - sf::Sprite outscal_sprite; - outscal_sprite.setTexture(outscal_texture); - - outscal_sprite.setPosition(500, 200); // Position - outscal_sprite.setRotation(45); // Rotation in degrees - outscal_sprite.setScale(0.5, 0.5); // Scale factor - - window.draw(outscal_sprite); - - //Drawing a text - sf::Font font; - font.loadFromFile("assets/fonts/OpenSans.ttf"); - sf::Text text("SFML is Awesome", font, 50); - text.setFillColor(sf::Color::White); - window.draw(text); - - //End + Player player; // Display whatever you draw window.display(); From 1116b79e83abc5d24616221af8faec5fe5cb694a Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 15:31:37 +0530 Subject: [PATCH 07/28] Added keyboard inputs and getters and setters --- Space-Invaders/main.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index a92576c58..1c0f08fff 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -27,6 +27,14 @@ class Player player_score = newScore; }; + sf::Vector2f getPosition() { + return position; + } + + void setPosition(sf::Vector2f newPosition) { + position = newPosition; + } + //New methods void takeDamage() {}; void move() {}; @@ -41,6 +49,14 @@ int main() // Create a window object with specific dimensions and a title sf::RenderWindow window(videoMode, "My SFML Window"); + //Player object + Player player; + + //Load Textures and sprite + player.player_texture.loadFromFile("assets/textures/player_ship.png"); + + player.player_sprite.setTexture(player.player_texture); + while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { @@ -49,11 +65,22 @@ int main() window.close(); } + // Handle keyboard input + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + player.move(); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + player.move(); + } // Clear the window--- window.clear(sf::Color::Black); - Player player; + player.player_sprite.setPosition(player.getPosition()); // Set the position of the player sprite + + window.draw(player.player_sprite); // Draw the player sprite + + window.display(); // Display what was drawn // Display whatever you draw window.display(); From f7c02e37d4686c4b0c40f22870c5180a40718833 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Mon, 29 Jul 2024 16:01:16 +0530 Subject: [PATCH 08/28] Added move functions --- Space-Invaders/main.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 1c0f08fff..8bf8be270 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -9,7 +9,7 @@ class Player // Private Properties int health = 3; sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); - int movement_speed = 5; + int movement_speed = 1; int player_score = 0; public: @@ -18,7 +18,15 @@ class Player sf::Texture player_texture; sf::Sprite player_sprite; - //Public Getter & Setter methods + //Public Functions, Getter & Setter methods + void move(float offsetX) { + position.x += offsetX; + } + + int getMoveSpeed() { + return movement_speed; + } + int getScore() { return player_score; }; @@ -35,7 +43,7 @@ class Player position = newPosition; } - //New methods + //New methods to be added void takeDamage() {}; void move() {}; void shootBullets() {}; @@ -67,20 +75,19 @@ int main() // Handle keyboard input if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - player.move(); + player.move(-1.0f* player.getMoveSpeed()); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - player.move(); + player.move(1.0f * player.getMoveSpeed()); } // Clear the window--- window.clear(sf::Color::Black); - player.player_sprite.setPosition(player.getPosition()); // Set the position of the player sprite - - window.draw(player.player_sprite); // Draw the player sprite + // Set and draw player + player.player_sprite.setPosition(player.getPosition()); - window.display(); // Display what was drawn + window.draw(player.player_sprite); // Display whatever you draw window.display(); From 45ea81265b2cef8b95f19842cf6a5780a16c62ad Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 30 Jul 2024 13:23:19 +0530 Subject: [PATCH 09/28] Added Header and Source folders with GameService --- Space-Invaders/Header/GameService.h | 18 ++++++++++++++ Space-Invaders/Source/GameService.cpp | 34 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Space-Invaders/Header/GameService.h create mode 100644 Space-Invaders/Source/GameService.cpp diff --git a/Space-Invaders/Header/GameService.h b/Space-Invaders/Header/GameService.h new file mode 100644 index 000000000..be6de25a8 --- /dev/null +++ b/Space-Invaders/Header/GameService.h @@ -0,0 +1,18 @@ +#pragma once + + +class GameService +{ +private: + void initialize(); // Handles game initialization. + void destroy(); // Handles cleanup tasks. + +public: + GameService(); // Constructor for initializing the GameService object. + ~GameService(); // Destructor for cleaning up resources upon object deletion. + + void ignite(); // Initiates the game. + void update(); // Updates the game logic and game state. + void render(); // Renders each frame of the game. + bool isRunning(); // Checks if the game is currently running. +}; \ No newline at end of file diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp new file mode 100644 index 000000000..0dc9832af --- /dev/null +++ b/Space-Invaders/Source/GameService.cpp @@ -0,0 +1,34 @@ +#include "../Header/GameService.h" + +void GameService::initialize() +{ +} + +void GameService::destroy() +{ +} + +GameService::GameService() +{ +} + +GameService::~GameService() +{ +} + +void GameService::ignite() +{ +} + +void GameService::update() +{ +} + +void GameService::render() +{ +} + +bool GameService::isRunning() +{ + return false; +} From e527f50120c9f729c8a745776f0b58df66e7100b Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 30 Jul 2024 13:32:04 +0530 Subject: [PATCH 10/28] comments and gameloop code shift --- Space-Invaders/main.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 8bf8be270..3659330ce 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,8 +1,9 @@ #include #include +#include "Header/GameService.h" -class Player +/*class Player { private: @@ -47,10 +48,11 @@ class Player void takeDamage() {}; void move() {}; void shootBullets() {}; -}; +};*/ int main() { + /* // Define the video mode (dimensions) sf::VideoMode videoMode = sf::VideoMode(800, 600); @@ -91,7 +93,13 @@ int main() // Display whatever you draw window.display(); + }*/ + GameService game_service; + game_service.ignite(); + + while (game_service.isRunning()) + { + game_service.update(); + game_service.render(); } - - return 0; } From a5529a711a782e280382708638e0e48bfb128f9c Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 30 Jul 2024 13:55:15 +0530 Subject: [PATCH 11/28] Added Service Locator header and cpp --- Space-Invaders/Header/GameService.h | 1 - Space-Invaders/Header/ServiceLocator.h | 28 ++++++++++++++++++++++ Space-Invaders/Source/ServiceLocator.cpp | 30 ++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 Space-Invaders/Header/ServiceLocator.h create mode 100644 Space-Invaders/Source/ServiceLocator.cpp diff --git a/Space-Invaders/Header/GameService.h b/Space-Invaders/Header/GameService.h index be6de25a8..674677d04 100644 --- a/Space-Invaders/Header/GameService.h +++ b/Space-Invaders/Header/GameService.h @@ -1,6 +1,5 @@ #pragma once - class GameService { private: diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h new file mode 100644 index 000000000..64e5d6126 --- /dev/null +++ b/Space-Invaders/Header/ServiceLocator.h @@ -0,0 +1,28 @@ +#pragma once +// ServiceLocator Class Summary: This class manages access to various services in the application. +// include relevant headers files + +class ServiceLocator +{ +private: + + // Public Methods + ServiceLocator(); + ~ServiceLocator(); + + // Private Methods: + void createServices(); + void clearAllServices(); + +public: + + // Public Methods: + static ServiceLocator* getInstance(); + void initialize(); // Initializes the ServiceLocator. + void update(); // Updates all services. + void render(); // Renders using the services. + + // Methods to Get Specific Services: + //EventService* getEventService(); // Retrieve the EventService instance + //GraphicService* getGraphicService(); // Retrieve the GraphicService instance +}; \ No newline at end of file diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp new file mode 100644 index 000000000..079366424 --- /dev/null +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -0,0 +1,30 @@ +#include "../Header/ServiceLocator.h" + +ServiceLocator::~ServiceLocator() +{ +} + +void ServiceLocator::createServices() +{ +} + +void ServiceLocator::clearAllServices() +{ +} + +ServiceLocator* ServiceLocator::getInstance() +{ + return nullptr; +} + +void ServiceLocator::initialize() +{ +} + +void ServiceLocator::update() +{ +} + +void ServiceLocator::render() +{ +} From 9c1cd5ada61a695938bb3d0927eff154137a46f9 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 30 Jul 2024 15:11:59 +0530 Subject: [PATCH 12/28] Changed Architecture and added links between units --- Space-Invaders/Header/GameService.h | 8 ++++ Space-Invaders/Header/GraphicService.h | 36 ++++++++++++++++ Space-Invaders/Header/ServiceLocator.h | 7 ++- Space-Invaders/Source/GameService.cpp | 51 ++++++++++++++++------ Space-Invaders/Source/GraphicService.cpp | 55 ++++++++++++++++++++++++ Space-Invaders/Source/ServiceLocator.cpp | 47 +++++++++++++------- Space-Invaders/main.cpp | 11 ++--- 7 files changed, 180 insertions(+), 35 deletions(-) create mode 100644 Space-Invaders/Header/GraphicService.h create mode 100644 Space-Invaders/Source/GraphicService.cpp diff --git a/Space-Invaders/Header/GameService.h b/Space-Invaders/Header/GameService.h index 674677d04..32230d67d 100644 --- a/Space-Invaders/Header/GameService.h +++ b/Space-Invaders/Header/GameService.h @@ -1,9 +1,17 @@ #pragma once +#include +#include "../Header/ServiceLocator.h" class GameService { private: + + ServiceLocator* service_locator; + sf::RenderWindow* game_window; + + void initialize(); // Handles game initialization. + void initializeVariables();// Handles game initialization. void destroy(); // Handles cleanup tasks. public: diff --git a/Space-Invaders/Header/GraphicService.h b/Space-Invaders/Header/GraphicService.h new file mode 100644 index 000000000..5f719bb1c --- /dev/null +++ b/Space-Invaders/Header/GraphicService.h @@ -0,0 +1,36 @@ +#pragma once +#include + +class GraphicService +{ +private: + + const std::string game_window_title = "Alien Invader"; + + const int game_window_width = 800; + const int game_window_height = 600; + + const sf::Color window_color = sf::Color::Blue; + + sf::VideoMode* video_mode; // ptr to video mode + sf::RenderWindow* game_window; // ptr to a RenderWindow + + void setVideoMode(); // Method for setting our video mode + void onDestroy(); // method to run when window is deleted + +public: + GraphicService(); + ~GraphicService(); //cleanup + + //method to create the game window. returns a pointer to an instance of the game window + sf::RenderWindow* createGameWindow(); + + + void initialize(); //lifecycle functions + void update(); //.. + void render(); //.. + bool isGameWindowOpen(); //check if the window is open + + sf::RenderWindow* getGameWindow(); //getter for the game window instance + sf::Color getWindowColor();//get the color +}; \ No newline at end of file diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h index 64e5d6126..78dfe507e 100644 --- a/Space-Invaders/Header/ServiceLocator.h +++ b/Space-Invaders/Header/ServiceLocator.h @@ -1,4 +1,6 @@ #pragma once +#include "../Header/GraphicService.h" + // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -6,6 +8,9 @@ class ServiceLocator { private: + // Private Attributes: + GraphicService* graphic_service; + // Public Methods ServiceLocator(); ~ServiceLocator(); @@ -24,5 +29,5 @@ class ServiceLocator // Methods to Get Specific Services: //EventService* getEventService(); // Retrieve the EventService instance - //GraphicService* getGraphicService(); // Retrieve the GraphicService instance + GraphicService* getGraphicService(); // Retrieve the GraphicService instance }; \ No newline at end of file diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp index 0dc9832af..bf1f6f464 100644 --- a/Space-Invaders/Source/GameService.cpp +++ b/Space-Invaders/Source/GameService.cpp @@ -1,34 +1,57 @@ #include "../Header/GameService.h" +#include "../Header/GraphicService.h" -void GameService::initialize() -{ +// Constructor: Initializes pointers to null. +GameService::GameService() { + service_locator = nullptr; // Set service locator to null + game_window = nullptr; // Set game window to null } -void GameService::destroy() -{ +// Destructor: Calls the destroy function to clean up resources. +GameService::~GameService() { + destroy(); // Clean up and release resources } -GameService::GameService() -{ +// Prepares the game service for use by obtaining the service locator instance and initializing services. +void GameService::ignite() { + service_locator = ServiceLocator::getInstance(); // Get ServiceLocator + initialize(); // Initialize services. } -GameService::~GameService() +//initialize service locator and other variables +void GameService::initialize() { + service_locator->initialize(); + initializeVariables(); } -void GameService::ignite() +void GameService::initializeVariables() { + game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) } -void GameService::update() +void GameService::destroy() { + // don't need to do anything here for now. } -void GameService::render() -{ +// Updates the game logic by delegating to the service locator's update method. +void GameService::update() { + + service_locator->update(); // Call update on the service locator which then updates all its managed services } -bool GameService::isRunning() -{ - return false; +// Clears the window then display it. +void GameService::render() { + // Clears the game window with the background color provided by the graphic service + game_window->clear(service_locator->getGraphicService()->getWindowColor()); + service_locator->render(); // Render the current frame using the service locator + game_window->display(); // Display the rendered frame on the game window } + +// Checks if the game is still running by querying the graphic service's window open status. +bool GameService::isRunning() { + // Returns true if the game window is open, indicating the game is still running + return service_locator->getGraphicService()->isGameWindowOpen(); +} + diff --git a/Space-Invaders/Source/GraphicService.cpp b/Space-Invaders/Source/GraphicService.cpp new file mode 100644 index 000000000..8f3aae085 --- /dev/null +++ b/Space-Invaders/Source/GraphicService.cpp @@ -0,0 +1,55 @@ +#include "../Header/GraphicService.h" + +// Constructor: Initializes game window and video mode pointers to null. +GraphicService::GraphicService() { + game_window = nullptr; // Initializes game window pointer to null + video_mode = nullptr; // Initializes video mode pointer to null +} + +// Destructor: Cleans up resources by calling onDestroy. +GraphicService::~GraphicService() { + onDestroy(); // Calls onDestroy method to clean up resources +} + +// Initializes the graphic service by creating a new game window. +void GraphicService::initialize() { + game_window = createGameWindow(); // Assigns a new game window to the game_window pointer +} + +// Creates a new SFML RenderWindow object with specified video mode and title. +sf::RenderWindow* GraphicService::createGameWindow() { + setVideoMode(); // Sets up the video mode for the window + return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object +} + +// Sets up the video mode for the game window using specified dimensions and system's color depth. +void GraphicService::setVideoMode() { + video_mode = new sf::VideoMode(game_window_width, game_window_height, sf::VideoMode::getDesktopMode().bitsPerPixel); // Allocates and sets the video mode +} + +// Cleans up allocated memory for video mode and game window to avoid memory leaks. +void GraphicService::onDestroy() { + delete(video_mode); // Deletes the video mode object + delete(game_window); // Deletes the game window object +} + +// Placeholder function for game update logic. +void GraphicService::update() { } + +// Placeholder function for game rendering logic. +void GraphicService::render() { } + +// Checks if the game window is currently open. +bool GraphicService::isGameWindowOpen() { + return game_window->isOpen(); // Returns the open status of the game window +} + +// Returns a pointer to the game window object. +sf::RenderWindow* GraphicService::getGameWindow() { + return game_window; +} + +// Returns the configured window background color. +sf::Color GraphicService::getWindowColor() { + return window_color; +} \ No newline at end of file diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp index 079366424..b3e690efa 100644 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -1,30 +1,47 @@ #include "../Header/ServiceLocator.h" -ServiceLocator::~ServiceLocator() -{ +// Constructor: Initializes the graphic_service pointer to null and creates services. +ServiceLocator::ServiceLocator() { + graphic_service = nullptr; // Initialize graphic_service to null + createServices(); // Call createServices to instantiate services } -void ServiceLocator::createServices() -{ +// Destructor: Cleans up resources by clearing all services. +ServiceLocator::~ServiceLocator() { + clearAllServices(); // Call clearAllServices to delete any allocated services } -void ServiceLocator::clearAllServices() -{ +// Creates service instances, specifically the graphic service in this case. +void ServiceLocator::createServices() { + graphic_service = new GraphicService(); // Dynamically create a GraphicService instance } -ServiceLocator* ServiceLocator::getInstance() -{ - return nullptr; +// Deletes allocated services to prevent memory leaks, specifically the graphic service. +void ServiceLocator::clearAllServices() { + delete(graphic_service); // Delete the graphic_service instance + graphic_service = nullptr; // Reset pointer to null to avoid dangling pointer } -void ServiceLocator::initialize() -{ +// Returns a pointer to ServiceLocator. +ServiceLocator* ServiceLocator::getInstance() { + static ServiceLocator instance; // we will discuss what 'static' means at a later time. + return &instance; // Return address of the instance } -void ServiceLocator::update() -{ +// Calls initialize on the graphic service, readying it for use. +void ServiceLocator::initialize() { + graphic_service->initialize(); // Initialize graphic service } -void ServiceLocator::render() -{ +// Updates the state of the graphic service. +void ServiceLocator::update() { + graphic_service->update(); // Update graphic service } + +// Renders using the graphic service. +void ServiceLocator::render() { + graphic_service->render(); // Render graphic service +} + +// Returns a pointer to the currently set graphic service. +GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } \ No newline at end of file diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 3659330ce..900795c1d 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -94,12 +94,13 @@ int main() // Display whatever you draw window.display(); }*/ - GameService game_service; - game_service.ignite(); + GameService* game_service = new GameService(); - while (game_service.isRunning()) + game_service->ignite(); + + while (game_service->isRunning()) { - game_service.update(); - game_service.render(); + game_service->update(); + game_service->render(); } } From 452befa033bbd789689c2aa65aac5971af57d02e Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Tue, 30 Jul 2024 15:28:20 +0530 Subject: [PATCH 13/28] added event service header and cpp --- Space-Invaders/Header/EventService.h | 28 +++++++++++ Space-Invaders/Header/ServiceLocator.h | 4 +- Space-Invaders/Source/EventService.cpp | 42 ++++++++++++++++ Space-Invaders/Source/GameService.cpp | 2 + Space-Invaders/Source/ServiceLocator.cpp | 64 +++++++++++++----------- 5 files changed, 109 insertions(+), 31 deletions(-) create mode 100644 Space-Invaders/Header/EventService.h create mode 100644 Space-Invaders/Source/EventService.cpp diff --git a/Space-Invaders/Header/EventService.h b/Space-Invaders/Header/EventService.h new file mode 100644 index 000000000..80d016bb2 --- /dev/null +++ b/Space-Invaders/Header/EventService.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include + +class EventService +{ +private: + sf::Event game_event; //event var + sf::RenderWindow* game_window; //ptr to our game window + + bool isGameWindowOpen(); + bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. + bool hasQuitGame(); //for our new 'ESC' condition + + + +public: + EventService(); + ~EventService(); + + void initialize(); + void update(); + void processEvents(); // while window is open we will check for events + bool pressedEscapeKey(); + bool isKeyboardEvent(); + +}; \ No newline at end of file diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h index 78dfe507e..19d7a008d 100644 --- a/Space-Invaders/Header/ServiceLocator.h +++ b/Space-Invaders/Header/ServiceLocator.h @@ -1,5 +1,6 @@ #pragma once #include "../Header/GraphicService.h" +#include "../Header/EventService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -10,6 +11,7 @@ class ServiceLocator // Private Attributes: GraphicService* graphic_service; + EventService* event_service; // Public Methods ServiceLocator(); @@ -28,6 +30,6 @@ class ServiceLocator void render(); // Renders using the services. // Methods to Get Specific Services: - //EventService* getEventService(); // Retrieve the EventService instance + EventService* getEventService(); // Retrieve the EventService instance GraphicService* getGraphicService(); // Retrieve the GraphicService instance }; \ No newline at end of file diff --git a/Space-Invaders/Source/EventService.cpp b/Space-Invaders/Source/EventService.cpp new file mode 100644 index 000000000..d8fc0d68b --- /dev/null +++ b/Space-Invaders/Source/EventService.cpp @@ -0,0 +1,42 @@ +#include "../Header/EventService.h" +#include "../Header/GameService.h" +#include "../Header/GraphicService.h" + +EventService::EventService() { game_window = nullptr; } + +EventService::~EventService() = default; //calls the default destructor + +void EventService::initialize() +{ + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); +} + +void EventService::update() +{ + //for later +} + +void EventService::processEvents() +{ + if (isGameWindowOpen()) { + while (game_window->pollEvent(game_event)) { + // Check for window closure + if (gameWindowWasClosed() || hasQuitGame()) + { + game_window->close(); + } + } + } +} + +bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered + +//checks for if a keyboard key has been pressed +bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; } + +//control click on the SFML functions to see what they do internally +bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } + +bool EventService::isGameWindowOpen() { return game_window != nullptr; } + +bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } \ No newline at end of file diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp index bf1f6f464..e0fe519e8 100644 --- a/Space-Invaders/Source/GameService.cpp +++ b/Space-Invaders/Source/GameService.cpp @@ -38,6 +38,8 @@ void GameService::destroy() // Updates the game logic by delegating to the service locator's update method. void GameService::update() { + service_locator->getEventService()->processEvents(); + service_locator->update(); // Call update on the service locator which then updates all its managed services } diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp index b3e690efa..af461c730 100644 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -1,47 +1,51 @@ #include "../Header/ServiceLocator.h" -// Constructor: Initializes the graphic_service pointer to null and creates services. -ServiceLocator::ServiceLocator() { - graphic_service = nullptr; // Initialize graphic_service to null - createServices(); // Call createServices to instantiate services +ServiceLocator::ServiceLocator() +{ + graphic_service = nullptr; + event_service = nullptr; + createServices(); } - -// Destructor: Cleans up resources by clearing all services. -ServiceLocator::~ServiceLocator() { - clearAllServices(); // Call clearAllServices to delete any allocated services +ServiceLocator::~ServiceLocator() +{ + clearAllServices(); } -// Creates service instances, specifically the graphic service in this case. -void ServiceLocator::createServices() { - graphic_service = new GraphicService(); // Dynamically create a GraphicService instance +void ServiceLocator::createServices() +{ + graphic_service = new GraphicService(); + event_service = new EventService(); } -// Deletes allocated services to prevent memory leaks, specifically the graphic service. -void ServiceLocator::clearAllServices() { - delete(graphic_service); // Delete the graphic_service instance - graphic_service = nullptr; // Reset pointer to null to avoid dangling pointer +void ServiceLocator::clearAllServices() +{ + delete(graphic_service); + delete(event_service); } -// Returns a pointer to ServiceLocator. -ServiceLocator* ServiceLocator::getInstance() { - static ServiceLocator instance; // we will discuss what 'static' means at a later time. - return &instance; // Return address of the instance +ServiceLocator* ServiceLocator::getInstance() +{ + static ServiceLocator instance; + return &instance; } -// Calls initialize on the graphic service, readying it for use. -void ServiceLocator::initialize() { - graphic_service->initialize(); // Initialize graphic service +void ServiceLocator::initialize() +{ + graphic_service->initialize(); + event_service->initialize(); } -// Updates the state of the graphic service. -void ServiceLocator::update() { - graphic_service->update(); // Update graphic service +void ServiceLocator::update() +{ + graphic_service->update(); + event_service->update(); } -// Renders using the graphic service. -void ServiceLocator::render() { - graphic_service->render(); // Render graphic service +void ServiceLocator::render() +{ + graphic_service->render(); + //no event service because nothing to render } -// Returns a pointer to the currently set graphic service. -GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } \ No newline at end of file +GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } +EventService* ServiceLocator::getEventService() { return event_service; } From abdf09b6af4166d62085762cb955722d13a60e9f Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Wed, 31 Jul 2024 13:30:41 +0530 Subject: [PATCH 14/28] Added Player Service Header and cpp --- Space-Invaders/Header/EventService.h | 2 + Space-Invaders/Header/PlayerService.h | 37 ++++++++++++++ Space-Invaders/Header/ServiceLocator.h | 3 ++ Space-Invaders/Source/EventService.cpp | 6 ++- Space-Invaders/Source/PlayerService.cpp | 64 ++++++++++++++++++++++++ Space-Invaders/Source/ServiceLocator.cpp | 7 +++ 6 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 Space-Invaders/Header/PlayerService.h create mode 100644 Space-Invaders/Source/PlayerService.cpp diff --git a/Space-Invaders/Header/EventService.h b/Space-Invaders/Header/EventService.h index 80d016bb2..ce9882ddc 100644 --- a/Space-Invaders/Header/EventService.h +++ b/Space-Invaders/Header/EventService.h @@ -24,5 +24,7 @@ class EventService void processEvents(); // while window is open we will check for events bool pressedEscapeKey(); bool isKeyboardEvent(); + bool pressedLeftKey(); // getting inputs for the player + bool pressedRightKey(); }; \ No newline at end of file diff --git a/Space-Invaders/Header/PlayerService.h b/Space-Invaders/Header/PlayerService.h new file mode 100644 index 000000000..9aadf0a0c --- /dev/null +++ b/Space-Invaders/Header/PlayerService.h @@ -0,0 +1,37 @@ +#pragma once +#include + +class PlayerService +{ + +private: + + int health = 3; + sf::Vector2f position = sf::Vector2f(200.0f, 300.0f); + int movement_speed = 1; + int player_score = 0; + + const sf::String player_texture_path = "assets/textures/player_ship.png"; + + sf::Texture player_texture; + sf::Sprite player_sprite; + + sf::RenderWindow* game_window; //as always + + void initializePlayerSprite(); + void processPlayerInput(); + +public: + + PlayerService(); + ~PlayerService(); + + void initialize(); + void update(); + void render(); + + void move(float offsetX); + int getMoveSpeed(); + sf::Vector2f getPlayerPosition(); + +}; \ No newline at end of file diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h index 19d7a008d..7c9a1d6ac 100644 --- a/Space-Invaders/Header/ServiceLocator.h +++ b/Space-Invaders/Header/ServiceLocator.h @@ -1,6 +1,7 @@ #pragma once #include "../Header/GraphicService.h" #include "../Header/EventService.h" +#include "../Header/PlayerService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -12,6 +13,7 @@ class ServiceLocator // Private Attributes: GraphicService* graphic_service; EventService* event_service; + PlayerService* player_service; // Public Methods ServiceLocator(); @@ -32,4 +34,5 @@ class ServiceLocator // Methods to Get Specific Services: EventService* getEventService(); // Retrieve the EventService instance GraphicService* getGraphicService(); // Retrieve the GraphicService instance + PlayerService* getPlayerService(); // Retrieve the PlayerService instance }; \ No newline at end of file diff --git a/Space-Invaders/Source/EventService.cpp b/Space-Invaders/Source/EventService.cpp index d8fc0d68b..6f2941e93 100644 --- a/Space-Invaders/Source/EventService.cpp +++ b/Space-Invaders/Source/EventService.cpp @@ -36,7 +36,9 @@ bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyP //control click on the SFML functions to see what they do internally bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } - bool EventService::isGameWindowOpen() { return game_window != nullptr; } +bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } -bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } \ No newline at end of file +// Player inputs +bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } +bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } \ No newline at end of file diff --git a/Space-Invaders/Source/PlayerService.cpp b/Space-Invaders/Source/PlayerService.cpp new file mode 100644 index 000000000..e64c0dc1a --- /dev/null +++ b/Space-Invaders/Source/PlayerService.cpp @@ -0,0 +1,64 @@ +#include "../Header/PlayerService.h" +#include "../Header/ServiceLocator.h" + +PlayerService::PlayerService() +{ + game_window = nullptr; +} + +PlayerService::~PlayerService() = default; + +//init +void PlayerService::initialize() +{ + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); +} + +//take our players input in update, then set the position. +//order is important here +void PlayerService::update() +{ + processPlayerInput(); + player_sprite.setPosition(getPlayerPosition()); +} + +void PlayerService::render() +{ + game_window->draw(player_sprite); +} + +void PlayerService::processPlayerInput() +{ + // Handle movement - inputs now handled by eventservice + EventService* event_service = ServiceLocator::getInstance()->getEventService(); //get the event service object created in service locator + + if (event_service->isKeyboardEvent()) + { + if (event_service->pressedLeftKey()) + { + move(-1.0f * getMoveSpeed()); + } + + if (event_service->pressedRightKey()) + { + move(1.0f * getMoveSpeed()); + } + } +} + +void PlayerService::initializePlayerSprite() +{ + if (player_texture.loadFromFile(player_texture_path)) + { + player_sprite.setTexture(player_texture); + } +} + +void PlayerService::move(float offsetX) { + position.x += offsetX; +} + +//Getter and Setter Functions +sf::Vector2f PlayerService::getPlayerPosition() { return position; } +int PlayerService::getMoveSpeed() { return movement_speed; } \ No newline at end of file diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp index af461c730..0e95973c7 100644 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -4,6 +4,7 @@ ServiceLocator::ServiceLocator() { graphic_service = nullptr; event_service = nullptr; + player_service = nullptr; createServices(); } ServiceLocator::~ServiceLocator() @@ -15,12 +16,14 @@ void ServiceLocator::createServices() { graphic_service = new GraphicService(); event_service = new EventService(); + player_service = new PlayerService(); } void ServiceLocator::clearAllServices() { delete(graphic_service); delete(event_service); + delete(player_service); } ServiceLocator* ServiceLocator::getInstance() @@ -33,19 +36,23 @@ void ServiceLocator::initialize() { graphic_service->initialize(); event_service->initialize(); + player_service->initialize(); } void ServiceLocator::update() { graphic_service->update(); event_service->update(); + player_service->update(); } void ServiceLocator::render() { graphic_service->render(); + player_service->render(); //no event service because nothing to render } GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } EventService* ServiceLocator::getEventService() { return event_service; } +PlayerService* ServiceLocator::getPlayerService() { return player_service; } From e759f6e768adf7e03348899f4840dfbcb5a270ea Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Wed, 31 Jul 2024 14:26:58 +0530 Subject: [PATCH 15/28] Added time Service --- Space-Invaders/Header/GraphicService.h | 2 ++ Space-Invaders/Header/PlayerService.h | 5 ++- Space-Invaders/Header/ServiceLocator.h | 3 ++ Space-Invaders/Header/TimeService.h | 28 +++++++++++++++++ Space-Invaders/Source/GraphicService.cpp | 3 +- Space-Invaders/Source/PlayerService.cpp | 17 +++++++++-- Space-Invaders/Source/ServiceLocator.cpp | 7 +++++ Space-Invaders/Source/TimeService.cpp | 39 ++++++++++++++++++++++++ 8 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 Space-Invaders/Header/TimeService.h create mode 100644 Space-Invaders/Source/TimeService.cpp diff --git a/Space-Invaders/Header/GraphicService.h b/Space-Invaders/Header/GraphicService.h index 5f719bb1c..96ed95751 100644 --- a/Space-Invaders/Header/GraphicService.h +++ b/Space-Invaders/Header/GraphicService.h @@ -9,6 +9,8 @@ class GraphicService const int game_window_width = 800; const int game_window_height = 600; + + const int frame_rate = 60; const sf::Color window_color = sf::Color::Blue; diff --git a/Space-Invaders/Header/PlayerService.h b/Space-Invaders/Header/PlayerService.h index 9aadf0a0c..66397a35c 100644 --- a/Space-Invaders/Header/PlayerService.h +++ b/Space-Invaders/Header/PlayerService.h @@ -8,7 +8,7 @@ class PlayerService int health = 3; sf::Vector2f position = sf::Vector2f(200.0f, 300.0f); - int movement_speed = 1; + float movement_speed = 350.f; int player_score = 0; const sf::String player_texture_path = "assets/textures/player_ship.png"; @@ -21,6 +21,9 @@ class PlayerService void initializePlayerSprite(); void processPlayerInput(); + void moveLeft(); + void moveRight(); + public: PlayerService(); diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h index 7c9a1d6ac..df101d414 100644 --- a/Space-Invaders/Header/ServiceLocator.h +++ b/Space-Invaders/Header/ServiceLocator.h @@ -2,6 +2,7 @@ #include "../Header/GraphicService.h" #include "../Header/EventService.h" #include "../Header/PlayerService.h" +#include "../Header/TimeService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -14,6 +15,7 @@ class ServiceLocator GraphicService* graphic_service; EventService* event_service; PlayerService* player_service; + TimeService* time_service; // Public Methods ServiceLocator(); @@ -35,4 +37,5 @@ class ServiceLocator EventService* getEventService(); // Retrieve the EventService instance GraphicService* getGraphicService(); // Retrieve the GraphicService instance PlayerService* getPlayerService(); // Retrieve the PlayerService instance + TimeService* getTimeService(); // Retrieve the TimeService instance }; \ No newline at end of file diff --git a/Space-Invaders/Header/TimeService.h b/Space-Invaders/Header/TimeService.h new file mode 100644 index 000000000..24ddec14a --- /dev/null +++ b/Space-Invaders/Header/TimeService.h @@ -0,0 +1,28 @@ +#pragma once +#include + + // The TimeService class helps keep track of time in game and calculate delta time. + // Utilizes the library to calculate delta time. +class TimeService +{ +private: + + // A point in time which indicates the starting time of previous frame. + std::chrono::time_point previous_time; + + float delta_time; //to store the detla time + + void updateDeltaTime(); // method to update time + float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time + void updatePreviousTime(); // finally update the current time to be previous time + +public: + + //lifecycle methods + void initialize(); + void update(); + + //getter + float getDeltaTime(); +}; + diff --git a/Space-Invaders/Source/GraphicService.cpp b/Space-Invaders/Source/GraphicService.cpp index 8f3aae085..2cfef211f 100644 --- a/Space-Invaders/Source/GraphicService.cpp +++ b/Space-Invaders/Source/GraphicService.cpp @@ -13,7 +13,8 @@ GraphicService::~GraphicService() { // Initializes the graphic service by creating a new game window. void GraphicService::initialize() { - game_window = createGameWindow(); // Assigns a new game window to the game_window pointer + game_window = createGameWindow(); // Assigns a new game window + game_window->setFramerateLimit(frame_rate); // setting a frame rate limit } // Creates a new SFML RenderWindow object with specified video mode and title. diff --git a/Space-Invaders/Source/PlayerService.cpp b/Space-Invaders/Source/PlayerService.cpp index e64c0dc1a..0d8e4e45e 100644 --- a/Space-Invaders/Source/PlayerService.cpp +++ b/Space-Invaders/Source/PlayerService.cpp @@ -37,16 +37,27 @@ void PlayerService::processPlayerInput() { if (event_service->pressedLeftKey()) { - move(-1.0f * getMoveSpeed()); + moveLeft(); } if (event_service->pressedRightKey()) { - move(1.0f * getMoveSpeed()); + moveRight(); } } } +// New movement methods +void PlayerService::moveLeft() +{ + position.x -= movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); +} + +void PlayerService::moveRight() +{ + position.x += movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); +} + void PlayerService::initializePlayerSprite() { if (player_texture.loadFromFile(player_texture_path)) @@ -56,7 +67,7 @@ void PlayerService::initializePlayerSprite() } void PlayerService::move(float offsetX) { - position.x += offsetX; + position.x += offsetX * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); } //Getter and Setter Functions diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp index 0e95973c7..a8fbc2d90 100644 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -5,6 +5,7 @@ ServiceLocator::ServiceLocator() graphic_service = nullptr; event_service = nullptr; player_service = nullptr; + time_service = nullptr; createServices(); } ServiceLocator::~ServiceLocator() @@ -17,6 +18,7 @@ void ServiceLocator::createServices() graphic_service = new GraphicService(); event_service = new EventService(); player_service = new PlayerService(); + time_service = new TimeService(); } void ServiceLocator::clearAllServices() @@ -24,6 +26,7 @@ void ServiceLocator::clearAllServices() delete(graphic_service); delete(event_service); delete(player_service); + delete(time_service); } ServiceLocator* ServiceLocator::getInstance() @@ -37,6 +40,7 @@ void ServiceLocator::initialize() graphic_service->initialize(); event_service->initialize(); player_service->initialize(); + time_service->initialize(); } void ServiceLocator::update() @@ -44,6 +48,7 @@ void ServiceLocator::update() graphic_service->update(); event_service->update(); player_service->update(); + time_service->update(); } void ServiceLocator::render() @@ -51,8 +56,10 @@ void ServiceLocator::render() graphic_service->render(); player_service->render(); //no event service because nothing to render + //no time service } GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } EventService* ServiceLocator::getEventService() { return event_service; } PlayerService* ServiceLocator::getPlayerService() { return player_service; } +TimeService* ServiceLocator::getTimeService() { return time_service; } diff --git a/Space-Invaders/Source/TimeService.cpp b/Space-Invaders/Source/TimeService.cpp new file mode 100644 index 000000000..7ede82971 --- /dev/null +++ b/Space-Invaders/Source/TimeService.cpp @@ -0,0 +1,39 @@ +#include "../Header/TimeService.h" + +void TimeService::initialize() +{ + previous_time = std::chrono::steady_clock::now(); + delta_time = 0; +} + +void TimeService::update() +{ + updateDeltaTime(); +} + +float TimeService::getDeltaTime() +{ + return delta_time; +} + +void TimeService::updateDeltaTime() +{ + delta_time = calculateDeltaTime(); + updatePreviousTime(); +} + +float TimeService::calculateDeltaTime() +{ + // Calculate time difference in microseconds between the current and previous frame. + int delta = std::chrono::duration_cast( + std::chrono::steady_clock::now() - previous_time).count(); + + // The cast is used to convert delta time from microseconds into seconds. + return static_cast(delta) / static_cast(1000000); +} + +// Update previous_time to the current time +void TimeService::updatePreviousTime() +{ + previous_time = std::chrono::steady_clock::now(); +} From 943dfd9925012b9808f071d5bd12b06d098ca778 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Wed, 31 Jul 2024 15:23:11 +0530 Subject: [PATCH 16/28] Split the player into 3 different units --- .../Header/Player/PlayerController.h | 1 + Space-Invaders/Header/Player/PlayerModel.h | 43 ++++++++++++++++++ Space-Invaders/Header/Player/PlayerView.h | 1 + .../Source/Player/PlayerController.cpp | 0 Space-Invaders/Source/Player/PlayerModel.cpp | 45 +++++++++++++++++++ Space-Invaders/Source/Player/PlayerView.cpp | 0 6 files changed, 90 insertions(+) create mode 100644 Space-Invaders/Header/Player/PlayerController.h create mode 100644 Space-Invaders/Header/Player/PlayerModel.h create mode 100644 Space-Invaders/Header/Player/PlayerView.h create mode 100644 Space-Invaders/Source/Player/PlayerController.cpp create mode 100644 Space-Invaders/Source/Player/PlayerModel.cpp create mode 100644 Space-Invaders/Source/Player/PlayerView.cpp diff --git a/Space-Invaders/Header/Player/PlayerController.h b/Space-Invaders/Header/Player/PlayerController.h new file mode 100644 index 000000000..50e96676b --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerController.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Header/Player/PlayerModel.h b/Space-Invaders/Header/Player/PlayerModel.h new file mode 100644 index 000000000..cf048eca5 --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerModel.h @@ -0,0 +1,43 @@ +#pragma once +#include + +enum class PlayerState //Our Enum +{ + ALIVE, + DEAD, + // we will add more states later +}; + +class PlayerModel +{ +private: + const sf::Vector2f initial_player_position = sf::Vector2f(500.f, 500.f); + + sf::Vector2f player_position; + PlayerState player_state; //Declaration + int player_score; + +public: + const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); + const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); + + const float player_movement_speed = 200.0f; + + PlayerModel(); + ~PlayerModel(); + + void initialize(); + void reset(); + + sf::Vector2f getPlayerPosition(); + void setPlayerPosition(sf::Vector2f position); + + int getPlayerScore(); + void setPlayerScore(int score); + + //new getter and setter + PlayerState getPlayerState(); + void setPlayerState(PlayerState state); + + +}; diff --git a/Space-Invaders/Header/Player/PlayerView.h b/Space-Invaders/Header/Player/PlayerView.h new file mode 100644 index 000000000..50e96676b --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerView.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Space-Invaders/Source/Player/PlayerModel.cpp b/Space-Invaders/Source/Player/PlayerModel.cpp new file mode 100644 index 000000000..0d51db820 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerModel.cpp @@ -0,0 +1,45 @@ +#include "../../Header/Player/PlayerModel.h" + +PlayerModel::PlayerModel() { } + +PlayerModel::~PlayerModel() { } + +void PlayerModel::initialize() { reset(); } // remember to call reset() + +void PlayerModel::reset() +{ + player_state = PlayerState::ALIVE; // set state to alive + player_position = initial_player_position; + player_score = 0; +} + +sf::Vector2f PlayerModel::getPlayerPosition() +{ + return player_position; +} + +void PlayerModel::setPlayerPosition(sf::Vector2f position) +{ + player_position = position; +} + +int PlayerModel::getPlayerScore() +{ + return player_score; +} + +void PlayerModel::setPlayerScore(int score) +{ + player_score = score; +} + +//.. +PlayerState PlayerModel::getPlayerState() +{ + return player_state; +} + +void PlayerModel::setPlayerState(PlayerState state) +{ + player_state = state; +} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp new file mode 100644 index 000000000..e69de29bb From e01b88b7f463b44ebfa31ca910838490bc97376e Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Wed, 31 Jul 2024 15:38:14 +0530 Subject: [PATCH 17/28] Added code to player units --- .../Header/Player/PlayerController.h | 24 +++++++ Space-Invaders/Header/Player/PlayerView.h | 30 ++++++++ Space-Invaders/Header/PlayerService.h | 40 +++-------- .../Source/Player/PlayerController.cpp | 72 +++++++++++++++++++ Space-Invaders/Source/Player/PlayerView.cpp | 50 +++++++++++++ Space-Invaders/Source/PlayerService.cpp | 68 +++--------------- 6 files changed, 194 insertions(+), 90 deletions(-) diff --git a/Space-Invaders/Header/Player/PlayerController.h b/Space-Invaders/Header/Player/PlayerController.h index 50e96676b..9ea24d95d 100644 --- a/Space-Invaders/Header/Player/PlayerController.h +++ b/Space-Invaders/Header/Player/PlayerController.h @@ -1 +1,25 @@ #pragma once +#include +#include "../Player/PlayerModel.h" +#include "../Player/PlayerView.h" + +class PlayerController +{ +private: + PlayerView* player_view; + PlayerModel* player_model; + + void processPlayerInput(); + void moveLeft(); + void moveRight(); + +public: + PlayerController(); + ~PlayerController(); + + void initialize(); + void update(); + void render(); + + sf::Vector2f getPlayerPosition(); +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerView.h b/Space-Invaders/Header/Player/PlayerView.h index 50e96676b..8a1aa85e9 100644 --- a/Space-Invaders/Header/Player/PlayerView.h +++ b/Space-Invaders/Header/Player/PlayerView.h @@ -1 +1,31 @@ #pragma once +#include +#include "../../Header/Player/PlayerController.h" + +class PlayerView +{ +private: + + const sf::String player_texture_path = "assets/textures/player_ship.png"; + const float player_sprite_width = 60.f; + const float player_sprite_height = 60.f; + + sf::RenderWindow* game_window; + + sf::Texture player_texture; + sf::Sprite player_sprite; + + void initializePlayerSprite(); + void scalePlayerSprite(); + + PlayerController* player_controller; // ptr to player controller + +public: + PlayerView(); + ~PlayerView(); + + void initialize(); + void update(); + void render(); + void initialize(PlayerController* controller); +}; \ No newline at end of file diff --git a/Space-Invaders/Header/PlayerService.h b/Space-Invaders/Header/PlayerService.h index 66397a35c..2f46d4894 100644 --- a/Space-Invaders/Header/PlayerService.h +++ b/Space-Invaders/Header/PlayerService.h @@ -1,40 +1,16 @@ #pragma once -#include +#include "../../Header/Player/PlayerController.h" class PlayerService { - private: - - int health = 3; - sf::Vector2f position = sf::Vector2f(200.0f, 300.0f); - float movement_speed = 350.f; - int player_score = 0; - - const sf::String player_texture_path = "assets/textures/player_ship.png"; - - sf::Texture player_texture; - sf::Sprite player_sprite; - - sf::RenderWindow* game_window; //as always - - void initializePlayerSprite(); - void processPlayerInput(); - - void moveLeft(); - void moveRight(); + PlayerController* player_controller; public: + PlayerService(); + ~PlayerService(); - PlayerService(); - ~PlayerService(); - - void initialize(); - void update(); - void render(); - - void move(float offsetX); - int getMoveSpeed(); - sf::Vector2f getPlayerPosition(); - -}; \ No newline at end of file + void initialize(); + void update(); + void render(); +}; diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp index e69de29bb..19181c698 100644 --- a/Space-Invaders/Source/Player/PlayerController.cpp +++ b/Space-Invaders/Source/Player/PlayerController.cpp @@ -0,0 +1,72 @@ +#include "../../Header/Player/PlayerController.h" +#include "../../Header/EventService.h" +#include "../../Header/ServiceLocator.h" +#include + +PlayerController::PlayerController() +{ + player_view = new PlayerView(); + player_model = new PlayerModel(); +} + +PlayerController::~PlayerController() +{ + delete (player_view); + delete (player_model); +} +//the controller is responsible for calling the lifecycle methods for the other two +void PlayerController::initialize() +{ + player_model->initialize(); + + //This will give an error right now since we haven't included the controller in the view. + player_view->initialize(this); // 'this' refers to the class we are currently inside +} + +void PlayerController::update() +{ + processPlayerInput(); + player_view->update(); // we update() the view +} + +void PlayerController::render() +{ + player_view->render(); // render the view +} + +sf::Vector2f PlayerController::getPlayerPosition() +{ + return player_model->getPlayerPosition(); +} + +void PlayerController::processPlayerInput() +{ + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) + { + moveLeft(); + } + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) + { + moveRight(); + } +} + +void PlayerController::moveLeft() +{ + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x -= player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + currentPosition.x = std::max(currentPosition.x, player_model->left_most_position.x); + player_model->setPlayerPosition(currentPosition); +} + +void PlayerController::moveRight() +{ + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x += player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + currentPosition.x = std::min(currentPosition.x, player_model->right_most_position.x); + player_model->setPlayerPosition(currentPosition); +} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp index e69de29bb..93762cd15 100644 --- a/Space-Invaders/Source/Player/PlayerView.cpp +++ b/Space-Invaders/Source/Player/PlayerView.cpp @@ -0,0 +1,50 @@ +#include "../../Header/Player/PlayerView.h" +#include "../../Header/ServiceLocator.h" + +PlayerView::PlayerView() { } + +PlayerView::~PlayerView() { } + +void PlayerView::initialize() +{ + + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); +} + +void PlayerView::initialize(PlayerController* controller) +{ + player_controller = controller; //to later use it for setting position + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); +} + +void PlayerView::initializePlayerSprite() +{ + if (player_texture.loadFromFile(player_texture_path)) + { + player_sprite.setTexture(player_texture); + scalePlayerSprite(); + } +} + +void PlayerView::scalePlayerSprite() +{ + // setScale is an inbuilt method of the sprite class that takes two floats to scale the sprite. it scales the sprite to our desired height + player_sprite.setScale( + //Here we find the factor to scale our sprites with. Ignore the static_cast for now, we will discuss it later. + static_cast(player_sprite_width) / player_sprite.getTexture()->getSize().x, + static_cast(player_sprite_height) / player_sprite.getTexture()->getSize().y + ); +} + +void PlayerView::update() +{ + //set the updated position before we render + player_sprite.setPosition(player_controller->getPlayerPosition()); +} + +void PlayerView::render() +{ + game_window->draw(player_sprite); +} \ No newline at end of file diff --git a/Space-Invaders/Source/PlayerService.cpp b/Space-Invaders/Source/PlayerService.cpp index 0d8e4e45e..8b19d77f9 100644 --- a/Space-Invaders/Source/PlayerService.cpp +++ b/Space-Invaders/Source/PlayerService.cpp @@ -1,75 +1,27 @@ #include "../Header/PlayerService.h" -#include "../Header/ServiceLocator.h" +#include "../Header/Player/PlayerController.h" PlayerService::PlayerService() { - game_window = nullptr; + player_controller = new PlayerController(); } -PlayerService::~PlayerService() = default; +PlayerService::~PlayerService() +{ + delete (player_controller); +} -//init void PlayerService::initialize() { - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializePlayerSprite(); + player_controller->initialize(); } -//take our players input in update, then set the position. -//order is important here void PlayerService::update() { - processPlayerInput(); - player_sprite.setPosition(getPlayerPosition()); + player_controller->update(); } void PlayerService::render() { - game_window->draw(player_sprite); -} - -void PlayerService::processPlayerInput() -{ - // Handle movement - inputs now handled by eventservice - EventService* event_service = ServiceLocator::getInstance()->getEventService(); //get the event service object created in service locator - - if (event_service->isKeyboardEvent()) - { - if (event_service->pressedLeftKey()) - { - moveLeft(); - } - - if (event_service->pressedRightKey()) - { - moveRight(); - } - } -} - -// New movement methods -void PlayerService::moveLeft() -{ - position.x -= movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); -} - -void PlayerService::moveRight() -{ - position.x += movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); -} - -void PlayerService::initializePlayerSprite() -{ - if (player_texture.loadFromFile(player_texture_path)) - { - player_sprite.setTexture(player_texture); - } -} - -void PlayerService::move(float offsetX) { - position.x += offsetX * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); -} - -//Getter and Setter Functions -sf::Vector2f PlayerService::getPlayerPosition() { return position; } -int PlayerService::getMoveSpeed() { return movement_speed; } \ No newline at end of file + player_controller->render(); +} \ No newline at end of file From 621747db4a8ed9a8c99a3409b47593db7dd674bb Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:13:13 +0530 Subject: [PATCH 18/28] Delete Space-Invaders/Header directory --- Space-Invaders/Header/EventService.h | 28 -------------------- Space-Invaders/Header/GameService.h | 25 ------------------ Space-Invaders/Header/GraphicService.h | 36 -------------------------- Space-Invaders/Header/ServiceLocator.h | 35 ------------------------- 4 files changed, 124 deletions(-) delete mode 100644 Space-Invaders/Header/EventService.h delete mode 100644 Space-Invaders/Header/GameService.h delete mode 100644 Space-Invaders/Header/GraphicService.h delete mode 100644 Space-Invaders/Header/ServiceLocator.h diff --git a/Space-Invaders/Header/EventService.h b/Space-Invaders/Header/EventService.h deleted file mode 100644 index 80d016bb2..000000000 --- a/Space-Invaders/Header/EventService.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include -#include - -class EventService -{ -private: - sf::Event game_event; //event var - sf::RenderWindow* game_window; //ptr to our game window - - bool isGameWindowOpen(); - bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. - bool hasQuitGame(); //for our new 'ESC' condition - - - -public: - EventService(); - ~EventService(); - - void initialize(); - void update(); - void processEvents(); // while window is open we will check for events - bool pressedEscapeKey(); - bool isKeyboardEvent(); - -}; \ No newline at end of file diff --git a/Space-Invaders/Header/GameService.h b/Space-Invaders/Header/GameService.h deleted file mode 100644 index 32230d67d..000000000 --- a/Space-Invaders/Header/GameService.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include -#include "../Header/ServiceLocator.h" - -class GameService -{ -private: - - ServiceLocator* service_locator; - sf::RenderWindow* game_window; - - - void initialize(); // Handles game initialization. - void initializeVariables();// Handles game initialization. - void destroy(); // Handles cleanup tasks. - -public: - GameService(); // Constructor for initializing the GameService object. - ~GameService(); // Destructor for cleaning up resources upon object deletion. - - void ignite(); // Initiates the game. - void update(); // Updates the game logic and game state. - void render(); // Renders each frame of the game. - bool isRunning(); // Checks if the game is currently running. -}; \ No newline at end of file diff --git a/Space-Invaders/Header/GraphicService.h b/Space-Invaders/Header/GraphicService.h deleted file mode 100644 index 5f719bb1c..000000000 --- a/Space-Invaders/Header/GraphicService.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once -#include - -class GraphicService -{ -private: - - const std::string game_window_title = "Alien Invader"; - - const int game_window_width = 800; - const int game_window_height = 600; - - const sf::Color window_color = sf::Color::Blue; - - sf::VideoMode* video_mode; // ptr to video mode - sf::RenderWindow* game_window; // ptr to a RenderWindow - - void setVideoMode(); // Method for setting our video mode - void onDestroy(); // method to run when window is deleted - -public: - GraphicService(); - ~GraphicService(); //cleanup - - //method to create the game window. returns a pointer to an instance of the game window - sf::RenderWindow* createGameWindow(); - - - void initialize(); //lifecycle functions - void update(); //.. - void render(); //.. - bool isGameWindowOpen(); //check if the window is open - - sf::RenderWindow* getGameWindow(); //getter for the game window instance - sf::Color getWindowColor();//get the color -}; \ No newline at end of file diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h deleted file mode 100644 index 19d7a008d..000000000 --- a/Space-Invaders/Header/ServiceLocator.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once -#include "../Header/GraphicService.h" -#include "../Header/EventService.h" - -// ServiceLocator Class Summary: This class manages access to various services in the application. -// include relevant headers files - -class ServiceLocator -{ -private: - - // Private Attributes: - GraphicService* graphic_service; - EventService* event_service; - - // Public Methods - ServiceLocator(); - ~ServiceLocator(); - - // Private Methods: - void createServices(); - void clearAllServices(); - -public: - - // Public Methods: - static ServiceLocator* getInstance(); - void initialize(); // Initializes the ServiceLocator. - void update(); // Updates all services. - void render(); // Renders using the services. - - // Methods to Get Specific Services: - EventService* getEventService(); // Retrieve the EventService instance - GraphicService* getGraphicService(); // Retrieve the GraphicService instance -}; \ No newline at end of file From 433948443619e10fb6cd4f48589262f61a25e7e2 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:13:29 +0530 Subject: [PATCH 19/28] Delete Space-Invaders/Source directory --- Space-Invaders/Source/EventService.cpp | 42 ----------------- Space-Invaders/Source/GameService.cpp | 59 ------------------------ Space-Invaders/Source/GraphicService.cpp | 55 ---------------------- Space-Invaders/Source/ServiceLocator.cpp | 51 -------------------- 4 files changed, 207 deletions(-) delete mode 100644 Space-Invaders/Source/EventService.cpp delete mode 100644 Space-Invaders/Source/GameService.cpp delete mode 100644 Space-Invaders/Source/GraphicService.cpp delete mode 100644 Space-Invaders/Source/ServiceLocator.cpp diff --git a/Space-Invaders/Source/EventService.cpp b/Space-Invaders/Source/EventService.cpp deleted file mode 100644 index d8fc0d68b..000000000 --- a/Space-Invaders/Source/EventService.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "../Header/EventService.h" -#include "../Header/GameService.h" -#include "../Header/GraphicService.h" - -EventService::EventService() { game_window = nullptr; } - -EventService::~EventService() = default; //calls the default destructor - -void EventService::initialize() -{ - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); -} - -void EventService::update() -{ - //for later -} - -void EventService::processEvents() -{ - if (isGameWindowOpen()) { - while (game_window->pollEvent(game_event)) { - // Check for window closure - if (gameWindowWasClosed() || hasQuitGame()) - { - game_window->close(); - } - } - } -} - -bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered - -//checks for if a keyboard key has been pressed -bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; } - -//control click on the SFML functions to see what they do internally -bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } - -bool EventService::isGameWindowOpen() { return game_window != nullptr; } - -bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } \ No newline at end of file diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp deleted file mode 100644 index e0fe519e8..000000000 --- a/Space-Invaders/Source/GameService.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "../Header/GameService.h" -#include "../Header/GraphicService.h" - -// Constructor: Initializes pointers to null. -GameService::GameService() { - service_locator = nullptr; // Set service locator to null - game_window = nullptr; // Set game window to null -} - -// Destructor: Calls the destroy function to clean up resources. -GameService::~GameService() { - destroy(); // Clean up and release resources -} - -// Prepares the game service for use by obtaining the service locator instance and initializing services. -void GameService::ignite() { - service_locator = ServiceLocator::getInstance(); // Get ServiceLocator - initialize(); // Initialize services. -} - -//initialize service locator and other variables -void GameService::initialize() -{ - service_locator->initialize(); - initializeVariables(); -} - -void GameService::initializeVariables() -{ - game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) -} - -void GameService::destroy() -{ - // don't need to do anything here for now. -} - -// Updates the game logic by delegating to the service locator's update method. -void GameService::update() { - - service_locator->getEventService()->processEvents(); - - service_locator->update(); // Call update on the service locator which then updates all its managed services -} - -// Clears the window then display it. -void GameService::render() { - // Clears the game window with the background color provided by the graphic service - game_window->clear(service_locator->getGraphicService()->getWindowColor()); - service_locator->render(); // Render the current frame using the service locator - game_window->display(); // Display the rendered frame on the game window -} - -// Checks if the game is still running by querying the graphic service's window open status. -bool GameService::isRunning() { - // Returns true if the game window is open, indicating the game is still running - return service_locator->getGraphicService()->isGameWindowOpen(); -} - diff --git a/Space-Invaders/Source/GraphicService.cpp b/Space-Invaders/Source/GraphicService.cpp deleted file mode 100644 index 8f3aae085..000000000 --- a/Space-Invaders/Source/GraphicService.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "../Header/GraphicService.h" - -// Constructor: Initializes game window and video mode pointers to null. -GraphicService::GraphicService() { - game_window = nullptr; // Initializes game window pointer to null - video_mode = nullptr; // Initializes video mode pointer to null -} - -// Destructor: Cleans up resources by calling onDestroy. -GraphicService::~GraphicService() { - onDestroy(); // Calls onDestroy method to clean up resources -} - -// Initializes the graphic service by creating a new game window. -void GraphicService::initialize() { - game_window = createGameWindow(); // Assigns a new game window to the game_window pointer -} - -// Creates a new SFML RenderWindow object with specified video mode and title. -sf::RenderWindow* GraphicService::createGameWindow() { - setVideoMode(); // Sets up the video mode for the window - return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object -} - -// Sets up the video mode for the game window using specified dimensions and system's color depth. -void GraphicService::setVideoMode() { - video_mode = new sf::VideoMode(game_window_width, game_window_height, sf::VideoMode::getDesktopMode().bitsPerPixel); // Allocates and sets the video mode -} - -// Cleans up allocated memory for video mode and game window to avoid memory leaks. -void GraphicService::onDestroy() { - delete(video_mode); // Deletes the video mode object - delete(game_window); // Deletes the game window object -} - -// Placeholder function for game update logic. -void GraphicService::update() { } - -// Placeholder function for game rendering logic. -void GraphicService::render() { } - -// Checks if the game window is currently open. -bool GraphicService::isGameWindowOpen() { - return game_window->isOpen(); // Returns the open status of the game window -} - -// Returns a pointer to the game window object. -sf::RenderWindow* GraphicService::getGameWindow() { - return game_window; -} - -// Returns the configured window background color. -sf::Color GraphicService::getWindowColor() { - return window_color; -} \ No newline at end of file diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp deleted file mode 100644 index af461c730..000000000 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "../Header/ServiceLocator.h" - -ServiceLocator::ServiceLocator() -{ - graphic_service = nullptr; - event_service = nullptr; - createServices(); -} -ServiceLocator::~ServiceLocator() -{ - clearAllServices(); -} - -void ServiceLocator::createServices() -{ - graphic_service = new GraphicService(); - event_service = new EventService(); -} - -void ServiceLocator::clearAllServices() -{ - delete(graphic_service); - delete(event_service); -} - -ServiceLocator* ServiceLocator::getInstance() -{ - static ServiceLocator instance; - return &instance; -} - -void ServiceLocator::initialize() -{ - graphic_service->initialize(); - event_service->initialize(); -} - -void ServiceLocator::update() -{ - graphic_service->update(); - event_service->update(); -} - -void ServiceLocator::render() -{ - graphic_service->render(); - //no event service because nothing to render -} - -GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } -EventService* ServiceLocator::getEventService() { return event_service; } From 8df483d9826b1438e05817de5ef6aef66245fe52 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:14:16 +0530 Subject: [PATCH 20/28] Delete Space-Invaders/main.cpp --- Space-Invaders/main.cpp | 106 ---------------------------------------- 1 file changed, 106 deletions(-) delete mode 100644 Space-Invaders/main.cpp diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp deleted file mode 100644 index 900795c1d..000000000 --- a/Space-Invaders/main.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include -#include -#include "Header/GameService.h" - - -/*class Player -{ -private: - - // Private Properties - int health = 3; - sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); - int movement_speed = 1; - int player_score = 0; - -public: - - // Public Properties - sf::Texture player_texture; - sf::Sprite player_sprite; - - //Public Functions, Getter & Setter methods - void move(float offsetX) { - position.x += offsetX; - } - - int getMoveSpeed() { - return movement_speed; - } - - int getScore() { - return player_score; - }; - - void setScore(int newScore) { - player_score = newScore; - }; - - sf::Vector2f getPosition() { - return position; - } - - void setPosition(sf::Vector2f newPosition) { - position = newPosition; - } - - //New methods to be added - void takeDamage() {}; - void move() {}; - void shootBullets() {}; -};*/ - -int main() -{ - /* - // Define the video mode (dimensions) - sf::VideoMode videoMode = sf::VideoMode(800, 600); - - // Create a window object with specific dimensions and a title - sf::RenderWindow window(videoMode, "My SFML Window"); - - //Player object - Player player; - - //Load Textures and sprite - player.player_texture.loadFromFile("assets/textures/player_ship.png"); - - player.player_sprite.setTexture(player.player_texture); - - while (window.isOpen()) { - sf::Event event; - while (window.pollEvent(event)) { - // Check for window closure - if (event.type == sf::Event::Closed) - window.close(); - } - - // Handle keyboard input - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - player.move(-1.0f* player.getMoveSpeed()); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - player.move(1.0f * player.getMoveSpeed()); - } - - // Clear the window--- - window.clear(sf::Color::Black); - - // Set and draw player - player.player_sprite.setPosition(player.getPosition()); - - window.draw(player.player_sprite); - - // Display whatever you draw - window.display(); - }*/ - GameService* game_service = new GameService(); - - game_service->ignite(); - - while (game_service->isRunning()) - { - game_service->update(); - game_service->render(); - } -} From f01bc02802256f090ce092c68b287ac91f84192c Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:14:55 +0530 Subject: [PATCH 21/28] Delete Space-Invaders/Space-Invaders.sln --- Space-Invaders/Space-Invaders.sln | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 Space-Invaders/Space-Invaders.sln diff --git a/Space-Invaders/Space-Invaders.sln b/Space-Invaders/Space-Invaders.sln deleted file mode 100644 index 21ec086fc..000000000 --- a/Space-Invaders/Space-Invaders.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.7.34024.191 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Space-Invaders", "Space-Invaders.vcxproj", "{AB3664CD-9870-4359-8811-B481DB3B55A0}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x64.ActiveCfg = Debug|x64 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x64.Build.0 = Debug|x64 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x86.ActiveCfg = Debug|Win32 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x86.Build.0 = Debug|Win32 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x64.ActiveCfg = Release|x64 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x64.Build.0 = Release|x64 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x86.ActiveCfg = Release|Win32 - {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {F8206701-92F6-4A4D-B0E5-F40E32131C64} - EndGlobalSection -EndGlobal From 5844287564978c1a5be646e8a9dd43e40be5f1ab Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:16:54 +0530 Subject: [PATCH 22/28] New Feature 3 Arch branch --- Space-Invaders/Header/Event/EventService.h | 30 ++ Space-Invaders/Header/Global/ServiceLocator.h | 41 +++ .../Header/Graphic/GraphicService.h | 38 +++ Space-Invaders/Header/Main/GameService.h | 25 ++ .../Header/Player/PlayerController.h | 28 ++ Space-Invaders/Header/Player/PlayerModel.h | 43 +++ Space-Invaders/Header/Player/PlayerService.h | 16 + Space-Invaders/Header/Player/PlayerView.h | 31 ++ Space-Invaders/Header/Time/TimeService.h | 28 ++ Space-Invaders/Source/Event/EventService.cpp | 44 +++ .../Source/Global/ServiceLocator.cpp | 65 ++++ .../Source/Graphic/GraphicService.cpp | 56 ++++ Space-Invaders/Source/Main/GameService.cpp | 59 ++++ .../Source/Player/PlayerController.cpp | 74 +++++ Space-Invaders/Source/Player/PlayerModel.cpp | 45 +++ .../Source/Player/PlayerService.cpp | 27 ++ Space-Invaders/Source/Player/PlayerView.cpp | 50 +++ Space-Invaders/Source/Time/TimeService.cpp | 39 +++ Space-Invaders/Space-Invaders.sln | 31 ++ Space-Invaders/Space-Invaders.vcxproj | 298 ++++++++++-------- Space-Invaders/Space-Invaders.vcxproj.filters | 98 ++++-- Space-Invaders/Space-Invaders.vcxproj.user | 10 +- Space-Invaders/main.cpp | 106 +++++++ 23 files changed, 1117 insertions(+), 165 deletions(-) create mode 100644 Space-Invaders/Header/Event/EventService.h create mode 100644 Space-Invaders/Header/Global/ServiceLocator.h create mode 100644 Space-Invaders/Header/Graphic/GraphicService.h create mode 100644 Space-Invaders/Header/Main/GameService.h create mode 100644 Space-Invaders/Header/Player/PlayerController.h create mode 100644 Space-Invaders/Header/Player/PlayerModel.h create mode 100644 Space-Invaders/Header/Player/PlayerService.h create mode 100644 Space-Invaders/Header/Player/PlayerView.h create mode 100644 Space-Invaders/Header/Time/TimeService.h create mode 100644 Space-Invaders/Source/Event/EventService.cpp create mode 100644 Space-Invaders/Source/Global/ServiceLocator.cpp create mode 100644 Space-Invaders/Source/Graphic/GraphicService.cpp create mode 100644 Space-Invaders/Source/Main/GameService.cpp create mode 100644 Space-Invaders/Source/Player/PlayerController.cpp create mode 100644 Space-Invaders/Source/Player/PlayerModel.cpp create mode 100644 Space-Invaders/Source/Player/PlayerService.cpp create mode 100644 Space-Invaders/Source/Player/PlayerView.cpp create mode 100644 Space-Invaders/Source/Time/TimeService.cpp create mode 100644 Space-Invaders/Space-Invaders.sln create mode 100644 Space-Invaders/main.cpp diff --git a/Space-Invaders/Header/Event/EventService.h b/Space-Invaders/Header/Event/EventService.h new file mode 100644 index 000000000..ce9882ddc --- /dev/null +++ b/Space-Invaders/Header/Event/EventService.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +class EventService +{ +private: + sf::Event game_event; //event var + sf::RenderWindow* game_window; //ptr to our game window + + bool isGameWindowOpen(); + bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. + bool hasQuitGame(); //for our new 'ESC' condition + + + +public: + EventService(); + ~EventService(); + + void initialize(); + void update(); + void processEvents(); // while window is open we will check for events + bool pressedEscapeKey(); + bool isKeyboardEvent(); + bool pressedLeftKey(); // getting inputs for the player + bool pressedRightKey(); + +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h new file mode 100644 index 000000000..332e1549f --- /dev/null +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -0,0 +1,41 @@ +#pragma once +#include "../../Header/Graphic/GraphicService.h" +#include "../../Header/Event/EventService.h" +#include "../../Header/Player/PlayerService.h" +#include "../../Header/Time/TimeService.h" + +// ServiceLocator Class Summary: This class manages access to various services in the application. +// include relevant headers files + +class ServiceLocator +{ +private: + + // Private Attributes: + GraphicService* graphic_service; + EventService* event_service; + PlayerService* player_service; + TimeService* time_service; + + // Public Methods + ServiceLocator(); + ~ServiceLocator(); + + // Private Methods: + void createServices(); + void clearAllServices(); + +public: + + // Public Methods: + static ServiceLocator* getInstance(); + void initialize(); // Initializes the ServiceLocator. + void update(); // Updates all services. + void render(); // Renders using the services. + + // Methods to Get Specific Services: + EventService* getEventService(); // Retrieve the EventService instance + GraphicService* getGraphicService(); // Retrieve the GraphicService instance + PlayerService* getPlayerService(); // Retrieve the PlayerService instance + TimeService* getTimeService(); // Retrieve the TimeService instance +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Graphic/GraphicService.h b/Space-Invaders/Header/Graphic/GraphicService.h new file mode 100644 index 000000000..83674fde3 --- /dev/null +++ b/Space-Invaders/Header/Graphic/GraphicService.h @@ -0,0 +1,38 @@ +#pragma once +#include + +class GraphicService +{ +private: + + const std::string game_window_title = "Alien Invader"; + + const int game_window_width = 800; + const int game_window_height = 600; + + const int frame_rate = 60; + + const sf::Color window_color = sf::Color::Black; + + sf::VideoMode* video_mode; // ptr to video mode + sf::RenderWindow* game_window; // ptr to a RenderWindow + + void setVideoMode(); // Method for setting our video mode + void onDestroy(); // method to run when window is deleted + +public: + GraphicService(); + ~GraphicService(); //cleanup + + //method to create the game window. returns a pointer to an instance of the game window + sf::RenderWindow* createGameWindow(); + + + void initialize(); //lifecycle functions + void update(); //.. + void render(); //.. + bool isGameWindowOpen(); //check if the window is open + + sf::RenderWindow* getGameWindow(); //getter for the game window instance + sf::Color getWindowColor();//get the color +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Main/GameService.h b/Space-Invaders/Header/Main/GameService.h new file mode 100644 index 000000000..53f95bee5 --- /dev/null +++ b/Space-Invaders/Header/Main/GameService.h @@ -0,0 +1,25 @@ +#pragma once +#include +#include "../../Header/Global/ServiceLocator.h" + +class GameService +{ +private: + + ServiceLocator* service_locator; + sf::RenderWindow* game_window; + + + void initialize(); // Handles game initialization. + void initializeVariables();// Handles game initialization. + void destroy(); // Handles cleanup tasks. + +public: + GameService(); // Constructor for initializing the GameService object. + ~GameService(); // Destructor for cleaning up resources upon object deletion. + + void ignite(); // Initiates the game. + void update(); // Updates the game logic and game state. + void render(); // Renders each frame of the game. + bool isRunning(); // Checks if the game is currently running. +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerController.h b/Space-Invaders/Header/Player/PlayerController.h new file mode 100644 index 000000000..36fda230a --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerController.h @@ -0,0 +1,28 @@ +#pragma once +#include + +enum class PlayerState; +class PlayerView; +class PlayerModel; + +class PlayerController +{ +private: + + PlayerView* player_view; + PlayerModel* player_model; + + void processPlayerInput(); + void moveLeft(); + void moveRight(); + +public: + PlayerController(); + ~PlayerController(); + + void initialize(); + void update(); + void render(); + + sf::Vector2f getPlayerPosition(); +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerModel.h b/Space-Invaders/Header/Player/PlayerModel.h new file mode 100644 index 000000000..13d61e0ea --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerModel.h @@ -0,0 +1,43 @@ +#pragma once +#include + +enum class PlayerState //Our Enum +{ + ALIVE, + DEAD, + // we will add more states later +}; + +class PlayerModel +{ +private: + const sf::Vector2f initial_player_position = sf::Vector2f(400.f, 400.f); + + sf::Vector2f player_position; + PlayerState player_state; //Declaration + int player_score; + +public: + const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); + const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); + + const float player_movement_speed = 300.0f; + + PlayerModel(); + ~PlayerModel(); + + void initialize(); + void reset(); + + sf::Vector2f getPlayerPosition(); + void setPlayerPosition(sf::Vector2f position); + + int getPlayerScore(); + void setPlayerScore(int score); + + //new getter and setter + PlayerState getPlayerState(); + void setPlayerState(PlayerState state); + + +}; diff --git a/Space-Invaders/Header/Player/PlayerService.h b/Space-Invaders/Header/Player/PlayerService.h new file mode 100644 index 000000000..2f46d4894 --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerService.h @@ -0,0 +1,16 @@ +#pragma once +#include "../../Header/Player/PlayerController.h" + +class PlayerService +{ +private: + PlayerController* player_controller; + +public: + PlayerService(); + ~PlayerService(); + + void initialize(); + void update(); + void render(); +}; diff --git a/Space-Invaders/Header/Player/PlayerView.h b/Space-Invaders/Header/Player/PlayerView.h new file mode 100644 index 000000000..8a1aa85e9 --- /dev/null +++ b/Space-Invaders/Header/Player/PlayerView.h @@ -0,0 +1,31 @@ +#pragma once +#include +#include "../../Header/Player/PlayerController.h" + +class PlayerView +{ +private: + + const sf::String player_texture_path = "assets/textures/player_ship.png"; + const float player_sprite_width = 60.f; + const float player_sprite_height = 60.f; + + sf::RenderWindow* game_window; + + sf::Texture player_texture; + sf::Sprite player_sprite; + + void initializePlayerSprite(); + void scalePlayerSprite(); + + PlayerController* player_controller; // ptr to player controller + +public: + PlayerView(); + ~PlayerView(); + + void initialize(); + void update(); + void render(); + void initialize(PlayerController* controller); +}; \ No newline at end of file diff --git a/Space-Invaders/Header/Time/TimeService.h b/Space-Invaders/Header/Time/TimeService.h new file mode 100644 index 000000000..24ddec14a --- /dev/null +++ b/Space-Invaders/Header/Time/TimeService.h @@ -0,0 +1,28 @@ +#pragma once +#include + + // The TimeService class helps keep track of time in game and calculate delta time. + // Utilizes the library to calculate delta time. +class TimeService +{ +private: + + // A point in time which indicates the starting time of previous frame. + std::chrono::time_point previous_time; + + float delta_time; //to store the detla time + + void updateDeltaTime(); // method to update time + float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time + void updatePreviousTime(); // finally update the current time to be previous time + +public: + + //lifecycle methods + void initialize(); + void update(); + + //getter + float getDeltaTime(); +}; + diff --git a/Space-Invaders/Source/Event/EventService.cpp b/Space-Invaders/Source/Event/EventService.cpp new file mode 100644 index 000000000..9be3a667d --- /dev/null +++ b/Space-Invaders/Source/Event/EventService.cpp @@ -0,0 +1,44 @@ +#include "../../Header/Event/EventService.h" +#include "../../Header/Main/GameService.h" +#include "../../Header/Graphic/GraphicService.h" + +EventService::EventService() { game_window = nullptr; } + +EventService::~EventService() = default; //calls the default destructor + +void EventService::initialize() +{ + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); +} + +void EventService::update() +{ + //for later +} + +void EventService::processEvents() +{ + if (isGameWindowOpen()) { + while (game_window->pollEvent(game_event)) { + // Check for window closure + if (gameWindowWasClosed() || hasQuitGame()) + { + game_window->close(); + } + } + } +} + +bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered + +//checks for if a keyboard key has been pressed +bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; } + +//control click on the SFML functions to see what they do internally +bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } +bool EventService::isGameWindowOpen() { return game_window != nullptr; } +bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } + +// Player inputs +bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } +bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } \ No newline at end of file diff --git a/Space-Invaders/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp new file mode 100644 index 000000000..4c239d74b --- /dev/null +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -0,0 +1,65 @@ +#include "../../Header/Global/ServiceLocator.h" + +ServiceLocator::ServiceLocator() +{ + graphic_service = nullptr; + event_service = nullptr; + player_service = nullptr; + time_service = nullptr; + createServices(); +} +ServiceLocator::~ServiceLocator() +{ + clearAllServices(); +} + +void ServiceLocator::createServices() +{ + graphic_service = new GraphicService(); + event_service = new EventService(); + player_service = new PlayerService(); + time_service = new TimeService(); +} + +void ServiceLocator::clearAllServices() +{ + delete(graphic_service); + delete(event_service); + delete(player_service); + delete(time_service); +} + +ServiceLocator* ServiceLocator::getInstance() +{ + static ServiceLocator instance; + return &instance; +} + +void ServiceLocator::initialize() +{ + graphic_service->initialize(); + event_service->initialize(); + player_service->initialize(); + time_service->initialize(); +} + +void ServiceLocator::update() +{ + graphic_service->update(); + event_service->update(); + player_service->update(); + time_service->update(); +} + +void ServiceLocator::render() +{ + graphic_service->render(); + player_service->render(); + //no event service because nothing to render + //no time service +} + +GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } +EventService* ServiceLocator::getEventService() { return event_service; } +PlayerService* ServiceLocator::getPlayerService() { return player_service; } +TimeService* ServiceLocator::getTimeService() { return time_service; } diff --git a/Space-Invaders/Source/Graphic/GraphicService.cpp b/Space-Invaders/Source/Graphic/GraphicService.cpp new file mode 100644 index 000000000..4c8bb04ab --- /dev/null +++ b/Space-Invaders/Source/Graphic/GraphicService.cpp @@ -0,0 +1,56 @@ +#include "../../Header/Graphic/GraphicService.h" + +// Constructor: Initializes game window and video mode pointers to null. +GraphicService::GraphicService() { + game_window = nullptr; // Initializes game window pointer to null + video_mode = nullptr; // Initializes video mode pointer to null +} + +// Destructor: Cleans up resources by calling onDestroy. +GraphicService::~GraphicService() { + onDestroy(); // Calls onDestroy method to clean up resources +} + +// Initializes the graphic service by creating a new game window. +void GraphicService::initialize() { + game_window = createGameWindow(); // Assigns a new game window + game_window->setFramerateLimit(frame_rate); // setting a frame rate limit +} + +// Creates a new SFML RenderWindow object with specified video mode and title. +sf::RenderWindow* GraphicService::createGameWindow() { + setVideoMode(); // Sets up the video mode for the window + return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object +} + +// Sets up the video mode for the game window using specified dimensions and system's color depth. +void GraphicService::setVideoMode() { + video_mode = new sf::VideoMode(game_window_width, game_window_height, sf::VideoMode::getDesktopMode().bitsPerPixel); // Allocates and sets the video mode +} + +// Cleans up allocated memory for video mode and game window to avoid memory leaks. +void GraphicService::onDestroy() { + delete(video_mode); // Deletes the video mode object + delete(game_window); // Deletes the game window object +} + +// Placeholder function for game update logic. +void GraphicService::update() { } + +// Placeholder function for game rendering logic. +void GraphicService::render() { } + +// Checks if the game window is currently open. +bool GraphicService::isGameWindowOpen() { + return game_window->isOpen(); // Returns the open status of the game window +} + +// Returns a pointer to the game window object. +sf::RenderWindow* GraphicService::getGameWindow() { + return game_window; +} + +// Returns the configured window background color. +sf::Color GraphicService::getWindowColor() { + return window_color; +} \ No newline at end of file diff --git a/Space-Invaders/Source/Main/GameService.cpp b/Space-Invaders/Source/Main/GameService.cpp new file mode 100644 index 000000000..12918afe4 --- /dev/null +++ b/Space-Invaders/Source/Main/GameService.cpp @@ -0,0 +1,59 @@ +#include "../../Header/Main/GameService.h" +#include "../../Header/Graphic/GraphicService.h" + +// Constructor: Initializes pointers to null. +GameService::GameService() { + service_locator = nullptr; // Set service locator to null + game_window = nullptr; // Set game window to null +} + +// Destructor: Calls the destroy function to clean up resources. +GameService::~GameService() { + destroy(); // Clean up and release resources +} + +// Prepares the game service for use by obtaining the service locator instance and initializing services. +void GameService::ignite() { + service_locator = ServiceLocator::getInstance(); // Get ServiceLocator + initialize(); // Initialize services. +} + +//initialize service locator and other variables +void GameService::initialize() +{ + service_locator->initialize(); + initializeVariables(); +} + +void GameService::initializeVariables() +{ + game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) +} + +void GameService::destroy() +{ + // don't need to do anything here for now. +} + +// Updates the game logic by delegating to the service locator's update method. +void GameService::update() { + + service_locator->getEventService()->processEvents(); + + service_locator->update(); // Call update on the service locator which then updates all its managed services +} + +// Clears the window then display it. +void GameService::render() { + // Clears the game window with the background color provided by the graphic service + game_window->clear(service_locator->getGraphicService()->getWindowColor()); + service_locator->render(); // Render the current frame using the service locator + game_window->display(); // Display the rendered frame on the game window +} + +// Checks if the game is still running by querying the graphic service's window open status. +bool GameService::isRunning() { + // Returns true if the game window is open, indicating the game is still running + return service_locator->getGraphicService()->isGameWindowOpen(); +} + diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp new file mode 100644 index 000000000..14b9754c9 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerController.cpp @@ -0,0 +1,74 @@ +#include "../../Header/Player/PlayerController.h" +#include "../../Header/Player/PlayerModel.h" +#include "../../Header/Player/PlayerView.h" +#include "../../Header/Event/EventService.h" +#include "../../Header/Global/ServiceLocator.h" +#include + +PlayerController::PlayerController() +{ + player_view = new PlayerView(); + player_model = new PlayerModel(); +} + +PlayerController::~PlayerController() +{ + delete (player_view); + delete (player_model); +} +//the controller is responsible for calling the lifecycle methods for the other two +void PlayerController::initialize() +{ + player_model->initialize(); + + //This will give an error right now since we haven't included the controller in the view. + player_view->initialize(this); // 'this' refers to the class we are currently inside +} + +void PlayerController::update() +{ + processPlayerInput(); + player_view->update(); // we update() the view +} + +void PlayerController::render() +{ + player_view->render(); // render the view +} + +sf::Vector2f PlayerController::getPlayerPosition() +{ + return player_model->getPlayerPosition(); +} + +void PlayerController::processPlayerInput() +{ + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) + { + moveLeft(); + } + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) + { + moveRight(); + } +} + +void PlayerController::moveLeft() +{ + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x -= player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + currentPosition.x = std::max(currentPosition.x, player_model->left_most_position.x); + player_model->setPlayerPosition(currentPosition); +} + +void PlayerController::moveRight() +{ + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x += player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + + currentPosition.x = std::min(currentPosition.x, player_model->right_most_position.x); + player_model->setPlayerPosition(currentPosition); +} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerModel.cpp b/Space-Invaders/Source/Player/PlayerModel.cpp new file mode 100644 index 000000000..0d51db820 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerModel.cpp @@ -0,0 +1,45 @@ +#include "../../Header/Player/PlayerModel.h" + +PlayerModel::PlayerModel() { } + +PlayerModel::~PlayerModel() { } + +void PlayerModel::initialize() { reset(); } // remember to call reset() + +void PlayerModel::reset() +{ + player_state = PlayerState::ALIVE; // set state to alive + player_position = initial_player_position; + player_score = 0; +} + +sf::Vector2f PlayerModel::getPlayerPosition() +{ + return player_position; +} + +void PlayerModel::setPlayerPosition(sf::Vector2f position) +{ + player_position = position; +} + +int PlayerModel::getPlayerScore() +{ + return player_score; +} + +void PlayerModel::setPlayerScore(int score) +{ + player_score = score; +} + +//.. +PlayerState PlayerModel::getPlayerState() +{ + return player_state; +} + +void PlayerModel::setPlayerState(PlayerState state) +{ + player_state = state; +} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerService.cpp b/Space-Invaders/Source/Player/PlayerService.cpp new file mode 100644 index 000000000..af4e3d1e8 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerService.cpp @@ -0,0 +1,27 @@ +#include "../../Header/Player/PlayerService.h" +#include "../../Header/Player/PlayerController.h" + +PlayerService::PlayerService() +{ + player_controller = new PlayerController(); +} + +PlayerService::~PlayerService() +{ + delete (player_controller); +} + +void PlayerService::initialize() +{ + player_controller->initialize(); +} + +void PlayerService::update() +{ + player_controller->update(); +} + +void PlayerService::render() +{ + player_controller->render(); +} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp new file mode 100644 index 000000000..b3abf9070 --- /dev/null +++ b/Space-Invaders/Source/Player/PlayerView.cpp @@ -0,0 +1,50 @@ +#include "../../Header/Player/PlayerView.h" +#include "../../Header/Global/ServiceLocator.h" + +PlayerView::PlayerView() { } + +PlayerView::~PlayerView() { } + +void PlayerView::initialize() +{ + + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); +} + +void PlayerView::initialize(PlayerController* controller) +{ + player_controller = controller; //to later use it for setting position + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); +} + +void PlayerView::initializePlayerSprite() +{ + if (player_texture.loadFromFile(player_texture_path)) + { + player_sprite.setTexture(player_texture); + scalePlayerSprite(); + } +} + +void PlayerView::scalePlayerSprite() +{ + // setScale is an inbuilt method of the sprite class that takes two floats to scale the sprite. it scales the sprite to our desired height + player_sprite.setScale( + //Here we find the factor to scale our sprites with. Ignore the static_cast for now, we will discuss it later. + static_cast(player_sprite_width) / player_sprite.getTexture()->getSize().x, + static_cast(player_sprite_height) / player_sprite.getTexture()->getSize().y + ); +} + +void PlayerView::update() +{ + //set the updated position before we render + player_sprite.setPosition(player_controller->getPlayerPosition()); +} + +void PlayerView::render() +{ + game_window->draw(player_sprite); +} \ No newline at end of file diff --git a/Space-Invaders/Source/Time/TimeService.cpp b/Space-Invaders/Source/Time/TimeService.cpp new file mode 100644 index 000000000..0bfc0d679 --- /dev/null +++ b/Space-Invaders/Source/Time/TimeService.cpp @@ -0,0 +1,39 @@ +#include "../../Header/Time/TimeService.h" + +void TimeService::initialize() +{ + previous_time = std::chrono::steady_clock::now(); + delta_time = 0; +} + +void TimeService::update() +{ + updateDeltaTime(); +} + +float TimeService::getDeltaTime() +{ + return delta_time; +} + +void TimeService::updateDeltaTime() +{ + delta_time = calculateDeltaTime(); + updatePreviousTime(); +} + +float TimeService::calculateDeltaTime() +{ + // Calculate time difference in microseconds between the current and previous frame. + int delta = std::chrono::duration_cast( + std::chrono::steady_clock::now() - previous_time).count(); + + // The cast is used to convert delta time from microseconds into seconds. + return static_cast(delta) / static_cast(1000000); +} + +// Update previous_time to the current time +void TimeService::updatePreviousTime() +{ + previous_time = std::chrono::steady_clock::now(); +} diff --git a/Space-Invaders/Space-Invaders.sln b/Space-Invaders/Space-Invaders.sln new file mode 100644 index 000000000..21ec086fc --- /dev/null +++ b/Space-Invaders/Space-Invaders.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34024.191 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Space-Invaders", "Space-Invaders.vcxproj", "{AB3664CD-9870-4359-8811-B481DB3B55A0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x64.ActiveCfg = Debug|x64 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x64.Build.0 = Debug|x64 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x86.ActiveCfg = Debug|Win32 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Debug|x86.Build.0 = Debug|Win32 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x64.ActiveCfg = Release|x64 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x64.Build.0 = Release|x64 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x86.ActiveCfg = Release|Win32 + {AB3664CD-9870-4359-8811-B481DB3B55A0}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F8206701-92F6-4A4D-B0E5-F40E32131C64} + EndGlobalSection +EndGlobal diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 6f7fa388d..3dd5235bd 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -1,140 +1,160 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 17.0 - Win32Proj - {ab3664cd-9870-4359-8811-b481db3b55a0} - SpaceInvaders - 10.0 - - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - - - - - - - - - - - - - - - - - - - - Level3 - true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Level3 - true - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - Level3 - true - _DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - $(SolutionDir)sfml\include;%(AdditionalIncludeDirectories) - - - Console - true - $(SolutionDir)sfml\lib;%(AdditionalLibraryDirectories) - sfml-graphics-d.lib;sfml-window-d.lib;sfml-network-d.lib;sfml-audio-d.lib;sfml-system-d.lib;%(AdditionalDependencies) - - - - - Level3 - true - true - true - NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - $(SolutionDir)sfml\include;%(AdditionalIncludeDirectories) - - - Console - true - true - true - $(SolutionDir)sfml\lib;%(AdditionalLibraryDirectories) - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {ab3664cd-9870-4359-8811-b481db3b55a0} + SpaceInvaders + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)sfml\include;%(AdditionalIncludeDirectories) + + + Console + true + $(SolutionDir)sfml\lib;%(AdditionalLibraryDirectories) + sfml-graphics-d.lib;sfml-window-d.lib;sfml-network-d.lib;sfml-audio-d.lib;sfml-system-d.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)sfml\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + $(SolutionDir)sfml\lib;%(AdditionalLibraryDirectories) + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index ce0c35ccf..8ae369c39 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -1,22 +1,78 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Source Files - - + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj.user b/Space-Invaders/Space-Invaders.vcxproj.user index 966b4ffb6..429333de9 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.user +++ b/Space-Invaders/Space-Invaders.vcxproj.user @@ -1,6 +1,6 @@ - - - - true - + + + + true + \ No newline at end of file diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp new file mode 100644 index 000000000..434cdaeb5 --- /dev/null +++ b/Space-Invaders/main.cpp @@ -0,0 +1,106 @@ +#include +#include +#include "Header/Main/GameService.h" + + +/*class Player +{ +private: + + // Private Properties + int health = 3; + sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); + int movement_speed = 1; + int player_score = 0; + +public: + + // Public Properties + sf::Texture player_texture; + sf::Sprite player_sprite; + + //Public Functions, Getter & Setter methods + void move(float offsetX) { + position.x += offsetX; + } + + int getMoveSpeed() { + return movement_speed; + } + + int getScore() { + return player_score; + }; + + void setScore(int newScore) { + player_score = newScore; + }; + + sf::Vector2f getPosition() { + return position; + } + + void setPosition(sf::Vector2f newPosition) { + position = newPosition; + } + + //New methods to be added + void takeDamage() {}; + void move() {}; + void shootBullets() {}; +};*/ + +int main() +{ + /* + // Define the video mode (dimensions) + sf::VideoMode videoMode = sf::VideoMode(800, 600); + + // Create a window object with specific dimensions and a title + sf::RenderWindow window(videoMode, "My SFML Window"); + + //Player object + Player player; + + //Load Textures and sprite + player.player_texture.loadFromFile("assets/textures/player_ship.png"); + + player.player_sprite.setTexture(player.player_texture); + + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + // Check for window closure + if (event.type == sf::Event::Closed) + window.close(); + } + + // Handle keyboard input + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + player.move(-1.0f* player.getMoveSpeed()); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + player.move(1.0f * player.getMoveSpeed()); + } + + // Clear the window--- + window.clear(sf::Color::Black); + + // Set and draw player + player.player_sprite.setPosition(player.getPosition()); + + window.draw(player.player_sprite); + + // Display whatever you draw + window.display(); + }*/ + GameService* game_service = new GameService(); + + game_service->ignite(); + + while (game_service->isRunning()) + { + game_service->update(); + game_service->render(); + } +} From 58622f519b5551a0979c3eda676a741893668248 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:30:45 +0530 Subject: [PATCH 23/28] Delete Space-Invaders/Header directory --- Space-Invaders/Header/EventService.h | 30 ------------- Space-Invaders/Header/GameService.h | 25 ----------- Space-Invaders/Header/GraphicService.h | 38 ---------------- .../Header/Player/PlayerController.h | 25 ----------- Space-Invaders/Header/Player/PlayerModel.h | 43 ------------------- Space-Invaders/Header/Player/PlayerView.h | 31 ------------- Space-Invaders/Header/PlayerService.h | 16 ------- Space-Invaders/Header/ServiceLocator.h | 41 ------------------ Space-Invaders/Header/TimeService.h | 28 ------------ 9 files changed, 277 deletions(-) delete mode 100644 Space-Invaders/Header/EventService.h delete mode 100644 Space-Invaders/Header/GameService.h delete mode 100644 Space-Invaders/Header/GraphicService.h delete mode 100644 Space-Invaders/Header/Player/PlayerController.h delete mode 100644 Space-Invaders/Header/Player/PlayerModel.h delete mode 100644 Space-Invaders/Header/Player/PlayerView.h delete mode 100644 Space-Invaders/Header/PlayerService.h delete mode 100644 Space-Invaders/Header/ServiceLocator.h delete mode 100644 Space-Invaders/Header/TimeService.h diff --git a/Space-Invaders/Header/EventService.h b/Space-Invaders/Header/EventService.h deleted file mode 100644 index ce9882ddc..000000000 --- a/Space-Invaders/Header/EventService.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include -#include - -class EventService -{ -private: - sf::Event game_event; //event var - sf::RenderWindow* game_window; //ptr to our game window - - bool isGameWindowOpen(); - bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. - bool hasQuitGame(); //for our new 'ESC' condition - - - -public: - EventService(); - ~EventService(); - - void initialize(); - void update(); - void processEvents(); // while window is open we will check for events - bool pressedEscapeKey(); - bool isKeyboardEvent(); - bool pressedLeftKey(); // getting inputs for the player - bool pressedRightKey(); - -}; \ No newline at end of file diff --git a/Space-Invaders/Header/GameService.h b/Space-Invaders/Header/GameService.h deleted file mode 100644 index 32230d67d..000000000 --- a/Space-Invaders/Header/GameService.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include -#include "../Header/ServiceLocator.h" - -class GameService -{ -private: - - ServiceLocator* service_locator; - sf::RenderWindow* game_window; - - - void initialize(); // Handles game initialization. - void initializeVariables();// Handles game initialization. - void destroy(); // Handles cleanup tasks. - -public: - GameService(); // Constructor for initializing the GameService object. - ~GameService(); // Destructor for cleaning up resources upon object deletion. - - void ignite(); // Initiates the game. - void update(); // Updates the game logic and game state. - void render(); // Renders each frame of the game. - bool isRunning(); // Checks if the game is currently running. -}; \ No newline at end of file diff --git a/Space-Invaders/Header/GraphicService.h b/Space-Invaders/Header/GraphicService.h deleted file mode 100644 index 96ed95751..000000000 --- a/Space-Invaders/Header/GraphicService.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include - -class GraphicService -{ -private: - - const std::string game_window_title = "Alien Invader"; - - const int game_window_width = 800; - const int game_window_height = 600; - - const int frame_rate = 60; - - const sf::Color window_color = sf::Color::Blue; - - sf::VideoMode* video_mode; // ptr to video mode - sf::RenderWindow* game_window; // ptr to a RenderWindow - - void setVideoMode(); // Method for setting our video mode - void onDestroy(); // method to run when window is deleted - -public: - GraphicService(); - ~GraphicService(); //cleanup - - //method to create the game window. returns a pointer to an instance of the game window - sf::RenderWindow* createGameWindow(); - - - void initialize(); //lifecycle functions - void update(); //.. - void render(); //.. - bool isGameWindowOpen(); //check if the window is open - - sf::RenderWindow* getGameWindow(); //getter for the game window instance - sf::Color getWindowColor();//get the color -}; \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerController.h b/Space-Invaders/Header/Player/PlayerController.h deleted file mode 100644 index 9ea24d95d..000000000 --- a/Space-Invaders/Header/Player/PlayerController.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include -#include "../Player/PlayerModel.h" -#include "../Player/PlayerView.h" - -class PlayerController -{ -private: - PlayerView* player_view; - PlayerModel* player_model; - - void processPlayerInput(); - void moveLeft(); - void moveRight(); - -public: - PlayerController(); - ~PlayerController(); - - void initialize(); - void update(); - void render(); - - sf::Vector2f getPlayerPosition(); -}; \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerModel.h b/Space-Invaders/Header/Player/PlayerModel.h deleted file mode 100644 index cf048eca5..000000000 --- a/Space-Invaders/Header/Player/PlayerModel.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once -#include - -enum class PlayerState //Our Enum -{ - ALIVE, - DEAD, - // we will add more states later -}; - -class PlayerModel -{ -private: - const sf::Vector2f initial_player_position = sf::Vector2f(500.f, 500.f); - - sf::Vector2f player_position; - PlayerState player_state; //Declaration - int player_score; - -public: - const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); - const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); - - const float player_movement_speed = 200.0f; - - PlayerModel(); - ~PlayerModel(); - - void initialize(); - void reset(); - - sf::Vector2f getPlayerPosition(); - void setPlayerPosition(sf::Vector2f position); - - int getPlayerScore(); - void setPlayerScore(int score); - - //new getter and setter - PlayerState getPlayerState(); - void setPlayerState(PlayerState state); - - -}; diff --git a/Space-Invaders/Header/Player/PlayerView.h b/Space-Invaders/Header/Player/PlayerView.h deleted file mode 100644 index 8a1aa85e9..000000000 --- a/Space-Invaders/Header/Player/PlayerView.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include -#include "../../Header/Player/PlayerController.h" - -class PlayerView -{ -private: - - const sf::String player_texture_path = "assets/textures/player_ship.png"; - const float player_sprite_width = 60.f; - const float player_sprite_height = 60.f; - - sf::RenderWindow* game_window; - - sf::Texture player_texture; - sf::Sprite player_sprite; - - void initializePlayerSprite(); - void scalePlayerSprite(); - - PlayerController* player_controller; // ptr to player controller - -public: - PlayerView(); - ~PlayerView(); - - void initialize(); - void update(); - void render(); - void initialize(PlayerController* controller); -}; \ No newline at end of file diff --git a/Space-Invaders/Header/PlayerService.h b/Space-Invaders/Header/PlayerService.h deleted file mode 100644 index 2f46d4894..000000000 --- a/Space-Invaders/Header/PlayerService.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "../../Header/Player/PlayerController.h" - -class PlayerService -{ -private: - PlayerController* player_controller; - -public: - PlayerService(); - ~PlayerService(); - - void initialize(); - void update(); - void render(); -}; diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h deleted file mode 100644 index df101d414..000000000 --- a/Space-Invaders/Header/ServiceLocator.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once -#include "../Header/GraphicService.h" -#include "../Header/EventService.h" -#include "../Header/PlayerService.h" -#include "../Header/TimeService.h" - -// ServiceLocator Class Summary: This class manages access to various services in the application. -// include relevant headers files - -class ServiceLocator -{ -private: - - // Private Attributes: - GraphicService* graphic_service; - EventService* event_service; - PlayerService* player_service; - TimeService* time_service; - - // Public Methods - ServiceLocator(); - ~ServiceLocator(); - - // Private Methods: - void createServices(); - void clearAllServices(); - -public: - - // Public Methods: - static ServiceLocator* getInstance(); - void initialize(); // Initializes the ServiceLocator. - void update(); // Updates all services. - void render(); // Renders using the services. - - // Methods to Get Specific Services: - EventService* getEventService(); // Retrieve the EventService instance - GraphicService* getGraphicService(); // Retrieve the GraphicService instance - PlayerService* getPlayerService(); // Retrieve the PlayerService instance - TimeService* getTimeService(); // Retrieve the TimeService instance -}; \ No newline at end of file diff --git a/Space-Invaders/Header/TimeService.h b/Space-Invaders/Header/TimeService.h deleted file mode 100644 index 24ddec14a..000000000 --- a/Space-Invaders/Header/TimeService.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include - - // The TimeService class helps keep track of time in game and calculate delta time. - // Utilizes the library to calculate delta time. -class TimeService -{ -private: - - // A point in time which indicates the starting time of previous frame. - std::chrono::time_point previous_time; - - float delta_time; //to store the detla time - - void updateDeltaTime(); // method to update time - float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time - void updatePreviousTime(); // finally update the current time to be previous time - -public: - - //lifecycle methods - void initialize(); - void update(); - - //getter - float getDeltaTime(); -}; - From 4f64943e274206d11bea0b7811998b1380e34743 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 14:30:57 +0530 Subject: [PATCH 24/28] Delete Space-Invaders/Source directory --- Space-Invaders/Source/EventService.cpp | 44 ------------ Space-Invaders/Source/GameService.cpp | 59 --------------- Space-Invaders/Source/GraphicService.cpp | 56 --------------- .../Source/Player/PlayerController.cpp | 72 ------------------- Space-Invaders/Source/Player/PlayerModel.cpp | 45 ------------ Space-Invaders/Source/Player/PlayerView.cpp | 50 ------------- Space-Invaders/Source/PlayerService.cpp | 27 ------- Space-Invaders/Source/ServiceLocator.cpp | 65 ----------------- Space-Invaders/Source/TimeService.cpp | 39 ---------- 9 files changed, 457 deletions(-) delete mode 100644 Space-Invaders/Source/EventService.cpp delete mode 100644 Space-Invaders/Source/GameService.cpp delete mode 100644 Space-Invaders/Source/GraphicService.cpp delete mode 100644 Space-Invaders/Source/Player/PlayerController.cpp delete mode 100644 Space-Invaders/Source/Player/PlayerModel.cpp delete mode 100644 Space-Invaders/Source/Player/PlayerView.cpp delete mode 100644 Space-Invaders/Source/PlayerService.cpp delete mode 100644 Space-Invaders/Source/ServiceLocator.cpp delete mode 100644 Space-Invaders/Source/TimeService.cpp diff --git a/Space-Invaders/Source/EventService.cpp b/Space-Invaders/Source/EventService.cpp deleted file mode 100644 index 6f2941e93..000000000 --- a/Space-Invaders/Source/EventService.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "../Header/EventService.h" -#include "../Header/GameService.h" -#include "../Header/GraphicService.h" - -EventService::EventService() { game_window = nullptr; } - -EventService::~EventService() = default; //calls the default destructor - -void EventService::initialize() -{ - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); -} - -void EventService::update() -{ - //for later -} - -void EventService::processEvents() -{ - if (isGameWindowOpen()) { - while (game_window->pollEvent(game_event)) { - // Check for window closure - if (gameWindowWasClosed() || hasQuitGame()) - { - game_window->close(); - } - } - } -} - -bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered - -//checks for if a keyboard key has been pressed -bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; } - -//control click on the SFML functions to see what they do internally -bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } -bool EventService::isGameWindowOpen() { return game_window != nullptr; } -bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } - -// Player inputs -bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } -bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } \ No newline at end of file diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp deleted file mode 100644 index e0fe519e8..000000000 --- a/Space-Invaders/Source/GameService.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "../Header/GameService.h" -#include "../Header/GraphicService.h" - -// Constructor: Initializes pointers to null. -GameService::GameService() { - service_locator = nullptr; // Set service locator to null - game_window = nullptr; // Set game window to null -} - -// Destructor: Calls the destroy function to clean up resources. -GameService::~GameService() { - destroy(); // Clean up and release resources -} - -// Prepares the game service for use by obtaining the service locator instance and initializing services. -void GameService::ignite() { - service_locator = ServiceLocator::getInstance(); // Get ServiceLocator - initialize(); // Initialize services. -} - -//initialize service locator and other variables -void GameService::initialize() -{ - service_locator->initialize(); - initializeVariables(); -} - -void GameService::initializeVariables() -{ - game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) -} - -void GameService::destroy() -{ - // don't need to do anything here for now. -} - -// Updates the game logic by delegating to the service locator's update method. -void GameService::update() { - - service_locator->getEventService()->processEvents(); - - service_locator->update(); // Call update on the service locator which then updates all its managed services -} - -// Clears the window then display it. -void GameService::render() { - // Clears the game window with the background color provided by the graphic service - game_window->clear(service_locator->getGraphicService()->getWindowColor()); - service_locator->render(); // Render the current frame using the service locator - game_window->display(); // Display the rendered frame on the game window -} - -// Checks if the game is still running by querying the graphic service's window open status. -bool GameService::isRunning() { - // Returns true if the game window is open, indicating the game is still running - return service_locator->getGraphicService()->isGameWindowOpen(); -} - diff --git a/Space-Invaders/Source/GraphicService.cpp b/Space-Invaders/Source/GraphicService.cpp deleted file mode 100644 index 2cfef211f..000000000 --- a/Space-Invaders/Source/GraphicService.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "../Header/GraphicService.h" - -// Constructor: Initializes game window and video mode pointers to null. -GraphicService::GraphicService() { - game_window = nullptr; // Initializes game window pointer to null - video_mode = nullptr; // Initializes video mode pointer to null -} - -// Destructor: Cleans up resources by calling onDestroy. -GraphicService::~GraphicService() { - onDestroy(); // Calls onDestroy method to clean up resources -} - -// Initializes the graphic service by creating a new game window. -void GraphicService::initialize() { - game_window = createGameWindow(); // Assigns a new game window - game_window->setFramerateLimit(frame_rate); // setting a frame rate limit -} - -// Creates a new SFML RenderWindow object with specified video mode and title. -sf::RenderWindow* GraphicService::createGameWindow() { - setVideoMode(); // Sets up the video mode for the window - return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object -} - -// Sets up the video mode for the game window using specified dimensions and system's color depth. -void GraphicService::setVideoMode() { - video_mode = new sf::VideoMode(game_window_width, game_window_height, sf::VideoMode::getDesktopMode().bitsPerPixel); // Allocates and sets the video mode -} - -// Cleans up allocated memory for video mode and game window to avoid memory leaks. -void GraphicService::onDestroy() { - delete(video_mode); // Deletes the video mode object - delete(game_window); // Deletes the game window object -} - -// Placeholder function for game update logic. -void GraphicService::update() { } - -// Placeholder function for game rendering logic. -void GraphicService::render() { } - -// Checks if the game window is currently open. -bool GraphicService::isGameWindowOpen() { - return game_window->isOpen(); // Returns the open status of the game window -} - -// Returns a pointer to the game window object. -sf::RenderWindow* GraphicService::getGameWindow() { - return game_window; -} - -// Returns the configured window background color. -sf::Color GraphicService::getWindowColor() { - return window_color; -} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp deleted file mode 100644 index 19181c698..000000000 --- a/Space-Invaders/Source/Player/PlayerController.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include "../../Header/Player/PlayerController.h" -#include "../../Header/EventService.h" -#include "../../Header/ServiceLocator.h" -#include - -PlayerController::PlayerController() -{ - player_view = new PlayerView(); - player_model = new PlayerModel(); -} - -PlayerController::~PlayerController() -{ - delete (player_view); - delete (player_model); -} -//the controller is responsible for calling the lifecycle methods for the other two -void PlayerController::initialize() -{ - player_model->initialize(); - - //This will give an error right now since we haven't included the controller in the view. - player_view->initialize(this); // 'this' refers to the class we are currently inside -} - -void PlayerController::update() -{ - processPlayerInput(); - player_view->update(); // we update() the view -} - -void PlayerController::render() -{ - player_view->render(); // render the view -} - -sf::Vector2f PlayerController::getPlayerPosition() -{ - return player_model->getPlayerPosition(); -} - -void PlayerController::processPlayerInput() -{ - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) - { - moveLeft(); - } - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) - { - moveRight(); - } -} - -void PlayerController::moveLeft() -{ - sf::Vector2f currentPosition = player_model->getPlayerPosition(); - currentPosition.x -= player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - - currentPosition.x = std::max(currentPosition.x, player_model->left_most_position.x); - player_model->setPlayerPosition(currentPosition); -} - -void PlayerController::moveRight() -{ - sf::Vector2f currentPosition = player_model->getPlayerPosition(); - currentPosition.x += player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - - currentPosition.x = std::min(currentPosition.x, player_model->right_most_position.x); - player_model->setPlayerPosition(currentPosition); -} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerModel.cpp b/Space-Invaders/Source/Player/PlayerModel.cpp deleted file mode 100644 index 0d51db820..000000000 --- a/Space-Invaders/Source/Player/PlayerModel.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "../../Header/Player/PlayerModel.h" - -PlayerModel::PlayerModel() { } - -PlayerModel::~PlayerModel() { } - -void PlayerModel::initialize() { reset(); } // remember to call reset() - -void PlayerModel::reset() -{ - player_state = PlayerState::ALIVE; // set state to alive - player_position = initial_player_position; - player_score = 0; -} - -sf::Vector2f PlayerModel::getPlayerPosition() -{ - return player_position; -} - -void PlayerModel::setPlayerPosition(sf::Vector2f position) -{ - player_position = position; -} - -int PlayerModel::getPlayerScore() -{ - return player_score; -} - -void PlayerModel::setPlayerScore(int score) -{ - player_score = score; -} - -//.. -PlayerState PlayerModel::getPlayerState() -{ - return player_state; -} - -void PlayerModel::setPlayerState(PlayerState state) -{ - player_state = state; -} \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp deleted file mode 100644 index 93762cd15..000000000 --- a/Space-Invaders/Source/Player/PlayerView.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "../../Header/Player/PlayerView.h" -#include "../../Header/ServiceLocator.h" - -PlayerView::PlayerView() { } - -PlayerView::~PlayerView() { } - -void PlayerView::initialize() -{ - - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializePlayerSprite(); -} - -void PlayerView::initialize(PlayerController* controller) -{ - player_controller = controller; //to later use it for setting position - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializePlayerSprite(); -} - -void PlayerView::initializePlayerSprite() -{ - if (player_texture.loadFromFile(player_texture_path)) - { - player_sprite.setTexture(player_texture); - scalePlayerSprite(); - } -} - -void PlayerView::scalePlayerSprite() -{ - // setScale is an inbuilt method of the sprite class that takes two floats to scale the sprite. it scales the sprite to our desired height - player_sprite.setScale( - //Here we find the factor to scale our sprites with. Ignore the static_cast for now, we will discuss it later. - static_cast(player_sprite_width) / player_sprite.getTexture()->getSize().x, - static_cast(player_sprite_height) / player_sprite.getTexture()->getSize().y - ); -} - -void PlayerView::update() -{ - //set the updated position before we render - player_sprite.setPosition(player_controller->getPlayerPosition()); -} - -void PlayerView::render() -{ - game_window->draw(player_sprite); -} \ No newline at end of file diff --git a/Space-Invaders/Source/PlayerService.cpp b/Space-Invaders/Source/PlayerService.cpp deleted file mode 100644 index 8b19d77f9..000000000 --- a/Space-Invaders/Source/PlayerService.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "../Header/PlayerService.h" -#include "../Header/Player/PlayerController.h" - -PlayerService::PlayerService() -{ - player_controller = new PlayerController(); -} - -PlayerService::~PlayerService() -{ - delete (player_controller); -} - -void PlayerService::initialize() -{ - player_controller->initialize(); -} - -void PlayerService::update() -{ - player_controller->update(); -} - -void PlayerService::render() -{ - player_controller->render(); -} \ No newline at end of file diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp deleted file mode 100644 index a8fbc2d90..000000000 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "../Header/ServiceLocator.h" - -ServiceLocator::ServiceLocator() -{ - graphic_service = nullptr; - event_service = nullptr; - player_service = nullptr; - time_service = nullptr; - createServices(); -} -ServiceLocator::~ServiceLocator() -{ - clearAllServices(); -} - -void ServiceLocator::createServices() -{ - graphic_service = new GraphicService(); - event_service = new EventService(); - player_service = new PlayerService(); - time_service = new TimeService(); -} - -void ServiceLocator::clearAllServices() -{ - delete(graphic_service); - delete(event_service); - delete(player_service); - delete(time_service); -} - -ServiceLocator* ServiceLocator::getInstance() -{ - static ServiceLocator instance; - return &instance; -} - -void ServiceLocator::initialize() -{ - graphic_service->initialize(); - event_service->initialize(); - player_service->initialize(); - time_service->initialize(); -} - -void ServiceLocator::update() -{ - graphic_service->update(); - event_service->update(); - player_service->update(); - time_service->update(); -} - -void ServiceLocator::render() -{ - graphic_service->render(); - player_service->render(); - //no event service because nothing to render - //no time service -} - -GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } -EventService* ServiceLocator::getEventService() { return event_service; } -PlayerService* ServiceLocator::getPlayerService() { return player_service; } -TimeService* ServiceLocator::getTimeService() { return time_service; } diff --git a/Space-Invaders/Source/TimeService.cpp b/Space-Invaders/Source/TimeService.cpp deleted file mode 100644 index 7ede82971..000000000 --- a/Space-Invaders/Source/TimeService.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "../Header/TimeService.h" - -void TimeService::initialize() -{ - previous_time = std::chrono::steady_clock::now(); - delta_time = 0; -} - -void TimeService::update() -{ - updateDeltaTime(); -} - -float TimeService::getDeltaTime() -{ - return delta_time; -} - -void TimeService::updateDeltaTime() -{ - delta_time = calculateDeltaTime(); - updatePreviousTime(); -} - -float TimeService::calculateDeltaTime() -{ - // Calculate time difference in microseconds between the current and previous frame. - int delta = std::chrono::duration_cast( - std::chrono::steady_clock::now() - previous_time).count(); - - // The cast is used to convert delta time from microseconds into seconds. - return static_cast(delta) / static_cast(1000000); -} - -// Update previous_time to the current time -void TimeService::updatePreviousTime() -{ - previous_time = std::chrono::steady_clock::now(); -} From 2f3005b0ede7ff9f3f92f26b6f482523ac09116f Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Thu, 1 Aug 2024 15:53:18 +0530 Subject: [PATCH 25/28] Added Namespaces --- Space-Invaders/Header/Event/EventService.h | 40 +++--- Space-Invaders/Header/Global/ServiceLocator.h | 65 +++++----- .../Header/Graphic/GraphicService.h | 51 ++++---- Space-Invaders/Header/Main/GameService.h | 37 +++--- .../Header/Player/PlayerController.h | 43 +++--- Space-Invaders/Header/Player/PlayerModel.h | 59 +++++---- Space-Invaders/Header/Player/PlayerService.h | 23 ++-- Space-Invaders/Header/Player/PlayerView.h | 41 +++--- Space-Invaders/Header/Time/TimeService.h | 36 +++--- Space-Invaders/Source/Event/EventService.cpp | 65 +++++----- .../Source/Global/ServiceLocator.cpp | 122 ++++++++++-------- .../Source/Graphic/GraphicService.cpp | 109 ++++++++-------- Space-Invaders/Source/Main/GameService.cpp | 96 +++++++------- .../Source/Player/PlayerController.cpp | 109 ++++++++-------- Space-Invaders/Source/Player/PlayerModel.cpp | 85 ++++++------ .../Source/Player/PlayerService.cpp | 39 +++--- Space-Invaders/Source/Player/PlayerView.cpp | 78 +++++------ Space-Invaders/Source/Time/TimeService.cpp | 61 ++++----- Space-Invaders/main.cpp | 97 +------------- 19 files changed, 624 insertions(+), 632 deletions(-) diff --git a/Space-Invaders/Header/Event/EventService.h b/Space-Invaders/Header/Event/EventService.h index ce9882ddc..6f5924392 100644 --- a/Space-Invaders/Header/Event/EventService.h +++ b/Space-Invaders/Header/Event/EventService.h @@ -3,28 +3,32 @@ #include #include -class EventService +namespace Event { -private: - sf::Event game_event; //event var - sf::RenderWindow* game_window; //ptr to our game window + class EventService + { + private: + sf::Event game_event; //event var + sf::RenderWindow* game_window; //ptr to our game window - bool isGameWindowOpen(); - bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. - bool hasQuitGame(); //for our new 'ESC' condition + bool isGameWindowOpen(); + bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. + bool hasQuitGame(); //for our new 'ESC' condition -public: - EventService(); - ~EventService(); + public: + EventService(); + ~EventService(); - void initialize(); - void update(); - void processEvents(); // while window is open we will check for events - bool pressedEscapeKey(); - bool isKeyboardEvent(); - bool pressedLeftKey(); // getting inputs for the player - bool pressedRightKey(); + void initialize(); + void update(); + void processEvents(); // while window is open we will check for events + bool pressedEscapeKey(); + bool isKeyboardEvent(); + bool pressedLeftKey(); // getting inputs for the player + bool pressedRightKey(); + + }; +} -}; \ No newline at end of file diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h index 332e1549f..a10386a92 100644 --- a/Space-Invaders/Header/Global/ServiceLocator.h +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -7,35 +7,38 @@ // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files -class ServiceLocator +namespace Global { -private: - - // Private Attributes: - GraphicService* graphic_service; - EventService* event_service; - PlayerService* player_service; - TimeService* time_service; - - // Public Methods - ServiceLocator(); - ~ServiceLocator(); - - // Private Methods: - void createServices(); - void clearAllServices(); - -public: - - // Public Methods: - static ServiceLocator* getInstance(); - void initialize(); // Initializes the ServiceLocator. - void update(); // Updates all services. - void render(); // Renders using the services. - - // Methods to Get Specific Services: - EventService* getEventService(); // Retrieve the EventService instance - GraphicService* getGraphicService(); // Retrieve the GraphicService instance - PlayerService* getPlayerService(); // Retrieve the PlayerService instance - TimeService* getTimeService(); // Retrieve the TimeService instance -}; \ No newline at end of file + class ServiceLocator + { + private: + + // Private Attributes: + Graphics::GraphicService* graphic_service; + Event::EventService* event_service; + Player::PlayerService* player_service; + Time::TimeService* time_service; + + // Public Methods + ServiceLocator(); + ~ServiceLocator(); + + // Private Methods: + void createServices(); + void clearAllServices(); + + public: + + // Public Methods: + static ServiceLocator* getInstance(); + void initialize(); // Initializes the ServiceLocator. + void update(); // Updates all services. + void render(); // Renders using the services. + + // Methods to Get Specific Services: + Event::EventService* getEventService(); // Retrieve the EventService instance + Graphics::GraphicService* getGraphicService(); // Retrieve the GraphicService instance + Player::PlayerService* getPlayerService(); // Retrieve the PlayerService instance + Time::TimeService* getTimeService(); // Retrieve the TimeService instance + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Graphic/GraphicService.h b/Space-Invaders/Header/Graphic/GraphicService.h index 83674fde3..c87153f16 100644 --- a/Space-Invaders/Header/Graphic/GraphicService.h +++ b/Space-Invaders/Header/Graphic/GraphicService.h @@ -1,38 +1,41 @@ #pragma once #include -class GraphicService +namespace Graphics { -private: + class GraphicService + { + private: - const std::string game_window_title = "Alien Invader"; + const std::string game_window_title = "Alien Invader"; - const int game_window_width = 800; - const int game_window_height = 600; - - const int frame_rate = 60; + const int game_window_width = 800; + const int game_window_height = 600; - const sf::Color window_color = sf::Color::Black; + const int frame_rate = 60; - sf::VideoMode* video_mode; // ptr to video mode - sf::RenderWindow* game_window; // ptr to a RenderWindow + const sf::Color window_color = sf::Color::Black; - void setVideoMode(); // Method for setting our video mode - void onDestroy(); // method to run when window is deleted + sf::VideoMode* video_mode; // ptr to video mode + sf::RenderWindow* game_window; // ptr to a RenderWindow -public: - GraphicService(); - ~GraphicService(); //cleanup + void setVideoMode(); // Method for setting our video mode + void onDestroy(); // method to run when window is deleted - //method to create the game window. returns a pointer to an instance of the game window - sf::RenderWindow* createGameWindow(); + public: + GraphicService(); + ~GraphicService(); //cleanup + //method to create the game window. returns a pointer to an instance of the game window + sf::RenderWindow* createGameWindow(); - void initialize(); //lifecycle functions - void update(); //.. - void render(); //.. - bool isGameWindowOpen(); //check if the window is open - sf::RenderWindow* getGameWindow(); //getter for the game window instance - sf::Color getWindowColor();//get the color -}; \ No newline at end of file + void initialize(); //lifecycle functions + void update(); //.. + void render(); //.. + bool isGameWindowOpen(); //check if the window is open + + sf::RenderWindow* getGameWindow(); //getter for the game window instance + sf::Color getWindowColor();//get the color + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Main/GameService.h b/Space-Invaders/Header/Main/GameService.h index 53f95bee5..a4bc449c7 100644 --- a/Space-Invaders/Header/Main/GameService.h +++ b/Space-Invaders/Header/Main/GameService.h @@ -1,25 +1,30 @@ #pragma once #include -#include "../../Header/Global/ServiceLocator.h" +#include "../../header/Global/ServiceLocator.h" -class GameService +namespace Main { -private: + class ServiceLocator; - ServiceLocator* service_locator; - sf::RenderWindow* game_window; + class GameService + { + private: + Global::ServiceLocator* service_locator; + sf::RenderWindow* game_window; - void initialize(); // Handles game initialization. - void initializeVariables();// Handles game initialization. - void destroy(); // Handles cleanup tasks. -public: - GameService(); // Constructor for initializing the GameService object. - ~GameService(); // Destructor for cleaning up resources upon object deletion. + void initialize(); // Handles game initialization. + void initializeVariables();// Handles game initialization. + void destroy(); // Handles cleanup tasks. - void ignite(); // Initiates the game. - void update(); // Updates the game logic and game state. - void render(); // Renders each frame of the game. - bool isRunning(); // Checks if the game is currently running. -}; \ No newline at end of file + public: + GameService(); // Constructor for initializing the GameService object. + ~GameService(); // Destructor for cleaning up resources upon object deletion. + + void ignite(); // Initiates the game. + void update(); // Updates the game logic and game state. + void render(); // Renders each frame of the game. + bool isRunning(); // Checks if the game is currently running. + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerController.h b/Space-Invaders/Header/Player/PlayerController.h index 36fda230a..c3a12b538 100644 --- a/Space-Invaders/Header/Player/PlayerController.h +++ b/Space-Invaders/Header/Player/PlayerController.h @@ -1,28 +1,31 @@ #pragma once #include -enum class PlayerState; -class PlayerView; -class PlayerModel; - -class PlayerController +namespace Player { -private: - - PlayerView* player_view; - PlayerModel* player_model; + class PlayerView; + class PlayerModel; + enum class PlayerState; + + class PlayerController + { + private: + + PlayerView* player_view; + PlayerModel* player_model; - void processPlayerInput(); - void moveLeft(); - void moveRight(); + void processPlayerInput(); + void moveLeft(); + void moveRight(); -public: - PlayerController(); - ~PlayerController(); + public: + PlayerController(); + ~PlayerController(); - void initialize(); - void update(); - void render(); + void initialize(); + void update(); + void render(); - sf::Vector2f getPlayerPosition(); -}; \ No newline at end of file + sf::Vector2f getPlayerPosition(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Player/PlayerModel.h b/Space-Invaders/Header/Player/PlayerModel.h index 13d61e0ea..b76424d92 100644 --- a/Space-Invaders/Header/Player/PlayerModel.h +++ b/Space-Invaders/Header/Player/PlayerModel.h @@ -1,43 +1,46 @@ #pragma once #include -enum class PlayerState //Our Enum +namespace Player { - ALIVE, - DEAD, - // we will add more states later -}; + enum class PlayerState //Our Enum + { + ALIVE, + DEAD, + // we will add more states later + }; -class PlayerModel -{ -private: - const sf::Vector2f initial_player_position = sf::Vector2f(400.f, 400.f); + class PlayerModel + { + private: + const sf::Vector2f initial_player_position = sf::Vector2f(400.f, 400.f); - sf::Vector2f player_position; - PlayerState player_state; //Declaration - int player_score; + sf::Vector2f player_position; + PlayerState player_state; //Declaration + int player_score; -public: - const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); - const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); + public: + const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); + const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); - const float player_movement_speed = 300.0f; + const float player_movement_speed = 300.0f; - PlayerModel(); - ~PlayerModel(); + PlayerModel(); + ~PlayerModel(); - void initialize(); - void reset(); + void initialize(); + void reset(); - sf::Vector2f getPlayerPosition(); - void setPlayerPosition(sf::Vector2f position); + sf::Vector2f getPlayerPosition(); + void setPlayerPosition(sf::Vector2f position); - int getPlayerScore(); - void setPlayerScore(int score); + int getPlayerScore(); + void setPlayerScore(int score); - //new getter and setter - PlayerState getPlayerState(); - void setPlayerState(PlayerState state); + //new getter and setter + PlayerState getPlayerState(); + void setPlayerState(PlayerState state); -}; + }; +} diff --git a/Space-Invaders/Header/Player/PlayerService.h b/Space-Invaders/Header/Player/PlayerService.h index 2f46d4894..a978d53c5 100644 --- a/Space-Invaders/Header/Player/PlayerService.h +++ b/Space-Invaders/Header/Player/PlayerService.h @@ -1,16 +1,19 @@ #pragma once #include "../../Header/Player/PlayerController.h" -class PlayerService +namespace Player { -private: - PlayerController* player_controller; + class PlayerService + { + private: + PlayerController* player_controller; -public: - PlayerService(); - ~PlayerService(); + public: + PlayerService(); + ~PlayerService(); - void initialize(); - void update(); - void render(); -}; + void initialize(); + void update(); + void render(); + }; +} diff --git a/Space-Invaders/Header/Player/PlayerView.h b/Space-Invaders/Header/Player/PlayerView.h index 8a1aa85e9..8e901f3f4 100644 --- a/Space-Invaders/Header/Player/PlayerView.h +++ b/Space-Invaders/Header/Player/PlayerView.h @@ -2,30 +2,33 @@ #include #include "../../Header/Player/PlayerController.h" -class PlayerView +namespace Player { -private: + class PlayerView + { + private: - const sf::String player_texture_path = "assets/textures/player_ship.png"; - const float player_sprite_width = 60.f; - const float player_sprite_height = 60.f; + const sf::String player_texture_path = "assets/textures/player_ship.png"; + const float player_sprite_width = 60.f; + const float player_sprite_height = 60.f; - sf::RenderWindow* game_window; + sf::RenderWindow* game_window; - sf::Texture player_texture; - sf::Sprite player_sprite; + sf::Texture player_texture; + sf::Sprite player_sprite; - void initializePlayerSprite(); - void scalePlayerSprite(); + void initializePlayerSprite(); + void scalePlayerSprite(); - PlayerController* player_controller; // ptr to player controller + PlayerController* player_controller; // ptr to player controller -public: - PlayerView(); - ~PlayerView(); + public: + PlayerView(); + ~PlayerView(); - void initialize(); - void update(); - void render(); - void initialize(PlayerController* controller); -}; \ No newline at end of file + void initialize(); + void update(); + void render(); + void initialize(PlayerController* controller); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Header/Time/TimeService.h b/Space-Invaders/Header/Time/TimeService.h index 24ddec14a..71184c46e 100644 --- a/Space-Invaders/Header/Time/TimeService.h +++ b/Space-Invaders/Header/Time/TimeService.h @@ -3,26 +3,30 @@ // The TimeService class helps keep track of time in game and calculate delta time. // Utilizes the library to calculate delta time. -class TimeService + +namespace Time { -private: + class TimeService + { + private: + + // A point in time which indicates the starting time of previous frame. + std::chrono::time_point previous_time; - // A point in time which indicates the starting time of previous frame. - std::chrono::time_point previous_time; - - float delta_time; //to store the detla time + float delta_time; //to store the detla time - void updateDeltaTime(); // method to update time - float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time - void updatePreviousTime(); // finally update the current time to be previous time + void updateDeltaTime(); // method to update time + float calculateDeltaTime(); //calculate time by subtracting the previous time from the current time + void updatePreviousTime(); // finally update the current time to be previous time -public: + public: - //lifecycle methods - void initialize(); - void update(); + //lifecycle methods + void initialize(); + void update(); - //getter - float getDeltaTime(); -}; + //getter + float getDeltaTime(); + }; +} diff --git a/Space-Invaders/Source/Event/EventService.cpp b/Space-Invaders/Source/Event/EventService.cpp index 9be3a667d..f1622bcd5 100644 --- a/Space-Invaders/Source/Event/EventService.cpp +++ b/Space-Invaders/Source/Event/EventService.cpp @@ -1,44 +1,51 @@ #include "../../Header/Event/EventService.h" #include "../../Header/Main/GameService.h" #include "../../Header/Graphic/GraphicService.h" +#include "../../Header/Global/ServiceLocator.h" -EventService::EventService() { game_window = nullptr; } +namespace Event +{ -EventService::~EventService() = default; //calls the default destructor + EventService::EventService() { game_window = nullptr; } -void EventService::initialize() -{ - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); -} + EventService::~EventService() = default; //calls the default destructor -void EventService::update() -{ - //for later -} + using namespace Global; -void EventService::processEvents() -{ - if (isGameWindowOpen()) { - while (game_window->pollEvent(game_event)) { - // Check for window closure - if (gameWindowWasClosed() || hasQuitGame()) - { - game_window->close(); + void EventService::initialize() + { + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + } + + void EventService::update() + { + //for later + } + + void EventService::processEvents() + { + if (isGameWindowOpen()) { + while (game_window->pollEvent(game_event)) { + // Check for window closure + if (gameWindowWasClosed() || hasQuitGame()) + { + game_window->close(); + } } } } -} -bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered + bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered -//checks for if a keyboard key has been pressed -bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; } + //checks for if a keyboard key has been pressed + bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; } -//control click on the SFML functions to see what they do internally -bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } -bool EventService::isGameWindowOpen() { return game_window != nullptr; } -bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } + //control click on the SFML functions to see what they do internally + bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } + bool EventService::isGameWindowOpen() { return game_window != nullptr; } + bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } -// Player inputs -bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } -bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } \ No newline at end of file + // Player inputs + bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } + bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp index 4c239d74b..e4fe451ee 100644 --- a/Space-Invaders/Source/Global/ServiceLocator.cpp +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -1,65 +1,77 @@ #include "../../Header/Global/ServiceLocator.h" +#include "../../header/Main/GameService.h" -ServiceLocator::ServiceLocator() +namespace Global { - graphic_service = nullptr; - event_service = nullptr; - player_service = nullptr; - time_service = nullptr; - createServices(); -} -ServiceLocator::~ServiceLocator() -{ - clearAllServices(); -} + using namespace Main; + using namespace Graphics; + using namespace Event; + using namespace Time; + using namespace Player; + -void ServiceLocator::createServices() -{ - graphic_service = new GraphicService(); - event_service = new EventService(); - player_service = new PlayerService(); - time_service = new TimeService(); -} + ServiceLocator::ServiceLocator() + { + graphic_service = nullptr; + event_service = nullptr; + player_service = nullptr; + time_service = nullptr; + createServices(); + } + ServiceLocator::~ServiceLocator() + { + clearAllServices(); + } -void ServiceLocator::clearAllServices() -{ - delete(graphic_service); - delete(event_service); - delete(player_service); - delete(time_service); -} + void ServiceLocator::createServices() + { + graphic_service = new GraphicService(); + event_service = new EventService(); + player_service = new PlayerService(); + time_service = new TimeService(); + } -ServiceLocator* ServiceLocator::getInstance() -{ - static ServiceLocator instance; - return &instance; -} + void ServiceLocator::clearAllServices() + { + delete(graphic_service); + delete(event_service); + delete(player_service); + delete(time_service); + } -void ServiceLocator::initialize() -{ - graphic_service->initialize(); - event_service->initialize(); - player_service->initialize(); - time_service->initialize(); -} + ServiceLocator* ServiceLocator::getInstance() + { + static ServiceLocator instance; + return &instance; + } -void ServiceLocator::update() -{ - graphic_service->update(); - event_service->update(); - player_service->update(); - time_service->update(); -} + void ServiceLocator::initialize() + { + graphic_service->initialize(); + event_service->initialize(); + player_service->initialize(); + time_service->initialize(); + } -void ServiceLocator::render() -{ - graphic_service->render(); - player_service->render(); - //no event service because nothing to render - //no time service -} + void ServiceLocator::update() + { + graphic_service->update(); + event_service->update(); + player_service->update(); + time_service->update(); + } + + void ServiceLocator::render() + { + graphic_service->render(); + player_service->render(); + //no event service because nothing to render + //no time service + } + + GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } + EventService* ServiceLocator::getEventService() { return event_service; } + PlayerService* ServiceLocator::getPlayerService() { return player_service; } + TimeService* ServiceLocator::getTimeService() { return time_service; } -GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } -EventService* ServiceLocator::getEventService() { return event_service; } -PlayerService* ServiceLocator::getPlayerService() { return player_service; } -TimeService* ServiceLocator::getTimeService() { return time_service; } +} \ No newline at end of file diff --git a/Space-Invaders/Source/Graphic/GraphicService.cpp b/Space-Invaders/Source/Graphic/GraphicService.cpp index 4c8bb04ab..b10897d5d 100644 --- a/Space-Invaders/Source/Graphic/GraphicService.cpp +++ b/Space-Invaders/Source/Graphic/GraphicService.cpp @@ -1,56 +1,59 @@ #include "../../Header/Graphic/GraphicService.h" -// Constructor: Initializes game window and video mode pointers to null. -GraphicService::GraphicService() { - game_window = nullptr; // Initializes game window pointer to null - video_mode = nullptr; // Initializes video mode pointer to null -} - -// Destructor: Cleans up resources by calling onDestroy. -GraphicService::~GraphicService() { - onDestroy(); // Calls onDestroy method to clean up resources -} - -// Initializes the graphic service by creating a new game window. -void GraphicService::initialize() { - game_window = createGameWindow(); // Assigns a new game window - game_window->setFramerateLimit(frame_rate); // setting a frame rate limit -} - -// Creates a new SFML RenderWindow object with specified video mode and title. -sf::RenderWindow* GraphicService::createGameWindow() { - setVideoMode(); // Sets up the video mode for the window - return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object -} - -// Sets up the video mode for the game window using specified dimensions and system's color depth. -void GraphicService::setVideoMode() { - video_mode = new sf::VideoMode(game_window_width, game_window_height, sf::VideoMode::getDesktopMode().bitsPerPixel); // Allocates and sets the video mode -} - -// Cleans up allocated memory for video mode and game window to avoid memory leaks. -void GraphicService::onDestroy() { - delete(video_mode); // Deletes the video mode object - delete(game_window); // Deletes the game window object -} - -// Placeholder function for game update logic. -void GraphicService::update() { } - -// Placeholder function for game rendering logic. -void GraphicService::render() { } - -// Checks if the game window is currently open. -bool GraphicService::isGameWindowOpen() { - return game_window->isOpen(); // Returns the open status of the game window -} - -// Returns a pointer to the game window object. -sf::RenderWindow* GraphicService::getGameWindow() { - return game_window; -} - -// Returns the configured window background color. -sf::Color GraphicService::getWindowColor() { - return window_color; +namespace Graphics +{ + // Constructor: Initializes game window and video mode pointers to null. + GraphicService::GraphicService() { + game_window = nullptr; // Initializes game window pointer to null + video_mode = nullptr; // Initializes video mode pointer to null + } + + // Destructor: Cleans up resources by calling onDestroy. + GraphicService::~GraphicService() { + onDestroy(); // Calls onDestroy method to clean up resources + } + + // Initializes the graphic service by creating a new game window. + void GraphicService::initialize() { + game_window = createGameWindow(); // Assigns a new game window + game_window->setFramerateLimit(frame_rate); // setting a frame rate limit + } + + // Creates a new SFML RenderWindow object with specified video mode and title. + sf::RenderWindow* GraphicService::createGameWindow() { + setVideoMode(); // Sets up the video mode for the window + return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object + } + + // Sets up the video mode for the game window using specified dimensions and system's color depth. + void GraphicService::setVideoMode() { + video_mode = new sf::VideoMode(game_window_width, game_window_height, sf::VideoMode::getDesktopMode().bitsPerPixel); // Allocates and sets the video mode + } + + // Cleans up allocated memory for video mode and game window to avoid memory leaks. + void GraphicService::onDestroy() { + delete(video_mode); // Deletes the video mode object + delete(game_window); // Deletes the game window object + } + + // Placeholder function for game update logic. + void GraphicService::update() { } + + // Placeholder function for game rendering logic. + void GraphicService::render() { } + + // Checks if the game window is currently open. + bool GraphicService::isGameWindowOpen() { + return game_window->isOpen(); // Returns the open status of the game window + } + + // Returns a pointer to the game window object. + sf::RenderWindow* GraphicService::getGameWindow() { + return game_window; + } + + // Returns the configured window background color. + sf::Color GraphicService::getWindowColor() { + return window_color; + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Main/GameService.cpp b/Space-Invaders/Source/Main/GameService.cpp index 12918afe4..d61c57973 100644 --- a/Space-Invaders/Source/Main/GameService.cpp +++ b/Space-Invaders/Source/Main/GameService.cpp @@ -1,59 +1,67 @@ #include "../../Header/Main/GameService.h" #include "../../Header/Graphic/GraphicService.h" +#include "../../Header/Global/ServiceLocator.h" -// Constructor: Initializes pointers to null. -GameService::GameService() { - service_locator = nullptr; // Set service locator to null - game_window = nullptr; // Set game window to null -} +namespace Main +{ + using namespace Global; + using namespace Graphics; + using namespace Event; + // Constructor: Initializes pointers to null. + GameService::GameService() { + service_locator = nullptr; // Set service locator to null + game_window = nullptr; // Set game window to null + } -// Destructor: Calls the destroy function to clean up resources. -GameService::~GameService() { - destroy(); // Clean up and release resources -} + // Destructor: Calls the destroy function to clean up resources. + GameService::~GameService() { + destroy(); // Clean up and release resources + } -// Prepares the game service for use by obtaining the service locator instance and initializing services. -void GameService::ignite() { - service_locator = ServiceLocator::getInstance(); // Get ServiceLocator - initialize(); // Initialize services. -} + // Prepares the game service for use by obtaining the service locator instance and initializing services. + void GameService::ignite() { + service_locator = Global::ServiceLocator::getInstance(); // Get ServiceLocator + initialize(); // Initialize services. + } -//initialize service locator and other variables -void GameService::initialize() -{ - service_locator->initialize(); - initializeVariables(); -} + //initialize service locator and other variables + void GameService::initialize() + { + service_locator->initialize(); + initializeVariables(); + } -void GameService::initializeVariables() -{ - game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) -} + void GameService::initializeVariables() + { + game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) + } -void GameService::destroy() -{ - // don't need to do anything here for now. -} + void GameService::destroy() + { + // don't need to do anything here for now. + } -// Updates the game logic by delegating to the service locator's update method. -void GameService::update() { + // Updates the game logic by delegating to the service locator's update method. + void GameService::update() { - service_locator->getEventService()->processEvents(); + service_locator->getEventService()->processEvents(); - service_locator->update(); // Call update on the service locator which then updates all its managed services -} + service_locator->update(); // Call update on the service locator which then updates all its managed services + } -// Clears the window then display it. -void GameService::render() { - // Clears the game window with the background color provided by the graphic service - game_window->clear(service_locator->getGraphicService()->getWindowColor()); - service_locator->render(); // Render the current frame using the service locator - game_window->display(); // Display the rendered frame on the game window -} + // Clears the window then display it. + void GameService::render() { + // Clears the game window with the background color provided by the graphic service + game_window->clear(service_locator->getGraphicService()->getWindowColor()); + service_locator->render(); // Render the current frame using the service locator + game_window->display(); // Display the rendered frame on the game window + } -// Checks if the game is still running by querying the graphic service's window open status. -bool GameService::isRunning() { - // Returns true if the game window is open, indicating the game is still running - return service_locator->getGraphicService()->isGameWindowOpen(); + // Checks if the game is still running by querying the graphic service's window open status. + bool GameService::isRunning() { + // Returns true if the game window is open, indicating the game is still running + return service_locator->getGraphicService()->isGameWindowOpen(); + } } + diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp index 14b9754c9..534114f48 100644 --- a/Space-Invaders/Source/Player/PlayerController.cpp +++ b/Space-Invaders/Source/Player/PlayerController.cpp @@ -5,70 +5,77 @@ #include "../../Header/Global/ServiceLocator.h" #include -PlayerController::PlayerController() +namespace Player { - player_view = new PlayerView(); - player_model = new PlayerModel(); -} + using namespace Global; + using namespace Event; + using namespace Time; -PlayerController::~PlayerController() -{ - delete (player_view); - delete (player_model); -} -//the controller is responsible for calling the lifecycle methods for the other two -void PlayerController::initialize() -{ - player_model->initialize(); + PlayerController::PlayerController() + { + player_view = new PlayerView(); + player_model = new PlayerModel(); + } - //This will give an error right now since we haven't included the controller in the view. - player_view->initialize(this); // 'this' refers to the class we are currently inside -} + PlayerController::~PlayerController() + { + delete (player_view); + delete (player_model); + } + //the controller is responsible for calling the lifecycle methods for the other two + void PlayerController::initialize() + { + player_model->initialize(); -void PlayerController::update() -{ - processPlayerInput(); - player_view->update(); // we update() the view -} + //This will give an error right now since we haven't included the controller in the view. + player_view->initialize(this); // 'this' refers to the class we are currently inside + } -void PlayerController::render() -{ - player_view->render(); // render the view -} + void PlayerController::update() + { + processPlayerInput(); + player_view->update(); // we update() the view + } -sf::Vector2f PlayerController::getPlayerPosition() -{ - return player_model->getPlayerPosition(); -} + void PlayerController::render() + { + player_view->render(); // render the view + } -void PlayerController::processPlayerInput() -{ - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) + sf::Vector2f PlayerController::getPlayerPosition() { - moveLeft(); + return player_model->getPlayerPosition(); } - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) + + void PlayerController::processPlayerInput() { - moveRight(); + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) + { + moveLeft(); + } + // we will move this to event service at a later time + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) + { + moveRight(); + } } -} -void PlayerController::moveLeft() -{ - sf::Vector2f currentPosition = player_model->getPlayerPosition(); - currentPosition.x -= player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + void PlayerController::moveLeft() + { + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x -= player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - currentPosition.x = std::max(currentPosition.x, player_model->left_most_position.x); - player_model->setPlayerPosition(currentPosition); -} + currentPosition.x = std::max(currentPosition.x, player_model->left_most_position.x); + player_model->setPlayerPosition(currentPosition); + } -void PlayerController::moveRight() -{ - sf::Vector2f currentPosition = player_model->getPlayerPosition(); - currentPosition.x += player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); + void PlayerController::moveRight() + { + sf::Vector2f currentPosition = player_model->getPlayerPosition(); + currentPosition.x += player_model->player_movement_speed * ServiceLocator::getInstance()->getTimeService()->getDeltaTime(); - currentPosition.x = std::min(currentPosition.x, player_model->right_most_position.x); - player_model->setPlayerPosition(currentPosition); + currentPosition.x = std::min(currentPosition.x, player_model->right_most_position.x); + player_model->setPlayerPosition(currentPosition); + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerModel.cpp b/Space-Invaders/Source/Player/PlayerModel.cpp index 0d51db820..3f519eb06 100644 --- a/Space-Invaders/Source/Player/PlayerModel.cpp +++ b/Space-Invaders/Source/Player/PlayerModel.cpp @@ -1,45 +1,48 @@ #include "../../Header/Player/PlayerModel.h" -PlayerModel::PlayerModel() { } - -PlayerModel::~PlayerModel() { } - -void PlayerModel::initialize() { reset(); } // remember to call reset() - -void PlayerModel::reset() -{ - player_state = PlayerState::ALIVE; // set state to alive - player_position = initial_player_position; - player_score = 0; -} - -sf::Vector2f PlayerModel::getPlayerPosition() -{ - return player_position; -} - -void PlayerModel::setPlayerPosition(sf::Vector2f position) -{ - player_position = position; -} - -int PlayerModel::getPlayerScore() -{ - return player_score; -} - -void PlayerModel::setPlayerScore(int score) -{ - player_score = score; -} - -//.. -PlayerState PlayerModel::getPlayerState() -{ - return player_state; -} - -void PlayerModel::setPlayerState(PlayerState state) +namespace Player { - player_state = state; + PlayerModel::PlayerModel() { } + + PlayerModel::~PlayerModel() { } + + void PlayerModel::initialize() { reset(); } // remember to call reset() + + void PlayerModel::reset() + { + player_state = PlayerState::ALIVE; // set state to alive + player_position = initial_player_position; + player_score = 0; + } + + sf::Vector2f PlayerModel::getPlayerPosition() + { + return player_position; + } + + void PlayerModel::setPlayerPosition(sf::Vector2f position) + { + player_position = position; + } + + int PlayerModel::getPlayerScore() + { + return player_score; + } + + void PlayerModel::setPlayerScore(int score) + { + player_score = score; + } + + //.. + PlayerState PlayerModel::getPlayerState() + { + return player_state; + } + + void PlayerModel::setPlayerState(PlayerState state) + { + player_state = state; + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerService.cpp b/Space-Invaders/Source/Player/PlayerService.cpp index af4e3d1e8..e5ccd9d05 100644 --- a/Space-Invaders/Source/Player/PlayerService.cpp +++ b/Space-Invaders/Source/Player/PlayerService.cpp @@ -1,27 +1,30 @@ #include "../../Header/Player/PlayerService.h" #include "../../Header/Player/PlayerController.h" -PlayerService::PlayerService() +namespace Player { - player_controller = new PlayerController(); -} + PlayerService::PlayerService() + { + player_controller = new PlayerController(); + } -PlayerService::~PlayerService() -{ - delete (player_controller); -} + PlayerService::~PlayerService() + { + delete (player_controller); + } -void PlayerService::initialize() -{ - player_controller->initialize(); -} + void PlayerService::initialize() + { + player_controller->initialize(); + } -void PlayerService::update() -{ - player_controller->update(); -} + void PlayerService::update() + { + player_controller->update(); + } -void PlayerService::render() -{ - player_controller->render(); + void PlayerService::render() + { + player_controller->render(); + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Player/PlayerView.cpp b/Space-Invaders/Source/Player/PlayerView.cpp index b3abf9070..bab4189cc 100644 --- a/Space-Invaders/Source/Player/PlayerView.cpp +++ b/Space-Invaders/Source/Player/PlayerView.cpp @@ -1,50 +1,54 @@ #include "../../Header/Player/PlayerView.h" #include "../../Header/Global/ServiceLocator.h" -PlayerView::PlayerView() { } +namespace Player +{ + using namespace Global; + PlayerView::PlayerView() { } -PlayerView::~PlayerView() { } + PlayerView::~PlayerView() { } -void PlayerView::initialize() -{ + void PlayerView::initialize() + { - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializePlayerSprite(); -} + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); + } -void PlayerView::initialize(PlayerController* controller) -{ - player_controller = controller; //to later use it for setting position - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializePlayerSprite(); -} + void PlayerView::initialize(PlayerController* controller) + { + player_controller = controller; //to later use it for setting position + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); + } -void PlayerView::initializePlayerSprite() -{ - if (player_texture.loadFromFile(player_texture_path)) + void PlayerView::initializePlayerSprite() { - player_sprite.setTexture(player_texture); - scalePlayerSprite(); + if (player_texture.loadFromFile(player_texture_path)) + { + player_sprite.setTexture(player_texture); + scalePlayerSprite(); + } } -} -void PlayerView::scalePlayerSprite() -{ - // setScale is an inbuilt method of the sprite class that takes two floats to scale the sprite. it scales the sprite to our desired height - player_sprite.setScale( - //Here we find the factor to scale our sprites with. Ignore the static_cast for now, we will discuss it later. - static_cast(player_sprite_width) / player_sprite.getTexture()->getSize().x, - static_cast(player_sprite_height) / player_sprite.getTexture()->getSize().y - ); -} - -void PlayerView::update() -{ - //set the updated position before we render - player_sprite.setPosition(player_controller->getPlayerPosition()); -} + void PlayerView::scalePlayerSprite() + { + // setScale is an inbuilt method of the sprite class that takes two floats to scale the sprite. it scales the sprite to our desired height + player_sprite.setScale( + //Here we find the factor to scale our sprites with. Ignore the static_cast for now, we will discuss it later. + static_cast(player_sprite_width) / player_sprite.getTexture()->getSize().x, + static_cast(player_sprite_height) / player_sprite.getTexture()->getSize().y + ); + } -void PlayerView::render() -{ - game_window->draw(player_sprite); + void PlayerView::update() + { + //set the updated position before we render + player_sprite.setPosition(player_controller->getPlayerPosition()); + } + + void PlayerView::render() + { + game_window->draw(player_sprite); + } } \ No newline at end of file diff --git a/Space-Invaders/Source/Time/TimeService.cpp b/Space-Invaders/Source/Time/TimeService.cpp index 0bfc0d679..718d1fa8c 100644 --- a/Space-Invaders/Source/Time/TimeService.cpp +++ b/Space-Invaders/Source/Time/TimeService.cpp @@ -1,39 +1,42 @@ #include "../../Header/Time/TimeService.h" -void TimeService::initialize() +namespace Time { - previous_time = std::chrono::steady_clock::now(); - delta_time = 0; -} + void TimeService::initialize() + { + previous_time = std::chrono::steady_clock::now(); + delta_time = 0; + } -void TimeService::update() -{ - updateDeltaTime(); -} + void TimeService::update() + { + updateDeltaTime(); + } -float TimeService::getDeltaTime() -{ - return delta_time; -} + float TimeService::getDeltaTime() + { + return delta_time; + } -void TimeService::updateDeltaTime() -{ - delta_time = calculateDeltaTime(); - updatePreviousTime(); -} + void TimeService::updateDeltaTime() + { + delta_time = calculateDeltaTime(); + updatePreviousTime(); + } -float TimeService::calculateDeltaTime() -{ - // Calculate time difference in microseconds between the current and previous frame. - int delta = std::chrono::duration_cast( - std::chrono::steady_clock::now() - previous_time).count(); + float TimeService::calculateDeltaTime() + { + // Calculate time difference in microseconds between the current and previous frame. + int delta = std::chrono::duration_cast( + std::chrono::steady_clock::now() - previous_time).count(); - // The cast is used to convert delta time from microseconds into seconds. - return static_cast(delta) / static_cast(1000000); -} + // The cast is used to convert delta time from microseconds into seconds. + return static_cast(delta) / static_cast(1000000); + } -// Update previous_time to the current time -void TimeService::updatePreviousTime() -{ - previous_time = std::chrono::steady_clock::now(); + // Update previous_time to the current time + void TimeService::updatePreviousTime() + { + previous_time = std::chrono::steady_clock::now(); + } } diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 434cdaeb5..db819e86b 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,101 +1,10 @@ -#include -#include -#include "Header/Main/GameService.h" - - -/*class Player -{ -private: - - // Private Properties - int health = 3; - sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); - int movement_speed = 1; - int player_score = 0; - -public: - - // Public Properties - sf::Texture player_texture; - sf::Sprite player_sprite; - - //Public Functions, Getter & Setter methods - void move(float offsetX) { - position.x += offsetX; - } - - int getMoveSpeed() { - return movement_speed; - } - - int getScore() { - return player_score; - }; - - void setScore(int newScore) { - player_score = newScore; - }; - - sf::Vector2f getPosition() { - return position; - } - - void setPosition(sf::Vector2f newPosition) { - position = newPosition; - } - - //New methods to be added - void takeDamage() {}; - void move() {}; - void shootBullets() {}; -};*/ +#include "../../header/Main/GameService.h" int main() { - /* - // Define the video mode (dimensions) - sf::VideoMode videoMode = sf::VideoMode(800, 600); - - // Create a window object with specific dimensions and a title - sf::RenderWindow window(videoMode, "My SFML Window"); - - //Player object - Player player; - - //Load Textures and sprite - player.player_texture.loadFromFile("assets/textures/player_ship.png"); - - player.player_sprite.setTexture(player.player_texture); + using namespace Main; - while (window.isOpen()) { - sf::Event event; - while (window.pollEvent(event)) { - // Check for window closure - if (event.type == sf::Event::Closed) - window.close(); - } - - // Handle keyboard input - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - player.move(-1.0f* player.getMoveSpeed()); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - player.move(1.0f * player.getMoveSpeed()); - } - - // Clear the window--- - window.clear(sf::Color::Black); - - // Set and draw player - player.player_sprite.setPosition(player.getPosition()); - - window.draw(player.player_sprite); - - // Display whatever you draw - window.display(); - }*/ GameService* game_service = new GameService(); - game_service->ignite(); while (game_service->isRunning()) @@ -103,4 +12,6 @@ int main() game_service->update(); game_service->render(); } + + return 0; } From ff152f2b860f7e3137b179e6b15b4982e2678647 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Fri, 2 Aug 2024 13:09:58 +0530 Subject: [PATCH 26/28] Game State added --- Space-Invaders/Header/Main/GameService.h | 10 ++++++++++ Space-Invaders/Source/Main/GameService.cpp | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/Space-Invaders/Header/Main/GameService.h b/Space-Invaders/Header/Main/GameService.h index a4bc449c7..a20e216dc 100644 --- a/Space-Invaders/Header/Main/GameService.h +++ b/Space-Invaders/Header/Main/GameService.h @@ -4,12 +4,20 @@ namespace Main { + enum class GameState + { + BOOT, + MAIN_MENU, + GAMEPLAY, + }; + class ServiceLocator; class GameService { private: + static GameState current_state; Global::ServiceLocator* service_locator; sf::RenderWindow* game_window; @@ -26,5 +34,7 @@ namespace Main void update(); // Updates the game logic and game state. void render(); // Renders each frame of the game. bool isRunning(); // Checks if the game is currently running. + static void setGameState(GameState new_state); + static GameState getGameState(); }; } \ No newline at end of file diff --git a/Space-Invaders/Source/Main/GameService.cpp b/Space-Invaders/Source/Main/GameService.cpp index d61c57973..6153c00b6 100644 --- a/Space-Invaders/Source/Main/GameService.cpp +++ b/Space-Invaders/Source/Main/GameService.cpp @@ -7,6 +7,9 @@ namespace Main using namespace Global; using namespace Graphics; using namespace Event; + + GameState GameService::current_state = GameState::BOOT; + // Constructor: Initializes pointers to null. GameService::GameService() { service_locator = nullptr; // Set service locator to null @@ -62,6 +65,12 @@ namespace Main // Returns true if the game window is open, indicating the game is still running return service_locator->getGraphicService()->isGameWindowOpen(); } + + // Setter function foro the game state + void GameService::setGameState(GameState new_state) { current_state = new_state; } + + // Getter function for the current game state + GameState GameService::getGameState() { return current_state; } } From 6601dac47b8e3856bd78e9ff49472505043b4809 Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Fri, 2 Aug 2024 15:15:06 +0530 Subject: [PATCH 27/28] Added Main Menu and lifecycle --- Space-Invaders/Header/Global/ServiceLocator.h | 3 + .../Header/Graphic/GraphicService.h | 4 +- Space-Invaders/Header/Main/GameService.h | 1 + Space-Invaders/Header/Player/PlayerModel.h | 8 +- .../Header/UI/MainMenu/MainMenuUIController.h | 55 +++++++++ Space-Invaders/Header/UI/UIService.h | 23 ++++ .../Source/Global/ServiceLocator.cpp | 10 +- .../Source/Graphic/GraphicService.cpp | 2 +- .../UI/MainMenu/MainMenuUIController.cpp | 105 ++++++++++++++++++ Space-Invaders/Source/UI/UIService.cpp | 51 +++++++++ 10 files changed, 254 insertions(+), 8 deletions(-) create mode 100644 Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h create mode 100644 Space-Invaders/Header/UI/UIService.h create mode 100644 Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp create mode 100644 Space-Invaders/Source/UI/UIService.cpp diff --git a/Space-Invaders/Header/Global/ServiceLocator.h b/Space-Invaders/Header/Global/ServiceLocator.h index a10386a92..fe2ccf055 100644 --- a/Space-Invaders/Header/Global/ServiceLocator.h +++ b/Space-Invaders/Header/Global/ServiceLocator.h @@ -3,6 +3,7 @@ #include "../../Header/Event/EventService.h" #include "../../Header/Player/PlayerService.h" #include "../../Header/Time/TimeService.h" +#include "../../Header/UI/UIService.h" // ServiceLocator Class Summary: This class manages access to various services in the application. // include relevant headers files @@ -18,6 +19,7 @@ namespace Global Event::EventService* event_service; Player::PlayerService* player_service; Time::TimeService* time_service; + UI::UIService* ui_service; // Public Methods ServiceLocator(); @@ -40,5 +42,6 @@ namespace Global Graphics::GraphicService* getGraphicService(); // Retrieve the GraphicService instance Player::PlayerService* getPlayerService(); // Retrieve the PlayerService instance Time::TimeService* getTimeService(); // Retrieve the TimeService instance + UI::UIService* getUIService(); // Retrive the UIService instance }; } \ No newline at end of file diff --git a/Space-Invaders/Header/Graphic/GraphicService.h b/Space-Invaders/Header/Graphic/GraphicService.h index c87153f16..a0514d024 100644 --- a/Space-Invaders/Header/Graphic/GraphicService.h +++ b/Space-Invaders/Header/Graphic/GraphicService.h @@ -9,8 +9,8 @@ namespace Graphics const std::string game_window_title = "Alien Invader"; - const int game_window_width = 800; - const int game_window_height = 600; + const int game_window_width = 1920; + const int game_window_height = 1080; const int frame_rate = 60; diff --git a/Space-Invaders/Header/Main/GameService.h b/Space-Invaders/Header/Main/GameService.h index a20e216dc..123bf325a 100644 --- a/Space-Invaders/Header/Main/GameService.h +++ b/Space-Invaders/Header/Main/GameService.h @@ -18,6 +18,7 @@ namespace Main private: static GameState current_state; + Global::ServiceLocator* service_locator; sf::RenderWindow* game_window; diff --git a/Space-Invaders/Header/Player/PlayerModel.h b/Space-Invaders/Header/Player/PlayerModel.h index b76424d92..43e8ec35f 100644 --- a/Space-Invaders/Header/Player/PlayerModel.h +++ b/Space-Invaders/Header/Player/PlayerModel.h @@ -13,17 +13,17 @@ namespace Player class PlayerModel { private: - const sf::Vector2f initial_player_position = sf::Vector2f(400.f, 400.f); + const sf::Vector2f initial_player_position = sf::Vector2f(950.f, 950.f); sf::Vector2f player_position; PlayerState player_state; //Declaration int player_score; public: - const sf::Vector2f left_most_position = sf::Vector2f(50.f, 0.f); - const sf::Vector2f right_most_position = sf::Vector2f(700.f, 0.f); + const sf::Vector2f left_most_position = sf::Vector2f(50.f, 950.f); + const sf::Vector2f right_most_position = sf::Vector2f(1800.f, 950.f); - const float player_movement_speed = 300.0f; + const float player_movement_speed = 600.0f; PlayerModel(); ~PlayerModel(); diff --git a/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h b/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h new file mode 100644 index 000000000..32dc26439 --- /dev/null +++ b/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h @@ -0,0 +1,55 @@ +#pragma once +#include + +namespace UI +{ + namespace MainMenu + { + class MainMenuUIController + { + + private: + const sf::String background_texture_path = "assets/textures/space_invaders_bg.png"; + const sf::String play_button_texture_path = "assets/textures/play_button.png"; + const sf::String instructions_button_texture_path = "assets/textures/instructions_button.png"; + const sf::String quit_button_texture_path = "assets/textures/quit_button.png"; + + // Constants: + const float button_width = 400.f; + const float button_height = 140.f; + + sf::RenderWindow* game_window; + + // Textures: + sf::Texture background_texture; + sf::Sprite background_sprite; + + sf::Texture play_button_texture; + sf::Sprite play_button_sprite; + + sf::Texture instructions_button_texture; + sf::Sprite instructions_button_sprite; + + sf::Texture quit_button_texture; + sf::Sprite quit_button_sprite; + + // Buttons and scaling + void initializeBackgroundImage(); + void scaleBackgroundImage(); + void initializeButtons(); + bool loadButtonTexturesFromFile(); + void setButtonSprites(); + void scaleAllButttons(); + void scaleButton(sf::Sprite* button_to_scale); + void positionButtons(); + + public: + MainMenuUIController(); + + void initialize(); + void update(); + void render(); + + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Header/UI/UIService.h b/Space-Invaders/Header/UI/UIService.h new file mode 100644 index 000000000..2f8856ee3 --- /dev/null +++ b/Space-Invaders/Header/UI/UIService.h @@ -0,0 +1,23 @@ +#pragma once +#include "../../Header/UI/MainMenu/MainMenuUIController.h" + +namespace UI +{ + class UIService + { + private: + MainMenu::MainMenuUIController* main_menu_controller; + + void createControllers(); + void initializeControllers(); + void destroy(); + + public: + UIService(); + ~UIService(); + + void initialize(); + void update(); + void render(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Source/Global/ServiceLocator.cpp b/Space-Invaders/Source/Global/ServiceLocator.cpp index e4fe451ee..9983889db 100644 --- a/Space-Invaders/Source/Global/ServiceLocator.cpp +++ b/Space-Invaders/Source/Global/ServiceLocator.cpp @@ -8,6 +8,7 @@ namespace Global using namespace Event; using namespace Time; using namespace Player; + using namespace UI; ServiceLocator::ServiceLocator() @@ -16,6 +17,7 @@ namespace Global event_service = nullptr; player_service = nullptr; time_service = nullptr; + ui_service = nullptr; createServices(); } ServiceLocator::~ServiceLocator() @@ -29,6 +31,7 @@ namespace Global event_service = new EventService(); player_service = new PlayerService(); time_service = new TimeService(); + ui_service = new UIService(); } void ServiceLocator::clearAllServices() @@ -37,6 +40,7 @@ namespace Global delete(event_service); delete(player_service); delete(time_service); + delete(ui_service); } ServiceLocator* ServiceLocator::getInstance() @@ -51,6 +55,7 @@ namespace Global event_service->initialize(); player_service->initialize(); time_service->initialize(); + ui_service->initialize(); } void ServiceLocator::update() @@ -59,19 +64,22 @@ namespace Global event_service->update(); player_service->update(); time_service->update(); + ui_service->update(); } void ServiceLocator::render() { graphic_service->render(); player_service->render(); + ui_service->render(); //no event service because nothing to render - //no time service + //no time service } GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } EventService* ServiceLocator::getEventService() { return event_service; } PlayerService* ServiceLocator::getPlayerService() { return player_service; } TimeService* ServiceLocator::getTimeService() { return time_service; } + UIService* ServiceLocator::getUIService() { return ui_service; } } \ No newline at end of file diff --git a/Space-Invaders/Source/Graphic/GraphicService.cpp b/Space-Invaders/Source/Graphic/GraphicService.cpp index b10897d5d..98ab5cc20 100644 --- a/Space-Invaders/Source/Graphic/GraphicService.cpp +++ b/Space-Invaders/Source/Graphic/GraphicService.cpp @@ -22,7 +22,7 @@ namespace Graphics // Creates a new SFML RenderWindow object with specified video mode and title. sf::RenderWindow* GraphicService::createGameWindow() { setVideoMode(); // Sets up the video mode for the window - return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object + return new sf::RenderWindow(*video_mode, game_window_title, sf::Style::Fullscreen); // Creates and returns a new RenderWindow object } // Sets up the video mode for the game window using specified dimensions and system's color depth. diff --git a/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp new file mode 100644 index 000000000..e5accf6e4 --- /dev/null +++ b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp @@ -0,0 +1,105 @@ +#include "../../Header/UI/MainMenu/MainMenuUIController.h" +#include "../../Header/UI/MainMenu/MainMenuUIController.h" +#include "../../Header/Main/GameService.h" +#include "../../Header/Global/ServiceLocator.h" +#include "../../Header/Graphic/GraphicService.h" + +namespace UI +{ + namespace MainMenu //nested namespace since everything in MainMenu exists inside UI + { + using namespace Global; + using namespace Main; + using namespace Graphics; + using namespace Event; + + MainMenuUIController::MainMenuUIController() { game_window = nullptr; } + + void MainMenuUIController::initialize() + { + game_window = Global::ServiceLocator::getInstance()->getGraphicService()->getGameWindow();// added global to access + initializeBackgroundImage(); + initializeButtons(); + } + + void MainMenuUIController::initializeBackgroundImage() + { //check if a texture loaded properly + if (background_texture.loadFromFile(background_texture_path)) + { //if it did then set the bg image and scale it + background_sprite.setTexture(background_texture); + scaleBackgroundImage(); + } + } + + void MainMenuUIController::scaleBackgroundImage() + { + background_sprite.setScale( + static_cast(game_window->getSize().x) / background_sprite.getTexture()->getSize().x, + static_cast(game_window->getSize().y) / background_sprite.getTexture()->getSize().y + ); + } + + void MainMenuUIController::initializeButtons() + { + // check if the tectures loaded + if (loadButtonTexturesFromFile()) + { + // order of function calls matter + setButtonSprites(); + scaleAllButttons(); + positionButtons(); + } + } + // only returns true if all tectures are loaded + bool MainMenuUIController::loadButtonTexturesFromFile() + { + return play_button_texture.loadFromFile(play_button_texture_path) && + instructions_button_texture.loadFromFile(instructions_button_texture_path) && + quit_button_texture.loadFromFile(quit_button_texture_path); + } + + void MainMenuUIController::setButtonSprites() + { + play_button_sprite.setTexture(play_button_texture); + instructions_button_sprite.setTexture(instructions_button_texture); + quit_button_sprite.setTexture(quit_button_texture); + } + + + void MainMenuUIController::scaleAllButttons() + { + scaleButton(&play_button_sprite); + scaleButton(&instructions_button_sprite); + scaleButton(&quit_button_sprite); + } + + void MainMenuUIController::scaleButton(sf::Sprite* button_to_scale) + { + button_to_scale->setScale( + button_width / button_to_scale->getTexture()->getSize().x, + button_height / button_to_scale->getTexture()->getSize().y + ); + } + + void MainMenuUIController::positionButtons() + { + float x_position = (static_cast(game_window->getSize().x) / 2) - button_width / 2; + + play_button_sprite.setPosition({ x_position, 500.f }); + instructions_button_sprite.setPosition({ x_position, 700.f }); + quit_button_sprite.setPosition({ x_position, 900.f }); + } + + void MainMenuUIController::update() + { + } + + void MainMenuUIController::render() + { + game_window->draw(background_sprite); + game_window->draw(play_button_sprite); + game_window->draw(instructions_button_sprite); + game_window->draw(quit_button_sprite); + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Source/UI/UIService.cpp b/Space-Invaders/Source/UI/UIService.cpp new file mode 100644 index 000000000..33b445b99 --- /dev/null +++ b/Space-Invaders/Source/UI/UIService.cpp @@ -0,0 +1,51 @@ +#include "../../Header/UI/UIService.h" +#include "../../Header/Main/GameService.h" + +namespace UI +{ + using namespace Main; + using namespace MainMenu; + + UIService::UIService() + { + main_menu_controller = nullptr; + + createControllers(); + } + + void UIService::createControllers() + { + main_menu_controller = new MainMenuUIController(); + } + + UIService::~UIService() + { + destroy(); + } + + void UIService::initialize() + { + initializeControllers(); + + } + + void UIService::update() + { + main_menu_controller->update(); + } + + void UIService::render() + { + main_menu_controller->render(); + } + + void UIService::initializeControllers() + { + main_menu_controller->initialize(); + } + + void UIService::destroy() + { + delete(main_menu_controller); + } +} \ No newline at end of file From 50e219cd90c8baeaf764a50ca776ce476458a60f Mon Sep 17 00:00:00 2001 From: Rishi Saxena Date: Fri, 2 Aug 2024 15:49:04 +0530 Subject: [PATCH 28/28] Added new control functions --- Space-Invaders/Header/Event/EventService.h | 22 ++++++ Space-Invaders/Header/Main/GameService.h | 1 + .../Header/UI/MainMenu/MainMenuUIController.h | 3 + Space-Invaders/Source/Event/EventService.cpp | 73 ++++++++++++++++++- Space-Invaders/Source/Main/GameService.cpp | 7 ++ .../Source/Player/PlayerController.cpp | 10 ++- .../UI/MainMenu/MainMenuUIController.cpp | 25 +++++++ Space-Invaders/Source/UI/UIService.cpp | 14 +++- 8 files changed, 146 insertions(+), 9 deletions(-) diff --git a/Space-Invaders/Header/Event/EventService.h b/Space-Invaders/Header/Event/EventService.h index 6f5924392..ebecfd5d6 100644 --- a/Space-Invaders/Header/Event/EventService.h +++ b/Space-Invaders/Header/Event/EventService.h @@ -5,6 +5,13 @@ namespace Event { + enum class ButtonState + { + PRESSED, + HELD, + RELEASED, + }; + class EventService { private: @@ -15,6 +22,17 @@ namespace Event bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. bool hasQuitGame(); //for our new 'ESC' condition + //Button States + ButtonState left_mouse_button_state; + ButtonState right_mouse_button_state; + ButtonState left_arrow_button_state; + ButtonState right_arrow_button_state; + ButtonState A_button_state; + ButtonState D_button_state; + + //Button and mouse state functions + void updateMouseButtonsState(ButtonState& current_button_state, sf::Mouse::Button mouse_button); + void updateKeyboardButtonsState(ButtonState& current_button_state, sf::Keyboard::Key keyboard_button); public: @@ -28,6 +46,10 @@ namespace Event bool isKeyboardEvent(); bool pressedLeftKey(); // getting inputs for the player bool pressedRightKey(); + bool pressedLeftMouseButton(); // Mouse inputs for the player + bool pressedRightMouseButton(); + bool pressedAKey(); // AD held check + bool pressedDKey(); }; } diff --git a/Space-Invaders/Header/Main/GameService.h b/Space-Invaders/Header/Main/GameService.h index 123bf325a..5671190cb 100644 --- a/Space-Invaders/Header/Main/GameService.h +++ b/Space-Invaders/Header/Main/GameService.h @@ -26,6 +26,7 @@ namespace Main void initialize(); // Handles game initialization. void initializeVariables();// Handles game initialization. void destroy(); // Handles cleanup tasks. + void showMainMenu(); public: GameService(); // Constructor for initializing the GameService object. diff --git a/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h b/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h index 32dc26439..0b811811c 100644 --- a/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h +++ b/Space-Invaders/Header/UI/MainMenu/MainMenuUIController.h @@ -42,6 +42,9 @@ namespace UI void scaleAllButttons(); void scaleButton(sf::Sprite* button_to_scale); void positionButtons(); + // Which button and function + void processButtonInteractions(); + bool clickedButton(sf::Sprite*, sf::Vector2f); public: MainMenuUIController(); diff --git a/Space-Invaders/Source/Event/EventService.cpp b/Space-Invaders/Source/Event/EventService.cpp index f1622bcd5..cd456eff0 100644 --- a/Space-Invaders/Source/Event/EventService.cpp +++ b/Space-Invaders/Source/Event/EventService.cpp @@ -19,7 +19,12 @@ namespace Event void EventService::update() { - //for later + updateMouseButtonsState(left_mouse_button_state, sf::Mouse::Left); + updateMouseButtonsState(right_mouse_button_state, sf::Mouse::Right); + updateKeyboardButtonsState(left_arrow_button_state, sf::Keyboard::Left); + updateKeyboardButtonsState(right_arrow_button_state, sf::Keyboard::Right); + updateKeyboardButtonsState(A_button_state, sf::Keyboard::A); + updateKeyboardButtonsState(D_button_state, sf::Keyboard::D); } void EventService::processEvents() @@ -35,6 +40,46 @@ namespace Event } } + void EventService::updateMouseButtonsState(ButtonState& current_button_state, sf::Mouse::Button mouse_button) + { + if (sf::Mouse::isButtonPressed(mouse_button)) + { + switch (current_button_state) + { + case ButtonState::RELEASED: + current_button_state = ButtonState::PRESSED; + break; + case ButtonState::PRESSED: + current_button_state = ButtonState::HELD; + break; + } + } + else + { + current_button_state = ButtonState::RELEASED; + } + } + + void EventService::updateKeyboardButtonsState(ButtonState& current_button_state, sf::Keyboard::Key keyboard_button) + { + if (sf::Keyboard::isKeyPressed(keyboard_button)) + { + switch (current_button_state) + { + case ButtonState::RELEASED: + current_button_state = ButtonState::PRESSED; + break; + case ButtonState::PRESSED: + current_button_state = ButtonState::HELD; + break; + } + } + else + { + current_button_state = ButtonState::RELEASED; + } + } + bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered //checks for if a keyboard key has been pressed @@ -45,7 +90,29 @@ namespace Event bool EventService::isGameWindowOpen() { return game_window != nullptr; } bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } - // Player inputs - bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } + // Player inputs + bool EventService::pressedLeftKey() { return left_arrow_button_state == ButtonState::HELD; } + + bool EventService::pressedRightKey() { return right_arrow_button_state == ButtonState::HELD; } + + bool EventService::pressedAKey() { return A_button_state == ButtonState::HELD; } + + bool EventService::pressedDKey() { return D_button_state == ButtonState::HELD; } + + bool EventService::pressedLeftMouseButton() { return left_mouse_button_state == ButtonState::PRESSED; } + + bool EventService::pressedRightMouseButton() { return right_mouse_button_state == ButtonState::PRESSED; } + + /*bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } + bool EventService::pressedLeftMouseButton() + { + // check if a mouse button was pressed and which mouse button it was + return game_event.type == sf::Event::MouseButtonPressed && game_event.mouseButton.button == sf::Mouse::Left; + } + + bool EventService::pressedRightMouseButton() + { + return game_event.type == sf::Event::MouseButtonPressed && game_event.mouseButton.button == sf::Mouse::Right; + }*/ } \ No newline at end of file diff --git a/Space-Invaders/Source/Main/GameService.cpp b/Space-Invaders/Source/Main/GameService.cpp index 6153c00b6..0118a4dab 100644 --- a/Space-Invaders/Source/Main/GameService.cpp +++ b/Space-Invaders/Source/Main/GameService.cpp @@ -32,6 +32,8 @@ namespace Main { service_locator->initialize(); initializeVariables(); + // set the gamestate to main menu + showMainMenu(); } void GameService::initializeVariables() @@ -44,6 +46,11 @@ namespace Main // don't need to do anything here for now. } + void GameService::showMainMenu() + { + setGameState(GameState::MAIN_MENU); + } + // Updates the game logic by delegating to the service locator's update method. void GameService::update() { diff --git a/Space-Invaders/Source/Player/PlayerController.cpp b/Space-Invaders/Source/Player/PlayerController.cpp index 534114f48..d84805283 100644 --- a/Space-Invaders/Source/Player/PlayerController.cpp +++ b/Space-Invaders/Source/Player/PlayerController.cpp @@ -49,13 +49,15 @@ namespace Player void PlayerController::processPlayerInput() { - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left))) + + EventService* event_service = ServiceLocator::getInstance()->getEventService(); + + if (event_service->pressedLeftKey() || event_service->pressedAKey()) { moveLeft(); } - // we will move this to event service at a later time - if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right))) + + if (event_service->pressedRightKey() || event_service->pressedDKey()) { moveRight(); } diff --git a/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp index e5accf6e4..37b309a04 100644 --- a/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp +++ b/Space-Invaders/Source/UI/MainMenu/MainMenuUIController.cpp @@ -90,8 +90,33 @@ namespace UI quit_button_sprite.setPosition({ x_position, 900.f }); } + void MainMenuUIController::processButtonInteractions() + { + sf::Vector2f mouse_position = sf::Vector2f(sf::Mouse::getPosition(*game_window)); + + if (clickedButton(&play_button_sprite, mouse_position)) + { + GameService::setGameState(GameState::GAMEPLAY); + } + + if (clickedButton(&instructions_button_sprite, mouse_position)) + { + printf("Clicked Instruction Button \\n"); + } + + if (clickedButton(&quit_button_sprite, mouse_position)) + game_window->close(); + } + + bool MainMenuUIController::clickedButton(sf::Sprite* button_sprite, sf::Vector2f mouse_position) + { + EventService* event_service = Global::ServiceLocator::getInstance()->getEventService(); + return event_service->pressedLeftMouseButton() && button_sprite->getGlobalBounds().contains(mouse_position); + } + void MainMenuUIController::update() { + processButtonInteractions(); } void MainMenuUIController::render() diff --git a/Space-Invaders/Source/UI/UIService.cpp b/Space-Invaders/Source/UI/UIService.cpp index 33b445b99..3e60dec73 100644 --- a/Space-Invaders/Source/UI/UIService.cpp +++ b/Space-Invaders/Source/UI/UIService.cpp @@ -31,12 +31,22 @@ namespace UI void UIService::update() { - main_menu_controller->update(); + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->update();; + break; + } } void UIService::render() { - main_menu_controller->render(); + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->render(); + break; + } } void UIService::initializeControllers()