Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.67 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <vector>
  5. #define BLOCKSIZE 64
  6.  
  7.  
  8. int map_h[1];
  9. int map_w[1];
  10.  
  11. void Draw(sf::RenderWindow &Window, std::vector<std::vector<int>> &tab, sf::Texture &tile)
  12. {
  13.     sf::Sprite block;
  14.     block.setTexture(tile);
  15.  
  16.     for(int i = 0; i < map_h[0]; i++)
  17.     {
  18.         for(int j = 0; j < map_w[0]; j++)
  19.         {
  20.    
  21.             if(tab[i][j] == 1){
  22.                 //block.setTexture(ttab[0]);
  23.                 block.setTextureRect(sf::IntRect(1*BLOCKSIZE,0,BLOCKSIZE,BLOCKSIZE));
  24.                 block.setPosition(i * BLOCKSIZE, j * BLOCKSIZE);
  25.                
  26.             }
  27.             else if (tab[i][j] == 2 || tab[i][j] == 0){
  28.                 //block.setTexture(ttab[1]);
  29.                 block.setTextureRect(sf::IntRect(2*BLOCKSIZE,0,BLOCKSIZE,BLOCKSIZE));
  30.                 block.setPosition(i * BLOCKSIZE, j * BLOCKSIZE);
  31.                
  32.             }
  33.             else if (tab[i][j] == 3){
  34.                 //block.setTexture(ttab[2]);
  35.                 block.setTextureRect(sf::IntRect(3*BLOCKSIZE,0,BLOCKSIZE,BLOCKSIZE));
  36.                 block.setPosition(i * BLOCKSIZE, j * BLOCKSIZE);
  37.             }
  38.             else if (tab[i][j] == 100){
  39.                 //block.setTexture(ttab[2]);
  40.                 block.setTextureRect(sf::IntRect(4*BLOCKSIZE,0,BLOCKSIZE,BLOCKSIZE));
  41.                 block.setPosition(i * BLOCKSIZE, j * BLOCKSIZE);
  42.             }
  43.             Window.draw(block);
  44.         }
  45.     }
  46. }
  47.  
  48.  
  49. int main()
  50. {
  51.     std::vector<std::vector<int>> Map;
  52.     std::vector<int> tmpv;
  53.     map_h[0]=10;
  54.     map_w[0]=10;
  55.  
  56.     for(int i=0;i<map_h[0];i++)
  57.     {
  58.         for(int j=0;j<map_w[0];j++)
  59.         {
  60.            
  61.             tmpv.push_back(0); 
  62.         }
  63.         Map.push_back(tmpv);
  64.         tmpv.clear();
  65.     }
  66.  
  67.  
  68.     int tmp[1];
  69.     int move_x=0, move_y=0;
  70.     int actual_block=0;
  71.     int mouse_x=0, mouse_y=0;
  72.     int deltas=0;
  73.     sf::Clock Clock;
  74.     sf::Texture t_tab[4];
  75.     sf::Texture ramka;
  76.     sf::Sprite sramka;
  77.     sf::Texture tile;
  78.     if(ramka.loadFromFile("Data/ramka.png"))
  79.         sramka.setTexture(ramka);
  80.    
  81.  
  82.  
  83.  
  84.     if(!tile.loadFromFile("Data/tile.png"))
  85.             {
  86.                 std::cerr<<"Error loading tile.png"<<std::endl;
  87.                 return -1;
  88.             }
  89.  
  90.  
  91.     sf::View mainView;
  92.     sf::View Hud;
  93.     sf::View BigView;
  94.     sf::Sprite smouse;
  95.     smouse.setTexture(t_tab[actual_block]);
  96.     smouse.setOrigin(2,2);
  97.  
  98.     sf::Font Verdana;
  99.     Verdana.loadFromFile("Data/verdanab.ttf");
  100.  
  101.     sf::Text s1,s2,s3,s4;
  102.     s1.setFont(Verdana);
  103.     s1.setColor(sf::Color::White);
  104.     s1.setCharacterSize(14);
  105.     s1.setString("Q/A - Wybierz Sprite");
  106.  
  107.     s2.setFont(Verdana);
  108.     s2.setColor(sf::Color::White);
  109.     s2.setCharacterSize(14);
  110.     s2.setString("W/S - Oddal/Przybliz");
  111.  
  112.     s3.setFont(Verdana);
  113.     s3.setColor(sf::Color::White);
  114.     s3.setCharacterSize(14);
  115.     s3.setString("Sprite:");
  116.  
  117.     s4.setFont(Verdana);
  118.     s4.setColor(sf::Color::White);
  119.     s4.setCharacterSize(14);
  120.     s4.setString("X - Poczatkowe ustawienie");
  121.  
  122.     s1.setPosition(20,100);
  123.     s2.setPosition(20,125);
  124.     s3.setPosition(20,35);
  125.     s4.setPosition(20,150);
  126.  
  127.     sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "TileEditor", !sf::Style::Resize | sf::Style::Close);
  128.  
  129.     Window.setFramerateLimit(60);
  130.     //Window.setMouseCursorVisible(false); // Hide the cursor
  131.    
  132.     mainView.setSize(800*0.65,600*0.85);
  133.     mainView.setCenter(256,256);
  134.     mainView.setViewport(sf::FloatRect(0.05,0.1,0.65,0.85));
  135.     BigView.setSize(800,600);
  136.     BigView.setCenter(401,301);
  137.     BigView.setViewport(sf::FloatRect(0,0,1,1));
  138.  
  139.     Hud.setSize(800*0.3,600);
  140.     Hud.setCenter(((800*0.3)/2),300);
  141.     Hud.setViewport(sf::FloatRect(0.7,0,0.3,1));
  142.  
  143.     smouse.setPosition(100,10);
  144.     smouse.setTexture(tile);
  145.  
  146.     while (Window.isOpen())
  147.     {
  148.         sf::Event Event;
  149.         while(Window.pollEvent(Event))
  150.         {
  151.             if(Event.type == sf::Event::Closed || Event.key.code == sf::Keyboard::Escape)
  152.                 Window.close();
  153.         }
  154.    
  155.  
  156.         if(Event.type == sf::Event::MouseButtonPressed)
  157.         {
  158.             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){
  159.  
  160.             if (Event.mouseButton.button == sf::Mouse::Left)
  161.             {
  162.                 mouse_x=(Event.mouseButton.x+move_x+20)/64-1;
  163.                 mouse_y=(Event.mouseButton.y+move_y)/64-1;
  164.                 if(mouse_x>=0 && mouse_x<map_w[0] && mouse_y>=0 && mouse_y<map_h[0])
  165.                 {
  166.                     if(actual_block==3)
  167.                         Map[mouse_x][mouse_y]=100;
  168.                     else
  169.                         Map[mouse_x][mouse_y]=actual_block+1;
  170.                
  171.                 }
  172.                
  173.  
  174.                 std::cout<<move_x<<" "<<move_y<<" "<<mouse_x<<" "<<mouse_y<<std::endl;
  175.             }
  176.        
  177.             }
  178.         }
  179.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  180.         {
  181.             //mainView.setCenter(mainView.getCenter().x-1,mainView.getCenter().y);
  182.             mainView.move(-10,0);
  183.             move_x-=10;
  184.         }
  185.         else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  186.         {
  187.             //mainView.setCenter(mainView.getCenter().x,mainView.getCenter().y-1);
  188.             mainView.move(0,-10);
  189.             move_y-=10;
  190.         }
  191.         else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  192.         {
  193.             //mainView.setCenter(mainView.getCenter().x+1,mainView.getCenter().y);
  194.             mainView.move(10,0);
  195.             move_x+=10;
  196.         }
  197.         else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  198.         {
  199.             //mainView.setCenter(mainView.getCenter().x,mainView.getCenter().y+1);
  200.             mainView.move(0,10);
  201.             move_y+=10;
  202.         }
  203.  
  204.        
  205.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
  206.         {
  207.             if(Clock.getElapsedTime().asMilliseconds()>50)
  208.                 actual_block++;
  209.             Clock.restart();
  210.         }
  211.         else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
  212.         {
  213.             if(Clock.getElapsedTime().asMilliseconds()>50)
  214.                 actual_block--;
  215.             Clock.restart();
  216.         }
  217.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
  218.         {
  219.             mainView.zoom(1.1f);
  220.         }
  221.         else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
  222.         {
  223.             mainView.zoom(0.9f);
  224.         }
  225.         else if(sf::Keyboard::isKeyPressed(sf::Keyboard::X))
  226.         {
  227.             mainView.setSize(800*0.65,600*0.85);
  228.         }
  229.         if(actual_block<0) actual_block=0;
  230.         if(actual_block>3) actual_block=3;
  231.         //std::cout<<Event.mouseMove.x<<" "<<Event.mouseMove.y<<std::endl;
  232.         //std::cout<<Clock.getElapsedTime().asMilliseconds()<<std::endl;
  233.        
  234.         //smouse.setTexture(t_tab[actual_block]);
  235.        
  236.         smouse.setTextureRect(sf::IntRect((actual_block+1)*BLOCKSIZE,0,BLOCKSIZE,BLOCKSIZE));
  237.  
  238.         Window.clear();
  239.  
  240.         Window.setView(BigView);
  241.         Window.draw(sramka);
  242.  
  243.         Window.setView(mainView);
  244.         Draw(Window,Map,tile);
  245.  
  246.         Window.setView(Hud);
  247.         Window.draw(smouse);
  248.         Window.draw(s1);
  249.         Window.draw(s2);
  250.         Window.draw(s3);
  251.         Window.draw(s4);
  252.         Window.display();
  253.     }
  254. //  std::ofstream plik_out("Data/level1.txt");
  255. //  plik_out<<MAP_WIDTH<<" "<<MAP_HEIGHT<<"\n";
  256. //  for(int i=0;i<MAP_HEIGHT;i++)
  257. //  {
  258. //      for (int j=0;j<MAP_WIDTH;j++)
  259. //      {
  260. //          plik_out<<tab[j][i]<<" ";
  261. //      }
  262. //      plik_out<<"\n";
  263. //  }
  264. //  plik_out.close();
  265.  
  266.     FILE *fp = fopen("level.lvl","w+b");
  267.     if(fp != NULL)
  268.     {
  269.  
  270.         fwrite(map_w, sizeof(int), 1 , fp);
  271.         fwrite(map_h, sizeof(int), 1 , fp);
  272.         for(int i=0;i<map_h[0];i++)
  273.         {
  274.             for(int j=0;j<map_w[0];j++)
  275.             {
  276.                 tmp[0]=Map[j][i];
  277.                 fwrite(tmp, sizeof(int), 1 , fp);
  278.                    
  279.             }
  280.        
  281.         }
  282.  
  283.     fclose(fp);
  284.    
  285.  
  286.     }
  287.     return 0;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement