Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //================================WORLD.H=======================================
- #ifndef __WORLD_H__
- #define __WORLD_H__
- #include "globals.h"
- #include <vector>
- class Region;
- class ShaderProgram;
- class World
- {
- public:
- static ShaderProgram* BlockRenderProgram;
- private:
- Region**** _regions; //A 3D ARRAY OF POINTERS, MUAH HA HA HA HA HA
- uint8 _width, _height, _depth;
- //For loading the world using multiple threads, width of world in regions, start height in reagions, end height in regions, depth of world in regions
- void load(int width, int start, int end, int depth);
- public:
- World(uint8 w, uint8 h, uint8 d);
- ~World();
- bool InRegionBounds(uint8 x, uint8 y, uint8 z);
- bool InBlockBounds(uint16 x, uint16 y, uint16 z);
- const Region* GetRegionAt(uint8 x, uint8 y, uint8 z);
- uint16 GetBlockAt(uint16 x, uint16 y, uint16 z);
- void SetBlockAt(uint16 x, uint16 y, uint16 z, uint16 id);
- uint8 GetVisibilityAt(uint16 x, uint16 y, uint16 z);
- void SetVisibilityAt(uint16 x, uint16 y, uint16 z, uint16 face, bool visible);
- bool IsVisible(uint16 x, uint16 y, uint16 z, uint16 face);
- uint8 GetRegionWidth();
- uint8 GetRegionHeight();
- uint8 GetRegionDepth();
- uint16 GetBlockWidth();
- uint16 GetBlockHeight();
- uint16 GetBlockDepth();
- void Update(float elapsed);
- void Render(float elapsed);
- uint32 GetTotalTriangles();
- };
- #endif
- //=========================================WORLD.CPP===========================================
- #include "world.h"
- #include "region.h"
- #include "graphics\shader_program.h"
- #include "graphics\camera.h"
- #include "GLFW\glfw3.h"
- #include <thread>
- ShaderProgram* World::BlockRenderProgram = nullptr;
- void World::load(int width, int start, int end, int depth)
- {
- for (int x = 0; x < width; x++)
- for (int y = start; y <= end; y++)
- for (int z = 0; z < depth; z++)
- _regions[x][y][z] = new Region(this, x, y, z);
- }
- World::World(uint8 w, uint8 h, uint8 d)
- {
- if (!BlockRenderProgram)
- BlockRenderProgram = new ShaderProgram("data\\shaders\\BlockBasic.vert", "data\\shaders\\BlockBasic.frag");
- if (h % 4 != 0)
- h += (4 - (h % 4));
- //Allocate region data
- _regions = new Region***[w];
- for (int i = 0; i < w; i++)
- {
- _regions[i] = new Region**[h];
- for (int j = 0; j < h; j++)
- _regions[i][j] = new Region*[d];
- }
- _width = w;
- _height = h;
- _depth = d;
- int qh = _height / 4;
- int h1 = 0, h2 = qh, h3 = 2*qh, h4 = 3*qh;
- std::thread t1(std::bind(&World::load, this, w, h1, h2 - 1, d));
- std::thread t2(std::bind(&World::load, this, w, h2, h3 - 1, d));
- std::thread t3(std::bind(&World::load, this, w, h3, h4 - 1, d));
- std::thread t4(std::bind(&World::load, this, w, h4, h - 1, d));
- t1.join();
- t2.join();
- t3.join();
- t4.join();
- FOR(i,500)
- SetBlockAt(rand() % (REGION_DIM * _width), rand() % (REGION_DIM * _height), rand() % (REGION_DIM * _depth), 1);
- SetBlockAt(0, 0, 0, 1);
- }
- World::~World()
- {
- //TODO implement region saving
- //Deallocate region data
- for (int i = 0; i < _height; i++)
- {
- for (int j = 0; j < _width; j++)
- delete [] _regions[i][j];
- delete [] _regions[i];
- }
- delete [] _regions;
- }
- void World::Update(float elapsed)
- {
- for (int x = 0; x < _width; x++)
- for (int y = 0; y < _height; y++)
- for (int z = 0; z < _depth; z++)
- _regions[x][y][z]->Update();
- }
- void World::Render(float elapsed)
- {
- BlockRenderProgram->SetActive();
- BlockRenderProgram->Uniform1i("blockAtlas", 0);
- BlockRenderProgram->UniformMat4("projectionMat", 1, false, Camera::GetActiveCamera().GetProjectionMatrix());
- BlockRenderProgram->UniformMat4("viewMat", 1, false, Camera::GetActiveCamera().GetViewMatrix());
- for (int x = 0; x < _width; x++)
- for (int y = 0; y < _height; y++)
- for (int z = 0; z < _depth; z++)
- _regions[x][y][z]->Render();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement