WWCharacter.c 3.8 KB

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