Advertisement
DarkoreXOR

Untitled

May 1st, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <SFML/Audio.hpp>
  5. #include <SFML/Graphics.hpp>
  6.  
  7. class Chunk
  8. {
  9. public:
  10.     static const int ChunkWidth = 32;
  11.     static const int ChunkHeight = 8;
  12.  
  13.     int x; // TODO: сделать функцию вместо переменной
  14.  
  15.     void SetChunkBlock(int y, int x, int blockId);
  16.  
  17.     int GetChunkBlock(int y, int x);
  18.  
  19. private:
  20.     int blocks[ChunkHeight][ChunkWidth] = { 0 };
  21. };
  22.  
  23. class ChunkVertexArrayGenerator
  24. {
  25. public:
  26.     static void Generate(
  27.         Chunk &chunk,
  28.         sf::VertexArray &va,
  29.         sf::Texture &text,
  30.         int blockWidth,
  31.         int blockHeight);
  32.  
  33.     static void get_texture_tile_by_index(
  34.         const sf::Texture &texture,
  35.         const unsigned int &tileWidth,
  36.         const unsigned int &tileHeight,
  37.         const unsigned int &&tileIndex,
  38.         sf::Vector2f &uv1,
  39.         sf::Vector2f &uv2,
  40.         sf::Vector2f &uv3,
  41.         sf::Vector2f &uv4);
  42. };
  43.  
  44. class World
  45. {
  46. public:
  47.     Chunk chunks[6];
  48.  
  49.     void setBlock(int y, int x, int blockId);
  50.     int getBlock(int y, int x);
  51. };
  52.  
  53.  
  54. int ZerosInChunk(Chunk &chunk)
  55. {
  56.     int count = 0;
  57.     for (int i = 0; i < Chunk::ChunkHeight; i++)
  58.     {
  59.         for (int j = 0; j < Chunk::ChunkWidth; j++)
  60.         {
  61.             if (chunk.GetChunkBlock(i, j) == 0)
  62.                 count++;
  63.         }
  64.     }
  65.     return count;
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. int main()
  85. {
  86.     srand(static_cast<unsigned int>(time(NULL)));
  87.  
  88.     const int W = 800;
  89.     const int H = 600;
  90.  
  91.     sf::RenderWindow window(sf::VideoMode(W, H), "modificated-suffering");
  92.  
  93.     window.setFramerateLimit(60);
  94.  
  95.     sf::Texture tilemap;
  96.  
  97.     //if (!tilemap.loadFromFile("/Users/powerful-mac/Desktop/Main_Project/sfml-vertices/sfml-vertices/tilemap.jpg"))
  98.     if (!tilemap.loadFromFile("tilemap.jpg"))
  99.     {
  100.         std::cout << "Error" << std::endl;
  101.     }
  102.  
  103.     World world;
  104.  
  105.     world.chunks[0].x = 0;
  106.     world.chunks[1].x = 1;
  107.  
  108.     sf::VertexArray vertices, v2;
  109.  
  110.     const int blockWidth = 64;
  111.     const int blockHeight = 64;
  112.  
  113.     for (int i = 0; i < Chunk::ChunkHeight; i++)
  114.     {
  115.         for (int j = 0; j < Chunk::ChunkWidth; j++)
  116.         {
  117.             if ((i + j) % 2 == 0)
  118.                 world.chunks[0].SetChunkBlock(i, j, 5);
  119.             else
  120.                 world.chunks[0].SetChunkBlock(i, j, 1);
  121.  
  122.             world.chunks[1].SetChunkBlock(i, j, 4);
  123.         }
  124.     }
  125.  
  126.     world.chunks[0].SetChunkBlock(0, 0, 5);
  127.  
  128.  
  129.     ChunkVertexArrayGenerator::Generate(world.chunks[0], v2, tilemap, blockWidth, blockHeight);
  130.     ChunkVertexArrayGenerator::Generate(world.chunks[1], vertices, tilemap, blockWidth, blockHeight);
  131.  
  132.     sf::Event event{};
  133.  
  134.     while (window.isOpen())
  135.     {
  136.         short int mouseButtonPressed = 0;    // -1 if left;  1 if right
  137.         while (window.pollEvent(event))
  138.         {
  139.             if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
  140.             {
  141.                 window.close();
  142.             }
  143.  
  144.             if (event.type == sf::Event::MouseButtonPressed)
  145.             {
  146.                 if (event.mouseButton.button == sf::Mouse::Left)
  147.                     mouseButtonPressed += 1;
  148.  
  149.                 else if (event.mouseButton.button == sf::Mouse::Right)
  150.                     mouseButtonPressed -= 1;
  151.  
  152.                 sf::Vector2i Mouse = sf::Mouse::getPosition(window);
  153.                 const sf::Vector2f result = window.mapPixelToCoords(Mouse/*, view*/);
  154.  
  155.                 const int blockX = Mouse.x / blockWidth;
  156.                 const int blockY = Mouse.y / blockHeight;
  157.                 const int ChunkX = Mouse.x / Chunk::ChunkWidth;
  158.  
  159.                 std::cout << "MouseX = " << Mouse.x << std::endl;
  160.                 std::cout << "MouseY = " << Mouse.y << std::endl;
  161.  
  162.                 world.setBlock(blockY, blockX, 0);
  163.  
  164.                 std::cout << "Chunk #" << ChunkX << " -> " << blockX << " : " << blockY << " DELETED" << std::endl;
  165.  
  166.                 ChunkVertexArrayGenerator::Generate(world.chunks[0], v2, tilemap, blockWidth, blockHeight);
  167.                 ChunkVertexArrayGenerator::Generate(world.chunks[1], vertices, tilemap, blockWidth, blockHeight);
  168.             }
  169.         }
  170.  
  171.         window.clear(sf::Color(50, 153, 200));
  172.         window.draw(v2, &tilemap);
  173.         window.draw(vertices, &tilemap);
  174.         window.display();
  175.  
  176.     }
  177.  
  178.     return EXIT_SUCCESS;
  179. }
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. void Chunk::SetChunkBlock(int y, int x, int blockId)
  188. {
  189.     blocks[y][x] = blockId;
  190. }
  191.  
  192. int Chunk::GetChunkBlock(int y, int x)
  193. {
  194.     return blocks[y][x];
  195. }
  196.  
  197. void ChunkVertexArrayGenerator::Generate(
  198.     Chunk &chunk,
  199.     sf::VertexArray &va,
  200.     sf::Texture &text,
  201.     const int blockWidth,
  202.     const int blockHeight)
  203. {
  204.     const int NewVertexArraySize = (Chunk::ChunkHeight * Chunk::ChunkWidth - ZerosInChunk(chunk)) * 4;
  205.  
  206.     va.clear();
  207.     va.resize(NewVertexArraySize);
  208.     va.setPrimitiveType(sf::PrimitiveType::Quads);
  209.  
  210.     //
  211.     // new generator
  212.     //
  213.  
  214.     const int chunkPosX = chunk.x * Chunk::ChunkWidth * blockWidth;
  215.  
  216.     int index = 0;
  217.  
  218.     for (int y = 0; y < Chunk::ChunkHeight; y++)
  219.     {
  220.         for (int x = 0; x < Chunk::ChunkWidth; x++)
  221.         {
  222.             const int blockId = chunk.GetChunkBlock(y, x);
  223.  
  224.             if (blockId != 0)
  225.             {
  226.                 // bottom-left vertex
  227.  
  228.                 va[index + 0].position = sf::Vector2f(chunkPosX + x * blockWidth, (y + 1) * blockHeight);
  229.  
  230.                 // top-left vertex
  231.  
  232.                 va[index + 1].position = sf::Vector2f(chunkPosX + x * blockWidth, y * blockHeight);
  233.  
  234.                 // top-right vertex
  235.  
  236.                 va[index + 2].position = sf::Vector2f(chunkPosX + (x + 1) * blockWidth, y * blockHeight);
  237.  
  238.                 // bottom-right vertex
  239.  
  240.                 va[index + 3].position = sf::Vector2f(chunkPosX + (x + 1) * blockWidth, (y + 1) * blockHeight);
  241.  
  242.                 //
  243.                 //
  244.                 //
  245.  
  246.                 get_texture_tile_by_index(
  247.                     text,
  248.                     blockWidth,
  249.                     blockHeight,
  250.                     static_cast<unsigned int>(blockId),
  251.                     va[index + 0].texCoords,
  252.                     va[index + 1].texCoords,
  253.                     va[index + 2].texCoords,
  254.                     va[index + 3].texCoords);
  255.  
  256.                 index += 4;
  257.             }
  258.         }
  259.     }
  260. }
  261.  
  262. void ChunkVertexArrayGenerator::get_texture_tile_by_index(
  263.     const sf::Texture &texture,
  264.     const unsigned int &tileWidth,
  265.     const unsigned int &tileHeight,
  266.     const unsigned int &&tileIndex,
  267.     sf::Vector2f &uv1,
  268.     sf::Vector2f &uv2,
  269.     sf::Vector2f &uv3,
  270.     sf::Vector2f &uv4)
  271. {
  272.     const unsigned int textureWidth = texture.getSize().x;        //  1-----2
  273.     const unsigned int elementsInRow = textureWidth / tileWidth;  //  |     |
  274.     const unsigned int tileRowIndex = tileIndex / elementsInRow;  //  |     |
  275.     const unsigned int tileColIndex = tileIndex % elementsInRow;  //  0-----3
  276.     uv1 = sf::Vector2f(tileColIndex * tileWidth, tileHeight * tileRowIndex + tileHeight);
  277.     uv2 = sf::Vector2f(tileColIndex * tileWidth, tileHeight * tileRowIndex);
  278.     uv3 = sf::Vector2f(tileColIndex * tileWidth + tileWidth, tileHeight * tileRowIndex);
  279.     uv4 = sf::Vector2f(tileColIndex * tileWidth + tileWidth, tileHeight * tileRowIndex + tileHeight);
  280. }
  281.  
  282. void World::setBlock(int y, int x, int blockId)
  283. {
  284.     chunks[x / Chunk::ChunkWidth].SetChunkBlock(y, x % Chunk::ChunkWidth, blockId);
  285. }
  286.  
  287. int World::getBlock(int y, int x)
  288. {
  289.     return chunks[x / Chunk::ChunkWidth].GetChunkBlock(y, x % Chunk::ChunkWidth);
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement