-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.cpp
175 lines (143 loc) · 5.95 KB
/
Application.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* File: Application.cpp
* Author: williampoynter
*
* Created on September 22, 2013, 5:52 PM
*/
#include "Application.h"
Application::Application(int argc, char** argv) : Kernal(&Application::kernal, this) {
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
window.create(sf::VideoMode(1024,640),"We are one",sf::Style::Fullscreen, settings);
WIDTH = window.getSize().x;
HEIGHT = window.getSize().y;
FR = 200;
paused = false;
}
Application::~Application() {
}
bool Application::run() {
sf::CircleShape *cir = new sf::CircleShape(100.f);
cir->setFillColor(sf::Color::Green);
cir->setOutlineColor(sf::Color::Red);
cir->setOutlineThickness(25);
cir->setOrigin(100,100);
cir->setPosition(WIDTH/2,HEIGHT/2);
cir->setPointCount(100);
toDraw.push_back(cir);
circles.push_back(cir);
sf::CircleShape *cir2 = new sf::CircleShape(40.f);
toDraw.push_back(cir2);
circles.push_back(cir2);
cir2->setFillColor(sf::Color::Blue);
cir2->setOutlineColor(sf::Color::Yellow);
cir2->setOutlineThickness(2);
cir2->setOrigin(40,40);
cir2->setPosition(WIDTH/2,HEIGHT/2-140);
cir2->rotate(0);
cir2->setPointCount(100);
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text *text = new sf::Text("hello", font);
text->setCharacterSize(30);
text->setStyle(sf::Text::Bold);
text->setColor(sf::Color::Red);
sf::FloatRect textRect = text->getLocalBounds();
text->setOrigin(textRect.left + textRect.width,
textRect.top + textRect.height);
text->setPosition(sf::Vector2f(WIDTH-10,HEIGHT-10));
toDraw.push_back(text);
texts.push_back(text);
sf::Texture texture;
if (!texture.loadFromFile("Space_envader.png")) exit(0);
texture.setRepeated(false);
sf::Sprite *sprite = new sf::Sprite;
sprite->setTexture(texture);
toDraw.push_back(sprite);
sprites.push_back(sprite);
sprite->setScale(0.2,0.2);
music.openFromFile("music.flac");
music.play();
Kernal.launch();
sf::Event event;
while (window.isOpen())
{
while (window.pollEvent(event))
{
switch(event.type) {
case sf::Event::LostFocus:
pause();
break;
case sf::Event::GainedFocus:
unpause();
break;
case sf::Event::Closed:
stop();
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Escape)
stop();
if (event.key.code == sf::Keyboard::P)
togglePause();
break;
default:
break;
}
}
usleep(200000);
}
return 0;
}
void Application::kernal() {
float angle = 0; float mag;
int dir = 1;
sf::Time time = clock.getElapsedTime();
while (window.isOpen())
{
float elapsed = clock.getElapsedTime().asMilliseconds() - time.asMilliseconds();
if (elapsed >= (1000/FR)) {
while (paused)
usleep(1000);
time = clock.getElapsedTime();
float freq = 1000/elapsed;
angle = angle + 60.f*(elapsed/1000.f);
mag = 300*(elapsed/1000.f);
circles.back()->setPosition(cos(angle*PI/180)*140+WIDTH/2, sin(angle*PI/180)*140+HEIGHT/2);
std::stringstream ss;
ss << freq << "Hz";
texts.back()->setString(ss.str());
sprites.back()->move(dir*mag,0);
if (sprites.back()->getGlobalBounds().width + sprites.back()->getGlobalBounds().left >= WIDTH ||
sprites.back()->getGlobalBounds().left <= 0)
dir = dir * -1;
sf::FloatRect textRect = texts.back()->getLocalBounds();
texts.back()->setOrigin(textRect.left + textRect.width,
textRect.top + textRect.height);
texts.back()->setPosition(sf::Vector2f(WIDTH-10,HEIGHT-10));
window.clear();
for (std::list<sf::Drawable*>::iterator iter = toDraw.begin(); iter != toDraw.end(); iter++)
window.draw(**iter);
window.display();
} else
usleep(((1000/FR)-1-elapsed) * 1000);
}
}
void Application::stop() {
Kernal.terminate();
window.close();
}
void Application::pause() {
paused = true;
music.pause();
}
void Application::unpause() {
paused = false;
music.play();
}
void Application::togglePause() {
paused = !paused;
if (music.getStatus() == sf::Music::Paused)
music.play();
else
music.pause();
}