#include "init.hpp"
#include "../graphics/Viewport.hpp"
+#include "../io/Token.hpp"
+#include "../io/TokenStreamReader.hpp"
+#include <fstream>
#include <SDL.h>
#include <SDL_image.h>
+using std::string;
+
namespace blobs {
namespace app {
Assets::Assets()
: path("assets/")
-, tile_path(path + "tiles/")
-, skin_path(path + "skins/") {
+, data_path(path + "data/")
+, skin_path(path + "skins/")
+, tile_path(path + "tiles/") {
+ data.resources.Add({ "air", "Air", 0 });
+ data.resources.Add({ "biomass", "Biomass", 0 });
+ data.resources.Add({ "dirt", "Dirt", 0 });
+ data.resources.Add({ "ice", "Ice", 0 });
+ data.resources.Add({ "rock", "Rock", 0 });
+ data.resources.Add({ "sand", "Sand", 0 });
+ data.resources.Add({ "water", "Water", 0 });
+ data.resources.Add({ "wood", "Wood", 0 });
+
+ {
+ std::ifstream tile_file(data_path + "tiles");
+ io::TokenStreamReader tile_reader(tile_file);
+ ReadTileTypes(tile_reader);
+ }
+
graphics::Format format;
textures.tiles.Bind();
textures.tiles.Reserve(256, 256, 14, format);
LoadTileTexture("tundra", textures.tiles, 11);
LoadTileTexture("water", textures.tiles, 12);
LoadTileTexture("wheat", textures.tiles, 13);
+
textures.skins.Bind();
textures.skins.Reserve(256, 256, 9, format);
LoadSkinTexture("1", textures.skins, 0);
Assets::~Assets() {
}
-void Assets::LoadTileTexture(const std::string &name, graphics::ArrayTexture &tex, int layer) const {
- std::string path = tile_path + name + ".png";
+void Assets::ReadTileTypes(io::TokenStreamReader &in) {
+ while (in.HasMore()) {
+ string name;
+ in.ReadIdentifier(name);
+ in.Skip(io::Token::EQUALS);
+
+ int id = 0;
+ if (data.tiles.Has(name)) {
+ id = data.tiles[name].id;
+ } else {
+ world::TileType type;
+ type.name = name;
+ id = data.tiles.Add(type);
+ }
+
+ in.Skip(io::Token::ANGLE_BRACKET_OPEN);
+ while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
+ in.ReadIdentifier(name);
+ in.Skip(io::Token::EQUALS);
+ if (name == "label") {
+ in.ReadString(data.tiles[id].label);
+ } else if (name == "texture") {
+ data.tiles[id].texture = in.GetInt();
+ } else if (name == "yield") {
+ in.Skip(io::Token::BRACKET_OPEN);
+ while (in.Peek().type != io::Token::BRACKET_CLOSE) {
+ world::TileType::Yield yield;
+ in.Skip(io::Token::ANGLE_BRACKET_OPEN);
+ while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
+ in.ReadIdentifier(name);
+ in.Skip(io::Token::EQUALS);
+ if (name == "resource") {
+ in.ReadIdentifier(name);
+ yield.resource = data.resources[name].id;
+ } else if (name == "ubiquity") {
+ yield.ubiquity = in.GetDouble();
+ } else {
+ throw std::runtime_error("unknown tile type yield property '" + name + "'");
+ }
+ in.Skip(io::Token::SEMICOLON);
+ }
+ in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
+ data.tiles[id].resources.push_back(yield);
+ if (in.Peek().type == io::Token::COMMA) {
+ in.Skip(io::Token::COMMA);
+ }
+ }
+ in.Skip(io::Token::BRACKET_CLOSE);
+ } else {
+ throw std::runtime_error("unknown tile type property '" + name + "'");
+ }
+ in.Skip(io::Token::SEMICOLON);
+ }
+ in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
+ in.Skip(io::Token::SEMICOLON);
+ }
+}
+
+void Assets::LoadTileTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
+ string path = tile_path + name + ".png";
SDL_Surface *srf = IMG_Load(path.c_str());
if (!srf) {
throw SDLError("IMG_Load");
SDL_FreeSurface(srf);
}
-void Assets::LoadSkinTexture(const std::string &name, graphics::ArrayTexture &tex, int layer) const {
- std::string path = skin_path + name + ".png";
+void Assets::LoadSkinTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
+ string path = skin_path + name + ".png";
SDL_Surface *srf = IMG_Load(path.c_str());
if (!srf) {
throw SDLError("IMG_Load");