Created
February 8, 2015 20:26
-
-
Save prewk/0c2b879a5a06a9d182dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------------- | |
// scripts/breeds.json (This is okay, I've used it in another context and its interpreted correctly) | |
// ----------------- | |
{ | |
"Warrior": { | |
"sheet": "resources/Fighter-F-01.png", | |
"stats": { | |
"health": 100, | |
"strength": 50, | |
"intelligence": 10 | |
}, | |
"sprites": [ | |
{ | |
"name": "STANDING_NORTH", | |
"frames": [ | |
{ "x": 28, "y": 5, "width": 16, "height": 27 } | |
] | |
}, | |
{ | |
"name": "WALKING_NORTH", | |
"frames": [ | |
{ "x": 4, "y": 5, "width": 16, "height": 27 }, | |
{ "x": 28, "y": 5, "width": 16, "height": 27 }, | |
{ "x": 52, "y": 5, "width": 16, "height": 27 }, | |
{ "x": 28, "y": 5, "width": 16, "height": 27 } | |
] | |
} | |
] | |
} | |
} | |
// ----------------- | |
// BREED | |
// ----------------- | |
#ifndef BREED_HPP | |
#define BREED_HPP | |
#include <string> | |
#include <map> | |
#include "SpriteSheet.hpp" | |
struct ActorStats { | |
int health; | |
int strength; | |
int intelligence; | |
}; | |
class Breed | |
{ | |
public: | |
~Breed() {}; | |
Breed(); | |
ActorStats getStats(); | |
SpriteSheet& getSpriteSheet(); | |
static Breed &getBreed(const std::string& breedName); | |
private: | |
Breed(ActorStats const& stats, SpriteSheet const& spriteSheet); | |
ActorStats stats; | |
SpriteSheet spriteSheet; | |
static std::map<std::string, Breed> breeds; | |
}; | |
#endif | |
#include "Breed.hpp" | |
#include <json/json.h> | |
#include <iostream> | |
#include <fstream> | |
Breed::Breed() | |
: stats() | |
, spriteSheet() | |
{} | |
Breed::Breed(ActorStats const& stats, SpriteSheet const& spriteSheet) | |
: stats(stats) | |
, spriteSheet(spriteSheet) | |
{} | |
ActorStats Breed::getStats() { | |
return stats; | |
} | |
std::map<std::string, Breed> Breed::breeds = std::map<std::string, Breed>(); | |
Breed& Breed::getBreed(const std::string &breedName) { | |
if (breeds.count(breedName)) { | |
return breeds[breedName]; | |
} | |
std::ifstream breedJson("scripts/breeds.json", std::ifstream::binary); | |
Json::Value root; | |
Json::Reader reader; | |
if (!reader.parse(breedJson, root)) { | |
std::cerr << "JSON Parse error: " << std::endl << reader.getFormattedErrorMessages() << std::endl; | |
} | |
Json::Value scope = root[breedName]; | |
SpriteSheet newSheet(scope["sheet"].asString()); | |
ActorStats newStats { | |
scope["stats"]["health"].asInt(), | |
scope["stats"]["strength"].asInt(), | |
scope["stats"]["intelligence"].asInt(), | |
}; | |
for (uint i = 0; i < scope["sprites"].size(); ++i) { | |
std::vector<sf::IntRect> frames; | |
for (uint ii = 0; ii < scope["sprites"][i]["frames"].size(); ++ii) { | |
frames.push_back(sf::IntRect { | |
scope["sprites"][i]["frames"][ii]["x"].asInt(), | |
scope["sprites"][i]["frames"][ii]["y"].asInt(), | |
scope["sprites"][i]["frames"][ii]["width"].asInt(), | |
scope["sprites"][i]["frames"][ii]["height"].asInt() | |
}); | |
} | |
newSheet.defineSprite(scope["sprites"][i]["name"].asString(), frames); | |
} | |
breeds[breedName] = Breed(newStats, newSheet); | |
return breeds[breedName]; | |
} | |
SpriteSheet &Breed::getSpriteSheet() { | |
return spriteSheet; | |
} | |
// ----------------- | |
// SPRITE SHEET | |
// ----------------- | |
#ifndef SPRITE_SHEET_HPP | |
#define SPRITE_SHEET_HPP | |
#include <SFML/Graphics.hpp> | |
#include <string> | |
#include <map> | |
class SpriteSheet | |
{ | |
public: | |
SpriteSheet(); | |
SpriteSheet(const std::string& filename); | |
void defineSprite(const std::string& name, const std::vector<sf::IntRect>& frames); | |
const sf::Sprite& getFrame(const std::string& name, int frame); | |
unsigned getFrameCount(const std::string& name); | |
sf::Texture texture; | |
private: | |
std::map<std::string, std::vector<sf::Sprite>> sprites; | |
}; | |
#endif | |
#include "SpriteSheet.hpp" | |
#include <iostream> | |
SpriteSheet::SpriteSheet() {} | |
SpriteSheet::SpriteSheet(const std::string &filename) | |
: sprites{} | |
{ | |
if (!texture.loadFromFile(filename)) { | |
std::cerr << "Couldn't load " << filename << std::endl; | |
} | |
} | |
void SpriteSheet::defineSprite(const std::string& name, const std::vector<sf::IntRect>& frames) { | |
std::vector<sf::Sprite> namedFrames(frames.size()); | |
for (auto& i : frames) { | |
namedFrames.push_back(sf::Sprite(texture, i)); | |
} | |
sprites[name] = namedFrames; | |
} | |
const sf::Sprite& SpriteSheet::getFrame(const std::string &name, int frame) { | |
return sprites[name][frame]; | |
} | |
unsigned SpriteSheet::getFrameCount(const std::string &name) { | |
return static_cast<unsigned>(sprites[name].size()); | |
} | |
// ----------------- | |
// MAIN | |
// ----------------- | |
#include "Breed.hpp" | |
#include "SpriteSheet.hpp" | |
#include <SFML/Graphics.hpp> | |
int main() { | |
sf::RenderWindow window; | |
window.create(sf::VideoMode(800, 600), "Hej"); | |
Breed warrior = Breed::getBreed("Warrior"); | |
sf::Sprite frame = warrior.getSpriteSheet().getFrame("WALKING_NORTH", 0); | |
bool run = true; | |
while (run) { | |
sf::Event event; | |
while (window.pollEvent(event)) { | |
if (event.type == sf::Event::Closed) { | |
run = false; | |
} | |
window.clear(sf::Color::White); | |
// This doesn't work, accessing one of the frames | |
frame.setPosition(300, 0); | |
window.draw(frame); | |
// This works, accessing texture directly: | |
window.draw(sf::Sprite(warrior.getSpriteSheet().texture)); | |
window.display(); | |
} | |
} | |
window.close(); | |
return 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment