diff options
| author | 3gg <3gg@shellblade.net> | 2025-11-16 18:23:37 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-11-16 18:23:37 -0800 |
| commit | 403730174aeaadcf7a8aad842bc5319050411ef9 (patch) | |
| tree | ca19383484d51593666fc8add3da2dd00098ef42 /include | |
Diffstat (limited to 'include')
| -rw-r--r-- | include/swgfx.h | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/include/swgfx.h b/include/swgfx.h new file mode 100644 index 0000000..b6dc769 --- /dev/null +++ b/include/swgfx.h | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | /* | ||
| 2 | Software rendering library. | ||
| 3 | |||
| 4 | Cooridnate systems: | ||
| 5 | - Pixel coordinates (i,j) refer to the center of the pixel. | ||
| 6 | Thus, real-valued coordinates (x,y) with no fractional part point at the pixel center. | ||
| 7 | - Viewport origin is the top-left corner of the screen. | ||
| 8 | The viewport axes extend down and to the right. | ||
| 9 | |||
| 10 | Multi-threading: | ||
| 11 | - Internal resources (swgfx context) are externally synchronized. | ||
| 12 | - External resources (colour buffer) are internally synchronized. | ||
| 13 | */ | ||
| 14 | #pragma once | ||
| 15 | |||
| 16 | #include <stddef.h> | ||
| 17 | #include <stdint.h> | ||
| 18 | |||
| 19 | typedef float R; | ||
| 20 | |||
| 21 | typedef struct sgVec2i { int x, y; } sgVec2i; | ||
| 22 | typedef struct sgVec2 { R x, y; } sgVec2; | ||
| 23 | typedef struct sgVec3 { R x, y, z; } sgVec3; | ||
| 24 | typedef struct sgVec4 { R x, y, z, w; } sgVec4; | ||
| 25 | |||
| 26 | typedef sgVec3 sgNormal; | ||
| 27 | |||
| 28 | typedef struct sgQuadi { sgVec2i p0, p1; } sgQuadi; | ||
| 29 | typedef struct sgQuad { sgVec2 p0, p1; } sgQuad; | ||
| 30 | typedef struct sgTri2 { sgVec2 p0, p1, p2; } sgTri2; | ||
| 31 | typedef struct sgTri3 { sgVec3 p0, p1, p2; } sgTri3; | ||
| 32 | |||
| 33 | // TODO: Should we use real-valued colours? | ||
| 34 | typedef struct sgPixel { uint8_t r, g, b, a; } sgPixel; | ||
| 35 | |||
| 36 | typedef struct swgfx swgfx; | ||
| 37 | |||
| 38 | swgfx* sgNew(); | ||
| 39 | void sgDel(swgfx**); | ||
| 40 | |||
| 41 | // TODO: Write client app first, then implement the functions below in the C file. | ||
| 42 | |||
| 43 | void sgColourBuffer(swgfx*, sgVec2i dimensions, sgPixel* buffer); | ||
| 44 | void sgPresent (swgfx*, sgVec2i dimensions, sgPixel* screen); | ||
| 45 | |||
| 46 | void sgCam (swgfx*, sgVec3 position, sgVec3 forward); | ||
| 47 | void sgOrtho (swgfx*, R left, R right, R top, R bottom, R near, R far); | ||
| 48 | void sgPerspective(swgfx*, R fovy, R aspect, R near, R far); | ||
| 49 | void sgViewport (swgfx*, int x0, int y0, int width, int height); | ||
| 50 | |||
| 51 | void sgClear(swgfx*); | ||
| 52 | void sgPixels(swgfx*, size_t count, const sgVec2i* positions, sgPixel colour); | ||
| 53 | void sgQuads (swgfx*, size_t count, const sgQuad*); | ||
| 54 | void sgQuadsi(swgfx*, size_t count, const sgQuadi*); | ||
| 55 | void sgTriangles2 (swgfx*, size_t count, const sgTri2*); | ||
| 56 | void sgTriangleStrip2(swgfx*, size_t count, const sgVec2*); | ||
| 57 | void sgTriangles (swgfx*, size_t count, const sgTri3*, const sgNormal*); | ||
| 58 | void sgTriangleStrip (swgfx*, size_t count, const sgVec3*, const sgNormal*); | ||
| 59 | |||
| 60 | void sgCheck(swgfx*); | ||
| 61 | |||
| 62 | // Memory | ||
| 63 | #define SG_ALIGN 64 | ||
| 64 | #define SG_ALIGN_PTR(P) ((uintptr_t)(P) & (~(SG_ALIGN-1))) | ||
| 65 | #define SG_ALIGN_ALLOC(COUNT, TYPE) (TYPE*)sgAlloc(1, COUNT * sizeof(TYPE)) | ||
| 66 | #define SG_FREE(PP) sgFree((void**)PP) | ||
| 67 | void* sgAlloc(size_t count, size_t size); | ||
| 68 | void sgFree(void**); | ||
