1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <SDL2/SDL_render.h>
#include <string>
#include <vector>
#include <limits>
#include <istream>
#include <ostream>
#include <fstream>
enum MenuState { MS_Unknown }; // I added this, dunno what it ought to be
struct Slot
{
std::string position;
SDL_Rect Rect = { 0, 0, 0, 0 };
std::string description;
};
std::ostream& saveOn(std::ostream& os, const Slot& n);
std::istream& restoreFrom(std::istream& is, Slot& n);
struct Inventory
{
int inventoryPage;
std::string inventoryTexturePath;
SDL_Texture *Texture = nullptr;
SDL_Rect renderCoordinates = { 0, 0, 640, 640 };
std::vector<Slot> slots;
SDL_Rect currencyPosition = { 436, 501 }; // expecting 4 fields
MenuState StateOfMenu = MS_Unknown;
};
std::ostream& saveOn(std::ostream& os, const Inventory& n);
std::istream& restoreFrom(std::istream& is, Inventory& n);
//---------------------------------------------------------------------------
//
std::ostream& saveOn(std::ostream& os, const Slot& n)
{
// I don't know what the file format is
return os;
}
std::istream& restoreFrom(std::istream& is, Slot& n)
{
// using your code ...
std::string line;
std::getline(is, line, ':'); is >> n.position;
is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(is, line, ':');
std::getline(is, line, ':'); is >> n.Rect.x;
std::getline(is, line, ':'); is >> n.Rect.y;
std::getline(is, line, ':'); is >> n.Rect.h;
std::getline(is, line, ':'); is >> n.Rect.w;
std::getline(is, line, ':'); is >> n.description;
return is;
}
std::ostream& saveOn(std::ostream& os, const Inventory& n)
{
// I don't know what the file format is
return os;
}
std::istream& restoreFrom(std::istream& is, Inventory& n)
{
// again, using your code ...
std::string line;
std::getline(is, line, '#'); is >> line;
int inventoryPage = atoi(line.c_str());
if (inventoryPage == -1) {
is.setstate(std::ios_base::badbit);
return is;
}
n.inventoryPage = inventoryPage;
std::getline(is, line, ':'); is >> n.inventoryTexturePath;
// n.Texture = TextureManager::loadTexture(n.inventoryTexturePath.c_str());
size_t nSlots;
std::getline(is, line, '#'); is >> nSlots;
n.slots.resize(nSlots);
for (size_t i = 0; i != nSlots; ++i)
if (!restoreFrom(is, n.slots[i])) {
n.slots.resize(i);
break;
}
return is;
}
//---------------------------------------------------------------------------
//
typedef std::vector<Inventory> InventoryEntriesType;
InventoryEntriesType loadInventoryData(const std::string& filename);
InventoryEntriesType loadInventoryData(std::istream& is);
InventoryEntriesType loadInventoryData(const std::string& filename)
{
std::ifstream is(filename);
return loadInventoryData(is);
}
InventoryEntriesType loadInventoryData(std::istream& is)
{
InventoryEntriesType inventories;
do {
Inventory inventory;
if (restoreFrom(is, inventory))
inventories.push_back(inventory);
}
while (is);
return inventories;
}
int main(int argc, char* argv[])
{
if (argc != 2)
return 1;
loadInventoryData(argv[1]);
}
|