Guest User

Untitled

a guest
Oct 30th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include "block.h"
  2. #include "graphics\texture_loader.h"
  3.  
  4. #include <GL\glew.h>
  5.  
  6. uint32 Block::textureID = 0;
  7. bool Block::initialized = false;
  8. std::map<uint16, Block> Block::idMap = std::map<uint16, Block>();
  9. std::map<std::string, Block> Block::nameMap = std::map<std::string, Block>();
  10.  
  11. uint32 Block::GetTextureID()
  12. {
  13.     return textureID;
  14. }
  15.  
  16. bool Block::IdInUse(uint16 id)
  17. {
  18.     return idMap.find(id) != idMap.end();
  19. }
  20.  
  21. const Block& Block::GetBlock(uint16 id)
  22. {
  23.     if (!IdInUse(id))
  24.     {
  25.         fprintf(stderr, "Could not find Block with ID %u.", (uint32)id);
  26.         throw std::runtime_error("Could not find Block with passed id.");
  27.     }
  28.  
  29.     return idMap.at(id);
  30. }
  31.  
  32. bool Block::NameInUse(std::string name)
  33. {
  34.     return nameMap.find(name) != nameMap.end();
  35. }
  36.  
  37. const Block& Block::GetBlock(std::string name)
  38. {
  39.     if (!NameInUse(name))
  40.     {
  41.         fprintf(stderr, "Could not find Block with name s.", name);
  42.         throw std::runtime_error("Could not find Block with passed name.");
  43.     }
  44.  
  45.     return nameMap.at(name);
  46. }
  47.  
  48. //========================================Block=============================================
  49. Block::Block(std::string name, uint16 id, uint8 tex)
  50. {
  51.     //Check for repeat ids
  52.     if (IdInUse(id))
  53.     {
  54.         fprintf(stderr, "Block id %u is already in use!", (uint32)id);
  55.         throw std::runtime_error("You cannot reuse block ids!");
  56.     }
  57.     _id = id;
  58.     idMap.insert(std::make_pair(id, *this));
  59.     //Check for repeat names
  60.     if (NameInUse(name))
  61.     {
  62.         fprintf(stderr, "Block name %s is already in use!", name);
  63.         throw std::runtime_error("You cannot reuse block names!");
  64.     }
  65.     _name = name;
  66.     nameMap.insert(std::make_pair(name, *this));
  67.  
  68.     _tex = tex;
  69.     fprintf(stdout, "Using texture %u\n", _tex);
  70.  
  71.     _transparent = false;
  72. }
  73.  
  74. uint16 Block::GetID() const
  75. {
  76.     return _id;
  77. }
  78.  
  79. uint8 Block::GetIndex() const
  80. {
  81.     fprintf(stdout, "Returning texture %u\n", _tex);
  82.     return _tex;
  83. }
  84.  
  85. std::string Block::GetName() const
  86. {
  87.     return _name;
  88. }
  89.  
  90. bool Block::IsTransparent() const
  91. {
  92.     return _transparent;
  93. }
  94.  
  95. void Block::setTransparent(bool b)
  96. {
  97.     _transparent = b;
  98. }
  99.  
  100. //=========Virtual Methods===========
  101.  
  102. //=========================================================================================
  103.  
  104. //=================================STATIC BLOCK INITIALIZATION=======================================
  105. const Block Block::Test = Block("Test", 1, 0);
  106.  
  107. //=========================BLOCK DATA============================
  108. #define V1 glm::vec3(0.0f,0.0f,0.0f)
  109. #define V2 glm::vec3(1.0f,0.0f,0.0f)
  110. #define V3 glm::vec3(1.0f,0.0f,1.0f)
  111. #define V4 glm::vec3(0.0f,0.0f,1.0f)
  112. #define V5 glm::vec3(0.0f,1.0f,0.0f)
  113. #define V6 glm::vec3(1.0f,1.0f,0.0f)
  114. #define V7 glm::vec3(1.0f,1.0f,1.0f)
  115. #define V8 glm::vec3(0.0f,1.0f,1.0f)
  116. #define NXP glm::vec3(1.0f,0.0f,0.0f)
  117. #define NXN glm::vec3(-1.0f,0.0f,0.0f)
  118. #define NYP glm::vec3(0.0f,1.0f,0.0f)
  119. #define NYN glm::vec3(0.0f,-1.0f,0.0f)
  120. #define NZP glm::vec3(0.0f,0.0f,1.0f)
  121. #define NZN glm::vec3(0.0f,0.0f,-1.0f)
  122.  
  123. const glm::vec3 Block::VERT_X_POS[6] = { V7, V6, V3, V6, V2, V3 };
  124. const glm::vec3 Block::VERT_X_NEG[6] = { V5, V8, V1, V8, V4, V1 };
  125. const glm::vec3 Block::VERT_Y_POS[6] = { V5, V6, V8, V6, V7, V8 };
  126. const glm::vec3 Block::VERT_Y_NEG[6] = { V1, V4, V2, V4, V3, V2 };
  127. const glm::vec3 Block::VERT_Z_POS[6] = { V8, V7, V4, V7, V3, V4 };
  128. const glm::vec3 Block::VERT_Z_NEG[6] = { V6, V5, V2, V5, V1, V2 };
  129. const glm::vec3 Block::NORM_X_POS = NXP;
  130. const glm::vec3 Block::NORM_X_NEG = NXN;
  131. const glm::vec3 Block::NORM_Y_POS = NYP;
  132. const glm::vec3 Block::NORM_Y_NEG = NYN;
  133. const glm::vec3 Block::NORM_Z_POS = NZP;
  134. const glm::vec3 Block::NORM_Z_NEG = NZN;
Advertisement
Add Comment
Please, Sign In to add comment