#include <iostream>
#include <fstream>
#include <SFML/graphics.hpp>
#include <SFML/window.hpp>
#define ScreenWidth 800
#define ScreenHeight 600
#define BLOCKSIZE 40
using namespace std;
int loadCounterX = 0, loadCounterY = 0;
int mapSizeX, mapSizeY;
int MapFile[100][100];
void LoadMap(const char *filename)
{
ifstream openfile(filename);
if(openfile.is_open())
{
openfile >> mapSizeX >> mapSizeY;
while(!openfile.eof())
{
openfile >> MapFile[loadCounterX][loadCounterY];
loadCounterX++;
if(loadCounterX >= mapSizeX)
{
loadCounterX = 0;
loadCounterY++;
}
}
}
}
void DrawMap(sf::RenderWindow &window)
{
sf::Shape rect = sf::Shape::Rectangle(0, 0, BLOCKSIZE, BLOCKSIZE, sf::Color(255,255,255,255));
sf::Color rectCol;
for(int i = 0; i < mapSizeX; i++)
{
for(int j = 0; j < mapSizeY; j++)
{
if(MapFile[i][j] == 0)
rectCol = sf::Color(44, 117, 255);
if (MapFile[i][j] == 1)
rectCol = sf::Color(255, 55, 5);
rect.SetPosition(i * BLOCKSIZE, j * BLOCKSIZE);
rect.SetColor(rectCol);
window.Draw(rect);
}
}
}
int main()
{
sf::RenderWindow window(sf::VideoMode(ScreenWidth, ScreenHeight, 32),"SFML tile mapping");
LoadMap("or.txt");
while(window.IsOpened())
{
sf::Event Event;
while(window.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
window.Close();
}
window.Clear();
DrawMap(window);
window.Display();
}
}