Debian Codegraph @

source/code/sago/SagoSpriteHolder.cpp

raw symbols SCIP
1 /*
2 Copyright (c) 2015 Poul Sander
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #include "SagoSpriteHolder.hpp"
26 #include "SagoMisc.hpp"
27 #include <memory>
28 #include <unordered_map>
29 #include "rapidjson/document.h"
30 #include <iostream>
31 #include <string.h>
32 #include <boost/algorithm/string/predicate.hpp>
33
34
35 namespace sago {
36
37 struct SagoSpriteHolder::SagoSpriteHolderData {
38 const sago::SagoDataHolder* tex;
39 std::unordered_map<std::string,std::shared_ptr<sago::SagoSprite>> sprites;
40 const sago::SagoSprite* defaultSprite;
41 bool verbose = false;
42 };
43
44 SagoSpriteHolder::SagoSpriteHolder(const SagoDataHolder& texHolder) {
45 data = new SagoSpriteHolderData();
46 try {
47 data->tex = &texHolder;
48 ReadSprites();
49 data->defaultSprite = new sago::SagoSprite(texHolder,"fallback", {0,0,64,64},1,100);
50 }
51 catch (...) {
52 delete data;
53 }
54 }
55
56 SagoSpriteHolder::~SagoSpriteHolder() {
57 delete data;
58 }
59
60 static int getDefaultValue(const rapidjson::Value& value, const char* name, int defaultValue) {
61 assert(value.IsObject());
62 const auto& t = value.GetObject().FindMember(name);
63 if (t != value.MemberEnd() && t->value.IsInt()) {
64 return t->value.GetInt();
65 }
66 return defaultValue;
67 }
68
69 static std::string getDefaultValue(const rapidjson::Value& value, const char* name, std::string defaultValue) {
70 assert(value.IsObject());
71 const auto& t = value.GetObject().FindMember(name);
72 if (t != value.MemberEnd() && t->value.IsString()) {
73 defaultValue = t->value.GetString();
74 }
75 return defaultValue;
76 }
77
78 void SagoSpriteHolder::ReadSpriteFile(const std::string& filename) {
79 std::string fullfile = "sprites/"+filename;
80 std::string content = sago::GetFileContent(fullfile.c_str());
81 rapidjson::Document document;
82 document.Parse(content.c_str());
83 if ( !document.IsObject() ) {
84 std::cerr << "Failed to parse: " << fullfile << "\n";
85 return;
86 }
87 for (auto& m : document.GetObject()) {
88 const std::string& spriteName = m.name.GetString();
89 if (!m.value.IsObject()) {
90 if (spriteName[0] != '_') {
91 std::cerr << "Invalid sprite: " << spriteName << "\n";
92 }
93 continue;
94 }
95 std::string textureName = getDefaultValue(m.value, "texture", "fallback");
96 int topx = getDefaultValue(m.value, "topx", 0);
97 int topy = getDefaultValue(m.value, "topy",0);
98 int height = getDefaultValue(m.value, "height",0);
99 int width = getDefaultValue(m.value, "width",0);
100 int number_of_frames = getDefaultValue(m.value, "number_of_frames",1);
101 int frame_time = getDefaultValue(m.value, "frame_time",1);
102 SDL_Rect origin = {};
103 origin.x = getDefaultValue(m.value, "originx",0);
104 origin.y = getDefaultValue(m.value, "originy",0);
105 if (number_of_frames < 1) {
106 number_of_frames = 1;
107 }
108 if (frame_time < 1) {
109 frame_time = 1;
110 }
111 std::shared_ptr<sago::SagoSprite> ptr(new SagoSprite(*(data->tex),textureName, {topx,topy,width,height},number_of_frames,frame_time));
112 ptr->SetOrigin(origin);
113 this->data->sprites[std::string(spriteName)] = ptr;
114 }
115 }
116
117 void SagoSpriteHolder::ReadSprites() {
118 std::vector<std::string> spritefiles = GetFileList("sprites");
119 for (std::string& item : spritefiles ) {
120 if (boost::algorithm::ends_with(item,".sprite")) {
121 if (data->verbose) {
122 std::cout << "Found " << item << "\n";
123 }
124 ReadSpriteFile(item);
125 }
126 else {
127 if (data->verbose) {
128 std::cout << "Ignoreing " << item << "\n";
129 }
130 }
131 }
132 }
133
134 void SagoSpriteHolder::ReadSprites(const std::vector<std::string>& extra_sprites) {
135 for (std::string item : extra_sprites) {
136 item+=".sprite";
137 ReadSpriteFile(item);
138 }
139 }
140
141 const sago::SagoSprite& SagoSpriteHolder::GetSprite(const std::string& spritename) const {
142 std::unordered_map<std::string,std::shared_ptr<sago::SagoSprite>>::const_iterator got = data->sprites.find (spritename);
143 if ( got == data->sprites.end() ) {
144 return *data->defaultSprite;
145 }
146 else {
147 return *(got->second);
148 }
149 }
150
151
152 const SagoDataHolder& SagoSpriteHolder::GetDataHolder() const {
153 return *data->tex;
154 }
155
156 }