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
+13 -13
View File
@@ -5,16 +5,16 @@
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "../shaders/shaders.h"
#include "../mappers/mappers.h"
#include "../textures/textures.h"
namespace UwU {
// line
class Line {
private:
// plot lines if m <= 1
template <class MapperT, class ShaderT>
void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) {
template <class MapperT, class TextureT>
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 dy = iy1 - iy0;
int16_t yi = 1;
@@ -29,7 +29,7 @@ namespace UwU {
for (int16_t x = ix0; x < ix1; x++) {
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) {
y += yi;
d += (2 * (dy - dx));
@@ -41,8 +41,8 @@ namespace UwU {
}
// plot lines if m >1
template <class MapperT, class ShaderT>
void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) {
template <class MapperT, class TextureT>
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 dy = iy1 - iy0;
int16_t xi = 1;
@@ -57,7 +57,7 @@ namespace UwU {
for (int16_t y = iy0; y < iy1; 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) {
x += xi;
d += (2 * (dx - dy));
@@ -82,8 +82,8 @@ namespace UwU {
uint16_t y1;
// Uses Bresenham's line algorithm to draw line of any slope
template <class MapperT, class ShaderT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) {
template <class MapperT, class TextureT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
int16_t ix0 = x0;
int16_t iy0 = y0;
@@ -91,15 +91,15 @@ namespace UwU {
int16_t iy1 = y1;
if (abs(iy1 - iy0) < abs(ix1 - ix0)) {
if (ix0 > ix1) {
lineLow(ix1, iy1, ix0, iy0, buffer, mapper, shader);
lineLow(ix1, iy1, ix0, iy0, buffer, mapper, texture);
} else {
lineLow(ix0, iy0, ix1, iy1, buffer, mapper, shader);
lineLow(ix0, iy0, ix1, iy1, buffer, mapper, texture);
}
} else {
if (iy0 > iy1) {
lineHigh(ix1, iy1, ix0, iy0, buffer, mapper, shader);
lineHigh(ix1, iy1, ix0, iy0, buffer, mapper, texture);
} else {
lineHigh(ix0, iy0, ix1, iy1, buffer, mapper, shader);
lineHigh(ix0, iy0, ix1, iy1, buffer, mapper, texture);
}
}
return;
+4 -4
View File
@@ -5,8 +5,8 @@
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "../shaders/shaders.h"
#include "../mappers/mappers.h"
#include "../textures/textures.h"
namespace UwU {
// point
@@ -21,11 +21,11 @@ namespace UwU {
uint16_t y;
// Draw a point
template <class MapperT, class ShaderT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) {
template <class MapperT, class TextureT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
if (x > buffer.width || y > buffer.height) {return;};
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;
}
};
+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 "../helpers.cpp"
#include "../buffer.cpp"
#include "../shaders/shaders.h"
#include "../mappers/mappers.h"
#include "../textures/textures.h"
namespace UwU {
// rectangle
@@ -25,13 +25,13 @@ namespace UwU {
uint16_t y1;
// Draw a rectangle
template <class MapperT, class ShaderT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) {
template <class MapperT, class TextureT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
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 x = x0; x <= x1; x++) {
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;
+4 -4
View File
@@ -6,8 +6,8 @@
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "../shaders/shaders.h"
#include "../mappers/mappers.h"
#include "../textures/textures.h"
namespace UwU {
// trapezoid (horizontally constrained)
@@ -31,8 +31,8 @@ namespace UwU {
// Draw a horizontally bounded trapezoid
// y1 >= y0
template <class MapperT, class ShaderT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) {
template <class MapperT, class TextureT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
uint16_t height = 1 + y1 - y0;
int16_t LeftBound[height];
int16_t RightBound[height];
@@ -45,7 +45,7 @@ namespace UwU {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
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;
+5 -5
View File
@@ -6,8 +6,8 @@
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "../shaders/shaders.h"
#include "../mappers/mappers.h"
#include "../textures/textures.h"
namespace UwU {
class Triangle {
@@ -31,8 +31,8 @@ namespace UwU {
uint16_t y2;
// Draw a triangle
template <class MapperT, class ShaderT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) {
template <class MapperT, class TextureT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, TextureBase<TextureT>& texture) {
uint16_t topX, topY, midX, midY, btmX, btmY;
// Find top, middle and bottom points of triangle
if (y0 <= y1 && y0 <= y2) {
@@ -100,7 +100,7 @@ namespace UwU {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) {
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)
@@ -108,7 +108,7 @@ namespace UwU {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) {
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));
}
}
}