From 7b05a9d90e1af449763d5fa372da7e1c68cd919c Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Wed, 22 May 2024 19:31:00 +0530 Subject: [PATCH 1/8] Starting off the player created a player class --- Space-Invaders/main.cpp | 50 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 7d5f90dff..f1c84fc83 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,5 +1,51 @@ +#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; + } + + + void takeDamage() {}; + void move() {}; + void shootBullets() {}; + +}; + +int main() { + // Create a Player object + Player player; + + // Access the private variable using the public getter + std::cout << "Player Score: " << player.getScore(); + + // Modify the variable using the public setter + player.setScore(100); + + // Access the modified variable using the public getter + std::cout << "Player Modified Score: " << player.getScore(); -int main() -{ return 0; } \ No newline at end of file From 43fdc54fad1679a7d9395f89ace1d849ee277299 Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Sat, 25 May 2024 16:15:53 +0530 Subject: [PATCH 2/8] Fetch Keyboard Inputs Added keyboard inputs --- Space-Invaders/main.cpp | 46 ++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index f1c84fc83..2d262fa91 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -27,6 +27,10 @@ class Player { player_score = newScore; } + sf::Vector2f getPosition() { + return position; + } + void takeDamage() {}; void move() {}; @@ -35,17 +39,43 @@ class Player { }; int main() { - // Create a Player object - Player player; - // Access the private variable using the public getter - std::cout << "Player Score: " << player.getScore(); + sf::VideoMode videoMode = sf::VideoMode(800, 600); // Define the video mode (dimensions) + + sf::RenderWindow window(videoMode, "SFML Window"); // Create a window object + + Player player; // Create the player object + + player.player_texture.loadFromFile("assets/textures/player_ship.png"); // Load the player ship texture + + player.player_sprite.setTexture(player.player_texture); // Set the player sprite variable + + 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(); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + player.move(); + } + + // Clear the window + window.clear(sf::Color::Blue); // this code will set a blue background color (optional) + + player.player_sprite.setPosition(player.getPosition()); // Set the position of the player sprite + + window.draw(player.player_sprite); // Draw the player sprite - // Modify the variable using the public setter - player.setScore(100); + window.display(); // Display what was drawn - // Access the modified variable using the public getter - std::cout << "Player Modified Score: " << player.getScore(); + } // end while loop return 0; } \ No newline at end of file From ab73700854331c2e26504a2b2bfa597edc542f37 Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Sat, 25 May 2024 17:14:43 +0530 Subject: [PATCH 3/8] Added Player Movement added movement to the player ship --- Space-Invaders/main.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 2d262fa91..f33c7ded6 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: @@ -33,7 +33,14 @@ class Player { void takeDamage() {}; - void move() {}; + void move(float offsetX) { + position.x += offsetX; + }; + + int getMoveSpeed() { + return movement_speed; + } + void shootBullets() {}; }; @@ -46,9 +53,6 @@ int main() { Player player; // Create the player object - player.player_texture.loadFromFile("assets/textures/player_ship.png"); // Load the player ship texture - - player.player_sprite.setTexture(player.player_texture); // Set the player sprite variable while (window.isOpen()) { sf::Event event; @@ -58,14 +62,19 @@ int main() { window.close(); } - // Handle keyboard input + // 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()); } + + player.player_texture.loadFromFile("assets/textures/player_ship.png"); // Load the player ship texture + + player.player_sprite.setTexture(player.player_texture); // Set the player sprite variable + // Clear the window window.clear(sf::Color::Blue); // this code will set a blue background color (optional) From 5e9ec42277797dbce0f04898e68f17be46914db5 Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Tue, 28 May 2024 03:51:50 +0530 Subject: [PATCH 4/8] added .h file --- Space-Invaders/Header/GameSevice.h | 1 + Space-Invaders/Source/GameSevice.cpp | 50 ++++++++++++++++++++++++++++ Space-Invaders/main.cpp | 12 +++++++ 3 files changed, 63 insertions(+) create mode 100644 Space-Invaders/Header/GameSevice.h create mode 100644 Space-Invaders/Source/GameSevice.cpp diff --git a/Space-Invaders/Header/GameSevice.h b/Space-Invaders/Header/GameSevice.h new file mode 100644 index 000000000..6f70f09be --- /dev/null +++ b/Space-Invaders/Header/GameSevice.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Source/GameSevice.cpp b/Space-Invaders/Source/GameSevice.cpp new file mode 100644 index 000000000..102fd0259 --- /dev/null +++ b/Space-Invaders/Source/GameSevice.cpp @@ -0,0 +1,50 @@ +#include +#include "../Header/GameSevice.h" + + + + + void GameService::initialize() + { + //... Get things running + } + + void GameService::destroy() + { + //cleanup resources + } + + GameService::GameService() + { + //constructor + } + + GameService::~GameService() + { + //destructor + } + + void GameService::ignite() + { + //starts the game + } + + void GameService::update() + { + // Updates the game logic and game state. + } + + void GameService::render() + { + // Renders each frame of the game. + } + + bool GameService::isRunning() + { + // Checks if the game is currently running. + return false; //default return + } + + + + diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index f33c7ded6..4b4b7f79b 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -35,8 +35,14 @@ class Player { void takeDamage() {}; void move(float offsetX) { position.x += offsetX; + }; + void move1(float offsetY) { + position.y += offsetY; + + }; + int getMoveSpeed() { return movement_speed; } @@ -69,6 +75,12 @@ int main() { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { player.move(1.0f * player.getMoveSpeed()); } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { + player.move1(-1.0f * player.getMoveSpeed()); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { + player.move1(1.0f * player.getMoveSpeed()); + } player.player_texture.loadFromFile("assets/textures/player_ship.png"); // Load the player ship texture From 30805a6d7dbf505d3eaa545452f823b92d576eda Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Tue, 28 May 2024 04:38:48 +0530 Subject: [PATCH 5/8] main.cpp changes --- Space-Invaders/Header/GameSevice.h | 16 ++++++++++++++++ Space-Invaders/main.cpp | 28 +++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Space-Invaders/Header/GameSevice.h b/Space-Invaders/Header/GameSevice.h index 6f70f09be..16e5f24c0 100644 --- a/Space-Invaders/Header/GameSevice.h +++ b/Space-Invaders/Header/GameSevice.h @@ -1 +1,17 @@ #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. +}; diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 4b4b7f79b..c82cf8088 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,3 +1,27 @@ +#include "Header/GameSevice.h" + +int main() { + + GameService gameService; + + gameService.ignite(); + + while (gameService.isRunning()) + { + + gameService.update(); + gameService.render(); + + } + +} + + + + + +/* + #include #include @@ -99,4 +123,6 @@ int main() { } // end while loop return 0; -} \ No newline at end of file +} + +*/ \ No newline at end of file From 2405fc78586a82464396c9356e460e6b91ff11bb Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Sat, 1 Jun 2024 00:12:13 +0530 Subject: [PATCH 6/8] Services --- Space-Invaders/Header/ServiceLocator.h | 39 +++++++++++++++++ Space-Invaders/Source/ServiceLocator.cpp | 43 +++++++++++++++++++ Space-Invaders/Space-Invaders.vcxproj | 6 +++ Space-Invaders/Space-Invaders.vcxproj.filters | 14 ++++++ 4 files changed, 102 insertions(+) create mode 100644 Space-Invaders/Header/ServiceLocator.h create mode 100644 Space-Invaders/Source/ServiceLocator.cpp diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h new file mode 100644 index 000000000..16e3410d6 --- /dev/null +++ b/Space-Invaders/Header/ServiceLocator.h @@ -0,0 +1,39 @@ +#pragma once + +// ServiceLocator Class Summary: This class manages access to various services in the application. +// include relevant headers files + +class ServiceLocator +{ +private: + // Private Attributes: + // - event_service: Manages event-related functionalities. + // - graphic_service: Handles graphics-related tasks. + // .......................... + // .......................... + + // Private Constructor and Destructor: + + // Constructor for initializing the ServiceLocator. + ServiceLocator(); + + // Destructor for cleaning up resources upon object deletion. + ~ServiceLocator(); + + // Private Methods: + void createServices(); // Creates instances of all services. + void clearAllServices(); // Deletes and deallocates memory for all services. + +public: + // Public Methods: + static ServiceLocator* getInstance(); // Provides a method to access the unique ServiceLocator instance (object). We will discuss this later. + + 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 + +}; diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp new file mode 100644 index 000000000..4db281330 --- /dev/null +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -0,0 +1,43 @@ +#include +#include "../Header/ServiceLocator.h" + + +ServiceLocator::ServiceLocator() +{ + //construct +} + +ServiceLocator::~ServiceLocator() + { + //deconstructor + } + +void ServiceLocator::createServices() +{ + // Creates instances of all services. +} + +void ServiceLocator::clearAllServices() +{ + // Deletes and deallocates memory for all services. +} + +void ServiceLocator::getInstance() +{ + // Provides a method to access the unique ServiceLocator instance (object). We will discuss this later. +} + +void ServiceLocator::initialize() +{ + // Initializes the ServiceLocator. +} + +void ServiceLocator::update() +{ + // Updates all services. +} + +void ServiceLocator::render() +{ + // Renders using the services. +} diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 6f7fa388d..3f4f1a171 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -133,6 +133,12 @@ + + + + + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index ce0c35ccf..23552d099 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -18,5 +18,19 @@ Source Files + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + \ No newline at end of file From eaedfa737358f03703a35ef2ed0c4e6baf16e9f2 Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Mon, 10 Jun 2024 16:17:45 +0530 Subject: [PATCH 7/8] Services --- .../Header/{GameSevice.h => GameService.h} | 14 ++++- Space-Invaders/Header/GraphicService.h | 37 ++++++++++++ Space-Invaders/Header/ServiceLocator.h | 22 ++----- Space-Invaders/Source/GameService.cpp | 56 ++++++++++++++++++ Space-Invaders/Source/GameSevice.cpp | 50 ---------------- Space-Invaders/Source/GraphicService.cpp | 55 ++++++++++++++++++ Space-Invaders/Source/ServiceLocator.cpp | 58 ++++++++++--------- Space-Invaders/Space-Invaders.vcxproj | 8 ++- Space-Invaders/Space-Invaders.vcxproj.filters | 10 +++- 9 files changed, 209 insertions(+), 101 deletions(-) rename Space-Invaders/Header/{GameSevice.h => GameService.h} (56%) create mode 100644 Space-Invaders/Header/GraphicService.h create mode 100644 Space-Invaders/Source/GameService.cpp delete mode 100644 Space-Invaders/Source/GameSevice.cpp create mode 100644 Space-Invaders/Source/GraphicService.cpp diff --git a/Space-Invaders/Header/GameSevice.h b/Space-Invaders/Header/GameService.h similarity index 56% rename from Space-Invaders/Header/GameSevice.h rename to Space-Invaders/Header/GameService.h index 16e5f24c0..acf939667 100644 --- a/Space-Invaders/Header/GameSevice.h +++ b/Space-Invaders/Header/GameService.h @@ -1,17 +1,25 @@ #pragma once +#include +#include "../Header/ServiceLocator.h" class GameService { private: - void initialize(); // Handles game initialization. + + ServiceLocator* service_locator; + sf::RenderWindow* game_window; + + void initialize(); + 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. + ~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 new file mode 100644 index 000000000..05399d5d4 --- /dev/null +++ b/Space-Invaders/Header/GraphicService.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +class GraphicService +{ +private: + + const std::string game_window_title = "Outscal Presents - 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 +}; diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h index 16e3410d6..591050055 100644 --- a/Space-Invaders/Header/ServiceLocator.h +++ b/Space-Invaders/Header/ServiceLocator.h @@ -1,24 +1,16 @@ #pragma once - -// ServiceLocator Class Summary: This class manages access to various services in the application. -// include relevant headers files +#include "../Header/GraphicService.h" class ServiceLocator { private: // Private Attributes: - // - event_service: Manages event-related functionalities. - // - graphic_service: Handles graphics-related tasks. - // .......................... - // .......................... + GraphicService* graphic_service; // Private Constructor and Destructor: - - // Constructor for initializing the ServiceLocator. ServiceLocator(); - - // Destructor for cleaning up resources upon object deletion. - ~ServiceLocator(); + // Constructor for initializing the ServiceLocator. + ~ServiceLocator(); // Destructor for cleaning up resources upon object deletion. // Private Methods: void createServices(); // Creates instances of all services. @@ -26,14 +18,12 @@ class ServiceLocator public: // Public Methods: - static ServiceLocator* getInstance(); // Provides a method to access the unique ServiceLocator instance (object). We will discuss this later. - + static ServiceLocator* getInstance(); // Provides a method to access the unique ServiceLocator instance (object). 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 + GraphicService* getGraphicService(); }; diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp new file mode 100644 index 000000000..dee9b2921 --- /dev/null +++ b/Space-Invaders/Source/GameService.cpp @@ -0,0 +1,56 @@ +#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->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/GameSevice.cpp b/Space-Invaders/Source/GameSevice.cpp deleted file mode 100644 index 102fd0259..000000000 --- a/Space-Invaders/Source/GameSevice.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include -#include "../Header/GameSevice.h" - - - - - void GameService::initialize() - { - //... Get things running - } - - void GameService::destroy() - { - //cleanup resources - } - - GameService::GameService() - { - //constructor - } - - GameService::~GameService() - { - //destructor - } - - void GameService::ignite() - { - //starts the game - } - - void GameService::update() - { - // Updates the game logic and game state. - } - - void GameService::render() - { - // Renders each frame of the game. - } - - bool GameService::isRunning() - { - // Checks if the game is currently running. - return false; //default return - } - - - - diff --git a/Space-Invaders/Source/GraphicService.cpp b/Space-Invaders/Source/GraphicService.cpp new file mode 100644 index 000000000..93d150b96 --- /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 4db281330..7070c3c83 100644 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -1,43 +1,47 @@ -#include #include "../Header/ServiceLocator.h" - -ServiceLocator::ServiceLocator() -{ - //construct +// 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() - { - //deconstructor - } +// Destructor: Cleans up resources by clearing all services. +ServiceLocator::~ServiceLocator() { + clearAllServices(); // Call clearAllServices to delete any allocated services +} -void ServiceLocator::createServices() -{ - // Creates instances of all services. +// Creates service instances, specifically the graphic service in this case. +void ServiceLocator::createServices() { + graphic_service = new GraphicService(); // Dynamically create a GraphicService instance } -void ServiceLocator::clearAllServices() -{ - // Deletes and deallocates memory for all services. +// 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::getInstance() -{ - // Provides a method to access the unique ServiceLocator instance (object). We will discuss this later. +// 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::initialize() -{ - // Initializes the ServiceLocator. +// Calls initialize on the graphic service, readying it for use. +void ServiceLocator::initialize() { + graphic_service->initialize(); // Initialize graphic service } -void ServiceLocator::update() -{ - // Updates all services. +// Updates the state of the graphic service. +void ServiceLocator::update() { + graphic_service->update(); // Update graphic service } -void ServiceLocator::render() -{ - // Renders using the services. +// 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; } diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 3f4f1a171..4a98f5175 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -1,4 +1,4 @@ - + @@ -133,11 +133,13 @@ - + + - + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 23552d099..2d13324df 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -18,19 +18,25 @@ Source Files - + Source Files Source Files + + Source Files + - + Header Files Header Files + + Header Files + \ No newline at end of file From c8bef7f3a49a32e527b06592745beeecb5edb489 Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Mon, 10 Jun 2024 23:32:27 +0530 Subject: [PATCH 8/8] Added Event Services --- Space-Invaders/Header/EventService.cpp | 42 ++++++++++++++++++ Space-Invaders/Header/EventService.h | 27 ++++++++++++ Space-Invaders/Header/ServiceLocator.h | 4 ++ Space-Invaders/Source/EventServices.cpp | 0 Space-Invaders/Source/GameService.cpp | 8 ++++ Space-Invaders/Source/ServiceLocator.cpp | 15 +++++++ Space-Invaders/Source/ServiceLocatorOld.cpp | 47 +++++++++++++++++++++ Space-Invaders/Space-Invaders.vcxproj | 4 ++ 8 files changed, 147 insertions(+) create mode 100644 Space-Invaders/Header/EventService.cpp create mode 100644 Space-Invaders/Header/EventService.h create mode 100644 Space-Invaders/Source/EventServices.cpp create mode 100644 Space-Invaders/Source/ServiceLocatorOld.cpp diff --git a/Space-Invaders/Header/EventService.cpp b/Space-Invaders/Header/EventService.cpp new file mode 100644 index 000000000..c7f9d4a29 --- /dev/null +++ b/Space-Invaders/Header/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; } diff --git a/Space-Invaders/Header/EventService.h b/Space-Invaders/Header/EventService.h new file mode 100644 index 000000000..dde134f20 --- /dev/null +++ b/Space-Invaders/Header/EventService.h @@ -0,0 +1,27 @@ +#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(); + +}; diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h index 591050055..f18f0ee2b 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" class ServiceLocator { @@ -7,6 +8,8 @@ class ServiceLocator // Private Attributes: GraphicService* graphic_service; + EventService* event_service; + // Private Constructor and Destructor: ServiceLocator(); // Constructor for initializing the ServiceLocator. @@ -25,5 +28,6 @@ class ServiceLocator // Methods to Get Specific Services: GraphicService* getGraphicService(); + EventService* getEventService(); }; diff --git a/Space-Invaders/Source/EventServices.cpp b/Space-Invaders/Source/EventServices.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp index dee9b2921..499a2d13a 100644 --- a/Space-Invaders/Source/GameService.cpp +++ b/Space-Invaders/Source/GameService.cpp @@ -1,6 +1,7 @@ #include "../Header/GameService.h" #include "../Header/GraphicService.h" + // Constructor: Initializes pointers to null. GameService::GameService() { service_locator = nullptr; // Set service locator to null @@ -39,8 +40,15 @@ void GameService::destroy() void GameService::update() { service_locator->update(); // Call update on the service locator which then updates all its managed services + + // Process Events. + service_locator->getEventService()->processEvents(); + + // Update Game Logic. + service_locator->update(); } + // Clears the window then display it. void GameService::render() { // Clears the game window with the background color provided by the graphic service diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp index 7070c3c83..f46815a63 100644 --- a/Space-Invaders/Source/ServiceLocator.cpp +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -3,6 +3,7 @@ // Constructor: Initializes the graphic_service pointer to null and creates services. ServiceLocator::ServiceLocator() { graphic_service = nullptr; // Initialize graphic_service to null + event_service = nullptr; createServices(); // Call createServices to instantiate services } @@ -14,11 +15,13 @@ ServiceLocator::~ServiceLocator() { // Creates service instances, specifically the graphic service in this case. void ServiceLocator::createServices() { graphic_service = new GraphicService(); // Dynamically create a GraphicService instance + 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 + delete(event_service); graphic_service = nullptr; // Reset pointer to null to avoid dangling pointer } @@ -31,11 +34,13 @@ ServiceLocator* ServiceLocator::getInstance() { // Calls initialize on the graphic service, readying it for use. void ServiceLocator::initialize() { graphic_service->initialize(); // Initialize graphic service + event_service->initialize(); } // Updates the state of the graphic service. void ServiceLocator::update() { graphic_service->update(); // Update graphic service + event_service->update(); } // Renders using the graphic service. @@ -45,3 +50,13 @@ void ServiceLocator::render() { // Returns a pointer to the currently set graphic service. GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } +EventService* ServiceLocator::getEventService() { return event_service; } + + + + + + + + + diff --git a/Space-Invaders/Source/ServiceLocatorOld.cpp b/Space-Invaders/Source/ServiceLocatorOld.cpp new file mode 100644 index 000000000..7070c3c83 --- /dev/null +++ b/Space-Invaders/Source/ServiceLocatorOld.cpp @@ -0,0 +1,47 @@ +#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 +} + +// Destructor: Cleans up resources by clearing all services. +ServiceLocator::~ServiceLocator() { + clearAllServices(); // Call clearAllServices to delete any allocated services +} + +// Creates service instances, specifically the graphic service in this case. +void ServiceLocator::createServices() { + graphic_service = new GraphicService(); // Dynamically create a GraphicService instance +} + +// 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 +} + +// 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 +} + +// Calls initialize on the graphic service, readying it for use. +void ServiceLocator::initialize() { + graphic_service->initialize(); // Initialize graphic service +} + +// 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; } diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 4a98f5175..ffe923c95 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,12 +132,16 @@ + + + +