aboutsummaryrefslogtreecommitdiff
path: root/src/canvas.c
diff options
context:
space:
mode:
authorKrow Savcik <krow@savcik.xyz>2024-01-21 01:24:28 +0200
committerKrow Savcik <krow@savcik.xyz>2024-01-21 01:24:28 +0200
commit5cb6ff3c7186cbf48247e13c498a78f92b166d27 (patch)
treeb05da5294854f868727acf365aa78b42309bad93 /src/canvas.c
parentf15cde15550aaa8118b56709b67178a858f64012 (diff)
improved: faster rendering
Diffstat (limited to 'src/canvas.c')
-rw-r--r--src/canvas.c45
1 files changed, 34 insertions, 11 deletions
diff --git a/src/canvas.c b/src/canvas.c
index 8d48d0f..ea50d53 100644
--- a/src/canvas.c
+++ b/src/canvas.c
@@ -72,12 +72,24 @@ canvas_init(uint w, uint h, void *ren)
SDL_TEXTUREACCESS_TARGET,
w, h);
+ c->half_pres = 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;
}
+ if (c->half_pres == NULL) {
+ printf("SDL_CreateTexture failed: %s\n", SDL_GetError());
+ free(c);
+ return NULL;
+ }
+ SDL_SetTextureBlendMode(c->half_pres, SDL_BLENDMODE_BLEND);
+
c->pres = SDL_CreateTexture(
ren, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
@@ -101,6 +113,9 @@ canvas_init(uint w, uint h, void *ren)
SDL_RenderFillRect(ren, &dest);
}
}
+ SDL_SetRenderTarget(ren, c->half_pres);
+ SDL_SetRenderDrawColor(ren, 0, 0, 0, 0);
+ SDL_RenderClear(ren);
SDL_SetRenderTarget(ren, NULL);
return c;
@@ -166,6 +181,7 @@ canvas_destroy(Canvas *c)
if (c == NULL) return;
SDL_DestroyTexture(c->pres);
SDL_DestroyTexture(c->back);
+ SDL_DestroyTexture(c->half_pres);
for (i = 0; i < c->layer_arr_cnt; ++i)
layer_destroy(c->layers[i]);
free(c->layers);
@@ -182,14 +198,7 @@ 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);
- }
- }
+ SDL_RenderCopy(ren,c->half_pres,NULL,NULL);
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,
@@ -249,10 +258,12 @@ canvas_point_draw(Canvas *c, long int x, long int y)
if (newcol == oldcol)
return;
- SDL_SetRenderTarget(ren, c->pres);
+ SDL_SetRenderTarget(ren, c->half_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);
+ SDL_SetRenderDrawColor(ren, INTTOCOLA(c->pres_pix[COORD(x,y)]));
+ SDL_RenderDrawPoint(ren, 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,
@@ -260,10 +271,15 @@ canvas_point_draw(Canvas *c, long int x, long int y)
((Palette *)def_palette)->clist[c->cur_col].b,
255);
canvas_point_redraw(c, x, y);
+ SDL_SetRenderDrawColor(ren, INTTOCOLA(c->pres_pix[COORD(x,y)]));
+ SDL_RenderDrawPoint(ren, 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)
+ 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_SetRenderDrawColor(ren, INTTOCOLA(c->pres_pix[COORD(((SDL_Point *)c->temp_pix)[i].x,((SDL_Point *)c->temp_pix)[i].y)]));
+ SDL_RenderDrawPoint(ren, ((SDL_Point *)c->temp_pix)[i].x, ((SDL_Point *)c->temp_pix)[i].y);
+ }
}
SDL_SetRenderTarget(ren, NULL);
}
@@ -411,10 +427,16 @@ canvas_add_layer(Canvas *c, unsigned char pos)
void
canvas_refresh(Canvas *c)
{
+ /* Fully redraws the canvas */
int i, j;
+ SDL_SetRenderTarget(ren, c->half_pres);
for (i = 0; i < c->w; ++i)
- for (j = 0; j < c->h; ++j)
+ for (j = 0; j < c->h; ++j) {
canvas_point_redraw(c, i, j);
+ SDL_SetRenderDrawColor(ren, INTTOCOLA(c->pres_pix[COORD(i,j)]));
+ SDL_RenderDrawPoint(ren, i, j);
+ }
+ SDL_SetRenderTarget(ren, NULL);
}
static void
@@ -526,6 +548,7 @@ canvas_blend_color(uint a, uint b)
static void
canvas_point_redraw(Canvas *c, long int x, long int y)
{
+ /* expects for the render target to be c->half_pres */
int i;
c->pres_pix[COORD(x, y)] = 0;