From 46d3eb914ebec96c25f0997df65c4dece32700c2 Mon Sep 17 00:00:00 2001 From: Tarun Date: Fri, 26 Jul 2024 19:48:55 +0530 Subject: [PATCH 01/15] The whole file gets corrupted thats why i have to write all programs till player ship again --- Space-Invaders/Headers/GameService.cpp | 59 +++++++++++++++++++ Space-Invaders/Headers/GameService.h | 26 ++++++++ Space-Invaders/Headers/GraphicService.cpp | 56 ++++++++++++++++++ Space-Invaders/Headers/GraphicService.h | 38 ++++++++++++ Space-Invaders/Headers/ServiceLocator.cpp | 49 +++++++++++++++ Space-Invaders/Headers/ServiceLocator.h | 29 +++++++++ Space-Invaders/Space-Invaders.vcxproj | 8 +++ Space-Invaders/Space-Invaders.vcxproj.filters | 20 +++++++ Space-Invaders/main.cpp | 19 +++++- 9 files changed, 302 insertions(+), 2 deletions(-) create mode 100644 Space-Invaders/Headers/GameService.cpp create mode 100644 Space-Invaders/Headers/GameService.h create mode 100644 Space-Invaders/Headers/GraphicService.cpp create mode 100644 Space-Invaders/Headers/GraphicService.h create mode 100644 Space-Invaders/Headers/ServiceLocator.cpp create mode 100644 Space-Invaders/Headers/ServiceLocator.h diff --git a/Space-Invaders/Headers/GameService.cpp b/Space-Invaders/Headers/GameService.cpp new file mode 100644 index 000000000..4607039c2 --- /dev/null +++ b/Space-Invaders/Headers/GameService.cpp @@ -0,0 +1,59 @@ +#include "GameService.h" +#include"GraphicService.h" + +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/Headers/GameService.h b/Space-Invaders/Headers/GameService.h new file mode 100644 index 000000000..c09fa800f --- /dev/null +++ b/Space-Invaders/Headers/GameService.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include "ServiceLocator.h" + +class GameService +{ +private: + + 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. + + 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/Headers/GraphicService.cpp b/Space-Invaders/Headers/GraphicService.cpp new file mode 100644 index 000000000..1303689b7 --- /dev/null +++ b/Space-Invaders/Headers/GraphicService.cpp @@ -0,0 +1,56 @@ +#include"ServiceLocator.h" +#include"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; +} diff --git a/Space-Invaders/Headers/GraphicService.h b/Space-Invaders/Headers/GraphicService.h new file mode 100644 index 000000000..3a6cac6df --- /dev/null +++ b/Space-Invaders/Headers/GraphicService.h @@ -0,0 +1,38 @@ + +#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 +}; \ No newline at end of file diff --git a/Space-Invaders/Headers/ServiceLocator.cpp b/Space-Invaders/Headers/ServiceLocator.cpp new file mode 100644 index 000000000..fd1922572 --- /dev/null +++ b/Space-Invaders/Headers/ServiceLocator.cpp @@ -0,0 +1,49 @@ +#include "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; } \ No newline at end of file diff --git a/Space-Invaders/Headers/ServiceLocator.h b/Space-Invaders/Headers/ServiceLocator.h new file mode 100644 index 000000000..84028c811 --- /dev/null +++ b/Space-Invaders/Headers/ServiceLocator.h @@ -0,0 +1,29 @@ +#pragma once +#include "GraphicService.h" + +class ServiceLocator +{ +private: + // Private Attributes: + GraphicService* graphic_service; + + // Private Constructor and Destructor: + ServiceLocator(); + // Constructor for initializing the ServiceLocator. + ~ServiceLocator(); // Destructor for cleaning up resources upon object deletion. + + // 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). + void initialize(); // Initializes the ServiceLocator. + void update(); // Updates all services. + void render(); // Renders using the services. + + // Methods to Get Specific Services: + GraphicService* getGraphicService(); + +}; diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 6f7fa388d..fa705159d 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,8 +132,16 @@ + + + + + + + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index ce0c35ccf..c1a3d6972 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -18,5 +18,25 @@ Source Files + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 7d5f90dff..1a2cf9819 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,5 +1,20 @@ - +#include "Headers/GameService.h" int main() { + + GameService* game_service = new GameService(); + + game_service->ignite(); + + while (game_service->isRunning()) + { + game_service->update(); + game_service->render(); + + + + + } return 0; -} \ No newline at end of file +} + From ef66188d0746f79e04d8e20d57328872c815dd6b Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 28 Jul 2024 15:12:44 +0530 Subject: [PATCH 02/15] IMPLEMENTED POLLING EVENT --- Space-Invaders/Headers/EventService.cpp | 0 Space-Invaders/Headers/EventService.h | 5 +++++ Space-Invaders/Space-Invaders.vcxproj | 1 + Space-Invaders/Space-Invaders.vcxproj.filters | 3 +++ 4 files changed, 9 insertions(+) create mode 100644 Space-Invaders/Headers/EventService.cpp create mode 100644 Space-Invaders/Headers/EventService.h diff --git a/Space-Invaders/Headers/EventService.cpp b/Space-Invaders/Headers/EventService.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Space-Invaders/Headers/EventService.h b/Space-Invaders/Headers/EventService.h new file mode 100644 index 000000000..09dfcb4c7 --- /dev/null +++ b/Space-Invaders/Headers/EventService.h @@ -0,0 +1,5 @@ +#pragma once +#include +#include + +class E \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index fa705159d..0ee183c2a 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -138,6 +138,7 @@ + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index c1a3d6972..5f6eba855 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -38,5 +38,8 @@ Header Files + + Header Files + \ No newline at end of file From 5673590201775fa44acc60f8fd80cc34cfa611d2 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 28 Jul 2024 16:04:40 +0530 Subject: [PATCH 03/15] added the player spirite and also added the movement --- Space-Invaders/Headers/EventService.cpp | 49 ++++++++++++++ Space-Invaders/Headers/EventService.h | 31 ++++++++- Space-Invaders/Headers/GameService.cpp | 2 + Space-Invaders/Headers/GraphicService.cpp | 12 +++- Space-Invaders/Headers/GraphicService.h | 1 + Space-Invaders/Headers/PlayerService.cpp | 66 +++++++++++++++++++ Space-Invaders/Headers/PlayerService.h | 38 +++++++++++ Space-Invaders/Headers/ServiceLocator.cpp | 31 +++++++-- Space-Invaders/Headers/ServiceLocator.h | 6 ++ Space-Invaders/Space-Invaders.vcxproj | 3 + Space-Invaders/Space-Invaders.vcxproj.filters | 9 +++ 11 files changed, 239 insertions(+), 9 deletions(-) create mode 100644 Space-Invaders/Headers/PlayerService.cpp create mode 100644 Space-Invaders/Headers/PlayerService.h diff --git a/Space-Invaders/Headers/EventService.cpp b/Space-Invaders/Headers/EventService.cpp index e69de29bb..a5c4965e3 100644 --- a/Space-Invaders/Headers/EventService.cpp +++ b/Space-Invaders/Headers/EventService.cpp @@ -0,0 +1,49 @@ +#include"ServiceLocator.h" +#include"GraphicService.h" +#include"EventService.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; } + +bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } +bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } + diff --git a/Space-Invaders/Headers/EventService.h b/Space-Invaders/Headers/EventService.h index 09dfcb4c7..467eff7ca 100644 --- a/Space-Invaders/Headers/EventService.h +++ b/Space-Invaders/Headers/EventService.h @@ -1,5 +1,30 @@ #pragma once -#include -#include +#include +#include -class E \ No newline at end of file +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(); + bool pressedRightKey(); + +}; \ No newline at end of file diff --git a/Space-Invaders/Headers/GameService.cpp b/Space-Invaders/Headers/GameService.cpp index 4607039c2..960d7acb4 100644 --- a/Space-Invaders/Headers/GameService.cpp +++ b/Space-Invaders/Headers/GameService.cpp @@ -37,6 +37,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/Headers/GraphicService.cpp b/Space-Invaders/Headers/GraphicService.cpp index 1303689b7..f6987679e 100644 --- a/Space-Invaders/Headers/GraphicService.cpp +++ b/Space-Invaders/Headers/GraphicService.cpp @@ -1,10 +1,12 @@ #include"ServiceLocator.h" #include"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. @@ -35,10 +37,16 @@ void GraphicService::onDestroy() { } // Placeholder function for game update logic. -void GraphicService::update() { } +void GraphicService::update() { + + +} + // Placeholder function for game rendering logic. -void GraphicService::render() { } +void GraphicService::render() { + + } // Checks if the game window is currently open. bool GraphicService::isGameWindowOpen() { diff --git a/Space-Invaders/Headers/GraphicService.h b/Space-Invaders/Headers/GraphicService.h index 3a6cac6df..68ca09cef 100644 --- a/Space-Invaders/Headers/GraphicService.h +++ b/Space-Invaders/Headers/GraphicService.h @@ -6,6 +6,7 @@ class GraphicService { private: + const std::string game_window_title = "Outscal Presents - Alien Invader"; diff --git a/Space-Invaders/Headers/PlayerService.cpp b/Space-Invaders/Headers/PlayerService.cpp new file mode 100644 index 000000000..49be2d11a --- /dev/null +++ b/Space-Invaders/Headers/PlayerService.cpp @@ -0,0 +1,66 @@ +#include "PlayerService.h" +#include"ServiceLocator.h" + + +void PlayerService::initializePlayerSprite() +{ + if (player_texture.loadFromFile(player_texture_path)) { + player_sprite.setTexture(player_texture); + } +} + +void PlayerService::processPlayerInput() +{ + + EventService* checkkey = ServiceLocator::getInstance()->getEventService(); + if (checkkey->isKeyboardEvent()) { + if (checkkey->pressedLeftKey()) { + move(-1.0 * getMoveSpeed()); + } + + if (checkkey->pressedRightKey()) { + move(1.0 * getMoveSpeed()); + } + } +} + +PlayerService::PlayerService() +{ + game_window = nullptr; +} + +PlayerService::~PlayerService() +{ +} + +void PlayerService::initialize() +{ + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializePlayerSprite(); +} + +void PlayerService::update() +{ + processPlayerInput(); + player_sprite.setPosition(getPlayerPosition()); +} + +void PlayerService::render() +{ + game_window->draw(player_sprite); +} + +void PlayerService::move(float offsetX) +{ + position.x += offsetX; +} + +int PlayerService::getMoveSpeed() +{ + return movement_speed; +} + +sf::Vector2f PlayerService::getPlayerPosition() +{ + return position; +} diff --git a/Space-Invaders/Headers/PlayerService.h b/Space-Invaders/Headers/PlayerService.h new file mode 100644 index 000000000..a9440127c --- /dev/null +++ b/Space-Invaders/Headers/PlayerService.h @@ -0,0 +1,38 @@ +#pragma once +#include +class PlayerService +{ + +private: + + + + int health = 3; + sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); + int movement_speed = 5; + 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(); + +}; diff --git a/Space-Invaders/Headers/ServiceLocator.cpp b/Space-Invaders/Headers/ServiceLocator.cpp index fd1922572..0a81bc1aa 100644 --- a/Space-Invaders/Headers/ServiceLocator.cpp +++ b/Space-Invaders/Headers/ServiceLocator.cpp @@ -1,10 +1,12 @@ #include "ServiceLocator.h" - +#include"EventService.h" // Constructor: Initializes the graphic_service pointer to null and creates services. ServiceLocator::ServiceLocator() { - graphic_service = nullptr; // Initialize graphic_service to null + graphic_service = nullptr;// Initialize graphic_service to null + event_service = nullptr; + player_service = nullptr; createServices(); // Call createServices to instantiate services } @@ -16,12 +18,17 @@ 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(); + player_service = new PlayerService(); + } // 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 + delete(event_service); + delete(player_service); + } // Returns a pointer to ServiceLocator. @@ -33,17 +40,33 @@ 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(); + player_service->initialize(); } // Updates the state of the graphic service. void ServiceLocator::update() { graphic_service->update(); // Update graphic service + event_service->update(); + player_service->update(); } // Renders using the graphic service. void ServiceLocator::render() { graphic_service->render(); // Render graphic service + player_service->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; +} + +PlayerService* ServiceLocator::getplayerservice() { + return player_service; +} + + + diff --git a/Space-Invaders/Headers/ServiceLocator.h b/Space-Invaders/Headers/ServiceLocator.h index 84028c811..c8a41b91e 100644 --- a/Space-Invaders/Headers/ServiceLocator.h +++ b/Space-Invaders/Headers/ServiceLocator.h @@ -1,11 +1,15 @@ #pragma once #include "GraphicService.h" +#include"EventService.h" +#include"PlayerService.h" class ServiceLocator { private: // Private Attributes: GraphicService* graphic_service; + EventService* event_service; + PlayerService* player_service; // Private Constructor and Destructor: ServiceLocator(); @@ -25,5 +29,7 @@ class ServiceLocator // Methods to Get Specific Services: GraphicService* getGraphicService(); + EventService* getEventService(); + PlayerService* getplayerservice(); }; diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 0ee183c2a..efb75e465 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,8 +132,10 @@ + + @@ -141,6 +143,7 @@ + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 5f6eba855..73c3a15d7 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -27,6 +27,12 @@ Source Files + + Source Files + + + Source Files + @@ -41,5 +47,8 @@ Header Files + + Header Files + \ No newline at end of file From cd4e9cd77a6191b9f0e2cd1ee701371a64afac12 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 28 Jul 2024 18:12:06 +0530 Subject: [PATCH 04/15] in this commit we are adding the time dependency in the sip for the better and movement of the ship --- Space-Invaders/Headers/ServiceLocator.cpp | 14 ++++++- Space-Invaders/Headers/ServiceLocator.h | 4 +- Space-Invaders/Headers/TimeService .cpp | 42 +++++++++++++++++++ Space-Invaders/Headers/TimeService .h | 23 ++++++++++ Space-Invaders/Space-Invaders.vcxproj | 2 + Space-Invaders/Space-Invaders.vcxproj.filters | 6 +++ 6 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 Space-Invaders/Headers/TimeService .cpp create mode 100644 Space-Invaders/Headers/TimeService .h diff --git a/Space-Invaders/Headers/ServiceLocator.cpp b/Space-Invaders/Headers/ServiceLocator.cpp index 0a81bc1aa..21ba91f85 100644 --- a/Space-Invaders/Headers/ServiceLocator.cpp +++ b/Space-Invaders/Headers/ServiceLocator.cpp @@ -7,6 +7,7 @@ ServiceLocator::ServiceLocator() { graphic_service = nullptr;// Initialize graphic_service to null event_service = nullptr; player_service = nullptr; + time_Service = nullptr; createServices(); // Call createServices to instantiate services } @@ -20,7 +21,7 @@ void ServiceLocator::createServices() { graphic_service = new GraphicService(); // Dynamically create a GraphicService instance event_service = new EventService(); player_service = new PlayerService(); - + time_Service = new TimeService(); } // Deletes allocated services to prevent memory leaks, specifically the graphic service. @@ -28,6 +29,7 @@ void ServiceLocator::clearAllServices() { delete(graphic_service); // Delete the graphic_service instance delete(event_service); delete(player_service); + delete(time_Service); } @@ -42,6 +44,7 @@ void ServiceLocator::initialize() { graphic_service->initialize(); // Initialize graphic service event_service->initialize(); player_service->initialize(); + time_Service->intialize(); } // Updates the state of the graphic service. @@ -49,8 +52,10 @@ void ServiceLocator::update() { graphic_service->update(); // Update graphic service event_service->update(); player_service->update(); + time_Service->update(); } + // Renders using the graphic service. void ServiceLocator::render() { graphic_service->render(); // Render graphic service @@ -68,5 +73,10 @@ PlayerService* ServiceLocator::getplayerservice() { return player_service; } - +TimeService* ServiceLocator::gettimeservice() +{ + return time_Service; +} + + diff --git a/Space-Invaders/Headers/ServiceLocator.h b/Space-Invaders/Headers/ServiceLocator.h index c8a41b91e..86a93eed9 100644 --- a/Space-Invaders/Headers/ServiceLocator.h +++ b/Space-Invaders/Headers/ServiceLocator.h @@ -2,6 +2,7 @@ #include "GraphicService.h" #include"EventService.h" #include"PlayerService.h" +#include"TimeService .h" class ServiceLocator { @@ -10,6 +11,7 @@ class ServiceLocator GraphicService* graphic_service; EventService* event_service; PlayerService* player_service; + TimeService* time_Service; // Private Constructor and Destructor: ServiceLocator(); @@ -31,5 +33,5 @@ class ServiceLocator GraphicService* getGraphicService(); EventService* getEventService(); PlayerService* getplayerservice(); - + TimeService* gettimeservice(); }; diff --git a/Space-Invaders/Headers/TimeService .cpp b/Space-Invaders/Headers/TimeService .cpp new file mode 100644 index 000000000..0c719b841 --- /dev/null +++ b/Space-Invaders/Headers/TimeService .cpp @@ -0,0 +1,42 @@ +#include "TimeService .h" + +float TimeService::calculate_delta_time() +{ + // 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. + // We will learn aboit how this works in detail later. + return static_cast(delta) / static_cast(1000000); +} + +void TimeService::updatePreviousTime() +{ + + previous_time = std::chrono::steady_clock::now(); +} + +void TimeService::updateDeltaTime() +{ + delta_time = calculate_delta_time(); + updatePreviousTime(); + +} + +void TimeService::update() +{ + updateDeltaTime(); +} + +void TimeService::intialize() +{ + previous_time = std::chrono::steady_clock::now(); + delta_time = 0; +} + +float TimeService::getdeltatime() +{ + return delta_time; +} + diff --git a/Space-Invaders/Headers/TimeService .h b/Space-Invaders/Headers/TimeService .h new file mode 100644 index 000000000..2a4183d17 --- /dev/null +++ b/Space-Invaders/Headers/TimeService .h @@ -0,0 +1,23 @@ +#pragma once +//Capture the current time at the beginning of a frame +//Capture the current time again at the beginning of the next frame +//Calculate the delta time by subtracting the previous frame's start time from the current frame's start time. +//Update the current frame's start time to become the previous frame's start time so as to prepare for the next frame. +#include + +class TimeService { +private: + std::chrono::time_point previous_time; + + float delta_time;//to store the value of delta time + float calculate_delta_time();//calculate time by subtracting the previous time from the current time + void updatePreviousTime(); // finally update the current time to be previous time + void updateDeltaTime(); +public: + void update(); + void intialize(); + float getdeltatime(); + + + +}; diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index efb75e465..f85d9a4d0 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -137,6 +137,7 @@ + @@ -145,6 +146,7 @@ + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 73c3a15d7..219bacc87 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -33,6 +33,9 @@ Source Files + + Source Files + @@ -50,5 +53,8 @@ Header Files + + Header Files + \ No newline at end of file From b02ba2867db71a6320aba115f41d9efc20953ff5 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 28 Jul 2024 19:50:32 +0530 Subject: [PATCH 05/15] in this commit we are implementing the mvc model for the player service --- Space-Invaders/Headers/GraphicService.cpp | 3 ++ Space-Invaders/Headers/GraphicService.h | 2 + Space-Invaders/Headers/PlayerService.cpp | 17 +++++-- Space-Invaders/Headers/PlayerService.h | 6 ++- .../Headers/player/PlayerController.h | 1 + Space-Invaders/Headers/player/PlayerModel.cpp | 50 ++++++++++++++++++ Space-Invaders/Headers/player/PlayerModel.h | 51 +++++++++++++++++++ Space-Invaders/Headers/player/PlayerView.h | 1 + Space-Invaders/Space-Invaders.vcxproj | 4 ++ Space-Invaders/Space-Invaders.vcxproj.filters | 12 +++++ 10 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 Space-Invaders/Headers/player/PlayerController.h create mode 100644 Space-Invaders/Headers/player/PlayerModel.cpp create mode 100644 Space-Invaders/Headers/player/PlayerModel.h create mode 100644 Space-Invaders/Headers/player/PlayerView.h diff --git a/Space-Invaders/Headers/GraphicService.cpp b/Space-Invaders/Headers/GraphicService.cpp index f6987679e..e4a35584d 100644 --- a/Space-Invaders/Headers/GraphicService.cpp +++ b/Space-Invaders/Headers/GraphicService.cpp @@ -17,8 +17,11 @@ 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->setFramerateLimit(framerate); } + // 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 diff --git a/Space-Invaders/Headers/GraphicService.h b/Space-Invaders/Headers/GraphicService.h index 68ca09cef..570c7f584 100644 --- a/Space-Invaders/Headers/GraphicService.h +++ b/Space-Invaders/Headers/GraphicService.h @@ -15,6 +15,8 @@ class GraphicService const sf::Color window_color = sf::Color::Blue; + const int framerate = 60; + sf::VideoMode* video_mode; // ptr to video mode sf::RenderWindow* game_window; // ptr to a RenderWindow diff --git a/Space-Invaders/Headers/PlayerService.cpp b/Space-Invaders/Headers/PlayerService.cpp index 49be2d11a..e8932f90d 100644 --- a/Space-Invaders/Headers/PlayerService.cpp +++ b/Space-Invaders/Headers/PlayerService.cpp @@ -15,11 +15,11 @@ void PlayerService::processPlayerInput() EventService* checkkey = ServiceLocator::getInstance()->getEventService(); if (checkkey->isKeyboardEvent()) { if (checkkey->pressedLeftKey()) { - move(-1.0 * getMoveSpeed()); + moveLef(); } if (checkkey->pressedRightKey()) { - move(1.0 * getMoveSpeed()); + moveRight(); } } } @@ -50,11 +50,20 @@ void PlayerService::render() game_window->draw(player_sprite); } -void PlayerService::move(float offsetX) +void PlayerService::moveLef() { - position.x += offsetX; + + position.x -= ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); } +void PlayerService::moveRight() +{ + + position.x += ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); +} + + + int PlayerService::getMoveSpeed() { return movement_speed; diff --git a/Space-Invaders/Headers/PlayerService.h b/Space-Invaders/Headers/PlayerService.h index a9440127c..997402155 100644 --- a/Space-Invaders/Headers/PlayerService.h +++ b/Space-Invaders/Headers/PlayerService.h @@ -1,5 +1,6 @@ #pragma once #include +#include"TimeService .h" class PlayerService { @@ -9,7 +10,7 @@ class PlayerService int health = 3; sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); - int movement_speed = 5; + float movement_speed = 5.0f; int player_score = 0; const sf::String player_texture_path = "assets/textures/player_ship.png"; @@ -31,7 +32,8 @@ class PlayerService void update(); void render(); - void move(float offsetX); + void moveLef(); + void moveRight(); int getMoveSpeed(); sf::Vector2f getPlayerPosition(); diff --git a/Space-Invaders/Headers/player/PlayerController.h b/Space-Invaders/Headers/player/PlayerController.h new file mode 100644 index 000000000..6f70f09be --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerController.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Headers/player/PlayerModel.cpp b/Space-Invaders/Headers/player/PlayerModel.cpp new file mode 100644 index 000000000..bc266d1d0 --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerModel.cpp @@ -0,0 +1,50 @@ +#include"PlayerModel.h" + +PlayerModel::PlayerModel() { } + +PlayerModel::~PlayerModel() { } + +void PlayerModel::initialize() { reset(); } + +void PlayerModel::reset() +{ + player_position = initial_player_position; + player_score = 0; +} + +sf::Vector2f PlayerModel::getPlayerPosition() +{ + return player_position; +} + +void PlayerModel::setPlayerPosition(sf::Vector2f position) +{ + player_position = position; +} + +PlayerState PlayerModel::getplayerstate() +{ + return player_state; +} + +void PlayerModel::setplayerstate(PlayerState state) + +{ + player_state = state; + +} + + + + + + + + + + + + + + + diff --git a/Space-Invaders/Headers/player/PlayerModel.h b/Space-Invaders/Headers/player/PlayerModel.h new file mode 100644 index 000000000..5d0ba8e79 --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerModel.h @@ -0,0 +1,51 @@ +#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); //new var + sf::Vector2f player_position; //new var + PlayerState player_state; + + int player_score = 0; + + +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(); //new method + + + + + //getters and setters + sf::Vector2f getPlayerPosition(); + void setPlayerPosition(sf::Vector2f position); + + PlayerState getplayerstate(); + + + void setplayerstate(PlayerState state); + + + +}; \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerView.h b/Space-Invaders/Headers/player/PlayerView.h new file mode 100644 index 000000000..6f70f09be --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerView.h @@ -0,0 +1 @@ +#pragma once diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index f85d9a4d0..87d1530fc 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -136,6 +136,7 @@ + @@ -145,6 +146,9 @@ + + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index 219bacc87..a5307cb2f 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -36,6 +36,9 @@ Source Files + + Source Files + @@ -56,5 +59,14 @@ Header Files + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file From b1564b096b29492f74d2ddd3e771b29d05589be0 Mon Sep 17 00:00:00 2001 From: Tarun Date: Mon, 29 Jul 2024 18:29:50 +0530 Subject: [PATCH 06/15] IN THSI COMMIT WE HAVE IMPLEMENTED THE MVC MODEL AND WE ENCOUNTER SOME OF THE ERRORS WHICH WE ARE GOING TO FIX IN NEXT MODULE --- Space-Invaders/Headers/PlayerService.cpp | 4 ++ Space-Invaders/Headers/PlayerService.h | 3 + .../Headers/player/PlayerController.cpp | 67 +++++++++++++++++++ .../Headers/player/PlayerController.h | 30 ++++++++- Space-Invaders/Headers/player/PlayerView.cpp | 42 ++++++++++++ Space-Invaders/Headers/player/PlayerView.h | 31 +++++++++ Space-Invaders/Space-Invaders.vcxproj | 2 + Space-Invaders/Space-Invaders.vcxproj.filters | 6 ++ 8 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 Space-Invaders/Headers/player/PlayerController.cpp create mode 100644 Space-Invaders/Headers/player/PlayerView.cpp diff --git a/Space-Invaders/Headers/PlayerService.cpp b/Space-Invaders/Headers/PlayerService.cpp index e8932f90d..d06939a78 100644 --- a/Space-Invaders/Headers/PlayerService.cpp +++ b/Space-Invaders/Headers/PlayerService.cpp @@ -27,6 +27,7 @@ void PlayerService::processPlayerInput() PlayerService::PlayerService() { game_window = nullptr; + pcontroller = nullptr; } PlayerService::~PlayerService() @@ -36,17 +37,20 @@ PlayerService::~PlayerService() void PlayerService::initialize() { game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + pcontroller->intialize(); initializePlayerSprite(); } void PlayerService::update() { processPlayerInput(); + pcontroller->update(); player_sprite.setPosition(getPlayerPosition()); } void PlayerService::render() { + pcontroller->render(); game_window->draw(player_sprite); } diff --git a/Space-Invaders/Headers/PlayerService.h b/Space-Invaders/Headers/PlayerService.h index 997402155..a1cdb65d1 100644 --- a/Space-Invaders/Headers/PlayerService.h +++ b/Space-Invaders/Headers/PlayerService.h @@ -1,6 +1,7 @@ #pragma once #include #include"TimeService .h" +#include"../Headers/player/PlayerController.h" class PlayerService { @@ -23,6 +24,8 @@ class PlayerService void initializePlayerSprite(); void processPlayerInput(); + PlayerController* pcontroller; + public: PlayerService(); diff --git a/Space-Invaders/Headers/player/PlayerController.cpp b/Space-Invaders/Headers/player/PlayerController.cpp new file mode 100644 index 000000000..4be7a2a9c --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerController.cpp @@ -0,0 +1,67 @@ +#include "PlayerController.h" +#include"../EventService.h" +#include"../ServiceLocator.h" +#include +void PlayerController::processinput() +{ + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + processmoveright(); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + processmoveleft(); + } +} + +void PlayerController::processmoveleft() +{ + + sf::Vector2f current_position = model->getPlayerPosition(); + current_position.x -= model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + current_position.x = std::max(current_position.x, model->left_most_position.x); + model->setPlayerPosition(current_position); +} + + +void PlayerController::processmoveright() +{ + sf::Vector2f current_position = model->getPlayerPosition(); + current_position.x += model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + current_position.x = std::min(current_position.x, model->right_most_position.x); + model->setPlayerPosition(current_position); + +} + +PlayerController::PlayerController() +{ + + model = new PlayerModel(); + view = new PlayerView(); +} + +PlayerController::~PlayerController() +{ + delete (view); + delete (model); +} + +void PlayerController::intialize() +{ + model->initialize(); + view->initialize(); +} + +void PlayerController::update() +{ + view->update(); +} + +void PlayerController::render() +{ + view->render(); +} + +sf::Vector2f PlayerController::getPlayerPosition() +{ + return model->getPlayerPosition(); +} diff --git a/Space-Invaders/Headers/player/PlayerController.h b/Space-Invaders/Headers/player/PlayerController.h index 6f70f09be..924e5b845 100644 --- a/Space-Invaders/Headers/player/PlayerController.h +++ b/Space-Invaders/Headers/player/PlayerController.h @@ -1 +1,29 @@ -#pragma once +#pragma one +#include +#include"PlayerModel.h" +#include"PlayerView.h" + +class PlayerController { + +private: + + PlayerModel* model; + PlayerView* view; + + + void processinput(); + void processmoveleft(); + void processmoveright(); +public: + PlayerController(); + ~PlayerController(); + void intialize(); + void update(); + void render(); + + + + + sf::Vector2f getPlayerPosition(); + +}; diff --git a/Space-Invaders/Headers/player/PlayerView.cpp b/Space-Invaders/Headers/player/PlayerView.cpp new file mode 100644 index 000000000..ceb9af683 --- /dev/null +++ b/Space-Invaders/Headers/player/PlayerView.cpp @@ -0,0 +1,42 @@ +#include"PlayerView.h" +#include"../ServiceLocator.h" +PlayerView::PlayerView() { } + +PlayerView::~PlayerView() { } + +void PlayerView::initialize(PlayerController* controller) +{ + + player_controller = controller; + 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() +{ + //empty for now +} + +void PlayerView::render() +{ + game_window->draw(player_sprite); +} \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerView.h b/Space-Invaders/Headers/player/PlayerView.h index 6f70f09be..23dd3dad1 100644 --- a/Space-Invaders/Headers/player/PlayerView.h +++ b/Space-Invaders/Headers/player/PlayerView.h @@ -1 +1,32 @@ #pragma once +#include +#include"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; + + + +public: + PlayerView(); + ~PlayerView(); + + void initialize(PlayerController* player_controller); + void update(); + void render(); +}; \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 87d1530fc..9c1f9c7a3 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -136,7 +136,9 @@ + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index a5307cb2f..e01c392bf 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -39,6 +39,12 @@ Source Files + + Source Files + + + Source Files + From 571656de35b456208007afd36f41fa0f53cbe772 Mon Sep 17 00:00:00 2001 From: Tarun Date: Mon, 29 Jul 2024 19:52:30 +0530 Subject: [PATCH 07/15] did nothing much just add forward declrations --- Space-Invaders/Headers/{ => EVENT}/EventService.h | 0 Space-Invaders/Headers/{ => Global}/ServiceLocator.h | 0 Space-Invaders/Headers/{ => Graphic}/GraphicService.h | 0 Space-Invaders/Headers/{ => TIME}/TimeService .h | 0 Space-Invaders/Headers/{ => main}/GameService.h | 0 Space-Invaders/Headers/{ => player}/PlayerService.h | 0 Space-Invaders/{Headers => source}/EventService.cpp | 0 Space-Invaders/{Headers => source}/GameService.cpp | 0 Space-Invaders/{Headers => source}/GraphicService.cpp | 0 Space-Invaders/{Headers/player => source}/PlayerController.cpp | 0 Space-Invaders/{Headers/player => source}/PlayerModel.cpp | 0 Space-Invaders/{Headers => source}/PlayerService.cpp | 0 Space-Invaders/{Headers/player => source}/PlayerView.cpp | 0 Space-Invaders/{Headers => source}/ServiceLocator.cpp | 0 Space-Invaders/{Headers => source}/TimeService .cpp | 0 15 files changed, 0 insertions(+), 0 deletions(-) rename Space-Invaders/Headers/{ => EVENT}/EventService.h (100%) rename Space-Invaders/Headers/{ => Global}/ServiceLocator.h (100%) rename Space-Invaders/Headers/{ => Graphic}/GraphicService.h (100%) rename Space-Invaders/Headers/{ => TIME}/TimeService .h (100%) rename Space-Invaders/Headers/{ => main}/GameService.h (100%) rename Space-Invaders/Headers/{ => player}/PlayerService.h (100%) rename Space-Invaders/{Headers => source}/EventService.cpp (100%) rename Space-Invaders/{Headers => source}/GameService.cpp (100%) rename Space-Invaders/{Headers => source}/GraphicService.cpp (100%) rename Space-Invaders/{Headers/player => source}/PlayerController.cpp (100%) rename Space-Invaders/{Headers/player => source}/PlayerModel.cpp (100%) rename Space-Invaders/{Headers => source}/PlayerService.cpp (100%) rename Space-Invaders/{Headers/player => source}/PlayerView.cpp (100%) rename Space-Invaders/{Headers => source}/ServiceLocator.cpp (100%) rename Space-Invaders/{Headers => source}/TimeService .cpp (100%) diff --git a/Space-Invaders/Headers/EventService.h b/Space-Invaders/Headers/EVENT/EventService.h similarity index 100% rename from Space-Invaders/Headers/EventService.h rename to Space-Invaders/Headers/EVENT/EventService.h diff --git a/Space-Invaders/Headers/ServiceLocator.h b/Space-Invaders/Headers/Global/ServiceLocator.h similarity index 100% rename from Space-Invaders/Headers/ServiceLocator.h rename to Space-Invaders/Headers/Global/ServiceLocator.h diff --git a/Space-Invaders/Headers/GraphicService.h b/Space-Invaders/Headers/Graphic/GraphicService.h similarity index 100% rename from Space-Invaders/Headers/GraphicService.h rename to Space-Invaders/Headers/Graphic/GraphicService.h diff --git a/Space-Invaders/Headers/TimeService .h b/Space-Invaders/Headers/TIME/TimeService .h similarity index 100% rename from Space-Invaders/Headers/TimeService .h rename to Space-Invaders/Headers/TIME/TimeService .h diff --git a/Space-Invaders/Headers/GameService.h b/Space-Invaders/Headers/main/GameService.h similarity index 100% rename from Space-Invaders/Headers/GameService.h rename to Space-Invaders/Headers/main/GameService.h diff --git a/Space-Invaders/Headers/PlayerService.h b/Space-Invaders/Headers/player/PlayerService.h similarity index 100% rename from Space-Invaders/Headers/PlayerService.h rename to Space-Invaders/Headers/player/PlayerService.h diff --git a/Space-Invaders/Headers/EventService.cpp b/Space-Invaders/source/EventService.cpp similarity index 100% rename from Space-Invaders/Headers/EventService.cpp rename to Space-Invaders/source/EventService.cpp diff --git a/Space-Invaders/Headers/GameService.cpp b/Space-Invaders/source/GameService.cpp similarity index 100% rename from Space-Invaders/Headers/GameService.cpp rename to Space-Invaders/source/GameService.cpp diff --git a/Space-Invaders/Headers/GraphicService.cpp b/Space-Invaders/source/GraphicService.cpp similarity index 100% rename from Space-Invaders/Headers/GraphicService.cpp rename to Space-Invaders/source/GraphicService.cpp diff --git a/Space-Invaders/Headers/player/PlayerController.cpp b/Space-Invaders/source/PlayerController.cpp similarity index 100% rename from Space-Invaders/Headers/player/PlayerController.cpp rename to Space-Invaders/source/PlayerController.cpp diff --git a/Space-Invaders/Headers/player/PlayerModel.cpp b/Space-Invaders/source/PlayerModel.cpp similarity index 100% rename from Space-Invaders/Headers/player/PlayerModel.cpp rename to Space-Invaders/source/PlayerModel.cpp diff --git a/Space-Invaders/Headers/PlayerService.cpp b/Space-Invaders/source/PlayerService.cpp similarity index 100% rename from Space-Invaders/Headers/PlayerService.cpp rename to Space-Invaders/source/PlayerService.cpp diff --git a/Space-Invaders/Headers/player/PlayerView.cpp b/Space-Invaders/source/PlayerView.cpp similarity index 100% rename from Space-Invaders/Headers/player/PlayerView.cpp rename to Space-Invaders/source/PlayerView.cpp diff --git a/Space-Invaders/Headers/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp similarity index 100% rename from Space-Invaders/Headers/ServiceLocator.cpp rename to Space-Invaders/source/ServiceLocator.cpp diff --git a/Space-Invaders/Headers/TimeService .cpp b/Space-Invaders/source/TimeService .cpp similarity index 100% rename from Space-Invaders/Headers/TimeService .cpp rename to Space-Invaders/source/TimeService .cpp From c033f7310ced58b1bf2bf1267033ac2ec8385701 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sat, 3 Aug 2024 10:40:56 +0530 Subject: [PATCH 08/15] here i have put the namespace in the code and l also debug the code for the error after the namespace --- Space-Invaders/Headers/EVENT/EventService.h | 41 ++--- .../Headers/Global/ServiceLocator.h | 80 +++++---- .../Headers/Graphic/GraphicService.h | 53 +++--- Space-Invaders/Headers/main/GameService.h | 40 +++-- .../Headers/player/PlayerController.h | 44 +++-- Space-Invaders/Headers/player/PlayerModel.h | 69 ++++---- Space-Invaders/Headers/player/PlayerService.h | 57 +++--- Space-Invaders/Headers/player/PlayerView.h | 45 ++--- Space-Invaders/Space-Invaders.vcxproj | 32 ++-- Space-Invaders/Space-Invaders.vcxproj.filters | 36 ++-- Space-Invaders/main.cpp | 3 +- Space-Invaders/source/EventService.cpp | 84 +++++---- Space-Invaders/source/GameService.cpp | 104 ++++++----- Space-Invaders/source/GraphicService.cpp | 100 +++++------ Space-Invaders/source/PlayerController.cpp | 110 ++++++------ Space-Invaders/source/PlayerModel.cpp | 54 +++--- Space-Invaders/source/PlayerService.cpp | 155 +++++++++-------- Space-Invaders/source/PlayerView.cpp | 77 +++++---- Space-Invaders/source/ServiceLocator.cpp | 162 +++++++++--------- Space-Invaders/source/TimeService .cpp | 2 +- 20 files changed, 723 insertions(+), 625 deletions(-) diff --git a/Space-Invaders/Headers/EVENT/EventService.h b/Space-Invaders/Headers/EVENT/EventService.h index 467eff7ca..93f993fd5 100644 --- a/Space-Invaders/Headers/EVENT/EventService.h +++ b/Space-Invaders/Headers/EVENT/EventService.h @@ -1,30 +1,31 @@ #pragma once #include #include +namespace event { + class EventService + { + 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(); - bool pressedRightKey(); + void initialize(); + void update(); + void processEvents(); // while window is open we will check for events + bool pressedEscapeKey(); + bool isKeyboardEvent(); + bool pressedLeftKey(); + bool pressedRightKey(); -}; \ No newline at end of file + }; +} \ No newline at end of file diff --git a/Space-Invaders/Headers/Global/ServiceLocator.h b/Space-Invaders/Headers/Global/ServiceLocator.h index 86a93eed9..bf7b53343 100644 --- a/Space-Invaders/Headers/Global/ServiceLocator.h +++ b/Space-Invaders/Headers/Global/ServiceLocator.h @@ -1,37 +1,45 @@ #pragma once -#include "GraphicService.h" -#include"EventService.h" -#include"PlayerService.h" -#include"TimeService .h" - -class ServiceLocator -{ -private: - // Private Attributes: - GraphicService* graphic_service; - EventService* event_service; - PlayerService* player_service; - TimeService* time_Service; - - // Private Constructor and Destructor: - ServiceLocator(); - // Constructor for initializing the ServiceLocator. - ~ServiceLocator(); // Destructor for cleaning up resources upon object deletion. - - // 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). - void initialize(); // Initializes the ServiceLocator. - void update(); // Updates all services. - void render(); // Renders using the services. - - // Methods to Get Specific Services: - GraphicService* getGraphicService(); - EventService* getEventService(); - PlayerService* getplayerservice(); - TimeService* gettimeservice(); -}; + +#include"../Graphic/GraphicService.h" +#include"../EVENT/EventService.h" +#include"../player/PlayerService.h" +#include"../TIME/TimeService .h" + + +namespace Global { + using namespace player; + using namespace Graphic; + using namespace event; + + class ServiceLocator + { + private: + // Private Attributes: + GraphicService* graphic_service; + EventService* event_service; + PlayerService* player_service; + TimeService* time_Service; + + // Private Constructor and Destructor: + ServiceLocator(); + // Constructor for initializing the ServiceLocator. + ~ServiceLocator(); // Destructor for cleaning up resources upon object deletion. + + // 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). + void initialize(); // Initializes the ServiceLocator. + void update(); // Updates all services. + void render(); // Renders using the services. + + // Methods to Get Specific Services: + GraphicService* getGraphicService(); + EventService* getEventService(); + PlayerService* getplayerservice(); + TimeService* gettimeservice(); + }; +} \ No newline at end of file diff --git a/Space-Invaders/Headers/Graphic/GraphicService.h b/Space-Invaders/Headers/Graphic/GraphicService.h index 570c7f584..e013140cf 100644 --- a/Space-Invaders/Headers/Graphic/GraphicService.h +++ b/Space-Invaders/Headers/Graphic/GraphicService.h @@ -2,40 +2,43 @@ #pragma once #include +namespace Graphic { -class GraphicService -{ -private: - + 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 std::string game_window_title = "Outscal Presents - Alien Invader"; - const sf::Color window_color = sf::Color::Blue; + const int game_window_width = 800; + const int game_window_height = 600; - const int framerate = 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 + const int framerate = 60; - 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/Headers/main/GameService.h b/Space-Invaders/Headers/main/GameService.h index c09fa800f..96ffa1177 100644 --- a/Space-Invaders/Headers/main/GameService.h +++ b/Space-Invaders/Headers/main/GameService.h @@ -1,26 +1,32 @@ #pragma once #include -#include "ServiceLocator.h" +#include"../Global/ServiceLocator.h" -class GameService -{ -private: +namespace Main { - ServiceLocator* service_locator; - sf::RenderWindow* game_window; + using namespace Global; - void initialize(); - void initializeVariables();// Handles game initialization. - void destroy(); // Handles cleanup tasks. + class GameService + { + private: -public: - GameService(); // Constructor for initializing the GameService object. - ~GameService(); // Destructor for cleaning up resources upon object deletion. + ServiceLocator* service_locator; + sf::RenderWindow* game_window; - 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 + 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. + + 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/Headers/player/PlayerController.h b/Space-Invaders/Headers/player/PlayerController.h index 924e5b845..9406c7c23 100644 --- a/Space-Invaders/Headers/player/PlayerController.h +++ b/Space-Invaders/Headers/player/PlayerController.h @@ -1,29 +1,35 @@ -#pragma one +#pragma once #include -#include"PlayerModel.h" -#include"PlayerView.h" +namespace player { -class PlayerController { + enum class PlayerState; + class PlayerView; + class PlayerModel; -private: + class PlayerController { - PlayerModel* model; - PlayerView* view; + private: - void processinput(); - void processmoveleft(); - void processmoveright(); -public: - PlayerController(); - ~PlayerController(); - void intialize(); - void update(); - void render(); - + void processinput(); + void processmoveleft(); + void processmoveright(); + public: + PlayerModel* model; + PlayerView* view; + PlayerController(); + ~PlayerController(); + void intialize(); + void update(); + void render(); - sf::Vector2f getPlayerPosition(); -}; + + + sf::Vector2f getPlayerPosition(); + + }; + +} \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerModel.h b/Space-Invaders/Headers/player/PlayerModel.h index 5d0ba8e79..90d116e84 100644 --- a/Space-Invaders/Headers/player/PlayerModel.h +++ b/Space-Invaders/Headers/player/PlayerModel.h @@ -1,51 +1,58 @@ #pragma once #include +class PlayerView; +class PlayerModel; +namespace player { -enum class PlayerState //Our Enum -{ - 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(500.f, 500.f); //new var - sf::Vector2f player_position; //new var - PlayerState player_state; - - int player_score = 0; + class PlayerModel + { + private: -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 initial_player_position = sf::Vector2f(500.f, 500.f); //new var + sf::Vector2f player_position; //new var + PlayerState player_state; + PlayerModel* model; + PlayerView* view; + int player_score = 0; - const float player_movement_speed = 200.0f; - PlayerModel(); - ~PlayerModel(); + 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); - void initialize(); + const float player_movement_speed = 200.0f; - void reset(); //new method + PlayerModel(); + ~PlayerModel(); + void initialize(); - + void reset(); //new method - //getters and setters - sf::Vector2f getPlayerPosition(); - void setPlayerPosition(sf::Vector2f position); - PlayerState getplayerstate(); - - void setplayerstate(PlayerState state); - + //getters and setters + sf::Vector2f getPlayerPosition(); + void setPlayerPosition(sf::Vector2f position); -}; \ No newline at end of file + PlayerState getplayerstate(); + + + void setplayerstate(PlayerState state); + + + + }; +} \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerService.h b/Space-Invaders/Headers/player/PlayerService.h index a1cdb65d1..25d03fecf 100644 --- a/Space-Invaders/Headers/player/PlayerService.h +++ b/Space-Invaders/Headers/player/PlayerService.h @@ -1,43 +1,48 @@ #pragma once -#include -#include"TimeService .h" -#include"../Headers/player/PlayerController.h" -class PlayerService -{ +#include" +#include"../TIME/TimeService .h" +#include"../player/PlayerController.h" -private: - +namespace player { + class PlayerService + { - int health = 3; - sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); - float movement_speed = 5.0f; - int player_score = 0; + private: - 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 + int health = 3; + sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); + float movement_speed = 5.0f; + int player_score = 0; - void initializePlayerSprite(); - void processPlayerInput(); + const sf::String player_texture_path = "assets/textures/player_ship.png"; - PlayerController* pcontroller; + sf::Texture player_texture; + sf::Sprite player_sprite; -public: + sf::RenderWindow* game_window; //as always + + void initializePlayerSprite(); + void processPlayerInput(); + + PlayerController* player_controller; + + + public: PlayerService(); - ~PlayerService(); + ~PlayerService(); - void initialize(); - void update(); - void render(); + void initialize(); + void update(); + void render(); void moveLef(); void moveRight(); - int getMoveSpeed(); - sf::Vector2f getPlayerPosition(); + int getMoveSpeed(); + sf::Vector2f getPlayerPosition(); -}; + }; +} \ No newline at end of file diff --git a/Space-Invaders/Headers/player/PlayerView.h b/Space-Invaders/Headers/player/PlayerView.h index 23dd3dad1..cba5f3eba 100644 --- a/Space-Invaders/Headers/player/PlayerView.h +++ b/Space-Invaders/Headers/player/PlayerView.h @@ -1,32 +1,33 @@ #pragma once #include -#include"PlayerController.h" +#include"../player/PlayerController.h" -class PlayerView -{ -private: + class PlayerController; + namespace player { + 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; + PlayerController* player_controller; + public: + PlayerView(); + ~PlayerView(); - -public: - PlayerView(); - ~PlayerView(); - - void initialize(PlayerController* player_controller); - void update(); - void render(); -}; \ No newline at end of file + void initialize(PlayerController* player_controller); + void update(); + void render(); + }; + } \ No newline at end of file diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 9c1f9c7a3..69138384f 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -1,4 +1,4 @@ - + @@ -132,27 +132,27 @@ - - - - - - - - - + + + + + + + + + - - - - + + + + - - + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index e01c392bf..31167764f 100644 --- a/Space-Invaders/Space-Invaders.vcxproj.filters +++ b/Space-Invaders/Space-Invaders.vcxproj.filters @@ -18,60 +18,60 @@ 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 diff --git a/Space-Invaders/main.cpp b/Space-Invaders/main.cpp index 1a2cf9819..a7a0b06f7 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,4 +1,5 @@ -#include "Headers/GameService.h" +#include"../Space-Invaders/Headers/main/GameService.h" +using namespace Main; int main() { diff --git a/Space-Invaders/source/EventService.cpp b/Space-Invaders/source/EventService.cpp index a5c4965e3..729ac8cd0 100644 --- a/Space-Invaders/source/EventService.cpp +++ b/Space-Invaders/source/EventService.cpp @@ -1,49 +1,61 @@ -#include"ServiceLocator.h" -#include"GraphicService.h" -#include"EventService.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(); +//#include"ServiceLocator.h" +//#include"GraphicService.h" +//#include"EventService.h" + +#include"../Headers/main/GameService.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/EVENT/EventService.h" + + +namespace event { + + using namespace Global; + using namespace Graphic; + using namespace event; + + 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 + 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; } + //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::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; } -bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } -bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } + 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 index 960d7acb4..398a7bb0b 100644 --- a/Space-Invaders/source/GameService.cpp +++ b/Space-Invaders/source/GameService.cpp @@ -1,61 +1,73 @@ -#include "GameService.h" -#include"GraphicService.h" +//#include "GameService.h" +//#include"GraphicService.h" +#include"../Headers/Graphic/GraphicService.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/main/GameService.h" -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. -} +namespace Main { + using namespace Global; + using namespace event; + using namespace Graphic; + GameService::GameService() { -//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. -} + service_locator = nullptr; // Set service locator to null + game_window = nullptr; // Set game window to null + } -// Updates the game logic by delegating to the service locator's update method. -void GameService::update() { + // Destructor: Calls the destroy function to clean up resources. + GameService::~GameService() { + destroy(); // Clean up and release resources + } - service_locator->getEventService()->processEvents(); + // 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. + } - service_locator->update(); // Call update on the service locator which then updates all its managed services -} + //initialize service locator and other variables + void GameService::initialize() + { + service_locator->initialize(); + initializeVariables(); + } -// 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 -} + void GameService::initializeVariables() + { + game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) + } -// 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(); -} + 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 index e4a35584d..5b690f231 100644 --- a/Space-Invaders/source/GraphicService.cpp +++ b/Space-Invaders/source/GraphicService.cpp @@ -1,67 +1,71 @@ -#include"ServiceLocator.h" -#include"GraphicService.h" +//#include"ServiceLocator.h" +//#include"GraphicService.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/Graphic/GraphicService.h" +namespace Graphic { -// 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 - -} + // 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 + // Destructor: Cleans up resources by calling onDestroy. + GraphicService::~GraphicService() { + onDestroy(); // Calls onDestroy method to clean up resources + } - game_window->setFramerateLimit(framerate); -} + // 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->setFramerateLimit(framerate); + } -// 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 -} + // 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 + } -// 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 -} + // 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 + } -// Placeholder function for game update logic. -void GraphicService::update() { + // 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() { + // 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 -} + // 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 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; -} + // 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/PlayerController.cpp b/Space-Invaders/source/PlayerController.cpp index 4be7a2a9c..05d122b62 100644 --- a/Space-Invaders/source/PlayerController.cpp +++ b/Space-Invaders/source/PlayerController.cpp @@ -1,67 +1,73 @@ -#include "PlayerController.h" -#include"../EventService.h" -#include"../ServiceLocator.h" +#include"../Headers/player/PlayerController.h" +#include"../Headers//EVENT/EventService.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/player/PlayerModel.h" +#include"../Headers//player/PlayerView.h" #include -void PlayerController::processinput() -{ +namespace player{ + using namespace Global; + void PlayerController::processinput() + { - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - processmoveright(); + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + processmoveright(); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + processmoveleft(); + } } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - processmoveleft(); - } -} -void PlayerController::processmoveleft() -{ + void PlayerController::processmoveleft() + { - sf::Vector2f current_position = model->getPlayerPosition(); - current_position.x -= model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); - current_position.x = std::max(current_position.x, model->left_most_position.x); - model->setPlayerPosition(current_position); -} + sf::Vector2f current_position = model->getPlayerPosition(); + current_position.x -= model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + current_position.x = std::max(current_position.x, model->left_most_position.x); + model->setPlayerPosition(current_position); + } -void PlayerController::processmoveright() -{ - sf::Vector2f current_position = model->getPlayerPosition(); - current_position.x += model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); - current_position.x = std::min(current_position.x, model->right_most_position.x); - model->setPlayerPosition(current_position); + void PlayerController::processmoveright() + { + sf::Vector2f current_position = model->getPlayerPosition(); + current_position.x += model->player_movement_speed * ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + current_position.x = std::min(current_position.x, model->right_most_position.x); + model->setPlayerPosition(current_position); -} + } -PlayerController::PlayerController() -{ - - model = new PlayerModel(); - view = new PlayerView(); -} + PlayerController::PlayerController() + { -PlayerController::~PlayerController() -{ - delete (view); - delete (model); -} + model = new PlayerModel(); + view = new PlayerView(); + } -void PlayerController::intialize() -{ - model->initialize(); - view->initialize(); -} + PlayerController::~PlayerController() + { + delete (view); + delete (model); + } -void PlayerController::update() -{ - view->update(); -} + void PlayerController::intialize() + { + model->initialize(); + view->initialize(this); + } -void PlayerController::render() -{ - view->render(); -} + void PlayerController::update() + { + processinput(); + view->update(); + } + + void PlayerController::render() + { + view->render(); + } -sf::Vector2f PlayerController::getPlayerPosition() -{ - return model->getPlayerPosition(); + sf::Vector2f PlayerController::getPlayerPosition() + { + return model->getPlayerPosition(); + } } diff --git a/Space-Invaders/source/PlayerModel.cpp b/Space-Invaders/source/PlayerModel.cpp index bc266d1d0..0218d5e85 100644 --- a/Space-Invaders/source/PlayerModel.cpp +++ b/Space-Invaders/source/PlayerModel.cpp @@ -1,40 +1,44 @@ -#include"PlayerModel.h" +//#include"PlayerModel.h" +#include"../Headers/player/PlayerModel.h" +namespace player { -PlayerModel::PlayerModel() { } + PlayerModel::PlayerModel() { } -PlayerModel::~PlayerModel() { } + PlayerModel::~PlayerModel() { } -void PlayerModel::initialize() { reset(); } + void PlayerModel::initialize() { reset(); } -void PlayerModel::reset() -{ - player_position = initial_player_position; - player_score = 0; -} + void PlayerModel::reset() + { -sf::Vector2f PlayerModel::getPlayerPosition() -{ - return player_position; -} + player_state = PlayerState::ALIVE; + player_position = initial_player_position; + player_score = 0; + } -void PlayerModel::setPlayerPosition(sf::Vector2f position) -{ - player_position = position; -} + sf::Vector2f PlayerModel::getPlayerPosition() + { + return player_position; + } -PlayerState PlayerModel::getplayerstate() -{ - return player_state; -} + void PlayerModel::setPlayerPosition(sf::Vector2f position) + { + player_position = position; + } -void PlayerModel::setplayerstate(PlayerState state) + PlayerState PlayerModel::getplayerstate() + { + return player_state; + } -{ - player_state = state; + void PlayerModel::setplayerstate(PlayerState state) -} + { + player_state = state; + } +} diff --git a/Space-Invaders/source/PlayerService.cpp b/Space-Invaders/source/PlayerService.cpp index d06939a78..8729355e4 100644 --- a/Space-Invaders/source/PlayerService.cpp +++ b/Space-Invaders/source/PlayerService.cpp @@ -1,79 +1,88 @@ -#include "PlayerService.h" -#include"ServiceLocator.h" +//#include "../Headers/PlayerService.h" +//#include"../Headers/ServiceLocator.h" +//#include"../Headers/player/PlayerController.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/player/PlayerService.h" +#include"../Headers/player/PlayerController.h" +#include"../Headers/EVENT/EventService.h" +namespace player { + using namespace event; + using namespace Global; + + void PlayerService::initializePlayerSprite() + { + if (player_texture.loadFromFile(player_texture_path)) { + player_sprite.setTexture(player_texture); + } + } + void PlayerService::processPlayerInput() + { -void PlayerService::initializePlayerSprite() -{ - if (player_texture.loadFromFile(player_texture_path)) { - player_sprite.setTexture(player_texture); + EventService* checkkey = ServiceLocator::getInstance()->getEventService(); + if (checkkey->isKeyboardEvent()) { + if (checkkey->pressedLeftKey()) { + moveLef(); + } + + if (checkkey->pressedRightKey()) { + moveRight(); + } + } } -} -void PlayerService::processPlayerInput() -{ + PlayerService::PlayerService() + { + game_window = nullptr; + player_controller = new PlayerController; + } - EventService* checkkey = ServiceLocator::getInstance()->getEventService(); - if (checkkey->isKeyboardEvent()) { - if (checkkey->pressedLeftKey()) { - moveLef(); - } + PlayerService::~PlayerService() + { + delete player_controller; + } - if (checkkey->pressedRightKey()) { - moveRight(); - } - } -} - -PlayerService::PlayerService() -{ - game_window = nullptr; - pcontroller = nullptr; -} - -PlayerService::~PlayerService() -{ -} - -void PlayerService::initialize() -{ - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - pcontroller->intialize(); - initializePlayerSprite(); -} - -void PlayerService::update() -{ - processPlayerInput(); - pcontroller->update(); - player_sprite.setPosition(getPlayerPosition()); -} - -void PlayerService::render() -{ - pcontroller->render(); - game_window->draw(player_sprite); -} - -void PlayerService::moveLef() -{ - - position.x -= ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); -} - -void PlayerService::moveRight() -{ - - position.x += ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); -} - - - -int PlayerService::getMoveSpeed() -{ - return movement_speed; -} - -sf::Vector2f PlayerService::getPlayerPosition() -{ - return position; -} + void PlayerService::initialize() + { + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + player_controller->intialize(); + initializePlayerSprite(); + } + + void PlayerService::update() + { + processPlayerInput(); + player_controller->update(); + player_sprite.setPosition(getPlayerPosition()); + } + + void PlayerService::render() + { + player_controller->render(); + game_window->draw(player_sprite); + } + + void PlayerService::moveLef() + { + + position.x -= ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + } + + void PlayerService::moveRight() + { + + position.x += ServiceLocator::getInstance()->gettimeservice()->getdeltatime(); + } + + + + int PlayerService::getMoveSpeed() + { + return movement_speed; + } + + sf::Vector2f PlayerService::getPlayerPosition() + { + return position; + } +} \ No newline at end of file diff --git a/Space-Invaders/source/PlayerView.cpp b/Space-Invaders/source/PlayerView.cpp index ceb9af683..c4068235f 100644 --- a/Space-Invaders/source/PlayerView.cpp +++ b/Space-Invaders/source/PlayerView.cpp @@ -1,42 +1,49 @@ -#include"PlayerView.h" -#include"../ServiceLocator.h" -PlayerView::PlayerView() { } +//#include"PlayerView.h" +//#include"../ServiceLocator.h" +#include"../Headers/player/PlayerView.h" +#include"../Headers/Global/ServiceLocator.h" -PlayerView::~PlayerView() { } +namespace player { + using namespace Global; -void PlayerView::initialize(PlayerController* controller) -{ + PlayerView::PlayerView() { } - player_controller = controller; - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializePlayerSprite(); -} + PlayerView::~PlayerView() { } -void PlayerView::initializePlayerSprite() -{ - if (player_texture.loadFromFile(player_texture_path)) + void PlayerView::initialize(PlayerController* controller) { - player_sprite.setTexture(player_texture); - scalePlayerSprite(); + + player_controller = controller; + 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() + { + player_sprite.setPosition(player_controller->getPlayerPosition()); + } + + void PlayerView::render() + { + game_window->draw(player_sprite); } -} - -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() -{ - //empty for now -} - -void PlayerView::render() -{ - game_window->draw(player_sprite); } \ No newline at end of file diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index 21ba91f85..a21653f1d 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -1,82 +1,88 @@ -#include "ServiceLocator.h" -#include"EventService.h" +#include"../Headers/Global/ServiceLocator.h" +#include"../Headers/EVENT/EventService.h" +#include"../Headers/player/PlayerService.h" +#include"../Headers/TIME/TimeService .h" +#include"../Headers//Graphic/GraphicService.h" + +namespace Global { + + // Constructor: Initializes the graphic_service pointer to null and creates services. + ServiceLocator::ServiceLocator() { + graphic_service = nullptr;// Initialize graphic_service to null + event_service = nullptr; + player_service = nullptr; + time_Service = nullptr; + 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 + event_service = new EventService(); + + player_service = new PlayerService(); + time_Service = new TimeService(); + } + + // 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); + delete(player_service); + delete(time_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 + } + + // Calls initialize on the graphic service, readying it for use. + void ServiceLocator::initialize() { + graphic_service->initialize(); // Initialize graphic service + event_service->initialize(); + player_service->initialize(); + time_Service->intialize(); + } + + // Updates the state of the graphic service. + void ServiceLocator::update() { + graphic_service->update(); // Update graphic service + event_service->update(); + player_service->update(); + time_Service->update(); + } + + + // Renders using the graphic service. + void ServiceLocator::render() { + graphic_service->render(); // Render graphic service + player_service->render(); + } + + // Returns a pointer to the currently set graphic 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; + } -// Constructor: Initializes the graphic_service pointer to null and creates services. -ServiceLocator::ServiceLocator() { - graphic_service = nullptr;// Initialize graphic_service to null - event_service = nullptr; - player_service = nullptr; - time_Service = nullptr; - 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 - event_service = new EventService(); - player_service = new PlayerService(); - time_Service = new TimeService(); -} - -// 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); - delete(player_service); - delete(time_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 -} - -// Calls initialize on the graphic service, readying it for use. -void ServiceLocator::initialize() { - graphic_service->initialize(); // Initialize graphic service - event_service->initialize(); - player_service->initialize(); - time_Service->intialize(); -} - -// Updates the state of the graphic service. -void ServiceLocator::update() { - graphic_service->update(); // Update graphic service - event_service->update(); - player_service->update(); - time_Service->update(); -} - - -// Renders using the graphic service. -void ServiceLocator::render() { - graphic_service->render(); // Render graphic service - player_service->render(); -} - -// Returns a pointer to the currently set graphic 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 index 0c719b841..7667ed227 100644 --- a/Space-Invaders/source/TimeService .cpp +++ b/Space-Invaders/source/TimeService .cpp @@ -1,4 +1,4 @@ -#include "TimeService .h" +#include"../Headers/TIME/TimeService .h" float TimeService::calculate_delta_time() { From abb00d35af5d0c3dd2b783379843837125317524 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 10:53:48 +0530 Subject: [PATCH 09/15] JUST ADDED THE GAME STATE --- Space-Invaders/Headers/main/GameService.h | 14 +++++++++++++- Space-Invaders/source/GameService.cpp | 10 +++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Space-Invaders/Headers/main/GameService.h b/Space-Invaders/Headers/main/GameService.h index 96ffa1177..a6864e698 100644 --- a/Space-Invaders/Headers/main/GameService.h +++ b/Space-Invaders/Headers/main/GameService.h @@ -2,6 +2,12 @@ #include #include"../Global/ServiceLocator.h" +enum class GameState //create the enum +{ + BOOT, + MAIN_MENU, + GAMEPLAY, +}; namespace Main { @@ -12,6 +18,8 @@ namespace Main { private: + static GameState current_state; + ServiceLocator* service_locator; sf::RenderWindow* game_window; @@ -27,6 +35,10 @@ namespace Main { 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. + 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/GameService.cpp b/Space-Invaders/source/GameService.cpp index 398a7bb0b..76641bac5 100644 --- a/Space-Invaders/source/GameService.cpp +++ b/Space-Invaders/source/GameService.cpp @@ -10,10 +10,11 @@ namespace Main { using namespace Global; using namespace event; using namespace Graphic; - GameService::GameService() { + GameState GameService::current_state = GameState::BOOT; + GameService::GameService() { service_locator = nullptr; // Set service locator to null game_window = nullptr; // Set game window to null } @@ -68,6 +69,13 @@ namespace Main { return service_locator->getGraphicService()->isGameWindowOpen(); } + + void GameService::setGameState(GameState new_state) { current_state = new_state; } + + GameState GameService::getGameState() { return current_state; } } + + + From 82351a8d65e05d30fb07660923dc68c2e5b459e2 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 11:47:09 +0530 Subject: [PATCH 10/15] added the ui sevice also added the mainmenucobteoller --- .../Headers/Global/ServiceLocator.h | 6 ++- .../MainMenu/MainMenuUIController.cpp | 32 +++++++++++++ .../UIService/MainMenu/MainMenuUIController.h | 23 +++++++++ .../Headers/UIService/UIService.cpp | 47 +++++++++++++++++++ Space-Invaders/Headers/UIService/UIService.h | 20 ++++++++ Space-Invaders/Space-Invaders.vcxproj | 4 ++ Space-Invaders/source/ServiceLocator.cpp | 12 ++++- 7 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp create mode 100644 Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h create mode 100644 Space-Invaders/Headers/UIService/UIService.cpp create mode 100644 Space-Invaders/Headers/UIService/UIService.h diff --git a/Space-Invaders/Headers/Global/ServiceLocator.h b/Space-Invaders/Headers/Global/ServiceLocator.h index bf7b53343..f3c9ec9be 100644 --- a/Space-Invaders/Headers/Global/ServiceLocator.h +++ b/Space-Invaders/Headers/Global/ServiceLocator.h @@ -4,12 +4,13 @@ #include"../EVENT/EventService.h" #include"../player/PlayerService.h" #include"../TIME/TimeService .h" - +#include"../../Headers/UIService/UIService.h" namespace Global { using namespace player; using namespace Graphic; using namespace event; + using namespace UI; class ServiceLocator { @@ -19,7 +20,7 @@ namespace Global { EventService* event_service; PlayerService* player_service; TimeService* time_Service; - + UiService* uiservice; // Private Constructor and Destructor: ServiceLocator(); // Constructor for initializing the ServiceLocator. @@ -41,5 +42,6 @@ namespace Global { EventService* getEventService(); PlayerService* getplayerservice(); TimeService* gettimeservice(); + UiService* getuiservice(); }; } \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp new file mode 100644 index 000000000..7435399f7 --- /dev/null +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp @@ -0,0 +1,32 @@ +#include "MainMenuUIController.h" +#include"../../Headers/main/GameService.h" +#include"../../Headers/Global/ServiceLocator.h" +#include"../../Headers/Graphic/GraphicService.h" + +namespace UI { + namespace MainMenu { + + using namespace Global; + using namespace Main; + using namespace Graphic; + using namespace event; + + UI::MainMenu::MainMenuUIController::MainMenuUIController() + { + game_window = nullptr; + } + + void UI::MainMenu::MainMenuUIController::initialize() + { + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + } + + void UI::MainMenu::MainMenuUIController::update() + { + } + + void UI::MainMenu::MainMenuUIController::render() + { + } + } +} \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h new file mode 100644 index 000000000..91e7bff33 --- /dev/null +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h @@ -0,0 +1,23 @@ +#pragma once +#include + +namespace UI +{ + namespace MainMenu + { + class MainMenuUIController + { + private: + + sf::RenderWindow* game_window; + + public: + MainMenuUIController(); + + void initialize(); + void update(); + void render(); + + }; + } +} \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/UIService.cpp b/Space-Invaders/Headers/UIService/UIService.cpp new file mode 100644 index 000000000..ed09f6af0 --- /dev/null +++ b/Space-Invaders/Headers/UIService/UIService.cpp @@ -0,0 +1,47 @@ +#include "UIService.h" +#include"../main/GameService.h" + + + +using namespace Main; +using namespace MainMenu; + + +void UI::UiService::createControllers() +{ + main_menu_controller = new MainMenuUIController(); +} + +void UI::UiService::initializeControllers() +{ + main_menu_controller->initialize(); +} + +void UI::UiService::destroy() +{ + delete(main_menu_controller); +} + +UI::UiService::UiService() +{ + main_menu_controller = nullptr; + createControllers(); +} + +UI::UiService::~UiService() +{ + destroy(); +} + +void UI::UiService::initialize() +{ + initializeControllers(); +} + +void UI::UiService::update() +{ +} + +void UI::UiService::render() +{ +} diff --git a/Space-Invaders/Headers/UIService/UIService.h b/Space-Invaders/Headers/UIService/UIService.h new file mode 100644 index 000000000..65de033b8 --- /dev/null +++ b/Space-Invaders/Headers/UIService/UIService.h @@ -0,0 +1,20 @@ +#pragma once +#include"../../Headers/UIService/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/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 69138384f..67c83d458 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,6 +132,8 @@ + + @@ -153,6 +155,8 @@ + + diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index a21653f1d..abe815cf3 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -13,6 +13,7 @@ namespace Global { event_service = nullptr; player_service = nullptr; time_Service = nullptr; + uiservice = nullptr; createServices(); // Call createServices to instantiate services } @@ -28,6 +29,7 @@ namespace Global { player_service = new PlayerService(); time_Service = new TimeService(); + uiservice = new UiService(); } // Deletes allocated services to prevent memory leaks, specifically the graphic service. @@ -36,7 +38,7 @@ namespace Global { delete(event_service); delete(player_service); delete(time_Service); - + delete(uiservice); } // Returns a pointer to ServiceLocator. @@ -51,6 +53,7 @@ namespace Global { event_service->initialize(); player_service->initialize(); time_Service->intialize(); + uiservice->initialize(); } // Updates the state of the graphic service. @@ -59,6 +62,7 @@ namespace Global { event_service->update(); player_service->update(); time_Service->update(); + uiservice->update(); } @@ -66,6 +70,7 @@ namespace Global { void ServiceLocator::render() { graphic_service->render(); // Render graphic service player_service->render(); + uiservice->render(); } // Returns a pointer to the currently set graphic service. @@ -84,5 +89,10 @@ namespace Global { return time_Service; } + UiService* ServiceLocator::getuiservice() { + + return uiservice; + } + } From 02ac2f66b5601cd440002940475ad86e68ce8b00 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 12:12:00 +0530 Subject: [PATCH 11/15] implemented the spirites and textures --- .../Headers/Graphic/GraphicService.h | 4 +- .../MainMenu/MainMenuUIController.cpp | 86 +++++++++++++++++++ .../UIService/MainMenu/MainMenuUIController.h | 48 +++++++++++ Space-Invaders/Headers/player/PlayerService.h | 8 ++ Space-Invaders/source/GraphicService.cpp | 2 +- Space-Invaders/source/ServiceLocator.cpp | 6 +- 6 files changed, 150 insertions(+), 4 deletions(-) diff --git a/Space-Invaders/Headers/Graphic/GraphicService.h b/Space-Invaders/Headers/Graphic/GraphicService.h index e013140cf..fc8491480 100644 --- a/Space-Invaders/Headers/Graphic/GraphicService.h +++ b/Space-Invaders/Headers/Graphic/GraphicService.h @@ -11,8 +11,8 @@ namespace Graphic { const std::string game_window_title = "Outscal Presents - 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 sf::Color window_color = sf::Color::Blue; diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp index 7435399f7..703cba2bc 100644 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp @@ -19,6 +19,10 @@ namespace UI { void UI::MainMenu::MainMenuUIController::initialize() { game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializeBackgroundImage(); + initializeButtons(); + + } void UI::MainMenu::MainMenuUIController::update() @@ -27,6 +31,88 @@ namespace UI { void UI::MainMenu::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); + + + + + } + void MainMenuUIController::initializeBackgroundImage() + { + 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(); + } + + } + 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 }); } } } \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h index 91e7bff33..3b9a0ec50 100644 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h @@ -4,16 +4,64 @@ 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"; + + + 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; + + + + + + + + const float button_height = 400.f; + const float button_width = 140.f; + + + void initializeBackgroundImage(); + void scaleBackgroundImage(); + + // however, we have 3 buttons so it's better to create a seperate function to check if they are loaded + void initializeButtons(); + bool loadButtonTexturesFromFile(); + void setButtonSprites(); + + void scaleAllButttons(); + void scaleButton(sf::Sprite* button_to_scale); + void positionButtons(); + + + + + sf::RenderWindow* game_window; public: MainMenuUIController(); + + void initialize(); void update(); void render(); diff --git a/Space-Invaders/Headers/player/PlayerService.h b/Space-Invaders/Headers/player/PlayerService.h index 25d03fecf..3819a008c 100644 --- a/Space-Invaders/Headers/player/PlayerService.h +++ b/Space-Invaders/Headers/player/PlayerService.h @@ -16,6 +16,7 @@ namespace player { sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); float movement_speed = 5.0f; int player_score = 0; + const sf::Vector2f initial_player_position = sf::Vector2f(950.f, 950.f); const sf::String player_texture_path = "assets/textures/player_ship.png"; @@ -32,6 +33,13 @@ namespace player { public: + + 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 = 350.0f; + + PlayerService(); ~PlayerService(); diff --git a/Space-Invaders/source/GraphicService.cpp b/Space-Invaders/source/GraphicService.cpp index 5b690f231..f435a8a0b 100644 --- a/Space-Invaders/source/GraphicService.cpp +++ b/Space-Invaders/source/GraphicService.cpp @@ -28,7 +28,7 @@ namespace Graphic { // 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/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index abe815cf3..c1f78bb6f 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -68,9 +68,13 @@ namespace Global { // Renders using the graphic service. void ServiceLocator::render() { + uiservice->render(); + player_service->render(); graphic_service->render(); // Render graphic service player_service->render(); - uiservice->render(); + + + } // Returns a pointer to the currently set graphic service. From 542c41539120014990a753f9603e8701298e2174 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 13:59:34 +0530 Subject: [PATCH 12/15] added the mouse inputs --- Space-Invaders/Headers/EVENT/EventService.h | 3 ++- .../MainMenu/MainMenuUIController.cpp | 25 +++++++++++++++++++ .../UIService/MainMenu/MainMenuUIController.h | 3 +++ .../Headers/UIService/UIService.cpp | 13 ++++++++++ Space-Invaders/Headers/main/GameService.h | 2 ++ Space-Invaders/source/EventService.cpp | 10 ++++++++ Space-Invaders/source/GameService.cpp | 7 ++++++ 7 files changed, 62 insertions(+), 1 deletion(-) diff --git a/Space-Invaders/Headers/EVENT/EventService.h b/Space-Invaders/Headers/EVENT/EventService.h index 93f993fd5..1a82c69da 100644 --- a/Space-Invaders/Headers/EVENT/EventService.h +++ b/Space-Invaders/Headers/EVENT/EventService.h @@ -26,6 +26,7 @@ namespace event { bool isKeyboardEvent(); bool pressedLeftKey(); bool pressedRightKey(); - + bool leftmousebutton(); + bool rightmousebutton(); }; } \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp index 703cba2bc..e759716c4 100644 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp @@ -27,6 +27,7 @@ namespace UI { void UI::MainMenu::MainMenuUIController::update() { + processButtonInteractions(); } void UI::MainMenu::MainMenuUIController::render() @@ -114,5 +115,29 @@ namespace UI { instructions_button_sprite.setPosition({ x_position, 700.f }); 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 = ServiceLocator::getInstance()->getEventService(); + return event_service->leftmousebutton() && button_sprite->getGlobalBounds().contains(mouse_position); + } } } \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h index 3b9a0ec50..19369eda7 100644 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h +++ b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h @@ -51,6 +51,9 @@ namespace UI void scaleButton(sf::Sprite* button_to_scale); void positionButtons(); + void processButtonInteractions(); + bool clickedButton(sf::Sprite*, sf::Vector2f); + diff --git a/Space-Invaders/Headers/UIService/UIService.cpp b/Space-Invaders/Headers/UIService/UIService.cpp index ed09f6af0..33781fe3d 100644 --- a/Space-Invaders/Headers/UIService/UIService.cpp +++ b/Space-Invaders/Headers/UIService/UIService.cpp @@ -40,8 +40,21 @@ void UI::UiService::initialize() void UI::UiService::update() { + + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->update(); + break; + } } void UI::UiService::render() { + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->render(); + break; + } } diff --git a/Space-Invaders/Headers/main/GameService.h b/Space-Invaders/Headers/main/GameService.h index a6864e698..58eb6ce2c 100644 --- a/Space-Invaders/Headers/main/GameService.h +++ b/Space-Invaders/Headers/main/GameService.h @@ -38,6 +38,8 @@ namespace Main { bool isRunning();// Checks if the game is currently running. static void setGameState(GameState new_state); static GameState getGameState(); + + void showmainmenu(); }; diff --git a/Space-Invaders/source/EventService.cpp b/Space-Invaders/source/EventService.cpp index 729ac8cd0..b1e9156fc 100644 --- a/Space-Invaders/source/EventService.cpp +++ b/Space-Invaders/source/EventService.cpp @@ -58,4 +58,14 @@ namespace event { bool EventService::pressedLeftKey() { return game_event.key.code == sf::Keyboard::Left; } bool EventService::pressedRightKey() { return game_event.key.code == sf::Keyboard::Right; } + bool EventService::leftmousebutton() + { + return game_event.type == sf::Event::MouseButtonPressed && game_event.mouseButton.button == sf::Mouse::Left; + } + + bool EventService::rightmousebutton() + { + 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/GameService.cpp b/Space-Invaders/source/GameService.cpp index 76641bac5..390341aec 100644 --- a/Space-Invaders/source/GameService.cpp +++ b/Space-Invaders/source/GameService.cpp @@ -35,6 +35,7 @@ namespace Main { { service_locator->initialize(); initializeVariables(); + showmainmenu(); } void GameService::initializeVariables() @@ -73,6 +74,12 @@ namespace Main { void GameService::setGameState(GameState new_state) { current_state = new_state; } GameState GameService::getGameState() { return current_state; } + void GameService::showmainmenu() + { + setGameState(GameState::MAIN_MENU); + + + } } From 845c5053a5e60ed8b8cd99ac2e1db881b36cc7b9 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 14:09:24 +0530 Subject: [PATCH 13/15] added enum class for the checking of the press events --- Space-Invaders/Headers/EVENT/EventService.h | 24 ++++++++ Space-Invaders/source/EventService.cpp | 67 ++++++++++++++++++++- Space-Invaders/source/PlayerController.cpp | 16 +++-- 3 files changed, 102 insertions(+), 5 deletions(-) diff --git a/Space-Invaders/Headers/EVENT/EventService.h b/Space-Invaders/Headers/EVENT/EventService.h index 1a82c69da..3591e0e4c 100644 --- a/Space-Invaders/Headers/EVENT/EventService.h +++ b/Space-Invaders/Headers/EVENT/EventService.h @@ -2,6 +2,14 @@ #include #include namespace event { + + enum class ButtonState + { + PRESSED, + HELD, + RELEASED, + }; + class EventService { private: @@ -12,7 +20,19 @@ namespace event { bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. bool hasQuitGame(); //for our new 'ESC' condition + + 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; + //....some other code + void updateMouseButtonsState(ButtonState& current_button_state, sf::Mouse::Button mouse_button); + void updateKeyboardButtonsState(ButtonState& current_button_state, sf::Keyboard::Key keyboard_button); + + public: @@ -28,5 +48,9 @@ namespace event { bool pressedRightKey(); bool leftmousebutton(); bool rightmousebutton(); + + + bool pressedAKey(); + bool pressedDKey(); }; } \ No newline at end of file diff --git a/Space-Invaders/source/EventService.cpp b/Space-Invaders/source/EventService.cpp index b1e9156fc..91ac93fae 100644 --- a/Space-Invaders/source/EventService.cpp +++ b/Space-Invaders/source/EventService.cpp @@ -13,6 +13,52 @@ namespace event { using namespace Graphic; using 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; + } + + } + EventService::EventService() { game_window = nullptr; } EventService::~EventService() = default; //calls the default destructor @@ -24,7 +70,16 @@ 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() @@ -68,4 +123,14 @@ namespace event { return game_event.type==sf::Event::MouseButtonPressed && game_event.mouseButton.button == sf::Mouse::Right; } + bool EventService::pressedAKey() + { + return false; + } + + bool EventService::pressedDKey() + { + return false; + } + } \ No newline at end of file diff --git a/Space-Invaders/source/PlayerController.cpp b/Space-Invaders/source/PlayerController.cpp index 05d122b62..e27a1ee91 100644 --- a/Space-Invaders/source/PlayerController.cpp +++ b/Space-Invaders/source/PlayerController.cpp @@ -8,13 +8,21 @@ namespace player{ using namespace Global; void PlayerController::processinput() { + EventService* event_service = ServiceLocator::getInstance()->getEventService(); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - processmoveright(); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + if (event_service->pressedLeftKey() || event_service->pressedAKey()) + { processmoveleft(); } + + if (event_service->pressedRightKey() || event_service->pressedDKey()) + { + processmoveright(); + } + + + + } void PlayerController::processmoveleft() From ec92637619add050f45770e80c7c61d90e2c08e8 Mon Sep 17 00:00:00 2001 From: Tarun Date: Sun, 4 Aug 2024 19:49:21 +0530 Subject: [PATCH 14/15] again created the ui service and ui managnemnt kit --- .../Headers/Global/ServiceLocator.h | 8 +- .../MainMenu/MainMenuUIController.cpp | 143 ------------------ .../Headers/UIService/UIService.cpp | 60 -------- Space-Invaders/Headers/UIService/UIService.h | 20 --- Space-Invaders/Headers/player/PlayerService.h | 2 + Space-Invaders/Space-Invaders.vcxproj | 8 +- .../UI/MainMenu/MainMenuUIController.cpp | 109 +++++++++++++ .../MainMenu/MainMenuUIController.h | 49 +++--- Space-Invaders/UI/UISERVICE.cpp | 44 ++++++ Space-Invaders/UI/UISERVICE.h | 19 +++ Space-Invaders/source/PlayerService.cpp | 5 +- Space-Invaders/source/ServiceLocator.cpp | 29 ++-- 12 files changed, 222 insertions(+), 274 deletions(-) delete mode 100644 Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp delete mode 100644 Space-Invaders/Headers/UIService/UIService.cpp delete mode 100644 Space-Invaders/Headers/UIService/UIService.h create mode 100644 Space-Invaders/UI/MainMenu/MainMenuUIController.cpp rename Space-Invaders/{Headers/UIService => UI}/MainMenu/MainMenuUIController.h (63%) create mode 100644 Space-Invaders/UI/UISERVICE.cpp create mode 100644 Space-Invaders/UI/UISERVICE.h diff --git a/Space-Invaders/Headers/Global/ServiceLocator.h b/Space-Invaders/Headers/Global/ServiceLocator.h index f3c9ec9be..a1f61be32 100644 --- a/Space-Invaders/Headers/Global/ServiceLocator.h +++ b/Space-Invaders/Headers/Global/ServiceLocator.h @@ -4,8 +4,7 @@ #include"../EVENT/EventService.h" #include"../player/PlayerService.h" #include"../TIME/TimeService .h" -#include"../../Headers/UIService/UIService.h" - +#include"../../UI/UISERVICE.h" namespace Global { using namespace player; using namespace Graphic; @@ -20,7 +19,8 @@ namespace Global { EventService* event_service; PlayerService* player_service; TimeService* time_Service; - UiService* uiservice; + UiService* ui_service; + // Private Constructor and Destructor: ServiceLocator(); // Constructor for initializing the ServiceLocator. @@ -42,6 +42,6 @@ namespace Global { EventService* getEventService(); PlayerService* getplayerservice(); TimeService* gettimeservice(); - UiService* getuiservice(); + UiService* getUiservice(); }; } \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp b/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp deleted file mode 100644 index e759716c4..000000000 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.cpp +++ /dev/null @@ -1,143 +0,0 @@ -#include "MainMenuUIController.h" -#include"../../Headers/main/GameService.h" -#include"../../Headers/Global/ServiceLocator.h" -#include"../../Headers/Graphic/GraphicService.h" - -namespace UI { - namespace MainMenu { - - using namespace Global; - using namespace Main; - using namespace Graphic; - using namespace event; - - UI::MainMenu::MainMenuUIController::MainMenuUIController() - { - game_window = nullptr; - } - - void UI::MainMenu::MainMenuUIController::initialize() - { - game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); - initializeBackgroundImage(); - initializeButtons(); - - - } - - void UI::MainMenu::MainMenuUIController::update() - { - processButtonInteractions(); - } - - void UI::MainMenu::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); - - - - - } - void MainMenuUIController::initializeBackgroundImage() - { - 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(); - } - - } - 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::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 = ServiceLocator::getInstance()->getEventService(); - return event_service->leftmousebutton() && button_sprite->getGlobalBounds().contains(mouse_position); - } - } -} \ No newline at end of file diff --git a/Space-Invaders/Headers/UIService/UIService.cpp b/Space-Invaders/Headers/UIService/UIService.cpp deleted file mode 100644 index 33781fe3d..000000000 --- a/Space-Invaders/Headers/UIService/UIService.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "UIService.h" -#include"../main/GameService.h" - - - -using namespace Main; -using namespace MainMenu; - - -void UI::UiService::createControllers() -{ - main_menu_controller = new MainMenuUIController(); -} - -void UI::UiService::initializeControllers() -{ - main_menu_controller->initialize(); -} - -void UI::UiService::destroy() -{ - delete(main_menu_controller); -} - -UI::UiService::UiService() -{ - main_menu_controller = nullptr; - createControllers(); -} - -UI::UiService::~UiService() -{ - destroy(); -} - -void UI::UiService::initialize() -{ - initializeControllers(); -} - -void UI::UiService::update() -{ - - switch (GameService::getGameState()) - { - case GameState::MAIN_MENU: - return main_menu_controller->update(); - break; - } -} - -void UI::UiService::render() -{ - switch (GameService::getGameState()) - { - case GameState::MAIN_MENU: - return main_menu_controller->render(); - break; - } -} diff --git a/Space-Invaders/Headers/UIService/UIService.h b/Space-Invaders/Headers/UIService/UIService.h deleted file mode 100644 index 65de033b8..000000000 --- a/Space-Invaders/Headers/UIService/UIService.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include"../../Headers/UIService/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/Headers/player/PlayerService.h b/Space-Invaders/Headers/player/PlayerService.h index 3819a008c..e8620c5f4 100644 --- a/Space-Invaders/Headers/player/PlayerService.h +++ b/Space-Invaders/Headers/player/PlayerService.h @@ -8,6 +8,8 @@ namespace player { class PlayerService { + + private: diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 67c83d458..6d436d8bf 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -132,8 +132,6 @@ - - @@ -144,6 +142,8 @@ + + @@ -155,8 +155,8 @@ - - + + diff --git a/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp b/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp new file mode 100644 index 000000000..432ca1171 --- /dev/null +++ b/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp @@ -0,0 +1,109 @@ +#include "MainMenuUIController.h" +#include"../../Headers/main/GameService.h" +#include"../../Headers/Global/ServiceLocator.h" +#include"../../Headers/Graphic/GraphicService.h" +namespace UI { + + namespace MainMenu { + + using namespace Global; + using namespace Main; + using namespace Graphic; + using namespace event; + + void MainMenuUI::initializeBackgroundImage() + { + if (background_texture.loadFromFile(background_texture_path)) + { + background_sprite.setTexture(background_texture); + scaleBackgroundImage(); + } + } + + void MainMenuUI::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 MainMenuUI::initializeButtons() + { + } + + bool MainMenuUI::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 MainMenuUI::setButtonSprites() + { + play_button_sprite.setTexture(play_button_texture); + instructions_button_sprite.setTexture(instructions_button_texture); + quit_button_sprite.setTexture(quit_button_texture); + } + + void MainMenuUI::scaleAllButttons() + { + scaleButton(&play_button_sprite); + scaleButton(&instructions_button_sprite); + scaleButton(&quit_button_sprite); + } + + void MainMenuUI::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 MainMenuUI::positionButtons() + { + float xposition = (static_cast(game_window->getSize().x) / 2) - button_width / 2; + play_button_sprite.setPosition(xposition, 540); + instructions_button_sprite.setPosition(xposition, 560); + quit_button_sprite.setPosition(xposition, 580); + } + + UI::MainMenu::MainMenuUI::MainMenuUI() + { + game_window = nullptr; + } + + UI::MainMenu::MainMenuUI::~MainMenuUI() + { + + } + + void UI::MainMenu::MainMenuUI::intialize() + { + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); + initializeButtons(); + initializeBackgroundImage(); + + } + + void UI::MainMenu::MainMenuUI::update() + { + + } + + void UI::MainMenu::MainMenuUI::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/Headers/UIService/MainMenu/MainMenuUIController.h b/Space-Invaders/UI/MainMenu/MainMenuUIController.h similarity index 63% rename from Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h rename to Space-Invaders/UI/MainMenu/MainMenuUIController.h index 19369eda7..c842e1d12 100644 --- a/Space-Invaders/Headers/UIService/MainMenu/MainMenuUIController.h +++ b/Space-Invaders/UI/MainMenu/MainMenuUIController.h @@ -1,22 +1,19 @@ #pragma once -#include +#include +namespace UI { + namespace MainMenu { + class MainMenuUI { -namespace UI -{ - namespace MainMenu - - { - - class MainMenuUIController - { private: + sf::RenderWindow* game_window; + const float button_height = 100.f; + const float button_width = 100.f; 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 instructions_button_texture_path = "assets/textures/instructions_button.png"; const sf::String quit_button_texture_path = "assets/textures/quit_button.png"; - sf::Texture background_texture; sf::Sprite background_sprite; @@ -29,20 +26,10 @@ namespace UI sf::Texture quit_button_texture; sf::Sprite quit_button_sprite; - - - - - - - const float button_height = 400.f; - const float button_width = 140.f; - - void initializeBackgroundImage(); void scaleBackgroundImage(); - // however, we have 3 buttons so it's better to create a seperate function to check if they are loaded + void initializeButtons(); bool loadButtonTexturesFromFile(); void setButtonSprites(); @@ -51,24 +38,24 @@ namespace UI void scaleButton(sf::Sprite* button_to_scale); void positionButtons(); - void processButtonInteractions(); - bool clickedButton(sf::Sprite*, sf::Vector2f); - + public: + MainMenuUI(); + ~MainMenuUI(); - sf::RenderWindow* game_window; - public: - MainMenuUIController(); - void initialize(); + void intialize(); void update(); void render(); - }; - } + + + +}; +} } \ No newline at end of file diff --git a/Space-Invaders/UI/UISERVICE.cpp b/Space-Invaders/UI/UISERVICE.cpp new file mode 100644 index 000000000..49b0e2a69 --- /dev/null +++ b/Space-Invaders/UI/UISERVICE.cpp @@ -0,0 +1,44 @@ +#include "UISERVICE.h" +#include"../../Headers/main/GameService.h" +namespace UI { + using namespace Main; + using namespace MainMenu; + void UI::UiService::createControllers() + { + main_menu_controller = new MainMenuUI(); + } + + void UI::UiService::intializecontrollers() + { + main_menu_controller->intialize(); + } + + void UI::UiService::destroycontrollers() + { + delete(main_menu_controller); + } + + UI::UiService::UiService() + { + main_menu_controller = nullptr; + createControllers(); + } + + UI::UiService::~UiService() + { + destroycontrollers(); + } + + void UI::UiService::intialize() + { + intializecontrollers(); + } + + void UI::UiService::update() + { + } + + void UI::UiService::render() + { + } +} \ No newline at end of file diff --git a/Space-Invaders/UI/UISERVICE.h b/Space-Invaders/UI/UISERVICE.h new file mode 100644 index 000000000..aefc88036 --- /dev/null +++ b/Space-Invaders/UI/UISERVICE.h @@ -0,0 +1,19 @@ +#pragma once +#include"../../UI/MainMenu/MainMenuUIController.h" +namespace UI { + class UiService { + + private: + void createControllers(); + void intializecontrollers(); + void destroycontrollers(); + MainMenu::MainMenuUI* main_menu_controller; + public: + UiService(); + ~UiService(); + void intialize(); + void update(); + void render(); + }; + +} \ No newline at end of file diff --git a/Space-Invaders/source/PlayerService.cpp b/Space-Invaders/source/PlayerService.cpp index 8729355e4..6ccda2e3a 100644 --- a/Space-Invaders/source/PlayerService.cpp +++ b/Space-Invaders/source/PlayerService.cpp @@ -9,6 +9,7 @@ namespace player { using namespace event; using namespace Global; + void PlayerService::initializePlayerSprite() { if (player_texture.loadFromFile(player_texture_path)) { @@ -57,9 +58,10 @@ namespace player { } void PlayerService::render() + { player_controller->render(); - game_window->draw(player_sprite); + } void PlayerService::moveLef() @@ -85,4 +87,5 @@ namespace player { { return position; } + } \ No newline at end of file diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index c1f78bb6f..3ad706fe7 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -13,7 +13,7 @@ namespace Global { event_service = nullptr; player_service = nullptr; time_Service = nullptr; - uiservice = nullptr; + ui_service = nullptr; createServices(); // Call createServices to instantiate services } @@ -29,7 +29,7 @@ namespace Global { player_service = new PlayerService(); time_Service = new TimeService(); - uiservice = new UiService(); + ui_service = new UiService(); } // Deletes allocated services to prevent memory leaks, specifically the graphic service. @@ -38,12 +38,13 @@ namespace Global { delete(event_service); delete(player_service); delete(time_Service); - delete(uiservice); + delete(ui_service); + } // Returns a pointer to ServiceLocator. ServiceLocator* ServiceLocator::getInstance() { - static ServiceLocator instance; // we will discuss what 'static' means at a later time. + static ServiceLocator instance; return &instance; // Return address of the instance } @@ -53,7 +54,7 @@ namespace Global { event_service->initialize(); player_service->initialize(); time_Service->intialize(); - uiservice->initialize(); + ui_service->intialize(); } // Updates the state of the graphic service. @@ -62,17 +63,21 @@ namespace Global { event_service->update(); player_service->update(); time_Service->update(); - uiservice->update(); + ui_service->update(); } // Renders using the graphic service. void ServiceLocator::render() { - uiservice->render(); + + player_service->render(); graphic_service->render(); // Render graphic service + + // Render graphic service + player_service->render(); - + ui_service->render(); } @@ -93,10 +98,12 @@ namespace Global { return time_Service; } - UiService* ServiceLocator::getuiservice() { - - return uiservice; + UiService* ServiceLocator::getUiservice() + { + return ui_service; } + + } From 55dbc087c6e478aa9939eda4b8db20fd5ced0b47 Mon Sep 17 00:00:00 2001 From: Tarun Date: Mon, 5 Aug 2024 11:00:13 +0530 Subject: [PATCH 15/15] Just corrected the corrupt files in the game --- Space-Invaders/Headers/EVENT/EventService.h | 4 ++ Space-Invaders/Headers/main/GameService.h | 3 ++ Space-Invaders/Headers/player/PlayerModel.h | 8 ++-- .../UI/MainMenu/MainMenuUIController.cpp | 42 +++++++++++++++---- .../UI/MainMenu/MainMenuUIController.h | 7 ++-- Space-Invaders/UI/UISERVICE.cpp | 21 ++++++++++ Space-Invaders/source/EventService.cpp | 18 ++++++-- Space-Invaders/source/ServiceLocator.cpp | 5 ++- 8 files changed, 87 insertions(+), 21 deletions(-) diff --git a/Space-Invaders/Headers/EVENT/EventService.h b/Space-Invaders/Headers/EVENT/EventService.h index 3591e0e4c..7da3d125e 100644 --- a/Space-Invaders/Headers/EVENT/EventService.h +++ b/Space-Invaders/Headers/EVENT/EventService.h @@ -10,6 +10,7 @@ namespace event { RELEASED, }; + class EventService { private: @@ -49,8 +50,11 @@ namespace event { bool leftmousebutton(); bool rightmousebutton(); + bool pressedLeftMouseButton(); + bool pressedRightMouseButton(); bool pressedAKey(); bool pressedDKey(); + }; } \ No newline at end of file diff --git a/Space-Invaders/Headers/main/GameService.h b/Space-Invaders/Headers/main/GameService.h index 58eb6ce2c..8850e9ade 100644 --- a/Space-Invaders/Headers/main/GameService.h +++ b/Space-Invaders/Headers/main/GameService.h @@ -9,6 +9,9 @@ enum class GameState //create the enum GAMEPLAY, }; + + + namespace Main { using namespace Global; diff --git a/Space-Invaders/Headers/player/PlayerModel.h b/Space-Invaders/Headers/player/PlayerModel.h index 90d116e84..eb54839b4 100644 --- a/Space-Invaders/Headers/player/PlayerModel.h +++ b/Space-Invaders/Headers/player/PlayerModel.h @@ -19,7 +19,7 @@ namespace player { { private: - const sf::Vector2f initial_player_position = sf::Vector2f(500.f, 500.f); //new var + const sf::Vector2f initial_player_position = sf::Vector2f(950.f, 950.f); //new var sf::Vector2f player_position; //new var PlayerState player_state; PlayerModel* model; @@ -28,10 +28,10 @@ namespace player { 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 = 200.0f; + const float player_movement_speed = 350.0f; PlayerModel(); ~PlayerModel(); diff --git a/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp b/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp index 432ca1171..e7fbf934c 100644 --- a/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp +++ b/Space-Invaders/UI/MainMenu/MainMenuUIController.cpp @@ -24,16 +24,20 @@ namespace UI { { 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 - + static_cast(game_window->getSize().x) / background_sprite.getTexture()->getSize().x, + static_cast(game_window->getSize().y) / background_sprite.getTexture()->getSize().y ); } void MainMenuUI::initializeButtons() { + if (loadButtonTexturesFromFile()) + { + // order of function calls matter + setButtonSprites(); + scaleAllButttons(); + positionButtons(); + } } bool MainMenuUI::loadButtonTexturesFromFile() @@ -69,9 +73,9 @@ namespace UI { void MainMenuUI::positionButtons() { float xposition = (static_cast(game_window->getSize().x) / 2) - button_width / 2; - play_button_sprite.setPosition(xposition, 540); - instructions_button_sprite.setPosition(xposition, 560); - quit_button_sprite.setPosition(xposition, 580); + play_button_sprite.setPosition(xposition, 500); + instructions_button_sprite.setPosition(xposition,700); + quit_button_sprite.setPosition(xposition, 900); } UI::MainMenu::MainMenuUI::MainMenuUI() @@ -94,6 +98,7 @@ namespace UI { void UI::MainMenu::MainMenuUI::update() { + processButtonInteractions(); } @@ -105,5 +110,26 @@ namespace UI { game_window->draw(instructions_button_sprite); game_window->draw(quit_button_sprite); } + void MainMenuUI::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 MainMenuUI::clickedButton(sf::Sprite* button_sprite, sf::Vector2f mouse_position) + { + + EventService* event_service = ServiceLocator::getInstance()->getEventService(); + return event_service->pressedLeftMouseButton() && button_sprite->getGlobalBounds().contains(mouse_position); + } } } \ No newline at end of file diff --git a/Space-Invaders/UI/MainMenu/MainMenuUIController.h b/Space-Invaders/UI/MainMenu/MainMenuUIController.h index c842e1d12..3ed4df406 100644 --- a/Space-Invaders/UI/MainMenu/MainMenuUIController.h +++ b/Space-Invaders/UI/MainMenu/MainMenuUIController.h @@ -6,8 +6,8 @@ namespace UI { private: sf::RenderWindow* game_window; - const float button_height = 100.f; - const float button_width = 100.f; + const float button_height = 200.f; + const float button_width = 200.f; const sf::String background_texture_path = "assets/textures/space_invaders_bg.png"; const sf::String play_button_texture_path = "assets/textures/play_button.png"; @@ -54,7 +54,8 @@ namespace UI { void render(); - + void processButtonInteractions(); + bool clickedButton(sf::Sprite* button_sprite, sf::Vector2f mouse_position); }; } diff --git a/Space-Invaders/UI/UISERVICE.cpp b/Space-Invaders/UI/UISERVICE.cpp index 49b0e2a69..158fc78c2 100644 --- a/Space-Invaders/UI/UISERVICE.cpp +++ b/Space-Invaders/UI/UISERVICE.cpp @@ -31,14 +31,35 @@ namespace UI { void UI::UiService::intialize() { + + + + main_menu_controller->intialize(); intializecontrollers(); + + + } void UI::UiService::update() { + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->update(); + break; + } } void UI::UiService::render() { + + + switch (GameService::getGameState()) + { + case GameState::MAIN_MENU: + return main_menu_controller->render(); + break; + } } } \ No newline at end of file diff --git a/Space-Invaders/source/EventService.cpp b/Space-Invaders/source/EventService.cpp index 91ac93fae..70f1de3f7 100644 --- a/Space-Invaders/source/EventService.cpp +++ b/Space-Invaders/source/EventService.cpp @@ -123,14 +123,24 @@ namespace event { return game_event.type==sf::Event::MouseButtonPressed && game_event.mouseButton.button == sf::Mouse::Right; } - bool EventService::pressedAKey() + bool EventService::pressedLeftMouseButton() { - return false; + 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; + } + bool EventService::pressedAKey() + { + return A_button_state == ButtonState::HELD; + } bool EventService::pressedDKey() { - return false; + return D_button_state == ButtonState::HELD; } + } + + -} \ No newline at end of file diff --git a/Space-Invaders/source/ServiceLocator.cpp b/Space-Invaders/source/ServiceLocator.cpp index 3ad706fe7..9e21dc4c3 100644 --- a/Space-Invaders/source/ServiceLocator.cpp +++ b/Space-Invaders/source/ServiceLocator.cpp @@ -71,13 +71,14 @@ namespace Global { void ServiceLocator::render() { - player_service->render(); + graphic_service->render(); // Render graphic service // Render graphic service - player_service->render(); ui_service->render(); + + }