WWCharacter.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*****************************************************************************/
  2. /* ここから */
  3. /*****************************************************************************/
  4. #include <string.h>
  5. #include "WWCharacterP.h"
  6. #include "WonX.h"
  7. #include "etc.h"
  8. /*****************************************************************************/
  9. /* メンバ関数の定義 */
  10. /*****************************************************************************/
  11. /*===========================================================================*/
  12. /* ナンバーの操作 */
  13. /*===========================================================================*/
  14. int WWCharacter_GetNumber(WWCharacter c)
  15. {
  16. if (c == NULL) WonX_Error("WWCharacter_GetNumber", "WWCharacter is NULL.");
  17. return (c->number);
  18. }
  19. int WWCharacter_SetNumber(WWCharacter c, int n)
  20. {
  21. if (c == NULL) WonX_Error("WWCharacter_SetNumber", "WWCharacter is NULL.");
  22. if ((n < 0) || (n > 512 - 1))
  23. WonX_Error("WWCharacter_SetNumber", "Invalid range.");
  24. return (c->number = n);
  25. }
  26. /*===========================================================================*/
  27. /* ビットマップの操作 */
  28. /*===========================================================================*/
  29. /*---------------------------------------------------------------------------*/
  30. /* char 型として操作するための関数 */
  31. /*---------------------------------------------------------------------------*/
  32. unsigned char WWCharacter_GetBitmapAsChar(WWCharacter c, int n)
  33. {
  34. if (c == NULL)
  35. WonX_Error("WWCharacter_GetBitmapAsChar", "WWCharacter is NULL.");
  36. if ((n < 0) || (n > 32 - 1))
  37. WonX_Error("WWCharacter_GetBitmapAsChar", "Invalid range.");
  38. return (c->bitmap.bitmap_char[n]);
  39. }
  40. unsigned char WWCharacter_SetBitmapAsChar(WWCharacter c, int n,
  41. unsigned char bitmap)
  42. {
  43. if (c == NULL)
  44. WonX_Error("WWCharacter_SetBitmapAsChar", "WWCharacter is NULL.");
  45. if ((n < 0) || (n > 32 - 1))
  46. WonX_Error("WWCharacter_SetBitmapAsChar", "Invalid range.");
  47. return (c->bitmap.bitmap_char[n] = bitmap);
  48. }
  49. /*---------------------------------------------------------------------------*/
  50. /* short int 型として操作するための関数 */
  51. /*---------------------------------------------------------------------------*/
  52. unsigned short int WWCharacter_GetBitmapAsShortInt(WWCharacter c, int n)
  53. {
  54. if (c == NULL)
  55. WonX_Error("WWCharacter_GetBitmapAsShortInt", "WWCharacter is NULL.");
  56. if ((n < 0) || (n > 16 - 1))
  57. WonX_Error("WWCharacter_GetBitmapAsShortInt", "Invalid range.");
  58. return (c->bitmap.bitmap_short_int[n]);
  59. }
  60. unsigned short int WWCharacter_SetBitmapAsShortInt(WWCharacter c, int n,
  61. unsigned short int bitmap)
  62. {
  63. if (c == NULL)
  64. WonX_Error("WWCharacter_SetBitmapAsShortInt", "WWCharacter is NULL.");
  65. if ((n < 0) || (n > 16 - 1))
  66. WonX_Error("WWCharacter_SetBitmapAsShortInt", "Invalid range.");
  67. return (c->bitmap.bitmap_short_int[n] = bitmap);
  68. }
  69. /*---------------------------------------------------------------------------*/
  70. /* long int 型として操作するための関数 */
  71. /*---------------------------------------------------------------------------*/
  72. unsigned long int WWCharacter_GetBitmapAsLongInt(WWCharacter c, int n)
  73. {
  74. if (c == NULL)
  75. WonX_Error("WWCharacter_GetBitmapAsLongInt", "WWCharacter is NULL.");
  76. if ((n < 0) || (n > 8 - 1))
  77. WonX_Error("WWCharacter_GetBitmapAsLongInt", "Invalid range.");
  78. return (c->bitmap.bitmap_long_int[n]);
  79. }
  80. unsigned long int WWCharacter_SetBitmapAsLongInt(WWCharacter c, int n,
  81. unsigned long int bitmap)
  82. {
  83. if (c == NULL)
  84. WonX_Error("WWCharacter_SetBitmapAsLongInt", "WWCharacter is NULL.");
  85. if ((n < 0) || (n > 8 - 1))
  86. WonX_Error("WWCharacter_SetBitmapAsLongInt", "Invalid range.");
  87. return (c->bitmap.bitmap_long_int[n] = bitmap);
  88. }
  89. /*===========================================================================*/
  90. /* オブジェクトの生成と削除 */
  91. /*===========================================================================*/
  92. WWCharacter WWCharacter_Create(int number)
  93. {
  94. WWCharacter character;
  95. character = (WWCharacter)malloc(sizeof(_WWCharacter));
  96. if (character == NULL)
  97. WonX_Error("WWCharacter_Create", "Cannot allocate memory.");
  98. WWCharacter_SetNumber(character, number);
  99. WWCharacter_ClearAllPixels(character);
  100. return (character);
  101. }
  102. WWCharacter WWCharacter_Destroy(WWCharacter character)
  103. {
  104. if (character == NULL)
  105. WonX_Error("WWCharacter_Destroy", "Object is not created.");
  106. free(character);
  107. return (NULL);
  108. }
  109. /*===========================================================================*/
  110. /* ピクセルの操作 */
  111. /*===========================================================================*/
  112. int WWCharacter_GetPixel(WWCharacter character, int x, int y,
  113. WWDisplay display)
  114. {
  115. unsigned short int pixel;
  116. unsigned short int spixel;
  117. unsigned long int lpixel;
  118. if (character == NULL)
  119. WonX_Error("WWCharacter_GetPixel", "WWCharacter is NULL.");
  120. if ((x < 0) || (x > 7))
  121. WonX_Error("WWCharacter_GetPixel", "x is out of range.");
  122. if ((y < 0) || (y > 7))
  123. WonX_Error("WWCharacter_GetPixel", "y is out of range.");
  124. /* パレット色を返す */
  125. pixel = 0;
  126. switch (WWDisplay_GetColorMode(display)) {
  127. case COLOR_MODE_GRAYSCALE:
  128. case COLOR_MODE_4COLOR:
  129. spixel = (character->bitmap.bitmap_short_int[y] >> (7-x)) & 0x0101;
  130. pixel =
  131. ((spixel & 0x0001) ? 1 : 0) |
  132. ((spixel & 0x0100) ? 2 : 0);
  133. #if 0
  134. pixel = ((character->bitmap.bitmap_char[y * 2 + 0] >> (7-x)) & 1) << 0;
  135. pixel |= ((character->bitmap.bitmap_char[y * 2 + 1] >> (7-x)) & 1) << 1;
  136. #endif
  137. break;
  138. case COLOR_MODE_16COLOR:
  139. lpixel = (character->bitmap.bitmap_long_int[y] >> (7-x)) & 0x01010101;
  140. pixel =
  141. ((lpixel & 0x00000001) ? 1 : 0) |
  142. ((lpixel & 0x00000100) ? 2 : 0) |
  143. ((lpixel & 0x00010000) ? 4 : 0) |
  144. ((lpixel & 0x01000000) ? 8 : 0);
  145. #if 0
  146. pixel = ((character->bitmap[y * 4 + 0] >> (7-x)) & 1) << 0;
  147. pixel |= ((character->bitmap[y * 4 + 1] >> (7-x)) & 1) << 1;
  148. pixel |= ((character->bitmap[y * 4 + 2] >> (7-x)) & 1) << 2;
  149. pixel |= ((character->bitmap[y * 4 + 3] >> (7-x)) & 1) << 3;
  150. #endif
  151. break;
  152. case COLOR_MODE_16PACKED:
  153. lpixel = character->bitmap.bitmap_long_int[y];
  154. lpixel = lpixel >> (3 - (7-x) / 2) * 8;
  155. lpixel = lpixel >> ((7-x) % 2) * 4;
  156. pixel = lpixel & 0x0f;
  157. #if 0
  158. pixel = character->bitmap[y * 4 + 3 - (7-x) / 2] >> (((7-x) % 2) * 4);
  159. pixel &= 0x0f;
  160. #endif
  161. break;
  162. default:
  163. WonX_Error("WWCharacter_GetPixel", "Unknown color mode.");
  164. }
  165. return (pixel);
  166. }
  167. int WWCharacter_ClearAllPixels(WWCharacter character)
  168. {
  169. if (character == NULL)
  170. WonX_Error("WWCharacter_ClearAllPixels", "WWCharacter is NULL.");
  171. memset(character->bitmap.bitmap_char, 0, 32);
  172. return (0);
  173. }
  174. int WWCharacter_CopyAllPixels(WWCharacter dst, WWCharacter src)
  175. {
  176. if (dst == NULL)
  177. WonX_Error("WWCharacter_CopyAllPixel", "dst is NULL.");
  178. if (src == NULL)
  179. WonX_Error("WWCharacter_CopyAllPixel", "src is NULL.");
  180. memcpy(dst->bitmap.bitmap_char, src->bitmap.bitmap_char, 32);
  181. return (0);
  182. }
  183. /*===========================================================================*/
  184. /* 内部データ出力 */
  185. /*===========================================================================*/
  186. int WWCharacter_PrintData(WWCharacter character, WWDisplay display, FILE * f)
  187. {
  188. int i, x, y, n;
  189. int pixel;
  190. if (character == NULL)
  191. WonX_Error("WWCharacter_PrintData", "WWCharacter is NULL.");
  192. n = WWCharacter_GetNumber(character);
  193. fprintf(f, "\n");
  194. fprintf(f, "character[%03d] :\tnumber = %d\n",
  195. n, WWCharacter_GetNumber(character));
  196. for (i = 0; i < 32; i += 8) {
  197. fprintf(f, "character[%03d] :\tbitmap[%02d] =", n, i);
  198. fprintf(f, " %02x", (int)WWCharacter_GetBitmapAsChar(character, i ));
  199. fprintf(f, " %02x", (int)WWCharacter_GetBitmapAsChar(character, i+1));
  200. fprintf(f, " %02x", (int)WWCharacter_GetBitmapAsChar(character, i+2));
  201. fprintf(f, " %02x", (int)WWCharacter_GetBitmapAsChar(character, i+3));
  202. fprintf(f, " %02x", (int)WWCharacter_GetBitmapAsChar(character, i+4));
  203. fprintf(f, " %02x", (int)WWCharacter_GetBitmapAsChar(character, i+5));
  204. fprintf(f, " %02x", (int)WWCharacter_GetBitmapAsChar(character, i+6));
  205. fprintf(f, " %02x", (int)WWCharacter_GetBitmapAsChar(character, i+7));
  206. fprintf(f, "\n");
  207. }
  208. fprintf(f, "character[%03d] :\tpixels : 01234567\n", n);
  209. for (y = 0; y < 8; y++) {
  210. fprintf(f, "character[%03d] :\tpixels : %01d ", n, y);
  211. for (x = 0; x < 8; x++) {
  212. pixel = WWCharacter_GetPixel(character, x, y, display);
  213. fprintf(f, "%c", wonx_dec_to_hex(pixel));
  214. }
  215. fprintf(f, "\n");
  216. }
  217. fflush(f);
  218. return (0);
  219. }
  220. /*****************************************************************************/
  221. /* ここまで */
  222. /*****************************************************************************/
  223. /*****************************************************************************/
  224. /* End of File. */
  225. /*****************************************************************************/