WWCharacter.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*****************************************************************************/
  2. /* ここから */
  3. /*****************************************************************************/
  4. #include "WWCharacterP.h"
  5. #include "etc.h"
  6. /*****************************************************************************/
  7. /* メンバ関数の定義 */
  8. /*****************************************************************************/
  9. int WWCharacter_GetNumber(WWCharacter c)
  10. {
  11. if (c == NULL) WonX_Error("WWCharacter_GetNumber()", "WWCharacter is NULL.");
  12. return (c->number);
  13. }
  14. int WWCharacter_SetNumber(WWCharacter c, int n)
  15. {
  16. if ((n < 0) || (n >= 512))
  17. WonX_Error("WWCharacter_SetNumber()", "Invalid range.");
  18. return (c->number = n);
  19. }
  20. WWCharacter WWCharacter_Create(int number, unsigned char * bitmap)
  21. {
  22. WWCharacter character;
  23. character = (WWCharacter)malloc(sizeof(_WWCharacter));
  24. if (character == NULL)
  25. WonX_Error("WWCharacter_Create", "Cannot allocate memory.");
  26. WWCharacter_SetNumber(character, number);
  27. WWCharacter_SetBitmap(character, bitmap);
  28. return (character);
  29. }
  30. WWCharacter WWCharacter_Destroy(WWCharacter character)
  31. {
  32. if (character == NULL)
  33. WonX_Error("WWCharacter_Destroy()", "WWCharacter is NULL.");
  34. free(character);
  35. return (NULL);
  36. }
  37. unsigned char * WWCharacter_GetBitmap(WWCharacter character)
  38. {
  39. return (character->bitmap);
  40. }
  41. int WWCharacter_SetBitmap(WWCharacter character, unsigned char * bitmap)
  42. {
  43. int i;
  44. if (character == NULL)
  45. WonX_Error("WWCharacter_SetBitmap()", "WWCharacter is NULL.");
  46. for (i = 0; i < 16; i++) {
  47. if (bitmap == NULL) {
  48. character->bitmap[i] = 0x00;
  49. } else {
  50. character->bitmap[i] = bitmap[i];
  51. }
  52. }
  53. return (0);
  54. }
  55. int WWCharacter_GetPixel(WWCharacter character, int x, int y)
  56. {
  57. if (character == NULL)
  58. WonX_Error("WWCharacter_GetPixel()", "WWCharacter is NULL.");
  59. if ((x < 0) || (x > 7))
  60. WonX_Error("WWCharacter_GetPixel()", "x is invalid value.");
  61. if ((y < 0) || (y > 7))
  62. WonX_Error("WWCharacter_GetPixel()", "y is invalid value.");
  63. /* ビットマップは2ビットでぴとつのピクセルに対応する. */
  64. /* 2ビットの値が,palette の色に対応する. */
  65. /* bitmap は unsigned char bitmap[16]; に定義してある. */
  66. /* パレット色(0~3)を返す */
  67. return ( (character->bitmap[y * 2 + x / 4] >> ((x % 4) * 2)) & 0x03 );
  68. }
  69. int WWCharacter_SetPixel(WWCharacter character, int x, int y, int pixel)
  70. {
  71. unsigned char p;
  72. if (character == NULL)
  73. WonX_Error("WWCharacter_SetPixel()", "WWCharacter is NULL.");
  74. if ((x < 0) || (x > 7))
  75. WonX_Error("WWCharacter_SetPixel()", "x is invalid value.");
  76. if ((y < 0) || (y > 7))
  77. WonX_Error("WWCharacter_SetPixel()", "y is invalid value.");
  78. if ((pixel < 0) || (pixel > 3))
  79. WonX_Error("WWCharacter_SetPixel()", "Invalid pixel.");
  80. p = ((unsigned char)pixel) & 0x03;
  81. p = p << ((x % 4) * 2);
  82. character->bitmap[y * 2 + x / 4] &= ~(0x03 << ((x % 4) * 2));
  83. character->bitmap[y * 2 + x / 4] |= p;
  84. return (pixel);
  85. }
  86. int WWCharacter_CopyBitmap(WWCharacter dst, WWCharacter src)
  87. {
  88. return (WWCharacter_SetBitmap(dst, src->bitmap));
  89. }
  90. int WWCharacter_PrintData(WWCharacter character, FILE * f)
  91. {
  92. int x, y, i, n;
  93. n = WWCharacter_GetNumber(character);
  94. fprintf(f, "\n");
  95. fprintf(f, "character[%d] :\tnumber = %d\n",
  96. n, WWCharacter_GetNumber(character));
  97. for (i = 0; i < 16; i++) {
  98. fprintf(f, "character[%d] :\tbitmap[%d] = 0x%02x\n",
  99. n, i, (int)(WWCharacter_GetBitmap(character)[i]));
  100. }
  101. for (y = 0; y < 8; y++) {
  102. fprintf(f, "character[%d] :\tbitmap : ", n);
  103. for (x = 0; x < 8; x++) {
  104. fprintf(f, "%d", WWCharacter_GetPixel(character, x, y));
  105. }
  106. fprintf(f, "\n");
  107. }
  108. fflush(f);
  109. return (0);
  110. }
  111. /*****************************************************************************/
  112. /* ここまで */
  113. /*****************************************************************************/
  114. /*****************************************************************************/
  115. /* End of File. */
  116. /*****************************************************************************/