summaryrefslogtreecommitdiff
path: root/include/swgfx.h
blob: 005359b8768451eff498a4991385282fddcbac6f (plain)
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.

Coordinate 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;

typedef uint16_t sgIdx;
typedef struct   sgVert   { sgIdx position, normal, texcoord; } sgVert;
typedef struct   sgTriIdx { sgVert v0, v1, v2; } sgTriIdx;

// TODO: Should we use real-valued colours?
typedef struct sgPixel { uint8_t r, g, b, a; } sgPixel;

typedef struct swgfx swgfx;

size_t sgMem(int width, int height); // Get memory requirements.
swgfx* sgNew(int width, int height, void* mem);
void   sgDel(swgfx**);

// TODO: Write client app first, then implement the functions below in the C file.

void sgPresent(swgfx*, sgVec2i dimensions, sgPixel* screen);

void sgModelId    (swgfx*);
void sgModel      (swgfx*, sgVec3 position, sgVec3 right, sgVec3 up, sgVec3 forward);
void sgView       (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 sgTrianglesIndexed(swgfx*, size_t numIndices, const sgIdx* indices, const sgVec3* positions);
void sgTrianglesIndexedNonUniform(swgfx*, size_t numTris, const sgTriIdx* tris, const sgVec3* positions);

void sgCheck(swgfx*);