Wow... It should need <string>, but was compiling without it... well - here is the constants.h for acedemic practices.
Note: Code::Blocks has been flaking out on me a lot lately. Not usually things like this though... how the heck was it compiling. I'm always split between the nitty-gritty world of vim and hand-made makefiles, and the conversely shiny allure of neat IDE's Additional notes: The World class was pulled out of another project to work on independently. The implementation file includes <string>. I'll include the implementation file also for academic potential. Don't laugh.
constants.h
Code:
#ifndef CONSTANTS_H_INCLUDED
#define CONSTANTS_H_INCLUDED
const bool MUSIC = false;
const bool DEBUG = false;
const bool SHOWFPS = true;
// Global System Constants
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
const int SCREEN_BPP = 32;
const char GAME_TITLE[] = "Sellsword";
// Global Game Constants
const int TILE_WIDTH = 32;
const int TILE_HEIGHT = 32;
const int TILE_COLS_X_W = SCREEN_WIDTH / TILE_WIDTH;
const int TILE_ROWS_Y_H = SCREEN_HEIGHT / TILE_HEIGHT;
const int TILE_SHEET_COLS = 16;
const int TILE_SHEET_ROWS = 12;
const double CHARACTER_SPEED = 200.0;
// Enumerations
enum {SOUTH, WEST, EAST, NORTH};
enum {RIGHT_FOOT, MIDDLE_FOOT, LEFT_FOOT};
enum {MENU_CHOICE_NEW, MENU_CHOICE_LOAD, MENU_CHOICE_QUIT};
// GameState
enum {INITIALIZING, OPENING, MENU, PLAYING, LOADING, QUITTING, OPTIONS};
// GameStatePlay
enum {WALKING, SAILING, FLYING, PAUSE, RESTING, COMBAT, TALKING, QSAVE, QLOAD, OPTS, CUTSCENE, QUIT, EDITMODE, HELPMODE, SCRNSHOT};
#endif // CONSTANTS_H_INCLUDED
world.cpp
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <dirent.h>
#include <cstring>
#include <cstdlib>
#include <vector>
#include "world.h"
using namespace std;
void World::Load(int ID)
{
// Read all valid zone (*.zon) files in the maps directory.
// Load the zones ending in the AreaID we want.
// This function uses a logic waterfall to sanity check
// the file names against a number of criteria.
AreaID = ID;
// Read the Zones from disk.
LoadZones();
// Check that 9 usable zones were loaded, lest we create them.
if (VecZoneFileNames.size() < 9)
{
VecZoneFileNames.clear(); // Clear potential damaged or partial zones.
int w = SCREEN_WIDTH;
int h = SCREEN_HEIGHT;
int cx = w/2;
int cy = h/2;
CreateZone(cx, cy); // Center
CreateZone(cx, cy-h); // North
CreateZone(cx+w, cy-h); // Northeast
CreateZone(cx+w, cy); // East
CreateZone(cx+w, cy+h); // Southeast
CreateZone(cx, cy+h); // South
CreateZone(cx-w, cy+h); // Southwest
CreateZone(cx-w, cy); // West
CreateZone(cx-w, cy-h); // Northwest
// Read the Zones from disk.
LoadZones();
if (VecZoneFileNames.size() < 9) throw("World::Load: Zone Read error.");
}
}
void Update(float x, float y)
//void World::Update(const std::string &ZFP, std::vector<sf::Sprite> &vTS)
{
// ifstream ZoneFile(ZFP.c_str());
// string input;
// stringstream ss;
// vector<string> vInput;
// ZoneMap.clear();
// while(getline(ZoneFile, input))
// {
// // Skip comments
// if (input[0] != '#')
// {
// vInput.clear();
// // Read in and tokenize line
// split(input, ':', vInput);
// // convert to ints
// //cout << "Debug: vInput size: " << vInput.size() << endl;
// unsigned int i = 0;
// while (i < vInput.size())
// {
// int x = atoi(vInput[i].c_str());
// int y = atoi(vInput[i+1].c_str());
// int tileID = atoi(vInput[i+2].c_str());
// i += 3;
//
// Tile NewTile(vTS[tileID], x, y);
// //if (DEBUG) cout << "World Loader Data Sample: NewTile.GetPos().y: " << NewTile.GetPos().y << endl;
// // push Tiles into [RowMap]
// RowMap.push_back(NewTile);
// }
// ZoneMap.push_back(RowMap);
// }
// }
// ZoneFile.close();
}
void World::Save()
{
// //Check the ZoneLookUp table
// // Save the 8 areas there
//
// CurrentArea
// string ZFP = GenerateZonePath(InteriorPoint);
//
// MapVectorZone TZM;
// //TZM = LocateZoneMap(TargetZonePath);
// TZM = GetZone(); // Place holder
//
// float Time = WorldClock.GetElapsedTime();
// cout << "Saving: " << ZFP << endl;
// ofstream outfile(ZFP.c_str());
//
// if (!outfile) throw("World::SaveZone: Couldn't open zone file for saving!");
// outfile << "# Area Label \"" << string(ZFP).substr(5, 11) << "\"." << endl;
// outfile << "# System generated zone file: " << ZFP << endl;
// outfile << "# Version 1.2" << endl;
//
// for (int y = 0; y < TILE_ROWS_Y_H; ++y)
// {
// for(int x = 0; x < TILE_COLS_X_W; ++x)
// {
// int t = TZM.at(y).at(x).GetID();
// outfile << (y < 100 ? "0" : "") << (y < 10 ? "0" : "") << y << ":"
// << (x < 100 ? "0" : "") << (x < 10 ? "0" : "") << x << ":"
// << (t < 100 ? "0" : "") << (t < 10 ? "0" : "") << t;
// if (x < TILE_COLS_X_W - 1) outfile << ": ";
// }
// outfile << endl;
// cout << "[]"; // Update status indicator
// }
//
// outfile.close();
// cout << " Complete!" << endl;
// cout << ZFP << " saved in " << (WorldClock.GetElapsedTime() - Time) << " secs."<< endl;
//
}
void World::CreateZone(float x, float y)
{
string ZoneFileName = GenerateZoneFilePath(x, y);
/*DEBUG*/cout << "Creating: " << ZoneFileName;
ofstream outfile;
outfile.open(ZoneFileName.c_str());
if (!outfile) throw("World::CreatZone: Couldn't open zone file for creation!");
outfile << "# Area Label \"" << string(ZoneFileName).substr(5, 11) << "\"." << endl;
outfile << "# System generated zone file: " << ZoneFileName << endl;
outfile << "# Version 1.4" << endl;
// Text Files must be written left to right
for (int y = 0; y < TILE_ROWS_Y_H; ++y)
{
for(int x = 0; x < TILE_COLS_X_W; ++x)
{
// Write the ID for tile (1,0) and Walkable Type (000)
outfile << "001" << ":" << "000";
if (x < TILE_COLS_X_W - 1) outfile << ": ";
}
outfile << endl;
}
outfile.close();
cout << " Complete!" << endl;
}
// Helper for Loader
std::string World::GenerateZoneFilePath(float x, float y)
{
char SignX = x > -1 ? 'P' : 'N';
char SignY = y > -1 ? 'P' : 'N';
int coord_x = abs(static_cast<int>(x) / SCREEN_WIDTH);
int coord_y = abs(static_cast<int>(y) / SCREEN_HEIGHT);
stringstream ZoneName;
ZoneName << "maps/";
ZoneName << SignX << (coord_x < 100 ? "0" : "") << (coord_x < 10 ? "0" : "") << coord_x;
ZoneName << SignY << (coord_y < 100 ? "0" : "") << (coord_y < 10 ? "0" : "") << coord_y;
ZoneName << "A" << (AreaID < 100 ? "0" : "") << (AreaID < 10 ? "0" : "") << AreaID << ".zon";
/*DEBUG*/ cout << "World::GenerateZoneFilePath(" << x << "," << y << "):" << endl;
cout << "\"" << ZoneName.str() << "\"" << endl;
return ZoneName.str();
}
std::vector<std::string> &World::split(const std::string &s, char delim, std::vector<std::string> &elems)
{
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
void World::LoadZones()
{
DIR *DirPtr;
struct dirent *DirEntry;
if((DirPtr = opendir("maps")))
{
while ((DirEntry = readdir(DirPtr)))
{
string CurrFile(DirEntry->d_name);
if(CurrFile.length() == 16)
if(CurrFile[8] == 'A')
if(atoi(string(CurrFile.substr(9, 3)).c_str()) == AreaID)
if(CurrFile[0] == 'P' || CurrFile[0] == 'N')
if(CurrFile[4] == 'P' || CurrFile[4] == 'N')
if(CurrFile.compare(12, 4, ".zon") == 0)
VecZoneFileNames.push_back(CurrFile.substr(0, 12));
}
closedir(DirPtr);
}
else throw("[World::Load]: No maps directory found. (ex: ./maps/N000P000A001.zon)");
}