Skip to content

Commit 9e6523c

Browse files
committed
Implement undo (single undo; undo + undo = redo)
1 parent 48cdd4a commit 9e6523c

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,3 @@ License
2525
-------
2626
ISC License.
2727
See `LICENSE` for details.
28-
29-
Todo
30-
----
31-
* Undo command (u)

sprite.c

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ typedef struct pixel {
4242
} pixel_t;
4343
static pixel_t pixel[32][32];
4444

45+
static pixel_t temp[32][32];
46+
static pixel_t undo[32][32];
47+
4548
static int
4649
switch_color(int color)
4750
{
@@ -141,11 +144,13 @@ instructions(void)
141144
printw("d: delete pixel");
142145
move(11, 50 + (extended ? 16 : 0));
143146
printw("f: fill region");
144-
move(13, 50 + (extended ? 16 : 0));
145-
printw("e: export to PNG");
147+
move(12, 50 + (extended ? 16 : 0));
148+
printw("u: undo");
146149
move(14, 50 + (extended ? 16 : 0));
147-
printw("s: save");
150+
printw("e: export to PNG");
148151
move(15, 50 + (extended ? 16 : 0));
152+
printw("s: save");
153+
move(16, 50 + (extended ? 16 : 0));
149154
printw("q: quit");
150155
}
151156

@@ -228,6 +233,38 @@ change_color(int y, int x, int color)
228233
return new_color;
229234
}
230235

236+
static void
237+
do_undo(void)
238+
{
239+
int i, j;
240+
241+
for (i = 0; i < 16 + (extended ? 16 : 0); i++) {
242+
for (j = 0; j < 16 + (extended ? 16 : 0); j++)
243+
temp[i][j].color = pixel[i][j].color;
244+
}
245+
246+
for (i = 0; i < 16 + (extended ? 16 : 0); i++) {
247+
for (j = 0; j < 16 + (extended ? 16 : 0); j++)
248+
pixel[i][j].color = undo[i][j].color;
249+
}
250+
251+
for (i = 0; i < 16 + (extended ? 16 : 0); i++) {
252+
for (j = 0; j < 16 + (extended ? 16 : 0); j++)
253+
undo[i][j].color = temp[i][j].color;
254+
}
255+
}
256+
257+
static void
258+
update_undo(void)
259+
{
260+
int i, j;
261+
262+
for (i = 0; i < 16 + (extended ? 16 : 0); i++) {
263+
for (j = 0; j < 16 + (extended ? 16 : 0); j++)
264+
undo[i][j].color = pixel[i][j].color;
265+
}
266+
}
267+
231268
static void
232269
fill_region(int y, int x, int color, int target)
233270
{
@@ -538,6 +575,7 @@ main_loop(void)
538575
break;
539576
case ' ':
540577
print:
578+
update_undo();
541579
pixel[y - 4][x - 32].color = color;
542580
dirty = 1;
543581
break;
@@ -549,6 +587,7 @@ main_loop(void)
549587
break;
550588
case 'D':
551589
case 'd':
590+
update_undo();
552591
pixel[y - 4][x - 32].color = -1;
553592
break;
554593
case 'E':
@@ -557,6 +596,7 @@ main_loop(void)
557596
break;
558597
case 'F':
559598
case 'f':
599+
update_undo();
560600
fill_region(y, x, color, pixel[y - 4][x - 32].color);
561601
break;
562602
case 'S':
@@ -569,6 +609,10 @@ main_loop(void)
569609
if (dirty == 1)
570610
confirm_quit(y, x);
571611
loop = 0;
612+
break;
613+
case 'U':
614+
case 'u':
615+
do_undo();
572616
}
573617

574618
draw_screen(y, x, color);

0 commit comments

Comments
 (0)