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