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.

TextureManager.h 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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) { return glm::vec2(vertices.at(i).xPixel / 255.0f,
  29. vertices.at(i).yPixel / 255.0f); }
  30. private:
  31. unsigned int attribute;
  32. unsigned int texture;
  33. std::vector<TextureTileVertex> vertices;
  34. };
  35. /*!
  36. * \brief Texture registry
  37. */
  38. class TextureManager {
  39. public:
  40. enum class ColorMode {
  41. RGB,
  42. RGBA,
  43. ARGB,
  44. BGR,
  45. BGRA
  46. };
  47. enum class TextureStorage {
  48. GAME,
  49. SYSTEM
  50. };
  51. ~TextureManager();
  52. int initialize();
  53. int initializeSplash();
  54. void clear();
  55. int numTextures(TextureStorage s = TextureStorage::GAME);
  56. /*!
  57. * \brief Binds the texture for use in GL
  58. * \param n valid texture index
  59. * \param s Which TextureStorage should be accessed
  60. * \param unit Which GL texture unit should be used
  61. */
  62. void bindTextureId(unsigned int n, TextureStorage s = TextureStorage::GAME, unsigned int unit = 0);
  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(const char* 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. int loadTGA(const char* filename, TextureStorage s, int slot);
  91. int loadPCX(const char* filename, TextureStorage s, int slot);
  92. int loadPNG(const char* 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. };
  98. TextureManager& getTextureManager();
  99. #endif