Skip to content

Commit a0152c7

Browse files
committed
32x32 canvas support.
1 parent 9801e37 commit a0152c7

File tree

1 file changed

+104
-56
lines changed

1 file changed

+104
-56
lines changed

sprite.c

Lines changed: 104 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include "colors.h"
3131

32+
static char extended;
33+
3234
/*
3335
* color -1 == transparent
3436
*/
@@ -37,7 +39,7 @@ typedef struct pixel {
3739
char y;
3840
int color;
3941
} pixel_t;
40-
static pixel_t pixel[16][16];
42+
static pixel_t pixel[32][32];
4143

4244
static int
4345
switch_color(int color)
@@ -54,11 +56,11 @@ draw_transparency(void)
5456
{
5557
int i, j, k = 7;
5658

57-
for (i = 4; i < 20; i++) {
58-
if (i % 4 == 0)
59+
for (i = 4; i < 20 + (extended ? 16 : 0); i++) {
60+
if ((i - 4) % (extended ? 8 : 4) == 0)
5961
k = switch_color(k);
60-
for (j = 32; j < 48; j++) {
61-
if (j % 4 == 0)
62+
for (j = 32; j < 48 + (extended ? 16 : 0); j++) {
63+
if (j % (extended ? 8: 4) == 0)
6264
k = switch_color(k);
6365
attron(COLOR_PAIR(k));
6466
mvaddch(i, j, ' ');
@@ -74,8 +76,8 @@ draw_screen(int y, int x, int color)
7476

7577
draw_transparency();
7678

77-
for (i = 4; i < 20; i++) {
78-
for (j = 32; j < 48; j++) {
79+
for (i = 4; i < 20 + (extended ? 16 : 0); i++) {
80+
for (j = 32; j < 48 + (extended ? 16 : 0); j++) {
7981
if (pixel[i - 4][j - 32].color != -1) {
8082
attron(COLOR_PAIR(pixel[i - 4][j - 32].color));
8183
mvaddch(i, j, ' ');
@@ -96,8 +98,8 @@ init_pixels(void)
9698
{
9799
int i, j;
98100

99-
for (i = 0; i < 16; i++) {
100-
for (j = 0; j < 16; j++) {
101+
for (i = 0; i < 16 + (extended ? 16 : 0); i++) {
102+
for (j = 0; j < 16 + (extended ? 16 : 0); j++) {
101103
pixel[i][j].x = j;
102104
pixel[i][j].y = i;
103105
pixel[i][j].color = -1;
@@ -119,26 +121,26 @@ instructions(void)
119121
{
120122
int i;
121123

122-
move(2, 35);
123-
printw("Sprite 1.4");
124+
move(2, 35 + (extended ? 8 : 0));
125+
printw("Sprite 1.5");
124126

125-
move(4, 50);
127+
move(4, 50 + (extended ? 16 : 0));
126128
printw("Key commands");
127-
move(5, 50);
129+
move(5, 50 + (extended ? 16 : 0));
128130
printw("============");
129-
move(6, 50);
131+
move(6, 50 + (extended ? 16 : 0));
130132
printw("Arrow keys: move");
131-
move(7, 50);
133+
move(7, 50 + (extended ? 16 : 0));
132134
printw("Spacebar: draw pixel");
133-
move(8, 50);
135+
move(8, 50 + (extended ? 16 : 0));
134136
printw("c: change color");
135-
move(9, 50);
137+
move(9, 50 + (extended ? 16 : 0));
136138
printw("d: delete pixel");
137-
move(10, 50);
139+
move(10, 50 + (extended ? 16 : 0));
138140
printw("e: export to PNG");
139-
move(11, 50);
141+
move(11, 50 + (extended ? 16 : 0));
140142
printw("s: save");
141-
move(12, 50);
143+
move(12, 50 + (extended ? 16 : 0));
142144
printw("q: quit");
143145
}
144146

@@ -173,21 +175,21 @@ scrinit(void)
173175
move(3, 31);
174176

175177
addch(ACS_ULCORNER);
176-
for (i = 0; i < 16; i++)
178+
for (i = 0; i < 16 + (extended ? 16 : 0); i++)
177179
addch(ACS_HLINE);
178180
addch(ACS_URCORNER);
179181

180-
for (i = 4; i < 20; i++) {
182+
for (i = 4; i < 20 + (extended ? 16 : 0); i++) {
181183
move(i, 31);
182184
addch(ACS_VLINE);
183-
move(i, 48);
185+
move(i, 48 + (extended ? 16 : 0));
184186
addch(ACS_VLINE);
185187
}
186188

187189
move(i, 31);
188190

189191
addch(ACS_LLCORNER);
190-
for (i = 0; i < 16; i++)
192+
for (i = 0; i < 16 + (extended ? 16 : 0); i++)
191193
addch(ACS_HLINE);
192194
addch(ACS_LRCORNER);
193195
}
@@ -226,42 +228,45 @@ file_export(int y, int x)
226228
{
227229
FILE *fp;
228230
char buf[PATH_MAX];
229-
int code, height = 16, i, j, k = 0, width = 16;
231+
int code, height, i, j, k = 0, width;
230232
png_structp png_ptr = NULL;
231233
png_infop info_ptr = NULL;
232-
png_byte row[64];
234+
png_byte row[64], extended_row[128];
233235
png_text title_text;
234236

237+
height = (extended ? 32 : 16);
238+
width = (extended ? 32 : 16);
239+
235240
memset(buf, 0, sizeof(buf));
236241

237-
move(21, 31);
242+
move(21 + (extended ? 16 : 0), 31);
238243
printw("Name: ");
239244
echo();
240245
getnstr(buf, sizeof(buf) - 1);
241246
noecho();
242247

243248
if ((fp = fopen(buf, "w+")) == NULL) {
244-
move(21, 31);
249+
move(21 + (extended ? 16 : 0), 31);
245250
printw("Error: could not open %s for writing", buf);
246251
goto out;
247252
}
248253

249254
if ((png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) {
250-
move(21, 31);
255+
move(21 + (extended ? 16 : 0), 31);
251256
printw("Error: could not allocate png write struct");
252257
(void) fclose(fp);
253258
goto out;
254259
}
255260

256261
if ((info_ptr = png_create_info_struct(png_ptr)) == NULL) {
257-
move(21, 31);
262+
move(21 + (extended ? 16 : 0), 31);
258263
printw("Error: could not allocate png info struct");
259264
(void) fclose(fp);
260265
goto out;
261266
}
262267

263268
if (setjmp(png_jmpbuf(png_ptr))) {
264-
move(21, 31);
269+
move(21 + (extended ? 16 : 0), 31);
265270
printw("Error: could not create png");
266271
(void) fclose(fp);
267272
goto out;
@@ -281,15 +286,29 @@ file_export(int y, int x)
281286
for (i = 0; i < height; i++) {
282287
k = 0;
283288
for (j = 0; j < width; j++) {
284-
row[k++] = (colors[pixel[i][j].color] >> 16) & 0xff;
285-
row[k++] = (colors[pixel[i][j].color] >> 8) & 0xff;
286-
row[k++] = colors[pixel[i][j].color] & 0xff;
287-
if (pixel[i][j].color == -1)
288-
row[k++] = 0;
289-
else
290-
row[k++] = 0xff;
289+
if (extended) {
290+
extended_row[k++] = (colors[pixel[i][j].color] >> 16) & 0xff;
291+
extended_row[k++] = (colors[pixel[i][j].color] >> 8) & 0xff;
292+
extended_row[k++] = colors[pixel[i][j].color] & 0xff;
293+
if (pixel[i][j].color == -1)
294+
extended_row[k++] = 0;
295+
else
296+
extended_row[k++] = 0xff;
297+
} else {
298+
row[k++] = (colors[pixel[i][j].color] >> 16) & 0xff;
299+
row[k++] = (colors[pixel[i][j].color] >> 8) & 0xff;
300+
row[k++] = colors[pixel[i][j].color] & 0xff;
301+
if (pixel[i][j].color == -1)
302+
row[k++] = 0;
303+
else
304+
row[k++] = 0xff;
305+
}
306+
}
307+
if (extended) {
308+
png_write_row(png_ptr, extended_row);
309+
} else {
310+
png_write_row(png_ptr, row);
291311
}
292-
png_write_row(png_ptr, row);
293312
}
294313
png_write_end(png_ptr, NULL);
295314

@@ -318,7 +337,7 @@ file_save(int y, int x)
318337

319338
memset(buf, 0, sizeof(buf));
320339

321-
move(21, 31);
340+
move(21 + (extended ? 16 : 0), 31);
322341
printw("Name: ");
323342
echo();
324343
getnstr(buf, sizeof(buf) - 1);
@@ -330,8 +349,8 @@ file_save(int y, int x)
330349
goto out;
331350
}
332351

333-
for (i = 0; i < 16; i++) {
334-
for (j = 0; j < 16; j++) {
352+
for (i = 0; i < 16 + (extended ? 16 : 0); i++) {
353+
for (j = 0; j < 16 + (extended ? 16 : 0); j++) {
335354
if (pixel[i][j].color != -1)
336355
fprintf(fp, "%d,%d,%d\n", pixel[i][j].y, pixel[i][j].x, pixel[i][j].color);
337356
}
@@ -389,11 +408,17 @@ file_open(const char *fn)
389408
}
390409
colorbuf[i] = '\0';
391410

392-
y = strtonum(ybuf, 0, 15, &errstr);
411+
if (extended)
412+
y = strtonum(ybuf, 0, 31, &errstr);
413+
else
414+
y = strtonum(ybuf, 0, 15, &errstr);
393415
if (errstr != NULL)
394416
return;
395417

396-
x = strtonum(xbuf, 0, 15, &errstr);
418+
if (extended)
419+
x = strtonum(xbuf, 0, 31, &errstr);
420+
else
421+
x = strtonum(xbuf, 0, 15, &errstr);
397422
if (errstr != NULL)
398423
return;
399424

@@ -418,13 +443,13 @@ confirm_quit(int y, int x)
418443
int c;
419444

420445
again:
421-
move(21, 31);
446+
move(21 + (extended ? 16 : 0), 31);
422447
printw("Save? [y/n]: ");
423448
echo();
424449
c = getch();
425450
noecho();
426451

427-
move(21, 31);
452+
move(21 + (extended ? 16 : 0), 31);
428453
printw(" ");
429454

430455
if (c == 'Y' || c == 'y')
@@ -436,7 +461,10 @@ confirm_quit(int y, int x)
436461
static void
437462
main_loop(void)
438463
{
439-
int c, color = 0, dirty = 0, loop = 1, o, x = 39, y = 11;
464+
int c, color = 0, dirty = 0, loop = 1, o, x, y;
465+
466+
x = (extended ? 47 : 39);
467+
y = (extended ? 19 : 11);
440468

441469
attron(COLOR_PAIR(color));
442470
mvaddch(y, x, ' ');
@@ -454,8 +482,8 @@ main_loop(void)
454482
case KEY_DOWN:
455483
case 'J':
456484
case 'j':
457-
if (++y > 19)
458-
y = 19;
485+
if (++y > (extended ? 35: 19))
486+
y = (extended ? 35: 19);
459487
break;
460488
case KEY_LEFT:
461489
case 'H':
@@ -466,8 +494,8 @@ main_loop(void)
466494
case KEY_RIGHT:
467495
case 'L':
468496
case 'l':
469-
if (++x > 47)
470-
x = 47;
497+
if (++x > (extended ? 63 : 47))
498+
x = (extended ? 63 : 47);
471499
break;
472500
case ' ':
473501
pixel[y - 4][x - 32].color = color;
@@ -501,14 +529,34 @@ main_loop(void)
501529
}
502530
}
503531

532+
static void
533+
usage(void)
534+
{
535+
536+
fprintf(stderr, "usage: %s [-e] [file]\n", getprogname());
537+
538+
exit(1);
539+
}
540+
504541
int
505542
main(int argc, char *argv[])
506543
{
544+
int ch;
507545

508-
if (argc > 2) {
509-
fprintf(stderr, "usage: %s [file]\n", getprogname());
510-
exit(1);
546+
while ((ch = getopt(argc, argv, "e")) != -1) {
547+
switch (ch) {
548+
case 'e':
549+
extended = 1;
550+
break;
551+
default:
552+
usage();
553+
}
511554
}
555+
argc -= optind;
556+
argv += optind;
557+
558+
if (argc > 1)
559+
usage();
512560

513561
initscr();
514562
clear();
@@ -525,8 +573,8 @@ main(int argc, char *argv[])
525573

526574
draw_transparency();
527575

528-
if (argc == 2)
529-
file_open(argv[1]);
576+
if (argc == 1)
577+
file_open(*argv);
530578

531579
main_loop();
532580

0 commit comments

Comments
 (0)