WWCharacter.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <stdio.h>
  51. int WWCharacter_GetPixel(WWCharacter character, int x, int y)
  52. {
  53. if (character == NULL)
  54. Error("WWCharacter_GetPixel()", "WWCharacter is NULL.");
  55. if ((x < 0) || (x > 7))
  56. Error("WWCharacter_GetPixel()", "x is invalid value.");
  57. if ((y < 0) || (y > 7))
  58. Error("WWCharacter_GetPixel()", "y is invalid value.");
  59. /* ビットマップは2ビットでぴとつのピクセルに対応する. */
  60. /* 2ビットの値が,palette の色に対応する. */
  61. /* bitmap は unsigned char bitmap[16]; に定義してある. */
  62. /* パレット色(0~3)を返す */
  63. return ( (character->bitmap[y * 2 + x / 4] >> ((x % 4) * 2)) & 0x03 );
  64. }
  65. int WWCharacter_SetPixel(WWCharacter character, int x, int y, int pixel)
  66. {
  67. unsigned char p;
  68. if (character == NULL)
  69. Error("WWCharacter_SetPixel()", "WWCharacter is NULL.");
  70. if ((x < 0) || (x > 7))
  71. Error("WWCharacter_SetPixel()", "x is invalid value.");
  72. if ((y < 0) || (y > 7))
  73. Error("WWCharacter_SetPixel()", "y is invalid value.");
  74. if ((pixel < 0) || (pixel > 3))
  75. Error("WWCharacter_SetPixel()", "Invalid pixel.");
  76. p = ((unsigned char)pixel) & 0x03;
  77. p = p << ((x % 4) * 2);
  78. character->bitmap[y * 2 + x / 4] &= ~(0x03 << ((x % 4) * 2));
  79. character->bitmap[y * 2 + x / 4] |= p;
  80. return (pixel);
  81. }
  82. /*****************************************************************************/
  83. /* ここまで */
  84. /*****************************************************************************/
  85. /*****************************************************************************/
  86. /* End of File. */
  87. /*****************************************************************************/