Files
SplenkOS/kernel/src/BasicRenderer.cpp

152 lines
4.3 KiB
C++
Raw Normal View History

2026-01-28 09:13:12 +11:00
#include "BasicRenderer.h"
2026-01-29 08:36:04 +11:00
BasicRenderer* GlobalRenderer;
2026-01-28 09:13:12 +11:00
BasicRenderer::BasicRenderer(Framebuffer* framebuffer, PSF1_FONT* psf1_Font) {
targetFramebuffer = framebuffer;
PSF1_Font = psf1_Font;
2026-01-28 19:34:59 +11:00
Colour = 0xFFFFFFFF;
2026-01-28 09:13:12 +11:00
cursorPosition = {0,0};
}
2026-01-28 19:34:59 +11:00
void BasicRenderer::PutChar(char chr, unsigned int xOff, unsigned int yOff) {
2026-01-28 09:13:12 +11:00
unsigned int* pixPtr = (unsigned int*)targetFramebuffer->BaseAddress;
char* fontPtr = (char*)PSF1_Font->glyphBuffer + (chr * PSF1_Font->psf1_Header->charsize);
for (unsigned long y = yOff; y < yOff + 18; y++) {
for (unsigned long x = xOff; x < xOff+8; x++) {
if ((*fontPtr & (0b10000000 >> (x - xOff))) > 0) {
2026-01-28 19:34:59 +11:00
*(unsigned int*)(pixPtr + x + (y * targetFramebuffer->PixelsPerScanline)) = Colour;
2026-01-28 09:13:12 +11:00
}
}
fontPtr++;
}
}
2026-01-29 12:20:41 +11:00
void BasicRenderer::PutChar(char chr) {
PutChar(chr, cursorPosition.x, cursorPosition.y);
cursorPosition.x += 8;
if (cursorPosition.x + 8 > targetFramebuffer->Width) {
cursorPosition.x = 0;
cursorPosition.y += 16;
}
}
2026-01-28 19:34:59 +11:00
void BasicRenderer::Print(const char* str) {
2026-01-28 09:13:12 +11:00
unsigned int x = 0;
char* chr = (char*)str;
while (*chr != 0) {
2026-01-28 19:34:59 +11:00
PutChar(*chr, cursorPosition.x, cursorPosition.y);
2026-01-28 09:13:12 +11:00
cursorPosition.x+=8;
if (cursorPosition.x + 8 > targetFramebuffer->Width) {
cursorPosition.x = 0;
cursorPosition.y += 18;
}
chr++;
}
2026-01-29 09:02:15 +11:00
}
2026-01-29 13:34:34 +11:00
void BasicRenderer::PutPixel(uint32_t x, uint32_t y, uint32_t colour) {
*(uint32_t*)((uint64_t)targetFramebuffer->BaseAddress + (x*4) + (y * targetFramebuffer->PixelsPerScanline * 4)) = colour;
}
2026-01-29 14:34:19 +11:00
uint32_t BasicRenderer::GetPixel(uint32_t x, uint32_t y){
return *(uint32_t*)((uint64_t)targetFramebuffer->BaseAddress + (x*4) + (y * targetFramebuffer->PixelsPerScanline * 4));
}
void BasicRenderer::ClearMouseCursor(uint8_t* mouseCursor, Point position) {
int xMax = 16;
int yMax = 19;
int diffX = targetFramebuffer->Width - position.x;
int diffY = targetFramebuffer->Height - position.y;
if (diffX < 16) xMax = diffX;
if (diffY < 19) yMax = diffY;
for (int y = 0; y < yMax; y++) {
for (int x = 0; x < xMax; x++) {
int bit = y * 16 + x;
int byte = bit / 8;
if (mouseCursor[byte] & (0b10000000 >> (x % 8))) {
PutPixel(position.x + x, position.y + y, mouseCursorBuffer[x + y*16]);
}
}
}
}
2026-01-29 13:34:34 +11:00
void BasicRenderer::DrawOverlayMouseCursor(uint8_t* mouseCursor, Point position, uint32_t colour) {
int xMax = 16;
int yMax = 19;
int diffX = targetFramebuffer->Width - position.x;
int diffY = targetFramebuffer->Height - position.y;
if (diffX < 16) xMax = diffX;
if (diffY < 19) yMax = diffY;
for (int y = 0; y < yMax; y++) {
for (int x = 0; x < xMax; x++) {
int bit = y * 16 + x;
int byte = bit / 8;
if (mouseCursor[byte] & (0b10000000 >> (x % 8))) {
2026-01-29 14:34:19 +11:00
mouseCursorBuffer[x + y*16] = GetPixel(position.x + x, position.y + y);
2026-01-29 13:34:34 +11:00
PutPixel(position.x + x, position.y + y, colour);
}
}
}
}
2026-01-29 12:20:41 +11:00
void BasicRenderer::Clear() {
2026-01-29 09:02:15 +11:00
uint64_t fbBase = (uint64_t)targetFramebuffer->BaseAddress;
uint64_t bytesPerScanline = targetFramebuffer->PixelsPerScanline * 4;
uint64_t fbHeight = targetFramebuffer->Height;
uint64_t fbSize = targetFramebuffer->BufferSize;
for (int verticalScanline = 0; verticalScanline < fbHeight; verticalScanline ++){
uint64_t pixPtrBase = fbBase + (bytesPerScanline * verticalScanline);
for (uint32_t* pixPtr = (uint32_t*)pixPtrBase; pixPtr < (uint32_t*)(pixPtrBase + bytesPerScanline); pixPtr ++){
2026-01-29 12:20:41 +11:00
*pixPtr = ClearColour;
2026-01-29 09:02:15 +11:00
}
}
}
2026-01-29 12:20:41 +11:00
void BasicRenderer::ClearChar(){
if (cursorPosition.x == 0){
cursorPosition.x = targetFramebuffer->Width;
cursorPosition.y -= 16;
if (cursorPosition.y < 0) cursorPosition.y = 0;
}
unsigned int xOff = cursorPosition.x;
unsigned int yOff = cursorPosition.y;
unsigned int* pixPtr = (unsigned int*)targetFramebuffer->BaseAddress;
for (unsigned long y = yOff; y < yOff + 16; y++){
for (unsigned long x = xOff - 8; x < xOff; x++){
*(unsigned int*)(pixPtr + x + (y * targetFramebuffer->PixelsPerScanline)) = ClearColour;
}
}
cursorPosition.x -= 8;
if (cursorPosition.x < 0){
cursorPosition.x = targetFramebuffer->Width;
cursorPosition.y -= 16;
if (cursorPosition.y < 0) cursorPosition.y = 0;
}
}
2026-01-29 09:02:15 +11:00
void BasicRenderer::Next() {
cursorPosition.x = 0;
cursorPosition.y += 16;
2026-01-28 09:13:12 +11:00
}