PageRenderTime 168ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/platform/Graphics.cpp

http://github.com/rossumur/microtouch
C++ | 407 lines | 308 code | 52 blank | 47 comment | 51 complexity | e65e3a7270dd230d38f5e70966ee770c MD5 | raw file
  1. /* Copyright (c) 2010, Peter Barrett
  2. **
  3. ** Permission to use, copy, modify, and/or distribute this software for
  4. ** any purpose with or without fee is hereby granted, provided that the
  5. ** above copyright notice and this permission notice appear in all copies.
  6. **
  7. ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
  10. ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
  11. ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  12. ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  13. ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  14. ** SOFTWARE.
  15. */
  16. #include "Platform.h"
  17. #include "LCD.h"
  18. extern const byte Verdana_font_11[] PROGMEM;
  19. const byte Verdana_font_11[] = {
  20. 0, // format
  21. 11, // fontHeight
  22. 9, // ascent
  23. 33, // from
  24. 95, // count
  25. 59, // totalWidth/8
  26. 0x00,0x01,0x00,0x04,0x00,0x0B,0x00,0x10,0x00,0x1A,0x00,0x21,0x00,0x22,0x00,0x25,0x00,0x28,0x00,0x2D,0x00,0x34,0x00,0x36,0x00,0x39,0x00,0x3A,0x00,0x3F,0x00,0x44,0x00,0x49,0x00,0x4E,0x00,0x53,0x00,0x59,0x00,0x5E,0x00,0x63,0x00,0x68,0x00,0x6D,0x00,0x72,0x00,0x73,0x00,0x75,0x00,0x7B,0x00,0x82,0x00,0x88,0x00,0x8C,0x00,0x94,0x00,0x9A,0x00,0xA0,0x00,0xA7,0x00,0xAE,0x00,0xB3,0x00,0xB8,0x00,0xBF,0x00,0xC5,0x00,0xC8,0x00,0xCC,0x00,0xD2,0x00,0xD7,0x00,0xDE,0x00,0xE4,0x00,0xEB,0x00,0xF0,0x00,0xF7,0x00,0xFD,0x01,0x03,0x01,0x0A,0x01,0x10,0x01,0x16,0x01,0x1F,0x01,0x25,0x01,0x2C,0x01,0x32,0x01,0x35,0x01,0x3A,0x01,0x3D,0x01,0x44,0x01,0x4B,0x01,0x4D,0x01,0x52,0x01,0x57,0x01,0x5C,0x01,0x61,0x01,0x66,0x01,0x6A,0x01,0x6F,0x01,0x74,0x01,0x75,0x01,0x78,0x01,0x7D,0x01,0x7E,0x01,0x87,0x01,0x8C,0x01,0x91,0x01,0x96,0x01,0x9B,0x01,0x9F,0x01,0xA3,0x01,0xA7,0x01,0xAC,0x01,0xB1,0x01,0xB8,0x01,0xBD,0x01,0xC2,0x01,0xC6,0x01,0xCB,0x01,0xCC,0x01,0xD1,0x01,0xD8,0x01,0xD8, // Col ends
  27. 0x50,0x04,0x00,0x00,0x4C,0x20,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x38,0x00,0x10,0x20,0x00,0x80,0xC1,0x09,0x84,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x00, // Line 0
  28. 0xD1,0x44,0x62,0x18,0x52,0xA8,0x00,0x02,0xE2,0x39,0xC1,0x7C,0xDF,0x73,0x80,0x00,0x00,0xE3,0xE3,0x3C,0x3D,0xF3,0xFF,0x3D,0x0F,0x78,0x61,0x8F,0x13,0x9E,0x39,0xE3,0xDF,0xE1,0x86,0x23,0x0C,0x1F,0xE4,0x08,0x80,0x08,0x20,0x00,0x81,0x01,0x00,0x84,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x92,0x00, // Line 1
  29. 0xD1,0x4F,0x92,0x24,0x52,0x70,0x80,0x05,0x1E,0x46,0x23,0x41,0x01,0x8C,0x40,0x00,0x00,0x14,0x13,0x22,0x43,0x0A,0x10,0x43,0x0A,0x18,0xA1,0x8F,0x14,0x51,0x45,0x14,0x22,0x21,0x86,0x23,0x0A,0x20,0x62,0x09,0x40,0x00,0x20,0x00,0x81,0x01,0x00,0x84,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x92,0x00, // Line 2
  30. 0x87,0xF4,0x94,0x24,0x21,0xA8,0x80,0x05,0x12,0x04,0x25,0x42,0x02,0x8C,0x68,0x60,0x30,0x19,0xD4,0xA2,0x81,0x06,0x10,0x81,0x0A,0x19,0x21,0x56,0x98,0x31,0x83,0x14,0x02,0x21,0x85,0x54,0x91,0x40,0xA2,0x0A,0x20,0x03,0xBC,0xE7,0xBB,0xDF,0xEB,0x8F,0xDD,0xE7,0x79,0xF6,0xFF,0x18,0xC9,0x8C,0x7C,0x92,0x00, // Line 3
  31. 0x82,0x94,0x64,0x19,0x21,0x20,0x80,0x09,0x12,0x08,0xC9,0x7B,0xC2,0x74,0x69,0x9F,0xCC,0x2A,0x54,0xBE,0x81,0x07,0xFE,0x81,0xFA,0x1A,0x21,0x56,0x98,0x31,0x83,0x13,0x02,0x21,0x49,0x54,0x60,0x81,0x21,0x0C,0x10,0x00,0x63,0x18,0xC5,0x23,0x19,0x96,0x23,0x18,0xC6,0x39,0x09,0x18,0xC9,0x52,0x84,0x92,0x31, // Line 4
  32. 0x82,0x8E,0x09,0xA5,0x21,0x07,0xF3,0x89,0x12,0x10,0x31,0x06,0x24,0x8B,0xC6,0x00,0x03,0x4A,0x54,0xA1,0x81,0x06,0x10,0x8F,0x0A,0x1E,0x21,0x26,0x58,0x3E,0x83,0xE0,0xC2,0x21,0x49,0x54,0x60,0x82,0x21,0x08,0x00,0x03,0xE3,0x08,0xFD,0x23,0x19,0xA6,0x23,0x18,0xC6,0x31,0x89,0x15,0x55,0x22,0x8B,0x11,0xC9, // Line 5
  33. 0x8F,0xC5,0x0A,0x62,0x21,0x00,0x80,0x11,0x12,0x20,0x3F,0x86,0x24,0x88,0x41,0x9F,0xCC,0x4A,0x5F,0xE1,0x81,0x06,0x10,0x83,0x0A,0x19,0x21,0x26,0x58,0x30,0x83,0x20,0x22,0x21,0x49,0x54,0x90,0x84,0x20,0x88,0x00,0x04,0x63,0x08,0xC1,0x23,0x19,0xE6,0x23,0x18,0xC6,0x30,0x69,0x15,0x55,0x22,0x90,0x92,0x46, // Line 6
  34. 0x05,0x05,0x12,0x63,0x21,0x00,0x84,0x51,0x12,0x42,0x21,0x46,0x28,0x88,0xA8,0x60,0x30,0x09,0xA8,0x61,0x43,0x0A,0x10,0x43,0x0A,0x18,0xA1,0x06,0x34,0x50,0x45,0x14,0x22,0x21,0x30,0x89,0x08,0x88,0x20,0x88,0x00,0x04,0x63,0x18,0xC5,0x23,0x19,0x96,0x23,0x18,0xC6,0x30,0x29,0x12,0x22,0x51,0x20,0x92,0x00, // Line 7
  35. 0x85,0x1E,0x11,0x9C,0x92,0x00,0x84,0x60,0xEF,0xFD,0xC1,0x39,0xC8,0x73,0x28,0x00,0x00,0x44,0x08,0x7E,0x3D,0xF3,0xF0,0x3D,0x0F,0xE8,0x7F,0x06,0x33,0x90,0x39,0x0B,0xC2,0x1E,0x30,0x89,0x08,0x8F,0xE0,0x48,0x00,0x03,0xFC,0xE7,0xB9,0x1F,0x19,0x8E,0x23,0x17,0x79,0xF1,0xC6,0xF2,0x22,0x89,0x3C,0x92,0x00, // Line 8
  36. 0x00,0x04,0x00,0x00,0x12,0x00,0x04,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x48,0x00,0x00,0x00,0x00,0x00,0x02,0x01,0x00,0x00,0x00,0x40,0x20,0x00,0x00,0x00,0x01,0x00,0x92,0x00, // Line 9
  37. 0x00,0x04,0x00,0x00,0x0C,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x38,0x0F,0xE0,0x00,0x00,0x00,0x1C,0x06,0x00,0x00,0x00,0x40,0x20,0x00,0x00,0x00,0x02,0x00,0x7C,0x00, // Line 10
  38. };
  39. #if 0
  40. extern const byte monofont[] PROGMEM;
  41. const byte monofont[] = { // 5x10 (596 bytes)
  42. 5, // formatWidth - monospaced
  43. 10, // fontHeight
  44. 8, // ascent
  45. 33, // from
  46. 94, // count
  47. 59, // totalWidth/8
  48. 0x02,0x80,0x00,0x00,0x82,0x40,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1D,0x07,0x00,0x00,0x04,0x00,0x10,0x1C,0x10,0x00,0x10,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x44,0x00,
  49. 0x22,0x80,0x40,0x30,0x84,0x20,0x00,0x00,0x00,0x2E,0x23,0x9C,0x2F,0x9B,0xEE,0x70,0x00,0x00,0x01,0xCF,0x27,0x9D,0xCF,0xFD,0xD1,0x71,0xE3,0x08,0xE5,0xDE,0x77,0x9D,0xF8,0xC6,0x31,0x8F,0xD1,0x01,0x10,0x04,0x04,0x00,0x10,0x20,0x10,0x20,0x90,0x40,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x08,0x42,0x00,
  50. 0x22,0x94,0xF4,0x48,0x88,0x11,0x08,0x00,0x00,0x51,0x64,0x62,0x68,0x20,0x31,0x88,0x00,0x10,0x42,0x30,0x24,0x63,0x28,0x42,0x31,0x20,0x65,0x08,0xE6,0x31,0x8C,0x62,0x48,0xC6,0xB1,0x88,0x50,0x81,0x28,0x02,0x04,0x00,0x10,0x20,0x10,0x00,0x10,0x40,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x08,0x42,0x00,
  51. 0x20,0x3F,0x4A,0xC8,0x08,0x15,0x48,0x00,0x00,0x53,0x20,0x42,0xA8,0x40,0x51,0x89,0x08,0x6F,0xB0,0x36,0x54,0x61,0x18,0x42,0x11,0x20,0x69,0x0D,0xD6,0x31,0x8C,0x60,0x48,0xC6,0xAA,0x88,0x90,0x81,0x44,0x00,0x77,0x9E,0xF7,0x79,0xDE,0x61,0x92,0x4D,0x79,0xDE,0x7D,0x9E,0xE8,0xC6,0x31,0x8F,0xC8,0x42,0x00,
  52. 0x20,0x14,0xE5,0x30,0x08,0x13,0xBE,0x0F,0x80,0x95,0x21,0x8D,0x2F,0x78,0x4E,0x78,0x01,0x80,0x0C,0x56,0x57,0xA1,0x1F,0x7A,0x7F,0x20,0x71,0x0D,0xD6,0x3E,0x8F,0x9C,0x48,0xAA,0xA4,0x71,0x10,0x41,0x00,0x00,0x0C,0x61,0x18,0xA2,0x31,0x20,0x94,0x4A,0xC6,0x31,0x8E,0x60,0x88,0xC6,0xAA,0x88,0x90,0x41,0x30,
  53. 0x20,0x14,0x52,0xC8,0x08,0x15,0x48,0x00,0x00,0x99,0x22,0x03,0xF0,0xC4,0x91,0x08,0x00,0x6F,0xB0,0x97,0x74,0x61,0x18,0x42,0x31,0x20,0x69,0x0A,0xD6,0x30,0x8C,0x42,0x48,0xA9,0x4A,0x22,0x10,0x41,0x00,0x00,0x7C,0x61,0x1F,0xA2,0x31,0x20,0x98,0x4A,0xC6,0x31,0x8C,0x1C,0x88,0xAA,0xA4,0x89,0x08,0x42,0x4C,
  54. 0x00,0x3F,0xE5,0x44,0x08,0x11,0x08,0x00,0x01,0x11,0x24,0x22,0x28,0xC4,0x91,0x11,0x08,0x10,0x40,0x10,0x8C,0x63,0x28,0x42,0x31,0x20,0x65,0x0A,0xCE,0x30,0x9C,0x62,0x48,0x91,0x51,0x24,0x10,0x21,0x00,0x00,0x8C,0x61,0x18,0x22,0x31,0x20,0x94,0x4A,0xC6,0x31,0x8C,0x02,0x88,0xA9,0x4A,0x8A,0x08,0x42,0x00,
  55. 0x20,0x14,0x48,0xB8,0x04,0x20,0x00,0x40,0x11,0x0E,0x77,0xDC,0x27,0x39,0x0E,0x60,0x10,0x00,0x00,0x8F,0x8F,0x9D,0xCF,0xC1,0xF1,0x77,0xA3,0xF8,0xCD,0xD0,0x7C,0x5C,0x47,0x11,0x51,0x27,0xD0,0x21,0x00,0x00,0x7F,0x9E,0xF7,0xA1,0xF1,0x20,0x92,0x4A,0xC5,0xDE,0x7C,0x3C,0x77,0x91,0x51,0x7F,0xC8,0x42,0x00,
  56. 0x00,0x00,0x00,0x00,0x02,0x40,0x00,0x80,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x17,0x03,0xE0,0x00,0x00,0x00,0x00,0x20,0x00,0x80,0x00,0x00,0x10,0x08,0x00,0x00,0x00,0x00,0x08,0x04,0x44,0x00,
  57. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC0,0x07,0x00,0x00,0x00,0x10,0x08,0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x00,};
  58. #endif
  59. #ifdef pgm_read_byte
  60. #define FNT(_x) pgm_read_byte(Verdana_font_11 + (_x))
  61. //#define FNT(_x) pgm_read_byte(monofont + (_x))
  62. #else
  63. #define FNT(_x) Verdana_font_11[_x]
  64. #endif
  65. typedef void (*CharProc)(int x, int y, int width, int height, int color);
  66. int DrawChar(int xx, int yy, int c, int color, CharProc proc = 0)
  67. {
  68. if (proc == 0)
  69. proc = Graphics_::Rectangle;
  70. //byte* f = Verdana_font_11;
  71. if (c == 32)
  72. return FNT(1)>>2; // Space is 1/4 font height (yuk);
  73. int i = c - FNT(3);
  74. if (i < 0 || i >= FNT(4)) return 0;
  75. int src = 0;
  76. int width = FNT(0);
  77. int height = FNT(1);
  78. if (width)
  79. {
  80. src = (6*8) + width*i;
  81. } else {
  82. int ci = 6 + i * 2;
  83. width = (FNT(ci) << 8) | FNT(ci + 1); // simplify
  84. if (i > 0)
  85. {
  86. src = (FNT(ci - 2) << 8) | FNT(ci - 1);
  87. width -= src;
  88. }
  89. src += (6 + 2 * FNT(4)) * 8; // start of pixels (roll into cols)
  90. }
  91. // clip?
  92. byte mask = 0x80 >> (src & 7); // Mask at start of line
  93. int end = (src+width) >> 3; // number of bytes read
  94. src >>= 3;
  95. int makeup = FNT(5) - (end + 1 - src);
  96. for (byte y = 0; y < (byte)height; y++)
  97. {
  98. byte p = FNT(src++);
  99. byte m = mask;
  100. for (byte x = 0; x < (byte)width; x++)
  101. {
  102. if (p & m)
  103. proc(x+xx,y+yy,1,1,color);
  104. //Graphics_::SetPixel(x + xx, y + yy);
  105. m >>= 1;
  106. if (m == 0)
  107. {
  108. p = FNT(src++);
  109. if (p == 0 && (width-x) <= 8) // early out
  110. break;
  111. m = 0x80;
  112. }
  113. }
  114. src += makeup;
  115. }
  116. return width;
  117. }
  118. int MeasureChar(int c)
  119. {
  120. if (FNT(0))
  121. return FNT(0); // monospaced font
  122. if (c == 32)
  123. return FNT(1)>>2; // Space is 1/4 font height (yuk);
  124. int i = c - FNT(3);
  125. if (i < 0 || i >= FNT(4)) return 0;
  126. int ci = 6 + i * 2;
  127. int width = (FNT(ci) << 8) | FNT(ci + 1); // simplify
  128. int src = 0;
  129. if (i > 0)
  130. {
  131. src = (FNT(ci - 2) << 8) | FNT(ci - 1);
  132. width -= src;
  133. }
  134. return width;
  135. }
  136. // Nasty Dot text drawing hack
  137. #define DOTSCALE 2
  138. typedef void (*CharProc)(int x, int y, int width, int height, int color);
  139. int DrawChar(int xx, int yy, int c, CharProc proc);
  140. static void DotProc(int x, int y, int width, int height, int color)
  141. {
  142. x *= DOTSCALE*2;
  143. y *= DOTSCALE*2;
  144. Graphics_::Circle(x,y,DOTSCALE,color,true);
  145. }
  146. void DotDrawStr(const char* s, int len, int x, int y, int dotColor, bool erase, int bgColor)
  147. {
  148. if (erase)
  149. Graphics_::Rectangle(x*DOTSCALE*2 - DOTSCALE,y*DOTSCALE*2 - DOTSCALE,len*DOTSCALE*2*7,DOTSCALE*2*10,bgColor);
  150. for (u8 i = 0; i < len; i++)
  151. x += DrawChar(x,y,s[i],dotColor,DotProc) + 1;
  152. }
  153. int Graphics_::Width()
  154. {
  155. return LCD.Width();
  156. }
  157. int Graphics_::Height()
  158. {
  159. return LCD.Height();
  160. }
  161. void Graphics_::OpenBounds()
  162. {
  163. LCD.SetBounds(0,0,LCD.Width(),LCD.Height());
  164. }
  165. void Graphics_::SetBounds(int x, int y, int width, int height)
  166. {
  167. LCD.SetBounds(x,y,width,height);
  168. }
  169. void Graphics_::Fill(int color, u32 count)
  170. {
  171. LCD.Fill(color,count);
  172. }
  173. void Graphics_::Blit(const u8* data, u32 count)
  174. {
  175. LCD.Blit(data,count);
  176. }
  177. void Graphics_::BlitIndexed(const u8* data, const u8* palette, u32 count)
  178. {
  179. LCD.BlitIndexed(data,palette,count);
  180. }
  181. void Graphics_::Scroll(int y)
  182. {
  183. LCD.Scroll(y);
  184. }
  185. void Graphics_::Direction(u8 rot, u8 dx, u8 dy)
  186. {
  187. LCD.Direction(rot,dx,dy);
  188. }
  189. void Graphics_::Clear(int color)
  190. {
  191. Rectangle(0,0,Width(),Height(),color);
  192. Scroll(0);
  193. }
  194. int Graphics_::MeasureString(const char* s, int len)
  195. {
  196. int x = 0;
  197. while (len--)
  198. x += MeasureChar(*s++) + 1;
  199. return x;
  200. }
  201. int Graphics_::DrawString(const char* s, int len, int x, int y, int color)
  202. {
  203. for (int i = 0; i < len; i++)
  204. x += DrawChar(x,y,s[i],color) + 1;
  205. return x;
  206. }
  207. int Graphics_::DrawString(const char* s, int x, int y, int color)
  208. {
  209. return DrawString(s,strlen(s),x,y,color);
  210. }
  211. void Graphics_::Init()
  212. {
  213. LCD.Init();
  214. }
  215. void Graphics_::Rectangle(int x, int y, int width, int height, int color)
  216. {
  217. // Clip
  218. int right = x + width;
  219. int bottom = y + height;
  220. right = min(right,(int)Graphics.Width());
  221. bottom = min(bottom,(int)Graphics.Height());
  222. x = max(x,0);
  223. y = max(y,0);
  224. width = right - x;
  225. height = bottom - y;
  226. if (width <= 0 || height <= 0)
  227. return;
  228. LCD.SetBounds(x,y,width,height);
  229. LCD.Fill(color,(u32)width*height);
  230. }
  231. static void Draw4(int cx, int cy, int x, int y, int color)
  232. {
  233. Graphics_::SetPixel(cx + x, cy + y,color);
  234. if (x != 0) Graphics_::SetPixel(cx - x, cy + y,color);
  235. if (y != 0) Graphics_::SetPixel(cx + x, cy - y,color);
  236. Graphics_::SetPixel(cx - x, cy - y,color);
  237. }
  238. static void Draw8(int cx, int cy, int x, int y, int color, bool fill)
  239. {
  240. if (fill)
  241. {
  242. Graphics_::Rectangle(cx-y,cy-x,y<<1,1,color);
  243. Graphics_::Rectangle(cx-x,cy-y,x<<1,1,color);
  244. Graphics_::Rectangle(cx-x,cy+y,x<<1,1,color);
  245. Graphics_::Rectangle(cx-y,cy+x,y<<1,1,color);
  246. } else {
  247. Draw4(cx, cy, x, y, color);
  248. if (x != y)
  249. Draw4(cx, cy, y, x, color);
  250. }
  251. }
  252. // Octant circle drawing
  253. void Graphics_::Circle(int cx, int cy, int radius, int color, bool fill)
  254. {
  255. int error = -radius;
  256. int x = radius;
  257. int y = 0;
  258. // draws in 4 strips from top down and middle up + reflection about y
  259. while (x >= y)
  260. {
  261. Draw8(cx, cy, x, y,color,fill);
  262. error += y;
  263. ++y;
  264. error += y;
  265. if (error >= 0)
  266. {
  267. --x;
  268. error -= x<<1;
  269. }
  270. }
  271. }
  272. void Graphics_::SetPixel(int x, int y, int color)
  273. {
  274. Graphics_::Rectangle(x,y,1,1,color);
  275. }
  276. bool cmp(const byte* d, const char* s)
  277. {
  278. while (*s)
  279. if (*d++ != *s++)
  280. return false;
  281. return true;
  282. }
  283. // FUSSY; needs cleanup
  284. // img2
  285. // 0 width 32
  286. // 4 height 32
  287. // 8 format 8
  288. // 9 reserved 8
  289. // 10 colors 8
  290. // 11 restartInterval 8
  291. // 12 reserved 32
  292. // 16 palette - restarts
  293. // TODO: backing up on repeated lines
  294. void CheckStack(void* t, int len);
  295. void Graphics_::DrawImage(File& f, int x, int y, int scroll, int lines)
  296. {
  297. //File f;
  298. //char s[256];
  299. //sprintf(s,"Draw %d %d %d %d\n",x,y,scroll,lines);
  300. //print(s);
  301. Img2 hdr;
  302. if (!f.Read(&hdr,sizeof(Img2)) || !cmp(hdr.sig,"img2"))
  303. return;
  304. // Palettes are bad news on small machines
  305. // Removing and replacing with new runlength format
  306. // Load palette
  307. // byte palette[512];
  308. int paletteLen = (hdr.colors*2 + 3) & ~3;
  309. f.Skip(paletteLen); // Align to 4
  310. //f.Read(palette+2,paletteLen); // Align to 4
  311. int width = hdr.width;
  312. if (lines == 0)
  313. lines = hdr.height;
  314. int maxy = min(hdr.height,lines) + y;
  315. // Skip restart indexes
  316. if (hdr.restartInterval)
  317. {
  318. int restarts = (hdr.height + hdr.restartInterval - 1)/hdr.restartInterval + 1;
  319. if (scroll > 0) //
  320. {
  321. int i = min(scroll/hdr.restartInterval,restarts-1);
  322. scroll -= i*hdr.restartInterval;
  323. maxy -= i*hdr.restartInterval;
  324. f.Skip(i*4);
  325. ulong restart;
  326. f.Read(&restart,4);
  327. f.SetPos(restart);
  328. } else
  329. f.Skip(restarts*4);
  330. }
  331. // Skip restart intervals
  332. if (maxy > 320)
  333. maxy = 320;
  334. LCD.SetBounds(x,y,hdr.width,maxy-y); // TODO: clip
  335. byte format = hdr.format;
  336. if (format & 0x80)
  337. {
  338. f.SetPos(512 + ((long)scroll)*512);
  339. while (y < maxy)
  340. {
  341. // 512 bytes in buffer
  342. int count;
  343. const byte* b = f.GetBuffer(&count);
  344. LCD.Blit(b,width);
  345. f.Skip(512);
  346. y++;
  347. }
  348. return;
  349. }
  350. // Other format deprecated because it was silly
  351. }
  352. u16 Graphics_::ToColor(u8 r, u8 g, u8 b)
  353. {
  354. return TOCOLOR(r,g,b);
  355. }