Created
May 31, 2011 03:51
-
-
Save joseph-montanez/999830 to your computer and use it in GitHub Desktop.
Read and Write Mapping
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
#include <SFML/Graphics.hpp> | |
#include <SFML/System.hpp> | |
#include <iostream> | |
#include <vector> | |
#include <stdint.h> | |
#include <cstring> | |
#include <string> | |
#include <fstream> | |
typedef struct mapformat { | |
uint16_t map[4][4]; | |
} mapformat; | |
void WriteMap(const std::string& filename, std::vector< std::vector<int> > map) | |
{ | |
mapformat format; | |
for (int x = 0; x < map.size (); x++) { | |
for (int y = 0; y < map[x].size (); y++) { | |
int tileId = map[x][y]; | |
format.map[x][y] = tileId; | |
} | |
} | |
std::ofstream writer(filename.c_str(), std::ios::out | std::ios::binary); | |
writer.write((char *) &format, sizeof(mapformat)); | |
writer.close(); | |
} | |
void ReadMap(const std::string& filename, std::vector< std::vector<int> >& map) | |
{ | |
mapformat format; | |
std::ifstream reader(filename.c_str(), std::ios::in | std::ios::binary); | |
reader.read((char *) &format, sizeof(mapformat)); | |
for (int x = 0; x < map.size (); x++) { | |
for (int y = 0; y < map[x].size (); y++) { | |
map[x][y] = format.map[x][y]; | |
} | |
} | |
} | |
int main() | |
{ | |
std::vector< std::vector<int> > map(4, std::vector<int>(4)); | |
// Define the map | |
map[0][0] = 0; | |
map[0][1] = 1; | |
map[0][2] = 0; | |
map[0][3] = 0; | |
map[1][0] = 0; | |
map[1][1] = 0; | |
map[1][2] = 0; | |
map[1][3] = 0; | |
map[2][0] = 0; | |
map[2][1] = 0; | |
map[2][2] = 0; | |
map[2][3] = 0; | |
map[3][0] = 0; | |
map[3][1] = 0; | |
map[3][2] = 0; | |
map[3][3] = 1; | |
WriteMap("test.data", map); | |
// Read the map from file! | |
ReadMap("test.data", map); | |
// Create the main rendering window | |
sf::RenderWindow App(sf::VideoMode(640, 480, 32), "SFML TileMap"); | |
// Load the sprite image from a file | |
std::vector<sf::Image> images(2); | |
if (!images[0].LoadFromFile("grass.png")) { | |
return EXIT_FAILURE; | |
} | |
if (!images[1].LoadFromFile("water.png")) { | |
return EXIT_FAILURE; | |
} | |
// Create the sprite | |
std::vector<sf::Sprite> tiles(2); | |
tiles[0] = sf::Sprite(images[0]); // Grass | |
tiles[1] = sf::Sprite(images[1]); // Water | |
// Start game loop | |
while (App.IsOpened()) | |
{ | |
// Process events | |
sf::Event Event; | |
while (App.GetEvent(Event)) | |
{ | |
// Close window : exit | |
if (Event.Type == sf::Event::Closed) | |
App.Close(); | |
} | |
// Get elapsed time | |
float ElapsedTime = App.GetFrameTime(); | |
// Clear screen | |
App.Clear(); | |
for (int x = 0; x < map.size (); x++) { | |
for (int y = 0; y < map[x].size (); y++) { | |
int tileId = map[x][y]; | |
// Get the tile's image | |
const sf::Image* image = tiles[tileId].GetImage(); | |
// Get the width and height of the image | |
int width = image->GetWidth(); | |
int height = image->GetHeight(); | |
// Adjust the offset by using the width | |
tiles[tileId].SetPosition(x * width, y * height); | |
// Draw the tile | |
App.Draw(tiles[tileId]); | |
} | |
} | |
// Display window contents on screen | |
App.Display(); | |
sf::Sleep(1.0f / 60.0f); | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment