git repos / blockattack-game

blame: source/code/editor/editor_loader.cpp

normal view · raw

1828235c Poul Sander 2025-08-10 10:46 1
/*
1828235c Poul Sander 2025-08-10 10:46 2
===========================================================================
1828235c Poul Sander 2025-08-10 10:46 3
 * Sago Multi Scrambler Puzzle
1828235c Poul Sander 2025-08-10 10:46 4
Copyright (C) 2022-2024 Poul Sander
1828235c Poul Sander 2025-08-10 10:46 5
1828235c Poul Sander 2025-08-10 10:46 6
This program is free software: you can redistribute it and/or modify
1828235c Poul Sander 2025-08-10 10:46 7
it under the terms of the GNU General Public License as published by
1828235c Poul Sander 2025-08-10 10:46 8
the Free Software Foundation, either version 2 of the License, or
1828235c Poul Sander 2025-08-10 10:46 9
(at your option) any later version.
1828235c Poul Sander 2025-08-10 10:46 10
1828235c Poul Sander 2025-08-10 10:46 11
This program is distributed in the hope that it will be useful,
1828235c Poul Sander 2025-08-10 10:46 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
1828235c Poul Sander 2025-08-10 10:46 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1828235c Poul Sander 2025-08-10 10:46 14
GNU General Public License for more details.
1828235c Poul Sander 2025-08-10 10:46 15
1828235c Poul Sander 2025-08-10 10:46 16
You should have received a copy of the GNU General Public License
1828235c Poul Sander 2025-08-10 10:46 17
along with this program.  If not, see http://www.gnu.org/licenses/
1828235c Poul Sander 2025-08-10 10:46 18
1828235c Poul Sander 2025-08-10 10:46 19
Source information and contacts persons can be found at
1828235c Poul Sander 2025-08-10 10:46 20
https://github.com/sago007/saland
1828235c Poul Sander 2025-08-10 10:46 21
===========================================================================
1828235c Poul Sander 2025-08-10 10:46 22
*/
1828235c Poul Sander 2025-08-10 10:46 23
1828235c Poul Sander 2025-08-10 10:46 24
#include "editor_loader.hpp"
1828235c Poul Sander 2025-08-10 10:46 25
#include "../sago/SagoMisc.hpp"
1828235c Poul Sander 2025-08-10 10:46 26
#include <boost/algorithm/string/predicate.hpp>
1828235c Poul Sander 2025-08-10 10:46 27
#include <iostream>
1828235c Poul Sander 2025-08-10 10:46 28
1828235c Poul Sander 2025-08-10 10:46 29
1828235c Poul Sander 2025-08-10 10:46 30
void to_json(nlohmann::json& j, const SagoSprite& p) {
1828235c Poul Sander 2025-08-10 10:46 31
	j = nlohmann::json{{"texture", p.texture}, {"topx", p.topx}, {"topy", p.topy}, {"height", p.height}, {"width", p.width}, {"number_of_frames", p.number_of_frames}, {"frame_time", p.frame_time}, {"originx", p.originx}, {"originy", p.originy}};
1828235c Poul Sander 2025-08-10 10:46 32
}
1828235c Poul Sander 2025-08-10 10:46 33
1828235c Poul Sander 2025-08-10 10:46 34
void from_json(const nlohmann::json& j, SagoSprite& p) {
1828235c Poul Sander 2025-08-10 10:46 35
	j.at("texture").get_to(p.texture);
1828235c Poul Sander 2025-08-10 10:46 36
	j.at("topx").get_to(p.topx);
1828235c Poul Sander 2025-08-10 10:46 37
	j.at("topy").get_to(p.topy);
1828235c Poul Sander 2025-08-10 10:46 38
	j.at("height").get_to(p.height);
1828235c Poul Sander 2025-08-10 10:46 39
	j.at("width").get_to(p.width);
1828235c Poul Sander 2025-08-10 10:46 40
	if (j.contains("number_of_frames")) {
1828235c Poul Sander 2025-08-10 10:46 41
		j.at("number_of_frames").get_to(p.number_of_frames);
1828235c Poul Sander 2025-08-10 10:46 42
	}
1828235c Poul Sander 2025-08-10 10:46 43
	if (j.contains("frame_time")) {
1828235c Poul Sander 2025-08-10 10:46 44
		j.at("frame_time").get_to(p.frame_time);
1828235c Poul Sander 2025-08-10 10:46 45
	}
1828235c Poul Sander 2025-08-10 10:46 46
	if(j.contains("originx")) {
1828235c Poul Sander 2025-08-10 10:46 47
		j.at("originx").get_to(p.originx);
1828235c Poul Sander 2025-08-10 10:46 48
	}
1828235c Poul Sander 2025-08-10 10:46 49
	if(j.contains("originy")) {
1828235c Poul Sander 2025-08-10 10:46 50
		j.at("originy").get_to(p.originy);
1828235c Poul Sander 2025-08-10 10:46 51
	}
1828235c Poul Sander 2025-08-10 10:46 52
}
1828235c Poul Sander 2025-08-10 10:46 53
1828235c Poul Sander 2025-08-10 10:46 54
void from_json(const nlohmann::json& j, SagoSpriteFile& p) {
1828235c Poul Sander 2025-08-10 10:46 55
	std::cout << "from_json\n";
1828235c Poul Sander 2025-08-10 10:46 56
	for (auto& [key, value] : j.items()) {
1828235c Poul Sander 2025-08-10 10:46 57
		SagoSprite s;
1828235c Poul Sander 2025-08-10 10:46 58
		if (!value.is_object()) {
1828235c Poul Sander 2025-08-10 10:46 59
			continue;
1828235c Poul Sander 2025-08-10 10:46 60
		}
1828235c Poul Sander 2025-08-10 10:46 61
		from_json(value, s);
1828235c Poul Sander 2025-08-10 10:46 62
		p.sprites[key] = s;
1828235c Poul Sander 2025-08-10 10:46 63
	}
1828235c Poul Sander 2025-08-10 10:46 64
}
1828235c Poul Sander 2025-08-10 10:46 65
1828235c Poul Sander 2025-08-10 10:46 66
void AppendFromFile(const std::string& json_content, const std::string& filename, std::map<std::string, SagoSprite>& sprites) {
1828235c Poul Sander 2025-08-10 10:46 67
	nlohmann::json j = nlohmann::json::parse(json_content);
1828235c Poul Sander 2025-08-10 10:46 68
	//std::map<std::string, SagoSprite> file;
1828235c Poul Sander 2025-08-10 10:46 69
	for (auto& sprite : j.items()) {
1828235c Poul Sander 2025-08-10 10:46 70
		std::cout << sprite.key() << "\n";
1828235c Poul Sander 2025-08-10 10:46 71
		if (!sprite.value().is_object()) {
1828235c Poul Sander 2025-08-10 10:46 72
			std::cout << "Not an object\n";
1828235c Poul Sander 2025-08-10 10:46 73
			continue;
1828235c Poul Sander 2025-08-10 10:46 74
		}
1828235c Poul Sander 2025-08-10 10:46 75
		std::string name = sprite.key();
1828235c Poul Sander 2025-08-10 10:46 76
		SagoSprite s = sprite.value();
1828235c Poul Sander 2025-08-10 10:46 77
		s.meta_from_file = filename;
1828235c Poul Sander 2025-08-10 10:46 78
		s.meta_name = name;
1828235c Poul Sander 2025-08-10 10:46 79
		sprites[name] = s;
1828235c Poul Sander 2025-08-10 10:46 80
	}
1828235c Poul Sander 2025-08-10 10:46 81
}
1828235c Poul Sander 2025-08-10 10:46 82
1828235c Poul Sander 2025-08-10 10:46 83
std::map<std::string, SagoSprite> LoadSprites() {
1828235c Poul Sander 2025-08-10 10:46 84
	std::map<std::string, SagoSprite> sprites;
1828235c Poul Sander 2025-08-10 10:46 85
	std::vector<std::string> spritefiles = sago::GetFileList("sprites");
1828235c Poul Sander 2025-08-10 10:46 86
	for (std::string& item : spritefiles  ) {
1828235c Poul Sander 2025-08-10 10:46 87
		if (boost::algorithm::ends_with(item,".sprite")) {
1828235c Poul Sander 2025-08-10 10:46 88
			std::string filename = "sprites/"+item;
1828235c Poul Sander 2025-08-10 10:46 89
			std::string content = sago::GetFileContent(filename.c_str());
1828235c Poul Sander 2025-08-10 10:46 90
			if (content.empty()) {
1828235c Poul Sander 2025-08-10 10:46 91
				std::cout << "File \"" << filename << "\" does not exit!\n";
1828235c Poul Sander 2025-08-10 10:46 92
				return sprites;
1828235c Poul Sander 2025-08-10 10:46 93
			}
1828235c Poul Sander 2025-08-10 10:46 94
			std::cout << "Reading " << filename << "\n";
1828235c Poul Sander 2025-08-10 10:46 95
			AppendFromFile(content, item, sprites);
1828235c Poul Sander 2025-08-10 10:46 96
		}
1828235c Poul Sander 2025-08-10 10:46 97
	}
1828235c Poul Sander 2025-08-10 10:46 98
	return sprites;
1828235c Poul Sander 2025-08-10 10:46 99
}