From a640c1a76483b6e9bc8d4023d2596c4da10f96d0 Mon Sep 17 00:00:00 2001 From: 0xmac Date: Thu, 23 Jan 2025 11:27:49 +0100 Subject: [PATCH] Reworked rendering system and added dynamic colors --- src/video.c | 131 +++++++++++++++++++++++++++++----------------------- 1 file changed, 73 insertions(+), 58 deletions(-) diff --git a/src/video.c b/src/video.c index c9a43a9..3252211 100644 --- a/src/video.c +++ b/src/video.c @@ -1,5 +1,9 @@ #include #include +#include "config.h" + +#define HIGHCOLOR 255 +#define LOWCOLOR 96 extern float displayScale; @@ -13,24 +17,43 @@ uint8_t videoMode; uint8_t videoA; /* Private Variables */ -static uint8_t videoRow; static uint8_t videoValue; static uint8_t videoColorValue; -static uint32_t videoColorIndex[] = { - 0x000000FF, - 0xFF0000FF, - 0x00FF00FF, - 0xFFFF00FF, - 0x0000FFFF, - 0xFF00FFFF, - 0x00FFFFFF, - 0xFFFFFFFF -}; +static uint32_t videoColorIndex[16]; static uint8_t videoMemory[0x8000]; -static uint32_t renderMemory[(SDL_X_SIZE * SDL_Y_SIZE)]; +uint32_t renderMemory[(SDL_X_SIZE * SDL_Y_SIZE)]; + +void updatePalette(){ + uint8_t accentColors = videoMode & 0b11101110; + for (uint8_t i = 0; i < 16; i++){ + uint32_t rawColor = + ((i & 0b1) ? + HIGHCOLOR - (i & 0x8 ? 0 : LOWCOLOR) : 0) << 24 | + + (((i & 0b10) ? + HIGHCOLOR - (i & 0x8 ? 0 : LOWCOLOR) : 0) << 16) | + + (((i & 0b100) ? + HIGHCOLOR - (i & 0x8 ? 0 : LOWCOLOR) : 0) << 8) | + + 0x000000FF; + + videoColorIndex[i] = rawColor; + } +} + +void initVideo(){ + videoModified = 1; + videoMode = 0x01; + updatePalette(); + + /* Fill VRAM with random values, just for fun */ + for (int i = 0; i < 0x8000; i++) videoMemory[i] = rand(); +} + void writeVideo(){ videoMemory[videoX + (videoY * 128)] = videoA; videoModified = 1; @@ -40,63 +63,55 @@ uint8_t readVideo(){ return videoMemory[videoX + (videoY * 128)]; } -void initVideo(){ - videoModified = 1; - videoMode = 0x01; +void setPixels(uint8_t x, uint8_t y){ + /* Check for Text or Graphics Mode */ + if ((videoMode & 0x01) == 0){ + /* + * Text Address Format + * 0bRRR1111AAAAAAAA + * + * R = row pins + * A = address + */ + + /* Get text index value */ + videoValue = videoMemory[(((y & 0xF8) >> 3) << 7) | x]; + + /* Index the value in font location */ + videoValue = videoMemory[((y & 0x7) | 0b1111000) + (videoValue * 128)]; + videoColorValue = videoMemory[(((y & 0xF8) >> 3) << 7) + (x | 0x40)]; + } + else { + videoValue = videoMemory[(y * 128) + x]; + videoColorValue = videoMemory[(y * 128) + (x | 0x40)]; + } - for (int i = 0; i < 0x8000; i++) videoMemory[i] = rand(); + // Snipping out the upper 8 colors + // videoColorValue &= 0x77; + + for (uint8_t i = 0; i < 8; i++){ + if (displayScale == 1){ + renderMemory[(y * SDL_X_SIZE) + ((x * 8) + i)] = (videoValue & (128 >> i)) ? videoColorIndex[((videoColorValue & 0xF0) >> 4)] : videoColorIndex[(videoColorValue & 0x0F)]; + } else { + for (uint8_t _x = 0; _x < displayScale; _x++){ + for (uint8_t _y = 0; _y < displayScale; _y++){ + renderMemory[(((y * (int)displayScale) + _y) * SDL_X_SIZE) + ((((x * (int)displayScale) * 8) + (i * (int)displayScale)) + _x)] = (videoValue & (128 >> i)) ? videoColorIndex[((videoColorValue & 0xF0) >> 4)] : videoColorIndex[(videoColorValue & 0x0F)]; + } + } + } + } } void updateVideo(){ if (videoModified){ //sfVertexArray_clear(renderArray); - for (uint8_t y = 0; y < 255; y++){ - videoRow = (int8_t)(y & 0x7); - + for (uint16_t y = 0; y < 256; y++){ for (uint8_t x = 0; x < 48; x++){ - - if ((videoMode & 0x01) == 0){ - /* - * Text Address Format - * 0bRRR1111AAAAAAAA - * - * R = row pins - * A = address - */ - - // Get text index value - videoValue = videoMemory[(((y & 0xF8) >> 3) << 7) | x]; - - // Index the value in font location - videoValue = videoMemory[(videoRow | 0b1111000) + (videoValue * 128)]; - videoColorValue = videoMemory[(((y & 0xF8) >> 3) << 7) + (x | 0x40)]; - } - else { - videoValue = videoMemory[(y * 128) + x]; - videoColorValue = videoMemory[(y * 128) + (x | 0x40)]; - } - - // Snipping out the upper 8 colors - videoColorValue &= 0x77; - - for (uint8_t i = 0; i < 8; i++){ - if (displayScale == 1){ - renderMemory[(y * SDL_X_SIZE) + ((x * 8) + i)] = (videoValue & (128 >> i)) ? videoColorIndex[((videoColorValue & 0xF0) >> 4)] : videoColorIndex[(videoColorValue & 0x0F)]; - } else { - for (uint8_t _x = 0; _x < displayScale; _x++){ - for (uint8_t _y = 0; _y < displayScale; _y++){ - renderMemory[(((y * (int)displayScale) + _y) * SDL_X_SIZE) + ((((x * (int)displayScale) * 8) + (i * (int)displayScale)) + _x)] = (videoValue & (128 >> i)) ? videoColorIndex[((videoColorValue & 0xF0) >> 4)] : videoColorIndex[(videoColorValue & 0x0F)]; - } - } - } - } + setPixels(x, (uint8_t)y); } } videoModified = 0; } } - - -