Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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