-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
94 lines (70 loc) · 2.47 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
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>
#include <memory>
#include "Block.h"
#include "TextureManager.h"
#include "Player.h"
#include "Level.h"
using namespace std;
int main()
{
const sf::Vector2f screenSize(800, 600);
sf::RenderWindow window(sf::VideoMode(screenSize.x, screenSize.y), "Filament Demo");
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
sf::View view(sf::FloatRect(0, 0, screenSize.x, screenSize.y));
view.setViewport(sf::FloatRect(0, 0, 1, 1));
sf::Font font;
font.loadFromFile("res/visitor1.ttf");
sf::Clock clock;
//Create player and first level
shared_ptr<Player> player{new Player};
Level level('1', player, 's');
while (window.isOpen())
{
sf::Event event;
sf::Time delta = clock.restart();
static sf::Vector2f mouse;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == Event::MouseMoved) {
mouse.x = event.mouseMove.x;
mouse.y = event.mouseMove.y;
} else {
player->handleEvent(event, level);
}
}
level.setMouse(mouse + view.getCenter() - (screenSize / (float)2));
level.update(delta);
//Handle level transition
if (level.getDestination() != '\0') {
level = Level(level.getDestination(), player, level.getId());
continue;
}
Vector2f center = player->getCenter();
Vector2f levelSize = level.getSize() * Level::BLOCK_SIZE;
//Center view on player, but keep it inside the level bounds
if (center.x > levelSize.x - screenSize.x / 2)
center.x = levelSize.x - screenSize.x / 2;
if (center.x < screenSize.x / 2)
center.x = screenSize.x / 2;
if (center.y > levelSize.y - screenSize.y / 2)
center.y = levelSize.y - screenSize.y / 2;
if (center.y < screenSize.y / 2)
center.y = screenSize.y / 2;
view.setCenter(center);
window.setView(view);
window.clear(sf::Color::White);
//Use special OpenGL rendering mode to create the color inversion effect
glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_EQUIV);
window.draw(level);
glDisable(GL_COLOR_LOGIC_OP);
player->drawUI(window, font, view);
window.display();
}
return 0;
}