Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Texture.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*==========================================================================
  3. *
  4. * Project : MTK, Freyja, OpenRaider
  5. * Author : Terry 'Mongoose' Hendrix II
  6. * Website : http://www.westga.edu/~stu7440
  7. * Email : stu7440@westga.edu
  8. * Object : MtkTexture
  9. * Comments: This is the Texture registery
  10. *
  11. * See file COPYING for license details.
  12. *
  13. *
  14. *-- History ----------------------------------------------------------
  15. *
  16. * 2003.06.30,
  17. * Mongoose - API update, SDL_TTF support moved here, misc features
  18. * SDL_TTF support based on Sam Lantinga's public domain
  19. * SDL_TTF demo functions and algorithms
  20. *
  21. * 2001.05.29:
  22. * Mongoose - Removed legacy code and done clean up
  23. *
  24. * 2001.02.19:
  25. * Mongoose - Moved from GooseEgg to mtk
  26. *
  27. * 2000.04.29:
  28. * Mongoose - Created from old PPM/PCX codebases I have done before
  29. ==========================================================================*/
  30. #ifndef _TEXTURE_H
  31. #define _TEXTURE_H
  32. #include <stdio.h>
  33. typedef struct
  34. {
  35. int x, y, w, h;
  36. int minx; int maxx; int miny; int maxy; int advance;
  37. } ttf_glyph_t;
  38. typedef struct
  39. {
  40. unsigned int utf8Offset;
  41. unsigned int count;
  42. /* [utf8Offset -> utf8Offset+count],
  43. matches indexing into glyphs[0 -> count] for texcoords, etc
  44. ----------------------------------------
  45. 41 -> 126 ASCII English w/ special chars,
  46. 0x303f -> 0x3093 Japanese hiragana kana,
  47. 0x301a -> 0x30f6 Japanese katakana kana */
  48. unsigned int width; /* Width and height of RGBA texture */
  49. unsigned char *texture; /* RGBA texture data */
  50. ttf_glyph_t *glyphs; /* For typesetting and rendering use */
  51. int fontHeight;
  52. int fontAscent;
  53. int fontDescent;
  54. int fontSpacing;
  55. } ttf_texture_t;
  56. typedef struct
  57. {
  58. unsigned int utf8Offset;
  59. unsigned int count;
  60. int textureId;
  61. int drawListBase;
  62. } gl_font_t;
  63. class Texture
  64. {
  65. public:
  66. enum ColorMode
  67. {
  68. GREYSCALE = 1,
  69. RGB,
  70. RGBA,
  71. ARGB
  72. };
  73. enum TextureFlag
  74. {
  75. fUseMipmaps = 1,
  76. fUseMultiTexture = 2,
  77. fUseSDL_TTF = 4
  78. };
  79. ////////////////////////////////////////////////////////////
  80. // Constructors
  81. ////////////////////////////////////////////////////////////
  82. Texture();
  83. /*------------------------------------------------------
  84. * Pre :
  85. * Post : Constructs an object of Texture
  86. *
  87. *-- History ------------------------------------------
  88. *
  89. * 2001.05.29:
  90. * Mongoose - Big code clean up, documentation
  91. *
  92. * 2000.10.05:
  93. * Mongoose - Created
  94. ------------------------------------------------------*/
  95. ~Texture();
  96. /*------------------------------------------------------
  97. * Pre : This object exists
  98. * Post : Deconstructs an object of Texture
  99. *
  100. *-- History ------------------------------------------
  101. *
  102. * 2001.05.29:
  103. * Mongoose - Big code clean up, documentation
  104. *
  105. * 2000.10.05:
  106. * Mongoose - Created
  107. ------------------------------------------------------*/
  108. ////////////////////////////////////////////////////////////
  109. // Public Accessors
  110. ////////////////////////////////////////////////////////////
  111. static unsigned char *generateColorTexture(unsigned char rgba[4],
  112. unsigned int width,
  113. unsigned int height);
  114. /*------------------------------------------------------
  115. * Pre : <Rgba> is 32bpp RGBA color
  116. * <Width> and <Height> are powers of two, pref
  117. * the same number
  118. * Post :
  119. *
  120. *-- History ------------------------------------------
  121. *
  122. * 2003.06.30:
  123. * Mongoose - Created
  124. ------------------------------------------------------*/
  125. gl_font_t *generateFont(ttf_texture_t *texture);
  126. /*------------------------------------------------------
  127. * Pre :
  128. * Post :
  129. *
  130. *-- History ------------------------------------------
  131. *
  132. * 2003.06.30:
  133. * Mongoose - Created
  134. ------------------------------------------------------*/
  135. ttf_texture_t *generateFontTexture(const char *filename, int pointSize,
  136. unsigned int textureWidth,
  137. unsigned char color[3],
  138. unsigned int utf8Offset,
  139. unsigned int count,
  140. char verbose);
  141. /*------------------------------------------------------
  142. * Pre : <Filename> of TTF font
  143. * <PointSize> to generate
  144. * <TextureWidth> is width of texture, height will match it
  145. * <Color> is RGB 24bit color
  146. * <Utf8Offset> is offset into font's encoding chart
  147. * <Count> is number of glyphs to read from offset start
  148. * <Verbose> dumps debug info to stdout
  149. *
  150. * Post : Generates a font texture with typeset info from TTF
  151. *
  152. * DOES NOT load the texture itself, call loadFont()
  153. * on returned ttf_texture_t
  154. *
  155. *
  156. *-- History ------------------------------------------
  157. *
  158. * 2003.06.03:
  159. * Mongoose - Created
  160. ------------------------------------------------------*/
  161. int getTextureCount();
  162. /*------------------------------------------------------
  163. * Pre :
  164. * Post : Returns number of textures in use, or -1 for
  165. * error ( Not initalized )
  166. *
  167. *-- History ------------------------------------------
  168. *
  169. * 2001.05.29:
  170. * Mongoose - Big code clean up, documentation
  171. *
  172. * 2000.10.05:
  173. * Mongoose - Created
  174. ------------------------------------------------------*/
  175. void glScreenShot(char *base, unsigned int width, unsigned int height);
  176. /*------------------------------------------------------
  177. * Pre : <Base> is base filename,
  178. * <Width> and <Height> are viewport dim
  179. *
  180. * Post : Dumps a screenshot to disk,
  181. * avoids overwriting files with same base name
  182. *
  183. *-- History ------------------------------------------
  184. *
  185. * 2002.06.16:
  186. * Mongoose - Created
  187. ------------------------------------------------------*/
  188. ////////////////////////////////////////////////////////////
  189. // Public Mutators
  190. ////////////////////////////////////////////////////////////
  191. void bindMultiTexture(int texture0, int texture1);
  192. /*------------------------------------------------------
  193. * Pre :
  194. * Post : Sets up multitexture rendering with passed texture ids
  195. *
  196. *-- History ------------------------------------------
  197. *
  198. * 2002.12.24:
  199. * Mongoose - Created
  200. ------------------------------------------------------*/
  201. void bindTextureId(unsigned int n);
  202. /*------------------------------------------------------
  203. * Pre : n is valid texture index
  204. * Post : Binds the texture for use in GL
  205. *
  206. *-- History ------------------------------------------
  207. *
  208. * 2001.05.29:
  209. * Mongoose - Big code clean up, documentation
  210. *
  211. * 2000.10.05:
  212. * Mongoose - Created
  213. ------------------------------------------------------*/
  214. void clearFlag(TextureFlag flag);
  215. /*------------------------------------------------------
  216. * Pre :
  217. * Post : CLears a option flag
  218. *
  219. *-- History ------------------------------------------
  220. *
  221. * 2003.01.05:
  222. * Mongoose - Created
  223. ------------------------------------------------------*/
  224. void disableMultiTexture();
  225. /*------------------------------------------------------
  226. * Pre :
  227. * Post :
  228. *
  229. *-- History ------------------------------------------
  230. *
  231. * 2002.12.24:
  232. * Mongoose - Created
  233. ------------------------------------------------------*/
  234. void initSDL_TTF();
  235. /*------------------------------------------------------
  236. * Pre :
  237. * Post : Loads SDL_TTF if avalible
  238. *
  239. *-- History ------------------------------------------
  240. *
  241. * 2003.06.03:
  242. * Mongoose - Created
  243. ------------------------------------------------------*/
  244. int loadBuffer(unsigned char *image,
  245. unsigned int width, unsigned int height,
  246. ColorMode mode, unsigned int bpp);
  247. /*------------------------------------------------------
  248. * Pre : image must be a valid pixmap that agrees
  249. * with mode, width, and height
  250. *
  251. * Post : Returns texture id or < 0 error flag
  252. *
  253. *-- History ------------------------------------------
  254. *
  255. * 2001.05.29:
  256. * Mongoose - Big code clean up, documentation
  257. *
  258. * 2000.10.05:
  259. * Mongoose - Created
  260. ------------------------------------------------------*/
  261. int loadBufferSlot(unsigned char *image,
  262. unsigned int width, unsigned int height,
  263. ColorMode mode, unsigned int bpp,
  264. unsigned int slot);
  265. /*------------------------------------------------------
  266. * Pre : image must be a valid pixmap that agrees
  267. * with mode, width, and height, slot ( ID )
  268. *
  269. * Post : Returns texture id or < 0 error flag
  270. *
  271. *-- History ------------------------------------------
  272. *
  273. * 2002.09.05:
  274. * Mongoose - Created
  275. ------------------------------------------------------*/
  276. int loadColorTexture(unsigned char rgba[4],
  277. unsigned int width, unsigned int height);
  278. /*------------------------------------------------------
  279. * Pre :
  280. * Post : Generates and loads a solid color texture,
  281. * returns texture Id or -1 if failed
  282. *
  283. *-- History ------------------------------------------
  284. *
  285. * 2003.06.30:
  286. * Mongoose - Created
  287. ------------------------------------------------------*/
  288. int loadFontTTF(const char *filename,
  289. unsigned int utf8Offset, unsigned int count);
  290. /*------------------------------------------------------
  291. * Pre : <Filename> of TTF font
  292. * <Utf8Offset> is offset into UNICODE table
  293. * <Count> is number of glyphs to load
  294. *
  295. * Post : Loads a TTF,
  296. * Generates: texture image, glyph list, and drawlist
  297. *
  298. * Returns font id if sucessful, or < 0 if error
  299. *
  300. *-- History ------------------------------------------
  301. *
  302. * 2003.07.05:
  303. * Mongoose - Created
  304. ------------------------------------------------------*/
  305. int loadPNG(const char *filename);
  306. /*------------------------------------------------------
  307. * Pre : Texture is init and filename/file is valid
  308. * Post : Loads PNG as texture and returns ID or -1 error
  309. *
  310. *-- History ------------------------------------------
  311. *
  312. * 2002.06.16:
  313. * Mongoose - Created, from Freyja
  314. ------------------------------------------------------*/
  315. int loadTGA(const char *filename);
  316. /*------------------------------------------------------
  317. * Pre : Texture is init and filename/file is valid
  318. * Post : Loads TGA as texture and returns ID or -1 error
  319. *
  320. *-- History ------------------------------------------
  321. *
  322. * 2002.06.16:
  323. * Mongoose - Created
  324. ------------------------------------------------------*/
  325. void reset();
  326. /*------------------------------------------------------
  327. * Pre :
  328. * Post : Resets all texture data
  329. *
  330. *-- History ------------------------------------------
  331. *
  332. * 2001.05.29:
  333. * Mongoose - Big code clean up, documentation
  334. *
  335. * 2000.10.05:
  336. * Mongoose - Created
  337. ------------------------------------------------------*/
  338. void setFlag(TextureFlag flag);
  339. /*------------------------------------------------------
  340. * Pre :
  341. * Post : Sets a option flag
  342. *
  343. *-- History ------------------------------------------
  344. *
  345. * 2003.01.05:
  346. * Mongoose - Created
  347. ------------------------------------------------------*/
  348. void setMaxTextureCount(unsigned int n);
  349. /*------------------------------------------------------
  350. * Pre : n is max number of textures you wish to allow
  351. * Post : Sets up GL texturing, and must be called
  352. * as the first setup step
  353. *
  354. *-- History ------------------------------------------
  355. *
  356. * 2001.05.29:
  357. * Mongoose - Big code clean up, documentation
  358. *
  359. * 2000.10.05:
  360. * Mongoose - Created
  361. ------------------------------------------------------*/
  362. void useMultiTexture(float u, float v);
  363. /*------------------------------------------------------
  364. * Pre :
  365. * Post :
  366. *
  367. *-- History ------------------------------------------
  368. *
  369. * 2002.12.24:
  370. * Mongoose - Created
  371. ------------------------------------------------------*/
  372. void useMultiTexture(float aU, float aV, float bU, float bV);
  373. /*------------------------------------------------------
  374. * Pre :
  375. * Post :
  376. *
  377. *-- History ------------------------------------------
  378. *
  379. * 2002.12.24:
  380. * Mongoose - Created
  381. ------------------------------------------------------*/
  382. private:
  383. ////////////////////////////////////////////////////////////
  384. // Private Accessors
  385. ////////////////////////////////////////////////////////////
  386. int nextPower(int seed);
  387. /*------------------------------------------------------
  388. * Pre :
  389. * Post :
  390. *
  391. *-- History ------------------------------------------
  392. *
  393. * 2002.06.16:
  394. * Mongoose - Created
  395. ------------------------------------------------------*/
  396. unsigned char *scaleBuffer(unsigned char *image, int width, int height,
  397. int components);
  398. /*------------------------------------------------------
  399. * Pre :
  400. * Post :
  401. *
  402. *-- History ------------------------------------------
  403. *
  404. * 2002.06.16:
  405. * Mongoose - Created
  406. ------------------------------------------------------*/
  407. ////////////////////////////////////////////////////////////
  408. // Private Mutators
  409. ////////////////////////////////////////////////////////////
  410. unsigned int *mTextureIds; /* GL texture list */
  411. unsigned int mTextureCount; /* Texture counter */
  412. unsigned int mTextureLimit; /* The texture limit */
  413. unsigned int mFlags; /* Class options */
  414. int mTextureId; /* Currently bound texture id */
  415. int mTextureId2; /* Multitexture Texture Id */
  416. };
  417. /* Experimental testing */
  418. void bufferedPrintf(char *string, unsigned int len, char *s, ...);
  419. void glPrint3d(float x, float y, float z,
  420. float pitch, float yaw, float roll,
  421. float scale,
  422. char *string);
  423. void glPrint2d(float x, float y, float scale, char *string);
  424. void glEnterMode2d(unsigned int width, unsigned int height);
  425. /*------------------------------------------------------
  426. * Pre :
  427. * Post : OpenGL ortho projection
  428. *
  429. *-- History ------------------------------------------
  430. *
  431. * 2003.06.03:
  432. * Mongoose - Created
  433. ------------------------------------------------------*/
  434. void glExitMode2d();
  435. /*------------------------------------------------------
  436. * Pre :
  437. * Post : OpenGL model matrix projection
  438. *
  439. *-- History ------------------------------------------
  440. *
  441. * 2003.06.03:
  442. * Mongoose - Created
  443. ------------------------------------------------------*/
  444. #endif