Advertisement
Guest User

block.h/.cpp

a guest
Oct 30th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. //================================WORLD.H=======================================
  2. #ifndef __WORLD_H__
  3. #define __WORLD_H__
  4.  
  5. #include "globals.h"
  6. #include <vector>
  7.  
  8. class Region;
  9. class ShaderProgram;
  10.  
  11. class World
  12. {
  13. public:
  14.     static ShaderProgram* BlockRenderProgram;
  15.  
  16. private:
  17.     Region**** _regions; //A 3D ARRAY OF POINTERS, MUAH HA HA HA HA HA
  18.     uint8 _width, _height, _depth;
  19.  
  20.     //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
  21.     void load(int width, int start, int end, int depth);
  22.  
  23. public:
  24.     World(uint8 w, uint8 h, uint8 d);
  25.     ~World();
  26.  
  27.     bool InRegionBounds(uint8 x, uint8 y, uint8 z);
  28.     bool InBlockBounds(uint16 x, uint16 y, uint16 z);
  29.     const Region* GetRegionAt(uint8 x, uint8 y, uint8 z);
  30.     uint16 GetBlockAt(uint16 x, uint16 y, uint16 z);
  31.     void SetBlockAt(uint16 x, uint16 y, uint16 z, uint16 id);
  32.     uint8 GetVisibilityAt(uint16 x, uint16 y, uint16 z);
  33.     void SetVisibilityAt(uint16 x, uint16 y, uint16 z, uint16 face, bool visible);
  34.     bool IsVisible(uint16 x, uint16 y, uint16 z, uint16 face);
  35.  
  36.     uint8 GetRegionWidth();
  37.     uint8 GetRegionHeight();
  38.     uint8 GetRegionDepth();
  39.     uint16 GetBlockWidth();
  40.     uint16 GetBlockHeight();
  41.     uint16 GetBlockDepth();
  42.  
  43.     void Update(float elapsed);
  44.     void Render(float elapsed);
  45.  
  46.     uint32 GetTotalTriangles();
  47. };
  48.  
  49. #endif
  50.  
  51.  
  52. //=========================================WORLD.CPP===========================================
  53. #include "world.h"
  54. #include "region.h"
  55.  
  56. #include "graphics\shader_program.h"
  57. #include "graphics\camera.h"
  58.  
  59. #include "GLFW\glfw3.h"
  60.  
  61. #include <thread>
  62.  
  63. ShaderProgram* World::BlockRenderProgram = nullptr;
  64.  
  65. void World::load(int width, int start, int end, int depth)
  66. {
  67.     for (int x = 0; x < width; x++)
  68.         for (int y = start; y <= end; y++)
  69.             for (int z = 0; z < depth; z++)
  70.                 _regions[x][y][z] = new Region(this, x, y, z);
  71. }
  72.  
  73. World::World(uint8 w, uint8 h, uint8 d)
  74. {
  75.     if (!BlockRenderProgram)
  76.         BlockRenderProgram = new ShaderProgram("data\\shaders\\BlockBasic.vert", "data\\shaders\\BlockBasic.frag");
  77.  
  78.     if (h % 4 != 0)
  79.         h += (4 - (h % 4));
  80.  
  81.     //Allocate region data
  82.     _regions = new Region***[w];
  83.     for (int i = 0; i < w; i++)
  84.     {
  85.         _regions[i] = new Region**[h];
  86.         for (int j = 0; j < h; j++)
  87.             _regions[i][j] = new Region*[d];
  88.     }
  89.  
  90.     _width = w;
  91.     _height = h;
  92.     _depth = d;
  93.  
  94.     int qh = _height / 4;
  95.     int h1 = 0, h2 = qh, h3 = 2*qh, h4 = 3*qh;
  96.  
  97.     std::thread t1(std::bind(&World::load, this, w, h1, h2 - 1, d));
  98.     std::thread t2(std::bind(&World::load, this, w, h2, h3 - 1, d));
  99.     std::thread t3(std::bind(&World::load, this, w, h3, h4 - 1, d));
  100.     std::thread t4(std::bind(&World::load, this, w, h4, h - 1, d));
  101.  
  102.     t1.join();
  103.     t2.join();
  104.     t3.join();
  105.     t4.join();
  106.  
  107.     FOR(i,500)
  108.         SetBlockAt(rand() % (REGION_DIM * _width), rand() % (REGION_DIM * _height), rand() % (REGION_DIM * _depth), 1);
  109.  
  110.     SetBlockAt(0, 0, 0, 1);
  111. }
  112.  
  113. World::~World()
  114. {
  115.     //TODO implement region saving
  116.  
  117.     //Deallocate region data
  118.     for (int i = 0; i < _height; i++)
  119.     {
  120.         for (int j = 0; j < _width; j++)
  121.             delete [] _regions[i][j];
  122.  
  123.         delete [] _regions[i];
  124.     }
  125.     delete [] _regions;
  126. }
  127.  
  128. void World::Update(float elapsed)
  129. {
  130.     for (int x = 0; x < _width; x++)
  131.         for (int y = 0; y < _height; y++)
  132.             for (int z = 0; z < _depth; z++)
  133.                 _regions[x][y][z]->Update();
  134. }
  135.  
  136. void World::Render(float elapsed)
  137. {
  138.     BlockRenderProgram->SetActive();
  139.     BlockRenderProgram->Uniform1i("blockAtlas", 0);
  140.     BlockRenderProgram->UniformMat4("projectionMat", 1, false, Camera::GetActiveCamera().GetProjectionMatrix());
  141.     BlockRenderProgram->UniformMat4("viewMat", 1, false, Camera::GetActiveCamera().GetViewMatrix());
  142.  
  143.     for (int x = 0; x < _width; x++)
  144.         for (int y = 0; y < _height; y++)
  145.             for (int z = 0; z < _depth; z++)
  146.                 _regions[x][y][z]->Render();
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement