spritelayout.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "eruta.h"
  2. #include "sprite.h"
  3. #include "spritelayout.h"
  4. /* Custom and built-in sprite layouts and loading them. */
  5. /* Layout info of the ULPCSS sprites. */
  6. static int ulpcss_sprites_per_row[] = {
  7. 7, 7, 7, 7,
  8. 8, 8, 8, 8,
  9. 9, 9, 9, 9,
  10. 6, 6, 6, 6,
  11. 13, 13, 13, 13,
  12. 6
  13. };
  14. /* Type info of the ULPCSS sprites. */
  15. static int ulpcss_row_type[] = {
  16. SPRITE_CAST , SPRITE_CAST , SPRITE_CAST , SPRITE_CAST ,
  17. SPRITE_STAB , SPRITE_STAB , SPRITE_STAB , SPRITE_STAB ,
  18. SPRITE_WALK , SPRITE_WALK , SPRITE_WALK , SPRITE_WALK ,
  19. SPRITE_SLASH, SPRITE_SLASH, SPRITE_SLASH, SPRITE_SLASH,
  20. SPRITE_SHOOT, SPRITE_SHOOT, SPRITE_SHOOT, SPRITE_SHOOT,
  21. SPRITE_DOWN
  22. };
  23. /* Direction info of the ULPCSS sprites. */
  24. static int ulpcss_row_direction[] = {
  25. SPRITE_NORTH , SPRITE_WEST , SPRITE_SOUTH, SPRITE_EAST,
  26. SPRITE_NORTH , SPRITE_WEST , SPRITE_SOUTH, SPRITE_EAST,
  27. SPRITE_NORTH , SPRITE_WEST , SPRITE_SOUTH, SPRITE_EAST,
  28. SPRITE_NORTH , SPRITE_WEST , SPRITE_SOUTH, SPRITE_EAST,
  29. SPRITE_NORTH , SPRITE_WEST , SPRITE_SOUTH, SPRITE_EAST,
  30. SPRITE_ALL
  31. };
  32. /* Frame duration info of ULPCSS sprites. */
  33. static double ulpcss_row_duration[] = {
  34. 0.1 , 0.1 , 0.1 , 0.1 ,
  35. 0.07, 0.07, 0.07, 0.07,
  36. 0.2 , 0.2 , 0.2 , 0.2 ,
  37. 0.07, 0.07, 0.07, 0.07,
  38. 0.1 , 0.1 , 0.1 , 0.1 ,
  39. 0.07,
  40. };
  41. /* Layout info of the ULPCSS sprites. Has 21 rows of cells.*/
  42. static SpriteLayout ulpcss_layout = {
  43. 21,
  44. ulpcss_sprites_per_row, ulpcss_row_type,
  45. ulpcss_row_direction , ulpcss_row_duration,
  46. 64, 64, 0, -16, -32
  47. };
  48. /* Layout info of the oversized ULPCSS sprites, for weapons only. */
  49. static int ulpcss_oversized_sprites_per_row[] = {
  50. 6, 6, 6, 6
  51. };
  52. /* Type info of the oversized ULPCSS sprites, for slashinhg weapons. */
  53. static int ulpcss_oversized_slash_row_type[] = {
  54. SPRITE_SLASH, SPRITE_SLASH, SPRITE_SLASH, SPRITE_SLASH
  55. };
  56. /* Type info of the ULPCSS sprites, for stabbing weapons. */
  57. static int ulpcss_oversized_stab_row_type[] = {
  58. SPRITE_STAB, SPRITE_STAB, SPRITE_STAB, SPRITE_STAB
  59. };
  60. /* Duration info of the ULPCSS sprites, for oversized weapons. */
  61. static double ulpcss_oversized_duration[] = {
  62. 0.07, 0.07, 0.07, 0.07
  63. };
  64. /* Direction info of the oversized ULPCSS sprites. */
  65. static int ulpcss_oversized_row_direction[] = {
  66. SPRITE_NORTH , SPRITE_WEST , SPRITE_SOUTH, SPRITE_EAST
  67. };
  68. /* Layout of an oversized slashing weapon ULPCSS sprite. */
  69. static SpriteLayout ulpcss_oversized_slash_layout = {
  70. 4,
  71. ulpcss_oversized_sprites_per_row, ulpcss_oversized_slash_row_type,
  72. ulpcss_oversized_row_direction, ulpcss_oversized_duration,
  73. 64 * 3, 64 * 3, 0, -80, -96
  74. };
  75. /* Layout of an oversized stabbing weapon ULPCSS sprite. */
  76. static SpriteLayout ulpcss_oversized_stab_layout = {
  77. 4,
  78. ulpcss_oversized_sprites_per_row, ulpcss_oversized_stab_row_type,
  79. ulpcss_row_direction, ulpcss_oversized_duration,
  80. 64 * 3, 64 * 3, 0, -80, -96
  81. };
  82. /* Amount of rows in a layout. */
  83. int spritelayout_rows(SpriteLayout * layout) {
  84. int index;
  85. if(!layout) return 0;
  86. if(!layout->per_row) return 0;
  87. return layout->rows;
  88. }
  89. /** Loads the stand-in-walk layers for sprites with layouts that have them.
  90. * It's ugly but it's needed because of how the ULPCSS sprite sheets are
  91. * organized...
  92. Sprite * sprite_loadlayerlayout_standinwalk(Sprite * self,
  93. int layerindex,
  94. Image * source,
  95. int oversized,
  96. SpriteLayout * layout) {
  97. return NULL;
  98. }
  99. */
  100. /* Loads a single row and column of a sprite layout from the given image
  101. * into the given layer for a normal cell. */
  102. int spritelayout_load_rc_normal(
  103. SpriteLayout * layout, Sprite * sprite, Image * source,
  104. int layeri, int rowi, int coli, int pose, int direction) {
  105. Point where = bevec(layout->size_x * coli, layout->size_y * rowi);
  106. Point size = bevec(layout->size_x, layout->size_y);
  107. Point offset = bevec(layout->offset_x, layout->offset_y);
  108. double duration = layout->row_duration[rowi];
  109. SpriteCell * cell = sprite_load_cell_from(
  110. sprite, pose, direction, layeri, source, size, where, offset, duration);
  111. if (!cell) return -1;
  112. return 1;
  113. }
  114. /* Loads a single row and column of a sprite layout from the given image
  115. * into the given layer for a stand in walk cell. */
  116. int spritelayout_load_rc_standinwalk(
  117. SpriteLayout * layout, Sprite * sprite, Image * source,
  118. int layeri, int rowi, int coli, int pose, int direction) {
  119. Point where = bevec(layout->size_x * coli, layout->size_y * rowi);
  120. Point size = bevec(layout->size_x , layout->size_y);
  121. Point offset = bevec(layout->offset_x, layout->offset_y);
  122. double duration = layout->row_duration[rowi];
  123. SpriteCell * cell = sprite_load_cell_from(
  124. sprite, SPRITE_STAND, direction, layeri, source, size, where, offset, duration);
  125. (void) pose;
  126. if (!cell) return -1;
  127. return 1;
  128. }
  129. /* Loads a single row and column of a sprite layout from the given image
  130. * into the given layer. */
  131. int spritelayout_load_rc(
  132. SpriteLayout * layout, Sprite * sprite, Image * source,
  133. int layeri, int rowi, int coli) {
  134. int pose = layout->row_type[rowi];
  135. int direction = layout->row_dir[rowi];
  136. /* special case for stand-in-walk cells. */
  137. if ( (layout->standinwalk >= 0)
  138. && (coli == layout->standinwalk)
  139. && (pose == SPRITE_WALK)
  140. ) {
  141. return spritelayout_load_rc_standinwalk(
  142. layout, sprite, source, layeri, rowi, coli, pose, direction);
  143. }
  144. /* Normal case */
  145. return spritelayout_load_rc_normal(
  146. layout, sprite, source, layeri, rowi, coli, pose, direction);
  147. }
  148. /* Loads a row of sprite cells into an action for the sprite,
  149. * according to the layout. */
  150. int spritelayout_load_row(
  151. SpriteLayout * layout, Sprite * sprite, Image * source, int layeri, int rowi){
  152. int col_max;
  153. int coli;
  154. if (rowi > spritelayout_rows(layout)) return -1;
  155. col_max = layout->per_row[rowi];
  156. for (coli = 0 ; coli < col_max; coli++) {
  157. spritelayout_load_rc(layout, sprite, source, layeri, rowi, coli);
  158. }
  159. return col_max;
  160. }
  161. /* Loads sprite cells for a whole layer in the sprite. The layout info in the
  162. * struct is used to correctly set up the sprite parts.
  163. */
  164. Sprite * spritelayout_load_layer
  165. (SpriteLayout * layout, Sprite * sprite, Image * source, int layeri) {
  166. int stop;
  167. int rowi;
  168. stop = spritelayout_rows(layout);
  169. for (rowi = 0 ; rowi < stop ; rowi++) {
  170. spritelayout_load_row(layout, sprite, source, layeri, rowi);
  171. }
  172. return sprite;
  173. }
  174. /** Returns a layout for a load type. Returns NULL on error or unknown type. */
  175. SpriteLayout * spritelayout_for(int load_type) {
  176. switch (load_type) {
  177. case SPRITE_LOAD_ULPCSS_NORMAL:
  178. return &ulpcss_layout;
  179. case SPRITE_LOAD_ULPCSS_OVERSIZED_SLASH:
  180. return &ulpcss_oversized_slash_layout;
  181. case SPRITE_LOAD_ULPCSS_OVERSIZED_STAB:
  182. return &ulpcss_oversized_stab_layout;
  183. default:
  184. return NULL;
  185. }
  186. }