moved textures into their own classes, better tilemapping

This commit is contained in:
2026-07-25 23:47:54 -07:00
parent e6ce80ce13
commit 1c2579148e
19 changed files with 277 additions and 56 deletions
BIN
View File
Binary file not shown.
+18 -18
View File
@@ -16,7 +16,7 @@
// #include "primatives/rectangle.cpp" // #include "primatives/rectangle.cpp"
// #include "primatives/trapezoid.cpp" // #include "primatives/trapezoid.cpp"
// #include "primatives/triangle.cpp" // #include "primatives/triangle.cpp"
// #include "shader.cpp" // #include "Texture.cpp"
#include "uwugl.h" #include "uwugl.h"
#include <time.h> #include <time.h>
@@ -43,31 +43,31 @@ int main(void) {
UwU::Color green = UwU::Color(0x00ff00ff); UwU::Color green = UwU::Color(0x00ff00ff);
UwU::Color blue = UwU::Color(0x0000ffff); UwU::Color blue = UwU::Color(0x0000ffff);
// UwU::ColorShader fuchsia = UwU::ColorShader(UwU::Color(0xb00b69ff)); // UwU::ColorTexture fuchsia = UwU::ColorTexture(UwU::Color(0xb00b69ff));
UwU::HGradientShader bisexual = UwU::HGradientShader(0, 179, red, blue); UwU::HGradientTexture bisexual = UwU::HGradientTexture(180, red, blue);
UwU::TileMapper tileMapper = UwU::TileMapper(); UwU::OffsetMapper offsetMapper = UwU::OffsetMapper();
UwU::Tilemap myTilemap = UwU::Tilemap();
myTilemap.tilemap[0] = 0x0000;
myTilemap.tilemap[1] = 0x0001;
myTilemap.tilemap[2] = 0x0000;
myTilemap.tilemap[3] = 0x0002;
myTilemap.tilemap[4] = 0x0003;
UwU::Buffer fontBuffer = font(); UwU::Buffer fontBuffer = font();
UwU::BufferShader textMap = UwU::BufferShader(fontBuffer); UwU::TilemapTexture textMap = UwU::TilemapTexture(myTilemap, fontBuffer);
tileMapper.tileMap[0] = 0x0000;
tileMapper.tileMap[1] = 0x0111;
tileMapper.tileMap[2] = 0x0000;
tileMapper.tileMap[3] = 0x0222;
tileMapper.tileMap[4] = 0x0333;
UwU::Rectangle rectangle = UwU::Rectangle(69, 69, 420, 420); UwU::Rectangle rectangle = UwU::Rectangle(69, 69, 420, 420);
rectangle.draw(surface.buffer, tileMapper, textMap); rectangle.draw(surface.buffer, offsetMapper, textMap);
UwU::ColorShader greenShader = UwU::ColorShader(green); UwU::ColorTexture greenTexture = UwU::ColorTexture(green);
UwU::NullMapper nullMapper = UwU::NullMapper(); UwU::NullMapper nullMapper = UwU::NullMapper();
UwU::Point point = UwU::Point(187, 37); UwU::Point point = UwU::Point(187, 37);
point.draw(surface.buffer, nullMapper, greenShader); point.draw(surface.buffer, nullMapper, greenTexture);
UwU::ColorShader blueShader = UwU::ColorShader(blue); UwU::ColorTexture blueTexture = UwU::ColorTexture(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, nullMapper, blueShader); trapezoid.draw(surface.buffer, nullMapper, blueTexture);
UwU::ColorShader redShader = UwU::ColorShader(red); UwU::ColorTexture redTexture = UwU::ColorTexture(red);
UwU::Triangle triangle = UwU::Triangle(140, 140, 60, 400, 240, 240); UwU::Triangle triangle = UwU::Triangle(140, 140, 60, 400, 240, 240);
UwU::OffsetMapper offsetMapper = UwU::OffsetMapper();
triangle.draw(surface.buffer, offsetMapper, bisexual); triangle.draw(surface.buffer, offsetMapper, bisexual);
// // Benchmarking // // Benchmarking
@@ -109,7 +109,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, nullMapper, blueShader); lines[i].draw(surface.buffer, nullMapper, blueTexture);
} }
+1 -1
View File
@@ -1,4 +1,4 @@
#include "mapperbase.cpp" #include "mapperbase.cpp"
#include "nullmapper.cpp" #include "nullmapper.cpp"
#include "offsetmapper.cpp" #include "offsetmapper.cpp"
#include "tilemapper.cpp" // #include "tilemapper.cpp"
+13 -13
View File
@@ -5,16 +5,16 @@
#include "../datatypes.cpp" #include "../datatypes.cpp"
#include "../helpers.cpp" #include "../helpers.cpp"
#include "../buffer.cpp" #include "../buffer.cpp"
#include "../shaders/shaders.h"
#include "../mappers/mappers.h" #include "../mappers/mappers.h"
#include "../textures/textures.h"
namespace UwU { namespace UwU {
// line // line
class Line { class Line {
private: private:
// plot lines if m <= 1 // plot lines if m <= 1
template <class MapperT, class ShaderT> template <class MapperT, class TextureT>
void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) { void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
int16_t dx = ix1 - ix0; int16_t dx = ix1 - ix0;
int16_t dy = iy1 - iy0; int16_t dy = iy1 - iy0;
int16_t yi = 1; int16_t yi = 1;
@@ -29,7 +29,7 @@ namespace UwU {
for (int16_t x = ix0; x < ix1; x++) { for (int16_t x = ix0; x < ix1; x++) {
uv = mapper.map(ix0, iy0, x, y); uv = mapper.map(ix0, iy0, x, y);
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.shade(uv.x, uv.y)); buffer.drawPixel((uint16_t)x, (uint16_t)y, texture.texel(uv.x, uv.y));
if (d > 0) { if (d > 0) {
y += yi; y += yi;
d += (2 * (dy - dx)); d += (2 * (dy - dx));
@@ -41,8 +41,8 @@ namespace UwU {
} }
// plot lines if m >1 // plot lines if m >1
template <class MapperT, class ShaderT> template <class MapperT, class TextureT>
void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) { void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
int16_t dx = ix1 - ix0; int16_t dx = ix1 - ix0;
int16_t dy = iy1 - iy0; int16_t dy = iy1 - iy0;
int16_t xi = 1; int16_t xi = 1;
@@ -57,7 +57,7 @@ namespace UwU {
for (int16_t y = iy0; y < iy1; y++) { for (int16_t y = iy0; y < iy1; y++) {
uv = mapper.map(ix0, iy0, x, y); uv = mapper.map(ix0, iy0, x, y);
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.shade(uv.x, uv.y)); buffer.drawPixel((uint16_t)x, (uint16_t)y, texture.texel(uv.x, uv.y));
if (d > 0) { if (d > 0) {
x += xi; x += xi;
d += (2 * (dx - dy)); d += (2 * (dx - dy));
@@ -82,8 +82,8 @@ namespace UwU {
uint16_t y1; uint16_t y1;
// Uses Bresenham's line algorithm to draw line of any slope // Uses Bresenham's line algorithm to draw line of any slope
template <class MapperT, class ShaderT> template <class MapperT, class TextureT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) { void draw (Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
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;};
int16_t ix0 = x0; int16_t ix0 = x0;
int16_t iy0 = y0; int16_t iy0 = y0;
@@ -91,15 +91,15 @@ namespace UwU {
int16_t iy1 = y1; int16_t iy1 = y1;
if (abs(iy1 - iy0) < abs(ix1 - ix0)) { if (abs(iy1 - iy0) < abs(ix1 - ix0)) {
if (ix0 > ix1) { if (ix0 > ix1) {
lineLow(ix1, iy1, ix0, iy0, buffer, mapper, shader); lineLow(ix1, iy1, ix0, iy0, buffer, mapper, texture);
} else { } else {
lineLow(ix0, iy0, ix1, iy1, buffer, mapper, shader); lineLow(ix0, iy0, ix1, iy1, buffer, mapper, texture);
} }
} else { } else {
if (iy0 > iy1) { if (iy0 > iy1) {
lineHigh(ix1, iy1, ix0, iy0, buffer, mapper, shader); lineHigh(ix1, iy1, ix0, iy0, buffer, mapper, texture);
} else { } else {
lineHigh(ix0, iy0, ix1, iy1, buffer, mapper, shader); lineHigh(ix0, iy0, ix1, iy1, buffer, mapper, texture);
} }
} }
return; return;
+4 -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/shaders.h"
#include "../mappers/mappers.h" #include "../mappers/mappers.h"
#include "../textures/textures.h"
namespace UwU { namespace UwU {
// point // point
@@ -21,11 +21,11 @@ namespace UwU {
uint16_t y; uint16_t y;
// Draw a point // Draw a point
template <class MapperT, class ShaderT> template <class MapperT, class TextureT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) { void draw (Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
if (x > buffer.width || y > buffer.height) {return;}; if (x > buffer.width || y > buffer.height) {return;};
Coord2 uv = mapper.map(x, y, x, y); Coord2 uv = mapper.map(x, y, x, y);
buffer.drawPixel(x, y, shader.shade(uv.x, uv.y)); buffer.drawPixel(x, y, texture.texel(uv.x, uv.y));
return; return;
} }
}; };
+5
View File
@@ -0,0 +1,5 @@
#include "point.cpp"
#include "line.cpp"
#include "rectangle.cpp"
#include "trapezoid.cpp"
#include "triangle.cpp"
+4 -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/shaders.h"
#include "../mappers/mappers.h" #include "../mappers/mappers.h"
#include "../textures/textures.h"
namespace UwU { namespace UwU {
// rectangle // rectangle
@@ -25,13 +25,13 @@ namespace UwU {
uint16_t y1; uint16_t y1;
// Draw a rectangle // Draw a rectangle
template <class MapperT, class ShaderT> template <class MapperT, class TextureT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) { void draw (Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
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++) {
Coord2 uv = mapper.map(x0, y0, x, y); Coord2 uv = mapper.map(x0, y0, x, y);
buffer.drawPixel(x, y, shader.shade(uv.x, uv.y)); buffer.drawPixel(x, y, texture.texel(uv.x, uv.y));
} }
} }
return; return;
+4 -4
View File
@@ -6,8 +6,8 @@
#include "../datatypes.cpp" #include "../datatypes.cpp"
#include "../helpers.cpp" #include "../helpers.cpp"
#include "../buffer.cpp" #include "../buffer.cpp"
#include "../shaders/shaders.h"
#include "../mappers/mappers.h" #include "../mappers/mappers.h"
#include "../textures/textures.h"
namespace UwU { namespace UwU {
// trapezoid (horizontally constrained) // trapezoid (horizontally constrained)
@@ -31,8 +31,8 @@ namespace UwU {
// Draw a horizontally bounded trapezoid // Draw a horizontally bounded trapezoid
// y1 >= y0 // y1 >= y0
template <class MapperT, class ShaderT> template <class MapperT, class TextureT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) { void draw (Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
uint16_t height = 1 + y1 - y0; uint16_t height = 1 + y1 - y0;
int16_t LeftBound[height]; int16_t LeftBound[height];
int16_t RightBound[height]; int16_t RightBound[height];
@@ -45,7 +45,7 @@ namespace UwU {
for (uint16_t i = 0; i < height; i++) { for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) { for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
Coord2 uv = mapper.map(x0, y0, x, i + y0); Coord2 uv = mapper.map(x0, y0, x, i + y0);
buffer.drawPixel(x, i + y0, shader.shade(uv.x, uv.y)); buffer.drawPixel(x, i + y0, texture.texel(uv.x, uv.y));
} }
} }
return; return;
+5 -5
View File
@@ -6,8 +6,8 @@
#include "../datatypes.cpp" #include "../datatypes.cpp"
#include "../helpers.cpp" #include "../helpers.cpp"
#include "../buffer.cpp" #include "../buffer.cpp"
#include "../shaders/shaders.h"
#include "../mappers/mappers.h" #include "../mappers/mappers.h"
#include "../textures/textures.h"
namespace UwU { namespace UwU {
class Triangle { class Triangle {
@@ -31,8 +31,8 @@ namespace UwU {
uint16_t y2; uint16_t y2;
// Draw a triangle // Draw a triangle
template <class MapperT, class ShaderT> template <class MapperT, class TextureT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) { void draw (Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
uint16_t topX, topY, midX, midY, btmX, btmY; uint16_t topX, topY, midX, midY, btmX, btmY;
// Find top, middle and bottom points of triangle // Find top, middle and bottom points of triangle
if (y0 <= y1 && y0 <= y2) { if (y0 <= y1 && y0 <= y2) {
@@ -100,7 +100,7 @@ namespace UwU {
for (uint16_t i = 0; i < height; i++) { for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) { for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) {
uv = mapper.map(leftX, topY, x, i + (uint16_t)topY); uv = mapper.map(leftX, topY, x, i + (uint16_t)topY);
buffer.drawPixel(x, i + (uint16_t)topY, shader.shade(uv.x, uv.y)); buffer.drawPixel(x, i + (uint16_t)topY, texture.texel(uv.x, uv.y));
} }
} }
// Right-handed triangle case (midpoint is right of triangle) // Right-handed triangle case (midpoint is right of triangle)
@@ -108,7 +108,7 @@ namespace UwU {
for (uint16_t i = 0; i < height; i++) { for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) { for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) {
uv = mapper.map(leftX, topY, x, i + (uint16_t)topY); uv = mapper.map(leftX, topY, x, i + (uint16_t)topY);
buffer.drawPixel(x, i + (uint16_t)topY, shader.shade(uv.x, uv.y)); buffer.drawPixel(x, i + (uint16_t)topY, texture.texel(uv.x, uv.y));
} }
} }
} }
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "texturebase.cpp"
namespace UwU {
class BitmapTexture : public TextureBase<BitmapTexture> {
private:
Buffer buffer;
public:
BitmapTexture(Buffer buffer) {
this->buffer = buffer;
}
Color texel(uint16_t u, uint16_t v) {
uint32_t index = (v * buffer.stride) + (u * 4);
Color c = Color(buffer.pixels[index + 2], buffer.pixels[index + 1], buffer.pixels[index], buffer.pixels[index + 3]);
return c;
}
};
}
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "texturebase.cpp"
namespace UwU {
class ColorTexture : public TextureBase<ColorTexture> {
private:
Color color;
public:
ColorTexture(Color color) {
this->color = color;
}
Color texel(uint16_t u, uint16_t v) {
(void)u;
(void)v;
return color;
}
};
}
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "texturebase.cpp"
namespace UwU {
class GradientTexture : public TextureBase<GradientTexture> {
public:
GradientTexture(uint16_t width, Color c0, Color c1) {
this->uScale = 0xffffffff / (1 + (uint32_t)width);
this->c0 = c0;
this->c1 = c1;
}
uint32_t uScale;
Color c0;
Color c1;
Color texel(uint16_t u, uint16_t v) {
(void)v;
uint32_t t = (((uint32_t)u) * uScale) / 0x01000000;
return Helpers::lerpColor(c0, c1, (uint8_t)t);
}
};
}
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "texturebase.cpp"
namespace UwU {
class HGradientTexture : public TextureBase<HGradientTexture> {
public:
HGradientTexture(uint16_t width, Color c0, Color c1) {
this->uScale = 0xffffffff / (1 + (uint32_t)width);
this->c0 = c0;
this->c1 = c1;
}
uint32_t uScale;
Color c0;
Color c1;
Color texel(uint16_t u, uint16_t v) {
(void)v;
uint32_t t = (((uint32_t)u) * uScale) / 0x01000000;
return Helpers::lerpColor(c0, c1, (uint8_t)t);
}
};
}
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
namespace UwU {
template <class TextureT>
class TextureBase {
public:
Color texel(uint16_t u, uint16_t v) {
return static_cast<TextureT*>(this)->texel(u, v);
}
};
}
+7
View File
@@ -0,0 +1,7 @@
#include "texturebase.cpp"
#include "colortexture.cpp"
#include "bitmaptexture.cpp"
#include "tilemaptexture.cpp"
#include "hgradienttexture.cpp"
#include "vgradienttexture.cpp"
// #include "gradienttexture.cpp"
+30
View File
@@ -0,0 +1,30 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "texturebase.cpp"
#include "../tilemap.cpp"
namespace UwU {
class TilemapTexture : public TextureBase<TilemapTexture> {
private:
Buffer buffer;
Tilemap tilemap;
public:
TilemapTexture(Tilemap tilemap, Buffer buffer) {
this->buffer = buffer;
this->tilemap = tilemap;
}
Color texel(uint16_t u, uint16_t v) {
Coord2 bitmapUV = tilemap.map(u, v);
uint32_t index = (bitmapUV.y * buffer.stride) + (bitmapUV.x * 4);
Color c = Color(buffer.pixels[index + 2], buffer.pixels[index + 1], buffer.pixels[index], buffer.pixels[index + 3]);
return c;
}
};
}
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "texturebase.cpp"
namespace UwU {
class VGradientTexture : public TextureBase<VGradientTexture> {
public:
VGradientTexture(uint16_t height, Color c0, Color c1) {
this->vScale = 0xffffffff / (1 + (uint32_t)height);
this->c0 = c0;
this->c1 = c1;
}
uint32_t vScale;
Color c0;
Color c1;
Color texel(uint16_t u, uint16_t v) {
(void)u;
uint32_t t = (((uint32_t)v) * vScale) / 0x01000000;
return Helpers::lerpColor(c0, c1, (uint8_t)t);
}
};
}
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <stdint.h>
#include "datatypes.cpp"
#include "helpers.cpp"
namespace UwU {
class Tilemap {
public:
Tilemap() {
this->tilemap = new uint16_t[16384]; // the map of tile positions 128x128 8x8 tiles from a 512x512 bitmap space
}
uint16_t* tilemap;
Coord2 map(uint16_t u, uint16_t v) {
// returns uv coordinates that point to a specific pixel of a specific tile on a tilesheet buffer
uint16_t mapX = (u & 0x03f8) >> 3;
uint16_t mapY = (v & 0x03f8) << 4;
uint16_t tileIndex = tilemap[mapY + mapX];
uint16_t u1 = (tileIndex & 0x003f) << 3;
u1 += (u & 0x0007);
uint16_t v1 = (tileIndex & 0x0fc0) >> 3;
v1 += (v & 0x0007);
return Coord2(u1, v1);
}
};
}
+5 -7
View File
@@ -2,10 +2,8 @@
#include "helpers.cpp" #include "helpers.cpp"
#include "buffer.cpp" #include "buffer.cpp"
#include "surface.cpp" #include "surface.cpp"
#include "primatives/line.cpp" #include "primatives/primatives.h"
#include "primatives/point.cpp" #include "mappers/mappers.h"
#include "primatives/rectangle.cpp" #include "textures/textures.h"
#include "primatives/trapezoid.cpp" #include "tilemap.cpp"
#include "primatives/triangle.cpp" // #include "shaders/shaders.h"
#include "shaders/shaders.h"
#include "mappers/mappers.h"