1 #include "Application.hpp"
6 #include "../graphics/Viewport.hpp"
7 #include "../io/Token.hpp"
8 #include "../io/TokenStreamReader.hpp"
12 #include <SDL_image.h>
20 Application::Application(Window &win, graphics::Viewport &vp)
26 Application::~Application() {
30 void Application::PushState(State *s) {
32 if (!states.empty()) {
33 states.top()->OnPause();
37 if (s->ref_count == 1) {
43 State *Application::PopState() {
44 State *s = states.top();
48 if (!states.empty()) {
49 states.top()->OnResume();
54 State *Application::SwitchState(State *s_new) {
56 State *s_old = states.top();
61 if (s_old->ref_count == 0) {
64 if (s_new->ref_count == 1) {
71 State &Application::GetState() {
75 bool Application::HasState() const noexcept {
76 return !states.empty();
80 void Application::Run() {
81 Uint32 last = SDL_GetTicks();
83 Uint32 now = SDL_GetTicks();
84 int delta = now - last;
90 void Application::Loop(int dt) {
92 if (!HasState()) return;
93 GetState().Update(dt);
94 if (!HasState()) return;
96 GetState().Render(viewport);
100 void Application::HandleEvents() {
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);
106 GetState().Handle(event);
110 void State::Handle(const SDL_Event &event) {
111 switch (event.type) {
113 OnKeyDown(event.key);
118 case SDL_MOUSEBUTTONDOWN:
119 OnMouseDown(event.button);
121 case SDL_MOUSEBUTTONUP:
122 OnMouseUp(event.button);
124 case SDL_MOUSEMOTION:
125 OnMouseMotion(event.motion);
128 OnMouseWheel(event.wheel);
133 case SDL_WINDOWEVENT:
134 Handle(event.window);
142 void State::Handle(const SDL_WindowEvent &event) {
143 switch (event.event) {
144 case SDL_WINDOWEVENT_FOCUS_GAINED:
147 case SDL_WINDOWEVENT_FOCUS_LOST:
150 case SDL_WINDOWEVENT_RESIZED:
151 OnResize(event.data1, event.data2);
158 void State::Update(int dt) {
162 void State::Render(graphics::Viewport &viewport) {
166 void State::OnQuit() {
167 while (App().HasState()) {
175 , data_path(path + "data/")
176 , font_path(path + "fonts/")
177 , skin_path(path + "skins/")
178 , tile_path(path + "tiles/")
179 , random(0x6283B64CEFE57925)
181 graphics::Font(font_path + "DejaVuSans.ttf", 32),
182 graphics::Font(font_path + "DejaVuSans.ttf", 24),
183 graphics::Font(font_path + "DejaVuSans.ttf", 16)
186 std::ifstream resource_file(data_path + "resources");
187 io::TokenStreamReader resource_reader(resource_file);
188 ReadResources(resource_reader);
192 std::ifstream tile_file(data_path + "tile_types");
193 io::TokenStreamReader tile_reader(tile_file);
194 ReadTileTypes(tile_reader);
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();
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();
231 void Assets::ReadResources(io::TokenStreamReader &in) {
232 while (in.HasMore()) {
234 in.ReadIdentifier(name);
235 in.Skip(io::Token::EQUALS);
238 if (data.resources.Has(name)) {
239 id = data.resources[name].id;
243 id = data.resources.Add(res);
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 == "energy") {
255 data.resources[id].energy = in.GetDouble();
256 data.resources[id].inverse_energy = 1.0 / data.resources[id].energy;
257 } else if (name == "state") {
258 in.ReadIdentifier(name);
259 if (name == "solid") {
260 data.resources[id].state = world::Resource::SOLID;
261 } else if (name == "liquid") {
262 data.resources[id].state = world::Resource::LIQUID;
263 } else if (name == "gas") {
264 data.resources[id].state = world::Resource::GAS;
265 } else if (name == "plasma") {
266 data.resources[id].state = world::Resource::PLASMA;
268 throw std::runtime_error("unknown resource state '" + name + "'");
270 } else if (name == "base_color") {
271 in.ReadVec(data.resources[id].base_color);
273 throw std::runtime_error("unknown resource property '" + name + "'");
275 in.Skip(io::Token::SEMICOLON);
277 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
278 in.Skip(io::Token::SEMICOLON);
282 void Assets::ReadTileTypes(io::TokenStreamReader &in) {
283 while (in.HasMore()) {
285 in.ReadIdentifier(name);
286 in.Skip(io::Token::EQUALS);
289 if (data.tile_types.Has(name)) {
290 id = data.tile_types[name].id;
292 world::TileType type;
294 id = data.tile_types.Add(type);
297 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
298 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
299 in.ReadIdentifier(name);
300 in.Skip(io::Token::EQUALS);
301 if (name == "label") {
302 in.ReadString(data.tile_types[id].label);
303 } else if (name == "texture") {
304 data.tile_types[id].texture = in.GetInt();
305 } else if (name == "yield") {
306 in.Skip(io::Token::BRACKET_OPEN);
307 while (in.Peek().type != io::Token::BRACKET_CLOSE) {
308 world::TileType::Yield yield;
309 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
310 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
311 in.ReadIdentifier(name);
312 in.Skip(io::Token::EQUALS);
313 if (name == "resource") {
314 in.ReadIdentifier(name);
315 yield.resource = data.resources[name].id;
316 } else if (name == "ubiquity") {
317 yield.ubiquity = in.GetDouble();
319 throw std::runtime_error("unknown tile type yield property '" + name + "'");
321 in.Skip(io::Token::SEMICOLON);
323 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
324 data.tile_types[id].resources.push_back(yield);
325 if (in.Peek().type == io::Token::COMMA) {
326 in.Skip(io::Token::COMMA);
329 in.Skip(io::Token::BRACKET_CLOSE);
331 throw std::runtime_error("unknown tile type property '" + name + "'");
333 in.Skip(io::Token::SEMICOLON);
335 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
336 in.Skip(io::Token::SEMICOLON);
340 void Assets::LoadTileTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
341 string path = tile_path + name + ".png";
342 SDL_Surface *srf = IMG_Load(path.c_str());
344 throw SDLError("IMG_Load");
347 tex.Data(layer, *srf);
349 SDL_FreeSurface(srf);
352 SDL_FreeSurface(srf);
355 void Assets::LoadSkinTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
356 string path = skin_path + name + ".png";
357 SDL_Surface *srf = IMG_Load(path.c_str());
359 throw SDLError("IMG_Load");
362 tex.Data(layer, *srf);
364 SDL_FreeSurface(srf);
367 SDL_FreeSurface(srf);