diff options
| -rw-r--r-- | examples/line.c | 25 | ||||
| -rw-r--r-- | examples/triangle.c | 2 | ||||
| -rw-r--r-- | examples/triangles.c | 38 | ||||
| -rw-r--r-- | sponge.h | 29 |
4 files changed, 91 insertions, 3 deletions
diff --git a/examples/line.c b/examples/line.c new file mode 100644 index 0000000..31724a9 --- /dev/null +++ b/examples/line.c @@ -0,0 +1,25 @@ +#include <math.h> + +#include "../sponge.h" + +#define PI ((float)3.14159265358979323846) +#define SPEED (0.5f / 360.0f * 2 * PI) +static float angle = 0.0f; +#define MIN(x, y) ((x) < (y) ? (x) : (y)) + +void init() {} + +void draw_frame(sponge_Texture c) { + sponge_clear(c, 0xFF000000); + angle += SPEED; + + float x = sinf(angle); + float y = cosf(angle); + int32_t half = (int32_t)MIN(c.width, c.height) / 2; + + sponge_draw_line( + c, + half + (int32_t)( x * (float)half), half + (int32_t)( y * (float)half), + half + (int32_t)(-x * (float)half), half + (int32_t)(-y * (float)half), + 0xFFFF00FF); +} diff --git a/examples/triangle.c b/examples/triangle.c index 452dc94..a39ee1b 100644 --- a/examples/triangle.c +++ b/examples/triangle.c @@ -1,5 +1,3 @@ -#include <math.h> - #include "../sponge.h" #define SPEED (4) diff --git a/examples/triangles.c b/examples/triangles.c new file mode 100644 index 0000000..f995574 --- /dev/null +++ b/examples/triangles.c @@ -0,0 +1,38 @@ +#include "../sponge.h" + +#define SPEED_ABS (2) +#define GRID_SIZE (10) + + +static int32_t offset = 0; +static int32_t speed = SPEED_ABS; + +void init() {} + +void draw_frame(sponge_Texture c) { + sponge_clear(c, 0xFF000000); + offset += speed; + + if (offset > 30) { + speed = -SPEED_ABS; + } else if (offset < -30) { + speed = SPEED_ABS; + } + + int32_t offset_x = c.width / GRID_SIZE; + int32_t offset_y = c.height / GRID_SIZE; + + for (int i = 0; i < GRID_SIZE; i++) { + for (int j = 0; j < GRID_SIZE; j++) { + int32_t x = i * offset_x; + int32_t y = j * offset_y + offset; + + sponge_draw_triangle_col3( + c, + x, y + offset_y, + x + offset_x, y, + x + (offset_x / 4), y + (offset_y / 4), + 0xFFFF0000, 0xFF00FF00, 0xFF0000FF); + } + } +} @@ -19,8 +19,9 @@ float sponge_dot2(sponge_Vec2 v0, sponge_Vec2 v1); int sponge_canvas_valid (sponge_Texture c); void sponge_clear (sponge_Texture c, uint32_t color); void sponge_draw_rect (sponge_Texture c, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t color); +void sponge_draw_line (sponge_Texture c, int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color); void sponge_draw_texture (sponge_Texture c, uint32_t x0, uint32_t y0, sponge_Texture tex); -void sponge_draw_triangle_col (sponge_Texture c, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color); +void sponge_draw_triangle_col (sponge_Texture c, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color); void sponge_draw_triangle_col3( sponge_Texture c, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color0, uint32_t color1, uint32_t color2); @@ -38,6 +39,7 @@ void sponge_draw_triangle_col3( #define SPONGE__CLAMP(val, min, max) ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val))) #define SPONGE__MIN(x, y) ((x) < (y) ? (x) : (y)) #define SPONGE__MAX(x, y) ((x) > (y) ? (x) : (y)) +#define SPONGE__ABS(x) ((x) >= 0 ? (x) : -(x)) typedef struct { float a; @@ -170,6 +172,31 @@ void sponge_draw_rect(sponge_Texture c, uint32_t x0, uint32_t y0, uint32_t x1, u } } +void sponge_draw_line(sponge_Texture c, int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color) { + SPONGE_ASSERT(sponge_canvas_valid(c)); + + int32_t dx = x1 - x0; + dx = SPONGE__ABS(dx); + int32_t dy = y1 - y0; + dy = SPONGE__ABS(dy); + int32_t d = (2 * dy) - dx; + + int32_t sx = (x0 < x1) ? 1 : -1; + int32_t sy = (y0 < y1) ? 1 : -1; + int32_t err = dx - dy; + + while (1) { + if (x0 >= 0 && x0 < (int32_t)c.width && y0 >= 0 && y0 < (int32_t)c.height) + c.pixels[(y0 * c.stride) + x0] = color; + + if (x0 == x1 && y0 == y1) break; + + int32_t e2 = 2 * err; + if (e2 > -dy) { err -= dy; x0 += sx; } + if (e2 < dx) { err += dx; y0 += sy; } + } +} + void sponge_draw_texture(sponge_Texture c, uint32_t x0, uint32_t y0, sponge_Texture tex) { SPONGE_ASSERT(sponge_canvas_valid(c)); |
