]> git.localhorst.tv Git - blobs.git/blob - src/app/app.cpp
950eda8cb3610c08e779ea478c578a68abc90c2b
[blobs.git] / src / app / app.cpp
1 #include "Application.hpp"
2 #include "Assets.hpp"
3 #include "State.hpp"
4
5 #include "init.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"
12
13 #include <fstream>
14 #include <SDL.h>
15 #include <SDL_image.h>
16
17 using std::string;
18
19
20 namespace blobs {
21 namespace app {
22
23 Application::Application(Window &win, graphics::Viewport &vp)
24 : window(win)
25 , viewport(vp)
26 , states() {
27 }
28
29 Application::~Application() {
30 }
31
32
33 void Application::PushState(State *s) {
34         s->app = this;
35         if (!states.empty()) {
36                 states.top()->OnPause();
37         }
38         states.emplace(s);
39         ++s->ref_count;
40         if (s->ref_count == 1) {
41                 s->OnEnter();
42         }
43         s->OnResume();
44 }
45
46 State *Application::PopState() {
47         State *s = states.top();
48         states.pop();
49         s->OnPause();
50         s->OnExit();
51         if (!states.empty()) {
52                 states.top()->OnResume();
53         }
54         return s;
55 }
56
57 State *Application::SwitchState(State *s_new) {
58         s_new->app = this;
59         State *s_old = states.top();
60         states.top() = s_new;
61         --s_old->ref_count;
62         ++s_new->ref_count;
63         s_old->OnPause();
64         if (s_old->ref_count == 0) {
65                 s_old->OnExit();
66         }
67         if (s_new->ref_count == 1) {
68                 s_new->OnEnter();
69         }
70         s_new->OnResume();
71         return s_old;
72 }
73
74 State &Application::GetState() {
75         return *states.top();
76 }
77
78 bool Application::HasState() const noexcept {
79         return !states.empty();
80 }
81
82
83 void Application::Run() {
84         Uint32 last = SDL_GetTicks();
85         while (HasState()) {
86                 Uint32 now = SDL_GetTicks();
87                 int delta = now - last;
88                 Loop(delta);
89                 last = now;
90         }
91 }
92
93 void Application::Loop(int dt) {
94         HandleEvents();
95         if (!HasState()) return;
96         GetState().Update(dt);
97         if (!HasState()) return;
98         viewport.Clear();
99         GetState().Render(viewport);
100         window.Flip();
101 }
102
103 void Application::HandleEvents() {
104         SDL_Event event;
105         while (HasState() && SDL_PollEvent(&event)) {
106                 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
107                         viewport.Resize(event.window.data1, event.window.data2);
108                 }
109                 GetState().Handle(event);
110         }
111 }
112
113 void State::Handle(const SDL_Event &event) {
114         switch (event.type) {
115                 case SDL_KEYDOWN:
116                         OnKeyDown(event.key);
117                         break;
118                 case SDL_KEYUP:
119                         OnKeyUp(event.key);
120                         break;
121                 case SDL_MOUSEBUTTONDOWN:
122                         OnMouseDown(event.button);
123                         break;
124                 case SDL_MOUSEBUTTONUP:
125                         OnMouseUp(event.button);
126                         break;
127                 case SDL_MOUSEMOTION:
128                         OnMouseMotion(event.motion);
129                         break;
130                 case SDL_MOUSEWHEEL:
131                         OnMouseWheel(event.wheel);
132                         break;
133                 case SDL_QUIT:
134                         OnQuit();
135                         break;
136                 case SDL_WINDOWEVENT:
137                         Handle(event.window);
138                         break;
139                 default:
140                         // ignore
141                         break;
142         }
143 }
144
145 void State::Handle(const SDL_WindowEvent &event) {
146         switch (event.event) {
147                 case SDL_WINDOWEVENT_FOCUS_GAINED:
148                         OnFocus();
149                         break;
150                 case SDL_WINDOWEVENT_FOCUS_LOST:
151                         OnBlur();
152                         break;
153                 case SDL_WINDOWEVENT_RESIZED:
154                         OnResize(event.data1, event.data2);
155                         break;
156                 default:
157                         break;
158         }
159 }
160
161 void State::Update(int dt) {
162         OnUpdate(dt);
163 }
164
165 void State::Render(graphics::Viewport &viewport) {
166         OnRender(viewport);
167 }
168
169 void State::OnQuit() {
170         while (App().HasState()) {
171                 App().PopState();
172         }
173 }
174
175
176 Assets::Assets()
177 : path("assets/")
178 , data_path(path + "data/")
179 , font_path(path + "fonts/")
180 , skin_path(path + "skins/")
181 , sky_path(path + "skies/")
182 , tile_path(path + "tiles/")
183 , random(0x6283B64CEFE57925)
184 , fonts{
185         graphics::Font(font_path + "DejaVuSans.ttf", 32),
186         graphics::Font(font_path + "DejaVuSans.ttf", 24),
187         graphics::Font(font_path + "DejaVuSans.ttf", 16)
188 } {
189         {
190                 std::ifstream resource_file(data_path + "resources");
191                 io::TokenStreamReader resource_reader(resource_file);
192                 ReadResources(resource_reader);
193         }
194
195         {
196                 std::ifstream tile_file(data_path + "tile_types");
197                 io::TokenStreamReader tile_reader(tile_file);
198                 ReadTileTypes(tile_reader);
199         }
200
201
202         graphics::Format format;
203         textures.tiles.Bind();
204         textures.tiles.Reserve(256, 256, 14, format);
205         LoadTileTexture("algae",    textures.tiles,  0);
206         LoadTileTexture("desert",   textures.tiles,  1);
207         LoadTileTexture("forest",   textures.tiles,  2);
208         LoadTileTexture("grass",    textures.tiles,  3);
209         LoadTileTexture("ice",      textures.tiles,  4);
210         LoadTileTexture("jungle",   textures.tiles,  5);
211         LoadTileTexture("mountain", textures.tiles,  6);
212         LoadTileTexture("ocean",    textures.tiles,  7);
213         LoadTileTexture("rock",     textures.tiles,  8);
214         LoadTileTexture("sand",     textures.tiles,  9);
215         LoadTileTexture("taiga",    textures.tiles, 10);
216         LoadTileTexture("tundra",   textures.tiles, 11);
217         LoadTileTexture("water",    textures.tiles, 12);
218         LoadTileTexture("wheat",    textures.tiles, 13);
219         textures.tiles.FilterTrilinear();
220
221         textures.skins.Bind();
222         textures.skins.Reserve(256, 256, 9, format);
223         LoadSkinTexture("plain", textures.skins, 0);
224         LoadSkinTexture("stripes", textures.skins, 1);
225         LoadSkinTexture("dots", textures.skins, 2);
226         LoadSkinTexture("lines", textures.skins, 3);
227         LoadSkinTexture("spots", textures.skins, 4);
228         LoadSkinTexture("circles", textures.skins, 5);
229         textures.skins.FilterTrilinear();
230
231         textures.sky.Bind();
232         LoadSkyTexture("blue", textures.sky);
233         textures.sky.FilterTrilinear();
234         textures.sky.WrapEdge();
235 }
236
237 Assets::~Assets() {
238 }
239
240 void Assets::ReadResources(io::TokenStreamReader &in) {
241         while (in.HasMore()) {
242                 string name;
243                 in.ReadIdentifier(name);
244                 in.Skip(io::Token::EQUALS);
245
246                 int id = 0;
247                 if (data.resources.Has(name)) {
248                         id = data.resources[name].id;
249                 } else {
250                         world::Resource res;
251                         res.name = name;
252                         id = data.resources.Add(res);
253                 }
254
255                 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
256                 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
257                         in.ReadIdentifier(name);
258                         in.Skip(io::Token::EQUALS);
259                         if (name == "label") {
260                                 in.ReadString(data.resources[id].label);
261                         } else if (name == "density") {
262                                 data.resources[id].density = in.GetDouble();
263                         } else if (name == "energy") {
264                                 data.resources[id].energy = in.GetDouble();
265                                 data.resources[id].inverse_energy = 1.0 / data.resources[id].energy;
266                         } else if (name == "state") {
267                                 in.ReadIdentifier(name);
268                                 if (name == "solid") {
269                                         data.resources[id].state = world::Resource::SOLID;
270                                 } else if (name == "liquid") {
271                                         data.resources[id].state = world::Resource::LIQUID;
272                                 } else if (name == "gas") {
273                                         data.resources[id].state = world::Resource::GAS;
274                                 } else if (name == "plasma") {
275                                         data.resources[id].state = world::Resource::PLASMA;
276                                 } else {
277                                         throw std::runtime_error("unknown resource state '" + name + "'");
278                                 }
279                         } else if (name == "base_color") {
280                                 in.ReadVec(data.resources[id].base_color);
281                         } else if (name == "compatibility") {
282                                 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
283                                 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
284                                         in.ReadIdentifier(name);
285                                         int sub_id = 0;
286                                         if (data.resources.Has(name)) {
287                                                 sub_id = data.resources[name].id;
288                                         } else {
289                                                 world::Resource res;
290                                                 res.name = name;
291                                                 sub_id = data.resources.Add(res);
292                                         }
293                                         in.Skip(io::Token::COLON);
294                                         double value = in.GetDouble();
295                                         in.Skip(io::Token::SEMICOLON);
296                                         data.resources[id].compatibility[sub_id] = value;
297                                 }
298                                 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
299                         } else {
300                                 throw std::runtime_error("unknown resource property '" + name + "'");
301                         }
302                         in.Skip(io::Token::SEMICOLON);
303                 }
304                 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
305                 in.Skip(io::Token::SEMICOLON);
306         }
307 }
308
309 void Assets::ReadTileTypes(io::TokenStreamReader &in) {
310         while (in.HasMore()) {
311                 string name;
312                 in.ReadIdentifier(name);
313                 in.Skip(io::Token::EQUALS);
314
315                 int id = 0;
316                 if (data.tile_types.Has(name)) {
317                         id = data.tile_types[name].id;
318                 } else {
319                         world::TileType type;
320                         type.name = name;
321                         id = data.tile_types.Add(type);
322                 }
323
324                 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
325                 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
326                         in.ReadIdentifier(name);
327                         in.Skip(io::Token::EQUALS);
328                         if (name == "label") {
329                                 in.ReadString(data.tile_types[id].label);
330                         } else if (name == "texture") {
331                                 data.tile_types[id].texture = in.GetInt();
332                         } else if (name == "yield") {
333                                 in.Skip(io::Token::BRACKET_OPEN);
334                                 while (in.Peek().type != io::Token::BRACKET_CLOSE) {
335                                         world::TileType::Yield yield;
336                                         in.Skip(io::Token::ANGLE_BRACKET_OPEN);
337                                         while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
338                                                 in.ReadIdentifier(name);
339                                                 in.Skip(io::Token::EQUALS);
340                                                 if (name == "resource") {
341                                                         in.ReadIdentifier(name);
342                                                         yield.resource = data.resources[name].id;
343                                                 } else if (name == "ubiquity") {
344                                                         yield.ubiquity = in.GetDouble();
345                                                 } else {
346                                                         throw std::runtime_error("unknown tile type yield property '" + name + "'");
347                                                 }
348                                                 in.Skip(io::Token::SEMICOLON);
349                                         }
350                                         in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
351                                         data.tile_types[id].resources.push_back(yield);
352                                         if (in.Peek().type == io::Token::COMMA) {
353                                                 in.Skip(io::Token::COMMA);
354                                         }
355                                 }
356                                 in.Skip(io::Token::BRACKET_CLOSE);
357                         } else {
358                                 throw std::runtime_error("unknown tile type property '" + name + "'");
359                         }
360                         in.Skip(io::Token::SEMICOLON);
361                 }
362                 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
363                 in.Skip(io::Token::SEMICOLON);
364         }
365 }
366
367 void Assets::LoadTileTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
368         string path = tile_path + name + ".png";
369         SDL_Surface *srf = IMG_Load(path.c_str());
370         if (!srf) {
371                 throw SDLError("IMG_Load");
372         }
373         try {
374                 tex.Data(layer, *srf);
375         } catch (...) {
376                 SDL_FreeSurface(srf);
377                 throw;
378         }
379         SDL_FreeSurface(srf);
380 }
381
382 void Assets::LoadSkinTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
383         string path = skin_path + name + ".png";
384         SDL_Surface *srf = IMG_Load(path.c_str());
385         if (!srf) {
386                 throw SDLError("IMG_Load");
387         }
388         try {
389                 tex.Data(layer, *srf);
390         } catch (...) {
391                 SDL_FreeSurface(srf);
392                 throw;
393         }
394         SDL_FreeSurface(srf);
395 }
396
397 void Assets::LoadSkyTexture(const string &name, graphics::CubeMap &cm) const {
398         string full = sky_path + name;
399         string right = full + "-right.png";
400         string left = full + "-left.png";
401         string top = full + "-top.png";
402         string bottom = full + "-bottom.png";
403         string back = full + "-back.png";
404         string front = full + "-front.png";
405
406         SDL_Surface *srf = nullptr;
407
408         if (!(srf = IMG_Load(right.c_str()))) throw SDLError("IMG_Load");
409         try {
410                 cm.Data(graphics::CubeMap::RIGHT, *srf);
411         } catch (...) {
412                 SDL_FreeSurface(srf);
413                 throw;
414         }
415         SDL_FreeSurface(srf);
416
417         if (!(srf = IMG_Load(left.c_str()))) throw SDLError("IMG_Load");
418         try {
419                 cm.Data(graphics::CubeMap::LEFT, *srf);
420         } catch (...) {
421                 SDL_FreeSurface(srf);
422                 throw;
423         }
424         SDL_FreeSurface(srf);
425
426         if (!(srf = IMG_Load(top.c_str()))) throw SDLError("IMG_Load");
427         try {
428                 cm.Data(graphics::CubeMap::TOP, *srf);
429         } catch (...) {
430                 SDL_FreeSurface(srf);
431                 throw;
432         }
433         SDL_FreeSurface(srf);
434
435         if (!(srf = IMG_Load(bottom.c_str()))) throw SDLError("IMG_Load");
436         try {
437                 cm.Data(graphics::CubeMap::BOTTOM, *srf);
438         } catch (...) {
439                 SDL_FreeSurface(srf);
440                 throw;
441         }
442         SDL_FreeSurface(srf);
443
444         if (!(srf = IMG_Load(back.c_str()))) throw SDLError("IMG_Load");
445         try {
446                 cm.Data(graphics::CubeMap::BACK, *srf);
447         } catch (...) {
448                 SDL_FreeSurface(srf);
449                 throw;
450         }
451         SDL_FreeSurface(srf);
452
453         if (!(srf = IMG_Load(front.c_str()))) throw SDLError("IMG_Load");
454         try {
455                 cm.Data(graphics::CubeMap::FRONT, *srf);
456         } catch (...) {
457                 SDL_FreeSurface(srf);
458                 throw;
459         }
460         SDL_FreeSurface(srf);
461 }
462
463 void Assets::LoadUniverse(const string &name, world::Simulation &sim) const {
464         std::ifstream universe_file(data_path + name);
465         io::TokenStreamReader universe_reader(universe_file);
466         ReadBody(universe_reader, sim);
467         universe_reader.Skip(io::Token::SEMICOLON);
468 }
469
470 world::Body *Assets::ReadBody(io::TokenStreamReader &in, world::Simulation &sim) const {
471         std::unique_ptr<world::Body> body;
472         string name;
473         in.ReadIdentifier(name);
474         if (name == "Sun") {
475                 world::Sun *sun = new world::Sun;
476                 body.reset(sun);
477                 sim.AddSun(*sun);
478                 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
479                 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
480                         in.ReadIdentifier(name);
481                         in.Skip(io::Token::EQUALS);
482                         ReadSunProperty(name, in, *sun, sim);
483                         in.Skip(io::Token::SEMICOLON);
484                 }
485                 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
486                 in.Skip(io::Token::SEMICOLON);
487         } else if (name == "Planet") {
488                 in.Skip(io::Token::PARENTHESIS_OPEN);
489                 int sidelength = in.GetInt();
490                 in.Skip(io::Token::PARENTHESIS_CLOSE);
491                 world::Planet *planet = new world::Planet(sidelength);
492                 sim.AddPlanet(*planet);
493                 body.reset(planet);
494                 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
495                 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
496                         in.ReadIdentifier(name);
497                         in.Skip(io::Token::EQUALS);
498                         ReadPlanetProperty(name, in, *planet, sim);
499                         in.Skip(io::Token::SEMICOLON);
500                 }
501                 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
502         } else {
503                 throw std::runtime_error("unknown body class " + name);
504         }
505         return body.release();
506 }
507
508 void Assets::ReadSunProperty(const std::string &name, io::TokenStreamReader &in, world::Sun &sun, world::Simulation &sim) const {
509         if (name == "color") {
510                 glm::dvec3 color(0.0);
511                 in.ReadVec(color);
512                 sun.Color(color);
513         } else if (name == "luminosity") {
514                 sun.Luminosity(in.GetDouble());
515         } else {
516                 ReadBodyProperty(name, in, sun, sim);
517         }
518 }
519
520 void Assets::ReadPlanetProperty(const std::string &name, io::TokenStreamReader &in, world::Planet &planet, world::Simulation &sim) const {
521         if (name == "generate") {
522                 string gen;
523                 in.ReadIdentifier(gen);
524                 if (gen == "earthlike") {
525                         world::GenerateEarthlike(data.tile_types, planet);
526                 } else if (gen == "test") {
527                         world::GenerateTest(data.tile_types, planet);
528                 } else {
529                         throw std::runtime_error("unknown surface generator " + gen);
530                 }
531         } else if (name == "atmosphere") {
532                 string atm;
533                 in.ReadIdentifier(atm);
534                 planet.Atmosphere(data.resources[atm].id);
535         } else {
536                 ReadBodyProperty(name, in, planet, sim);
537         }
538 }
539
540 void Assets::ReadBodyProperty(const std::string &name, io::TokenStreamReader &in, world::Body &body, world::Simulation &sim) const {
541         if (name == "name") {
542                 string value;
543                 in.ReadString(value);
544                 body.Name(value);
545         } else if (name == "mass") {
546                 body.Mass(in.GetDouble());
547         } else if (name == "radius") {
548                 body.Radius(in.GetDouble());
549         } else if (name == "axial_tilt") {
550                 glm::dvec2 tilt(0.0);
551                 in.ReadVec(tilt);
552                 body.AxialTilt(tilt);
553         } else if (name == "rotation") {
554                 body.Rotation(in.GetDouble());
555         } else if (name == "angular_momentum") {
556                 body.AngularMomentum(in.GetDouble());
557         } else if (name == "orbit") {
558                 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
559                 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
560                         string oname;
561                         in.ReadIdentifier(oname);
562                         in.Skip(io::Token::EQUALS);
563                         if (oname == "SMA" || oname == "semi_major_axis") {
564                                 body.GetOrbit().SemiMajorAxis(in.GetDouble());
565                         } else if (oname == "ECC" || oname == "eccentricity") {
566                                 body.GetOrbit().Eccentricity(in.GetDouble());
567                         } else if (oname == "INC" || oname == "inclination") {
568                                 body.GetOrbit().Inclination(in.GetDouble());
569                         } else if (oname == "ASC" || oname == "ascending_node" || oname == "longitude_ascending") {
570                                 body.GetOrbit().LongitudeAscending(in.GetDouble());
571                         } else if (oname == "ARG" || oname == "APE" || oname == "argument_periapsis") {
572                                 body.GetOrbit().ArgumentPeriapsis(in.GetDouble());
573                         } else if (oname == "MNA" || oname == "mean_anomaly") {
574                                 body.GetOrbit().MeanAnomaly(in.GetDouble());
575                         } else {
576                                 throw std::runtime_error("unknown orbit property " + oname);
577                         }
578                         in.Skip(io::Token::SEMICOLON);
579                 }
580                 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
581         } else if (name == "children") {
582                 in.Skip(io::Token::BRACKET_OPEN);
583                 while (in.Peek().type != io::Token::BRACKET_CLOSE) {
584                         world::Body *b = ReadBody(in, sim);
585                         b->SetParent(body);
586                         if (in.Peek().type == io::Token::COMMA) {
587                                 in.Skip(io::Token::COMMA);
588                         }
589                 }
590                 in.Skip(io::Token::BRACKET_CLOSE);
591         } else {
592                 throw std::runtime_error("unknown body property " + name);
593         }
594 }
595
596 }
597 }