Advertisement
s1ay3r44

TileCat

Nov 8th, 2011
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include <string>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <vector>
  7. #include <memory>
  8.  
  9. using namespace std;
  10.  
  11. class TileMap
  12. {
  13.     public:
  14.         sf::RenderWindow App;
  15.         sf::RenderWindow App2;
  16.         sf::View View;
  17.         vector<int> MapData;
  18.         vector<int> VisualMap;
  19.         vector<sf::Sprite> Sprites;
  20.         vector<sf::Sprite> VisualSprites;
  21.         vector<sf::Image> Images;
  22.         vector<sf::Image> VisualImages;
  23.         vector<sf::Shape> Shapes;
  24.         vector<string> MapTypes;
  25.         int MapWidth, MapHeight, ImageSize;
  26.         string MapType, MapName;
  27.  
  28.         //Drawing Functions
  29.         void Fill(int, int,int);
  30.  
  31.  
  32.         //void Draw(int, int, int);//Done
  33.         void Inputs();
  34.         //void Inputs(vector<TileMap>&);//Done
  35.         void LoadImages();//Done
  36.         void LoadVisualImages();
  37.         void LoadSprites();//Done
  38.         void LoadVisualSprites();
  39.  
  40.         void SaveFile();//Done
  41.         void LoadFile();//Done
  42.         void LoadVisualFile();
  43.  
  44.         void GetEvents();//Done
  45.         void Display();
  46.         void VisualDisplay();
  47.         //void SaveCalc();
  48.         //void SendCalc();//This will be the last thing to be implemented
  49.         TileMap();//Type of Map
  50.         //TileMap(string, string);
  51.         //~TileMap();
  52.     protected:
  53.  
  54.     private:
  55.         int ImgListNum, selectedimage, selectedimage2;
  56.         void FillBlock(int,int,int,int,int);
  57.         bool FillInput(int, int, int);
  58. };
  59.  
  60. int main()
  61. {
  62.     TileMap Maps;
  63.     while(Maps.App.IsOpened())
  64.     {
  65.         //cout << "Ok";
  66.             Maps.GetEvents();
  67.             Maps.Inputs();
  68.             Maps.App.Clear();
  69.             Maps.App2.Clear(sf::Color::White);
  70.             if(Maps.MapType == "visual")
  71.                 Maps.Display();
  72.             else
  73.                 Maps.VisualDisplay();
  74.     }
  75.     return 0;
  76. }
  77.  
  78. void TileMap::LoadImages()
  79. {
  80.     stringstream PictureName;
  81.     bool exist = true;
  82.     int ImageNum = 0;
  83.     sf::Image tempImage;
  84.     while(exist)
  85.     {
  86.  
  87.         PictureName << "data/images/" << MapType << "/" << ImageNum << ".png";
  88.  
  89.  
  90.         if(tempImage.LoadFromFile(PictureName.str().c_str()))
  91.         {
  92.             Images.push_back(tempImage);
  93.             Images[ImageNum].SetSmooth(false);
  94.         }
  95.         else
  96.             exist = false;
  97.         //cout << "\n" << PictureName.str().c_str() << "\n";
  98.         PictureName.str(string());
  99.         ImageNum++;
  100.     }
  101.     return;
  102. }
  103.  
  104. void TileMap::LoadVisualImages()
  105. {
  106.     stringstream PictureName;
  107.     bool exist = true;
  108.     int ImageNum = 0;
  109.     sf::Image tempImage;
  110.     while(exist)
  111.     {
  112.  
  113.         PictureName << "data/images/visual/" << ImageNum << ".png";
  114.  
  115.  
  116.         if(tempImage.LoadFromFile(PictureName.str().c_str()))
  117.         {
  118.             VisualImages.push_back(tempImage);
  119.             VisualImages[ImageNum].SetSmooth(false);
  120.         }
  121.         else
  122.             exist = false;
  123.         //cout << "\n" << PictureName.str().c_str() << "\n";
  124.         PictureName.str(string());
  125.         ImageNum++;
  126.     }
  127.     return;
  128. }
  129.  
  130. void TileMap::Inputs()
  131. //void TileMap::Inputs()
  132. {
  133.     float Time, Offset;
  134.     Time = App.GetFrameTime();
  135.     const sf::Input& Input = App.GetInput();
  136.     const sf::Input& Input2 = App2.GetInput();
  137.  
  138.     if (Input.IsMouseButtonDown(sf::Mouse::Left) || Input.IsMouseButtonDown(sf::Mouse::Right))
  139.         {
  140.             sf::Vector2f MousePos=App.ConvertCoords(Input.GetMouseX(), Input.GetMouseY());
  141.             int x=MousePos.x/ImageSize;
  142.             int y=MousePos.y/ImageSize;
  143.             if (x < 0)
  144.                 x = 0;
  145.             if (y < 0)
  146.                 y = 0;
  147.             if (x > MapWidth - 1)
  148.                 x = MapWidth - 1;
  149.             if (y > MapHeight - 1)
  150.                 y = MapHeight - 1;
  151.             if (Input.IsMouseButtonDown(sf::Mouse::Left))
  152.                 MapData[x + (y * MapWidth)] = selectedimage;//Used to "draw" on the screen
  153.             else if (Input.IsMouseButtonDown(sf::Mouse::Right))
  154.                 MapData[x + (y * MapWidth)] = selectedimage2;
  155.         }
  156.     if ((Input.IsKeyDown(sf::Key::Numpad0) || Input.IsKeyDown(sf::Key::Num0)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  157.             selectedimage = 0 + ImgListNum;
  158.         else if ((Input.IsKeyDown(sf::Key::Numpad1) || Input.IsKeyDown(sf::Key::Num1)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  159.             selectedimage = 1 + ImgListNum;
  160.         else if ((Input.IsKeyDown(sf::Key::Numpad2) || Input.IsKeyDown(sf::Key::Num2)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  161.             selectedimage = 2 + ImgListNum;
  162.         else if ((Input.IsKeyDown(sf::Key::Numpad3) || Input.IsKeyDown(sf::Key::Num3)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  163.             selectedimage = 3 + ImgListNum;
  164.         else if ((Input.IsKeyDown(sf::Key::Numpad4) || Input.IsKeyDown(sf::Key::Num4)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  165.             selectedimage = 4 + ImgListNum;
  166.         else if ((Input.IsKeyDown(sf::Key::Numpad5)|| Input.IsKeyDown(sf::Key::Num5)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  167.             selectedimage = 5 + ImgListNum;
  168.         else if ((Input.IsKeyDown(sf::Key::Numpad6) || Input.IsKeyDown(sf::Key::Num6)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  169.             selectedimage = 6 + ImgListNum;
  170.         else if ((Input.IsKeyDown(sf::Key::Numpad7) || Input.IsKeyDown(sf::Key::Num7)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  171.             selectedimage = 7 + ImgListNum;
  172.         else if ((Input.IsKeyDown(sf::Key::Numpad8) || Input.IsKeyDown(sf::Key::Num8)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  173.             selectedimage = 8 + ImgListNum;
  174.         else if ((Input.IsKeyDown(sf::Key::Numpad9) || Input.IsKeyDown(sf::Key::Num9)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  175.             selectedimage = 9 + ImgListNum;
  176.  
  177.  
  178.         if ((Input.IsKeyDown(sf::Key::Numpad0) || Input.IsKeyDown(sf::Key::Num0)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  179.             selectedimage2 = 0 + ImgListNum;
  180.         else if ((Input.IsKeyDown(sf::Key::Numpad1) || Input.IsKeyDown(sf::Key::Num1)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  181.             selectedimage2 = 1 + ImgListNum;
  182.         else if ((Input.IsKeyDown(sf::Key::Numpad2) || Input.IsKeyDown(sf::Key::Num2)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  183.             selectedimage2 = 2 + ImgListNum;
  184.         else if ((Input.IsKeyDown(sf::Key::Numpad3) || Input.IsKeyDown(sf::Key::Num3)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  185.             selectedimage2 = 3 + ImgListNum;
  186.         else if ((Input.IsKeyDown(sf::Key::Numpad4) || Input.IsKeyDown(sf::Key::Num4)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  187.             selectedimage2 = 4 + ImgListNum;
  188.         else if ((Input.IsKeyDown(sf::Key::Numpad5)|| Input.IsKeyDown(sf::Key::Num5)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  189.             selectedimage2 = 5 + ImgListNum;
  190.         else if ((Input.IsKeyDown(sf::Key::Numpad6) || Input.IsKeyDown(sf::Key::Num6)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  191.             selectedimage2 = 6 + ImgListNum;
  192.         else if ((Input.IsKeyDown(sf::Key::Numpad7) || Input.IsKeyDown(sf::Key::Num7)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  193.             selectedimage2 = 7 + ImgListNum;
  194.         else if ((Input.IsKeyDown(sf::Key::Numpad8) || Input.IsKeyDown(sf::Key::Num8)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  195.             selectedimage2 = 8 + ImgListNum;
  196.         else if ((Input.IsKeyDown(sf::Key::Numpad9) || Input.IsKeyDown(sf::Key::Num9)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  197.             selectedimage2 = 9 + ImgListNum;//used to select images
  198.  
  199.     if (Input.IsKeyDown(sf::Key::E))//Erases where the mouse is
  200.         {
  201.             sf::Vector2f MousePos=App.ConvertCoords(Input.GetMouseX(), Input.GetMouseY());
  202.             int x=MousePos.x/ImageSize;
  203.             int y=MousePos.y/ImageSize;
  204.             if (x < 0)
  205.                 x = 0;
  206.             if (y < 0)
  207.                 y = 0;
  208.             if (x > MapWidth - 1)
  209.                 x = MapWidth - 1;
  210.             if (y > MapHeight - 1)
  211.                 y = MapHeight - 1;
  212.             if (Input.IsMouseButtonDown(sf::Mouse::Left))
  213.                 MapData[x + (y * MapWidth)] = -1;//Used to "draw" on the screen
  214.             else if (Input.IsMouseButtonDown(sf::Mouse::Right))
  215.                 MapData[x + (y * MapWidth)] = -1;
  216.         }
  217.     if (Input.IsKeyDown(sf::Key::Right) && ((Input.IsKeyDown(sf::Key::LControl) || Input.IsKeyDown(sf::Key::RControl))))
  218.         ImgListNum += 10;//Used to page between image tiles
  219.     else if (Input.IsKeyDown(sf::Key::Left) && ((Input.IsKeyDown(sf::Key::LControl) || Input.IsKeyDown(sf::Key::RControl))))
  220.         ImgListNum -= 10;
  221.  
  222.  
  223.     //if(Input.)
  224.  
  225.         if (ImgListNum > (signed int)Sprites.size() - 10)
  226.             ImgListNum = Sprites.size() - 10;
  227.         if (ImgListNum < 0)
  228.             ImgListNum = 0;
  229.     Offset=Time*640;
  230.         if (Input.IsKeyDown(sf::Key::Left))
  231.             View.Move(-Offset, 0);
  232.         if (Input.IsKeyDown(sf::Key::Right))
  233.             View.Move(Offset,0);
  234.         if (Input.IsKeyDown(sf::Key::Up))
  235.             View.Move(0,-Offset);
  236.         if (Input.IsKeyDown(sf::Key::Down))
  237.             View.Move(0,Offset);//Pans the camera
  238.         if (Input.IsKeyDown(sf::Key::A))
  239.             View.Move(-Offset, 0);
  240.         if (Input.IsKeyDown(sf::Key::D))
  241.             View.Move(Offset,0);
  242.         if (Input.IsKeyDown(sf::Key::W))
  243.             View.Move(0,-Offset);
  244.         if (Input.IsKeyDown(sf::Key::S))
  245.             View.Move(0,Offset);
  246.  
  247.     if ((Input.IsKeyDown(sf::Key::LControl) || Input.IsKeyDown(sf::Key::RControl)) && Input.IsKeyDown(sf::Key::S))
  248.         SaveFile();
  249.     if ((Input.IsKeyDown(sf::Key::LControl) || Input.IsKeyDown(sf::Key::RControl)) && Input.IsKeyDown(sf::Key::L))
  250.         LoadFile();
  251.     /*if ((Input.IsKeyDown(sf::Key::LControl) || Input.IsKeyDown(sf::Key::RControl)) && Input.IsKeyDown(sf::Key::O))
  252.         {
  253.             Maps.push_back(TileMap());
  254.             for(int i; i < Maps.size(); i++)
  255.                 Maps[i].LoadSprites();
  256.         }*/
  257.     if (Input2.IsMouseButtonDown(sf::Mouse::Left))
  258.     {
  259.         sf::Vector2f MousePos = App2.ConvertCoords(Input2.GetMouseX(), Input2.GetMouseY());
  260.         selectedimage = MousePos.y/ImageSize + ImgListNum;
  261.     }
  262.  
  263.     if (Input2.IsMouseButtonDown(sf::Mouse::Right))
  264.     {
  265.         sf::Vector2f MousePos = App2.ConvertCoords(Input2.GetMouseX(), Input2.GetMouseY());
  266.         selectedimage2 = MousePos.y/ImageSize + ImgListNum;
  267.     }
  268.  
  269.     if((Input.IsKeyDown(sf::Key::LControl) || Input.IsKeyDown(sf::Key::RControl)) && Input.IsKeyDown(sf::Key::F) && Input.IsMouseButtonDown(sf::Mouse::Left))
  270.     {
  271.         sf::Vector2f MousePos=App.ConvertCoords(Input.GetMouseX(), Input.GetMouseY());
  272.         int x=MousePos.x/ImageSize;
  273.         int y=MousePos.y/ImageSize;
  274.         Fill(x,y,selectedimage);
  275.     }
  276.     else if((Input.IsKeyDown(sf::Key::LControl) || Input.IsKeyDown(sf::Key::RControl)) && Input.IsKeyDown(sf::Key::F) && Input.IsMouseButtonDown(sf::Mouse::Right))
  277.     {
  278.         sf::Vector2f MousePos=App.ConvertCoords(Input.GetMouseX(), Input.GetMouseY());
  279.         int x=MousePos.x/ImageSize;
  280.         int y=MousePos.y/ImageSize;
  281.         Fill(x,y,selectedimage2);
  282.     }
  283.     return;
  284. }
  285.  
  286.  
  287. void TileMap::LoadSprites()
  288. {
  289.     Sprites.resize(Images.size());
  290.     for(int i = 0; i < Sprites.size(); i++)
  291.         Sprites[i].SetImage(Images[i]);
  292.     return;
  293. }
  294.  
  295. void TileMap::LoadVisualSprites()
  296. {
  297.     VisualSprites.resize(VisualImages.size());
  298.     for(int i = 0; i < VisualSprites.size(); i++)
  299.         VisualSprites[i].SetImage(VisualImages[i]);
  300.     return;
  301. }
  302.  
  303. void TileMap::LoadFile()
  304. {
  305.     fstream map;
  306.     string map_path;//Holds the path to the files
  307.  
  308.     try
  309.     {
  310.  
  311.  
  312.         map_path = "data/maps/" + MapType + "/" + MapName;
  313.  
  314.         //cout << map_path.c_str() << "\n";
  315.         map.open(map_path.c_str(), ios::in|ios::binary);
  316.         int memblock[2];
  317.         //int* memblock;
  318.         if(map.is_open())
  319.         {
  320.             //memblock = new int [2];
  321.             //map.seekg(0, ios::beg);
  322.             map.read((char*)memblock, 2 * sizeof(int));
  323.             MapWidth = memblock[0];
  324.             MapHeight = memblock[1];
  325.             //delete[] memblock;
  326.             int mapblock[MapWidth * MapHeight];
  327.             MapData.resize(MapWidth * MapHeight);
  328.             //map.seekg(2, ios::beg);
  329.             map.read((char*)mapblock, MapWidth * MapHeight * sizeof(int));
  330.             for(int i = 0; i < (MapWidth * MapHeight); i++)
  331.                 MapData[i] = mapblock[i];
  332.             //cout << MapData[0];
  333.             //cout << map_path.c_str();
  334.  
  335.             map.close();
  336.         }
  337.     }
  338.     catch(exception& e)
  339.         {cout << e.what() << "\n";}
  340.     return;
  341. }
  342.  
  343. void TileMap::LoadVisualFile()
  344. {
  345.     fstream map;
  346.     string map_path;//Holds the path to the files
  347.  
  348.     try
  349.     {
  350.  
  351.  
  352.         map_path = "data/maps/visual/" + MapName;
  353.  
  354.         //cout << map_path.c_str() << "\n";
  355.         map.open(map_path.c_str(), ios::in|ios::binary);
  356.         int memblock[2];
  357.         //int* memblock;
  358.         if(map.is_open())
  359.         {
  360.             //memblock = new int [2];
  361.             //map.seekg(0, ios::beg);
  362.             map.read((char*)memblock, 2 * sizeof(int));
  363.             MapWidth = memblock[0];
  364.             MapHeight = memblock[1];
  365.             //delete[] memblock;
  366.             int mapblock[MapWidth * MapHeight];
  367.             VisualMap.resize(MapWidth * MapHeight);
  368.             //map.seekg(2, ios::beg);
  369.             map.read((char*)mapblock, MapWidth * MapHeight * sizeof(int));
  370.             for(int i = 0; i < (MapWidth * MapHeight); i++)
  371.                 VisualMap[i] = mapblock[i];
  372.             //cout << MapData[0];
  373.             //cout << map_path.c_str();
  374.  
  375.             map.close();
  376.         }
  377.     }
  378.     catch(exception& e)
  379.         {cout << e.what() << "\n";}
  380.     return;
  381. }
  382.  
  383. void TileMap::SaveFile()
  384. {
  385.     fstream map;
  386.     string map_path;
  387.  
  388.     map_path = "data/maps/" + MapType + "/" + MapName;
  389.     map.open(map_path.c_str(), ios::out|ios::binary);
  390.     if(map.is_open())
  391.         {
  392.             int memblock[MapWidth * MapHeight + 2];
  393.             memblock[0] = MapWidth;
  394.             memblock[1] = MapHeight;
  395.             for(int i = 0; i < (MapWidth * MapHeight); i++)
  396.                 memblock[i + 2] = MapData[i];
  397.             map.write((char*)memblock, (MapWidth * MapHeight + 2) * sizeof(int));
  398.             map.close();
  399.         }
  400.  
  401.     return;
  402. }
  403.  
  404. void TileMap::GetEvents()
  405. {
  406.     sf::Event Event;
  407.     while(App.GetEvent(Event))
  408.         if(Event.Type == sf::Event::Closed)
  409.         {
  410.             App.Close();
  411.             App2.Close();
  412.         }
  413.     return;
  414. }
  415.  
  416. void TileMap::Display()
  417. {
  418.     LoadSprites();
  419.     sf::FloatRect Rect =  View.GetRect();
  420.     //for (int y = (Rect.Top / 64) - 1; y < (int)(Rect.Bottom / 64) + 1; y++)
  421.     for(int y = 0; y < MapHeight; y++)
  422.     {
  423.         if(y < 0)
  424.             y = 0;
  425.         else if (y > MapHeight - 1)
  426.             y = MapHeight - 1;
  427.  
  428.         //for (int x = (Rect.Left / 64) - 1; x < (int)(Rect.Right / 64) + 1; x++)
  429.         for(int x = 0; x < MapWidth; x++)
  430.         {
  431.             if(x < 0)
  432.                 x = 0;
  433.             else if (x > MapWidth - 1)
  434.                 x = MapWidth - 1;
  435.             if (MapData[x + (y * MapWidth)] != -1)
  436.             {
  437.                 Sprites[MapData[x + (y * MapWidth)]].SetPosition(x * ImageSize, y * ImageSize);
  438.                 App.Draw(Sprites[MapData[x + (y * MapWidth)]]);
  439.             }
  440.         }
  441.     }
  442.     App.Draw(Shapes[0]);
  443.     for(int i = 0; i < 10; i++)
  444.     {
  445.         Sprites[i + ImgListNum].SetPosition(0, i * ImageSize);
  446.         //cout << i + ImgListNum;
  447.         App2.Draw(Sprites[i + ImgListNum]);
  448.     }
  449.     Shapes[1].SetPosition(0, ImageSize * (selectedimage - ImgListNum));
  450.     Shapes[2].SetPosition(0, ImageSize * (selectedimage2 - ImgListNum));
  451.  
  452.     for(int i = 1; i < Shapes.size(); i++)
  453.         App2.Draw(Shapes[i]);
  454.     App.Display();
  455.     App2.Display();
  456.     return;
  457. }
  458.  
  459. void TileMap::VisualDisplay()
  460. {
  461.     sf::FloatRect Rect =  View.GetRect();
  462.     //for (int y = (Rect.Top / 64) - 1; y < (int)(Rect.Bottom / 64) + 1; y++)
  463.     for(int y = 0; y < MapHeight; y++)
  464.     {
  465.         if(y < 0)
  466.             y = 0;
  467.         else if (y > MapHeight - 1)
  468.             y = MapHeight - 1;
  469.  
  470.         //for (int x = (Rect.Left / 64) - 1; x < (int)(Rect.Right / 64) + 1; x++)
  471.         for(int x = 0; x < MapWidth; x++)
  472.         {
  473.             if(x < 0)
  474.                 x = 0;
  475.             else if (x > MapWidth - 1)
  476.                 x = MapWidth - 1;
  477.             if (VisualMap[x + (y * MapWidth)] != -1)
  478.             {
  479.                 VisualSprites[VisualMap[x + (y * MapWidth)]].SetPosition(x * ImageSize, y * ImageSize);
  480.                 App.Draw(VisualSprites[VisualMap[x + (y * MapWidth)]]);
  481.             }
  482.             if (MapData[x + (y * MapWidth)] != -1)
  483.             {
  484.                 Sprites[MapData[x + (y * MapWidth)]].SetPosition(x * ImageSize, y * ImageSize);
  485.                 App.Draw(Sprites[MapData[x + (y * MapWidth)]]);
  486.             }
  487.         }
  488.     }
  489.     App.Draw(Shapes[0]);
  490.     for(int i = 0; i < 10; i++)
  491.     {
  492.         Sprites[i + ImgListNum].SetPosition(0, i * ImageSize);
  493.         App2.Draw(Sprites[i + ImgListNum]);
  494.     }
  495.     Shapes[1].SetPosition(0, ImageSize * (selectedimage - ImgListNum));
  496.     Shapes[2].SetPosition(0, ImageSize * (selectedimage2 - ImgListNum));
  497.  
  498.     for(int i = 1; i < Shapes.size(); i++)
  499.         App2.Draw(Shapes[i]);
  500.     App.Display();
  501.     App2.Display();
  502.     return;
  503. }
  504.  
  505. TileMap::TileMap()
  506. {
  507.     bool load = false;
  508.     MapTypes.push_back("visual");
  509.     MapTypes.push_back("collision");
  510.     MapTypes.push_back("transitional");
  511.     MapTypes.push_back("events");
  512.     MapTypes.push_back("NPC");
  513.  
  514.     int choice = -1;
  515.     while(choice < 0 || choice > MapTypes.size())
  516.     {
  517.         system("cls");
  518.         cout << "Map Type";
  519.         for(int i = 0; i < MapTypes.size(); i++)
  520.             cout << "\n" << i << ":\t" << MapTypes[i];
  521.         cout << "\n(1, 2, 3, 4...): ";
  522.         cin >> choice;
  523.     }
  524.     MapType = MapTypes[choice];
  525.     cout << "Do you want to load a map? (1/0)";
  526.     cin >> load;
  527.     if(load)
  528.     {
  529.         cout << "Name of Map to Load: ";
  530.         cin >> MapName;
  531.         LoadFile();
  532.     }
  533.     else
  534.     {
  535.         cout << "Name of New Map: ";
  536.         cin >> MapName;
  537.  
  538.         cout << "Input Map Size (X Y): ";
  539.         cin >> MapWidth >> MapHeight;
  540.  
  541.         MapData.resize(MapWidth * MapHeight, -1);
  542.     }
  543.  
  544.     cout << "Input Image Size: ";
  545.     cin >> ImageSize;
  546.  
  547.     App.Create(sf::VideoMode(1088, 832), MapType + " - " + MapName);
  548.     App2.Create(sf::VideoMode(64, 640), "Images");
  549.  
  550.     App.UseVerticalSync(true);
  551.     App2.UseVerticalSync(true);
  552.  
  553.     View.SetFromRect(sf::FloatRect(0,0,1088,832));
  554.     App.SetView(View);
  555.  
  556.     LoadImages();
  557.     cout << "Images Loaded\n";
  558.     LoadSprites();
  559.     if(MapType != "visual")
  560.     {
  561.         LoadVisualFile();
  562.         LoadVisualImages();
  563.         LoadVisualSprites();
  564.     }
  565.  
  566.     cout << "Sprites Loaded\n";
  567.     selectedimage = 0;
  568.     selectedimage2 = 1;
  569.     ImgListNum = 0;
  570.  
  571.     Shapes.push_back(sf::Shape::Rectangle(0,0,(MapWidth * ImageSize), (MapHeight * ImageSize),sf::Color(255,255,255,0),4,sf::Color(0,0,255,127)));
  572.     Shapes.push_back(sf::Shape::Rectangle(0,0,ImageSize,ImageSize,sf::Color(255,255,255,0),4,sf::Color(0,0,255,127)));
  573.     Shapes.push_back(sf::Shape::Rectangle(0,0,ImageSize,ImageSize,sf::Color(255,255,255,0),4,sf::Color(0,255,0,127)));
  574.  
  575. }
  576.  
  577. void TileMap::Fill(int arg1, int arg2, int value)
  578. {
  579.     //int arg3 = -1, arg4 = -1, hold1, hold2;
  580.     //const sf::Input& Input = App.GetInput();
  581.     bool ButtonDown = true;
  582.     sf::Event Event;
  583.  
  584.     while(ButtonDown)
  585.     {
  586.         while (App.GetEvent(Event))
  587.         {
  588.         if(Event.Type == sf::Event::MouseButtonReleased)
  589.             {
  590.                 ButtonDown = false;
  591.                 cout << "released";
  592.             }
  593.         }
  594.     }
  595.     while (!ButtonDown)
  596.     {
  597.         //cout << "Stuck";
  598.         ButtonDown = FillInput(arg1, arg2, value);
  599.  
  600.         App.Clear();
  601.  
  602.         if(MapType == "visual")
  603.             Display();
  604.         else
  605.             VisualDisplay();
  606.  
  607.     }
  608.     return;
  609. }
  610.  
  611. void TileMap::FillBlock(int x1, int y1, int x2, int y2, int value)
  612. {
  613.     for(int i = y1; i < y2; i++)
  614.         for(int n = x1; n < x2; n++)
  615.             MapData[n + (i * MapWidth)] = value;
  616.     return;
  617. }
  618.  
  619. bool TileMap::FillInput(int arg1, int arg2, int value)
  620. {
  621.     const sf::Input& Input = App.GetInput();
  622.     float Time, Offset;
  623.     Time = App.GetFrameTime();
  624.  
  625.         if (Input.IsMouseButtonDown(sf::Mouse::Left) || Input.IsMouseButtonDown(sf::Mouse::Right))
  626.         {
  627.             sf::Vector2f MousePos=App.ConvertCoords(Input.GetMouseX(), Input.GetMouseY());
  628.             int arg3=MousePos.x/ImageSize;
  629.             int arg4=MousePos.y/ImageSize;
  630.             if (arg3 < 0)
  631.                 arg3 = 0;
  632.             if (arg4 < 0)
  633.                 arg4 = 0;
  634.             if (arg3 > MapWidth - 1)
  635.                 arg3 = MapWidth - 1;
  636.             if (arg4 > MapHeight - 1)
  637.                 arg4 = MapHeight - 1;
  638.  
  639.             if(arg1 < arg3)
  640.             {
  641.                 int hold1 = arg1;
  642.                 arg1 = arg3;
  643.                 arg3 = hold1;
  644.             }
  645.             if(arg2 < arg4)
  646.             {
  647.                 int hold2 = arg2;
  648.                 arg2 = arg4;
  649.                 arg4 = hold2;
  650.             }
  651.             FillBlock(arg1,arg2,arg3,arg4,value);
  652.             cout << "fill\n";
  653.             return true;
  654.         }
  655.  
  656.         Offset=Time*640;
  657.         if (Input.IsKeyDown(sf::Key::Left))
  658.             View.Move(-Offset, 0);
  659.         if (Input.IsKeyDown(sf::Key::Right))
  660.             View.Move(Offset,0);
  661.         if (Input.IsKeyDown(sf::Key::Up))
  662.             View.Move(0,-Offset);
  663.         if (Input.IsKeyDown(sf::Key::Down))
  664.             View.Move(0,Offset);//Pans the camera
  665.         if (Input.IsKeyDown(sf::Key::A))
  666.             View.Move(-Offset, 0);
  667.         if (Input.IsKeyDown(sf::Key::D))
  668.             View.Move(Offset,0);
  669.         if (Input.IsKeyDown(sf::Key::W))
  670.             View.Move(0,-Offset);
  671.         if (Input.IsKeyDown(sf::Key::S))
  672.             View.Move(0,Offset);
  673.  
  674.     return false;
  675. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement