1 #include "Application.hpp"
6 #include "../graphics/Viewport.hpp"
7 #include "../io/Token.hpp"
8 #include "../io/TokenStreamReader.hpp"
9 #include "../world/Planet.hpp"
10 #include "../world/Simulation.hpp"
11 #include "../world/Sun.hpp"
15 #include <SDL_image.h>
23 Application::Application(Window &win, graphics::Viewport &vp)
29 Application::~Application() {
33 void Application::PushState(State *s) {
35 if (!states.empty()) {
36 states.top()->OnPause();
40 if (s->ref_count == 1) {
42 s->OnResize(viewport.Width(), viewport.Height());
47 State *Application::PopState() {
48 State *s = states.top();
52 if (!states.empty()) {
53 states.top()->OnResume();
58 State *Application::SwitchState(State *s_new) {
60 State *s_old = states.top();
65 if (s_old->ref_count == 0) {
68 if (s_new->ref_count == 1) {
70 s_new->OnResize(viewport.Width(), viewport.Height());
76 State &Application::GetState() {
80 bool Application::HasState() const noexcept {
81 return !states.empty();
85 void Application::Run() {
86 Uint32 last = SDL_GetTicks();
88 Uint32 now = SDL_GetTicks();
89 int delta = now - last;
95 void Application::Loop(int dt) {
97 if (!HasState()) return;
98 GetState().Update(dt);
99 if (!HasState()) return;
101 GetState().Render(viewport);
105 void Application::HandleEvents() {
107 while (HasState() && SDL_PollEvent(&event)) {
108 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
109 viewport.Resize(event.window.data1, event.window.data2);
111 GetState().Handle(event);
115 void State::Handle(const SDL_Event &event) {
116 switch (event.type) {
118 OnKeyDown(event.key);
123 case SDL_MOUSEBUTTONDOWN:
124 OnMouseDown(event.button);
126 case SDL_MOUSEBUTTONUP:
127 OnMouseUp(event.button);
129 case SDL_MOUSEMOTION:
130 OnMouseMotion(event.motion);
133 OnMouseWheel(event.wheel);
138 case SDL_WINDOWEVENT:
139 Handle(event.window);
147 void State::Handle(const SDL_WindowEvent &event) {
148 switch (event.event) {
149 case SDL_WINDOWEVENT_FOCUS_GAINED:
152 case SDL_WINDOWEVENT_FOCUS_LOST:
155 case SDL_WINDOWEVENT_RESIZED:
156 OnResize(event.data1, event.data2);
163 void State::Update(int dt) {
167 void State::Render(graphics::Viewport &viewport) {
171 void State::OnQuit() {
172 while (App().HasState()) {
180 , data_path(path + "data/")
181 , font_path(path + "fonts/")
182 , skin_path(path + "skins/")
183 , sky_path(path + "skies/")
184 , tile_path(path + "tiles/")
185 , random(0x6283B64CEFE57925)
187 graphics::Font(font_path + "DejaVuSans.ttf", 32),
188 graphics::Font(font_path + "DejaVuSans.ttf", 24),
189 graphics::Font(font_path + "DejaVuSans.ttf", 16)
192 std::ifstream resource_file(data_path + "resources");
193 io::TokenStreamReader resource_reader(resource_file);
194 ReadResources(resource_reader);
198 std::ifstream tile_file(data_path + "tile_types");
199 io::TokenStreamReader tile_reader(tile_file);
200 ReadTileTypes(tile_reader);
204 graphics::Format format;
205 textures.tiles.Bind();
206 textures.tiles.Reserve(256, 256, 14, format);
207 LoadTileTexture("algae", textures.tiles, 0);
208 LoadTileTexture("desert", textures.tiles, 1);
209 LoadTileTexture("forest", textures.tiles, 2);
210 LoadTileTexture("grass", textures.tiles, 3);
211 LoadTileTexture("ice", textures.tiles, 4);
212 LoadTileTexture("jungle", textures.tiles, 5);
213 LoadTileTexture("mountain", textures.tiles, 6);
214 LoadTileTexture("ocean", textures.tiles, 7);
215 LoadTileTexture("rock", textures.tiles, 8);
216 LoadTileTexture("sand", textures.tiles, 9);
217 LoadTileTexture("taiga", textures.tiles, 10);
218 LoadTileTexture("tundra", textures.tiles, 11);
219 LoadTileTexture("water", textures.tiles, 12);
220 LoadTileTexture("wheat", textures.tiles, 13);
221 textures.tiles.FilterTrilinear();
223 textures.skins.Bind();
224 textures.skins.Reserve(256, 256, 9, format);
225 LoadSkinTexture("plain", textures.skins, 0);
226 LoadSkinTexture("stripes", textures.skins, 1);
227 LoadSkinTexture("dots", textures.skins, 2);
228 LoadSkinTexture("lines", textures.skins, 3);
229 LoadSkinTexture("spots", textures.skins, 4);
230 LoadSkinTexture("circles", textures.skins, 5);
231 textures.skins.FilterTrilinear();
234 LoadSkyTexture("blue", textures.sky);
235 textures.sky.FilterTrilinear();
236 textures.sky.WrapEdge();
242 void Assets::ReadResources(io::TokenStreamReader &in) {
243 while (in.HasMore()) {
245 in.ReadIdentifier(name);
246 in.Skip(io::Token::EQUALS);
249 if (data.resources.Has(name)) {
250 id = data.resources[name].id;
254 id = data.resources.Add(res);
257 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
258 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
259 in.ReadIdentifier(name);
260 in.Skip(io::Token::EQUALS);
261 if (name == "label") {
262 in.ReadString(data.resources[id].label);
263 } else if (name == "density") {
264 data.resources[id].density = in.GetDouble();
265 } else if (name == "energy") {
266 data.resources[id].energy = in.GetDouble();
267 data.resources[id].inverse_energy = 1.0 / data.resources[id].energy;
268 } else if (name == "state") {
269 in.ReadIdentifier(name);
270 if (name == "solid") {
271 data.resources[id].state = world::Resource::SOLID;
272 } else if (name == "liquid") {
273 data.resources[id].state = world::Resource::LIQUID;
274 } else if (name == "gas") {
275 data.resources[id].state = world::Resource::GAS;
276 } else if (name == "plasma") {
277 data.resources[id].state = world::Resource::PLASMA;
279 throw std::runtime_error("unknown resource state '" + name + "'");
281 } else if (name == "base_color") {
282 in.ReadVec(data.resources[id].base_color);
283 } else if (name == "compatibility") {
284 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
285 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
286 in.ReadIdentifier(name);
288 if (data.resources.Has(name)) {
289 sub_id = data.resources[name].id;
293 sub_id = data.resources.Add(res);
295 in.Skip(io::Token::COLON);
296 double value = in.GetDouble();
297 in.Skip(io::Token::SEMICOLON);
298 data.resources[id].compatibility[sub_id] = value;
300 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
302 throw std::runtime_error("unknown resource property '" + name + "'");
304 in.Skip(io::Token::SEMICOLON);
306 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
307 in.Skip(io::Token::SEMICOLON);
311 void Assets::ReadTileTypes(io::TokenStreamReader &in) {
312 while (in.HasMore()) {
314 in.ReadIdentifier(name);
315 in.Skip(io::Token::EQUALS);
318 if (data.tile_types.Has(name)) {
319 id = data.tile_types[name].id;
321 world::TileType type;
323 id = data.tile_types.Add(type);
326 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
327 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
328 in.ReadIdentifier(name);
329 in.Skip(io::Token::EQUALS);
330 if (name == "label") {
331 in.ReadString(data.tile_types[id].label);
332 } else if (name == "texture") {
333 data.tile_types[id].texture = in.GetInt();
334 } else if (name == "shiny") {
335 data.tile_types[id].shiny = in.GetDouble();
336 } else if (name == "glossy") {
337 data.tile_types[id].glossy = in.GetDouble();
338 } else if (name == "metallic") {
339 data.tile_types[id].metallic = in.GetDouble();
340 } else if (name == "yield") {
341 in.Skip(io::Token::BRACKET_OPEN);
342 while (in.Peek().type != io::Token::BRACKET_CLOSE) {
343 world::TileType::Yield yield;
344 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
345 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
346 in.ReadIdentifier(name);
347 in.Skip(io::Token::EQUALS);
348 if (name == "resource") {
349 in.ReadIdentifier(name);
350 yield.resource = data.resources[name].id;
351 } else if (name == "ubiquity") {
352 yield.ubiquity = in.GetDouble();
354 throw std::runtime_error("unknown tile type yield property '" + name + "'");
356 in.Skip(io::Token::SEMICOLON);
358 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
359 data.tile_types[id].resources.push_back(yield);
360 if (in.Peek().type == io::Token::COMMA) {
361 in.Skip(io::Token::COMMA);
364 in.Skip(io::Token::BRACKET_CLOSE);
366 throw std::runtime_error("unknown tile type property '" + name + "'");
368 in.Skip(io::Token::SEMICOLON);
370 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
371 in.Skip(io::Token::SEMICOLON);
375 void Assets::LoadTileTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
376 string path = tile_path + name + ".png";
377 SDL_Surface *srf = IMG_Load(path.c_str());
379 throw SDLError("IMG_Load");
382 tex.Data(layer, *srf);
384 SDL_FreeSurface(srf);
387 SDL_FreeSurface(srf);
390 void Assets::LoadSkinTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
391 string path = skin_path + name + ".png";
392 SDL_Surface *srf = IMG_Load(path.c_str());
394 throw SDLError("IMG_Load");
397 tex.Data(layer, *srf);
399 SDL_FreeSurface(srf);
402 SDL_FreeSurface(srf);
405 void Assets::LoadSkyTexture(const string &name, graphics::CubeMap &cm) const {
406 string full = sky_path + name;
407 string right = full + "-right.png";
408 string left = full + "-left.png";
409 string top = full + "-top.png";
410 string bottom = full + "-bottom.png";
411 string back = full + "-back.png";
412 string front = full + "-front.png";
414 SDL_Surface *srf = nullptr;
416 if (!(srf = IMG_Load(right.c_str()))) throw SDLError("IMG_Load");
418 cm.Data(graphics::CubeMap::RIGHT, *srf);
420 SDL_FreeSurface(srf);
423 SDL_FreeSurface(srf);
425 if (!(srf = IMG_Load(left.c_str()))) throw SDLError("IMG_Load");
427 cm.Data(graphics::CubeMap::LEFT, *srf);
429 SDL_FreeSurface(srf);
432 SDL_FreeSurface(srf);
434 if (!(srf = IMG_Load(top.c_str()))) throw SDLError("IMG_Load");
436 cm.Data(graphics::CubeMap::TOP, *srf);
438 SDL_FreeSurface(srf);
441 SDL_FreeSurface(srf);
443 if (!(srf = IMG_Load(bottom.c_str()))) throw SDLError("IMG_Load");
445 cm.Data(graphics::CubeMap::BOTTOM, *srf);
447 SDL_FreeSurface(srf);
450 SDL_FreeSurface(srf);
452 if (!(srf = IMG_Load(back.c_str()))) throw SDLError("IMG_Load");
454 cm.Data(graphics::CubeMap::BACK, *srf);
456 SDL_FreeSurface(srf);
459 SDL_FreeSurface(srf);
461 if (!(srf = IMG_Load(front.c_str()))) throw SDLError("IMG_Load");
463 cm.Data(graphics::CubeMap::FRONT, *srf);
465 SDL_FreeSurface(srf);
468 SDL_FreeSurface(srf);
471 void Assets::LoadUniverse(const string &name, world::Simulation &sim) const {
472 std::ifstream universe_file(data_path + name);
473 io::TokenStreamReader universe_reader(universe_file);
474 ReadBody(universe_reader, sim);
475 universe_reader.Skip(io::Token::SEMICOLON);
478 world::Body *Assets::ReadBody(io::TokenStreamReader &in, world::Simulation &sim) const {
479 std::unique_ptr<world::Body> body;
481 in.ReadIdentifier(name);
483 world::Sun *sun = new world::Sun;
486 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
487 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
488 in.ReadIdentifier(name);
489 in.Skip(io::Token::EQUALS);
490 ReadSunProperty(name, in, *sun, sim);
491 in.Skip(io::Token::SEMICOLON);
493 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
494 in.Skip(io::Token::SEMICOLON);
495 } else if (name == "Planet") {
496 in.Skip(io::Token::PARENTHESIS_OPEN);
497 int sidelength = in.GetInt();
498 in.Skip(io::Token::PARENTHESIS_CLOSE);
499 world::Planet *planet = new world::Planet(sidelength);
500 sim.AddPlanet(*planet);
502 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
503 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
504 in.ReadIdentifier(name);
505 in.Skip(io::Token::EQUALS);
506 ReadPlanetProperty(name, in, *planet, sim);
507 in.Skip(io::Token::SEMICOLON);
509 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
511 throw std::runtime_error("unknown body class " + name);
513 return body.release();
516 void Assets::ReadSunProperty(const std::string &name, io::TokenStreamReader &in, world::Sun &sun, world::Simulation &sim) const {
517 if (name == "color") {
518 glm::dvec3 color(0.0);
521 } else if (name == "luminosity") {
522 sun.Luminosity(in.GetDouble());
524 ReadBodyProperty(name, in, sun, sim);
528 void Assets::ReadPlanetProperty(const std::string &name, io::TokenStreamReader &in, world::Planet &planet, world::Simulation &sim) const {
529 if (name == "generate") {
531 in.ReadIdentifier(gen);
532 if (gen == "earthlike") {
533 world::GenerateEarthlike(data.tile_types, planet);
534 } else if (gen == "test") {
535 world::GenerateTest(data.tile_types, planet);
537 throw std::runtime_error("unknown surface generator " + gen);
539 } else if (name == "atmosphere") {
541 in.ReadIdentifier(atm);
542 planet.Atmosphere(data.resources[atm].id);
544 ReadBodyProperty(name, in, planet, sim);
548 void Assets::ReadBodyProperty(const std::string &name, io::TokenStreamReader &in, world::Body &body, world::Simulation &sim) const {
549 if (name == "name") {
551 in.ReadString(value);
553 } else if (name == "mass") {
554 body.Mass(in.GetDouble());
555 } else if (name == "radius") {
556 body.Radius(in.GetDouble());
557 } else if (name == "axial_tilt") {
558 glm::dvec2 tilt(0.0);
560 body.AxialTilt(tilt);
561 } else if (name == "rotation") {
562 body.Rotation(in.GetDouble());
563 } else if (name == "angular_momentum") {
564 body.AngularMomentum(in.GetDouble());
565 } else if (name == "orbit") {
566 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
567 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
569 in.ReadIdentifier(oname);
570 in.Skip(io::Token::EQUALS);
571 if (oname == "SMA" || oname == "semi_major_axis") {
572 body.GetOrbit().SemiMajorAxis(in.GetDouble());
573 } else if (oname == "ECC" || oname == "eccentricity") {
574 body.GetOrbit().Eccentricity(in.GetDouble());
575 } else if (oname == "INC" || oname == "inclination") {
576 body.GetOrbit().Inclination(in.GetDouble());
577 } else if (oname == "ASC" || oname == "ascending_node" || oname == "longitude_ascending") {
578 body.GetOrbit().LongitudeAscending(in.GetDouble());
579 } else if (oname == "ARG" || oname == "APE" || oname == "argument_periapsis") {
580 body.GetOrbit().ArgumentPeriapsis(in.GetDouble());
581 } else if (oname == "MNA" || oname == "mean_anomaly") {
582 body.GetOrbit().MeanAnomaly(in.GetDouble());
584 throw std::runtime_error("unknown orbit property " + oname);
586 in.Skip(io::Token::SEMICOLON);
588 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
589 } else if (name == "children") {
590 in.Skip(io::Token::BRACKET_OPEN);
591 while (in.Peek().type != io::Token::BRACKET_CLOSE) {
592 world::Body *b = ReadBody(in, sim);
594 if (in.Peek().type == io::Token::COMMA) {
595 in.Skip(io::Token::COMMA);
598 in.Skip(io::Token::BRACKET_CLOSE);
600 throw std::runtime_error("unknown body property " + name);