Advertisement
Guest User

Main.cpp

a guest
Jan 22nd, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include <SFML/Audio.hpp>
  4. #include <SFML/System.hpp>
  5. #include <SFML/Window.hpp>
  6. #include <fstream>
  7. #include <vector>
  8. #include <limits>
  9. #include <string>
  10. #include <stdio.h>
  11. #include <windows.h>
  12. #include "connector.hpp"
  13. using namespace std;
  14. using namespace sf;
  15.  
  16. //Declarations
  17.  
  18. //Global variables
  19. const int scl = 16;         //the images will be placed vector2f(image[][].x * scl, image[][].y * scl)
  20. Sprite Bomberman;
  21. Sprite Bomb;
  22. //Classes
  23.  
  24. //Structures
  25. struct maps{
  26.     int width;
  27.     int height;
  28.     Texture Entities;
  29.     Texture Wall;
  30.     Texture Barrier;
  31. //    Texture Other;
  32.     vector<vector<Sprite> > all;
  33.     vector<vector<bool> > canGo;
  34.  
  35. };
  36. //Functions
  37. std::string getexepath()
  38. {
  39.   char result[ MAX_PATH ];
  40.   GetModuleFileName( NULL, result, MAX_PATH );
  41.   return string(result, 1);
  42. }
  43. ifstream& GotoLine(std::ifstream& file, unsigned int num){
  44.     file.seekg(std::ios::beg);
  45.     for(unsigned int i=0; i < num - 1; ++i){
  46.         file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
  47.     }
  48.     return file;
  49. }
  50. maps getMapData(string fileName){
  51.     const string DRIVE_LETTER = getexepath();
  52.     maps rtrn;
  53.     ////////////////////////////////
  54.     ///Line 1 = comment
  55.     ///Line 2 = comment
  56.     ///Line 3 = int width
  57.     ///Line 4 = comment
  58.     ///Line 5 = int height
  59.     ///Line 6 = Dirt and wall images
  60.     ///Line 7 = Entities images
  61.     ///Line 8 = Dirt array
  62.     ///Line 9 = Entities array
  63.     ///Line 10 = Walls array
  64.     ///////////////////////////////
  65.     string path = (DRIVE_LETTER + ":/C++ Projects/Bomberman/DATA/mapData/" + fileName);
  66.     //////////////////////////////
  67.     ifstream mapFl(path.c_str());
  68.     if (!mapFl){
  69.         cout << "ERR_FILE.NOTFOUND" << endl;
  70.         cout << path << endl;
  71.     }
  72.     string readline = " ";
  73.     GotoLine(mapFl, 3);
  74.     mapFl >> readline;
  75.     const int width_ = atoi(readline.c_str());
  76.     //const int width_ = int(readline.c_str() - '0');
  77.     rtrn.width = width_;
  78.     GotoLine(mapFl, 5);
  79.     mapFl >> readline;
  80.     const int height_ = atoi(readline.c_str());
  81.     rtrn.height = height_;
  82.     GotoLine(mapFl, 6);
  83.     mapFl >> readline;
  84.     string idlePath = readline;
  85.  
  86.     GotoLine(mapFl, 7);
  87.     mapFl >> readline;
  88.     string ePath = readline;
  89.  
  90.     GotoLine(mapFl, 8);
  91.     mapFl >> readline;
  92.     string dirtAr = readline;
  93.  
  94.     GotoLine(mapFl, 9);
  95.     mapFl >> readline;
  96.     string entityAr = readline;
  97.  
  98.     GotoLine(mapFl, 10);
  99.     mapFl >> readline;
  100.     string wallAr = readline;
  101.     ////////////////////////////////
  102.     mapFl.close();
  103.     path = (DRIVE_LETTER + ":/C++ Projects/Bomberman/" + idlePath);
  104.     if(!rtrn.Barrier.loadFromFile(path)){
  105.         cout << "ERROR_CANNOT_OPEN_FILE:" << idlePath;
  106.     }
  107.     path = (DRIVE_LETTER + ":/C++ Projects/Bomberman/" + ePath);
  108.     if(!rtrn.Entities.loadFromFile(path)){
  109.         cout << "ERROR_CANNOT_OPEN_FILE:" << ePath;
  110.     }
  111.     path = (DRIVE_LETTER + ":/C++ Projects/Bomberman/" + dirtAr);
  112.     mapFl.open(path.c_str());
  113.     vector<vector<char> > result(width_, vector<char>(height_));
  114.     char arrayResult = 0;
  115.     for(int j = 0; j != height_; j++){
  116.         for(int i = 0; i != width_; i++){
  117.             if(!mapFl.good()){
  118.                 cout << "ERROR WITH FILE" << endl;
  119.                 break;
  120.             }
  121.             arrayResult = mapFl.get();
  122.             (result[i])[j] = arrayResult;
  123. //            cout << (result[i])[j];
  124.         }
  125.     }
  126.     for (unsigned int i = 0; i < rtrn.canGo.size(); ++i) {
  127.         for (unsigned int j = 0; j < rtrn.canGo[i].size(); ++j) {
  128.             bool temp = false;
  129.             if(result[i][j] == 1){
  130.                 temp = true;
  131.             }
  132.             rtrn.canGo[i][j] = temp;
  133.         }
  134.     }
  135.     for (unsigned int i = 0; i < rtrn.all.size(); ++i) {
  136.         for (unsigned int j = 0; j < rtrn.all[i].size(); ++j) {
  137.             rtrn.all[i][j].setTexture(rtrn.Barrier);
  138.             rtrn.all[i][j].setPosition(Vector2f(i * scl, j * scl));
  139.             rtrn.all[i][j].setTextureRect(getRect(false, true, false, 1, 0));
  140.         }
  141.     }
  142.     return rtrn;
  143. }
  144. //More Global variables
  145. //Main
  146. int main()
  147. {
  148.     maps LM_MAIN = getMapData("Lvl1.map");
  149.     RenderWindow window(sf::VideoMode(LM_MAIN.width * scl, (LM_MAIN.height * scl) + 20), "Bomberman!");
  150.     while (window.isOpen())
  151.     {
  152.         sf::Event event;
  153.         while (window.pollEvent(event))
  154.         {
  155.             if (event.type == sf::Event::Closed)
  156.                 window.close();
  157.         }
  158.  
  159.         window.clear();
  160.         for (auto&& v : LM_MAIN.all) {
  161.             for (auto&& sprite : v) {
  162.                 window.draw(sprite);
  163.             }
  164.         }
  165.         window.display();
  166.     }
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement