gcsx_sprite.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* GCSx
  2. ** SPRITE.H
  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. #ifndef __GCSx_SPRITE_H_
  24. #define __GCSx_SPRITE_H_
  25. // Intentionally kept as simple and fast as possible-
  26. // no virtual table, some inline property sets
  27. // @TODO: derive from ObjectBase, handle everythine associated with that
  28. class Sprite : public ObjectBase {
  29. public:
  30. enum {
  31. DEFAULT_PRIORITY = 100
  32. };
  33. // Scripting-accessible properties
  34. enum {
  35. // Position within layer (can be outside bounds)
  36. PROP_X = 0,
  37. PROP_Y,
  38. // Origin can be offset
  39. // Added/deleted to x/y automatically
  40. PROP_ORIGIN_X,
  41. PROP_ORIGIN_Y,
  42. // User-assigned priority- lower priority drawn first
  43. // Equal- oldest (lowest ID) drawn first
  44. PROP_PRIORITY,
  45. PROP_LAST
  46. };
  47. RunData varTable[PROP_LAST];
  48. protected:
  49. // World pointer not included, but will add if needed
  50. // Numeric ID- computer-generated- unique to world- nonzero
  51. int id;
  52. // Active sprites lock resources, etc.
  53. int active;
  54. void activateResource();
  55. void deactivateResource();
  56. friend struct ltSprite;
  57. // Entity that owns us- we don't own this ptr- can be NULL
  58. class Entity* obj;
  59. // Image (ready-to-blit; CAN point to a portion of a tileset, etc.)
  60. int width;
  61. int height;
  62. const class TextureMap* texture; // (see origImage)
  63. int texGraphic;
  64. // If non-NULL, image is our own
  65. // Also determines whether texture is our own
  66. SDL_Surface* origImage;
  67. // If non-NULL, image came from this tileset and tileset has been locked
  68. class TileSet* sourceTileSet;
  69. int tileNum; // Not gauranteed to be valid if above is NULL
  70. // If non-NULL, image came from this animation group (has been locked)
  71. // Single-image frames will also fill tileset, above
  72. class AnimGroup* sourceAnimGroup;
  73. int animNum; // Not gauranteed to be valid if above is NULL
  74. int animFrame; // Not gauranteed to be valid if above is NULL
  75. // @TODO:
  76. // Collision data
  77. // Color/alpha (4 separate values for optimized drawing)
  78. // Rotation, flip, other effects
  79. // Velocity, acceleration
  80. // Animation speed
  81. public:
  82. // Id must be unique; use ID of 0 if we're going to load/create anyways
  83. // Can also use ID of 0 for internal temporaries, etc.
  84. Sprite(int myId = 0);
  85. ~Sprite();
  86. // Active sprites use resources; inactive do not (similar to locking
  87. // but no count is incremented- this is on or off only)
  88. void setActive();
  89. void setInactive();
  90. // Sets
  91. void setEntity(class Entity* newEntity) { obj = newEntity; }
  92. // Load a new image off a tileset/animation, or clear image
  93. void setImage(class TileSet* tileset, int tile);
  94. void setImage(class AnimGroup* animgroup, int anim, int frame);
  95. void setImage();
  96. // Movement
  97. void moveTo(int nX, int nY) { varTable[PROP_X].i = nX; varTable[PROP_Y].i = nY; }
  98. // Accessors
  99. int getId() const { return id; }
  100. int getX() const { return varTable[PROP_X].i; }
  101. int getY() const { return varTable[PROP_Y].i; }
  102. // Display
  103. void draw(int vX, int vY);
  104. };
  105. // For ordering sprites in priority queue
  106. // @TODO: ensure reordering done on changing priority/scripttype
  107. struct ltSprite {
  108. bool operator() (const Sprite* s1, const Sprite* s2) const {
  109. return (s1->varTable[Sprite::PROP_PRIORITY].i < s2->varTable[Sprite::PROP_PRIORITY].i) ||
  110. ((s1->varTable[Sprite::PROP_PRIORITY].i == s2->varTable[Sprite::PROP_PRIORITY].i) &&
  111. (s1->id < s2->id));
  112. }
  113. };
  114. // Hash for IDs- 1) as fast as possible 2) sorted iteration will be handled separately
  115. typedef hash_map<int, Sprite*, hash<int> > SpriteIndex;
  116. // List for sorted iteration- sorted by user priority, then ID
  117. typedef std::set<Sprite*, ltSprite> SpriteOrder;
  118. #endif