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.

Texture.h 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*!
  2. * \file include/Texture.h
  3. * \brief Texture registry
  4. *
  5. * \author Mongoose
  6. */
  7. #ifndef _TEXTURE_H
  8. #define _TEXTURE_H
  9. #include <stdio.h>
  10. typedef struct {
  11. int x;
  12. int y;
  13. int w;
  14. int h;
  15. int minx;
  16. int maxx;
  17. int miny;
  18. int maxy;
  19. int advance;
  20. } ttf_glyph_t;
  21. typedef struct {
  22. unsigned int utf8Offset;
  23. unsigned int count;
  24. /* [utf8Offset -> utf8Offset+count],
  25. matches indexing into glyphs[0 -> count] for texcoords, etc
  26. ----------------------------------------
  27. 41 -> 126 ASCII English w/ special chars,
  28. 0x303f -> 0x3093 Japanese hiragana kana,
  29. 0x301a -> 0x30f6 Japanese katakana kana */
  30. unsigned int width; //!< Width and height of RGBA texture
  31. unsigned char *texture; //!< RGBA texture data
  32. ttf_glyph_t *glyphs; //!< For typesetting and rendering use
  33. int fontHeight;
  34. int fontAscent;
  35. int fontDescent;
  36. int fontSpacing;
  37. } ttf_texture_t;
  38. typedef struct {
  39. unsigned int utf8Offset;
  40. unsigned int count;
  41. int textureId;
  42. int drawListBase;
  43. } gl_font_t;
  44. /*!
  45. * \brief Texture registry
  46. */
  47. class Texture {
  48. public:
  49. enum ColorMode {
  50. GREYSCALE = 1,
  51. RGB,
  52. RGBA,
  53. ARGB
  54. };
  55. enum TextureFlag {
  56. fUseMipmaps = (1 << 0),
  57. fUseMultiTexture = (1 << 1),
  58. fUseSDL_TTF = (1 << 2)
  59. };
  60. /*!
  61. * \brief Constructs an object of Texture
  62. */
  63. Texture();
  64. /*!
  65. * \brief Deconstructs an object of Texture
  66. */
  67. ~Texture();
  68. /*!
  69. * \brief Generates a texture buffer with (width * height * 4) bytes.
  70. * \param rgba 32bpp RGBA color to fill into buffer
  71. * \param width width of newly allocated buffer, power of 2, pref same as height
  72. * \param height height of newly allocated buffer, power of 2, pref same as width
  73. * \returns newly allocated texture buffer filled with specified color
  74. */
  75. static unsigned char *generateColorTexture(unsigned char rgba[4],
  76. unsigned int width,
  77. unsigned int height);
  78. gl_font_t *generateFont(ttf_texture_t *texture);
  79. /*!
  80. * \brief Generates a font texture with typeset info from TTF.
  81. *
  82. * Does not load the texture itself! Call loadFont() on returned ttf_texture_t
  83. * \param filename filename of TTF font
  84. * \param pointSize Point Size to generate
  85. * \param textureWidth width of texture, height will match it
  86. * \param color RGB 24bit color
  87. * \param utf8Offset Offset into fonts encoding chart
  88. * \param count number of glyphs to read from offset start
  89. * \param verbose dumps debug info to stdout
  90. * \returns font texture. Load it with loadFont()!
  91. */
  92. ttf_texture_t *generateFontTexture(const char *filename, int pointSize,
  93. unsigned int textureWidth,
  94. unsigned char color[3],
  95. unsigned int utf8Offset,
  96. unsigned int count,
  97. char verbose);
  98. /*!
  99. * \brief Get number of textures in use
  100. * \returns used texture count, or -1 on error (uninitialized)
  101. */
  102. int getTextureCount();
  103. /*!
  104. * \brief Dumps a screenshot to disk.
  105. *
  106. * Avoids overwriting files with same base name.
  107. * \param base base filename
  108. * \param width viewport width
  109. * \param height viewport height
  110. */
  111. void glScreenShot(char *base, unsigned int width, unsigned int height);
  112. /*!
  113. * \brief Sets up multitexture rendering with passed ids
  114. * \param texture0 first texture for multitexture
  115. * \param texture1 second texture for multitexture
  116. */
  117. void bindMultiTexture(int texture0, int texture1);
  118. /*!
  119. * \brief Binds the texture for use in GL
  120. * \param n valid texture index
  121. */
  122. void bindTextureId(unsigned int n);
  123. /*!
  124. * \brief Clears an option flag
  125. * \param flag flag to clear
  126. */
  127. void clearFlag(TextureFlag flag);
  128. void disableMultiTexture();
  129. /*!
  130. * \brief Loads SDL_TTF
  131. */
  132. void initSDL_TTF();
  133. /*!
  134. * \brief Loads Buffer as texture
  135. * \param image pixmap matching other params
  136. * \param width width of image
  137. * \param height height of image
  138. * \param mode mode of image
  139. * \param bpp bits per pixel of image
  140. * \returns texture ID or < 0 on error
  141. */
  142. int loadBuffer(unsigned char *image,
  143. unsigned int width, unsigned int height,
  144. ColorMode mode, unsigned int bpp);
  145. /*!
  146. * \brief Loads Buffer as texture
  147. * \param image pixmap matching other params
  148. * \param width width of image
  149. * \param height height of image
  150. * \param mode mode of image
  151. * \param bpp bits per pixel of image
  152. * \param slot slot (ID) of image
  153. * \returns texture ID or < 0 on error
  154. */
  155. int loadBufferSlot(unsigned char *image,
  156. unsigned int width, unsigned int height,
  157. ColorMode mode, unsigned int bpp,
  158. unsigned int slot);
  159. /*!
  160. * \brief Generates and loads a solid color texture.
  161. * \param rgba color for new texture
  162. * \param width width of new texture
  163. * \param height height of new texture
  164. * \returns texture ID or -1 on error
  165. */
  166. int loadColorTexture(unsigned char rgba[4],
  167. unsigned int width, unsigned int height);
  168. /*!
  169. * \brief Loads a TTF, generates texture image, glyph list and drawlist
  170. * \param filename Filename of TTF font
  171. * \param utf8Offset Offset into Unicode table
  172. * \param count number of glyphs to load
  173. * \returns font id if successful, or < 0 on error
  174. */
  175. int loadFontTTF(const char *filename,
  176. unsigned int utf8Offset, unsigned int count);
  177. /*!
  178. * \brief Loads TGA file as texture
  179. * \param filename Existing TGA file
  180. * \returns ID of new texture or -1 on error
  181. */
  182. int loadTGA(const char *filename);
  183. /*!
  184. * \brief Resets all texture data
  185. */
  186. void reset();
  187. /*!
  188. * \brief Sets an option flag
  189. * \param flag flag to set
  190. */
  191. void setFlag(TextureFlag flag);
  192. /*!
  193. * \brief Sets up GL texturing.
  194. *
  195. * Must be called as first setup step!
  196. * \param n maximum number of textures you wish to allow
  197. */
  198. void setMaxTextureCount(unsigned int n);
  199. void useMultiTexture(float u, float v);
  200. void useMultiTexture(float aU, float aV, float bU, float bV);
  201. /*!
  202. * \brief Show a texture for a second, for debugging
  203. * \param textureId texture id to show
  204. * \returns GL texture id on success, < 0 on error
  205. */
  206. int showTextureDebug(int textureId);
  207. /*!
  208. * \brief Renders debug texture.
  209. *
  210. * Called in 2D mode from Texture::Display().
  211. */
  212. void debugTextureRender(int width, int height);
  213. private:
  214. int nextPower(int seed);
  215. unsigned char *scaleBuffer(unsigned char *image, int width, int height,
  216. int components);
  217. int textureDebug;
  218. int textureDebugFrames;
  219. unsigned int *mTextureIds; //!< GL texture list
  220. unsigned int mTextureCount; //!< Texture counter
  221. unsigned int mTextureLimit; //!< The texture limit
  222. unsigned int mFlags; //!< Class options
  223. int mTextureId; //!< Currently bound texture id
  224. int mTextureId2; //!< Multitexture Texture Id
  225. };
  226. // Experimental testing
  227. void bufferedPrintf(char *string, unsigned int len, char *s, ...) __attribute__((format(printf, 3, 4)));
  228. void glPrint3d(float x, float y, float z,
  229. float pitch, float yaw, float roll,
  230. float scale, char *string);
  231. void glPrint2d(float x, float y, float scale, char *string);
  232. /*!
  233. * \brief OpenGL ortho projection.
  234. *
  235. * Call before using glPrint2d()
  236. * \param width window width
  237. * \param height window height
  238. */
  239. void glEnterMode2d(unsigned int width, unsigned int height);
  240. /*!
  241. * \brief OpenGL model matrix projection.
  242. *
  243. * Call after using glPrint2d()
  244. */
  245. void glExitMode2d();
  246. #endif