git repos / blockattack-game

blame: source/code/sago/SagoSpriteHolder.cpp

normal view · raw

82da8a1c sago007 2015-12-19 18:00 1
/*
47fef8e8 sago007 2016-11-24 21:37 2
Copyright (c) 2015 Poul Sander
82da8a1c sago007 2015-12-19 18:00 3
47fef8e8 sago007 2016-11-24 21:37 4
Permission is hereby granted, free of charge, to any person
47fef8e8 sago007 2016-11-24 21:37 5
obtaining a copy of this software and associated documentation files
47fef8e8 sago007 2016-11-24 21:37 6
(the "Software"), to deal in the Software without restriction,
47fef8e8 sago007 2016-11-24 21:37 7
including without limitation the rights to use, copy, modify, merge,
47fef8e8 sago007 2016-11-24 21:37 8
publish, distribute, sublicense, and/or sell copies of the Software,
47fef8e8 sago007 2016-11-24 21:37 9
and to permit persons to whom the Software is furnished to do so,
47fef8e8 sago007 2016-11-24 21:37 10
subject to the following conditions:
82da8a1c sago007 2015-12-19 18:00 11
47fef8e8 sago007 2016-11-24 21:37 12
The above copyright notice and this permission notice shall be
47fef8e8 sago007 2016-11-24 21:37 13
included in all copies or substantial portions of the Software.
82da8a1c sago007 2015-12-19 18:00 14
47fef8e8 sago007 2016-11-24 21:37 15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47fef8e8 sago007 2016-11-24 21:37 16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
47fef8e8 sago007 2016-11-24 21:37 17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
47fef8e8 sago007 2016-11-24 21:37 18
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
47fef8e8 sago007 2016-11-24 21:37 19
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
47fef8e8 sago007 2016-11-24 21:37 20
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
47fef8e8 sago007 2016-11-24 21:37 21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
47fef8e8 sago007 2016-11-24 21:37 22
SOFTWARE.
82da8a1c sago007 2015-12-19 18:00 23
*/
82da8a1c sago007 2015-12-19 18:00 24
82da8a1c sago007 2015-12-19 18:00 25
#include "SagoSpriteHolder.hpp"
82da8a1c sago007 2015-12-19 18:00 26
#include "SagoMisc.hpp"
82da8a1c sago007 2015-12-19 18:00 27
#include <memory>
82da8a1c sago007 2015-12-19 18:00 28
#include <unordered_map>
b81f58ad sago007 2015-12-25 22:48 29
#include <json/json.h>
82da8a1c sago007 2015-12-19 18:00 30
#include <iostream>
82da8a1c sago007 2015-12-19 18:00 31
#include <string.h>
82da8a1c sago007 2015-12-19 18:00 32
#include <boost/algorithm/string/predicate.hpp>
82da8a1c sago007 2015-12-19 18:00 33
96eb7a16 sago007 2016-04-29 17:01 34
using std::string;
96eb7a16 sago007 2016-04-29 17:01 35
using std::cerr;
96eb7a16 sago007 2016-04-29 17:01 36
using std::cout;
96eb7a16 sago007 2016-04-29 17:01 37
using std::vector;
82da8a1c sago007 2015-12-19 18:00 38
82da8a1c sago007 2015-12-19 18:00 39
namespace sago {
82da8a1c sago007 2015-12-19 18:00 40
82da8a1c sago007 2015-12-19 18:00 41
struct SagoSpriteHolder::SagoSpriteHolderData {
82da8a1c sago007 2015-12-19 18:00 42
	const sago::SagoDataHolder* tex;
82da8a1c sago007 2015-12-19 18:00 43
	std::unordered_map<std::string,std::shared_ptr<sago::SagoSprite>> sprites;
82da8a1c sago007 2015-12-19 18:00 44
	const sago::SagoSprite* defaultSprite;
e0223883 sago007 2016-01-30 10:58 45
	bool verbose = false;
82da8a1c sago007 2015-12-19 18:00 46
};
82da8a1c sago007 2015-12-19 18:00 47
82da8a1c sago007 2015-12-19 18:00 48
SagoSpriteHolder::SagoSpriteHolder(const SagoDataHolder& texHolder) {
82da8a1c sago007 2015-12-19 18:00 49
	data = new SagoSpriteHolderData();
82da8a1c sago007 2015-12-19 18:00 50
	try {
82da8a1c sago007 2015-12-19 18:00 51
		data->tex = &texHolder;
82da8a1c sago007 2015-12-19 18:00 52
		ReadSprites();
82da8a1c sago007 2015-12-19 18:00 53
		data->defaultSprite = new sago::SagoSprite(texHolder,"fallback", {0,0,64,64},1,100);
82da8a1c sago007 2015-12-19 18:00 54
	}
82da8a1c sago007 2015-12-19 18:00 55
	catch (...) {
82da8a1c sago007 2015-12-19 18:00 56
		delete data;
82da8a1c sago007 2015-12-19 18:00 57
	}
82da8a1c sago007 2015-12-19 18:00 58
}
82da8a1c sago007 2015-12-19 18:00 59
82da8a1c sago007 2015-12-19 18:00 60
SagoSpriteHolder::~SagoSpriteHolder() {
82da8a1c sago007 2015-12-19 18:00 61
	delete data;
82da8a1c sago007 2015-12-19 18:00 62
}
82da8a1c sago007 2015-12-19 18:00 63
82da8a1c sago007 2015-12-19 18:00 64
void SagoSpriteHolder::ReadSpriteFile(const std::string& filename) {
82da8a1c sago007 2015-12-19 18:00 65
	string fullfile = "sprites/"+filename;
82da8a1c sago007 2015-12-19 18:00 66
	string content = sago::GetFileContent(fullfile.c_str());
82da8a1c sago007 2015-12-19 18:00 67
	Json::Value root;   // will contains the root value after parsing
82da8a1c sago007 2015-12-19 18:00 68
	Json::Reader reader;
82da8a1c sago007 2015-12-19 18:00 69
	bool parsingSuccessful = reader.parse( content, root );
82da8a1c sago007 2015-12-19 18:00 70
	if ( !parsingSuccessful ) {
161a010c sago007 2016-05-06 14:00 71
		cerr << "Failed to parse: " << fullfile << "\n"
161a010c sago007 2016-05-06 14:00 72
		     << reader.getFormattedErrorMessages() << "\n";
82da8a1c sago007 2015-12-19 18:00 73
		return;
82da8a1c sago007 2015-12-19 18:00 74
	}
82da8a1c sago007 2015-12-19 18:00 75
	for (Json::Value::iterator it = root.begin(); it != root.end() ; ++it) {
82da8a1c sago007 2015-12-19 18:00 76
		string spriteName = it.key().asString();
82da8a1c sago007 2015-12-19 18:00 77
		Json::Value value = (*it);
7c8588cd sago007 2017-01-08 13:39 78
		if (!value.isObject()) {
7c8588cd sago007 2017-01-08 13:39 79
			if (spriteName.c_str()[0] != '_') {
7c8588cd sago007 2017-01-08 13:39 80
				//We ignore errors if the name starts with an underscore like "_comment"
7c8588cd sago007 2017-01-08 13:39 81
				std::cerr << "Invalid sprite: " << spriteName << "\n";
7c8588cd sago007 2017-01-08 13:39 82
			}
7c8588cd sago007 2017-01-08 13:39 83
			continue;
7c8588cd sago007 2017-01-08 13:39 84
		}
82da8a1c sago007 2015-12-19 18:00 85
		string textureName = value.get("texture","fallback").asString();
82da8a1c sago007 2015-12-19 18:00 86
		int topx = value.get("topx",0).asInt();
82da8a1c sago007 2015-12-19 18:00 87
		int topy = value.get("topy",0).asInt();
82da8a1c sago007 2015-12-19 18:00 88
		int height = value.get("height",0).asInt();
82da8a1c sago007 2015-12-19 18:00 89
		int width = value.get("width",0).asInt();
82da8a1c sago007 2015-12-19 18:00 90
		int number_of_frames = value.get("number_of_frames",1).asInt();
82da8a1c sago007 2015-12-19 18:00 91
		int frame_time = value.get("frame_time",1).asInt();
82da8a1c sago007 2015-12-19 18:00 92
		int originx = value.get("originx",0).asInt();
82da8a1c sago007 2015-12-19 18:00 93
		int originy = value.get("originy",0).asInt();
82da8a1c sago007 2015-12-19 18:00 94
		if (number_of_frames < 1) {
82da8a1c sago007 2015-12-19 18:00 95
			number_of_frames = 1;
82da8a1c sago007 2015-12-19 18:00 96
		}
82da8a1c sago007 2015-12-19 18:00 97
		if (frame_time < 1) {
82da8a1c sago007 2015-12-19 18:00 98
			frame_time = 1;
82da8a1c sago007 2015-12-19 18:00 99
		}
82da8a1c sago007 2015-12-19 18:00 100
		std::shared_ptr<sago::SagoSprite> ptr(new SagoSprite(*(data->tex),textureName, {topx,topy,width,height},number_of_frames,frame_time));
82da8a1c sago007 2015-12-19 18:00 101
		ptr->SetOrigin({originx,originy, 0, 0});
82da8a1c sago007 2015-12-19 18:00 102
		this->data->sprites[std::string(spriteName)] = ptr;
82da8a1c sago007 2015-12-19 18:00 103
	}
82da8a1c sago007 2015-12-19 18:00 104
}
82da8a1c sago007 2015-12-19 18:00 105
82da8a1c sago007 2015-12-19 18:00 106
void SagoSpriteHolder::ReadSprites() {
82da8a1c sago007 2015-12-19 18:00 107
	std::vector<std::string> spritefiles = GetFileList("sprites");
82da8a1c sago007 2015-12-19 18:00 108
	for (std::string& item : spritefiles  ) {
82da8a1c sago007 2015-12-19 18:00 109
		if (boost::algorithm::ends_with(item,".sprite")) {
e0223883 sago007 2016-01-30 10:58 110
			if (data->verbose) {
161a010c sago007 2016-05-06 14:00 111
				cout << "Found " << item << "\n";
e0223883 sago007 2016-01-30 10:58 112
			}
82da8a1c sago007 2015-12-19 18:00 113
			ReadSpriteFile(item);
82da8a1c sago007 2015-12-19 18:00 114
		}
82da8a1c sago007 2015-12-19 18:00 115
		else {
e0223883 sago007 2016-01-30 10:58 116
			if (data->verbose) {
161a010c sago007 2016-05-06 14:00 117
				cout << "Ignoreing " << item << "\n";
e0223883 sago007 2016-01-30 10:58 118
			}
82da8a1c sago007 2015-12-19 18:00 119
		}
82da8a1c sago007 2015-12-19 18:00 120
	}
82da8a1c sago007 2015-12-19 18:00 121
}
82da8a1c sago007 2015-12-19 18:00 122
82da8a1c sago007 2015-12-19 18:00 123
const sago::SagoSprite& SagoSpriteHolder::GetSprite(const std::string& spritename) const {
82da8a1c sago007 2015-12-19 18:00 124
	std::unordered_map<std::string,std::shared_ptr<sago::SagoSprite>>::const_iterator got = data->sprites.find (spritename);
82da8a1c sago007 2015-12-19 18:00 125
	if ( got == data->sprites.end() ) {
82da8a1c sago007 2015-12-19 18:00 126
		return *data->defaultSprite;
82da8a1c sago007 2015-12-19 18:00 127
	}
82da8a1c sago007 2015-12-19 18:00 128
	else {
82da8a1c sago007 2015-12-19 18:00 129
		return *(got->second);
82da8a1c sago007 2015-12-19 18:00 130
	}
82da8a1c sago007 2015-12-19 18:00 131
}
82da8a1c sago007 2015-12-19 18:00 132
82da8a1c sago007 2015-12-19 18:00 133
const SagoDataHolder& SagoSpriteHolder::GetDataHolder() const {
82da8a1c sago007 2015-12-19 18:00 134
	return *data->tex;
82da8a1c sago007 2015-12-19 18:00 135
}
82da8a1c sago007 2015-12-19 18:00 136
b81f58ad sago007 2015-12-25 22:48 137
}
1970-01-01 00:00 138