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/") {
178 data.resources.Add({ "air", "Air", 0 });
179 data.resources.Add({ "biomass", "Biomass", 0 });
180 data.resources.Add({ "dirt", "Dirt", 0 });
181 data.resources.Add({ "ice", "Ice", 0 });
182 data.resources.Add({ "rock", "Rock", 0 });
183 data.resources.Add({ "sand", "Sand", 0 });
184 data.resources.Add({ "water", "Water", 0 });
185 data.resources.Add({ "wood", "Wood", 0 });
188 std::ifstream tile_file(data_path + "tiles");
189 io::TokenStreamReader tile_reader(tile_file);
190 ReadTileTypes(tile_reader);
193 graphics::Format format;
194 textures.tiles.Bind();
195 textures.tiles.Reserve(256, 256, 14, format);
196 LoadTileTexture("algae", textures.tiles, 0);
197 LoadTileTexture("desert", textures.tiles, 1);
198 LoadTileTexture("forest", textures.tiles, 2);
199 LoadTileTexture("grass", textures.tiles, 3);
200 LoadTileTexture("ice", textures.tiles, 4);
201 LoadTileTexture("jungle", textures.tiles, 5);
202 LoadTileTexture("mountain", textures.tiles, 6);
203 LoadTileTexture("ocean", textures.tiles, 7);
204 LoadTileTexture("rock", textures.tiles, 8);
205 LoadTileTexture("sand", textures.tiles, 9);
206 LoadTileTexture("taiga", textures.tiles, 10);
207 LoadTileTexture("tundra", textures.tiles, 11);
208 LoadTileTexture("water", textures.tiles, 12);
209 LoadTileTexture("wheat", textures.tiles, 13);
211 textures.skins.Bind();
212 textures.skins.Reserve(256, 256, 9, format);
213 LoadSkinTexture("1", textures.skins, 0);
214 LoadSkinTexture("2", textures.skins, 1);
215 LoadSkinTexture("3", textures.skins, 2);
216 LoadSkinTexture("4", textures.skins, 3);
217 LoadSkinTexture("5", textures.skins, 4);
218 LoadSkinTexture("6", textures.skins, 5);
219 LoadSkinTexture("7", textures.skins, 6);
220 LoadSkinTexture("8", textures.skins, 7);
221 LoadSkinTexture("9", textures.skins, 8);
227 void Assets::ReadTileTypes(io::TokenStreamReader &in) {
228 while (in.HasMore()) {
230 in.ReadIdentifier(name);
231 in.Skip(io::Token::EQUALS);
234 if (data.tiles.Has(name)) {
235 id = data.tiles[name].id;
237 world::TileType type;
239 id = data.tiles.Add(type);
242 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
243 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
244 in.ReadIdentifier(name);
245 in.Skip(io::Token::EQUALS);
246 if (name == "label") {
247 in.ReadString(data.tiles[id].label);
248 } else if (name == "texture") {
249 data.tiles[id].texture = in.GetInt();
250 } else if (name == "yield") {
251 in.Skip(io::Token::BRACKET_OPEN);
252 while (in.Peek().type != io::Token::BRACKET_CLOSE) {
253 world::TileType::Yield yield;
254 in.Skip(io::Token::ANGLE_BRACKET_OPEN);
255 while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
256 in.ReadIdentifier(name);
257 in.Skip(io::Token::EQUALS);
258 if (name == "resource") {
259 in.ReadIdentifier(name);
260 yield.resource = data.resources[name].id;
261 } else if (name == "ubiquity") {
262 yield.ubiquity = in.GetDouble();
264 throw std::runtime_error("unknown tile type yield property '" + name + "'");
266 in.Skip(io::Token::SEMICOLON);
268 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
269 data.tiles[id].resources.push_back(yield);
270 if (in.Peek().type == io::Token::COMMA) {
271 in.Skip(io::Token::COMMA);
274 in.Skip(io::Token::BRACKET_CLOSE);
276 throw std::runtime_error("unknown tile type property '" + name + "'");
278 in.Skip(io::Token::SEMICOLON);
280 in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
281 in.Skip(io::Token::SEMICOLON);
285 void Assets::LoadTileTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
286 string path = tile_path + name + ".png";
287 SDL_Surface *srf = IMG_Load(path.c_str());
289 throw SDLError("IMG_Load");
292 tex.Data(layer, *srf);
294 SDL_FreeSurface(srf);
297 SDL_FreeSurface(srf);
300 void Assets::LoadSkinTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
301 string path = skin_path + name + ".png";
302 SDL_Surface *srf = IMG_Load(path.c_str());
304 throw SDLError("IMG_Load");
307 tex.Data(layer, *srf);
309 SDL_FreeSurface(srf);
312 SDL_FreeSurface(srf);