]> git.localhorst.tv Git - blobs.git/blob - src/app/app.cpp
figure out our creature's metabolism
[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
10 #include <fstream>
11 #include <SDL.h>
12 #include <SDL_image.h>
13
14 using std::string;
15
16
17 namespace blobs {
18 namespace app {
19
20 Application::Application(Window &win, graphics::Viewport &vp)
21 : window(win)
22 , viewport(vp)
23 , states() {
24 }
25
26 Application::~Application() {
27 }
28
29
30 void Application::PushState(State *s) {
31         s->app = this;
32         if (!states.empty()) {
33                 states.top()->OnPause();
34         }
35         states.emplace(s);
36         ++s->ref_count;
37         if (s->ref_count == 1) {
38                 s->OnEnter();
39         }
40         s->OnResume();
41 }
42
43 State *Application::PopState() {
44         State *s = states.top();
45         states.pop();
46         s->OnPause();
47         s->OnExit();
48         if (!states.empty()) {
49                 states.top()->OnResume();
50         }
51         return s;
52 }
53
54 State *Application::SwitchState(State *s_new) {
55         s_new->app = this;
56         State *s_old = states.top();
57         states.top() = s_new;
58         --s_old->ref_count;
59         ++s_new->ref_count;
60         s_old->OnPause();
61         if (s_old->ref_count == 0) {
62                 s_old->OnExit();
63         }
64         if (s_new->ref_count == 1) {
65                 s_new->OnEnter();
66         }
67         s_new->OnResume();
68         return s_old;
69 }
70
71 State &Application::GetState() {
72         return *states.top();
73 }
74
75 bool Application::HasState() const noexcept {
76         return !states.empty();
77 }
78
79
80 void Application::Run() {
81         Uint32 last = SDL_GetTicks();
82         while (HasState()) {
83                 Uint32 now = SDL_GetTicks();
84                 int delta = now - last;
85                 Loop(delta);
86                 last = now;
87         }
88 }
89
90 void Application::Loop(int dt) {
91         HandleEvents();
92         if (!HasState()) return;
93         GetState().Update(dt);
94         if (!HasState()) return;
95         viewport.Clear();
96         GetState().Render(viewport);
97         window.Flip();
98 }
99
100 void Application::HandleEvents() {
101         SDL_Event event;
102         while (HasState() && SDL_PollEvent(&event)) {
103                 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
104                         viewport.Resize(event.window.data1, event.window.data2);
105                 }
106                 GetState().Handle(event);
107         }
108 }
109
110 void State::Handle(const SDL_Event &event) {
111         switch (event.type) {
112                 case SDL_KEYDOWN:
113                         OnKeyDown(event.key);
114                         break;
115                 case SDL_KEYUP:
116                         OnKeyUp(event.key);
117                         break;
118                 case SDL_MOUSEBUTTONDOWN:
119                         OnMouseDown(event.button);
120                         break;
121                 case SDL_MOUSEBUTTONUP:
122                         OnMouseUp(event.button);
123                         break;
124                 case SDL_MOUSEMOTION:
125                         OnMouseMotion(event.motion);
126                         break;
127                 case SDL_MOUSEWHEEL:
128                         OnMouseWheel(event.wheel);
129                         break;
130                 case SDL_QUIT:
131                         OnQuit();
132                         break;
133                 case SDL_WINDOWEVENT:
134                         Handle(event.window);
135                         break;
136                 default:
137                         // ignore
138                         break;
139         }
140 }
141
142 void State::Handle(const SDL_WindowEvent &event) {
143         switch (event.event) {
144                 case SDL_WINDOWEVENT_FOCUS_GAINED:
145                         OnFocus();
146                         break;
147                 case SDL_WINDOWEVENT_FOCUS_LOST:
148                         OnBlur();
149                         break;
150                 case SDL_WINDOWEVENT_RESIZED:
151                         OnResize(event.data1, event.data2);
152                         break;
153                 default:
154                         break;
155         }
156 }
157
158 void State::Update(int dt) {
159         OnUpdate(dt);
160 }
161
162 void State::Render(graphics::Viewport &viewport) {
163         OnRender(viewport);
164 }
165
166 void State::OnQuit() {
167         while (App().HasState()) {
168                 App().PopState();
169         }
170 }
171
172
173 Assets::Assets()
174 : path("assets/")
175 , data_path(path + "data/")
176 , skin_path(path + "skins/")
177 , tile_path(path + "tiles/") {
178         {
179                 std::ifstream resource_file(data_path + "resources");
180                 io::TokenStreamReader resource_reader(resource_file);
181                 ReadResources(resource_reader);
182         }
183
184         {
185                 std::ifstream tile_file(data_path + "tiles");
186                 io::TokenStreamReader tile_reader(tile_file);
187                 ReadTileTypes(tile_reader);
188         }
189
190         graphics::Format format;
191         textures.tiles.Bind();
192         textures.tiles.Reserve(256, 256, 14, format);
193         LoadTileTexture("algae",    textures.tiles,  0);
194         LoadTileTexture("desert",   textures.tiles,  1);
195         LoadTileTexture("forest",   textures.tiles,  2);
196         LoadTileTexture("grass",    textures.tiles,  3);
197         LoadTileTexture("ice",      textures.tiles,  4);
198         LoadTileTexture("jungle",   textures.tiles,  5);
199         LoadTileTexture("mountain", textures.tiles,  6);
200         LoadTileTexture("ocean",    textures.tiles,  7);
201         LoadTileTexture("rock",     textures.tiles,  8);
202         LoadTileTexture("sand",     textures.tiles,  9);
203         LoadTileTexture("taiga",    textures.tiles, 10);
204         LoadTileTexture("tundra",   textures.tiles, 11);
205         LoadTileTexture("water",    textures.tiles, 12);
206         LoadTileTexture("wheat",    textures.tiles, 13);
207
208         textures.skins.Bind();
209         textures.skins.Reserve(256, 256, 9, format);
210         LoadSkinTexture("1", textures.skins, 0);
211         LoadSkinTexture("2", textures.skins, 1);
212         LoadSkinTexture("3", textures.skins, 2);
213         LoadSkinTexture("4", textures.skins, 3);
214         LoadSkinTexture("5", textures.skins, 4);
215         LoadSkinTexture("6", textures.skins, 5);
216         LoadSkinTexture("7", textures.skins, 6);
217         LoadSkinTexture("8", textures.skins, 7);
218         LoadSkinTexture("9", textures.skins, 8);
219 }
220
221 Assets::~Assets() {
222 }
223
224 void Assets::ReadResources(io::TokenStreamReader &in) {
225         while (in.HasMore()) {
226                 string name;
227                 in.ReadIdentifier(name);
228                 in.Skip(io::Token::EQUALS);
229
230                 int id = 0;
231                 if (data.resources.Has(name)) {
232                         id = data.resources[name].id;
233                 } else {
234                         world::Resource res;
235                         res.name = name;
236                         id = data.resources.Add(res);
237                 }
238
239                 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
240                 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
241                         in.ReadIdentifier(name);
242                         in.Skip(io::Token::EQUALS);
243                         if (name == "label") {
244                                 in.ReadString(data.resources[id].label);
245                         } else if (name == "state") {
246                                 in.ReadIdentifier(name);
247                                 if (name == "solid") {
248                                         data.resources[id].state = world::Resource::SOLID;
249                                 } else if (name == "liquid") {
250                                         data.resources[id].state = world::Resource::LIQUID;
251                                 } else if (name == "gas") {
252                                         data.resources[id].state = world::Resource::GAS;
253                                 } else if (name == "plasma") {
254                                         data.resources[id].state = world::Resource::PLASMA;
255                                 } else {
256                                         throw std::runtime_error("unknown resource state '" + name + "'");
257                                 }
258                         } else {
259                                 throw std::runtime_error("unknown resource property '" + name + "'");
260                         }
261                         in.Skip(io::Token::SEMICOLON);
262                 }
263                 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
264                 in.Skip(io::Token::SEMICOLON);
265         }
266 }
267
268 void Assets::ReadTileTypes(io::TokenStreamReader &in) {
269         while (in.HasMore()) {
270                 string name;
271                 in.ReadIdentifier(name);
272                 in.Skip(io::Token::EQUALS);
273
274                 int id = 0;
275                 if (data.tiles.Has(name)) {
276                         id = data.tiles[name].id;
277                 } else {
278                         world::TileType type;
279                         type.name = name;
280                         id = data.tiles.Add(type);
281                 }
282
283                 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
284                 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
285                         in.ReadIdentifier(name);
286                         in.Skip(io::Token::EQUALS);
287                         if (name == "label") {
288                                 in.ReadString(data.tiles[id].label);
289                         } else if (name == "texture") {
290                                 data.tiles[id].texture = in.GetInt();
291                         } else if (name == "yield") {
292                                 in.Skip(io::Token::BRACKET_OPEN);
293                                 while (in.Peek().type != io::Token::BRACKET_CLOSE) {
294                                         world::TileType::Yield yield;
295                                         in.Skip(io::Token::ANGLE_BRACKET_OPEN);
296                                         while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
297                                                 in.ReadIdentifier(name);
298                                                 in.Skip(io::Token::EQUALS);
299                                                 if (name == "resource") {
300                                                         in.ReadIdentifier(name);
301                                                         yield.resource = data.resources[name].id;
302                                                 } else if (name == "ubiquity") {
303                                                         yield.ubiquity = in.GetDouble();
304                                                 } else {
305                                                         throw std::runtime_error("unknown tile type yield property '" + name + "'");
306                                                 }
307                                                 in.Skip(io::Token::SEMICOLON);
308                                         }
309                                         in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
310                                         data.tiles[id].resources.push_back(yield);
311                                         if (in.Peek().type == io::Token::COMMA) {
312                                                 in.Skip(io::Token::COMMA);
313                                         }
314                                 }
315                                 in.Skip(io::Token::BRACKET_CLOSE);
316                         } else {
317                                 throw std::runtime_error("unknown tile type property '" + name + "'");
318                         }
319                         in.Skip(io::Token::SEMICOLON);
320                 }
321                 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
322                 in.Skip(io::Token::SEMICOLON);
323         }
324 }
325
326 void Assets::LoadTileTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
327         string path = tile_path + name + ".png";
328         SDL_Surface *srf = IMG_Load(path.c_str());
329         if (!srf) {
330                 throw SDLError("IMG_Load");
331         }
332         try {
333                 tex.Data(layer, *srf);
334         } catch (...) {
335                 SDL_FreeSurface(srf);
336                 throw;
337         }
338         SDL_FreeSurface(srf);
339 }
340
341 void Assets::LoadSkinTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
342         string path = skin_path + name + ".png";
343         SDL_Surface *srf = IMG_Load(path.c_str());
344         if (!srf) {
345                 throw SDLError("IMG_Load");
346         }
347         try {
348                 tex.Data(layer, *srf);
349         } catch (...) {
350                 SDL_FreeSurface(srf);
351                 throw;
352         }
353         SDL_FreeSurface(srf);
354 }
355
356 }
357 }