Open Source Tomb Raider Engine
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TextureManager.h 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <array>
  11. #include <tuple>
  12. #include <vector>
  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(int xc, int xp, int yc, int yp)
  19. : xCoordinate(xc), xPixel(xp), yCoordinate(yc), yPixel(yp) { }
  20. int xCoordinate, xPixel;
  21. int 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. enum class ColorMode {
  35. RGB,
  36. RGBA,
  37. ARGB,
  38. BGR,
  39. BGRA
  40. };
  41. enum class TextureStorage {
  42. GAME,
  43. SYSTEM
  44. };
  45. class BufferManager {
  46. public:
  47. BufferManager(int t, TextureStorage s) : texture(t), storage(s) { }
  48. int getTextureID() { return texture; }
  49. TextureStorage getTextureStorage() { return storage; }
  50. private:
  51. int texture;
  52. TextureStorage storage;
  53. };
  54. class TextureManager {
  55. public:
  56. static int initialize();
  57. static int initializeSplash();
  58. static void shutdown();
  59. static void clear();
  60. static void prepare();
  61. static int numTextures(TextureStorage s = TextureStorage::GAME);
  62. /*!
  63. * \brief Bind texture to next free texture unit.
  64. * \param n ID of texture to bind
  65. * \param s Place where texture is stored
  66. * \returns ID of GL texture unit to which this texture is bound.
  67. */
  68. static int bindTexture(unsigned int n, TextureStorage s);
  69. /*!
  70. * \brief Loads Buffer as texture
  71. * \param image pixmap matching other params
  72. * \param width width of image
  73. * \param height height of image
  74. * \param mode mode of image
  75. * \param bpp bits per pixel of image
  76. * \param s Which TextureStorage should be accessed
  77. * \param slot slot (ID) of image
  78. * \param filter if the texture should be mipmap filtered
  79. * \returns texture ID or < 0 on error
  80. */
  81. static int loadBufferSlot(unsigned char* image = nullptr,
  82. unsigned int width = 256, unsigned int height = 256,
  83. ColorMode mode = ColorMode::RGBA, unsigned int bpp = 32,
  84. TextureStorage s = TextureStorage::GAME,
  85. int slot = -1, bool filter = true);
  86. static int loadImage(std::string filename, TextureStorage s = TextureStorage::GAME, int slot = -1);
  87. static void addTile(TextureTile* t);
  88. static int numTiles();
  89. static TextureTile& getTile(int index);
  90. static void addAnimatedTile(int index, int tile);
  91. static int numAnimatedTiles();
  92. static int getFirstTileAnimation(int index);
  93. static int getNextTileAnimation(int index, int tile);
  94. static BufferManager* getBufferManager(int tex, TextureStorage store);
  95. static void display();
  96. static unsigned int getTextureID(int n, TextureStorage s);
  97. static void setPalette(int index, glm::vec4 color);
  98. static glm::vec4 getPalette(int index);
  99. static void addIndexedTexture(unsigned char* image, unsigned int width, unsigned int height);
  100. private:
  101. static std::vector<unsigned int>& getIds(TextureStorage s);
  102. static std::vector<int>& getUnits(TextureStorage s);
  103. static void bindTextureId(unsigned int n, TextureStorage s, unsigned int unit);
  104. static int loadPCX(std::string filename, TextureStorage s, int slot);
  105. static std::vector<unsigned int> mTextureIdsGame;
  106. static std::vector<unsigned int> mTextureIdsSystem;
  107. static std::vector<TextureTile*> tiles;
  108. static std::vector<std::vector<int>> animations;
  109. static std::vector<int> gameUnits;
  110. static std::vector<int> systemUnits;
  111. static unsigned int nextFreeTextureUnit;
  112. static std::vector<BufferManager> gameBuffers;
  113. static std::vector<BufferManager> systemBuffers;
  114. static std::array<glm::vec4, 256> colorPalette;
  115. static std::vector<std::tuple<unsigned char*, unsigned int, unsigned int>> indexedTextures;
  116. };
  117. #endif