implemented CRTP shader on ColorShader and Rectangle

This commit is contained in:
2026-07-20 01:41:10 -07:00
parent b95022de29
commit 444143ced9
9 changed files with 132 additions and 151 deletions
+6 -39
View File
@@ -5,7 +5,8 @@
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "../shader.cpp"
#include "../shaders/shaderbase.cpp"
#include "../shaders/colorshader.cpp"
namespace UwU {
// rectangle
@@ -24,47 +25,13 @@ namespace UwU {
uint16_t y1;
// Draw a rectangle
void draw (Buffer buffer, Shader shader) {
template <class ShaderT>
void draw (Buffer buffer, ShaderBase<ShaderT>& shader) {
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
if (shader.shaderType == COLOR) {
for (uint16_t y = y0; y <= y1; y++) {
for (uint16_t x = x0; x <= x1; x++) {
buffer.drawPixel(x, y, shader.c0);
}
}
return;
} else if (shader.shaderType == HGRADIENT) {
// Precompute gradient LUT
Color cols[x1 - x0];
for (uint16_t y = y0; y <= y1; y++) {
for (uint16_t x = x0; x <= x1; x++) {
cols[x - x0] = ShaderMethods::gradient(shader.u0, shader.scale, shader.c0, shader.c1, x);
buffer.drawPixel(x, y, shader.shade(x, y));
}
for (uint16_t y = y0; y <= y1; y++) {
for (uint16_t x = x0; x <= x1; x++) {
buffer.drawPixel(x, y, cols[x - x0]);
}
}
return;
} else if (shader.shaderType == VGRADIENT) {
for (uint16_t y = y0; y <= y1; y++) {
Color c = ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, y);
for (uint16_t x = x0; x <= x1; x++) {
buffer.drawPixel(x, y, c);
}
}
return;
} else if (shader.shaderType == BUFFER) {
for (uint16_t y = y0; y <= y1; y++) {
for (uint16_t x = x0; x <= x1; x++) {
buffer.drawPixel(x, y, ShaderMethods::buffer(shader.u0, shader.v0, shader.buffer, x, y));
}
}
return;
}
return;
}