Created
September 8, 2015 18:41
-
-
Save mbelicki/450c201712052a84ea78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <SFML/Graphics.hpp> | |
#include <string> | |
#include "Constants.h" | |
#include "Paddle.h" | |
#include "Ball.h" | |
/* This class will hold the state of the game, | |
* the state will be used by initialize, render and update functions, | |
* because the functions will be chaning the state of the class defined below | |
* I decided to implement them as members of the class | |
*/ | |
class GameState { | |
public: | |
GameState(); | |
/* This will initialize the game, if you need some configuration | |
* options, like window resolution you can pass them as arguments to | |
* this method. In case of larger number o arguments pass them as | |
* a const reference to configuration structure */ | |
bool initialize(); | |
/* Handle SMFL events, returns false if game should quit, true | |
* otherwise */ | |
bool handleEvents(); | |
/* Handle all game logic, that is not tied to SMFL events. */ | |
void update(); | |
/* Draw current game state on the screen. */ | |
void render(); | |
private: | |
gamePaddle paddleLeft; | |
gamePaddle paddleRight; | |
gameBall ball; | |
sf::RendererWindow window; | |
}; | |
GameState::GameState() | |
: paddleLeft(sf::Keyboard::Key::W, sf::Keyboard::Key::S, 20); | |
, paddleRight(sf::Keyboard::Key::Up, sf::Keyboard::Key::Down, (windowX-30)); | |
, window(sf::VideoMode(int(windowX),int(windowY)), "PANG"); | |
{ | |
} | |
bool GameState::initialize() { | |
sf::Image icon; | |
if (!icon.loadFromFile("Icon.png")) { | |
return false; | |
} | |
window.setIcon(128, 128, icon.getPixelsPtr()); | |
return true; | |
} | |
bool GameState::handleEvents() { | |
bool quit = false; | |
sf::Event event; | |
while (window.pollEvent(event)) | |
{ | |
if (event.type == sf::Event::Closed) | |
{ | |
window.close(); | |
quit = true; | |
} | |
else if (event.type == sf::Event::KeyPressed) | |
{ | |
if (event.key.code == paddleLeft.getKeyUp()) paddleLeft.setMoveUp(true); | |
if (event.key.code == paddleLeft.getKeyDown()) paddleLeft.setMoveDown(true); | |
if (event.key.code == paddleRight.getKeyUp()) paddleRight.setMoveUp(true); | |
if (event.key.code == paddleRight.getKeyDown()) paddleRight.setMoveDown(true); | |
if (event.key.code == sf::Keyboard::Escape) { | |
window.close(); | |
quit = true; | |
} | |
} | |
else if (event.type == sf::Event::KeyReleased) | |
{ | |
if (event.key.code == paddleLeft.getKeyUp()) paddleLeft.setMoveUp(false); | |
if (event.key.code == paddleLeft.getKeyDown()) paddleLeft.setMoveDown(false); | |
if (event.key.code == paddleRight.getKeyUp()) paddleRight.setMoveUp(false); | |
if (event.key.code == paddleRight.getKeyDown()) paddleRight.setMoveDown(false); | |
} | |
} | |
return quit; | |
} | |
void GameState::update() { | |
paddleLeft.paddleMove(); | |
paddleRight.paddleMove(); | |
ball.ballMove(); | |
bool hitLeftPaddle = ball.getBoundingBox().intersects(paddleLeft.getBoundingBox()); | |
bool hitRightPaddle = ball.getBoundingBox().intersects(paddleRight.getBoundingBox()); | |
bool ballMovingLeft = ball.getVelocityX() < 0; | |
bool ballMovingRight = ball.getVelocityX() > 0; | |
if ((hitLeftPaddle && ballMovingLeft) || (hitRightPaddle && ballMovingRight)) | |
ball.hitPaddle(); | |
} | |
void GameState::render() { | |
window.clear(sf::Color::Black); | |
window.draw(paddleLeft.getPaddle()); | |
window.draw(paddleRight.getPaddle()); | |
window.draw(ball.getBall()); | |
window.display(); | |
} | |
static void mainLoop(GameState *state) { | |
while (true) { | |
const bool quit = state->handleEvents(); | |
if (quit == true) { | |
return; | |
} | |
state->update(); | |
state->render(); | |
} | |
} | |
int main() | |
{ | |
GameState state; | |
if (state.initialize() == false) { | |
fprintf(stderr, "Initialization failed.\n"); | |
return 1; | |
} | |
mainLoop(&state); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment