SpriteBatch.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * Copyright (c) 2011 Nokia Corporation.
  3. *
  4. * CAUTION: This class is in early, experimental state.
  5. *
  6. */
  7. #ifndef __SPRITEBATCH__
  8. #define __SPRITEBATCH__
  9. #include <memory.h>
  10. struct SpriteDrawInfo
  11. {
  12. SpriteDrawInfo() {
  13. sourcex = 0;
  14. sourcey = 0;
  15. sourceWidth = 1;
  16. sourceHeight = 1;
  17. r = 1;
  18. g = 1;
  19. b = 1;
  20. a = 1;
  21. posx = 0;
  22. posy = 0;
  23. angle = 0;
  24. sourceAngle = 0;
  25. scaleX = 1.0f;
  26. scaleY = 1.0f;
  27. manualTransformActive = false;
  28. }
  29. // Setter helpers
  30. void setTargetPos( float xi, float yi ) {
  31. posx = xi;
  32. posy = yi;
  33. }
  34. void setColor( float ri, float gi, float bi, float ai ) {
  35. r = ri; g = gi; b = bi; a = ai;
  36. }
  37. void setColor( float *col ) {
  38. r = col[0]; g = col[1]; b = col[2]; a = col[3];
  39. }
  40. void setSourceRect( float x=0.0f, float y=0.0f, float w=1.0f, float h=1.0f) {
  41. sourcex = x;
  42. sourcey = y;
  43. sourceWidth = w;
  44. sourceHeight = h;
  45. }
  46. void setSourceRect( float *rec ) {
  47. sourcex = rec[0];
  48. sourcey = rec[1];
  49. sourceWidth = rec[2];
  50. sourceHeight = rec[3];
  51. }
  52. void setScale( float scale ) {
  53. scaleX = scale;
  54. scaleY = scale;
  55. }
  56. void setScale( float scaleXi, float scaleYi ) {
  57. scaleX = scaleXi;
  58. scaleY = scaleYi;
  59. }
  60. void manualTransform( float xvx, float xvy, float yvx, float yvy ) {
  61. manualTransformMatrix[0][0] = xvx;
  62. manualTransformMatrix[0][1] = xvy;
  63. manualTransformMatrix[1][0] = yvx;
  64. manualTransformMatrix[1][1] = yvy;
  65. manualTransformActive = true;
  66. }
  67. void disableManualTransform() { manualTransformActive = false; }
  68. // Texture handle
  69. unsigned int textureHandle;
  70. // Target position
  71. float posx;
  72. float posy;
  73. // Source rectangle.
  74. float sourcex;
  75. float sourcey;
  76. float sourceWidth;
  77. float sourceHeight;
  78. // Blit color. NOTE, these must be in this order r,g,b,a must be next to eachother in structures memory
  79. float r,g,b,a;
  80. // source angle
  81. float sourceAngle;
  82. // Scale
  83. bool manualTransformActive;
  84. float manualTransformMatrix[2][2];
  85. float scaleX, scaleY;
  86. float angle;
  87. // For now, TODO, thinkAbout.
  88. // Effects ignored.
  89. // Layer depth ignored.
  90. };
  91. class SpriteBatch
  92. {
  93. public:
  94. enum TransformMode { ePIXELSPACE, eCUSTOMPROJECTION };
  95. enum BlendMode { eALPHA, eADDITIVE };
  96. SpriteBatch(int tWidth, int tHeight) {
  97. setTargetSize(tWidth, tHeight);
  98. }
  99. virtual ~SpriteBatch() {}
  100. virtual void begin( BlendMode bmode = eALPHA, TransformMode dTransform = ePIXELSPACE, float *customProjectionMatrix = 0) {}
  101. virtual void end() {}
  102. virtual void draw ( SpriteDrawInfo *sdi, int spriteCount=1 ) = 0;
  103. // Helper draws
  104. void draw( int texture, float x, float y, float w, float h, float angle = 0.0f );
  105. void draw( int texture, float x, float y, float w, float h, float *col );
  106. void draw( int texture, float x, float y, float w, float h, float *col, float angle );
  107. virtual void flushSprites(bool useTable=true) = 0; // Force flush,..
  108. inline int getTargetWidth() { return targetWidth; }
  109. inline int getTargetHeight() { return targetHeight; }
  110. inline void setTargetSize( int w, int h ) {
  111. #ifdef Q_WS_MAEMO_6_removed_as_firecup_is_landscape_app
  112. targetWidth = h;
  113. targetHeight = w;
  114. #else
  115. targetWidth = w;
  116. targetHeight = h;
  117. #endif
  118. }
  119. protected:
  120. int targetWidth;
  121. int targetHeight;
  122. };
  123. #endif