From 1f69edbbf51d1db6ee14b43ea8233a5b1a5b5329 Mon Sep 17 00:00:00 2001 From: churl Date: Sun, 19 Jun 2022 13:18:35 +0200 Subject: [PATCH] implement sprite + line drawing --- c_os/devices/LFBgraphics.cc | 70 +++++++++++++++++++++++++++++++++---- c_os/devices/LFBgraphics.h | 7 ++++ 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/c_os/devices/LFBgraphics.cc b/c_os/devices/LFBgraphics.cc index 648d6c2..94de3d6 100644 --- a/c_os/devices/LFBgraphics.cc +++ b/c_os/devices/LFBgraphics.cc @@ -37,10 +37,8 @@ int abs(int a); * sich in den C-Dateien in fonts/ von selbst. * *****************************************************************************/ inline void LFBgraphics::drawMonoBitmap(unsigned int x, unsigned int y, - unsigned int width, - unsigned int height, - unsigned char* bitmap, - unsigned int color) { + unsigned int width, unsigned int height, + unsigned char* bitmap, unsigned int color) { // Breite in Bytes unsigned short width_byte = width / 8 + ((width % 8 != 0) ? 1 : 0); @@ -95,7 +93,9 @@ void LFBgraphics::drawPixel(unsigned int x, unsigned int y, unsigned int col) { return; } - if (mode == 0) ptr = (unsigned char*)hfb; + if (mode == BUFFER_INVISIBLE) { + ptr = (unsigned char*)hfb; + } // Pixel ausserhalb des sichtbaren Bereichs? if (x < 0 || x >= xres || y < 0 || y > yres) { @@ -134,6 +134,62 @@ void LFBgraphics::drawPixel(unsigned int x, unsigned int y, unsigned int col) { } } +void LFBgraphics::drawStraightLine(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int col) { + // Don't set mode inside the drawing function to use them in animations + + if (x1 == x2 && y2 > y1) { + // Vertical line + for (unsigned int i = y1; i <= y2; ++i) { + this->drawPixel(x1, i, col); + } + } else if (y1 == y2 && x2 > x1) { + // Horizontal line + for (unsigned int i = x1; i <= x2; ++i) { + this->drawPixel(i, y1, col); + } + } else { + kout << "Error (LFBgraphics::drawStraightLine): Line is not straight" << endl; + } +} + +// (x1, y1)-------------------------(x2, y1) +// | | +// | | +// | | +// (x1, y2)-------------------------(x2, y2) +void LFBgraphics::drawRectangle(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int col) { + this->drawStraightLine(x1, y1, x2, y1, col); + this->drawStraightLine(x2, y1, x2, y2, col); + this->drawStraightLine(x1, y2, x2, y2, col); + this->drawStraightLine(x1, y1, x1, y2, col); +} + +void LFBgraphics::drawCircle(unsigned int x, unsigned int y, unsigned int rad, unsigned int col) { + // TODO +} + +void LFBgraphics::drawSprite(unsigned int width, unsigned int height, unsigned int bytes_pp, unsigned char* pixel_data) { + unsigned char* ptr; + for (unsigned int x = 0; x < width; ++x) { + for (unsigned int y = 0; y < height; ++y) { + ptr = (unsigned char*)pixel_data + (x + y * width) * bytes_pp; + + switch (bytes_pp) { + case 2: + // TODO: Never tested, probably doesn't work + this->drawPixel(x, y, RGB_24(*ptr & 0b11111000, (*ptr & 0b111) | (*(ptr + 1) & 0b11100000), + *(ptr + 1) & 0b11111)); // RGB 565 + break; + case 3: + case 4: + // Alpha gets ignored anyway + this->drawPixel(x, y, RGB_24(*ptr, *(ptr + 1), *(ptr + 2))); + break; + } + } + } +} + /***************************************************************************** * Methode: LFBgraphics::clear * *---------------------------------------------------------------------------* @@ -195,7 +251,9 @@ void LFBgraphics::copyHiddenToVisible() { unsigned int* dptr = (unsigned int*)lfb; unsigned int i; - if (hfb == 0 || lfb == 0) return; + if (hfb == 0 || lfb == 0) { + return; + } switch (bpp) { case 8: diff --git a/c_os/devices/LFBgraphics.h b/c_os/devices/LFBgraphics.h index f236f76..74be8ec 100644 --- a/c_os/devices/LFBgraphics.h +++ b/c_os/devices/LFBgraphics.h @@ -48,6 +48,13 @@ public: void drawString(Font& fnt, unsigned int x, unsigned int y, unsigned int col, char* str, unsigned int len); + // TODO: Filled shapes + void drawCircle(unsigned int x, unsigned int y, unsigned int rad, unsigned int col); + void drawStraightLine(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int col); + void drawRectangle(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int col); + + void drawSprite(unsigned int width, unsigned int height, unsigned int bytes_pp, unsigned char* pixel_data); + // stellt ein, ob in den sichtbaren Puffer gezeichnet wird void setDrawingBuff(int v);