Skip to content

Commit

Permalink
lib/graphic: Use pitch instead of horizontal resolution to draw pixel…
Browse files Browse the repository at this point in the history
…s (Fixes pixel drawing on some exotic graphic modes

Co-authored-by: Christian Gesse <christian.gesse@hhu.de>
  • Loading branch information
fruhland and chges100 committed Sep 8, 2023
1 parent 4e1759f commit 781c32c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion loader/grub/boot/grub/grub.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ else
fi

menuentry "hhuOS" {
multiboot2 /boot/hhuOS.bin log_level=dbg log_ports=COM1 root=ide0p0,Filesystem::Fat::FatDriver
multiboot2 /boot/hhuOS.bin log_level=inf log_ports=COM1 root=ide0p0,Filesystem::Fat::FatDriver
module2 /boot/hhuOS.initrd initrd
}
2 changes: 1 addition & 1 deletion loader/towboot/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>

readonly TOWBOOT_VERSION="0.6.0"
readonly TOWBOOT_VERSION="0.6.1"
readonly FILE_LIST=("towboot-ia32.efi" "towboot-x64.efi" "hhuOS.bin" "hhuOS.initrd" "towboot.toml")

if [[ ! -f "towboot-ia32.efi" || ! -f "towboot-x64.efi" ]]; then
Expand Down
2 changes: 1 addition & 1 deletion src/lib/util/game/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ void Graphics::show() const {
} else {
auto pitch = lfb.getPitch();
auto colorDepthDivisor = (lfb.getColorDepth() == 15 ? 16 : lfb.getColorDepth()) / 8;
auto xOffset = static_cast<uint32_t>(game.getCurrentScene().getCamera().getPosition().getX() * pitch / 4) % pitch;
auto xOffset = static_cast<uint32_t>(game.getCurrentScene().getCamera().getPosition().getX() * static_cast<uint32_t>(pitch / colorDepthDivisor)) % pitch;
xOffset -= xOffset % colorDepthDivisor;

for (uint32_t i = 0; i < lfb.getResolutionY(); i++) {
Expand Down
22 changes: 11 additions & 11 deletions src/lib/util/graphic/PixelDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace Util::Graphic {

PixelDrawer::PixelDrawer(const LinearFrameBuffer &lfb): lfb(lfb), lfbBuffer(reinterpret_cast<uint8_t*>(lfb.getBuffer().get())), lfbWidth(lfb.getResolutionX()) {
PixelDrawer::PixelDrawer(const LinearFrameBuffer &lfb): lfb(lfb), lfbBuffer(reinterpret_cast<uint8_t*>(lfb.getBuffer().get())), lfbPitch(lfb.getPitch()) {
switch (lfb.getColorDepth()) {
case 15:
drawFunction = &drawPixel15Bit;
Expand Down Expand Up @@ -56,33 +56,33 @@ void PixelDrawer::drawPixel(uint16_t x, uint16_t y, const Color &color) const {

// Blend if necessary and draw pixel
if (color.getAlpha() < 255) {
drawFunction(lfbBuffer, lfbWidth, x, y, lfb.readPixel(x, y).blend(color));
drawFunction(lfbBuffer, lfbPitch, x, y, lfb.readPixel(x, y).blend(color));
} else {
drawFunction(lfbBuffer, lfbWidth, x, y, color);
drawFunction(lfbBuffer, lfbPitch, x, y, color);
}
}

void PixelDrawer::drawPixel15Bit(uint8_t *const buffer, const uint16_t width, const uint16_t x, const uint16_t y, const Color &color) {
const auto offset = x + y * width;
void PixelDrawer::drawPixel15Bit(uint8_t *const buffer, const uint16_t pitch, const uint16_t x, const uint16_t y, const Color &color) {
const auto offset = x + y * (pitch / 2);
reinterpret_cast<uint16_t*>(buffer)[offset] = color.getRGB15();
}

void PixelDrawer::drawPixel16Bit(uint8_t *const buffer, const uint16_t width, const uint16_t x, const uint16_t y, const Color &color) {
const auto offset = x + y * width;
void PixelDrawer::drawPixel16Bit(uint8_t *const buffer, const uint16_t pitch, const uint16_t x, const uint16_t y, const Color &color) {
const auto offset = x + y * (pitch / 2);
reinterpret_cast<uint16_t*>(buffer)[offset] = color.getRGB16();
}

void PixelDrawer::drawPixel24Bit(uint8_t *const buffer, const uint16_t width, const uint16_t x, const uint16_t y, const Color &color) {
void PixelDrawer::drawPixel24Bit(uint8_t *const buffer, const uint16_t pitch, const uint16_t x, const uint16_t y, const Color &color) {
uint32_t rgbColor = color.getRGB24();
const auto offset = x * 3 + y * width * 3;
const auto offset = x * 3 + y * pitch;

buffer[offset] = rgbColor & 0xff;
buffer[offset + 1] = (rgbColor >> 8) & 0xff;
buffer[offset + 2] = (rgbColor >> 16) & 0xff;
}

void PixelDrawer::drawPixel32Bit(uint8_t *const buffer, const uint16_t width, const uint16_t x, const uint16_t y, const Color &color) {
const auto offset = x + y * width;
void PixelDrawer::drawPixel32Bit(uint8_t *const buffer, const uint16_t pitch, const uint16_t x, const uint16_t y, const Color &color) {
const auto offset = x + y * (pitch / 4);
reinterpret_cast<uint32_t*>(buffer)[offset] = color.getRGB32();
}

Expand Down
10 changes: 5 additions & 5 deletions src/lib/util/graphic/PixelDrawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ class PixelDrawer {

private:

static void drawPixel15Bit(uint8_t *buffer, uint16_t width, uint16_t x, uint16_t y, const Color &color);
static void drawPixel15Bit(uint8_t *buffer, uint16_t pitch, uint16_t x, uint16_t y, const Color &color);

static void drawPixel16Bit(uint8_t *buffer, uint16_t width, uint16_t x, uint16_t y, const Color &color);
static void drawPixel16Bit(uint8_t *buffer, uint16_t pitch, uint16_t x, uint16_t y, const Color &color);

static void drawPixel24Bit(uint8_t *buffer, uint16_t width, uint16_t x, uint16_t y, const Color &color);
static void drawPixel24Bit(uint8_t *buffer, uint16_t pitch, uint16_t x, uint16_t y, const Color &color);

static void drawPixel32Bit(uint8_t *buffer, uint16_t width, uint16_t x, uint16_t y, const Color &color);
static void drawPixel32Bit(uint8_t *buffer, uint16_t pitch, uint16_t x, uint16_t y, const Color &color);

const LinearFrameBuffer &lfb;
uint8_t *const lfbBuffer;
const uint16_t lfbWidth;
const uint16_t lfbPitch;

void (*drawFunction)(uint8_t *buffer, uint16_t width, uint16_t x, uint16_t y, const Color &color);
};
Expand Down

0 comments on commit 781c32c

Please sign in to comment.