Advertisement
Guest User

Untitled

a guest
Sep 20th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <time.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <sstream>
  6. #include <fstream>
  7.  
  8. const int worldWidth = 30;
  9. const int worldHeight = 10;
  10. int topOfTheWorld = 5;
  11. int xPos = 0;
  12. int yPos = 0;
  13. int scene[worldWidth][worldHeight];
  14. int status;
  15. std::fstream fstr;
  16.  
  17. std::string NumberToString(int number) {
  18.  
  19. std::stringstream ss;
  20. std::string s;
  21.  
  22. ss << number;
  23. ss >> s;
  24.  
  25. return s;
  26.  
  27. }
  28.  
  29. std::string ruta(int id) {
  30.  
  31. if (id=='0') return "air";
  32. if (id=='1') return "stone";
  33. if (id=='2') return "dirt";
  34. if (id=='3') return "grass_side";
  35. if (id=='4') return "coal_ore";
  36.  
  37. }
  38.  
  39. void generar() {
  40.  
  41. bool finished = false;
  42.     while (!finished) {
  43.         if (yPos >= topOfTheWorld) {
  44.             scene[xPos][yPos] = 1;
  45.         } else if(yPos < topOfTheWorld) {
  46.             scene[xPos][yPos] = 0;
  47.         }
  48.  
  49.         yPos++;
  50.  
  51.         if(yPos == worldHeight) {
  52.             yPos = 0;
  53.             xPos++;
  54.         }
  55.  
  56.         if (xPos>worldWidth) {
  57.             finished = true;
  58.             goto Guardar;
  59.         }
  60.     }
  61.  
  62. Guardar:
  63.  
  64. std::ofstream output("mapa.txt");
  65.     for(int y=0;y<worldHeight;y++) {
  66.         for(int x=0;x<worldWidth;x++) {
  67.             output<<scene[x][y];
  68.  
  69.             if(x<(worldWidth-1)){output<<",";}
  70.         }
  71.         if(y<(worldHeight-1)){output<<std::endl;}
  72.     }
  73.  
  74. }
  75.  
  76. int main()
  77. {
  78.  
  79.     sf::RenderWindow window(sf::VideoMode(800, 600), "Prueba de caca");
  80.  
  81.     while (window.isOpen())
  82.     {
  83.         sf::Event event;
  84.         while (window.pollEvent(event))
  85.         {
  86.             if (event.type == sf::Event::Closed)
  87.                 window.close();
  88.  
  89.             if (event.type == sf::Event::MouseButtonPressed)
  90.             {
  91.                 if (event.mouseButton.button == sf::Mouse::Left)
  92.                 {
  93.                     generar();
  94.                 }
  95.             }
  96.  
  97.         }
  98.  
  99.         window.clear(sf::Color(0,223,215));
  100.  
  101. fstr.open("mapa.txt");
  102.  
  103. char mapa[worldWidth-1][worldHeight-1];
  104. int x = 0, y = 0;
  105. char c;
  106. while(fstr.good())
  107. {
  108.     c = fstr.get();
  109.     if (int(c) != -1 && c!= ',' && c!= '\n') {
  110.         mapa[x][y] = c;
  111.         x++;
  112.     }
  113.     if (c=='\n')
  114.     {
  115.         x = 0;
  116.         y++;
  117.     }
  118. }
  119.  
  120.         for (int x = 0; x<(worldWidth-1); x++) {
  121.             for (int y = 0; y<(worldHeight-1); y++) {
  122.                 sf::Texture texture;
  123.                 if (!texture.loadFromFile("images/blocks/" + ruta(mapa[y][x]) + ".png"))
  124.                 return -1;
  125.  
  126.                 sf::RectangleShape rectCaja(sf::Vector2f(16, 16));
  127.                 rectCaja.setPosition(sf::Vector2f(x*16, y*16));
  128.                 rectCaja.setTexture(&texture);
  129.                 window.draw(rectCaja);
  130.             }
  131.         }
  132.  
  133.         window.display();
  134.  
  135.     }
  136.  
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement