Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <SFML/Audio.hpp>
- #include <SFML/Graphics.hpp>
- class Chunk
- {
- public:
- static const int ChunkWidth = 32;
- static const int ChunkHeight = 8;
- int x; // TODO: сделать функцию вместо переменной
- void SetChunkBlock(int y, int x, int blockId);
- int GetChunkBlock(int y, int x);
- private:
- int blocks[ChunkHeight][ChunkWidth] = { 0 };
- };
- class ChunkVertexArrayGenerator
- {
- public:
- static void Generate(
- Chunk &chunk,
- sf::VertexArray &va,
- sf::Texture &text,
- int blockWidth,
- int blockHeight);
- static void get_texture_tile_by_index(
- const sf::Texture &texture,
- const unsigned int &tileWidth,
- const unsigned int &tileHeight,
- const unsigned int &&tileIndex,
- sf::Vector2f &uv1,
- sf::Vector2f &uv2,
- sf::Vector2f &uv3,
- sf::Vector2f &uv4);
- };
- class World
- {
- public:
- Chunk chunks[6];
- void setBlock(int y, int x, int blockId);
- int getBlock(int y, int x);
- };
- int ZerosInChunk(Chunk &chunk)
- {
- int count = 0;
- for (int i = 0; i < Chunk::ChunkHeight; i++)
- {
- for (int j = 0; j < Chunk::ChunkWidth; j++)
- {
- if (chunk.GetChunkBlock(i, j) == 0)
- count++;
- }
- }
- return count;
- }
- int main()
- {
- srand(static_cast<unsigned int>(time(NULL)));
- const int W = 800;
- const int H = 600;
- sf::RenderWindow window(sf::VideoMode(W, H), "modificated-suffering");
- window.setFramerateLimit(60);
- sf::Texture tilemap;
- //if (!tilemap.loadFromFile("/Users/powerful-mac/Desktop/Main_Project/sfml-vertices/sfml-vertices/tilemap.jpg"))
- if (!tilemap.loadFromFile("tilemap.jpg"))
- {
- std::cout << "Error" << std::endl;
- }
- World world;
- world.chunks[0].x = 0;
- world.chunks[1].x = 1;
- sf::VertexArray vertices, v2;
- const int blockWidth = 64;
- const int blockHeight = 64;
- for (int i = 0; i < Chunk::ChunkHeight; i++)
- {
- for (int j = 0; j < Chunk::ChunkWidth; j++)
- {
- if ((i + j) % 2 == 0)
- world.chunks[0].SetChunkBlock(i, j, 5);
- else
- world.chunks[0].SetChunkBlock(i, j, 1);
- world.chunks[1].SetChunkBlock(i, j, 4);
- }
- }
- world.chunks[0].SetChunkBlock(0, 0, 5);
- ChunkVertexArrayGenerator::Generate(world.chunks[0], v2, tilemap, blockWidth, blockHeight);
- ChunkVertexArrayGenerator::Generate(world.chunks[1], vertices, tilemap, blockWidth, blockHeight);
- sf::Event event{};
- while (window.isOpen())
- {
- short int mouseButtonPressed = 0; // -1 if left; 1 if right
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
- {
- window.close();
- }
- if (event.type == sf::Event::MouseButtonPressed)
- {
- if (event.mouseButton.button == sf::Mouse::Left)
- mouseButtonPressed += 1;
- else if (event.mouseButton.button == sf::Mouse::Right)
- mouseButtonPressed -= 1;
- sf::Vector2i Mouse = sf::Mouse::getPosition(window);
- const sf::Vector2f result = window.mapPixelToCoords(Mouse/*, view*/);
- const int blockX = Mouse.x / blockWidth;
- const int blockY = Mouse.y / blockHeight;
- const int ChunkX = Mouse.x / Chunk::ChunkWidth;
- std::cout << "MouseX = " << Mouse.x << std::endl;
- std::cout << "MouseY = " << Mouse.y << std::endl;
- world.setBlock(blockY, blockX, 0);
- std::cout << "Chunk #" << ChunkX << " -> " << blockX << " : " << blockY << " DELETED" << std::endl;
- ChunkVertexArrayGenerator::Generate(world.chunks[0], v2, tilemap, blockWidth, blockHeight);
- ChunkVertexArrayGenerator::Generate(world.chunks[1], vertices, tilemap, blockWidth, blockHeight);
- }
- }
- window.clear(sf::Color(50, 153, 200));
- window.draw(v2, &tilemap);
- window.draw(vertices, &tilemap);
- window.display();
- }
- return EXIT_SUCCESS;
- }
- void Chunk::SetChunkBlock(int y, int x, int blockId)
- {
- blocks[y][x] = blockId;
- }
- int Chunk::GetChunkBlock(int y, int x)
- {
- return blocks[y][x];
- }
- void ChunkVertexArrayGenerator::Generate(
- Chunk &chunk,
- sf::VertexArray &va,
- sf::Texture &text,
- const int blockWidth,
- const int blockHeight)
- {
- const int NewVertexArraySize = (Chunk::ChunkHeight * Chunk::ChunkWidth - ZerosInChunk(chunk)) * 4;
- va.clear();
- va.resize(NewVertexArraySize);
- va.setPrimitiveType(sf::PrimitiveType::Quads);
- //
- // new generator
- //
- const int chunkPosX = chunk.x * Chunk::ChunkWidth * blockWidth;
- int index = 0;
- for (int y = 0; y < Chunk::ChunkHeight; y++)
- {
- for (int x = 0; x < Chunk::ChunkWidth; x++)
- {
- const int blockId = chunk.GetChunkBlock(y, x);
- if (blockId != 0)
- {
- // bottom-left vertex
- va[index + 0].position = sf::Vector2f(chunkPosX + x * blockWidth, (y + 1) * blockHeight);
- // top-left vertex
- va[index + 1].position = sf::Vector2f(chunkPosX + x * blockWidth, y * blockHeight);
- // top-right vertex
- va[index + 2].position = sf::Vector2f(chunkPosX + (x + 1) * blockWidth, y * blockHeight);
- // bottom-right vertex
- va[index + 3].position = sf::Vector2f(chunkPosX + (x + 1) * blockWidth, (y + 1) * blockHeight);
- //
- //
- //
- get_texture_tile_by_index(
- text,
- blockWidth,
- blockHeight,
- static_cast<unsigned int>(blockId),
- va[index + 0].texCoords,
- va[index + 1].texCoords,
- va[index + 2].texCoords,
- va[index + 3].texCoords);
- index += 4;
- }
- }
- }
- }
- void ChunkVertexArrayGenerator::get_texture_tile_by_index(
- const sf::Texture &texture,
- const unsigned int &tileWidth,
- const unsigned int &tileHeight,
- const unsigned int &&tileIndex,
- sf::Vector2f &uv1,
- sf::Vector2f &uv2,
- sf::Vector2f &uv3,
- sf::Vector2f &uv4)
- {
- const unsigned int textureWidth = texture.getSize().x; // 1-----2
- const unsigned int elementsInRow = textureWidth / tileWidth; // | |
- const unsigned int tileRowIndex = tileIndex / elementsInRow; // | |
- const unsigned int tileColIndex = tileIndex % elementsInRow; // 0-----3
- uv1 = sf::Vector2f(tileColIndex * tileWidth, tileHeight * tileRowIndex + tileHeight);
- uv2 = sf::Vector2f(tileColIndex * tileWidth, tileHeight * tileRowIndex);
- uv3 = sf::Vector2f(tileColIndex * tileWidth + tileWidth, tileHeight * tileRowIndex);
- uv4 = sf::Vector2f(tileColIndex * tileWidth + tileWidth, tileHeight * tileRowIndex + tileHeight);
- }
- void World::setBlock(int y, int x, int blockId)
- {
- chunks[x / Chunk::ChunkWidth].SetChunkBlock(y, x % Chunk::ChunkWidth, blockId);
- }
- int World::getBlock(int y, int x)
- {
- return chunks[x / Chunk::ChunkWidth].GetChunkBlock(y, x % Chunk::ChunkWidth);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement