-
Notifications
You must be signed in to change notification settings - Fork 2
/
pxlart.c
522 lines (452 loc) · 10.2 KB
/
pxlart.c
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
_ _
_ ____ _| | __ _ _ __| |
| '_ \ \/ / |/ _` | '__| __|
| |_) > <| | (_| | | | |
| .__/_/\_\_|\__,_|_| \__|
|_|
*/
/////////////
// HEADERS //
/////////////
#include <curses.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
//////////////////////
// GLOBAL VARIABLES //
//////////////////////
// Stores the brush
char *brush = NULL;
int brushsize = 0;
// Stores the current coordinates for the cursor
int x, y;
// Stores the dimensions of the window
int maxy, maxx;
// Stores current foreground color
int fg;
// Stores current background color
int bg;
// Stores the current color count
int count = 2;
// Window for the draw area
WINDOW *draw_win;
// Window for status line
WINDOW *status_win;
typedef struct {
int fg;
int bg;
char c;
} pxl_cell;
pxl_cell *cells = NULL;
void write_cell(int x, int y, char c);
void erase_cell(int x, int y);
void store_cell(int x, int y, char c, int w, int h, int fg, int bg);
//////////////////////
// HELPER FUNCTIONS //
//////////////////////
/*
Creates a new window with dimensions `height` and `width` starting at `starty` and `startx`
*/
WINDOW *create_newwin(int height, int width, int starty, int startx)
{
WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
return local_win;
}
/*
Creates current_win, preview_win and status_win
*/
void init_windows(void)
{
getmaxyx(stdscr, maxy, maxx);
draw_win = create_newwin(maxy-1, maxx, 0, 0);
status_win = create_newwin(1, maxx, maxy-1, 0);
keypad(draw_win, TRUE);
}
/*
Initializes ncurses
*/
void init_curses(void)
{
initscr();
noecho();
start_color();
fg = 1;
bg = 0;
init_pair(1, 0, 0);
init_pair(2, fg, bg);
}
/*
Prints status line
*/
void printStatus(void)
{
werase(status_win);
wprintw(status_win, "(b)rush: ");
wattron(status_win, COLOR_PAIR(count));
wprintw(status_win, "%s",brush);
wattroff(status_win, COLOR_PAIR(count));
wprintw(status_win, " (c)olor: %d ", fg);
wprintw(status_win, "(B)ackground: %d", bg);
//wprintw(status_win, " (s)ave (l)oad");
wrefresh(status_win);
}
/*
Sets brush to some character
*/
void setBrush(char *newBrush)
{
if( strcmp(newBrush,"default") == 0 )
{
setBrush("█");
return;
}
free(brush);
int allocSize = snprintf(NULL, 0, "%s", newBrush);
brushsize = allocSize;
brush = malloc(allocSize+1);
snprintf(brush, allocSize+1, "%s", newBrush);
}
/*
Moves the cursor up
*/
void goUp(void)
{
getyx(draw_win, y, x);
y -= 1;
if(y < 0) y = 0;
wmove(draw_win, y, x);
}
/*
Moves the cursor down
*/
void goDown(void)
{
getyx(draw_win, y, x);
y += 1;
if(y >= (maxy - 1)) y = maxy - 2;
wmove(draw_win, y, x);
}
/*
Moves the cursor left
*/
void goLeft(void)
{
getyx(draw_win, y, x);
x -= 1;
if(x < 0) x = 0;
wmove(draw_win, y, x);
}
/*
Moves the cursor right
*/
void goRight(void)
{
getyx(draw_win, y, x);
x += 1;
if(x >= maxx) x = maxx - 1;
wmove(draw_win, y, x);
}
/*
Draw
*/
void draw(void)
{
int i;
getyx(draw_win, y, x);
wmove(draw_win, y, x);
wattron(draw_win, COLOR_PAIR(count));
wprintw(draw_win, "%s", brush);
wattroff(draw_win, COLOR_PAIR(count));
wmove(draw_win, y, x+1);
for(i = 0; i < brushsize; i++) {
write_cell(x + i, y, brush[i]);
}
}
/*
Erase
*/
void eraser(void)
{
getyx(draw_win, y, x);
wmove(draw_win, y, x);
wprintw(draw_win, "%s", " ");
erase_cell(x, y);
wmove(draw_win, y, x+1);
}
/*
Initializes the program
*/
void init(void)
{
int sz;
int i;
setlocale(LC_ALL, "");
setBrush("#");
init_curses();
init_windows();
sz = maxx * maxy;
cells = calloc(1, sizeof(pxl_cell) * sz);
for(i = 0; i < sz; i++) {
cells[i].c = ' ';
cells[i].fg = 0;
cells[i].bg = 0;
}
}
/*
Cleanup the program
*/
void cleanup(void)
{
endwin();
free(brush);
free(cells);
}
/*
Save Project
*/
void save(void)
{
// For input (stdin & file)
char *buf = NULL;
// Stores Filename of save file
char *filename = NULL;
// Stores Filename of colors file
char *colorsfile = NULL;
// Stores Cell Data
char *datafile = NULL;
// Stores amount to allocate
int allocSize;
// Write all colors in `colorsfile`
FILE *f;
// Write data in `datafile`
FILE *df;
int sz;
int i;
werase(status_win);
echo();
wprintw(status_win, "Enter Name of Project: ");
// Read filename and appropriately set `filename` and `colorsfile`
buf = malloc(250);
wgetnstr(status_win, buf, 250);
allocSize = snprintf(NULL,0,"%s.save",buf);
filename = malloc(allocSize+1);
snprintf(filename,allocSize+1,"%s.save",buf);
colorsfile = malloc(allocSize+1);
snprintf(colorsfile,allocSize+1,"%s.cols",buf);
datafile = malloc(allocSize+1);
snprintf(datafile ,allocSize+1,"%s.data",buf);
// Save screen output in ``filename
scr_dump(filename);
// Write all colors in `colorsfile`
f = fopen(colorsfile,"w");
df = fopen(datafile,"w");
for(int i=1; i<=count; i++)
{
short int *a, *b;
short int x, y;
a = &x;
b = &y;
pair_content(i, a, b);
fprintf(f,"%d %hd %hd\n", i, x, y);
}
fprintf(df, "%d %d\n", maxx, maxy);
sz = maxx * maxy;
for(i = 0; i < sz; i++) {
fprintf(df, "%d %d %d\n",
cells[i].fg,
cells[i].bg,
(unsigned char)cells[i].c);
}
// Close file and free memory
fclose(f);
fclose(df);
free(filename);
free(colorsfile);
free(buf);
noecho();
}
/*
Load Project
*/
void load(void)
{
// For input (stdin & file)
char *buf = NULL;
// Stores Filename of save file
char *filename = NULL;
// Stores Filename of colors file
char *colorsfile = NULL;
// Stores Filename of data file
char *datafile = NULL;
// Stores amount to allocate
int allocSize;
FILE *f;
int w, h;
int x, y;
int the_fg, the_bg;
int c;
werase(status_win);
echo();
wprintw(status_win, "Enter Name of Project: ");
// Take filename input and appropriately set `filename` and `colorsfile`
buf = malloc(250);
wgetnstr(status_win, buf, 250);
allocSize = snprintf(NULL,0,"%s.save",buf);
filename = malloc(allocSize+1);
snprintf(filename,allocSize+1,"%s.save",buf);
colorsfile = malloc(allocSize+1);
snprintf(colorsfile,allocSize+1,"%s.cols",buf);
datafile = malloc(allocSize+1);
snprintf(datafile,allocSize+1,"%s.data",buf);
// If colorsfile doesn't exists, free memory and break
f = fopen(colorsfile,"r");
if(f == NULL)
{
noecho();
free(filename);
free(colorsfile);
free(buf);
free(datafile);
return;
}
// Remove existing color pairs
reset_color_pairs();
// Load color pairs from `colorsfile`
int i=1;
while(fgets(buf, 250, (FILE*) f))
{
int n, f, b;
sscanf(buf,"%d %d %d", &n, &f, &b);
init_pair(n, f, b);
count = i++;
}
fclose(f);
// Restore screen layout
scr_restore(filename);
doupdate();
f = fopen(datafile,"r");
if(f != NULL) {
fscanf(f, "%d %d\n", &w, &h);
for(y = 0; y < h; y++) {
for(x = 0; x < w; x++) {
fscanf(f, "%d %d %d\n", &the_fg, &the_bg, (int *)&c);
store_cell(x, y, (char)c, w, h, the_fg, the_bg);
}
}
fclose(f);
}
// free memory
noecho();
free(filename);
free(colorsfile);
free(datafile);
free(buf);
}
/*
Stores cell (using arbitrary width/height)
*/
void store_cell(int x, int y, char c, int w, int h, int fg, int bg)
{
int pos;
if(x < 0 || x >= w) return;
if(y < 0 || y >= h) return;
pos = y * w + x;
cells[pos].c = c;
cells[pos].fg = fg;
cells[pos].bg = bg;
}
/*
Write a cell
*/
void write_cell(int x, int y, char c)
{
store_cell(x, y, c, maxx, maxy, fg, bg);
}
/*
Erase a cell
*/
void erase_cell(int x, int y)
{
write_cell(x, y, ' ');
}
///////////////////
// MAIN FUNCTION //
///////////////////
int main()
{
init();
int a;
char temp[10];
do
{
printStatus();
a = wgetch(draw_win);
switch(a)
{
case KEY_UP:
case 'k':
goUp();
break;
case KEY_DOWN:
case 'j':
goDown();
break;
case KEY_LEFT:
case 'h':
goLeft();
break;
case KEY_RIGHT:
case 'l':
goRight();
break;
// Draw the brush character
case ' ':
case 'd':
draw();
break;
// Erase the brush character
case 'e':
eraser();
break;
// Set new brush character
case 'b':
werase(status_win);
wprintw(status_win, "Enter New Brush Symbol: ");
echo();
wgetnstr(status_win, temp, 10);
noecho();
setBrush(temp);
break;
// Set new foreground color
case 'c':
werase(status_win);
wprintw(status_win, "Enter Shell Color (0-255): ");
echo();
wscanw(status_win, "%d", &fg);
init_pair(++count, fg, bg);
noecho();
break;
// Set new background color
case 'B':
werase(status_win);
wprintw(status_win, "Enter Shell Color (0-255): ");
echo();
wscanw(status_win, "%d", &bg);
init_pair(++count, fg, bg);
noecho();
break;
// Save
case 's':
save();
break;
// Load
case 'o':
load();
break;
}
} while(a != 'q');
cleanup();
return 0;
}