@@ -78,7 +78,7 @@ void GraphicsSoft::setSize(int w, int h) {
7878 _v = (h << 16 ) / GFX_H;
7979 _w = w;
8080 _h = h;
81- _byteDepth = _use565 ? 2 : 1 ;
81+ _byteDepth = _use555 ? 2 : 1 ;
8282 assert (_byteDepth == 1 || _byteDepth == 2 );
8383 _colorBuffer = (uint16_t *)realloc (_colorBuffer, _w * _h * sizeof (uint16_t ));
8484 if (!_colorBuffer) {
@@ -189,7 +189,7 @@ void GraphicsSoft::drawChar(uint8_t c, uint16_t x, uint16_t y, uint8_t color) {
189189 }
190190 }
191191 } else if (_byteDepth == 2 ) {
192- const uint16_t rgbColor = _pal[color].rgb565 ();
192+ const uint16_t rgbColor = _pal[color].rgb555 ();
193193 for (int j = 0 ; j < 8 ; ++j) {
194194 const uint8_t ch = ft[j];
195195 for (int i = 0 ; i < 8 ; ++i) {
@@ -202,19 +202,22 @@ void GraphicsSoft::drawChar(uint8_t c, uint16_t x, uint16_t y, uint8_t color) {
202202 }
203203}
204204
205- static void blend_rgb565 (uint16_t *dst, const uint16_t b) {
206- static const uint16_t RB_MASK = 0xf81f ;
207- static const uint16_t G_MASK = 0x07e0 ;
208- uint16_t r, a = *dst;
209- r = (((a & RB_MASK) + (b & RB_MASK)) >> 1 ) & RB_MASK;
210- r |= (((a & G_MASK) + (b & G_MASK)) >> 1 ) & G_MASK;
211- *dst = r;
205+ static void blend_rgb555 (uint16_t *dst, const uint16_t b) {
206+ static const uint16_t RB_MASK = 0x7c1f ;
207+ static const uint16_t G_MASK = 0x03e0 ;
208+ uint16_t a = *dst;
209+ if ((a & 0x8000 ) == 0 ) { // use bit 15 to prevent additive blending
210+ uint16_t r = 0x8000 ;
211+ r |= (((a & RB_MASK) + (b & RB_MASK)) >> 1 ) & RB_MASK;
212+ r |= (((a & G_MASK) + (b & G_MASK)) >> 1 ) & G_MASK;
213+ *dst = r;
214+ }
212215}
213216
214217void GraphicsSoft::drawPoint (int16_t x, int16_t y, uint8_t color) {
215218 x = xScale (x);
216219 y = yScale (y);
217- const int offset = y * _w + x * _byteDepth;
220+ const int offset = ( y * _w + x) * _byteDepth;
218221 if (_byteDepth == 1 ) {
219222 switch (color) {
220223 case COL_ALPHA:
@@ -230,13 +233,13 @@ void GraphicsSoft::drawPoint(int16_t x, int16_t y, uint8_t color) {
230233 } else if (_byteDepth == 2 ) {
231234 switch (color) {
232235 case COL_ALPHA:
233- blend_rgb565 ((uint16_t *)(_drawPagePtr + offset), _pal[12 ]. rgb565 ());
236+ blend_rgb555 ((uint16_t *)(_drawPagePtr + offset), _pal[ALPHA_COLOR_INDEX]. rgb555 ());
234237 break ;
235238 case COL_PAGE:
236239 *(uint16_t *)(_drawPagePtr + offset) = *(uint16_t *)(_pagePtrs[0 ] + offset);
237240 break ;
238241 default :
239- *(uint16_t *)(_drawPagePtr + offset) = _pal[color].rgb565 ();
242+ *(uint16_t *)(_drawPagePtr + offset) = _pal[color].rgb555 ();
240243 break ;
241244 }
242245 }
@@ -252,10 +255,10 @@ void GraphicsSoft::drawLineT(int16_t x1, int16_t x2, int16_t y, uint8_t color) {
252255 _drawPagePtr[offset + i] |= 8 ;
253256 }
254257 } else if (_byteDepth == 2 ) {
255- const uint16_t rgbColor = _pal[12 ]. rgb565 ();
258+ const uint16_t rgbColor = _pal[ALPHA_COLOR_INDEX]. rgb555 ();
256259 uint16_t *p = (uint16_t *)(_drawPagePtr + offset);
257260 for (int i = 0 ; i < w; ++i) {
258- blend_rgb565 (p + i, rgbColor);
261+ blend_rgb555 (p + i, rgbColor);
259262 }
260263 }
261264}
@@ -268,7 +271,7 @@ void GraphicsSoft::drawLineN(int16_t x1, int16_t x2, int16_t y, uint8_t color) {
268271 if (_byteDepth == 1 ) {
269272 memset (_drawPagePtr + offset, color, w);
270273 } else if (_byteDepth == 2 ) {
271- const uint16_t rgbColor = _pal[color].rgb565 ();
274+ const uint16_t rgbColor = _pal[color].rgb555 ();
272275 uint16_t *p = (uint16_t *)(_drawPagePtr + offset);
273276 for (int i = 0 ; i < w; ++i) {
274277 p[i] = rgbColor;
@@ -331,7 +334,7 @@ void GraphicsSoft::drawBitmap(int buffer, const uint8_t *data, int w, int h, int
331334 }
332335 break ;
333336 case 2 :
334- if (fmt == FMT_RGB565 && _w == w && _h == h) {
337+ if (fmt == FMT_RGB555 && _w == w && _h == h) {
335338 memcpy (getPagePtr (buffer), data, getPageSize ());
336339 }
337340 break ;
@@ -357,7 +360,7 @@ void GraphicsSoft::clearBuffer(int num, uint8_t color) {
357360 if (_byteDepth == 1 ) {
358361 memset (getPagePtr (num), color, getPageSize ());
359362 } else if (_byteDepth == 2 ) {
360- const uint16_t rgbColor = _pal[color].rgb565 ();
363+ const uint16_t rgbColor = _pal[color].rgb555 ();
361364 uint16_t *p = (uint16_t *)getPagePtr (num);
362365 for (int i = 0 ; i < _w * _h; ++i) {
363366 p[i] = rgbColor;
@@ -378,7 +381,7 @@ void GraphicsSoft::copyBuffer(int dst, int src, int vscroll) {
378381 }
379382}
380383
381- static void dumpBuffer565 (const uint16_t *src, int w, int h, int num) {
384+ static void dumpBuffer555 (const uint16_t *src, int w, int h, int num) {
382385 char name[32 ];
383386 snprintf (name, sizeof (name), " screenshot-%d.tga" , num);
384387 saveTGA (name, src, w, h);
@@ -391,7 +394,7 @@ static void dumpPalette555(uint16_t *dst, int w, const Color *pal) {
391394 uint16_t *p = dst + (color & 7 ) * SZ;
392395 for (int y = 0 ; y < SZ; ++y) {
393396 for (int x = 0 ; x < SZ; ++x) {
394- p[x] = pal[color].rgb565 ();
397+ p[x] = pal[color].rgb555 ();
395398 }
396399 p += w;
397400 }
@@ -408,22 +411,22 @@ void GraphicsSoft::drawBuffer(int num, SystemStub *stub) {
408411 if (_byteDepth == 1 ) {
409412 const uint8_t *src = getPagePtr (num);
410413 for (int i = 0 ; i < _w * _h; ++i) {
411- _colorBuffer[i] = _pal[src[i]].rgb565 ();
414+ _colorBuffer[i] = _pal[src[i]].rgb555 ();
412415 }
413416 if (0 ) {
414417 dumpPalette555 (_colorBuffer, _w, _pal);
415418 }
416- stub->setScreenPixels565 (_colorBuffer, _w, _h);
419+ stub->setScreenPixels555 (_colorBuffer, _w, _h);
417420 if (_screenshot) {
418- dumpBuffer565 (_colorBuffer, _w, _h, _screenshotNum);
421+ dumpBuffer555 (_colorBuffer, _w, _h, _screenshotNum);
419422 ++_screenshotNum;
420423 _screenshot = false ;
421424 }
422425 } else if (_byteDepth == 2 ) {
423426 const uint16_t *src = (uint16_t *)getPagePtr (num);
424- stub->setScreenPixels565 (src, _w, _h);
427+ stub->setScreenPixels555 (src, _w, _h);
425428 if (_screenshot) {
426- dumpBuffer565 (src, _w, _h, _screenshotNum);
429+ dumpBuffer555 (src, _w, _h, _screenshotNum);
427430 ++_screenshotNum;
428431 _screenshot = false ;
429432 }
@@ -434,7 +437,7 @@ void GraphicsSoft::drawBuffer(int num, SystemStub *stub) {
434437void GraphicsSoft::drawRect (int num, uint8_t color, const Point *pt, int w, int h) {
435438 assert (_byteDepth == 2 );
436439 setWorkPagePtr (num);
437- const uint16_t rgbColor = _pal[color].rgb565 ();
440+ const uint16_t rgbColor = _pal[color].rgb555 ();
438441 const int x1 = xScale (pt->x );
439442 const int y1 = yScale (pt->y );
440443 const int x2 = xScale (pt->x + w - 1 );
0 commit comments