From 30411b4b7895f01d10afe8cfc184b6b8a894cd62 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 1 Oct 2022 13:08:47 +0200 Subject: [PATCH] Fix hackiness in converting a uint32_t to a Color, by providing a function to do so --- kernel/include/render/Color.h | 2 ++ kernel/src/main.cpp | 6 ++---- kernel/src/render/Color.cpp | 12 +++++++++++- kernel/src/sys/gfx.cpp | 3 +-- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/kernel/include/render/Color.h b/kernel/include/render/Color.h index fbb83386..93b2bf76 100644 --- a/kernel/include/render/Color.h +++ b/kernel/include/render/Color.h @@ -17,4 +17,6 @@ struct Color static Color Cyan; static Color Magenta; static Color Gray; + + static Color from_integer(uint32_t source); } __attribute__((packed)); // to reinterpret this as a uint32_t AARRGGBB (in reversed order here because endianness) \ No newline at end of file diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 38927971..75e54b2e 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -79,7 +79,6 @@ extern "C" void _start() { sleep(2); uint32_t color = (uint32_t)Mersenne::get(); - uint32_t* colptr = &color; x += xvel; y += yvel; if ((x + 10) >= framebuffer0.width()) @@ -102,7 +101,7 @@ extern "C" void _start() yvel = -yvel; y = 0; } - framebuffer0.paint_rect(x, y, 10, 10, *(Color*)colptr); + framebuffer0.paint_rect(x, y, 10, 10, Color::from_integer(color)); } }); @@ -111,10 +110,9 @@ extern "C" void _start() { sleep(100); uint32_t color = (uint32_t)Mersenne::get(); - uint32_t* colptr = &color; framebuffer0.paint_rect(Mersenne::get() % (framebuffer0.width() - 256), Mersenne::get() % (framebuffer0.height() - 256), Mersenne::get() % 255, - Mersenne::get() % 255, *(Color*)colptr); + Mersenne::get() % 255, Color::from_integer(color)); } }); diff --git a/kernel/src/render/Color.cpp b/kernel/src/render/Color.cpp index 737c9610..505b8516 100644 --- a/kernel/src/render/Color.cpp +++ b/kernel/src/render/Color.cpp @@ -8,4 +8,14 @@ Color Color::Blue = {0xFF, 0x00, 0x00, 0xFF}; Color Color::Yellow = {0x00, 0xFF, 0xFF, 0xFF}; Color Color::Cyan = {0xFF, 0xFF, 0x00, 0xFF}; Color Color::Magenta = {0xFF, 0x00, 0xFF, 0xFF}; -Color Color::Gray = {0x80, 0x80, 0x80, 0xFF}; \ No newline at end of file +Color Color::Gray = {0x80, 0x80, 0x80, 0xFF}; + +#pragma GCC push_options +#pragma GCC diagnostic ignored "-Wstrict-aliasing" + +Color Color::from_integer(uint32_t source) +{ + return reinterpret_cast(source); +} + +#pragma GCC pop_options \ No newline at end of file diff --git a/kernel/src/sys/gfx.cpp b/kernel/src/sys/gfx.cpp index 070e4cde..aac06061 100644 --- a/kernel/src/sys/gfx.cpp +++ b/kernel/src/sys/gfx.cpp @@ -19,9 +19,8 @@ void sys_paint(Context* context, uint64_t x, uint64_t y, uint64_t w, uint64_t h, } uint32_t color = (uint32_t)c; - uint32_t* colptr = &color; - framebuffer0.paint_rect(x, y, w, h, *(Color*)colptr); + framebuffer0.paint_rect(x, y, w, h, Color::from_integer(color)); context->rax = 0; } \ No newline at end of file