~rabbits/moogle

30e5b48c688ff8d5cd62f6882d70c4dc7f437e6d — neauoire 1 year, 6 months ago ee1c3a8
Added static to fns
3 files changed, 192 insertions(+), 200 deletions(-)

M .gitignore
M build.sh
M moogle.c
M .gitignore => .gitignore +7 -1
@@ 1,9 1,15 @@
.DS*

*jpg
*jpg~
*png
*png~
*gif~
*bmp
*bmp~

*chr

moogle
moogle-export.chr
moogle-render.bmp
theme.svg
\ No newline at end of file

M build.sh => build.sh +1 -1
@@ 1,4 1,4 @@
#!/bin/bash
#!/bin/sh -e

# format code
clang-format -i moogle.c

M moogle.c => moogle.c +184 -198
@@ 63,23 63,23 @@ typedef struct {
	Projection projection;
} Camera;

int WIDTH = 8 * HOR + PAD * 2;
int HEIGHT = 8 * (VER + 2) + PAD * 2;
int FPS = 30, GUIDES = 1, ZOOM = 2;
static int WIDTH = 8 * HOR + PAD * 2;
static int HEIGHT = 8 * (VER + 2) + PAD * 2;
static int FPS = 30, GUIDES = 1, ZOOM = 2;

Scene scn;
Camera cam;
static Scene scn;
static Camera cam;

/* interface */

Uint32 theme[] = {
static Uint32 theme[] = {
	0x000000,
	0xFFFFFF,
	0x72DEC2,
	0x666666,
	0x222222};

Uint8 icons[][8] = {
static Uint8 icons[][8] = {
	{0x10, 0x28, 0x44, 0x92, 0x44, 0x28, 0x10, 0x00},
	{0x10, 0x28, 0x44, 0x82, 0x82, 0x82, 0xd6, 0x00},
	{0x1e, 0x22, 0x40, 0x82, 0x40, 0x22, 0x1e, 0x00},


@@ 89,14 89,14 @@ Uint8 icons[][8] = {
	{0x00, 0x38, 0x44, 0x92, 0x28, 0x10, 0x00, 0x00}  /* eye closed */
};

SDL_Window *gWindow = NULL;
SDL_Renderer *gRenderer = NULL;
SDL_Texture *gTexture = NULL;
Uint32 *pixels;
static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL;
static SDL_Texture *gTexture = NULL;
static Uint32 *pixels;

/* helpers */

Point2d
static Point2d
Pt2d(double x, double y)
{
	Point2d p;


@@ 105,7 105,7 @@ Pt2d(double x, double y)
	return p;
}

Point3d *
static Point3d *
set3d(Point3d *v, double x, double y, double z)
{
	v->x = x;


@@ 114,7 114,7 @@ set3d(Point3d *v, double x, double y, double z)
	return v;
}

Point3d
static Point3d
Pt3d(double x, double y, double z)
{
	Point3d p;


@@ 122,17 122,7 @@ Pt3d(double x, double y, double z)
	return p;
}

Mesh
Ms3d(double x, double y, double z)
{
	Mesh m;
	set3d(&m.position, x, y, z);
	m.verticeslen = 0;
	m.edgeslen = 0;
	return m;
}

Scene
static Scene
Sc3d(void)
{
	Scene s;


@@ 143,7 133,7 @@ Sc3d(void)
	return s;
}

Camera
static Camera
Cm3d(double pitch, double yaw, double roll)
{
	Camera c;


@@ 154,7 144,7 @@ Cm3d(double pitch, double yaw, double roll)
	return c;
}

int
static int
colortheme(Uint32 hex)
{
	int i = 0;


@@ 166,7 156,7 @@ colortheme(Uint32 hex)

/* geometry */

double
static double
interpolate(double a, double b, double speed, double range)
{
	if(a < b - range || a > b + range) {


@@ 176,7 166,7 @@ interpolate(double a, double b, double speed, double range)
	return a;
}

Point2d
static Point2d
rot2d(Point2d a, Point2d b, double deg)
{
	double radian = deg * (PI / 180);


@@ 185,25 175,25 @@ rot2d(Point2d a, Point2d b, double deg)
	return Pt2d(a.x + r * cos(angle), a.y + r * sin(angle));
}

int
static int
equpt3d(Point3d p0, Point3d p1)
{
	return p0.x == p1.x && p0.y == p1.y && p0.z == p1.z;
}

Point3d *
static Point3d *
addpt3d(Point3d *p, double x, double y, double z)
{
	return set3d(p, p->x + x, p->y + y, p->z + z);
}

Point3d
static Point3d
add3d(Point3d *a, Point3d *b)
{
	return Pt3d(a->x + b->x, a->y + b->y, a->z + b->z);
}

Point3d
static Point3d
mul3d(Point3d *a, Point3d *b)
{
	return Pt3d(a->x * b->x, a->y * b->y, a->z * b->z);


@@ 211,21 201,21 @@ mul3d(Point3d *a, Point3d *b)

/* scene */

Point3d *
static Point3d *
translate3d(Point3d *p, Point3d *t)
{
	*p = add3d(p, t);
	return p;
}

Point3d *
static Point3d *
scale3d(Point3d *p, Point3d *t)
{
	*p = mul3d(p, t);
	return p;
}

Point3d *
static Point3d *
rotate3d(Point3d *p, Point3d *o, Point3d *t)
{
	if(t->x) {


@@ 246,35 236,35 @@ rotate3d(Point3d *p, Point3d *o, Point3d *t)
	return p;
}

Scene *
static Scene *
moveto(Scene *s, double x, double y, double z)
{
	set3d(&s->position, x, y, z);
	return s;
}

Scene *
static Scene *
scaleto(Scene *s, double x, double y, double z)
{
	set3d(&s->scale, x, y, z);
	return s;
}

Scene *
static Scene *
rotateto(Scene *s, double x, double y, double z)
{
	set3d(&s->rotation, x, y, z);
	return s;
}

Scene *
static Scene *
reset(Scene *s)
{
	moveto(scaleto(rotateto(s, 0, 0, 0), 1, 1, 1), 0, 0, 0);
	return s;
}

Point3d *
static Point3d *
addvertex(Mesh *m, double x, double y, double z)
{
	int i;


@@ 292,7 282,7 @@ addvertex(Mesh *m, double x, double y, double z)
	return set3d(&m->vertices[m->verticeslen++], v.x, v.y, v.z);
}

Edge *
static Edge *
addedge(Mesh *m, Point3d *a, Point3d *b, int color)
{
	if(m->edgeslen == ELIMIT) {


@@ 305,7 295,7 @@ addedge(Mesh *m, Point3d *a, Point3d *b, int color)
	return &m->edges[m->edgeslen++];
}

Mesh *
static Mesh *
addmesh(Scene *s)
{
	if(s->len == MLIMIT) {


@@ 317,7 307,7 @@ addmesh(Scene *s)

/* transforms */

Mesh *
static Mesh *
translate(Mesh *m, double x, double y, double z)
{
	int i;


@@ 327,7 317,7 @@ translate(Mesh *m, double x, double y, double z)
	return m;
}

Mesh *
static Mesh *
scale(Mesh *m, double x, double y, double z)
{
	int i;


@@ 337,7 327,7 @@ scale(Mesh *m, double x, double y, double z)
	return m;
}

Mesh *
static Mesh *
rotate(Mesh *m, double pitch, double yaw, double roll)
{
	int i;


@@ 347,7 337,7 @@ rotate(Mesh *m, double pitch, double yaw, double roll)
	return m;
}

Mesh *
static Mesh *
extrude(Mesh *m, Point3d t, int color)
{
	int i, j, vl = m->verticeslen, el = m->edgeslen;


@@ 370,7 360,7 @@ extrude(Mesh *m, Point3d t, int color)

/* Primitives */

Mesh *
static Mesh *
addpoly(Mesh *m, int a, int b, int c, int color)
{
	addedge(m, &m->vertices[a], &m->vertices[b], color);


@@ 379,14 369,14 @@ addpoly(Mesh *m, int a, int b, int c, int color)
	return m;
}

Mesh *
static Mesh *
addline(Mesh *m, Point3d a, Point3d b, int color)
{
	addedge(m, addvertex(m, a.x, a.y, a.z), addvertex(m, b.x, b.y, b.z), color);
	return m;
}

Mesh *
static Mesh *
addarc(Mesh *m, double radius, double segs, double angle, int color)
{
	int i;


@@ 401,135 391,15 @@ addarc(Mesh *m, double radius, double segs, double angle, int color)
	return m;
}

Mesh *
static Mesh *
addshape(Mesh *m, double radius, int segs, int color)
{
	return addarc(m, radius, segs, 360, color);
}

/* Primitives */

Mesh *
createfrustum(Scene *s, double radius, int segs, double depth, double cap, int color)
{
	int i;
	Mesh *m = addmesh(s);
	addshape(m, cap, segs, color);
	translate(m, 0, 0, depth);
	addshape(m, radius, segs, color);
	for(i = 0; i < segs + 1; i++)
		addedge(m, &m->vertices[i], &m->vertices[segs + i + 1], color);
	return m;
}

Mesh *
createpyramid(Scene *s, double radius, int segs, double depth, int color)
{
	int i;
	Mesh *m = addmesh(s);
	Point3d *p = addvertex(m, 0, 0, depth);
	addshape(m, radius, segs, color);
	for(i = 0; i < segs + 1; i++)
		addedge(m, &m->vertices[i], p, color);
	return m;
}

Mesh *
createprism(Scene *s, double radius, int segs, double depth, int color)
{
	return createfrustum(s, radius, segs, depth, radius, color);
}

Mesh *
createplane(Scene *s, double width, double height, double xsegs, double ysegs, int color)
{
	int ix, iy;
	Mesh *m = addmesh(s);
	for(ix = 0; ix < xsegs + 1; ix++)
		addline(m,
			Pt3d(ix * (width / xsegs) - width / 2, height / 2, 0),
			Pt3d(ix * (width / xsegs) - width / 2, -height / 2, 0),
			color);
	for(iy = 0; iy < ysegs + 1; iy++)
		addline(m,
			Pt3d(width / 2, iy * (height / ysegs) - height / 2, 0),
			Pt3d(-width / 2, iy * (height / ysegs) - height / 2, 0),
			color);
	return m;
}

Mesh *
createbox(Scene *s, double width, double height, double depth, int color)
{
	Mesh *m = createplane(s, width, height, 1, 1, color);
	extrude(m, Pt3d(0, 0, depth), color);
	return m;
}

Mesh *
createicosaedron(Scene *s, double radius, int color)
{
	Mesh *m = addmesh(s);
	double t = (1.0 + sqrt(5.0)) / 2.0;
	addvertex(m, -1, t, 0);
	addvertex(m, 1, t, 0);
	addvertex(m, -1, -t, 0);
	addvertex(m, 1, -t, 0);
	addvertex(m, 0, -1, t);
	addvertex(m, 0, 1, t);
	addvertex(m, 0, -1, -t);
	addvertex(m, 0, 1, -t);
	addvertex(m, t, 0, -1);
	addvertex(m, t, 0, 1);
	addvertex(m, -t, 0, -1);
	addvertex(m, -t, 0, 1);
	addpoly(m, 0, 11, 5, 3); /* - */
	addpoly(m, 0, 5, 1, 3);
	addpoly(m, 0, 1, 7, 3);
	addpoly(m, 0, 7, 10, 3);
	addpoly(m, 0, 10, 11, 3);
	addpoly(m, 1, 5, 9, 3); /* - */
	addpoly(m, 5, 11, 4, 3);
	addpoly(m, 11, 10, 2, 3);
	addpoly(m, 10, 7, 6, 3);
	addpoly(m, 7, 1, 8, 3);
	addpoly(m, 3, 9, 4, 3); /* - */
	addpoly(m, 3, 4, 2, 3);
	addpoly(m, 3, 2, 6, 3);
	addpoly(m, 3, 6, 8, 3);
	addpoly(m, 3, 8, 9, 3);
	addpoly(m, 4, 9, 5, 3); /* - */
	addpoly(m, 2, 4, 11, 3);
	addpoly(m, 6, 2, 10, 3);
	addpoly(m, 8, 6, 7, 3);
	addpoly(m, 9, 8, 1, 3);
	scale(m, radius / 2, radius / 2, radius / 2);
	return m;
}

Mesh *
createumbrella(Scene *s)
{
	int i;
	Mesh *umbrella = addmesh(s);
	reset(s);
	for(i = 0; i < 5; ++i) {
		addarc(umbrella, 10, 8, 180, 2);
		rotateto(s, 0, 45 * i, 0);
	}
	rotateto(s, 90, 0, 0);
	addshape(umbrella, 10, 8, 1);
	translate(scale(umbrella, 1, 0.6, 1), 0, 2, 0);
	addline(umbrella, Pt3d(0, 0, -10), Pt3d(0, 0, 8), 1);
	rotateto(moveto(s, 0, -8, 2), 90, 90, 90);
	addarc(umbrella, 2, 8, 180, 2);
	reset(s);
	return umbrella;
}

/* draw */

void
static void
clear(Uint32 *dst)
{
	int i, j;


@@ 538,20 408,14 @@ clear(Uint32 *dst)
			dst[i * WIDTH + j] = theme[0];
}

int
getpixel(Uint32 *dst, int x, int y)
{
	return dst[(y + PAD) * WIDTH + (x + PAD)];
}

void
static void
putpixel(Uint32 *dst, int x, int y, int color)
{
	if(x >= 0 && x < WIDTH - 8 && y >= 0 && y < HEIGHT - 8)
		dst[(y + PAD) * WIDTH + (x + PAD)] = theme[color];
}

void
static void
line(Uint32 *dst, Point2d p0, Point2d p1, int color)
{
	int p0x = (int)p0.x, p0y = (int)p0.y;


@@ 575,7 439,7 @@ line(Uint32 *dst, Point2d p0, Point2d p1, int color)
	}
}

void
static void
drawicon(Uint32 *dst, int x, int y, Uint8 *icon, int color)
{
	int v, h;


@@ 586,7 450,7 @@ drawicon(Uint32 *dst, int x, int y, Uint8 *icon, int color)
		}
}

void
static void
drawui(Uint32 *dst)
{
	int bottom = VER * 8 + 8;


@@ 597,7 461,7 @@ drawui(Uint32 *dst)
	drawicon(dst, 5 * 8, bottom, icons[GUIDES ? 6 : 5], GUIDES ? 1 : 2);
}

Point2d
static Point2d
project(Camera *c, Point3d v)
{
	double r;


@@ 609,7 473,7 @@ project(Camera *c, Point3d v)
	return Pt2d(WIDTH / 2 + r * v.x, HEIGHT / 2 + r * v.y);
}

void
static void
redraw(Uint32 *dst)
{
	int i, j;


@@ 637,14 501,14 @@ redraw(Uint32 *dst)

/* options */

int
static int
error(char *msg, const char *err)
{
	printf("Error %s: %s\n", msg, err);
	return 0;
}

void
static void
update(Camera *c, double speed)
{
	if(!equpt3d(c->rotation, c->trotation) || !equpt3d(c->origin, c->torigin)) {


@@ 660,7 524,7 @@ update(Camera *c, double speed)
	}
}

void
static void
modzoom(int mod)
{
	if((mod > 0 && ZOOM < 4) || (mod < 0 && ZOOM > 1))


@@ 669,21 533,21 @@ modzoom(int mod)
	redraw(pixels);
}

void
static void
toggleprojection(Camera *c)
{
	c->projection = c->projection == ISOMETRIC ? PERSPECTIVE : ISOMETRIC;
	redraw(pixels);
}

void
static void
setguides(int v)
{
	GUIDES = v;
	redraw(pixels);
}

void
static void
modrange(int mod)
{
	int res = cam.range + mod;


@@ 692,7 556,7 @@ modrange(int mod)
	redraw(pixels);
}

void
static void
selectoption(int option)
{
	switch(option) {


@@ 704,7 568,7 @@ selectoption(int option)
	}
}

void
static void
putchr(Uint8 *chrbuf, int row, int col, int color)
{
	if(row < 0 || row > SZ - 8)


@@ 719,7 583,7 @@ putchr(Uint8 *chrbuf, int row, int col, int color)
		chrbuf[row + 8] |= 1UL << (7 - col);
}

void
static void
exportchr(void)
{
	int h, v, x, y;


@@ 741,7 605,7 @@ exportchr(void)
	puts("Exported moogle-export.chr");
}

void
static void
renderbmp(void)
{
	SDL_Surface *surface = SDL_GetWindowSurface(gWindow);


@@ 758,7 622,7 @@ renderbmp(void)
	SDL_FreeSurface(surface);
}

void
static void
quit(void)
{
	free(pixels);


@@ 772,7 636,7 @@ quit(void)
	exit(0);
}

void
static void
domouse(SDL_Event *event)
{
	switch(event->type) {


@@ 787,7 651,7 @@ domouse(SDL_Event *event)
	}
}

void
static void
dokey(SDL_Event *event)
{
	int shift = SDL_GetModState() & KMOD_LSHIFT || SDL_GetModState() & KMOD_RSHIFT;


@@ 851,7 715,7 @@ dokey(SDL_Event *event)
	redraw(pixels);
}

int
static int
init(void)
{
	if(SDL_Init(SDL_INIT_VIDEO) < 0)


@@ 881,6 745,128 @@ init(void)
	return 1;
}

/* Primitives */

/* Primitives */

Mesh *
createfrustum(Scene *s, double radius, int segs, double depth, double cap, int color)
{
	int i;
	Mesh *m = addmesh(s);
	addshape(m, cap, segs, color);
	translate(m, 0, 0, depth);
	addshape(m, radius, segs, color);
	for(i = 0; i < segs + 1; i++)
		addedge(m, &m->vertices[i], &m->vertices[segs + i + 1], color);
	return m;
}

Mesh *
createpyramid(Scene *s, double radius, int segs, double depth, int color)
{
	int i;
	Mesh *m = addmesh(s);
	Point3d *p = addvertex(m, 0, 0, depth);
	addshape(m, radius, segs, color);
	for(i = 0; i < segs + 1; i++)
		addedge(m, &m->vertices[i], p, color);
	return m;
}

Mesh *
createprism(Scene *s, double radius, int segs, double depth, int color)
{
	return createfrustum(s, radius, segs, depth, radius, color);
}

Mesh *
createplane(Scene *s, double width, double height, double xsegs, double ysegs, int color)
{
	int ix, iy;
	Mesh *m = addmesh(s);
	for(ix = 0; ix < xsegs + 1; ix++)
		addline(m,
			Pt3d(ix * (width / xsegs) - width / 2, height / 2, 0),
			Pt3d(ix * (width / xsegs) - width / 2, -height / 2, 0),
			color);
	for(iy = 0; iy < ysegs + 1; iy++)
		addline(m,
			Pt3d(width / 2, iy * (height / ysegs) - height / 2, 0),
			Pt3d(-width / 2, iy * (height / ysegs) - height / 2, 0),
			color);
	return m;
}

Mesh *
createbox(Scene *s, double width, double height, double depth, int color)
{
	Mesh *m = createplane(s, width, height, 1, 1, color);
	extrude(m, Pt3d(0, 0, depth), color);
	return m;
}

Mesh *
createicosaedron(Scene *s, double radius, int color)
{
	Mesh *m = addmesh(s);
	double t = (1.0 + sqrt(5.0)) / 2.0;
	addvertex(m, -1, t, 0);
	addvertex(m, 1, t, 0);
	addvertex(m, -1, -t, 0);
	addvertex(m, 1, -t, 0);
	addvertex(m, 0, -1, t);
	addvertex(m, 0, 1, t);
	addvertex(m, 0, -1, -t);
	addvertex(m, 0, 1, -t);
	addvertex(m, t, 0, -1);
	addvertex(m, t, 0, 1);
	addvertex(m, -t, 0, -1);
	addvertex(m, -t, 0, 1);
	addpoly(m, 0, 11, 5, color); /* - */
	addpoly(m, 0, 5, 1, color);
	addpoly(m, 0, 1, 7, color);
	addpoly(m, 0, 7, 10, color);
	addpoly(m, 0, 10, 11, color);
	addpoly(m, 1, 5, 9, color); /* - */
	addpoly(m, 5, 11, 4, color);
	addpoly(m, 11, 10, 2, color);
	addpoly(m, 10, 7, 6, color);
	addpoly(m, 7, 1, 8, color);
	addpoly(m, 3, 9, 4, color); /* - */
	addpoly(m, 3, 4, 2, color);
	addpoly(m, 3, 2, 6, color);
	addpoly(m, 3, 6, 8, color);
	addpoly(m, 3, 8, 9, color);
	addpoly(m, 4, 9, 5, color); /* - */
	addpoly(m, 2, 4, 11, color);
	addpoly(m, 6, 2, 10, color);
	addpoly(m, 8, 6, 7, color);
	addpoly(m, 9, 8, 1, color);
	scale(m, radius / 2, radius / 2, radius / 2);
	return m;
}

Mesh *
createumbrella(Scene *s)
{
	int i;
	Mesh *umbrella = addmesh(s);
	reset(s);
	for(i = 0; i < 5; ++i) {
		addarc(umbrella, 10, 8, 180, 2);
		rotateto(s, 0, 45 * i, 0);
	}
	rotateto(s, 90, 0, 0);
	addshape(umbrella, 10, 8, 1);
	translate(scale(umbrella, 1, 0.6, 1), 0, 2, 0);
	addline(umbrella, Pt3d(0, 0, -10), Pt3d(0, 0, 8), 1);
	rotateto(moveto(s, 0, -8, 2), 90, 90, 90);
	addarc(umbrella, 2, 8, 180, 2);
	reset(s);
	return umbrella;
}

int
main(void)
{