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 , skin_path(path + "skins/")
177 , tile_path(path + "tiles/") {
179 std::ifstream resource_file(data_path + "resources");
180 io::TokenStreamReader resource_reader(resource_file);
181 ReadResources(resource_reader);
185 std::ifstream tile_file(data_path + "tiles");
186 io::TokenStreamReader tile_reader(tile_file);
187 ReadTileTypes(tile_reader);
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);
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);
224 void Assets::ReadResources(io::TokenStreamReader &in) {
225 while (in.HasMore()) {
227 in.ReadIdentifier(name);
228 in.Skip(io::Token::EQUALS);
231 if (data.resources.Has(name)) {
232 id = data.resources[name].id;
236 id = data.resources.Add(res);
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);
246 throw std::runtime_error("unknown resource property '" + name + "'");
248 in.Skip(io::Token::SEMICOLON);
250 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
251 in.Skip(io::Token::SEMICOLON);
255 void Assets::ReadTileTypes(io::TokenStreamReader &in) {
256 while (in.HasMore()) {
258 in.ReadIdentifier(name);
259 in.Skip(io::Token::EQUALS);
262 if (data.tiles.Has(name)) {
263 id = data.tiles[name].id;
265 world::TileType type;
267 id = data.tiles.Add(type);
270 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
271 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
272 in.ReadIdentifier(name);
273 in.Skip(io::Token::EQUALS);
274 if (name == "label") {
275 in.ReadString(data.tiles[id].label);
276 } else if (name == "texture") {
277 data.tiles[id].texture = in.GetInt();
278 } else if (name == "yield") {
279 in.Skip(io::Token::BRACKET_OPEN);
280 while (in.Peek().type != io::Token::BRACKET_CLOSE) {
281 world::TileType::Yield yield;
282 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
283 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
284 in.ReadIdentifier(name);
285 in.Skip(io::Token::EQUALS);
286 if (name == "resource") {
287 in.ReadIdentifier(name);
288 yield.resource = data.resources[name].id;
289 } else if (name == "ubiquity") {
290 yield.ubiquity = in.GetDouble();
292 throw std::runtime_error("unknown tile type yield property '" + name + "'");
294 in.Skip(io::Token::SEMICOLON);
296 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
297 data.tiles[id].resources.push_back(yield);
298 if (in.Peek().type == io::Token::COMMA) {
299 in.Skip(io::Token::COMMA);
302 in.Skip(io::Token::BRACKET_CLOSE);
304 throw std::runtime_error("unknown tile type property '" + name + "'");
306 in.Skip(io::Token::SEMICOLON);
308 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
309 in.Skip(io::Token::SEMICOLON);
313 void Assets::LoadTileTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
314 string path = tile_path + name + ".png";
315 SDL_Surface *srf = IMG_Load(path.c_str());
317 throw SDLError("IMG_Load");
320 tex.Data(layer, *srf);
322 SDL_FreeSurface(srf);
325 SDL_FreeSurface(srf);
328 void Assets::LoadSkinTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
329 string path = skin_path + name + ".png";
330 SDL_Surface *srf = IMG_Load(path.c_str());
332 throw SDLError("IMG_Load");
335 tex.Data(layer, *srf);
337 SDL_FreeSurface(srf);
340 SDL_FreeSurface(srf);