gcsx_sprite.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* GCSx
  2. ** SPRITE.CPP
  3. **
  4. ** Sprite support (no edit-specific version)
  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. #include "all.h"
  24. // @TODO: respect ref
  25. Sprite::Sprite(int myId) { start_func
  26. id = myId;
  27. texture = NULL;
  28. origImage = NULL;
  29. sourceTileSet = NULL;
  30. sourceAnimGroup = NULL;
  31. obj = NULL;
  32. varTable[PROP_ORIGIN_X].i = varTable[PROP_ORIGIN_Y].i = 0;
  33. varTable[PROP_X].i = varTable[PROP_Y].i = 0;
  34. active = 0;
  35. varTable[PROP_PRIORITY].i = DEFAULT_PRIORITY;
  36. members = varTable;
  37. ref = 0;
  38. subtype = SUB_SPRITE;
  39. }
  40. Sprite::~Sprite() { start_func
  41. // @TODO: assert ref=0?
  42. setImage();
  43. }
  44. void Sprite::activateResource() { start_func
  45. if (sourceTileSet) {
  46. // @TODO: throw_File
  47. sourceTileSet->markLockPlay();
  48. texture = sourceTileSet->getTexture();
  49. texGraphic = tileNum;
  50. }
  51. if (sourceAnimGroup) {
  52. // @TODO:
  53. }
  54. if (origImage) {
  55. // @TODO:
  56. }
  57. }
  58. void Sprite::deactivateResource() { start_func
  59. if (sourceTileSet) {
  60. sourceTileSet->markUnlockPlay();
  61. texture = NULL;
  62. }
  63. if (sourceAnimGroup) {
  64. // @TODO:
  65. texture = NULL;
  66. }
  67. if (origImage) {
  68. // @TODO:
  69. texture = NULL;
  70. }
  71. }
  72. void Sprite::setActive() { start_func
  73. if (!active) {
  74. activateResource();
  75. active = 1;
  76. }
  77. }
  78. void Sprite::setInactive() { start_func
  79. if (active) {
  80. deactivateResource();
  81. active = 0;
  82. }
  83. }
  84. void Sprite::setImage(class TileSet* tileset, int tile) { start_func
  85. assert(tileset);
  86. setImage();
  87. if ((tile > 0) && (tile <= tileset->getCount())) {
  88. sourceTileSet = tileset;
  89. tileNum = tile;
  90. width = tileset->getWidth();
  91. height = tileset->getHeight();
  92. if (active) activateResource();
  93. }
  94. }
  95. void Sprite::setImage(AnimGroup* animgroup, int anim, int frame) { start_func
  96. assert(animgroup);
  97. setImage();
  98. if ((anim > 0) && (anim <= animgroup->getCount())) {
  99. sourceAnimGroup = animgroup;
  100. animNum = anim;
  101. animFrame = frame;
  102. // width = @TODO:
  103. // height = @TODO:
  104. if (active) activateResource();
  105. }
  106. }
  107. void Sprite::setImage() { start_func
  108. if (active) deactivateResource();
  109. if (sourceTileSet) {
  110. // sourceTileSet->markUnlock();
  111. sourceTileSet = NULL;
  112. }
  113. if (sourceAnimGroup) {
  114. // sourceAnimGroup->markUnlock();
  115. sourceAnimGroup = NULL;
  116. }
  117. if (origImage) {
  118. SDL_FreeSurface(origImage);
  119. origImage = NULL;
  120. }
  121. assert(!texture);
  122. }
  123. void Sprite::draw(int vX, int vY) { start_func
  124. if (!texture) return;
  125. // This clipping can't be used as-is for rotated/enlarged sprites
  126. int dX = varTable[PROP_X].i + varTable[PROP_ORIGIN_X].i - vX;
  127. if (dX >= screenWidth) return;
  128. if (dX + width < 0) return;
  129. int dY = varTable[PROP_Y].i + varTable[PROP_ORIGIN_Y].i - vY;
  130. if (dY >= screenHeight) return;
  131. if (dY + height < 0) return;
  132. // @TODO: don't set color if same as last one
  133. // (have a function that says "reset color optimization" called
  134. // right before doing a round of sprites)
  135. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  136. texture->draw(tileNum, dX, dY);
  137. }