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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <fstream>
#include <vector>
#include <limits>
#include <string>
#include <stdio.h>
#include <windows.h>
#include "connector.hpp"
using namespace std;
using namespace sf;
//Declarations
//Global variables
const int scl = 16; //the images will be placed vector2f(image[][].x * scl, image[][].y * scl)
Sprite Bomberman;
Sprite Bomb;
Texture Entities;
Texture Barrier;
//Classes
//Structures
struct maps{
int width;
int height;
// Texture Other;
vector<vector<Sprite> > all;
vector<vector<bool> > canGo;
maps(int width_, int height_) : all(width_ * height_) {width = width_; height = height_;}
};
//Functions
std::string getexepath()
{
char result[ MAX_PATH ];
GetModuleFileName( NULL, result, MAX_PATH );
return string(result, 1);
}
ifstream& GotoLine(std::ifstream& file, unsigned int num){
file.seekg(std::ios::beg);
for(unsigned int i=0; i < num - 1; ++i){
file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
return file;
}
maps getMapData(string fileName){
const string DRIVE_LETTER = getexepath();
////////////////////////////////
///Line 1 = comment
///Line 2 = comment
///Line 3 = int width
///Line 4 = comment
///Line 5 = int height
///Line 6 = Dirt and wall images
///Line 7 = Entities images
///Line 8 = Dirt array
///Line 9 = Entities array
///Line 10 = Walls array
///////////////////////////////
string path = (DRIVE_LETTER + ":/C++ Projects/Bomberman/DATA/mapData/" + fileName);
//////////////////////////////
ifstream mapFl(path.c_str());
if (!mapFl){
cout << "ERR_FILE.NOTFOUND" << endl;
cout << path << endl;
}
string readline = " ";
GotoLine(mapFl, 3);
mapFl >> readline;
//this is 22 for this case
const int width_ = atoi(readline.c_str());
GotoLine(mapFl, 5);
mapFl >> readline;
//this is 20 for this case
const int height_ = atoi(readline.c_str());
maps rtrn(width_, height_);
cout << "1" << endl << width_ << endl << height_ << endl << endl;
GotoLine(mapFl, 6);
mapFl >> readline;
string idlePath = readline;
GotoLine(mapFl, 7);
mapFl >> readline;
string ePath = readline;
GotoLine(mapFl, 8);
mapFl >> readline;
string dirtAr = readline;
GotoLine(mapFl, 9);
mapFl >> readline;
string entityAr = readline;
GotoLine(mapFl, 10);
mapFl >> readline;
string wallAr = readline;
////////////////////////////////
mapFl.close();
path = (DRIVE_LETTER + ":/C++ Projects/Bomberman" + idlePath);
if(!Barrier.loadFromFile(path)){
cout << "ERROR_CANNOT_OPEN_FILE:" << idlePath;
}
path = (DRIVE_LETTER + ":/C++ Projects/Bomberman" + ePath);
if(!Entities.loadFromFile(path)){
cout << "ERROR_CANNOT_OPEN_FILE:" << ePath;
}
path = (DRIVE_LETTER + ":/C++ Projects/Bomberman" + dirtAr);
mapFl.open(path.c_str());
vector<vector<char> > result(width_, vector<char>(height_));
char arrayResult = 0;
for(int j = 0; j != height_; j++){
for(int i = 0; i != width_; i++){
if(!mapFl.good()){
cout << "ERROR WITH FILE" << endl;
break;
}
arrayResult = mapFl.get();
(result[i])[j] = arrayResult;
// cout << (result[i])[j];
}
}
for (unsigned int i = 0; i < rtrn.canGo.size(); ++i) {
for (unsigned int j = 0; j < rtrn.canGo[i].size(); ++j) {
bool temp = false;
if(result[i][j] == 1){
temp = true;
}
rtrn.canGo[i][j] = temp;
}
}
//cout << "ERR";
// int i = 0;
// int j = 0;
// for (auto&& v : rtrn.all) {
// for (auto&& sprite : v) {
// sprite.setTexture(Barrier);
// sprite.setPosition(Vector2f(i * scl, j * scl));
// sprite.setTextureRect(getRect(false, true, false, 1, 0));
// j++;
// }
// i++;
// }
// for (unsigned int i = 0; i != width_; i++) {
// for(unsigned int j = 0; j != height_; j++)
// rtrn.all[i][j].setTexture(Barrier);
// rtrn.all[i][j].setPosition(Vector2f(i * scl, j * scl));
// rtrn.all[i][j].setTextureRect(getRect(false, true, false, 1, 0));
// // cout << endl << "K" << endl << int(index / width_) * scl << endl << (index - (int((index) / width_) * width_)) * scl << endl;
// }
return rtrn;
}
//More Global variables
//Main
int main()
{
maps LM_MAIN = getMapData("Lvl1.map");
int i = 0;
int j = 0;
for (auto&& v : LM_MAIN.all) {
for (auto&& sprite : v) {
sprite.setTexture(Barrier);
sprite.setPosition(Vector2f(i * scl, j * scl));
sprite.setTextureRect(getRect(false, true, false, 1, 0));
j++;
}
i++;
}
// cout << LM_MAIN.width << endl << LM_MAIN.height << endl;
RenderWindow window(sf::VideoMode(LM_MAIN.width * scl, (LM_MAIN.height * scl) + 20), "Game!");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//cout << "HERE" << endl;
window.clear();
i = 0;
j = 0;
for (auto&& v : LM_MAIN.all) {
for (auto&& sprite : v) {
window.draw(sprite);
j++;
}
i++;
}
window.display();
}
}
|