aboutsummaryrefslogtreecommitdiff
path: root/src/canvas.c
blob: 5e2956636363e79a7e42ce42f3875cb3c1396b38 (plain)
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
523
524
525
526
527
528
529
530
531
532
533
534
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

#include "types.h"
#include "canvas.h"
#include "cdraw.h"
#include "palette.h"
#include "tools.h"
#include "debug.h"

uint8 is_drawing;

#define COORD(x,y)  ((x) + (y) * c->w)

static Layer   *layer_create(uint, uint);
static void     layer_destroy(Layer *);
static uint     canvas_fill_bfs(Canvas *, int, int, uint, uint);
static uint     canvas_blend_color(uint, uint);
static void     canvas_point_redraw(Canvas *, long int, long int);
static void     canvas_set_proj_path(Canvas *, const char *);

static uint8
canvas_coord_get(Canvas *c, long int tx, long int ty, long int *x, long int *y)
{
    if (tx < c->x || ty < c->y) return 1;
    tx = (tx - c->x) / c->zoom;
    ty = (ty - c->y) / c->zoom;
    if (tx >= c->w || ty >= c->h) return 1;
    *x = tx;
    *y = ty;
    return 0;
}

Canvas *
canvas_init(uint w, uint h, void *ren)
{
	Canvas *c;
	int i, j;
	SDL_Rect dest;

	dest.w = dest.h = 16;
	is_drawing = 0;
	c = (Canvas *)malloc(sizeof(Canvas));
	c->w = w;
	c->h = h;
	c->cur_col = 0;
	c->cur_layer = 0;
	c->zoom = 1;
	c->proj_path = NULL;
	c->layer_arr_cnt = 1;
	c->layer_arr_sz = 1;
	c->x = c->y = 0;
	c->layers = malloc(sizeof(*(c->layers)));
	c->temp_pix = malloc(w * h * sizeof(SDL_Point));
	c->pres_pix = malloc(w * h * sizeof(* c->pres_pix));

	c->layers[0] = layer_create(w, h);

	for (i = 0; i < c->w * c->h; ++i)
		c->pres_pix[i] = 0;

    if (c->layers[0] == NULL) {
        fprintf(stderr, "Error while creating layer");
        free(c);
        return NULL;
    }

    c->back = SDL_CreateTexture(
            ren, SDL_PIXELFORMAT_RGBA8888,
            SDL_TEXTUREACCESS_TARGET,
            w, h);

    if (c->back == NULL) {
        printf("SDL_CreateTexture failed: %s\n", SDL_GetError());
        free(c);
        return NULL;
    }

    c->pres = SDL_CreateTexture(
            ren, SDL_PIXELFORMAT_RGBA8888,
            SDL_TEXTUREACCESS_TARGET,
            w, h);

    if (c->pres == NULL) {
        printf("SDL_CreateTexture failed: %s\n", SDL_GetError());
        free(c);
        return NULL;
    }

    SDL_SetRenderTarget(ren, c->back);
    for (i = 0; i < w; i += 16) {
        dest.x = i;
        for (j = 0; j < h; j += 16) {
            dest.y = j;
            if (((j+i)>>4)&1)
                SDL_SetRenderDrawColor(ren, 225, 225, 225, 255);
            else
                SDL_SetRenderDrawColor(ren, 125, 125, 125, 255);
            SDL_RenderFillRect(ren, &dest);
        }
    }
    SDL_SetRenderTarget(ren, NULL);

    return c;
}

Canvas *
canvas_open(const char *path, void *ren)
{
	int ver, w, h;
	Canvas *c;
	FILE *fp = fopen(path, "r");
	if (fp == NULL) return NULL;
	fscanf(fp, "%d;%d;%d;", &ver, &w, &h);
	c = canvas_init(w, h, ren);
	fread(c->layers[0]->pix, sizeof *(c->layers[0]->pix), c->w*c->h, fp);
	fclose(fp);

	canvas_refresh(c);
	canvas_set_proj_path(c, path);

	return c;
}

Canvas *
canvas_import_png(const char *path, void *ren)
{
/* TODO: check path and SDL errors */
/* TODO: import to layer */
    Canvas *c;
/* TODO: solve prob (maybe using STB) */
/* TODO: really a hack. Rewrite */
    SDL_Texture *tex;
    SDL_Surface *sur;
    SDL_Rect rect;
    rect.x = rect.y = 0;
    sur = IMG_Load(path);
    if (sur == NULL) return NULL;
    tex = SDL_CreateTextureFromSurface(ren, sur);
    if (tex == NULL) return NULL;

    c = canvas_init(sur->w, sur->h, ren);
    if (c == NULL) return NULL;

    SDL_SetRenderTarget(ren, c->pres);
    SDL_RenderCopy(ren, tex, NULL, NULL);
    rect.w = sur->w;
    rect.h = sur->h;
    SDL_RenderReadPixels(ren, &rect, SDL_PIXELFORMAT_RGBA8888,
            c->layers[0]->pix, (sizeof *(c->layers[0]->pix)) * c->w);
    SDL_SetRenderTarget(ren, NULL);

    SDL_DestroyTexture(tex);
    SDL_FreeSurface(sur);
	canvas_refresh(c);

    return c;
}

void
canvas_destroy(Canvas *c)
{
	int i;
	if (c == NULL) return;
	SDL_DestroyTexture(c->pres);
	SDL_DestroyTexture(c->back);
	for (i = 0; i < c->layer_arr_cnt; ++i)
		layer_destroy(c->layers[i]);
	free(c->layers);
	free(c->temp_pix);
	free(c->pres_pix);
	free(c->proj_path);
	free(c);
	SDL_SetWindowTitle(win, "cdraw");
}

void
canvas_redraw(Canvas *c, void *ren, int mx, int my)
{
	long int x, y;
	SDL_SetRenderTarget(ren, c->pres);
	SDL_RenderCopy(ren,c->back,NULL,NULL);
	/* TODO: Draw the pointer between selected layers instead of on top of all layers */
	/* TODO: Draw by copying data, not calling SDL function */
	for (x = 0; x < c->w; ++x) {
		for (y = 0; y < c->h; ++y) {
			SDL_SetRenderDrawColor(ren, INTTOCOLA(c->pres_pix[COORD(x, y)]));
			SDL_RenderDrawPoint(ren, x, y);
		}
	}
	if (mx > 0 && my > 0 && canvas_coord_get(c, mx, my, &x, &y) == 0) {
		SDL_SetRenderDrawColor(ren,
			    ((Palette *)def_palette)->clist[c->cur_col].r,
			    ((Palette *)def_palette)->clist[c->cur_col].g,
			    ((Palette *)def_palette)->clist[c->cur_col].b,
			    255);
		SDL_RenderDrawPoint(ren, x, y);
	}
	if (is_drawing) {
		SDL_SetRenderDrawColor(ren, 30, 30, 30, 255);
		SDL_RenderDrawPoint(ren, 0, 0);
	}
	SDL_SetRenderTarget(ren, NULL);
	return;
}

void
canvas_present(Canvas *c, void *ren)
{
    SDL_Rect dest;
    dest.x = c->x;
    dest.y = c->y;
    dest.h = c->h*c->zoom;
    dest.w = c->w*c->zoom;
    SDL_RenderCopy(ren,c->pres,NULL,&dest);
}

void
canvas_zoom_change(Canvas *c, short int v)
{
    if (v < 0) {
        if (c->zoom <= (-v))
            c->zoom = (-v)+1;
    }
    c->zoom += v;
    if (c->zoom > maxzoom)
        c->zoom = maxzoom;
}

/* TODO: make static */
void
canvas_point_draw(Canvas *c, long int x, long int y)
{
	unsigned int oldcol, newcol;
	int i;
/* TODO: better */
	oldcol = c->layers[c->cur_layer]->pix[COORD(x,y)];
	newcol = COLTOINTA(
	        ((Palette *)def_palette)->clist[c->cur_col].r,
	        ((Palette *)def_palette)->clist[c->cur_col].g,
	        ((Palette *)def_palette)->clist[c->cur_col].b,
	        255);

	if (tool_array[tool_cur] == TOOL_TYPE_ERASER)
		newcol = 0;

	if (newcol == oldcol)
		return;

	SDL_SetRenderTarget(ren, c->pres);
	if (tool_array[tool_cur] == TOOL_TYPE_ERASER) {
		c->layers[c->cur_layer]->pix[COORD(x,y)] = 0;
		canvas_point_redraw(c, x, y);
	} else if (tool_array[tool_cur] == TOOL_TYPE_PENCIL) {
		c->layers[c->cur_layer]->pix[COORD(x,y)] = COLTOINTA(
		        ((Palette *)def_palette)->clist[c->cur_col].r,
		        ((Palette *)def_palette)->clist[c->cur_col].g,
		        ((Palette *)def_palette)->clist[c->cur_col].b,
		        255);
		canvas_point_redraw(c, x, y);
	} else if (tool_array[tool_cur] == TOOL_TYPE_FILL) {
		oldcol = canvas_fill_bfs(c, x, y, oldcol, newcol);
		for (i = 0; i < oldcol; ++i)
			canvas_point_redraw(c, ((SDL_Point *)c->temp_pix)[i].x, ((SDL_Point *)c->temp_pix)[i].y);
	}
	SDL_SetRenderTarget(ren, NULL);
}

/* Mouse input */
void
canvas_mousel_down(Canvas *c, long int x, long int y)
{
	int i;
	if (canvas_coord_get(c, x, y, &x, &y))
		return;
	switch (tool_array[tool_cur]) {
	case TOOL_TYPE_CPICKER:
		for (i = 0; i < ((Palette *)def_palette)->num; ++i) {
			if (c->layers[c->cur_layer]->pix[COORD(x,y)] == COLTOINTA(
			        ((Palette *)def_palette)->clist[i].r,
			        ((Palette *)def_palette)->clist[i].g,
			        ((Palette *)def_palette)->clist[i].b,
			        255)) {
				c->cur_col = i;
				/* ui_redraw_panel(UI_PANELTYPE_PALETTE); */
				break;
			}
		}
		break;
	case TOOL_TYPE_PENCIL:
	case TOOL_TYPE_ERASER:
	case TOOL_TYPE_FILL:
		is_drawing = 1;
		canvas_point_draw(c, x, y);
		break;
	}
}

void
canvas_mouse_move(Canvas *c, long int x, long int y)
{
	if (is_drawing == 0)
		return;
	if (canvas_coord_get(c, x, y, &x, &y))
		return;
	switch (tool_array[tool_cur]) {
	case TOOL_TYPE_PENCIL:
	case TOOL_TYPE_ERASER:
	case TOOL_TYPE_FILL:
		canvas_point_draw(c, x, y);
		break;
	}
}

void
canvas_mousel_up(Canvas *c)
{
/*	if (is_drawing && (tool_array[tool_cur] == TOOL_TYPE_PENCIL || tool_array[tool_cur] = TOOL_TYPE_ERASER)) {*/
/*TODO: save action*/
/*	}*/
	is_drawing = 0;
}

void
canvas_move_x(Canvas *c, long int delta)
{
	c->x += delta;
}

void
canvas_move_y(Canvas *c, long int delta)
{
	c->y += delta;
}


uint8
canvas_save(Canvas *c, const char *path, short int s)
{
	FILE *fp = fopen(path, "w");
	if (fp == NULL) {
		/* TODO: Error handling */
		return 1;
	}

	fprintf(fp, "1;%d;%d;", c->w, c->h);
	int u = fwrite(c->layers[0]->pix, sizeof *(c->layers[0]->pix), c->w*c->h, fp);
	fclose(fp);
	if (s)
		canvas_set_proj_path(c, path);
}

uint8
canvas_export_png(Canvas *c, const char *path, void *ren)
{
/* TODO: check path */
	SDL_Surface *surf;
	/* SDL_Texture *tex; */
	SDL_Rect dest;
	int i, j, k, ret = 0;
	dest.x = dest.y = 0;
	dest.w = c->w;
	dest.h = c->h;


	surf = SDL_CreateRGBSurfaceWithFormat( 0, c->w, c->h, 32, SDL_PIXELFORMAT_RGBA8888);
	ret = ret || (surf == NULL);

	SDL_LockSurface(surf);

/* TODO: Assuming bad stuff (pixel size) */
	for (j = 0; j < c->h; j++) {
		for (k = 0; k < c->w; k++) {
			canvas_point_redraw(c, k, j);
			((unsigned int *)surf->pixels)[COORD(k,j)] = c->pres_pix[COORD(k, j)];
		}
	}

	SDL_UnlockSurface(surf);
	ret = ret || (IMG_SavePNG(surf, path) == -1);
	SDL_FreeSurface(surf);

	if (ret)
		fprintf(stderr, "%s:%d:canvas_export_png: Error while exporting\n", __FILE__, __LINE__);
	return ret;
}

void
canvas_add_layer(Canvas *c, unsigned char pos)
{
	int i;
	if (c == NULL)
		return;
	pos = (pos > c->layer_arr_cnt) ? c->layer_arr_cnt : pos;

	if (c->layer_arr_cnt == c->layer_arr_sz) {
		c->layer_arr_sz *= 2;
		c->layers = realloc(c->layers, c->layer_arr_sz * sizeof(* c->layers));
	}

	for (i = c->layer_arr_cnt; i > pos; i--) {
		c->layers[i] = c->layers[i-1];
	}

	c->layer_arr_cnt++;
	c->layers[pos] = layer_create(c->w, c->h);
}

void
canvas_refresh(Canvas *c)
{
	int i, j;
	for (i = 0; i < c->w; ++i)
		for (j = 0; j < c->h; ++j)
			canvas_point_redraw(c, i, j);
}

static void
canvas_set_proj_path(Canvas *c, const char *path)
{
	char *title;
	free(c->proj_path);
	c->proj_path = malloc((strlen(path) + 1) * sizeof(char));
	memcpy(c->proj_path, path, (strlen(path) + 1) * sizeof(char));

	
	/* TODO Set title format from config.h */
	title = malloc((strlen(path) + 8) * sizeof(char));
	strcpy(title, "cdraw: ");
	strcat(title, path);

	SDL_SetWindowTitle(win, title);
	free(title);
}

static Layer *
layer_create(uint w, uint h)
{
    Layer *res;
    int i;
	/* TODO: better error handling and maybe allocating together? */
    res = malloc(sizeof *res);

    res->pix = malloc(h * w * sizeof(* res->pix));
    if (res->pix == NULL) {
        fprintf(stderr, "Error creating layer\n");
        layer_destroy(res);
        return NULL;
    }

    for (i = 0; i < w*h; ++i)
        res->pix[i] = 0;

    return res;
}

static void
layer_destroy(Layer *lay)
{
    if (lay->pix != NULL)
        free(lay->pix);
    free(lay);
}

static uint
canvas_fill_bfs(Canvas *c, int x, int y, uint oldcol, uint newcol)
{
    int cnt;
    SDL_Point *pnt;

    if (c == NULL)
        return 0;

    pnt = c->temp_pix;
    cnt = 1;

    ((SDL_Point *)c->temp_pix)[0].x = x;
    ((SDL_Point *)c->temp_pix)[0].y = y;
    c->layers[c->cur_layer]->pix[COORD(x,y)] = newcol;

    for (; pnt != &((SDL_Point *)c->temp_pix)[cnt]; ++pnt) {
        x = pnt[0].x;
        y = pnt[0].y;
        if (x > 0 && c->layers[c->cur_layer]->pix[COORD(x - 1,y)] == oldcol) {
            c->layers[c->cur_layer]->pix[COORD(x-1, y)] = newcol;
            ((SDL_Point *)c->temp_pix)[cnt].x = x - 1;
            ((SDL_Point *)c->temp_pix)[cnt].y = y;
            ++cnt;
        }

        if (x < c->w - 1 && c->layers[c->cur_layer]->pix[COORD(x+1, y)] == oldcol) {
            c->layers[c->cur_layer]->pix[COORD(x+1, y)] = newcol;
            ((SDL_Point *)c->temp_pix)[cnt].x = x + 1;
            ((SDL_Point *)c->temp_pix)[cnt].y = y;
            ++cnt;
        }

        if (y > 0 && c->layers[c->cur_layer]->pix[COORD(x, y-1)] == oldcol) {
            c->layers[c->cur_layer]->pix[COORD(x, y-1)] = newcol;
            ((SDL_Point *)c->temp_pix)[cnt].x = x;
            ((SDL_Point *)c->temp_pix)[cnt].y = y - 1;
            ++cnt;
        }

        if (y < c->h - 1 && c->layers[c->cur_layer]->pix[COORD(x, y+1)] == oldcol) {
            c->layers[c->cur_layer]->pix[COORD(x, y+1)] = newcol;
            ((SDL_Point *)c->temp_pix)[cnt].x = x;
            ((SDL_Point *)c->temp_pix)[cnt].y = y + 1;
            ++cnt;
        }
    }

    return cnt;
}

static uint
canvas_blend_color(uint a, uint b)
{
	/* TODO: do actual blending */
	if ((b&0xff) == 0) return a;
	return b;
}

static void
canvas_point_redraw(Canvas *c, long int x, long int y)
{
	int i;
	c->pres_pix[COORD(x, y)] = 0;

	for (i = 0; i < c->layer_arr_cnt; i++)
		c->pres_pix[COORD(x, y)] = canvas_blend_color(c->pres_pix[COORD(x, y)], c->layers[i]->pix[COORD(x, y)]);
}