diff --git a/Space-Invaders/Header/EventService.cpp b/Space-Invaders/Header/EventService.cpp new file mode 100644 index 000000000..c7f9d4a29 --- /dev/null +++ b/Space-Invaders/Header/EventService.cpp @@ -0,0 +1,42 @@ +#include "../Header/EventService.h" +#include "../Header/GameService.h" +#include "../Header/GraphicService.h" + +EventService::EventService() { game_window = nullptr; } + +EventService::~EventService() = default; //calls the default destructor + +void EventService::initialize() +{ + game_window = ServiceLocator::getInstance()->getGraphicService()->getGameWindow(); +} + +void EventService::update() +{ + //for later +} + +void EventService::processEvents() +{ + if (isGameWindowOpen()) { + while (game_window->pollEvent(game_event)) { + // Check for window closure + if (gameWindowWasClosed() || hasQuitGame()) + { + game_window->close(); + } + } + } +} + +bool EventService::hasQuitGame() { return (isKeyboardEvent() && pressedEscapeKey()); } // only true if the ESC key is pressed and a keyboard event has been registered + +//checks for if a keyboard key has been pressed +bool EventService::isKeyboardEvent() { return game_event.type == sf::Event::KeyPressed; } + +//control click on the SFML functions to see what they do internally +bool EventService::pressedEscapeKey() { return game_event.key.code == sf::Keyboard::Escape; } + +bool EventService::isGameWindowOpen() { return game_window != nullptr; } + +bool EventService::gameWindowWasClosed() { return game_event.type == sf::Event::Closed; } diff --git a/Space-Invaders/Header/EventService.h b/Space-Invaders/Header/EventService.h new file mode 100644 index 000000000..dde134f20 --- /dev/null +++ b/Space-Invaders/Header/EventService.h @@ -0,0 +1,27 @@ +#pragma once +#include +#include + +class EventService +{ +private: + sf::Event game_event; //event var + sf::RenderWindow* game_window; //ptr to our game window + + bool isGameWindowOpen(); + bool gameWindowWasClosed(); //for the condition we already had - the title bar cross. + bool hasQuitGame(); //for our new 'ESC' condition + + + +public: + EventService(); + ~EventService(); + + void initialize(); + void update(); + void processEvents(); // while window is open we will check for events + bool pressedEscapeKey(); + bool isKeyboardEvent(); + +}; diff --git a/Space-Invaders/Header/GameService.h b/Space-Invaders/Header/GameService.h new file mode 100644 index 000000000..acf939667 --- /dev/null +++ b/Space-Invaders/Header/GameService.h @@ -0,0 +1,25 @@ +#pragma once +#include +#include "../Header/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/Header/GraphicService.h b/Space-Invaders/Header/GraphicService.h new file mode 100644 index 000000000..05399d5d4 --- /dev/null +++ b/Space-Invaders/Header/GraphicService.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +class GraphicService +{ +private: + + const std::string game_window_title = "Outscal Presents - Alien Invader"; + + const int game_window_width = 800; + const int game_window_height = 600; + + const sf::Color window_color = sf::Color::Blue; + + sf::VideoMode* video_mode; // ptr to video mode + sf::RenderWindow* game_window; // ptr to a RenderWindow + + void setVideoMode(); // Method for setting our video mode + void onDestroy(); // method to run when window is deleted + +public: + GraphicService(); + ~GraphicService(); //cleanup + + //method to create the game window. returns a pointer to an instance of the game window + sf::RenderWindow* createGameWindow(); + + + void initialize(); //lifecycle functions + void update(); //.. + void render(); //.. + bool isGameWindowOpen(); //check if the window is open + + sf::RenderWindow* getGameWindow(); //getter for the game window instance + sf::Color getWindowColor();//get the color +}; diff --git a/Space-Invaders/Header/ServiceLocator.h b/Space-Invaders/Header/ServiceLocator.h new file mode 100644 index 000000000..f18f0ee2b --- /dev/null +++ b/Space-Invaders/Header/ServiceLocator.h @@ -0,0 +1,33 @@ +#pragma once +#include "../Header/GraphicService.h" +#include "../Header/EventService.h" + +class ServiceLocator +{ +private: + // Private Attributes: + GraphicService* graphic_service; + + EventService* event_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(); + +}; diff --git a/Space-Invaders/Source/EventServices.cpp b/Space-Invaders/Source/EventServices.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Space-Invaders/Source/GameService.cpp b/Space-Invaders/Source/GameService.cpp new file mode 100644 index 000000000..499a2d13a --- /dev/null +++ b/Space-Invaders/Source/GameService.cpp @@ -0,0 +1,64 @@ +#include "../Header/GameService.h" +#include "../Header/GraphicService.h" + + +// Constructor: Initializes pointers to null. +GameService::GameService() { + service_locator = nullptr; // Set service locator to null + game_window = nullptr; // Set game window to null +} + +// Destructor: Calls the destroy function to clean up resources. +GameService::~GameService() { + destroy(); // Clean up and release resources +} + +// Prepares the game service for use by obtaining the service locator instance and initializing services. +void GameService::ignite() { + service_locator = ServiceLocator::getInstance(); // Get ServiceLocator + initialize(); // Initialize services. +} + +//initialize service locator and other variables +void GameService::initialize() +{ + service_locator->initialize(); + initializeVariables(); +} + +void GameService::initializeVariables() +{ + game_window = service_locator->getGraphicService()->getGameWindow(); //set game window (it was null before this) +} + +void GameService::destroy() +{ + // don't need to do anything here for now. +} + +// Updates the game logic by delegating to the service locator's update method. +void GameService::update() { + + service_locator->update(); // Call update on the service locator which then updates all its managed services + + // Process Events. + service_locator->getEventService()->processEvents(); + + // Update Game Logic. + service_locator->update(); +} + + +// Clears the window then display it. +void GameService::render() { + // Clears the game window with the background color provided by the graphic service + game_window->clear(service_locator->getGraphicService()->getWindowColor()); + service_locator->render(); // Render the current frame using the service locator + game_window->display(); // Display the rendered frame on the game window +} + +// Checks if the game is still running by querying the graphic service's window open status. +bool GameService::isRunning() { + // Returns true if the game window is open, indicating the game is still running + return service_locator->getGraphicService()->isGameWindowOpen(); +} diff --git a/Space-Invaders/Source/GraphicService.cpp b/Space-Invaders/Source/GraphicService.cpp new file mode 100644 index 000000000..93d150b96 --- /dev/null +++ b/Space-Invaders/Source/GraphicService.cpp @@ -0,0 +1,55 @@ +#include "../Header/GraphicService.h" + +// Constructor: Initializes game window and video mode pointers to null. +GraphicService::GraphicService() { + game_window = nullptr; // Initializes game window pointer to null + video_mode = nullptr; // Initializes video mode pointer to null +} + +// Destructor: Cleans up resources by calling onDestroy. +GraphicService::~GraphicService() { + onDestroy(); // Calls onDestroy method to clean up resources +} + +// Initializes the graphic service by creating a new game window. +void GraphicService::initialize() { + game_window = createGameWindow(); // Assigns a new game window to the game_window pointer +} + +// Creates a new SFML RenderWindow object with specified video mode and title. +sf::RenderWindow* GraphicService::createGameWindow() { + setVideoMode(); // Sets up the video mode for the window + return new sf::RenderWindow(*video_mode, game_window_title); // Creates and returns a new RenderWindow object +} + +// Sets up the video mode for the game window using specified dimensions and system's color depth. +void GraphicService::setVideoMode() { + video_mode = new sf::VideoMode(game_window_width, game_window_height, sf::VideoMode::getDesktopMode().bitsPerPixel); // Allocates and sets the video mode +} + +// Cleans up allocated memory for video mode and game window to avoid memory leaks. +void GraphicService::onDestroy() { + delete(video_mode); // Deletes the video mode object + delete(game_window); // Deletes the game window object +} + +// Placeholder function for game update logic. +void GraphicService::update() { } + +// Placeholder function for game rendering logic. +void GraphicService::render() { } + +// Checks if the game window is currently open. +bool GraphicService::isGameWindowOpen() { + return game_window->isOpen(); // Returns the open status of the game window +} + +// Returns a pointer to the game window object. +sf::RenderWindow* GraphicService::getGameWindow() { + return game_window; +} + +// Returns the configured window background color. +sf::Color GraphicService::getWindowColor() { + return window_color; +} \ No newline at end of file diff --git a/Space-Invaders/Source/ServiceLocator.cpp b/Space-Invaders/Source/ServiceLocator.cpp new file mode 100644 index 000000000..f46815a63 --- /dev/null +++ b/Space-Invaders/Source/ServiceLocator.cpp @@ -0,0 +1,62 @@ +#include "../Header/ServiceLocator.h" + +// Constructor: Initializes the graphic_service pointer to null and creates services. +ServiceLocator::ServiceLocator() { + graphic_service = nullptr; // Initialize graphic_service to null + event_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(); +} + +// Deletes allocated services to prevent memory leaks, specifically the graphic service. +void ServiceLocator::clearAllServices() { + delete(graphic_service); // Delete the graphic_service instance + delete(event_service); + graphic_service = nullptr; // Reset pointer to null to avoid dangling pointer +} + +// 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(); +} + +// Updates the state of the graphic service. +void ServiceLocator::update() { + graphic_service->update(); // Update graphic service + event_service->update(); +} + +// 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; } +EventService* ServiceLocator::getEventService() { return event_service; } + + + + + + + + + diff --git a/Space-Invaders/Source/ServiceLocatorOld.cpp b/Space-Invaders/Source/ServiceLocatorOld.cpp new file mode 100644 index 000000000..7070c3c83 --- /dev/null +++ b/Space-Invaders/Source/ServiceLocatorOld.cpp @@ -0,0 +1,47 @@ +#include "../Header/ServiceLocator.h" + +// Constructor: Initializes the graphic_service pointer to null and creates services. +ServiceLocator::ServiceLocator() { + graphic_service = nullptr; // Initialize graphic_service to null + createServices(); // Call createServices to instantiate services +} + +// Destructor: Cleans up resources by clearing all services. +ServiceLocator::~ServiceLocator() { + clearAllServices(); // Call clearAllServices to delete any allocated services +} + +// Creates service instances, specifically the graphic service in this case. +void ServiceLocator::createServices() { + graphic_service = new GraphicService(); // Dynamically create a GraphicService instance +} + +// Deletes allocated services to prevent memory leaks, specifically the graphic service. +void ServiceLocator::clearAllServices() { + delete(graphic_service); // Delete the graphic_service instance + graphic_service = nullptr; // Reset pointer to null to avoid dangling pointer +} + +// Returns a pointer to ServiceLocator. +ServiceLocator* ServiceLocator::getInstance() { + static ServiceLocator instance; // we will discuss what 'static' means at a later time. + return &instance; // Return address of the instance +} + +// Calls initialize on the graphic service, readying it for use. +void ServiceLocator::initialize() { + graphic_service->initialize(); // Initialize graphic service +} + +// Updates the state of the graphic service. +void ServiceLocator::update() { + graphic_service->update(); // Update graphic service +} + +// Renders using the graphic service. +void ServiceLocator::render() { + graphic_service->render(); // Render graphic service +} + +// Returns a pointer to the currently set graphic service. +GraphicService* ServiceLocator::getGraphicService() { return graphic_service; } diff --git a/Space-Invaders/Space-Invaders.vcxproj b/Space-Invaders/Space-Invaders.vcxproj index 6f7fa388d..ffe923c95 100644 --- a/Space-Invaders/Space-Invaders.vcxproj +++ b/Space-Invaders/Space-Invaders.vcxproj @@ -1,4 +1,4 @@ - + @@ -132,7 +132,19 @@ + + + + + + + + + + + + diff --git a/Space-Invaders/Space-Invaders.vcxproj.filters b/Space-Invaders/Space-Invaders.vcxproj.filters index ce0c35ccf..2d13324df 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..c82cf8088 100644 --- a/Space-Invaders/main.cpp +++ b/Space-Invaders/main.cpp @@ -1,5 +1,128 @@ +#include "Header/GameSevice.h" + +int main() { + + GameService gameService; + + gameService.ignite(); + + while (gameService.isRunning()) + { + + gameService.update(); + gameService.render(); + + } + +} + + + + + +/* + +#include + +#include + +class Player { + +private: + + // Private Properties + int health = 3; + sf::Vector2f position = sf::Vector2f(200.0f, 100.0f); + int movement_speed = 1; + int player_score = 0; + +public: + + // Public Properties + sf::Texture player_texture; + sf::Sprite player_sprite; + + //Public Getter & Setter methods + int getScore() { + return player_score; + } + + void setScore(int newScore) { + player_score = newScore; + } + + sf::Vector2f getPosition() { + return position; + } + + + void takeDamage() {}; + void move(float offsetX) { + position.x += offsetX; + + }; + + void move1(float offsetY) { + position.y += offsetY; + + }; + + int getMoveSpeed() { + return movement_speed; + } + + void shootBullets() {}; + +}; + +int main() { + + sf::VideoMode videoMode = sf::VideoMode(800, 600); // Define the video mode (dimensions) + + sf::RenderWindow window(videoMode, "SFML Window"); // Create a window object + + Player player; // Create the player object + + + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + // Check for window closure + if (event.type == sf::Event::Closed) + window.close(); + } + + // Handle keyboard input + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + player.move(-1.0f * player.getMoveSpeed()); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + player.move(1.0f * player.getMoveSpeed()); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { + player.move1(-1.0f * player.getMoveSpeed()); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { + player.move1(1.0f * player.getMoveSpeed()); + } + + + player.player_texture.loadFromFile("assets/textures/player_ship.png"); // Load the player ship texture + + player.player_sprite.setTexture(player.player_texture); // Set the player sprite variable + + // Clear the window + window.clear(sf::Color::Blue); // this code will set a blue background color (optional) + + player.player_sprite.setPosition(player.getPosition()); // Set the position of the player sprite + + window.draw(player.player_sprite); // Draw the player sprite + + window.display(); // Display what was drawn + + } // end while loop -int main() -{ return 0; -} \ No newline at end of file +} + +*/ \ No newline at end of file