Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Space-Invaders/Header/EventService.cpp
Original file line number Diff line number Diff line change
@@ -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; }
27 changes: 27 additions & 0 deletions Space-Invaders/Header/EventService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>

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();

};
25 changes: 25 additions & 0 deletions Space-Invaders/Header/GameService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <SFML/Graphics.hpp>
#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.
};
37 changes: 37 additions & 0 deletions Space-Invaders/Header/GraphicService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <SFML/Graphics.hpp>

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
};
33 changes: 33 additions & 0 deletions Space-Invaders/Header/ServiceLocator.h
Original file line number Diff line number Diff line change
@@ -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();

};
Empty file.
64 changes: 64 additions & 0 deletions Space-Invaders/Source/GameService.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
55 changes: 55 additions & 0 deletions Space-Invaders/Source/GraphicService.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
62 changes: 62 additions & 0 deletions Space-Invaders/Source/ServiceLocator.cpp
Original file line number Diff line number Diff line change
@@ -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; }









Loading