]> git.localhorst.tv Git - blobs.git/blob - src/app/app.cpp
a732ca64b21cc8cc5130864a740f47b5ccc47291
[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 , font_path(path + "fonts/")
177 , skin_path(path + "skins/")
178 , tile_path(path + "tiles/")
179 , random(0x6283B64CEFE47925)
180 , fonts{
181         graphics::Font(font_path + "DejaVuSans.ttf", 32),
182         graphics::Font(font_path + "DejaVuSans.ttf", 24),
183         graphics::Font(font_path + "DejaVuSans.ttf", 16)
184 } {
185         {
186                 std::ifstream resource_file(data_path + "resources");
187                 io::TokenStreamReader resource_reader(resource_file);
188                 ReadResources(resource_reader);
189         }
190
191         {
192                 std::ifstream tile_file(data_path + "tile_types");
193                 io::TokenStreamReader tile_reader(tile_file);
194                 ReadTileTypes(tile_reader);
195         }
196
197
198         graphics::Format format;
199         textures.tiles.Bind();
200         textures.tiles.Reserve(256, 256, 14, format);
201         LoadTileTexture("algae",    textures.tiles,  0);
202         LoadTileTexture("desert",   textures.tiles,  1);
203         LoadTileTexture("forest",   textures.tiles,  2);
204         LoadTileTexture("grass",    textures.tiles,  3);
205         LoadTileTexture("ice",      textures.tiles,  4);
206         LoadTileTexture("jungle",   textures.tiles,  5);
207         LoadTileTexture("mountain", textures.tiles,  6);
208         LoadTileTexture("ocean",    textures.tiles,  7);
209         LoadTileTexture("rock",     textures.tiles,  8);
210         LoadTileTexture("sand",     textures.tiles,  9);
211         LoadTileTexture("taiga",    textures.tiles, 10);
212         LoadTileTexture("tundra",   textures.tiles, 11);
213         LoadTileTexture("water",    textures.tiles, 12);
214         LoadTileTexture("wheat",    textures.tiles, 13);
215         textures.tiles.FilterTrilinear();
216
217         textures.skins.Bind();
218         textures.skins.Reserve(256, 256, 9, format);
219         LoadSkinTexture("plain", textures.skins, 0);
220         LoadSkinTexture("stripes", textures.skins, 1);
221         LoadSkinTexture("dots", textures.skins, 2);
222         LoadSkinTexture("lines", textures.skins, 3);
223         LoadSkinTexture("spots", textures.skins, 4);
224         LoadSkinTexture("circles", textures.skins, 5);
225         textures.skins.FilterTrilinear();
226 }
227
228 Assets::~Assets() {
229 }
230
231 void Assets::ReadResources(io::TokenStreamReader &in) {
232         while (in.HasMore()) {
233                 string name;
234                 in.ReadIdentifier(name);
235                 in.Skip(io::Token::EQUALS);
236
237                 int id = 0;
238                 if (data.resources.Has(name)) {
239                         id = data.resources[name].id;
240                 } else {
241                         world::Resource res;
242                         res.name = name;
243                         id = data.resources.Add(res);
244                 }
245
246                 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
247                 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
248                         in.ReadIdentifier(name);
249                         in.Skip(io::Token::EQUALS);
250                         if (name == "label") {
251                                 in.ReadString(data.resources[id].label);
252                         } else if (name == "density") {
253                                 data.resources[id].density = in.GetDouble();
254                         } else if (name == "state") {
255                                 in.ReadIdentifier(name);
256                                 if (name == "solid") {
257                                         data.resources[id].state = world::Resource::SOLID;
258                                 } else if (name == "liquid") {
259                                         data.resources[id].state = world::Resource::LIQUID;
260                                 } else if (name == "gas") {
261                                         data.resources[id].state = world::Resource::GAS;
262                                 } else if (name == "plasma") {
263                                         data.resources[id].state = world::Resource::PLASMA;
264                                 } else {
265                                         throw std::runtime_error("unknown resource state '" + name + "'");
266                                 }
267                         } else if (name == "base_color") {
268                                 in.ReadVec(data.resources[id].base_color);
269                         } else {
270                                 throw std::runtime_error("unknown resource property '" + name + "'");
271                         }
272                         in.Skip(io::Token::SEMICOLON);
273                 }
274                 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
275                 in.Skip(io::Token::SEMICOLON);
276         }
277 }
278
279 void Assets::ReadTileTypes(io::TokenStreamReader &in) {
280         while (in.HasMore()) {
281                 string name;
282                 in.ReadIdentifier(name);
283                 in.Skip(io::Token::EQUALS);
284
285                 int id = 0;
286                 if (data.tile_types.Has(name)) {
287                         id = data.tile_types[name].id;
288                 } else {
289                         world::TileType type;
290                         type.name = name;
291                         id = data.tile_types.Add(type);
292                 }
293
294                 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
295                 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
296                         in.ReadIdentifier(name);
297                         in.Skip(io::Token::EQUALS);
298                         if (name == "label") {
299                                 in.ReadString(data.tile_types[id].label);
300                         } else if (name == "texture") {
301                                 data.tile_types[id].texture = in.GetInt();
302                         } else if (name == "yield") {
303                                 in.Skip(io::Token::BRACKET_OPEN);
304                                 while (in.Peek().type != io::Token::BRACKET_CLOSE) {
305                                         world::TileType::Yield yield;
306                                         in.Skip(io::Token::ANGLE_BRACKET_OPEN);
307                                         while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
308                                                 in.ReadIdentifier(name);
309                                                 in.Skip(io::Token::EQUALS);
310                                                 if (name == "resource") {
311                                                         in.ReadIdentifier(name);
312                                                         yield.resource = data.resources[name].id;
313                                                 } else if (name == "ubiquity") {
314                                                         yield.ubiquity = in.GetDouble();
315                                                 } else {
316                                                         throw std::runtime_error("unknown tile type yield property '" + name + "'");
317                                                 }
318                                                 in.Skip(io::Token::SEMICOLON);
319                                         }
320                                         in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
321                                         data.tile_types[id].resources.push_back(yield);
322                                         if (in.Peek().type == io::Token::COMMA) {
323                                                 in.Skip(io::Token::COMMA);
324                                         }
325                                 }
326                                 in.Skip(io::Token::BRACKET_CLOSE);
327                         } else {
328                                 throw std::runtime_error("unknown tile type property '" + name + "'");
329                         }
330                         in.Skip(io::Token::SEMICOLON);
331                 }
332                 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
333                 in.Skip(io::Token::SEMICOLON);
334         }
335 }
336
337 void Assets::LoadTileTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
338         string path = tile_path + name + ".png";
339         SDL_Surface *srf = IMG_Load(path.c_str());
340         if (!srf) {
341                 throw SDLError("IMG_Load");
342         }
343         try {
344                 tex.Data(layer, *srf);
345         } catch (...) {
346                 SDL_FreeSurface(srf);
347                 throw;
348         }
349         SDL_FreeSurface(srf);
350 }
351
352 void Assets::LoadSkinTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
353         string path = skin_path + name + ".png";
354         SDL_Surface *srf = IMG_Load(path.c_str());
355         if (!srf) {
356                 throw SDLError("IMG_Load");
357         }
358         try {
359                 tex.Data(layer, *srf);
360         } catch (...) {
361                 SDL_FreeSurface(srf);
362                 throw;
363         }
364         SDL_FreeSurface(srf);
365 }
366
367 }
368 }