/
breakout.cpp
116 lines (97 loc) · 3.17 KB
/
breakout.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <stdlib.h>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <time.h>
#include "Constant.h"
#include "PS3Controller.h"
#include "Paddle.h"
#include "Ball.h"
#include "GameObjectManager.h"
#include "BlockManager.h"
void process_input();
void update();
void render();
bool is_intersecting(VisibleGameObject* A, VisibleGameObject* B);
sf::RenderWindow main_window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Breakout!");
GameObjectManager game_object_manager;
BlockManager block_manager;
bool keyboard = true;
bool end_game = false;
int main(int argc, char* argv[]) {
for (int i = 1; i < argc; i++) {
std::string arg = static_cast<std::string>(argv[i]);
if (arg == "-ps3") {
keyboard = false;
std::cout << "ps3" << std::endl;
}
}
if (keyboard == false) {
// Check that joystick #0 is connected.
if (sf::Joystick::isConnected(0)) {
std::cout << "Joystick is connected." << std::endl;
} else {
std::cout << "Joystick is NOT connected." << std::endl;
return 0;
}
}
srand(time(NULL));
// Initializing game objects.
Paddle* player = new Paddle;
player->load("images/paddle.png");
player->set_position(SCREEN_WIDTH / 2, SCREEN_HEIGHT - player->get_height());
game_object_manager.add("player", player);
Ball* game_ball = new Ball;
game_ball->load("images/ball.png");
game_ball->set_position(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
game_object_manager.add("ball", game_ball);
main_window.setFramerateLimit(60);
// Game loop
while (main_window.isOpen() && end_game == false) {
process_input();
update();
render();
}
return 0;
}
void process_input() {
sf::Event event;
if (main_window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
main_window.close();
} else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Escape) {
main_window.close();
}
} else if (event.type == sf::Event::JoystickButtonPressed) {
switch (event.joystickButton.button) {
case PS3::CROSS:
std::cout << "CROSS" << std::endl;
break;
case PS3::SQUARE:
std::cout << "SQUARE" << std::endl;
break;
case PS3::TRIANGLE:
std::cout << "TRIANGLE" << std::endl;
break;
case PS3::CIRCLE:
std::cout << "CIRCLE" << std::endl;
break;
}
}
}
}
void update() {
game_object_manager.update_all();
block_manager.update_all();
}
void render() {
main_window.clear(sf::Color(255, 255, 255));
game_object_manager.draw_all(main_window);
block_manager.draw_all(main_window);
main_window.display();
}
bool is_intersecting(VisibleGameObject* A, VisibleGameObject* B) {
return (A->get_right() >= B->get_left() && A->get_left() <= B->get_right()
&& A->get_bottom() >= B->get_top() && A->get_top() <= B->get_bottom());
}