main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <tilemap.h>
#include <vector>
#include <sstream>
#include <sstream>
#include <cctype>
#include <fstream>
#include <class.h>
using namespace std;
vector<vector<int> > colMap;
void LoadColMap(const char*filename)
{
ifstream openfile(filename);
vector<int> tempMap;
colMap.clear();
if(openfile.is_open())
{
while(!openfile.eof())
{
string str, value;
getline(openfile, str);
stringstream stream(str);
while(getline(stream, value, ' '))
{
if(value.length() > 0)
{
int a = atoi(value.c_str());
tempMap.push_back(a);
}
}
colMap.push_back(tempMap);
tempMap.clear();
}
}
}
int world() {
//character position
enum Direction{ Down, Left, Right, Up };
sf::Vector2i source(1, Down);
sf::RenderWindow window2(sf::VideoMode(640,640), "Game");
window2.setKeyRepeatEnabled(true);
Player player(sf::Vector2f(40, 40), sf::Vector2f(40, 40), sf::Color::Green);
player.rect.setFillColor(sf::Color::Green);
LoadColMap("lel.txt");
player.Update();
for(int i = 0; i < colMap.size(); i++)
{
for(int j = 0; j < colMap[i].size(); j++)
{
if(colMap[i][j] == 1)
{
int bottom, top, left, right;
bottom = i * 40 + 40;
top = i * 40;
right = j * 40 + 40;
left = j * 40;
if(player.right < left || player.left > right || player.top > bottom || player.bottom < top)
{
player.rect.setFillColor(sf::Color::Blue);
}
else
{
player.rect.setFillColor(sf::Color::Red);
break;
}
}
}
}
/*window2.clear(sf::Color(0, 240, 255));
for(int i = 0; i < map.size(); i++)
{
for(int j = 0; j < map[i].size(); j++)
{
if(map[i][j].x != -1 && map[i][j].y != -1)
{
tiles.setPosition(j * 40, i * 40);
tiles.setTextureRect(sf::IntRect(map[i][j].x * 32, map[i][j].y * 32, 32, 32));
window.draw(tiles);
}
}
}*/
const int level[] =
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1,
1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1,
1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1,
1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
/*for (int y = quad[0].position; y <= quad[1].position; ++y)
{
for (int x = quad[2].position; x <= quad[3].position; ++x)
{
// Entity is touching tile[x, y] so check and handle collision
}
} */
// create the tilemap from the level definition
TileMap map;
if (!map.load("tileset.png", sf::Vector2u(40, 40), level, 16, 16))
return -1;
sf::Texture char1texture;
if(!char1texture.loadFromFile("char1.jpg")) {
cout << "error" << endl;
}
sf::Sprite char1sprite;
char1sprite.setPosition(sf::Vector2f(40, 40));
char1sprite.setTexture(char1texture);
sf::Vector2f position = char1sprite.getPosition();
while(window2.isOpen()) {
sf::Event event;
while(window2.pollEvent (event)) {
switch(event.type) {
case sf::Event::Closed:
window2.close();
break;
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Up) {
cout << "Your character moved up" << endl;
source.y = Up;
player.rect.move(0, -40);
break; }
else if(event.key.code == sf::Keyboard::Down) {
cout << "Your character moved down" << endl;
source.y = Up;
player.rect.move(0, 40);
break;
}
else if(event.key.code == sf::Keyboard::Right) {
cout << "Your character moved right" << endl;
source.y = Up;
player.rect.move(40, 0);
break;
}
else if(event.key.code == sf::Keyboard::Left) {
cout << "Your character moved left" << endl;
source.y = Up;
player.rect.move(-40, 0);
break;
}
default: {
break;
}
}
}
window2.clear(sf::Color::Black);
window2.draw(map);
window2.draw(player.rect);
window2.display();
}
}
int main() {
sf::RenderWindow window(sf::VideoMode(800,600), "Game");
sf::Texture bgtexture;
if(!bgtexture.loadFromFile("bgtexture.jpg")) {
cout << "error" << endl;
}
sf::Sprite bgsprite;
bgsprite.setTexture(bgtexture);
sf::Texture sgtexture;
if(!sgtexture.loadFromFile("sgtexture.jpg")) {
cout << "error" << endl;
}
sf::Sprite sgsprite;
sgsprite.setTexture(sgtexture);
sgsprite.setPosition(250, 150);
sf::Texture sgtexture2;
if(!sgtexture2.loadFromFile("sgtexture2.jpg")) {
cout << "error" << endl;
}
sf::Sprite sgsprite2;
sgsprite2.setTexture(sgtexture2);
sgsprite2.setPosition(250, 250);
while(window.isOpen()) {
sf::Event event;
while(window.pollEvent (event)) {
switch(event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Left)
{
if(sgsprite2.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y)) {
window.close();
break;
}
else if(sgsprite.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y)) {
window.close();
world();
break;
}
}
default: {
break;
}
}
}
window.clear(sf::Color::Black);
window.draw(bgsprite);
window.draw(sgsprite);
window.draw(sgsprite2);
window.display();
}
return 0;
}