Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include <string>
- #include <fstream>
- using namespace std;
- void Save_File(char * array, int x, int y)
- {
- ifstream::pos_type size = x * y + 2;
- char * memblock;
- ofstream file ("Map.bin", ios::out|ios::binary);
- if(file.is_open())
- {
- memblock = new char [size];
- memblock[0] = x;
- memblock[1] = y;
- for(int i = 0; i<size; i++)
- {
- memblock[i+2] = *(array + i);
- }
- file.write (memblock, size);
- file.close();
- delete[] memblock;
- }
- else
- cout << "Unable to open file.\n";
- return;
- }
- char* PreLoad()
- {//Gets the stuff ready to load
- char counter;
- cout << "Finea";
- ifstream::pos_type size;
- char * memblock;
- ifstream file ("Map.bin", ios::in|ios::binary|ios::ate);
- if (file.is_open())
- {
- size = 2;
- memblock = new char [size];
- cout << "Fineb";
- file.seekg (0, ios::beg);
- cout << "Finec";
- file.read (memblock, size);
- cout << "Fined";
- file.close();
- }
- return memblock;
- }
- void Load_File(char * array)
- {
- ifstream::pos_type size;
- char * memblock;
- ifstream file ("Map.bin", ios::in|ios::binary|ios::ate);
- if (file.is_open())
- {
- size = file.tellg();
- memblock = new char [size];
- file.seekg (0, ios::beg);
- file.read (memblock, size);
- file.close();
- }
- int area = memblock[0] * memblock[1];
- for(int i = 0; i < area; i++)
- {
- *(array + i) = memblock[i+2];
- }
- delete[] memblock;
- return;
- }
- int main()
- {
- sf::RenderWindow App2(sf::VideoMode(64, 640), "Images");// Window for the images
- sf::RenderWindow App(sf::VideoMode(1024, 768), "Tile Map Editor v0.0.3.0 Experimental");
- App.UseVerticalSync(true);
- App2.UseVerticalSync(true);
- sf::View View(sf::FloatRect(0,0,1024,768));
- App.SetView(View);
- // Load Sprites
- const short int numofimgs=10; // this is the number of images
- 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
- sf::Sprite imgs[numofimgs]; // make a sprite array
- sf::Image imgfiles[numofimgs]; // make an image array
- int a;
- for (a=0; a<numofimgs; a++) // load images into ram, then make them sprites so they can be drawn onto the rendering window
- {
- 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
- imgs[a].SetImage(imgfiles[a]); // set the image into a usable sprite
- }
- sf::Shape Selection(sf::Shape::Rectangle(0,0,64,64,sf::Color(255,255,255,0),4,sf::Color(0,0,255,127)));
- sf::Shape Selection2(sf::Shape::Rectangle(0,0,64,64,sf::Color(255,255,255,0),4,sf::Color(0,255,0,127)));
- int xx, yy, y2, x, y;
- int selectedimage=1;
- int selectedimage2=0;
- float Time, Offset;
- //cout << "Do you want to load a saved map?";
- cout << "How big do you want it? (twss)";
- cin >> xx >> yy;
- char * tiles;
- tiles = new char [xx * yy]; // this array will store the image number for each tile
- char * array = &tiles[0];
- for (y=0; y<yy; y++)
- for (x=0; x<xx; x++)
- *(array + x + (x * y)) = -1;
- // Start the game loop
- while (App.IsOpened())
- {
- // Process events
- sf::Event Event;
- while (App.GetEvent(Event))
- {
- // Close window : exit
- if (Event.Type == sf::Event::Closed)
- {
- App.Close();
- App2.Close();
- }
- }
- Time=App.GetFrameTime();
- const sf::Input& Input=App.GetInput();
- //const sf::Input& Input2=App2.GetInput();
- if (Input.IsMouseButtonDown(sf::Mouse::Left) || Input.IsMouseButtonDown(sf::Mouse::Right))
- {
- sf::Vector2f MousePos=App.ConvertCoords(Input.GetMouseX(), Input.GetMouseY());
- x=MousePos.x/64;
- y=MousePos.y/64;
- if (x<0)
- x=0;
- if (y<0)
- y=0;
- if (x>xx-1)
- x=xx-1;
- if (y>yy-1)
- y=yy-1;
- if (Input.IsMouseButtonDown(sf::Mouse::Left))
- *(array + x + (x * y))=selectedimage;
- else if (Input.IsMouseButtonDown(sf::Mouse::Right))
- *(array + x + (x * y))=selectedimage2;
- }
- /*
- if (Input2.IsMouseButtonDown(sf::Mouse::Left))
- {
- sf::Vector2f MousePos2=App2.ConvertCoords(Input2.GetMouseX(), Input2.GetMouseY());
- selectedimage=MousePos2.y/64;
- if (selectedimage<0)
- selectedimage=0;
- if (selectedimage>9)
- selectedimage=9;
- }
- */
- if ((Input.IsKeyDown(sf::Key::Numpad0) || Input.IsKeyDown(sf::Key::Num0)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage = 0;
- else if ((Input.IsKeyDown(sf::Key::Numpad1) || Input.IsKeyDown(sf::Key::Num1)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage = 1;
- else if ((Input.IsKeyDown(sf::Key::Numpad2) || Input.IsKeyDown(sf::Key::Num2)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage = 2;
- else if ((Input.IsKeyDown(sf::Key::Numpad3) || Input.IsKeyDown(sf::Key::Num3)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage = 3;
- else if ((Input.IsKeyDown(sf::Key::Numpad4) || Input.IsKeyDown(sf::Key::Num4)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage = 4;
- else if ((Input.IsKeyDown(sf::Key::Numpad5)|| Input.IsKeyDown(sf::Key::Num5)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage = 5;
- else if ((Input.IsKeyDown(sf::Key::Numpad6) || Input.IsKeyDown(sf::Key::Num6)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage = 6;
- else if ((Input.IsKeyDown(sf::Key::Numpad7) || Input.IsKeyDown(sf::Key::Num7)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage = 7;
- else if ((Input.IsKeyDown(sf::Key::Numpad8) || Input.IsKeyDown(sf::Key::Num8)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage = 8;
- else if ((Input.IsKeyDown(sf::Key::Numpad9) || Input.IsKeyDown(sf::Key::Num9)) && !(Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage = 9;
- if ((Input.IsKeyDown(sf::Key::Numpad0) || Input.IsKeyDown(sf::Key::Num0)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage2 = 0;
- else if ((Input.IsKeyDown(sf::Key::Numpad1) || Input.IsKeyDown(sf::Key::Num1)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage2 = 1;
- else if ((Input.IsKeyDown(sf::Key::Numpad2) || Input.IsKeyDown(sf::Key::Num2)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage2 = 2;
- else if ((Input.IsKeyDown(sf::Key::Numpad3) || Input.IsKeyDown(sf::Key::Num3)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage2 = 3;
- else if ((Input.IsKeyDown(sf::Key::Numpad4) || Input.IsKeyDown(sf::Key::Num4)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage2 = 4;
- else if ((Input.IsKeyDown(sf::Key::Numpad5)|| Input.IsKeyDown(sf::Key::Num5)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage2 = 5;
- else if ((Input.IsKeyDown(sf::Key::Numpad6) || Input.IsKeyDown(sf::Key::Num6)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage2 = 6;
- else if ((Input.IsKeyDown(sf::Key::Numpad7) || Input.IsKeyDown(sf::Key::Num7)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage2 = 7;
- else if ((Input.IsKeyDown(sf::Key::Numpad8) || Input.IsKeyDown(sf::Key::Num8)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage2 = 8;
- else if ((Input.IsKeyDown(sf::Key::Numpad9) || Input.IsKeyDown(sf::Key::Num9)) && (Input.IsKeyDown(sf::Key::LControl)) || Input.IsKeyDown(sf::Key::RControl))
- selectedimage2 = 9;
- Offset=Time*640;
- if (Input.IsKeyDown(sf::Key::Left))
- View.Move(-Offset, 0);
- if (Input.IsKeyDown(sf::Key::Right))
- View.Move(Offset,0);
- if (Input.IsKeyDown(sf::Key::Up))
- View.Move(0,-Offset);
- if (Input.IsKeyDown(sf::Key::Down))
- View.Move(0,Offset);
- if ((Input.IsKeyDown(sf::Key::LControl) || Input.IsKeyDown(sf::Key::RControl)) && Input.IsKeyDown(sf::Key::S))
- Save_File(array, xx, yy);// Ctrl+S will Save
- if ((Input.IsKeyDown(sf::Key::LControl) || Input.IsKeyDown(sf::Key::RControl)) && Input.IsKeyDown(sf::Key::L))
- {
- //cout << "bad";
- delete[] tiles;
- //cout << "nevermind";
- char * memblock = PreLoad();
- xx = memblock[0];
- yy = memblock[1];
- delete[] memblock;
- char * tiles;
- tiles = new char [xx * yy]; // this array will store the image number for each tile
- array = &tiles[0];
- Load_File(array);// Ctrl+L will Load
- }
- /*
- =========================================
- =========================================
- =========================================
- EDIT CODE HERE
- =========================================
- =========================================
- */
- // Clear screen
- App.Clear();
- App2.Clear();
- // Draw stuff on main window
- for (y=0; y<yy; y++)
- {
- for (x=0; x<xx; x++)
- {
- if (*(array + x + (x * y))==-1)
- continue;
- imgs[*(array + x + (x * y))].SetPosition(x*64, y*64);
- App.Draw(imgs[*(array + x + (x * y))]);
- }
- }
- // Draw stuff on the image list
- for (a=0; a<numofimgs; a++)
- {
- imgs[a].SetPosition(0, a*64);
- App2.Draw(imgs[a]);
- }
- Selection.SetPosition(0,64*selectedimage);
- App2.Draw(Selection);
- Selection2.SetPosition(0,64*selectedimage2);
- App2.Draw(Selection2);
- // Update the window
- App.Display();
- App2.Display();
- }
- delete[] tiles;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement