~rabbits/orca-toy

0314d8ac2f5b41105741579faf15fd2f14f2c94f — neauoire 10 months ago e3332d1
Improved font
7 files changed, 89 insertions(+), 26 deletions(-)

M build.sh
M font-light.chr
A font-wide.chr
A midi.c
A midi.h
M sim.c
M toy.c
M build.sh => build.sh +3 -1
@@ 4,6 4,8 @@ clang-format -i sim.c
clang-format -i sim.h
clang-format -i cli.c
clang-format -i toy.c
clang-format -i midi.c
clang-format -i midi.h

# cli
# rm -f ./cli


@@ 13,6 15,6 @@ clang-format -i toy.c

# toy
rm -f ./toy
cc -std=c89 -DDEBUG -Wall -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined -L/usr/local/lib -lSDL2 toy.c sim.c -o toy
cc -std=c89 -DDEBUG -Wall -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined -L/usr/local/lib -lSDL2 toy.c sim.c midi.c -o toy
# cc toy.c sim.c -std=c89 -O2 -DNDEBUG -g0 -s -Wall -L/usr/local/lib -lSDL2 -o toy
./toy

M font-light.chr => font-light.chr +0 -0
A font-wide.chr => font-wide.chr +0 -0
A midi.c => midi.c +27 -0
@@ 0,0 1,27 @@
#include <linux/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include "midi.h"

int
note(void)
{
	char *device = "/dev/midi2";
	unsigned char g_on[3] = {0x90, 0x43, 0x40};
	unsigned char g_off[3] = {0x80, 0x43, 0x00};
	int f = open(device, O_WRONLY, 0);
	if(f < 0)
		return 0;
	printf("Note ON\n");
	if(!write(f, g_on, sizeof(g_on)))
		return 0;
	sleep(2);
	printf("Note OFF\n");
	if(!write(f, g_off, sizeof(g_off)))
		return 0;
	close(f);
	return 1;
}

A midi.h => midi.h +10 -0
@@ 0,0 1,10 @@
#pragma once
#include <linux/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>

int
checkmidi(void);

M sim.c => sim.c +35 -18
@@ 91,17 91,6 @@ set(Grid *g, int x, int y, char c)
		g->data[x + (y * g->w)] = c;
}

/* Locks */

void
lock(Grid *g, int x, int y)
{
	if(valid(g, x, y)) {
		g->lock[x + (y * g->w)] = 1;
		g->type[x + (y * g->w)] = 1;
	}
}

/* Variables */

void


@@ 133,6 122,18 @@ settype(Grid *g, int x, int y, int t)
		g->type[x + (y * g->w)] = t;
}

/* Locks */

void
lock(Grid *g, int x, int y)
{
	if(valid(g, x, y)) {
		g->lock[x + (y * g->w)] = 1;
		if(!gettype(g, x, y))
			settype(g, x, y, 1);
	}
}

/* Port Setters */

void


@@ 220,6 221,7 @@ ope(Grid *g, int x, int y, char c)
		settype(g, x, y, 0);
		setport(g, x + 1, y, c);
		lock(g, x + 1, y);
		settype(g, x + 1, y, 0);
	}
}



@@ 325,6 327,7 @@ opn(Grid *g, int x, int y, char c)
		settype(g, x, y, 0);
		setport(g, x, y - 1, c);
		lock(g, x, y - 1);
		settype(g, x, y - 1, 0);
	}
}



@@ 369,9 372,11 @@ opq(Grid *g, int x, int y, char c)
void
opr(Grid *g, int x, int y, char c)
{
	int min = cint(getport(g, x - 1, y, 0));
	char min = getport(g, x - 1, y, 0);
	char max = getport(g, x + 1, y, 1);
	setport(g, x, y + 1, cchr((random(g) % ((cint(max) - min) || 1)) + min, ciuc(max)));
	int min_ = cint(min);
	int max_ = cint(max);
	setport(g, x, y + 1, cchr((random(g) % ((cint(max_) - min_) || 1)) + min_, ciuc(max)));
	(void)c;
}



@@ 405,10 410,17 @@ opt(Grid *g, int x, int y, char c)
void
opu(Grid *g, int x, int y, char c)
{
	int max = cint(getport(g, x - 1, y, 0));
	int step = cint(getport(g, x + 1, y, 1));
	int bucket = (step * (g->f + max - 1)) % max + step;
	setport(g, x, y + 1, bucket >= max ? '*' : '.');
	char step = getport(g, x - 1, y, 1);
	char max = getport(g, x + 1, y, 0);
	int step_ = cint(step);
	int max_ = cint(max);
	int bucket;
	if(!step_)
		step_ = 1;
	if(!max_)
		max_ = 8;
	bucket = (step_ * (g->f + max_ - 1)) % max_ + step_;
	setport(g, x, y + 1, bucket >= max_ ? '*' : '.');
	(void)c;
}



@@ 434,6 446,7 @@ opw(Grid *g, int x, int y, char c)
		settype(g, x, y, 0);
		setport(g, x - 1, y, c);
		lock(g, x - 1, y);
		settype(g, x - 1, y, 0);
	}
}



@@ 469,7 482,11 @@ opz(Grid *g, int x, int y, char c)
	int rate_ = cint(rate);
	int target_ = cint(target);
	int val_ = cint(val);
	setport(g, x, y + 1, cchr(val_ + val_ < target_ ? rate_ : val_ > target_ ? -rate_ : 0, ciuc(target)));
	int mod;
	if(!rate_)
		rate_ = 1;
	mod = val_ <= target_ - rate_ ? rate_ : val_ >= target_ + rate_ ? -rate_ : target_ - val_;
	setport(g, x, y + 1, cchr(val_ + mod, ciuc(target)));
	(void)c;
}


M toy.c => toy.c +14 -7
@@ 1,6 1,7 @@
#include <SDL2/SDL.h>
#include <stdio.h>
#include "sim.h"
#include "midi.h"

#define HOR 32
#define VER 16


@@ 35,6 36,7 @@ SDL_Renderer *gRenderer = NULL;
SDL_Texture *gTexture = NULL;
uint32_t *pixels;

int down = 0;
Rect2d selection;
Grid g;



@@ 168,10 170,10 @@ draw(uint32_t *dst)
void
select(int x, int y, int w, int h)
{
	selection.x = x;
	selection.y = y;
	selection.w = w;
	selection.h = h;
	selection.x = clamp(x, 0, HOR - 1);
	selection.y = clamp(y, 0, VER - 1);
	selection.w = clamp(w, 1, 36);
	selection.h = clamp(h, 1, 36);
	draw(pixels);
}



@@ 180,8 182,8 @@ move(int x, int y)
{
	selection.x += x;
	selection.y += y;
	selection.x = clamp(selection.x, 0, HOR);
	selection.y = clamp(selection.y, 0, VER);
	selection.x = clamp(selection.x, 0, HOR - 1);
	selection.y = clamp(selection.y, 0, VER - 1);
	draw(pixels);
}



@@ 209,6 211,7 @@ play(void)
	for(i = 0; i < g.msg_len; ++i) {
		printf("%c", g.msg[i]);
	}
	fflush(stdout);
}

void


@@ 236,11 239,15 @@ domouse(SDL_Event *event)
	switch(event->type) {
	case SDL_MOUSEBUTTONUP:
		select(selection.x, selection.y, touch.x / 8 - selection.x + 1, touch.y / 8 - selection.y + 1);
		down = 0;
		break;
	case SDL_MOUSEBUTTONDOWN:
		select(touch.x / 8, touch.y / 8, 1, 1);
		down = 1;
		break;
	case SDL_MOUSEMOTION:
		if(down)
			select(selection.x, selection.y, touch.x / 8 - selection.x + 1, touch.y / 8 - selection.y + 1);
		break;
	}
}


@@ 337,7 344,7 @@ init(void)
int
loadfont(void)
{
	FILE *f = fopen("font-bold.chr", "rb");
	FILE *f = fopen("font-light.chr", "rb");
	if(f == NULL)
		return error("Font", "Invalid input file");
	if(!fread(font, sizeof(font), 1, f))