Open Source Tomb Raider Engine
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TextureManager.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*!
  2. * \file include/TextureManager.h
  3. * \brief Texture registry
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #ifndef _TEXTURE_MANAGER_H
  9. #define _TEXTURE_MANAGER_H
  10. #include <cstdint>
  11. #include <vector>
  12. #include <glm/vec2.hpp>
  13. // These are loaded into TextureStorage::SYSTEM by initialize()!
  14. #define TEXTURE_WHITE 0
  15. #define TEXTURE_SPLASH 1
  16. class TextureTileVertex {
  17. public:
  18. TextureTileVertex(uint8_t xc, uint8_t xp, uint8_t yc, uint8_t yp)
  19. : xCoordinate(xc), xPixel(xp), yCoordinate(yc), yPixel(yp) { }
  20. uint8_t xCoordinate, xPixel;
  21. uint8_t yCoordinate, yPixel;
  22. };
  23. class TextureTile {
  24. public:
  25. TextureTile(unsigned int a, unsigned int t) : attribute(a), texture(t) { }
  26. void add(TextureTileVertex t) { vertices.push_back(t); }
  27. unsigned int getTexture() { return texture; }
  28. glm::vec2 getUV(unsigned int i);
  29. private:
  30. unsigned int attribute;
  31. unsigned int texture;
  32. std::vector<TextureTileVertex> vertices;
  33. };
  34. /*!
  35. * \brief Texture registry
  36. */
  37. class TextureManager {
  38. public:
  39. enum class ColorMode {
  40. RGB,
  41. RGBA,
  42. ARGB,
  43. BGR,
  44. BGRA
  45. };
  46. enum class TextureStorage {
  47. GAME,
  48. SYSTEM
  49. };
  50. TextureManager() : nextFreeTextureUnit(0) { }
  51. ~TextureManager();
  52. int initialize();
  53. int initializeSplash();
  54. void clear();
  55. int numTextures(TextureStorage s = TextureStorage::GAME);
  56. /*!
  57. * \brief Bind texture to next free texture unit.
  58. * \param n ID of texture to bind
  59. * \param s Place where texture is stored
  60. * \returns ID of GL texture unit to which this texture is bound.
  61. */
  62. int bindTexture(unsigned int n, TextureStorage s);
  63. /*!
  64. * \brief Loads Buffer as texture
  65. * \param image pixmap matching other params
  66. * \param width width of image
  67. * \param height height of image
  68. * \param mode mode of image
  69. * \param bpp bits per pixel of image
  70. * \param s Which TextureStorage should be accessed
  71. * \param slot slot (ID) of image
  72. * \param filter if the texture should be mipmap filtered
  73. * \returns texture ID or < 0 on error
  74. */
  75. int loadBufferSlot(unsigned char* image = nullptr,
  76. unsigned int width = 256, unsigned int height = 256,
  77. ColorMode mode = ColorMode::RGBA, unsigned int bpp = 32,
  78. TextureStorage s = TextureStorage::GAME,
  79. int slot = -1, bool filter = true);
  80. int loadImage(std::string filename, TextureStorage s = TextureStorage::GAME, int slot = -1);
  81. void addTile(TextureTile* t);
  82. int numTiles();
  83. TextureTile& getTile(int index);
  84. void addAnimatedTile(int index, int tile);
  85. int numAnimatedTiles();
  86. int getFirstTileAnimation(int index);
  87. int getNextTileAnimation(int tile);
  88. private:
  89. std::vector<unsigned int>& getIds(TextureStorage s);
  90. std::vector<int>& getUnits(TextureStorage s);
  91. void bindTextureId(unsigned int n, TextureStorage s, unsigned int unit);
  92. int loadPCX(std::string filename, TextureStorage s, int slot);
  93. std::vector<unsigned int> mTextureIdsGame;
  94. std::vector<unsigned int> mTextureIdsSystem;
  95. std::vector<TextureTile*> tiles;
  96. std::vector<std::vector<int>> animations;
  97. std::vector<int> gameUnits;
  98. std::vector<int> systemUnits;
  99. unsigned int nextFreeTextureUnit;
  100. };
  101. TextureManager& getTextureManager();
  102. #endif