Advertisement
s1ay3r44

Tile Map Editor Source

Jun 20th, 2011
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. void Save_File(char * array, int x, int y)
  9. {
  10.  
  11.     ifstream::pos_type size = x * y + 2;
  12.     char * memblock;
  13.     ofstream file ("Map.bin", ios::out|ios::binary);
  14.     if(file.is_open())
  15.     {
  16.  
  17.     memblock = new char [size];
  18.         memblock[0] = x;
  19.         memblock[1] = y;
  20.         for(int i = 0; i<size; i++)
  21.         {
  22.             memblock[i+2] = *(array + i);
  23.         }
  24.         file.write (memblock, size);
  25.         file.close();
  26.         delete[] memblock;
  27.     }
  28.     else
  29.     cout << "Unable to open file.\n";
  30.     return;
  31. }
  32.  
  33. char* PreLoad()
  34. {//Gets the stuff ready to load
  35.     char counter;
  36.         cout << "Finea";
  37.  
  38.     ifstream::pos_type size;
  39.  
  40.     char * memblock;
  41.     ifstream file ("Map.bin", ios::in|ios::binary|ios::ate);
  42.     if (file.is_open())
  43.     {
  44.         size = 2;
  45.         memblock = new char [size];
  46.         cout << "Fineb";
  47.  
  48.         file.seekg (0, ios::beg);
  49.         cout << "Finec";
  50.  
  51.         file.read (memblock, size);
  52.         cout << "Fined";
  53.  
  54.         file.close();
  55.     }
  56.     return memblock;
  57. }
  58.  
  59. void Load_File(char * array)
  60. {
  61.  
  62.     ifstream::pos_type size;
  63.     char * memblock;
  64.     ifstream file ("Map.bin", ios::in|ios::binary|ios::ate);
  65.     if (file.is_open())
  66.     {
  67.         size = file.tellg();
  68.         memblock = new char [size];
  69.         file.seekg (0, ios::beg);
  70.         file.read (memblock, size);
  71.         file.close();
  72.     }
  73.  
  74.     int area = memblock[0] * memblock[1];
  75.     for(int i = 0; i < area; i++)
  76.     {
  77.         *(array + i) = memblock[i+2];
  78.     }
  79.  
  80.     delete[] memblock;
  81.     return;
  82. }
  83.  
  84. int main()
  85. {
  86.  
  87.  
  88.     sf::RenderWindow App2(sf::VideoMode(64, 640), "Images");// Window for the images
  89.     sf::RenderWindow App(sf::VideoMode(1024, 768), "Tile Map Editor v0.0.3.0 Experimental");
  90.  
  91.     App.UseVerticalSync(true);
  92.     App2.UseVerticalSync(true);
  93.  
  94.     sf::View View(sf::FloatRect(0,0,1024,768));
  95.     App.SetView(View);
  96.  
  97.     // Load Sprites
  98.     const short int numofimgs=10; // this is the number of images
  99.     string imgnames[numofimgs]={"0.png","1.png","2.png","3.png","4.png","5.png","6.png","7.png","8.png","9.png"}; // set the image filenames here into a string array
  100.     sf::Sprite imgs[numofimgs]; // make a sprite array
  101.     sf::Image imgfiles[numofimgs]; // make an image array
  102.  
  103.     int a;
  104.  
  105.     for (a=0; a<numofimgs; a++) // load images into ram, then make them sprites so they can be drawn onto the rendering window
  106.     {
  107.         if (!imgfiles[a].LoadFromFile(imgnames[a])) return a+1; // if the file can't be loaded then just end the program returning the image number in the array
  108.         imgs[a].SetImage(imgfiles[a]); // set the image into a usable sprite
  109.     }
  110.  
  111.     sf::Shape Selection(sf::Shape::Rectangle(0,0,64,64,sf::Color(255,255,255,0),4,sf::Color(0,0,255,127)));
  112.     sf::Shape Selection2(sf::Shape::Rectangle(0,0,64,64,sf::Color(255,255,255,0),4,sf::Color(0,255,0,127)));
  113.  
  114.     int xx, yy, y2, x, y;
  115.     int selectedimage=1;
  116.     int selectedimage2=0;
  117.     float Time, Offset;
  118.         //cout << "Do you want to load a saved map?";
  119.     cout << "How big do you want it? (twss)";
  120.     cin >> xx >> yy;
  121.     char * tiles;
  122.     tiles = new char [xx * yy]; // this array will store the image number for each tile
  123.     char * array = &tiles[0];
  124.     for (y=0; y<yy; y++)
  125.         for (x=0; x<xx; x++)
  126.             *(array + x + (x * y)) = -1;
  127.  
  128.     // Start the game loop
  129.     while (App.IsOpened())
  130.     {
  131.         // Process events
  132.         sf::Event Event;
  133.         while (App.GetEvent(Event))
  134.         {
  135.             // Close window : exit
  136.             if (Event.Type == sf::Event::Closed)
  137.             {
  138.                 App.Close();
  139.                 App2.Close();
  140.             }
  141.  
  142.         }
  143.  
  144.         Time=App.GetFrameTime();
  145.  
  146.         const sf::Input& Input=App.GetInput();
  147.         //const sf::Input& Input2=App2.GetInput();
  148.  
  149.         if (Input.IsMouseButtonDown(sf::Mouse::Left) || Input.IsMouseButtonDown(sf::Mouse::Right))
  150.         {
  151.             sf::Vector2f MousePos=App.ConvertCoords(Input.GetMouseX(), Input.GetMouseY());
  152.             x=MousePos.x/64;
  153.             y=MousePos.y/64;
  154.             if (x<0)
  155.                 x=0;
  156.             if (y<0)
  157.                 y=0;
  158.             if (x>xx-1)
  159.                 x=xx-1;
  160.             if (y>yy-1)
  161.                 y=yy-1;
  162.             if (Input.IsMouseButtonDown(sf::Mouse::Left))
  163.                 *(array + x + (x * y))=selectedimage;
  164.             else if (Input.IsMouseButtonDown(sf::Mouse::Right))
  165.                 *(array + x + (x * y))=selectedimage2;
  166.         }
  167.  
  168. /*
  169.         if (Input2.IsMouseButtonDown(sf::Mouse::Left))
  170.         {
  171.             sf::Vector2f MousePos2=App2.ConvertCoords(Input2.GetMouseX(), Input2.GetMouseY());
  172.             selectedimage=MousePos2.y/64;
  173.             if (selectedimage<0)
  174.                 selectedimage=0;
  175.             if (selectedimage>9)
  176.                 selectedimage=9;
  177.         }
  178. */
  179.  
  180. if ((Input.IsKeyDown(sf::Key::Numpad0) || Input.IsKeyDown(sf::Key::Num0)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  181.             selectedimage = 0;
  182.         else if ((Input.IsKeyDown(sf::Key::Numpad1) || Input.IsKeyDown(sf::Key::Num1)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  183.             selectedimage = 1;
  184.         else if ((Input.IsKeyDown(sf::Key::Numpad2) || Input.IsKeyDown(sf::Key::Num2)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  185.             selectedimage = 2;
  186.         else if ((Input.IsKeyDown(sf::Key::Numpad3) || Input.IsKeyDown(sf::Key::Num3)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  187.             selectedimage = 3;
  188.         else if ((Input.IsKeyDown(sf::Key::Numpad4) || Input.IsKeyDown(sf::Key::Num4)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  189.             selectedimage = 4;
  190.         else if ((Input.IsKeyDown(sf::Key::Numpad5)|| Input.IsKeyDown(sf::Key::Num5)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  191.             selectedimage = 5;
  192.         else if ((Input.IsKeyDown(sf::Key::Numpad6) || Input.IsKeyDown(sf::Key::Num6)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  193.             selectedimage = 6;
  194.         else if ((Input.IsKeyDown(sf::Key::Numpad7) || Input.IsKeyDown(sf::Key::Num7)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  195.             selectedimage = 7;
  196.         else if ((Input.IsKeyDown(sf::Key::Numpad8) || Input.IsKeyDown(sf::Key::Num8)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  197.             selectedimage = 8;
  198.         else if ((Input.IsKeyDown(sf::Key::Numpad9) || Input.IsKeyDown(sf::Key::Num9)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  199.             selectedimage = 9;
  200.  
  201.  
  202. if ((Input.IsKeyDown(sf::Key::Numpad0) || Input.IsKeyDown(sf::Key::Num0)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  203.             selectedimage2 = 0;
  204.         else if ((Input.IsKeyDown(sf::Key::Numpad1) || Input.IsKeyDown(sf::Key::Num1)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  205.             selectedimage2 = 1;
  206.         else if ((Input.IsKeyDown(sf::Key::Numpad2) || Input.IsKeyDown(sf::Key::Num2)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  207.             selectedimage2 = 2;
  208.         else if ((Input.IsKeyDown(sf::Key::Numpad3) || Input.IsKeyDown(sf::Key::Num3)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  209.             selectedimage2 = 3;
  210.         else if ((Input.IsKeyDown(sf::Key::Numpad4) || Input.IsKeyDown(sf::Key::Num4)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  211.             selectedimage2 = 4;
  212.         else if ((Input.IsKeyDown(sf::Key::Numpad5)|| Input.IsKeyDown(sf::Key::Num5)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  213.             selectedimage2 = 5;
  214.         else if ((Input.IsKeyDown(sf::Key::Numpad6) || Input.IsKeyDown(sf::Key::Num6)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  215.             selectedimage2 = 6;
  216.         else if ((Input.IsKeyDown(sf::Key::Numpad7) || Input.IsKeyDown(sf::Key::Num7)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  217.             selectedimage2 = 7;
  218.         else if ((Input.IsKeyDown(sf::Key::Numpad8) || Input.IsKeyDown(sf::Key::Num8)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  219.             selectedimage2 = 8;
  220.         else if ((Input.IsKeyDown(sf::Key::Numpad9) || Input.IsKeyDown(sf::Key::Num9)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
  221.             selectedimage2 = 9;
  222.  
  223.         Offset=Time*640;
  224.         if (Input.IsKeyDown(sf::Key::Left))
  225.             View.Move(-Offset, 0);
  226.         if (Input.IsKeyDown(sf::Key::Right))
  227.             View.Move(Offset,0);
  228.         if (Input.IsKeyDown(sf::Key::Up))
  229.             View.Move(0,-Offset);
  230.         if (Input.IsKeyDown(sf::Key::Down))
  231.             View.Move(0,Offset);
  232.         if ((Input.IsKeyDown(sf::Key::LControl) || Input.IsKeyDown(sf::Key::RControl)) && Input.IsKeyDown(sf::Key::S))
  233.         Save_File(array, xx, yy);// Ctrl+S will Save
  234.  
  235.         if ((Input.IsKeyDown(sf::Key::LControl) || Input.IsKeyDown(sf::Key::RControl)) && Input.IsKeyDown(sf::Key::L))
  236.         {
  237.  
  238.             //cout << "bad";
  239.             delete[] tiles;
  240.             //cout << "nevermind";
  241.             char * memblock = PreLoad();
  242.             xx = memblock[0];
  243.             yy = memblock[1];
  244.             delete[] memblock;
  245.             char * tiles;
  246.             tiles = new char [xx * yy]; // this array will store the image number for each tile
  247.             array = &tiles[0];
  248.             Load_File(array);// Ctrl+L will Load
  249.         }
  250.  
  251. /*
  252. =========================================
  253. =========================================
  254. =========================================
  255. EDIT CODE HERE
  256. =========================================
  257. =========================================
  258. */
  259.         // Clear screen
  260.         App.Clear();
  261.         App2.Clear();
  262.  
  263.         // Draw stuff on main window
  264.         for (y=0; y<yy; y++)
  265.         {
  266.             for (x=0; x<xx; x++)
  267.             {
  268.                 if (*(array + x + (x * y))==-1)
  269.                     continue;
  270.                 imgs[*(array + x + (x * y))].SetPosition(x*64, y*64);
  271.  
  272.                 App.Draw(imgs[*(array + x + (x * y))]);
  273.             }
  274.         }
  275.  
  276.         // Draw stuff on the image list
  277.         for (a=0; a<numofimgs; a++)
  278.         {
  279.             imgs[a].SetPosition(0, a*64);
  280.             App2.Draw(imgs[a]);
  281.         }
  282.         Selection.SetPosition(0,64*selectedimage);
  283.         App2.Draw(Selection);
  284.  
  285.         Selection2.SetPosition(0,64*selectedimage2);
  286.         App2.Draw(Selection2);
  287.  
  288.         // Update the window
  289.         App.Display();
  290.         App2.Display();
  291.     }
  292.     delete[] tiles;
  293.     return 0;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement