~rabbits/noton

affac4e5190d60fd13cd35f20b22e732084a6066 — neauoire 3 years ago 188be09
Updated clang format
1 files changed, 81 insertions(+), 109 deletions(-)

M noton.c
M noton.c => noton.c +81 -109
@@ 58,12 58,12 @@ typedef struct Brush {

int WIDTH = 8 * HOR + PAD * 2;
int HEIGHT = 8 * VER + PAD * 2;
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
SDL_Texture* gTexture = NULL;
uint32_t* pixels;
SDL_Window *gWindow = NULL;
SDL_Renderer *gRenderer = NULL;
SDL_Texture *gTexture = NULL;
uint32_t *pixels;
Noton noton;
PmStream* midi;
PmStream *midi;

/* helpers */



@@ 73,8 73,8 @@ distance(Point2d a, Point2d b)
	return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y);
}

Point2d*
setpt2d(Point2d* p, int x, int y)
Point2d *
setpt2d(Point2d *p, int x, int y)
{
	p->x = x;
	p->y = y;


@@ 93,31 93,31 @@ void
play(int channel, int octave, int note, int z)
{
	Pm_WriteShort(midi,
	              Pt_Time(),
	              Pm_Message(0x90 + channel, (octave * 12) + note, z ? 100 : 0));
		Pt_Time(),
		Pm_Message(0x90 + channel, (octave * 12) + note, z ? 100 : 0));
}

void
select(Noton* n, int channel)
select(Noton *n, int channel)
{
	printf("Select channel #%d\n", channel);
	n->channel = channel;
}

void
destroy(Noton* n)
destroy(Noton *n)
{
	/* TODO */
}

void
toggle(Noton* n)
toggle(Noton *n)
{
	n->alive = !n->alive;
}

Gate*
findgateid(Noton* n, int id)
Gate *
findgateid(Noton *n, int id)
{
	if(id < 0 || id >= GATEMAX)
		return NULL;


@@ 126,8 126,8 @@ findgateid(Noton* n, int id)
	return &n->gates[id];
}

Gate*
availgate(Noton* n)
Gate *
availgate(Noton *n)
{
	int i;
	for(i = 0; i < GATEMAX; ++i)


@@ 138,8 138,8 @@ availgate(Noton* n)
	return NULL;
}

Wire*
availwire(Noton* n)
Wire *
availwire(Noton *n)
{
	int i;
	for(i = 0; i < WIREMAX; ++i)


@@ 150,12 150,12 @@ availwire(Noton* n)
	return NULL;
}

Gate*
findgateat(Noton* n, Point2d pos)
Gate *
findgateat(Noton *n, Point2d pos)
{
	int i;
	for(i = 0; i < GATEMAX; ++i) {
		Gate* g = &n->gates[i];
		Gate *g = &n->gates[i];
		if(g->active && distance(pos, g->pos) < 50)
			return g;
	}


@@ 163,7 163,7 @@ findgateat(Noton* n, Point2d pos)
}

int
getpolarity(Gate* g)
getpolarity(Gate *g)
{
	int i;
	if(!g->active || g->inlen < 1)


@@ 178,7 178,7 @@ getpolarity(Gate* g)
}

void
polarize(Gate* g)
polarize(Gate *g)
{
	int i;
	if(!g->active)


@@ 195,7 195,7 @@ polarize(Gate* g)
}

void
bang(Gate* g, int depth)
bang(Gate *g, int depth)
{
	int i, a = depth - 1;
	if(a < 1 || !g || !g->active)


@@ 207,11 207,11 @@ bang(Gate* g, int depth)

/* Add/Remove */

Wire*
addwire(Noton* n, Wire* temp, Gate* from, Gate* to)
Wire *
addwire(Noton *n, Wire *temp, Gate *from, Gate *to)
{
	int i;
	Wire* w = availwire(n);
	Wire *w = availwire(n);
	printf("Add wire #%d(#%d->#%d) \n", w->id, from->id, to->id);
	w->active = 1;
	w->polarity = -1;


@@ 223,10 223,10 @@ addwire(Noton* n, Wire* temp, Gate* from, Gate* to)
	return w;
}

Gate*
addgate(Noton* n, GateType type, int polarity, Point2d pos)
Gate *
addgate(Noton *n, GateType type, int polarity, Point2d pos)
{
	Gate* g = availgate(n);
	Gate *g = availgate(n);
	printf("Add gate #%d \n", g->id);
	g->active = 1;
	g->polarity = polarity;


@@ 243,7 243,7 @@ addgate(Noton* n, GateType type, int polarity, Point2d pos)
/* Wiring */

void
extendwire(Brush* b)
extendwire(Brush *b)
{
	if(b->wire.len >= WIREPTMAX)
		return;


@@ 253,9 253,9 @@ extendwire(Brush* b)
}

void
beginwire(Brush* b)
beginwire(Brush *b)
{
	Gate* gate = findgateat(&noton, b->pos);
	Gate *gate = findgateat(&noton, b->pos);
	b->wire.active = 1;
	b->wire.polarity = -1;
	if(gate) {


@@ 267,7 267,7 @@ beginwire(Brush* b)
}

int
abandon(Brush* b)
abandon(Brush *b)
{
	b->wire.len = 0;
	b->wire.active = 0;


@@ 275,9 275,9 @@ abandon(Brush* b)
}

int
endwire(Brush* b)
endwire(Brush *b)
{
	Wire* newwire;
	Wire *newwire;
	Gate *gatefrom, *gateto;
	if(b->wire.len < 1)
		return abandon(b);


@@ 309,14 309,14 @@ endwire(Brush* b)
/* draw */

void
pixel(uint32_t* dst, int x, int y, int color)
pixel(uint32_t *dst, int x, int y, int color)
{
	if(x >= 0 && x < WIDTH - PAD * 2 && y >= 0 && y < HEIGHT - PAD * 2)
		dst[(y + PAD) * WIDTH + (x + PAD)] = color;
}

void
line(uint32_t* dst, int ax, int ay, int bx, int by, int color)
line(uint32_t *dst, int ax, int ay, int bx, int by, int color)
{
	int dx = abs(bx - ax), sx = ax < bx ? 1 : -1;
	int dy = -abs(by - ay), sy = ay < by ? 1 : -1;


@@ 338,7 338,7 @@ line(uint32_t* dst, int ax, int ay, int bx, int by, int color)
}

void
circle(uint32_t* dst, int ax, int ay, int d, int color)
circle(uint32_t *dst, int ax, int ay, int d, int color)
{
	int i, r = d / 2;
	for(i = 0; i < d * d; ++i) {


@@ 349,14 349,14 @@ circle(uint32_t* dst, int ax, int ay, int d, int color)
}

void
drawwire(uint32_t* dst, Wire* w, int color)
drawwire(uint32_t *dst, Wire *w, int color)
{
	int i;
	if(!w->active)
		return;
	for(i = 0; i < w->len - 1; i++) {
		Point2d p1 = w->points[i];
		Point2d* p2 = &w->points[i + 1];
		Point2d *p2 = &w->points[i + 1];
		if(p2) {
			line(dst, p1.x, p1.y, p2->x, p2->y, color);
			if((int)(noton.frame / 3) % w->len != i)


@@ 366,7 366,7 @@ drawwire(uint32_t* dst, Wire* w, int color)
}

void
drawgate(uint32_t* dst, Gate* g)
drawgate(uint32_t *dst, Gate *g)
{
	int r = 17;
	if(!g->active)


@@ 382,7 382,7 @@ drawgate(uint32_t* dst, Gate* g)
}

void
clear(uint32_t* dst)
clear(uint32_t *dst)
{
	int i, j;
	for(i = 0; i < HEIGHT; i++)


@@ 391,7 391,7 @@ clear(uint32_t* dst)
}

void
redraw(uint32_t* dst, Brush* b)
redraw(uint32_t *dst, Brush *b)
{
	int i;
	clear(dst);


@@ 410,7 410,7 @@ redraw(uint32_t* dst, Brush* b)
/* operation */

void
run(Noton* n)
run(Noton *n)
{
	int i;
	n->inputs[0]->polarity = (n->frame / 4) % 2;


@@ 459,7 459,7 @@ setup(void)
/* options */

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


@@ 481,13 481,13 @@ quit(void)
}

void
domouse(SDL_Event* event, Brush* b)
domouse(SDL_Event *event, Brush *b)
{
	switch(event->type) {
	case SDL_MOUSEBUTTONDOWN:
		setpt2d(&b->pos,
		        (event->motion.x - (PAD * ZOOM)) / ZOOM,
		        (event->motion.y - (PAD * ZOOM)) / ZOOM);
			(event->motion.x - (PAD * ZOOM)) / ZOOM,
			(event->motion.y - (PAD * ZOOM)) / ZOOM);
		if(event->button.button == SDL_BUTTON_RIGHT)
			break;
		b->down = 1;


@@ 499,16 499,16 @@ domouse(SDL_Event* event, Brush* b)
			break;
		if(b->down) {
			setpt2d(&b->pos,
			        (event->motion.x - (PAD * ZOOM)) / ZOOM,
			        (event->motion.y - (PAD * ZOOM)) / ZOOM);
				(event->motion.x - (PAD * ZOOM)) / ZOOM,
				(event->motion.y - (PAD * ZOOM)) / ZOOM);
			extendwire(b);
			redraw(pixels, b);
		}
		break;
	case SDL_MOUSEBUTTONUP:
		setpt2d(&b->pos,
		        (event->motion.x - (PAD * ZOOM)) / ZOOM,
		        (event->motion.y - (PAD * ZOOM)) / ZOOM);
			(event->motion.x - (PAD * ZOOM)) / ZOOM,
			(event->motion.y - (PAD * ZOOM)) / ZOOM);
		if(event->button.button == SDL_BUTTON_RIGHT) {
			if(!findgateat(&noton, b->pos))
				addgate(&noton, XOR, -1, b->pos);


@@ 523,49 523,23 @@ domouse(SDL_Event* event, Brush* b)
}

void
dokey(Noton* n, SDL_Event* event, Brush* b)
dokey(Noton *n, SDL_Event *event, Brush *b)
{
	/* int shift = SDL_GetModState() & KMOD_LSHIFT || SDL_GetModState() & KMOD_RSHIFT; */
	switch(event->key.keysym.sym) {
	case SDLK_ESCAPE:
		quit();
		break;
	case SDLK_BACKSPACE:
		redraw(pixels, b);
		break;
	case SDLK_SPACE:
		toggle(n);
		break;
	case SDLK_n:
		destroy(n);
		break;
	case SDLK_1:
		select(n, 0);
		break;
	case SDLK_2:
		select(n, 1);
		break;
	case SDLK_3:
		select(n, 2);
		break;
	case SDLK_4:
		select(n, 3);
		break;
	case SDLK_5:
		select(n, 4);
		break;
	case SDLK_6:
		select(n, 5);
		break;
	case SDLK_7:
		select(n, 6);
		break;
	case SDLK_8:
		select(n, 7);
		break;
	case SDLK_9:
		select(n, 8);
		break;
	case SDLK_ESCAPE: quit(); break;
	case SDLK_BACKSPACE: redraw(pixels, b); break;
	case SDLK_SPACE: toggle(n); break;
	case SDLK_n: destroy(n); break;
	case SDLK_1: select(n, 0); break;
	case SDLK_2: select(n, 1); break;
	case SDLK_3: select(n, 2); break;
	case SDLK_4: select(n, 3); break;
	case SDLK_5: select(n, 4); break;
	case SDLK_6: select(n, 5); break;
	case SDLK_7: select(n, 6); break;
	case SDLK_8: select(n, 7); break;
	case SDLK_9: select(n, 8); break;
	}
}



@@ 575,8 549,7 @@ initmidi(void)
	int i, select = 0;
	Pm_Initialize();
	for(i = 0; i < Pm_CountDevices(); ++i)
		printf("#%d -> %s%s\n", i,
		       Pm_GetDeviceInfo(i)->name, i == select ? "(selected)" : "");
		printf("#%d -> %s%s\n", i, Pm_GetDeviceInfo(i)->name, i == select ? "(selected)" : "");
	Pm_OpenOutput(&midi, select, NULL, 128, 0, NULL, 1);
}



@@ 587,24 560,24 @@ init(void)
		return error("Init", SDL_GetError());
	printf("init\n");
	gWindow = SDL_CreateWindow("Noton",
	                           SDL_WINDOWPOS_UNDEFINED,
	                           SDL_WINDOWPOS_UNDEFINED,
	                           WIDTH * ZOOM,
	                           HEIGHT * ZOOM,
	                           SDL_WINDOW_SHOWN);
		SDL_WINDOWPOS_UNDEFINED,
		SDL_WINDOWPOS_UNDEFINED,
		WIDTH * ZOOM,
		HEIGHT * ZOOM,
		SDL_WINDOW_SHOWN);
	if(gWindow == NULL)
		return error("Window", SDL_GetError());
	gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
	if(gRenderer == NULL)
		return error("Renderer", SDL_GetError());
	gTexture = SDL_CreateTexture(gRenderer,
	                             SDL_PIXELFORMAT_ARGB8888,
	                             SDL_TEXTUREACCESS_STATIC,
	                             WIDTH,
	                             HEIGHT);
		SDL_PIXELFORMAT_ARGB8888,
		SDL_TEXTUREACCESS_STATIC,
		WIDTH,
		HEIGHT);
	if(gTexture == NULL)
		return error("Texture", SDL_GetError());
	pixels = (uint32_t*)malloc(WIDTH * HEIGHT * sizeof(uint32_t));
	pixels = (uint32_t *)malloc(WIDTH * HEIGHT * sizeof(uint32_t));
	if(pixels == NULL)
		return error("Pixels", "Failed to allocate memory");
	clear(pixels);


@@ 613,7 586,7 @@ init(void)
}

int
main(int argc, char** argv)
main(int argc, char **argv)
{
	Uint32 begintime = 0;
	Uint32 endtime = 0;


@@ 649,7 622,6 @@ main(int argc, char** argv)
			printf("Slowdown: %ifps\n", fps);

		if(noton.alive) {
			printf("%d\n", noton.alive);
			run(&noton);
			redraw(pixels, &brush);
		}


@@ 658,8 630,8 @@ main(int argc, char** argv)
			if(event.type == SDL_QUIT)
				quit();
			else if(event.type == SDL_MOUSEBUTTONUP ||
			        event.type == SDL_MOUSEBUTTONDOWN ||
			        event.type == SDL_MOUSEMOTION) {
					event.type == SDL_MOUSEBUTTONDOWN ||
					event.type == SDL_MOUSEMOTION) {
				domouse(&event, &brush);
			} else if(event.type == SDL_KEYDOWN)
				dokey(&noton, &event, &brush);