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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
int Map::Init(std::string initfile, std::string mapfile)
{
std::ifstream mapinitfile;
// Set the colors
Black.r = 0;
Black.g = 0;
Black.b = 0;
// Set the mapfile location to equal the class's string
s_mapfile = mapfile;
// Attempt to open the map's initialzation file.
mapinitfile.open(initfile.c_str());
// If the the initialzation file is equal to NULL, output my message to
// the console, close the map, and return an error value.
if(mapinitfile == NULL)
{
std::cout << "Failure to load map init file.\n";
SDL_Delay(5000); // Stop the program for a bit so the user can see the message
// displayed
mapinitfile.close();
return 1;
}
Name = "";
// Now get initalization information from the text file.
mapinitfile >> Name;
mapinitfile >> MaxTiles;
mapinitfile >> MapW;
mapinitfile >> MapH;
std::cout << "\n\nLoaded values from map initialization file(debug):\n" << "========================\n";
std::cout << Name << "\n";
std::cout << MaxTiles << "\n";
std::cout << MapW << "\n";
std::cout << MapH << "\n";
std::cout << "========================\n\n";
MapW *= 32;
MapH *= 32;
MapW -= 32;
// Use the Draw class to load the Tileset image.
Tileset = Res.TileSet_IMG;
std::cout << "Attempting to load Tileset\n";
// If the Tileset fails to load, I output a message and return an error value
if(Tileset == NULL)
{
std::cout << "Tileset failed to load.\n";
return 1;
}
else
{
std::cout << "Tileset successfully loaded\n";
}
Font = TTF_OpenFont("Fonts/din-mediumalternate.ttf", 18);
window[0].x = 500 + 2;
window[0].y = 32 + 2;
window[0].h = 32 - 4;
window[0].w = 200 - 4;
window[1].x = 500 ;
window[1].y = 32;
window[1].h = 32;
window[1].w = 200;
nameDisplay = NULL;
nameDisplay = TTF_RenderText_Solid(Font, Name.c_str(), Black);
// If this function encounters no errors, return a
// successful value
return 0;
}
int Map::InitTiles()
{
std::ifstream map;
// Temporary variables to determine where tiles are spawned
int x = 0;
int y = 0;
// Attempt to open the map
map.open(s_mapfile.c_str());
// If the map doesn't open, display an error message and
// return an error value
if(map == NULL)
{
std::cout << "Failure to set tiles.\n";
return 1;
}
// A for loop to go through the tiles one by one
for(int t = 0; t < MaxTiles; t++)
{
// Make a offset for the tile types
int tiletype = -1;
map >> tiletype;
// If the map fails to get the tile id
if(map.fail() == true)
{
// Display this message
std::cout << "Map failed to load.\n";
// Close the map file, then return an error value.
map.close();
return 1;
}
// If the tile type is a valid number, go on to work with them
if((tiletype >= 0 ) && ( tiletype < TILETYPES))
{
// Add a tile to the vector at the coordinates of x, and y, then with the tiletype
// loaded from the map.
Tiles.push_back(new Tile(x, y, tiletype));
}
// If it isn't a valid number close the map immediately and return an error
else
{
map.close();
return 1;
}
// Add the value of x by 1 tile pixel width each time a tile is loaded
x += TILE_W;
// If the x value passes the level width we set x to 0 and
// make the y value move on tile height down.
if(x > MapW)
{
x = 0;
y += TILE_H;
}
}
std::cout << "Tiles successfully initialized\n";
map.close();
return 0;
}
|