~rabbits/orca-toy

2ac4b18d751f3051afe9d9c40f5c6bdb89b2e7b7 — neauoire 9 months ago fccd7b1
Improved font and fixed issues with operators
7 files changed, 53 insertions(+), 126 deletions(-)

M README.md
M build.sh
M demo.orca
D midi.c
D midi.h
M sim.c
M toy.c
M README.md => README.md +0 -20
@@ 44,23 44,3 @@ To display the list of operators inside of Orca, use `CmdOrCtrl+G`.
- `3` operator
- `4` port(right)
- `5` port(output)

## Notes

### SDL Mod Codes

```
KMOD_NONE = 0x0000,
KMOD_LSHIFT = 0x0001,
KMOD_RSHIFT = 0x0002,
KMOD_LCTRL = 0x0040,
KMOD_RCTRL = 0x0080,
KMOD_LALT = 0x0100,
KMOD_RALT = 0x0200,
KMOD_LGUI = 0x0400,
KMOD_RGUI = 0x0800,
KMOD_NUM = 0x1000,
KMOD_CAPS = 0x2000,
KMOD_MODE = 0x4000,
KMOD_RESERVED = 0x8000
```

M build.sh => build.sh +0 -2
@@ 4,8 4,6 @@ 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

## Cleanup
rm -f ./cli

M demo.orca => demo.orca +4 -4
@@ 1,14 1,14 @@
................................
.#.DEMO.......................#.
................................
...A....B....C....D....F....G...
..1AC..1BC..1CC..1DC..1FC...G...
................................
................................
...H....I....J....K....L....M...
...H...1IC...J....K...1LC..1MC..
................................
................................
...O....P....Q....R....T....U...
...O....P....Q...1RC...T...1UC..
................................
................................
...V....X....Y....Z.............
...V....X....Y...1ZC............
................................
\ No newline at end of file

D midi.c => midi.c +0 -27
@@ 1,27 0,0 @@
#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;
}

D midi.h => midi.h +0 -10
@@ 1,10 0,0 @@
#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 +5 -8
@@ 184,8 184,8 @@ opb(Grid *g, int x, int y, char c)
void
opc(Grid *g, int x, int y, char c)
{
	char mod = getport(g, x + 1, y, 0);
	char rate = getport(g, x - 1, y, 0);
	char mod = getport(g, x + 1, y, 1);
	int mod_ = cint(mod);
	int rate_ = cint(rate);
	if(!rate_)


@@ 200,7 200,7 @@ void
opd(Grid *g, int x, int y, char c)
{
	char rate = getport(g, x - 1, y, 0);
	char mod = getport(g, x + 1, y, 0);
	char mod = getport(g, x + 1, y, 1);
	int rate_ = cint(rate);
	int mod_ = cint(mod);
	if(!rate_)


@@ 219,7 219,6 @@ ope(Grid *g, int x, int y, char c)
	else {
		set(g, x, y, '.');
		setport(g, x + 1, y, c);
		lock(g, x + 1, y);
		settype(g, x + 1, y, 0);
	}
	settype(g, x, y, 0);


@@ 325,7 324,6 @@ opn(Grid *g, int x, int y, char c)
	else {
		set(g, x, y, '.');
		setport(g, x, y - 1, c);
		lock(g, x, y - 1);
		settype(g, x, y - 1, 0);
	}
	settype(g, x, y, 0);


@@ 387,8 385,8 @@ ops(Grid *g, int x, int y, char c)
		set(g, x, y, '*');
	else {
		set(g, x, y, '.');
		set(g, x, y + 1, c);
		lock(g, x, y + 1);
		setport(g, x, y + 1, c);
		settype(g, x, y + 1, 0);
	}
	settype(g, x, y, 0);
}


@@ 411,7 409,7 @@ void
opu(Grid *g, int x, int y, char c)
{
	char step = getport(g, x - 1, y, 1);
	char max = getport(g, x + 1, y, 0);
	char max = getport(g, x + 1, y, 1);
	int step_ = cint(step);
	int max_ = cint(max);
	int bucket;


@@ 444,7 442,6 @@ opw(Grid *g, int x, int y, char c)
	else {
		set(g, x, y, '.');
		setport(g, x - 1, y, c);
		lock(g, x - 1, y);
		settype(g, x - 1, y, 0);
	}
	settype(g, x, y, 0);

M toy.c => toy.c +44 -55
@@ 38,14 38,14 @@ unsigned char font[1200];
int colors[] = {color1, color2, color3, color4, color0};
int WIDTH = 8 * HOR + PAD * 2;
int HEIGHT = 8 * VER + PAD * 2;
int FPS = 30, GUIDES = 1, DOWN = 0;
int FPS = 30, DOWN = 0;

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

Rect2d selection;
Rect2d cursor;
Grid g;

Point2d


@@ 66,33 66,21 @@ clamp(int val, int min, int max)
int
selected(int x, int y)
{
	return x < selection.x + selection.w && x >= selection.x && y < selection.y + selection.h && y >= selection.y;
	return x < cursor.x + cursor.w && x >= cursor.x && y < cursor.y + cursor.h && y >= cursor.y;
}

void
insert(char c)
{
	int x, y;
	for(x = 0; x < selection.w; ++x)
		for(y = 0; y < selection.h; ++y)
			set(&g, selection.x + x, selection.y + y, c);
	for(x = 0; x < cursor.w; ++x)
		for(y = 0; y < cursor.h; ++y)
			set(&g, cursor.x + x, cursor.y + y, c);
}

/* misc */

int
guide(int x, int y)
{
	if(!GUIDES)
		return 0;
	if(x % 32 == 0 && y % 32 == 0)
		return 3;
	else if(x % 8 == 0 && y % 8 == 0)
		return 4;
	return 0;
}

int
getfont(int x, int y, char c, int type, int sel)
{
	if(c >= 'A' && c <= 'Z')


@@ 109,11 97,9 @@ getfont(int x, int y, char c, int type, int sel)
		return 65;
	if(x % 8 == 0 && y % 8 == 0)
		return 68;
	if(selection.x == x && selection.y == y)
	if(cursor.x == x && cursor.y == y)
		return 66;
	if(sel || type)
		return 64;
	if(x % 2 == 0 && y % 2 == 0)
	if(sel || type || (x % 2 == 0 && y % 2 == 0))
		return 64;
	return 70;
}


@@ 165,35 151,35 @@ draw(Uint32 *dst)
	SDL_RenderPresent(gRenderer);
}

/* etc */

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

void
select(int x, int y, int w, int 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);
	cursor.x = clamp(x, 0, HOR - 1);
	cursor.y = clamp(y, 0, VER - 1);
	cursor.w = clamp(w, 1, 36);
	cursor.h = clamp(h, 1, 36);
	draw(pixels);
}

void
move(int x, int y)
{
	select(selection.x + x, selection.y + y, selection.w, selection.h);
	select(cursor.x + x, cursor.y + y, cursor.w, cursor.h);
}

void
scale(int w, int h)
{
	select(selection.x, selection.y, selection.w + w, selection.h + h);
}

/* etc */

int
error(char *msg, const char *err)
{
	printf("Error %s: %s\n", msg, err);
	return 1;
	select(cursor.x, cursor.y, cursor.w + w, cursor.h + h);
}

void


@@ 236,7 222,11 @@ domouse(SDL_Event *event)
		break;
	case SDL_MOUSEMOTION:
		if(DOWN)
			select(selection.x, selection.y, touch.x / 8 - selection.x + 1, touch.y / 8 - selection.y + 1);
			select(
				cursor.x,
				cursor.y,
				touch.x / 8 - cursor.x + 1,
				touch.y / 8 - cursor.y + 1);
		break;
	}
}


@@ 246,13 236,17 @@ dokey(SDL_Event *event)
{
	int shift = SDL_GetModState() & KMOD_LSHIFT || SDL_GetModState() & KMOD_RSHIFT;
	switch(event->key.keysym.sym) {
	case SDLK_UP: shift ? scale(0, -1) : move(0, -1); break;
	case SDLK_DOWN: shift ? scale(0, 1) : move(0, 1); break;
	case SDLK_LEFT: shift ? scale(-1, 0) : move(-1, 0); break;
	case SDLK_RIGHT: shift ? scale(1, 0) : move(1, 0); break;
	case SDLK_BACKSPACE: insert('.'); break;
	case SDLK_ASTERISK: insert('*'); break;
	case SDLK_HASH: insert('#'); break;
	case SDLK_PERIOD: insert('.'); break;
	case SDLK_COLON: insert(':'); break;
	case SDLK_SEMICOLON: insert(':'); break;
	case SDLK_ESCAPE: select(selection.x, selection.y, 1, 1); break;
	case SDLK_ESCAPE: select(cursor.x, cursor.y, 1, 1); break;
	case SDLK_0: insert('0'); break;
	case SDLK_1: insert('1'); break;
	case SDLK_2: insert('2'); break;


@@ 289,10 283,6 @@ dokey(SDL_Event *event)
	case SDLK_x: insert(shift ? 'X' : 'x'); break;
	case SDLK_y: insert(shift ? 'Y' : 'y'); break;
	case SDLK_z: insert(shift ? 'Z' : 'z'); break;
	case SDLK_UP: shift ? scale(0, -1) : move(0, -1); break;
	case SDLK_DOWN: shift ? scale(0, 1) : move(0, 1); break;
	case SDLK_LEFT: shift ? scale(-1, 0) : move(-1, 0); break;
	case SDLK_RIGHT: shift ? scale(1, 0) : move(1, 0); break;
	}
}



@@ 342,29 332,25 @@ loadfont(void)
}

int
tryload(FILE *f)
loadorca(FILE *f)
{
	char c;
	if(!f)
		return error("Load", "Invalid input file");
	while((c = fgetc(f)) != EOF) {
		if(c == '\n') {
			selection.x = 0;
			selection.y += 1;
		} else {
			set(&g, selection.x, selection.y, c);
			selection.x += 1;
		}
			cursor.x = 0;
			cursor.y += 1;
		} else
			set(&g, cursor.x++, cursor.y, c);
	}
	select(0, 0, 1, 1);
	return 1;
}

int
main(int argc, char *argv[])
{
	int ticknext = 0;
	int tickrun = 0;
	int ticknext = 0, tickrun = 0;

	if(!init())
		return error("Init", "Failure");


@@ 373,9 359,12 @@ main(int argc, char *argv[])

	create(&g, HOR, VER);
	select(0, 0, 1, 1);

	if(argc > 0)
		tryload(fopen(argv[1], "r"));
	draw(pixels);
		if(!loadorca(fopen(argv[1], "r")))
			return error("Load", "Failure");

	select(0, 0, 1, 1);

	while(1) {
		int tick = SDL_GetTicks();