-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
119 lines (103 loc) · 3.69 KB
/
main.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
117
118
119
#include <entities/Fireball.h>
#include <entities/InvisibleWall.h>
#include <file_util.h>
#include "ControllerOverlay.h"
#include "Input.h"
#include "Level.h"
#include "SFML/Graphics.hpp"
#include "SFML/Window.hpp"
#include "SpriteMaker.h"
#include "Text.h"
#include "Timer.h"
#include "entities/Block.h"
#include "entities/Goomba.h"
#include "entities/Ground.h"
#include "entities/Mario.h"
#include "entities/Pipe.h"
int main(int argc, char* argv[])
{
const auto root = findRootDirectory(argv[0]);
const auto resourceDir = root + "resources/";
sf::RenderWindow window(sf::VideoMode(200, 200), "Super Mario Bros");
window.setFramerateLimit(30);
window.setSize(sf::Vector2u(960, 720));
window.clear();
initializeSpriteMaker(resourceDir);
initializeHUDOverlay(resourceDir);
auto& spriteMaker = getSpriteMaker();
std::vector<std::unique_ptr<Entity>> entities;
entities.reserve(100);
std::unique_ptr<Mario> mario(
new Mario(spriteMaker->playerTexture, {60, 90}));
std::unique_ptr<Goomba> goomba(
new Goomba(spriteMaker->enemyTexture, {200, 50}));
std::unique_ptr<Pipe> rightPipe(
new Pipe(spriteMaker->inanimateObjectTexture, {130, 100}));
std::unique_ptr<Pipe> leftPipe(
new Pipe(spriteMaker->inanimateObjectTexture, {-10, 100}));
entities.push_back(std::move(leftPipe));
entities.push_back(std::move(rightPipe));
entities.push_back(std::move(goomba));
entities.push_back(
std::make_unique<InvisibleWall>(spriteMaker->inanimateObjectTexture,
sf::Vector2f(-16, -500)));
auto& wall = *(
dynamic_cast<InvisibleWall*>(entities[entities.size() - 1].get()));
for (int i = 0; i < 20; i++)
{
entities.push_back(
std::make_unique<Ground>(spriteMaker->inanimateObjectTexture,
sf::Vector2f(i * 16, 132)));
}
entities.push_back(std::make_unique<BreakableBlock>(
spriteMaker->inanimateObjectTexture, sf::Vector2f(40, 75)));
entities.push_back(
std::make_unique<ItemBlock>(spriteMaker->inanimateObjectTexture,
sf::Vector2f(56, 75)));
entities.push_back(
std::make_unique<ItemBlock>(spriteMaker->inanimateObjectTexture,
sf::Vector2f(72, 75)));
Level level(std::move(mario), std::move(entities), window, wall);
KeyboardInput currentInput = {};
KeyboardInput previousInput = {};
std::vector<KeyboardInput> keyboardInputs;
while (window.isOpen())
{
sf::Event event = {};
#ifdef MANUAL_INPUT
currentInput = nextInput(keyboardInputs, idx);
++idx;
#endif
std::vector<sf::Keyboard::Key> keycodes;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
#ifndef MANUAL_INPUT
case sf::Event::KeyPressed:
currentInput.setKey(event.key.code, true);
break;
case sf::Event::KeyReleased:
{
currentInput.setKey(event.key.code, false);
break;
}
#endif
default:
break;
}
}
currentInput.updateWasDown(previousInput);
previousInput = currentInput;
level.executeFrame(currentInput);
level.drawFrame(window);
// Comment/uncomment line below to display in-game controller
ControllerOverlay::draw(currentInput, window);
window.display();
getTimer().incrementNumFrames();
}
return 0;
}