Skip to content

Commit c722d16

Browse files
committed
MEGA65: unfinished VIC4 alpha blend for FCM #437
Not working 100% correctly yet ...
1 parent 5a9dc9e commit c722d16

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

targets/mega65/vic4.c

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,23 @@ static XEMU_INLINE void vic4_do_sprites ( void )
12681268
}
12691269

12701270

1271+
static XEMU_INLINE Uint32 blend32 ( const Uint32 a, const Uint32 b, const Uint8 s )
1272+
{
1273+
// Used by the FCM alpha-blend renderer on SDL2 colour representation (RGBA
1274+
// per bytes within 32 bit unsigned integers). Technically I wouldn't need
1275+
// to "blend" the alpha channel, however I cannot be sure which byte is alpha
1276+
// (platform dependent, byte order ...). But since alpha channel is always $FF
1277+
// for "A" and "B" too, blending those wouldn't hurt. And btw, do not confuse
1278+
// the "alpha" in RGBA and the nature of alpha-blending, independent things.
1279+
return
1280+
(( a & 0xFF) * s + ( b & 0xFF) * (255U - s)) / 255U +
1281+
(((((a >> 8) & 0xFF) * s + ((b >> 8) & 0xFF) * (255U - s)) / 255U) << 8) +
1282+
(((((a >> 16) & 0xFF) * s + ((b >> 16) & 0xFF) * (255U - s)) / 255U) << 16) +
1283+
(((( a >> 24 ) * s + ( b >> 24 ) * (255U - s)) / 255U) << 24);
1284+
}
1285+
1286+
1287+
12711288
// Render a monochrome character cell row
12721289
// flip = 00 Dont flip, 01 = flip vertical, 10 = flip horizontal, 11 = flip both
12731290
static XEMU_INLINE void vic4_render_mono_char_row ( Uint8 char_byte, const int glyph_width, const Uint8 bg_color, Uint8 fg_color, Uint8 vic3attr )
@@ -1339,6 +1356,16 @@ static XEMU_INLINE void vic4_render_fullcolor_char_row ( const Uint8* char_row,
13391356
}
13401357

13411358

1359+
static XEMU_INLINE void vic4_render_fullcolor_char_row_with_alpha ( const Uint8* char_row_ptr, const int glyph_width, const Uint32 bg_sdl_color, const Uint32 fg_sdl_color, const int hflip )
1360+
{
1361+
for (float cx = 0; cx < glyph_width && xcounter < border_x_right; cx += char_x_step) {
1362+
const Uint8 char_data = draw_mask & char_row_ptr[XEMU_LIKELY(!hflip) ? (int)cx : glyph_width - 1 - (int)cx];
1363+
*current_pixel++ = blend32(fg_sdl_color, bg_sdl_color, char_data);
1364+
is_fg[xcounter++] = char_data;
1365+
}
1366+
}
1367+
1368+
13421369
// 16-color (Nybl) mode (4-bit per pixel / 16 pixel wide characters)
13431370
static XEMU_INLINE void vic4_render_16color_char_row ( const Uint8* char_row, const int glyph_width, const Uint32 bg_sdl_color, const Uint32 fg_sdl_color, const Uint32 *palette16, const int hflip )
13441371
{
@@ -1589,14 +1616,23 @@ static XEMU_INLINE void vic4_render_char_raster ( void )
15891616
// fgcolor in case of FCM should mean colour index $FF
15901617
// FIXME: check if the passed palette[color_data & 0xFF] is correct or another index should be used for that $FF colour stuff
15911618
const Uint32 *palette_now = ((REG_VICIII_ATTRIBS) && SXA_ATTR_ALTPALETTE(color_data)) ? altpalette : used_palette;
1592-
vic4_render_fullcolor_char_row(
1593-
main_ram + (((char_id * 64) + ((sel_char_row + char_fetch_offset) * 8)) & 0x7FFFF),
1594-
8 - glyph_trim,
1595-
palette_now[char_bgcolor], // bg SDL colour
1596-
palette_now[color_data & 0xFF], // fg SDL colour
1597-
SXA_HORIZONTAL_FLIP(color_data), // hflip?
1598-
palette_now
1599-
);
1619+
if (XEMU_UNLIKELY((vic_registers[0x54] & 0x81) == 0x81 && SXA_ALPHA_BLEND(color_data)))
1620+
vic4_render_fullcolor_char_row_with_alpha(
1621+
main_ram + (((char_id * 64) + ((sel_char_row + char_fetch_offset) * 8)) & 0x7FFFF),
1622+
8 - glyph_trim,
1623+
palette_now[char_bgcolor], // bg SDL colour
1624+
palette_now[color_data & 0xFF], // fg SDL colour
1625+
SXA_HORIZONTAL_FLIP(color_data) // hflip?
1626+
);
1627+
else
1628+
vic4_render_fullcolor_char_row(
1629+
main_ram + (((char_id * 64) + ((sel_char_row + char_fetch_offset) * 8)) & 0x7FFFF),
1630+
8 - glyph_trim,
1631+
palette_now[char_bgcolor], // bg SDL colour
1632+
palette_now[color_data & 0xFF], // fg SDL colour
1633+
SXA_HORIZONTAL_FLIP(color_data), // hflip?
1634+
palette_now
1635+
);
16001636
} else if ((REG_MCM && ((color_data & 8) || (vic_registers[0x63] & 0x40))) || (REG_MCM && REG_BMM)) { // Multicolor character
16011637
// using static vars: faster in a rapid loop like this, no need to re-adjust stack pointer all the time to allocate space and this way using constant memory address
16021638
// also, as an optimization, later, some value can be re-used and not always initialized here, when in reality VIC

targets/mega65/vic4.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
182182
#define SXA_TRIM_RIGHT_BITS012(sw) ((sw) >> 13)
183183
#define SXA_VERTICAL_FLIP(cw) ((cw) & 0x8000)
184184
#define SXA_HORIZONTAL_FLIP(cw) ((cw) & 0x4000)
185-
//#define SXA_ALPHA_BLEND(cw) ((cw) & 0x2000)
185+
#define SXA_ALPHA_BLEND(cw) ((cw) & 0x2000)
186186
#define SXA_GOTO_X(cw) ((cw) & 0x1000)
187187
#define SXA_4BIT_PER_PIXEL(cw) ((cw) & 0x0800)
188188
#define SXA_TRIM_RIGHT_BIT3(cw) ((cw) & 0x0400)

0 commit comments

Comments
 (0)