added basic mappers

This commit is contained in:
2026-07-24 18:01:59 -07:00
parent c7cdfd402e
commit a89ef52247
10 changed files with 127 additions and 20 deletions
+19
View File
@@ -0,0 +1,19 @@
shapes:
triangles:
point
line
solid
gradient
rectangle
solid
gradient
blitter (tilemaps)
triangle
solid
gradient
perspective mapping (3D!!!)
quad
solid
gradient (charts)
blitter (isometric)
affine mapping (sprite rotation and scaling)
+9 -9
View File
@@ -26,15 +26,15 @@ namespace UwU {
}; };
// Coord2 datatype for xy coordinates, from top-left of surface // Coord2 datatype for xy coordinates, from top-left of surface
// struct Coord2 { struct Coord2 {
// Coord2(uint16_t x, uint16_t y) { Coord2(uint16_t x, uint16_t y) {
// this->x = x; this->x = x;
// this->y = y; this->y = y;
// } }
// Coord2() {} Coord2() {}
// uint16_t x; uint16_t x;
// uint16_t y; uint16_t y;
// }; };
// Vec2 datatype for signed x, y coordinates // Vec2 datatype for signed x, y coordinates
// struct Vec2 { // struct Vec2 {
Executable
BIN
View File
Binary file not shown.
+8 -7
View File
@@ -44,21 +44,22 @@ int main(void) {
UwU::Color blue = UwU::Color(0x0000ffff); UwU::Color blue = UwU::Color(0x0000ffff);
// UwU::ColorShader fuchsia = UwU::ColorShader(UwU::Color(0xb00b69ff)); // UwU::ColorShader fuchsia = UwU::ColorShader(UwU::Color(0xb00b69ff));
UwU::VGradientShader bisexual = UwU::VGradientShader(69, 420, UwU::Color(0xff0000ff), UwU::Color(0x0000ffff)); UwU::VGradientShader bisexual = UwU::VGradientShader(0, 351, UwU::Color(0xff0000ff), UwU::Color(0x0000ffff));
UwU::OffsetMapper rectMapper = UwU::OffsetMapper(69, 69);
UwU::Rectangle rectangle = UwU::Rectangle(69, 69, 420, 420); UwU::Rectangle rectangle = UwU::Rectangle(69, 69, 420, 420);
rectangle.draw(surface.buffer, bisexual); rectangle.draw(surface.buffer, rectMapper, bisexual);
UwU::ColorShader greenShader = UwU::ColorShader(green); UwU::ColorShader greenShader = UwU::ColorShader(green);
UwU::Point point = UwU::Point(187, 37); UwU::Point point = UwU::Point(187, 37);
point.draw(surface.buffer, greenShader); point.draw(surface.buffer, greenShader);
UwU::ColorShader blue = UwU::ColorShader(UwU::Color(0x0000ffff)); UwU::ColorShader blueShader = UwU::ColorShader(blue);
UwU::Trapezoid trapezoid = UwU::Trapezoid(100, 300, 690, 320, 720, 780); UwU::Trapezoid trapezoid = UwU::Trapezoid(100, 300, 690, 320, 720, 780);
trapezoid.draw(surface.buffer, blue); trapezoid.draw(surface.buffer, blueShader);
UwU::ColorShader red = UwU::ColorShader(UwU::Color(0xff0000ff)); UwU::ColorShader redShader = UwU::ColorShader(red);
UwU::Triangle triangle = UwU::Triangle(140, 140, 60, 400, 240, 240); UwU::Triangle triangle = UwU::Triangle(140, 140, 60, 400, 240, 240);
triangle.draw(surface.buffer, red); triangle.draw(surface.buffer, redShader);
// // Benchmarking // // Benchmarking
// struct timespec start, end; // struct timespec start, end;
@@ -99,7 +100,7 @@ int main(void) {
}; };
for (uint16_t i = 0; i < 8; i++) { for (uint16_t i = 0; i < 8; i++) {
lines[i].draw(surface.buffer, blue); lines[i].draw(surface.buffer, blueShader);
} }
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
namespace UwU {
template <class MapperT>
class MapperBase {
public:
Coord2 map(uint16_t x, uint16_t y) {
return static_cast<MapperT*>(this)->map(x, y);
}
};
}
+4
View File
@@ -0,0 +1,4 @@
#include "mapperbase.cpp"
#include "nullmapper.cpp"
#include "offsetmapper.cpp"
#include "tilemapper.cpp"
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "mapperbase.cpp"
namespace UwU {
class NullMapper : public MapperBase<NullMapper> {
public:
NullMapper() {}
Coord2 map(uint16_t x, uint16_t y) {
return Coord2(x, y);
}
};
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "mapperbase.cpp"
namespace UwU {
class OffsetMapper : public MapperBase<OffsetMapper> {
public:
OffsetMapper(uint16_t x0, uint16_t y0) {
this->x0 = x0;
this->y0 = y0;
}
uint16_t x0, y0;
Coord2 map(uint16_t x, uint16_t y) {
uint16_t u = x -x0;
uint16_t v = y -y0;
return Coord2(u, v);
}
};
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "mapperbase.cpp"
namespace UwU {
class TileMapper : public MapperBase<TileMapper> { // not actually tilemapping yet
public:
TileMapper(uint16_t x0, uint16_t y0) {
this->x0 = x0;
this->y0 = y0;
}
uint16_t x0, y0;
Coord2 map(uint16_t x, uint16_t y) {
uint16_t u = x -x0;
uint16_t v = y -y0;
return Coord2(u, v);
}
};
}
+5 -4
View File
@@ -5,8 +5,8 @@
#include "../datatypes.cpp" #include "../datatypes.cpp"
#include "../helpers.cpp" #include "../helpers.cpp"
#include "../buffer.cpp" #include "../buffer.cpp"
#include "../shaders/shaderbase.cpp"
#include "../shaders/shaders.h" #include "../shaders/shaders.h"
#include "../mappers/mappers.h"
namespace UwU { namespace UwU {
// rectangle // rectangle
@@ -25,12 +25,13 @@ namespace UwU {
uint16_t y1; uint16_t y1;
// Draw a rectangle // Draw a rectangle
template <class ShaderT> template <class ShaderT, class MapperT>
void draw (Buffer buffer, ShaderBase<ShaderT>& shader) { void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) {
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
for (uint16_t y = y0; y <= y1; y++) { for (uint16_t y = y0; y <= y1; y++) {
for (uint16_t x = x0; x <= x1; x++) { for (uint16_t x = x0; x <= x1; x++) {
buffer.drawPixel(x, y, shader.shade(x, y)); Coord2 uv = mapper.map(x, y);
buffer.drawPixel(x, y, shader.shade(uv.x, uv.y));
} }
} }
return; return;