gcsx_texture.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* GCSx
  2. ** TEXTURE.H
  3. **
  4. ** Texture-management for OpenGL
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #ifndef __GCSx_TEXTURE_H_
  24. #define __GCSx_TEXTURE_H_
  25. // TextureMap class: Allocates textures to graphics as efficiently as possible.
  26. // Internally, may be any number of textures, subtextures of one or more textures,
  27. // share subtextures from other textures, etc.
  28. class TextureMap {
  29. private:
  30. int dispWidth;
  31. int dispHeight;
  32. int graphicCount;
  33. int multiTexture; // if >1, more than one texture per graphic (due to size)
  34. struct TexturePos {
  35. // Texture number
  36. GLuint tex;
  37. // Sub-texture number (not part of OpenGL)
  38. // < 0 = self-contained texture, not in global list
  39. int subtex;
  40. // Position within texture
  41. GLfloat x1, x2, y1, y2;
  42. GLint x, y;
  43. };
  44. TexturePos* graphics;
  45. // Cached updates
  46. std::map<int, Uint8*> updates;
  47. // Global texture list for textures with >1 sub-texture
  48. struct TextureList {
  49. GLuint tex;
  50. int subtexW, subtexH;
  51. int subtexCount;
  52. std::map<int, int> inUse;
  53. };
  54. static std::vector<TextureList>* globalTextures;
  55. // Upscale to nearest power of two IF textures must be powers of two
  56. // Also clips to minSize (but only asserts for maxSize!)
  57. static int optionalPowerOfTwo(int val);
  58. // All existing texture maps that need updating form a circular double-linked list
  59. // A standalone texture needing updating points to itself
  60. // Textures that don't need to update have NULL pointers here
  61. TextureMap* nextUpdate;
  62. TextureMap* prevUpdate;
  63. static TextureMap* headUpdate;
  64. void removeFromUpdates();
  65. // These optimizations assume all texture display goes through a Texture class
  66. static GLuint lastTexture;
  67. public:
  68. // How many graphics of what size?
  69. // Can optionally pass surface/function that will provide all surface info now
  70. // (will pass numbers starting at 1)
  71. // EXPECTS SURFACE32s
  72. TextureMap(int count, int width, int height,
  73. void (*tileCoords)(int position, const SDL_Surface*& src, int& x, int& y) = NULL);
  74. ~TextureMap();
  75. // Store a graphic- does not retain pointer
  76. // Indexed starting at 1 to correspond to tiles, sprites, etc.
  77. // Assumes 32bit RGBA surface
  78. // Must call updateTexture() when done with all updates
  79. void store(int graphic, const SDL_Surface* src, int sx = 0, int sy = 0, int dx = 0, int dy = 0, int dw = -1, int dh = -1);
  80. // Perform any/all texture updates now, for this texture map only
  81. void updateTexture();
  82. // Perform all texture updates for ALL existing texture maps
  83. static void updateAllTextures();
  84. // Draws graphic onscreen
  85. // Indexed starting at 1 to correspond to tiles, sprites, etc.
  86. void draw(int graphic, GLint x, GLint y) const;
  87. void drawScale(int graphic, GLfloat x, GLfloat y, GLfloat scale) const;
  88. // orient matches LAYER_TILE_* constants (flip, mirror, rotate)
  89. enum {
  90. TEXTURE_FLIP = 0x10000000,
  91. TEXTURE_MIRROR = 0x20000000,
  92. TEXTURE_ROTATE = 0x40000000,
  93. };
  94. void draw(int graphic, GLint x, GLint y, int orient) const;
  95. // Draws graphic with clipping hints (ignorable)
  96. // (clipping in screen coordinates, not graphic coordinates)
  97. void draw(int graphic, GLint x, GLint y, Rect clip) const;
  98. // Given X textures of YxZ, detemine best size(s) of textures to store them on
  99. // Doesn't work for textures larger than largest texture size
  100. // Public for testing purposes
  101. struct tGroup {
  102. int texW, texH, countW, countH;
  103. // Number of this texture size to do
  104. int qty;
  105. };
  106. // (returns total texture count)
  107. static int groupTextures(int count, int w, int h, std::vector<tGroup>& sizes);
  108. // Must call when initializing GL to set up optimizations
  109. // (currently no 'exit' function is needed)
  110. static void setupOptimizations();
  111. };
  112. #endif