Skip to content

Commit

Permalink
Hid console on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
LxtteDev committed Feb 25, 2023
1 parent 3c40f54 commit 2cddb8f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ target_link_libraries(Pong sfml-graphics)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
include(CPack)
7 changes: 4 additions & 3 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Game::Game(sf::Vector2f size):
mBackground(Background(mWindow)),
mPlayer(Player(mWindow)),
mBall(Ball(mWindow)),
mScore(Score(mWindow)),
mEnemy(Enemy(mWindow, mBall))
mEnemy(Enemy(mWindow, mBall)),
mScore(Score(mWindow, mBall))
{
std::cout << "Creating game" << std::endl;

Expand Down Expand Up @@ -36,9 +36,10 @@ void Game::display() {
Game::mPlayer.Resize();
Game::mBall.Resize();
Game::mEnemy.Resize();
Game::mScore.Resize();
}

// this->mWindow.clear();
this->mWindow.clear();

Game::mBackground.Draw(this->deltaTime);
Game::mPlayer.Draw(this->deltaTime);
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#include <iostream>
#include "game/game.h"

Expand Down
59 changes: 41 additions & 18 deletions src/score/score.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,54 @@
#include "score.h"

Score::Score(sf::RenderWindow &window): mWindow(window), playerText(sf::Text()), enemyText(sf::Text()) {
sf::Font arial;
arial.loadFromFile("content/arial.ttf");
Score::Score(sf::RenderWindow &window, Ball& ball): mWindow(window), mBall(ball) {
Score::font.loadFromFile("content/font.ttf");
const_cast<sf::Texture&>(Score::font.getTexture(128)).setSmooth(false);

Score::playerText.setFont(arial);
Score::enemyText.setFont(arial);

Score::playerText.setCharacterSize(60);
Score::enemyText.setCharacterSize(60);

Score::playerText.setPosition(sf::Vector2f(0.0f, 0.0f));
Score::enemyText.setPosition(sf::Vector2f(0.0f, 0.0f));

Score::updateScore(0, 0);

Score::mWindow.draw(Score::playerText);
Score::Resize();
}

void Score::updateScore(int player, int enemy) {
Score::playerScore += player;
Score::enemyScore += enemy;

Score::playerText.setString("00");
Score::enemyText.setString("00");
}

void Score::Resize() {
sf::Vector2f windowSize(this->mWindow.getView().getSize());

Score::playerPosition = sf::Vector2f(windowSize.x / 2 - 10.0f, 21.0f);
Score::enemyPosition = sf::Vector2f(windowSize.x / 2 + 10.0f, 21.0f);
}

void Score::draw(float deltaTime) {
// Score::mWindow.draw(Score::enemyText);
// Update scores
sf::Vector2f windowSize(this->mWindow.getView().getSize());
sf::Vector2f ballPosition = Score::mBall.getPosition();
sf::Vector2f ballVelocity = Score::mBall.getVelocity();
if (canScore) {
if (ballPosition.x <= 6 && ballVelocity.x < 0) {
Score::updateScore(1, 0);
canScore = 0;
} else if (ballPosition.x >= windowSize.x - 6 && ballVelocity.x > 0) {
Score::updateScore(0, 1);
canScore = 0;
}
} else {
float centre = windowSize.x / 2;
if (ballPosition.x >= centre - 15.0f && ballPosition.x <= centre + 15.05)
canScore = 1;
}

// Draw text
std::string playerScore = std::to_string(this->playerScore);
std::string enemyScore = std::to_string(this->enemyScore);

sf::Text player(enemyScore, Score::font, 64);
player.setOrigin(sf::Vector2f(player.getLocalBounds().width, 0));
player.setPosition(Score::playerPosition);
Score::mWindow.draw(player);

sf::Text enemy(playerScore, Score::font, 64);
enemy.setPosition(Score::enemyPosition);
Score::mWindow.draw(enemy);
}
14 changes: 10 additions & 4 deletions src/score/score.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include "../ball/ball.h"

#ifndef SCORE_H_
#define SCORE_H_

class Score {
public:
Score(sf::RenderWindow& window);
Score(sf::RenderWindow& window, Ball& ball);

void updateScore(int player, int enemey);
void Resize();
void draw(float deltaTime);
private:
Ball& mBall;
sf::RenderWindow& mWindow;
sf::Font font;

sf::Text& playerText;
int playerScore = 0;
sf::Vector2f playerPosition;
sf::Vector2f enemyPosition;

sf::Text& enemyText;
bool canScore = 1;
int playerScore = 0;
int enemyScore = 0;
};

Expand Down

0 comments on commit 2cddb8f

Please sign in to comment.