From 5cb6ff3c7186cbf48247e13c498a78f92b166d27 Mon Sep 17 00:00:00 2001 From: Krow Savcik Date: Sun, 21 Jan 2024 01:24:28 +0200 Subject: improved: faster rendering --- src/canvas.c | 45 ++++++++++++++++++++++++++++++++++----------- src/canvas.h | 2 +- src/cdraw.c | 2 +- 3 files changed, 36 insertions(+), 13 deletions(-) (limited to 'src') 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; diff --git a/src/canvas.h b/src/canvas.h index 2bdb775..104f130 100644 --- a/src/canvas.h +++ b/src/canvas.h @@ -9,7 +9,7 @@ struct Canvas { char *proj_path; struct Layer **layers; int x, y; - void *back, *pres; + void *back, *pres, *half_pres; void *temp_pix; uint *pres_pix; }; diff --git a/src/cdraw.c b/src/cdraw.c index 5b67c5e..c0c06c5 100644 --- a/src/cdraw.c +++ b/src/cdraw.c @@ -83,7 +83,7 @@ main_window_init(const char *s) } SDL_SetRenderDrawColor(ren, 18, 18, 18, 255); - SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_BLEND); + SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_NONE); SDL_RenderPresent(ren); def_palette = palette_open_gpl(def_palette_path); -- cgit v1.2.3