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
23 changes: 23 additions & 0 deletions Header/Core/GameLoop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "../../Header/Core/GameWindowManager.h"
#include "../../Header/Event/EventManager.h"
#include "../Gameplay/GameplayManager.h"


namespace Core
{
class GameLoop
{
private:
Events::EventManager* eventManager;
GameWindowManager* gameWindowManager;
Gameplay::GameplayManager* gameplayManager;

public:
void initialize();
bool isGameRunning();
void pollEvents();
void update();
void render();
};

}
25 changes: 25 additions & 0 deletions Header/Core/GameWindowManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <SFML/Graphics.hpp>


using namespace sf;
namespace Core
{
class GameWindowManager
{
private:
int gameWindowHeight = 720;
int gameWindowWidth = 1280;
std::string gameTitle = "SFML_Pong";

RenderWindow* game_window;
void createGameWindow();
public:
void initialize();
RenderWindow* getGameWindow();
bool isGameRunning();
void clearGameWindow();
void displayGameWindow();
};

}
44 changes: 44 additions & 0 deletions Header/Core/UIService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <SFML/Graphics.hpp>
#include <string>
using namespace sf;

namespace Core
{
class UIService
{
private:
Font font;
Text left_score_text;
Text right_score_text;

std::string texture_path = "Assets/Fonts/Aloevera-OVoWO.ttf";

int font_size = 40;
Color font_color = Color::White;
std::string initial_string = "00";

float left_score_postion_x = 570.0f;
float left_score_postion_y = 30.0f;

float right_score_position_x = 680.0f;
float right_score_position_y = 30.0f;

int player1_score = 0;
int player2_score = 0;
std::string formatScore(int score);

void loadFontTexture();
void createLeftScoreText();
void createRightScoreText();
public:
UIService();

void render(RenderWindow* game_window);
void update();
void incrementPlayer1Score();
void incrementPlayer2Score();

};
}
56 changes: 56 additions & 0 deletions Header/Entities/Ball.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once
#include <SFML/Graphics.hpp>
#include <string>
using namespace sf;

namespace Gameplay
{
class Ball
{
private:

Sprite pong_ball_sprite;
const float scale_x = 0.05f; // 20% of original size
const float scale_y = 0.05f; // 20% of original size
float ball_speed = 0.5f;
Vector2f velocity = Vector2f(ball_speed, ball_speed);
RectangleShape paddle1;
RectangleShape paddle2;
const float top_boundary = 20.0f;
const float bottom_boundary = 700.0f;
const float left_boundary = 0.0f;
const float right_boundary = 1280.0f;

//Center Position
const float center_position_x = 615.0f;
const float center_position_y = 325.0f;
Texture pong_ball_texture;
const std::string texture_path = "Assets/Textures/Ball.png";
const float radius = 10.0f;
const float position_x = 640.0f;
const float position_y = 360.0f;
const float speed_constant = 550;
bool had_left_collision;
bool had_right_collision;

void loadTexture();
void prepareSprite();
void handlePaddleCollision();
void move();
void reset();
void handleOutOfBoundsCollision();
void onCollision();


public:
Ball(RectangleShape paddle1, RectangleShape paddle2);
void handleBoudaryCollision();
void update();
void render(RenderWindow* game_window);
bool isRightCollisionOccured();
bool isLeftCollisionOccured();
void updateRightCollisionState(bool value);
void updateLeftCollisionState(bool value);

};
}
63 changes: 63 additions & 0 deletions Header/Entities/Boundary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;

namespace Gameplay
{
class Boundary
{
private:
RectangleShape topBoundary;

RectangleShape leftBoundary;
RectangleShape centerLine;
RectangleShape rightBoundary;
RectangleShape bottomBoundary;

const float horizontal_boundary_width = 1280.0f;
const float horizontal_boundary_height = 20.0f;

// Vertical boundaries dimensions (left and right)
const float vertical_boundary_width = 20.0f;
const float vertical_boundary_height = 720.0f;

const float top_position_x = 0.0f;
const float top_position_y = 0.0f;

const float left_position_x = 0.0f;
const float left_position_y = 0.0f;

const float right_position_x = horizontal_boundary_width - vertical_boundary_width;
const float right_position_y = 0.0f;

const float bottom_position_x = 0.0f;
const float bottom_position_y = vertical_boundary_height - horizontal_boundary_height;

const float top_boundary = 20.0f;
const float bottom_boundary = 700.0f;

//Boundary Colors
const Color boundary_color = Color::Blue;
const Color center_line_color = Color::White;

//center lines properties
const float center_line_width = 10.0f;
const float center_line_height = 680.0f;

const float center_line_position_x = 640.0f;
const float center_line_position_y = 20.0f;

//create boundaries and the center line
void createTopBoundary();
void createBottomBoundary();
void createLeftBoundary();
void createRightBoundary();

void createCenterLine();

public:
Boundary();
void render(RenderWindow* game_window);
};
}

27 changes: 27 additions & 0 deletions Header/Entities/Paddle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;

namespace Gameplay
{
class Paddle
{
private:
RectangleShape paddle_sprite;
const float paddle_width = 20.0f;
const float paddle_height = 140.0f;
const float paddleSpeed = 0.5f;
const float top_boundary = 20.0f;
const float paddle_speed_constant = 550.0f;
const float bottom_boundary = 700.0f;
void movePaddle(bool move_up_key_pressed, bool move_down_key_pressed);
public:
Paddle(float position_x, float position_y);
void update(bool move_up_key_pressed, bool move_down_key_pressed);
void render(RenderWindow* game_window);
RectangleShape getPaddleSprite() const { return paddle_sprite; }
void reset(float position_x, float position_y);

};
}

15 changes: 15 additions & 0 deletions Header/Event/EventManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;

namespace Events
{
class EventManager
{
public:
void pollEvents(RenderWindow* gameWindow);
bool isKeyPressed(Keyboard::Key key);
bool isLeftMouseButtonPressed();

};
}
36 changes: 36 additions & 0 deletions Header/Gameplay/GameplayManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include "../../Header/Entities/Paddle.h"
#include "../../Header/Entities/Ball.h"
#include "../Event/EventManager.h"
#include "../../Header/Entities/Boundary.h"
#include "../../Header/Utility/TimeService.h"
#include "../../Header/Core/UIService.h"

namespace Gameplay
{
class GameplayManager
{
private:

float player1_position_x = 40.0f;
float player1_position_y = 300.0f;

float player2_position_x = 1210.0f;
float player2_position_y = 300.0f;

Paddle* player1_paddle;
Paddle* player2_paddle;
Ball* ball;
Boundary* boundary;
Core::UIService* ui_service;
Utility::TimeService* time_service;
Events::EventManager* event_manager;
void initialize();
void resetPlayers();
public:
GameplayManager(Events::EventManager* manager);
void render(sf::RenderWindow* game_window);
void update();
void UpdateScore();
};
} // namespace Gameplay
25 changes: 25 additions & 0 deletions Header/Utility/TimeService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <chrono>

namespace Utility
{
class TimeService
{
private:
std::chrono::steady_clock::time_point previous_time;
float delta_time;
void updateDeltaTime();
float calculateDeltaTime();
void updatePreviousTime(); // Update previous_time to the current time
void initialize();
static TimeService* instance; // Static instance for singleton pattern
TimeService();

TimeService(const TimeService&) = delete; // Disable copy
TimeService& operator=(const TimeService&) = delete; // Disable assignment
public:
static TimeService* getInstance();
void update();
float getDeltaTime();
};
}
28 changes: 12 additions & 16 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Header/Core/GameLoop.h"

int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);

Core::GameLoop* gameLoop = new Core::GameLoop();
gameLoop->initialize();

while (gameLoop->isGameRunning())
{
gameLoop->pollEvents();
gameLoop->update();
gameLoop->render();
}

while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}

window.clear();
window.draw(shape);
window.display();
}
return 0;
}
Loading