~rabbits/nasu

nasu/nasu.c -rw-r--r-- 10.1 KiB
45df4b8dneauoire Fixed cursor issue 22 hours ago
                                                                                
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include <SDL2/SDL.h>
#include <stdio.h>

/* 
Copyright (c) 2020 Devine Lu Linvega

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

#define HOR 32
#define VER 16
#define PAD 8
#define ZOOM 2
#define color1 0x000000
#define color2 0x72DEC2
#define color3 0xFFFFFF
#define color4 0x444444
#define color0 0x111111

#define SZ (HOR * VER * 16)

typedef struct Brush {
	int x, y, px, py;
	int mode, size, color;
	int down, erase;
} Brush;

int colors[] = {color1, color2, color3, color4, color0};
int WIDTH = 8 * HOR + PAD * 2;
int HEIGHT = 8 * VER + PAD * 2;
int FPS = 30, GUIDES = 1;

unsigned char chrbuf[SZ];
SDL_Window *gWindow;
SDL_Renderer *gRenderer;
SDL_Texture *gTexture;
Uint32 *pixels;

Brush brush;

/* helpers */

int
distance(int ax, int ay, int bx, int by)
{
	return (bx - ax) * (bx - ax) + (by - ay) * (by - ay);
}

int
spos(char *s, char *ss)
{
	int a = 0, b = 0;
	while(s[a] != '\0') {
		if(s[a] == ss[b]) {
			if(ss[b + 1] == '\0')
				return a - b;
			b++;
		} else
			b = 0;
		a++;
	}
	return -1;
}

int
getclr(int r, int g, int b)
{
	return r && g && b && r == g && g == b ? 1 : r > g && r > b ? 2 : g > r && g > b ? 3 : 0;
}

/* chr */

int
rowchr(int x, int y)
{
	return (y % 8) + ((x / 8 + y / 8 * HOR) * 16);
}

int
getchr(int x, int y)
{
	int ch1, ch2;
	int r = rowchr(x, y);
	int px = x % 8;
	if(r < 0 || r > SZ - 8)
		return 0;
	ch1 = (chrbuf[r] >> (7 - px)) & 1;
	ch2 = (chrbuf[r + 8] >> (7 - px)) & 1;
	return ch1 && !ch2 ? 1 : !ch1 && ch2 ? 2 : ch1 && ch2 ? 3 : 0;
}

void
putchr(int x, int y, int color)
{
	int r = rowchr(x, y), px = x % 8;
	if(r < 0 || r > SZ - 8)
		return;
	if(!color) {
		chrbuf[r] &= ~(1UL << (7 - px));
		chrbuf[r + 8] &= ~(1UL << (7 - px));
	} else if(color == 2) {
		chrbuf[r] |= 1UL << (7 - px);
		chrbuf[r + 8] &= ~(1UL << (7 - px));
	} else if(color == 1) {
		chrbuf[r] &= ~(1UL << (7 - px));
		chrbuf[r + 8] |= 1UL << (7 - px);
	} else if(color == 3) {
		chrbuf[r] |= 1UL << (7 - px);
		chrbuf[r + 8] |= 1UL << (7 - px);
	}
}

void
newchr(void)
{
	int i;
	for(i = 0; i < SZ; ++i)
		chrbuf[i] = 0x00;
}

int
jagg(int x, int y)
{
	int n = getchr(x, y + 1);
	int e = getchr(x + 1, y);
	int s = getchr(x, y - 1);
	int w = getchr(x - 1, y);
	int h = getchr(x, y);
	if(h == n && h == e && h != s && h != w)
		return 1;
	if(h == e && h == s && h != w && h != n)
		return 1;
	if(h == s && h == w && h != n && h != e)
		return 1;
	if(h == w && h == n && h != e && h != s)
		return 1;
	return 0;
}

int
patt(int x, int y, int mode, int size)
{
	if(mode == 1)
		return ((x + y) % 4) == 0 && ((y - x) % 4) == 0;
	if(mode == 2)
		return ((x + y) % 2) == 0 && ((y - x) % 2) == 0;
	if(mode == 3)
		return 1;
	if(mode == 4)
		return y % size == 0;
	if(mode == 5)
		return x % size == 0;
	if(mode == 6)
		return (x + y) % size == 0 || (x - y) % size == 0;
	return 0;
}

void
fill(int x, int y, int mode, int size, int color)
{
	int ox, oy;
	for(ox = x - (size / 2); ox < x + size; ++ox)
		for(oy = y - (size / 2); oy < y + size; ++oy)
			if(mode == 7 && jagg(ox, oy))
				putchr(ox, oy, 0);
			else if(patt(ox, oy, mode, size) && distance(x, y, ox, oy) < size)
				putchr(ox, oy, color);
}

void
line(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;
	int err = dx + dy, e2;
	for(;;) {
		putchr(ax, ay, color);
		if(ax == bx && ay == by)
			break;
		e2 = 2 * err;
		if(e2 >= dy) {
			err += dy;
			ax += sx;
		}
		if(e2 <= dx) {
			err += dx;
			ay += sy;
		}
	}
}

/* draw */

void
drawchr(Uint32 *dst, int x, int y, int id)
{
	int v, h, offset = id * 16;
	for(v = 0; v < 8; v++)
		for(h = 0; h < 8; h++) {
			int px = (x * 8) + (8 - h);
			int py = (y * 8) + v;
			int ch1 = chrbuf[offset + v];
			int ch2 = chrbuf[offset + v + 8];
			int clr = ((ch1 >> h) & 0x1) + (((ch2 >> h) & 0x1) << 1);
			int key = (py + PAD) * WIDTH + (px + PAD);
			int guides = GUIDES && !clr && (x + y) % 2;
			dst[key] = colors[guides ? 4 : clr];
		}
}

void
draw(Uint32 *dst)
{
	int x, y;
	for(y = 0; y < VER; ++y)
		for(x = 0; x < HOR; ++x)
			drawchr(dst, x, y, x + y * HOR);
	SDL_UpdateTexture(gTexture, NULL, dst, WIDTH * sizeof(Uint32));
	SDL_RenderClear(gRenderer);
	SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
	SDL_RenderPresent(gRenderer);
}

/* options */

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

void
setcolor(Brush *b, int c)
{
	b->color = c;
	printf("Set Color %d\n", b->color);
}

void
setmode(Brush *b, int m)
{
	b->mode = m;
	printf("Set Mode %d\n", b->mode);
}

void
modsize(Brush *b, int mod)
{
	int res = b->size + mod;
	if(res > 0 && res < 30)
		b->size = res;
	printf("Set Size %d\n", b->size);
}

void
toggleguide(void)
{
	GUIDES = !GUIDES;
	draw(pixels);
	printf("%s Guides\n", GUIDES ? "Show" : "Hide");
}

void
destroy(void)
{
	newchr();
	draw(pixels);
	puts("Destroy");
}

void
exportchr(Brush *b)
{
	FILE *f = fopen("nasu-export.chr", "wb");
	if(!fwrite(chrbuf, sizeof(chrbuf), 1, f))
		error("Save", "Invalid output file");
	fclose(f);
	puts("Exported nasu-export.chr");
}

void
renderbmp(void)
{
	SDL_Surface *surface = SDL_GetWindowSurface(gWindow);
	GUIDES = 0;
	draw(pixels);
	SDL_RenderReadPixels(gRenderer,
		NULL,
		SDL_PIXELFORMAT_ARGB8888,
		surface->pixels,
		surface->pitch);
	SDL_SaveBMP(surface, "nasu-render.bmp");
	SDL_FreeSurface(surface);
	puts("Rendered nasu-render.bmp");
}

void
loadchr(char *path)
{
	FILE *f = fopen(path, "rb");
	if(f == NULL)
		error("Load", "Invalid input file");
	if(!fread(chrbuf, sizeof(chrbuf), 1, f))
		error("Load", "Invalid input size");
	fclose(f);
}

void
loadbmp(char *path)
{
	FILE *f = fopen(path, "rb");
	int i, width = HOR * 8, height = VER * 8, size = 3 * width * height;
	unsigned char header[54];
	unsigned char data[4096 * 256];
	if(!fread(header, sizeof(unsigned char), 54, f))
		error("Load", "Invalid bmp header");
	if(!fread(data, sizeof(unsigned char), size, f))
		error("Load", "Invalid bmp body");
	for(i = 0; i < size; i += 3) {
		int x = (i / 3) % width, y = height - (i / 3) / width - 1;
		putchr(x, y, getclr(data[i + 2], data[i + 1], data[i]));
	}
	fclose(f);
}

void
quit(void)
{
	free(pixels);
	SDL_DestroyTexture(gTexture);
	gTexture = NULL;
	SDL_DestroyRenderer(gRenderer);
	gRenderer = NULL;
	SDL_DestroyWindow(gWindow);
	gWindow = NULL;
	SDL_Quit();
	exit(0);
}

void
domouse(SDL_Event *event, Brush *b)
{
	switch(event->type) {
	case SDL_MOUSEBUTTONUP:
		if(event->button.button == SDL_BUTTON_LEFT)
			b->down = 0;
		if(event->button.button == SDL_BUTTON_RIGHT)
			b->erase = 0;
		break;
	case SDL_MOUSEBUTTONDOWN:
		if(event->button.button == SDL_BUTTON_LEFT)
			b->down = 1;
		if(event->button.button == SDL_BUTTON_RIGHT)
			b->erase = 1;
		if(event->button.button == SDL_BUTTON_MIDDLE) {
			b->erase = 0;
			if(b->px != 0 && b->py != 0) {
				b->x = (event->motion.x - (PAD * ZOOM)) / ZOOM;
				b->y = (event->motion.y - (PAD * ZOOM)) / ZOOM;
				line(b->px - 1, b->py, b->x, b->y, b->erase ? 0 : b->color);
				draw(pixels);
			}
		}
		b->px = (event->motion.x - (PAD * ZOOM)) / ZOOM;
		b->py = (event->motion.y - (PAD * ZOOM)) / ZOOM;
		if(b->down) {
			putchr(b->px - 1, b->py, b->erase ? 0 : b->color);
			draw(pixels);
		}
		break;
	case SDL_MOUSEMOTION:
		if(b->down) {
			b->x = (event->motion.x - (PAD * ZOOM)) / ZOOM;
			b->y = (event->motion.y - (PAD * ZOOM)) / ZOOM;
			if(!b->mode)
				line(b->px - 1, b->py, b->x - 1, b->y, b->erase ? 0 : b->color);
			else
				fill(b->x - 1, b->y, b->mode, b->size, b->erase ? 0 : b->color);
			draw(pixels);
			b->px = b->x;
			b->py = b->y;
		}
		break;
	}
}

void
dokey(SDL_Event *event, Brush *b)
{
	switch(event->key.keysym.sym) {
	case SDLK_1: setmode(b, 0); break;
	case SDLK_2: setmode(b, 1); break;
	case SDLK_3: setmode(b, 2); break;
	case SDLK_4: setmode(b, 3); break;
	case SDLK_5: setmode(b, 4); break;
	case SDLK_6: setmode(b, 5); break;
	case SDLK_7: setmode(b, 6); break;
	case SDLK_e: exportchr(b); break;
	case SDLK_r: renderbmp(); break;
	case SDLK_a: setcolor(b, 0); break;
	case SDLK_s: setcolor(b, 1); break;
	case SDLK_d: setcolor(b, 2); break;
	case SDLK_f: setcolor(b, 3); break;
	case SDLK_h: toggleguide(); break;
	case SDLK_z: modsize(b, -1); break;
	case SDLK_x: modsize(b, 1); break;
	case SDLK_c: setmode(b, 7); break;
	case SDLK_n: destroy(); break;
	}
}

int
init(void)
{
	int i, j;
	if(SDL_Init(SDL_INIT_VIDEO) < 0)
		return error("Init", SDL_GetError());
	gWindow = SDL_CreateWindow("Nasu",
		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);
	if(gTexture == NULL)
		return error("Texture", SDL_GetError());
	pixels = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32));
	if(pixels == NULL)
		return error("Pixels", "Failed to allocate memory");
	for(i = 0; i < HEIGHT; i++)
		for(j = 0; j < WIDTH; j++)
			pixels[i * WIDTH + j] = color1;
	return 1;
}

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

	brush.erase = 0;
	brush.down = 0;
	brush.color = 1;
	brush.size = 8;
	brush.mode = 0;

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

	if(argc > 1 && spos(argv[1], ".bmp") > -1)
		loadbmp(argv[1]);
	else if(argc > 1 && spos(argv[1], ".chr") > -1)
		loadchr(argv[1]);
	else
		newchr();

	draw(pixels);

	while(1) {
		int tick = SDL_GetTicks();
		SDL_Event event;
		if(tick < ticknext)
			SDL_Delay(ticknext - tick);
		ticknext = tick + (1000 / FPS);
		while(SDL_PollEvent(&event) != 0) {
			if(event.type == SDL_QUIT)
				quit();
			else if(event.type == SDL_MOUSEBUTTONUP ||
					event.type == SDL_MOUSEBUTTONDOWN ||
					event.type == SDL_MOUSEMOTION) {
				domouse(&event, &brush);
			} else if(event.type == SDL_KEYDOWN)
				dokey(&event, &brush);
			else if(event.type == SDL_WINDOWEVENT)
				if(event.window.event == SDL_WINDOWEVENT_EXPOSED)
					draw(pixels);
		}
	}
	quit();
	return 0;
}