Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <fstream>
- #include <iostream>
- #include <vector>
- #define BLOCKSIZE 64
- int map_h[1];
- int map_w[1];
- void Draw(sf::RenderWindow &Window, std::vector<std::vector<int>> &tab, sf::Texture &tile)
- {
- sf::Sprite block;
- block.setTexture(tile);
- for(int i = 0; i < map_h[0]; i++)
- {
- for(int j = 0; j < map_w[0]; j++)
- {
- if(tab[i][j] == 1){
- //block.setTexture(ttab[0]);
- block.setTextureRect(sf::IntRect(1*BLOCKSIZE,0,BLOCKSIZE,BLOCKSIZE));
- block.setPosition(i * BLOCKSIZE, j * BLOCKSIZE);
- }
- else if (tab[i][j] == 2 || tab[i][j] == 0){
- //block.setTexture(ttab[1]);
- block.setTextureRect(sf::IntRect(2*BLOCKSIZE,0,BLOCKSIZE,BLOCKSIZE));
- block.setPosition(i * BLOCKSIZE, j * BLOCKSIZE);
- }
- else if (tab[i][j] == 3){
- //block.setTexture(ttab[2]);
- block.setTextureRect(sf::IntRect(3*BLOCKSIZE,0,BLOCKSIZE,BLOCKSIZE));
- block.setPosition(i * BLOCKSIZE, j * BLOCKSIZE);
- }
- else if (tab[i][j] == 100){
- //block.setTexture(ttab[2]);
- block.setTextureRect(sf::IntRect(4*BLOCKSIZE,0,BLOCKSIZE,BLOCKSIZE));
- block.setPosition(i * BLOCKSIZE, j * BLOCKSIZE);
- }
- Window.draw(block);
- }
- }
- }
- int main()
- {
- std::vector<std::vector<int>> Map;
- std::vector<int> tmpv;
- map_h[0]=10;
- map_w[0]=10;
- for(int i=0;i<map_h[0];i++)
- {
- for(int j=0;j<map_w[0];j++)
- {
- tmpv.push_back(0);
- }
- Map.push_back(tmpv);
- tmpv.clear();
- }
- int tmp[1];
- int move_x=0, move_y=0;
- int actual_block=0;
- int mouse_x=0, mouse_y=0;
- int deltas=0;
- sf::Clock Clock;
- sf::Texture t_tab[4];
- sf::Texture ramka;
- sf::Sprite sramka;
- sf::Texture tile;
- if(ramka.loadFromFile("Data/ramka.png"))
- sramka.setTexture(ramka);
- if(!tile.loadFromFile("Data/tile.png"))
- {
- std::cerr<<"Error loading tile.png"<<std::endl;
- return -1;
- }
- sf::View mainView;
- sf::View Hud;
- sf::View BigView;
- sf::Sprite smouse;
- smouse.setTexture(t_tab[actual_block]);
- smouse.setOrigin(2,2);
- sf::Font Verdana;
- Verdana.loadFromFile("Data/verdanab.ttf");
- sf::Text s1,s2,s3,s4;
- s1.setFont(Verdana);
- s1.setColor(sf::Color::White);
- s1.setCharacterSize(14);
- s1.setString("Q/A - Wybierz Sprite");
- s2.setFont(Verdana);
- s2.setColor(sf::Color::White);
- s2.setCharacterSize(14);
- s2.setString("W/S - Oddal/Przybliz");
- s3.setFont(Verdana);
- s3.setColor(sf::Color::White);
- s3.setCharacterSize(14);
- s3.setString("Sprite:");
- s4.setFont(Verdana);
- s4.setColor(sf::Color::White);
- s4.setCharacterSize(14);
- s4.setString("X - Poczatkowe ustawienie");
- s1.setPosition(20,100);
- s2.setPosition(20,125);
- s3.setPosition(20,35);
- s4.setPosition(20,150);
- sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "TileEditor", !sf::Style::Resize | sf::Style::Close);
- Window.setFramerateLimit(60);
- //Window.setMouseCursorVisible(false); // Hide the cursor
- mainView.setSize(800*0.65,600*0.85);
- mainView.setCenter(256,256);
- mainView.setViewport(sf::FloatRect(0.05,0.1,0.65,0.85));
- BigView.setSize(800,600);
- BigView.setCenter(401,301);
- BigView.setViewport(sf::FloatRect(0,0,1,1));
- Hud.setSize(800*0.3,600);
- Hud.setCenter(((800*0.3)/2),300);
- Hud.setViewport(sf::FloatRect(0.7,0,0.3,1));
- smouse.setPosition(100,10);
- smouse.setTexture(tile);
- while (Window.isOpen())
- {
- sf::Event Event;
- while(Window.pollEvent(Event))
- {
- if(Event.type == sf::Event::Closed || Event.key.code == sf::Keyboard::Escape)
- Window.close();
- }
- if(Event.type == sf::Event::MouseButtonPressed)
- {
- if(Event.mouseButton.x>800*0.05 && Event.mouseButton.x< 800*0.7 && Event.mouseButton.y>600*0.1 && Event.mouseButton.y< 600*0.95){
- if (Event.mouseButton.button == sf::Mouse::Left)
- {
- mouse_x=(Event.mouseButton.x+move_x+20)/64-1;
- mouse_y=(Event.mouseButton.y+move_y)/64-1;
- if(mouse_x>=0 && mouse_x<map_w[0] && mouse_y>=0 && mouse_y<map_h[0])
- {
- if(actual_block==3)
- Map[mouse_x][mouse_y]=100;
- else
- Map[mouse_x][mouse_y]=actual_block+1;
- }
- std::cout<<move_x<<" "<<move_y<<" "<<mouse_x<<" "<<mouse_y<<std::endl;
- }
- }
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- {
- //mainView.setCenter(mainView.getCenter().x-1,mainView.getCenter().y);
- mainView.move(-10,0);
- move_x-=10;
- }
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- {
- //mainView.setCenter(mainView.getCenter().x,mainView.getCenter().y-1);
- mainView.move(0,-10);
- move_y-=10;
- }
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- {
- //mainView.setCenter(mainView.getCenter().x+1,mainView.getCenter().y);
- mainView.move(10,0);
- move_x+=10;
- }
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
- {
- //mainView.setCenter(mainView.getCenter().x,mainView.getCenter().y+1);
- mainView.move(0,10);
- move_y+=10;
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
- {
- if(Clock.getElapsedTime().asMilliseconds()>50)
- actual_block++;
- Clock.restart();
- }
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
- {
- if(Clock.getElapsedTime().asMilliseconds()>50)
- actual_block--;
- Clock.restart();
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
- {
- mainView.zoom(1.1f);
- }
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
- {
- mainView.zoom(0.9f);
- }
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::X))
- {
- mainView.setSize(800*0.65,600*0.85);
- }
- if(actual_block<0) actual_block=0;
- if(actual_block>3) actual_block=3;
- //std::cout<<Event.mouseMove.x<<" "<<Event.mouseMove.y<<std::endl;
- //std::cout<<Clock.getElapsedTime().asMilliseconds()<<std::endl;
- //smouse.setTexture(t_tab[actual_block]);
- smouse.setTextureRect(sf::IntRect((actual_block+1)*BLOCKSIZE,0,BLOCKSIZE,BLOCKSIZE));
- Window.clear();
- Window.setView(BigView);
- Window.draw(sramka);
- Window.setView(mainView);
- Draw(Window,Map,tile);
- Window.setView(Hud);
- Window.draw(smouse);
- Window.draw(s1);
- Window.draw(s2);
- Window.draw(s3);
- Window.draw(s4);
- Window.display();
- }
- // std::ofstream plik_out("Data/level1.txt");
- // plik_out<<MAP_WIDTH<<" "<<MAP_HEIGHT<<"\n";
- // for(int i=0;i<MAP_HEIGHT;i++)
- // {
- // for (int j=0;j<MAP_WIDTH;j++)
- // {
- // plik_out<<tab[j][i]<<" ";
- // }
- // plik_out<<"\n";
- // }
- // plik_out.close();
- FILE *fp = fopen("level.lvl","w+b");
- if(fp != NULL)
- {
- fwrite(map_w, sizeof(int), 1 , fp);
- fwrite(map_h, sizeof(int), 1 , fp);
- for(int i=0;i<map_h[0];i++)
- {
- for(int j=0;j<map_w[0];j++)
- {
- tmp[0]=Map[j][i];
- fwrite(tmp, sizeof(int), 1 , fp);
- }
- }
- fclose(fp);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement