From d551cdbb70aeb0cba938723993e5d35b06e7c832 Mon Sep 17 00:00:00 2001 From: kichikuou Date: Sun, 16 Feb 2025 11:14:49 +0900 Subject: [PATCH] Refactor text rendering functions in modules/ --- modules/SACT/sact.h | 2 +- modules/SACT/sactcg.c | 18 ++++++++------- modules/SACT/sactlog.c | 12 +++++----- modules/SACT/sprite_sel.c | 8 ++++--- modules/lib/drawtext.c | 33 ++++++++++++--------------- modules/lib/drawtext.h | 4 +++- modules/lib/graph_expandcolor_blend.c | 13 +++++++---- modules/lib/ngraph.h | 2 +- modules/lib/surface.c | 3 --- src/ags.c | 20 +++------------- src/ags.h | 2 +- 11 files changed, 52 insertions(+), 65 deletions(-) diff --git a/modules/SACT/sact.h b/modules/SACT/sact.h index aec8852..df796d2 100644 --- a/modules/SACT/sact.h +++ b/modules/SACT/sact.h @@ -234,7 +234,7 @@ struct _sact { int align; // 行そろえ void (* cbmove)(agsevent_t *); void (* cbrelease)(agsevent_t *); - surface_t *charcanvas; + struct SDL_Surface *charcanvas; } sel; // event listener diff --git a/modules/SACT/sactcg.c b/modules/SACT/sactcg.c index a948969..aa6dc4f 100644 --- a/modules/SACT/sactcg.c +++ b/modules/SACT/sactcg.c @@ -158,11 +158,12 @@ void scg_create_text(int wNumCG, int wSize, int wR, int wG, int wB, int wText) { if (svar_length(wText) == 0) return; ags_setFont(FONT_GOTHIC, wSize); - agsurface_t *glyph = ags_drawStringToSurface(svar_get(wText)); + SDL_Surface *glyph = ags_drawStringToSurface(svar_get(wText)); - surface_t *sf = sf_create_surface(glyph->width, wSize, nact->ags.dib->depth); - gr_fill(sf, 0, 0, glyph->width, wSize, wR, wG, wB); - gr_draw_amap(sf, 0, 0, glyph->pixel, glyph->width, wSize, glyph->bytes_per_line); + surface_t *sf = sf_create_surface(glyph->w, wSize, nact->ags.dib->depth); + gr_fill(sf, 0, 0, glyph->w, wSize, wR, wG, wB); + gr_draw_amap(sf, 0, 0, glyph->pixels, glyph->w, wSize, glyph->pitch); + SDL_FreeSurface(glyph); scg_new(CG_SET, wNumCG, sf); } @@ -182,11 +183,12 @@ void scg_create_textnum(int wNumCG, int wSize, int wR, int wG, int wB, int wFigs sprintf(s, ss, wValue); ags_setFont(FONT_GOTHIC, wSize); - agsurface_t *glyph = ags_drawStringToSurface(s); + SDL_Surface *glyph = ags_drawStringToSurface(s); - surface_t *sf = sf_create_surface(glyph->width, wSize, nact->ags.dib->depth); - gr_fill(sf, 0, 0, glyph->width, wSize, wR, wG, wB); - gr_draw_amap(sf, 0, 0, glyph->pixel, glyph->width, wSize, glyph->bytes_per_line); + surface_t *sf = sf_create_surface(glyph->w, wSize, nact->ags.dib->depth); + gr_fill(sf, 0, 0, glyph->w, wSize, wR, wG, wB); + gr_draw_amap(sf, 0, 0, glyph->pixels, glyph->w, wSize, glyph->pitch); + SDL_FreeSurface(glyph); scg_new(CG_SET, wNumCG, sf); } diff --git a/modules/SACT/sactlog.c b/modules/SACT/sactlog.c index fc0cb45..0b0b318 100644 --- a/modules/SACT/sactlog.c +++ b/modules/SACT/sactlog.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "portab.h" #include "system.h" @@ -61,7 +62,7 @@ static const char *logmsg_utf8[LOGMSG_LINES] = { #define LOGLINENUM (sf0->height / FONTSIZE) static int curline; static surface_t *back; -static surface_t *chr; +static SDL_Surface *chr; static void draw_log() { int i, y = 0, len; @@ -69,8 +70,7 @@ static void draw_log() { char pinfo[256]; List *node; - // canvas clear - memset(chr->pixel, 0, chr->bytes_per_line * chr->height); + SDL_FillRect(chr, NULL, 0); // ページ位置情報 len = snprintf(pinfo, sizeof(pinfo) -1, "%d/%d", curline, list_length(sact.log)); @@ -85,7 +85,7 @@ static void draw_log() { char *str = (char *)(node->data); if (0 == strcmp(str, "\n")) { - gr_fill(chr, 0, y + FONTSIZE/2, sf0->width, 3, 128, 0, 0); + SDL_FillRect(chr, &(SDL_Rect){0, y + FONTSIZE/2, sf0->width, 3}, 128); } else { if (cur < 6) { dt_setfont(FONT_MINCHO, FONTSIZE); @@ -122,7 +122,7 @@ bool sblog_start(void) { sact.log = list_append(sact.log, logmsg[i]); back = sf_dup(sf0); - chr = sf_create_surface(sf0->width, sf0->height, 8); + chr = SDL_CreateRGBSurfaceWithFormat(0, sf0->width, sf0->height, 8, SDL_PIXELFORMAT_INDEX8); curline = 6; draw_log(); return true; @@ -136,7 +136,7 @@ void sblog_end(void) { ags_updateFull(); sf_free(back); - sf_free(chr); + if (chr) SDL_FreeSurface(chr); // 説明文章を削除 for (i = 0; i < LOGMSG_LINES; i++) { diff --git a/modules/SACT/sprite_sel.c b/modules/SACT/sprite_sel.c index 435803b..408d1bd 100644 --- a/modules/SACT/sprite_sel.c +++ b/modules/SACT/sprite_sel.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "portab.h" #include "nact.h" @@ -176,7 +177,7 @@ static void setup_selwindow() { selcanvas = sf_dup(sp->cg1->sf); // 選択肢文字用 canvas - sact.sel.charcanvas = sf_create_pixel(selcanvas->width, selcanvas->height, 8); + sact.sel.charcanvas = SDL_CreateRGBSurfaceWithFormat(0, selcanvas->width, selcanvas->height, 8, SDL_PIXELFORMAT_INDEX8); dt_setfont(sact.sel.font_type, sact.sel.font_size); @@ -224,7 +225,8 @@ static void remove_selwindow() { // 作業用 surface の削除 sf_free(selcanvas); selcanvas = NULL; - sf_free(sact.sel.charcanvas); + if (sact.sel.charcanvas) + SDL_FreeSurface(sact.sel.charcanvas); sact.sel.charcanvas = NULL; } @@ -266,7 +268,7 @@ void ssel_reset(void) { selcanvas = NULL; } if (sact.sel.charcanvas) { - sf_free(sact.sel.charcanvas); + SDL_FreeSurface(sact.sel.charcanvas); sact.sel.charcanvas = NULL; } } diff --git a/modules/lib/drawtext.c b/modules/lib/drawtext.c index 8c038ac..2ec5414 100644 --- a/modules/lib/drawtext.c +++ b/modules/lib/drawtext.c @@ -64,21 +64,15 @@ void dt_setfont(FontType type, int size) { * @param buf: 描画文字列 (SJIS) * @return: 実際に描画した幅 */ -int dt_drawtext(surface_t *sf, int x, int y, char *buf) { - int sx, sy, sw, sh; - +int dt_drawtext(SDL_Surface *sf, int x, int y, char *buf) { ags_setFont(ftype, fsize); - agsurface_t *glyph = ags_drawStringToSurface(buf); + SDL_Surface *glyph = ags_drawStringToSurface(buf); if (glyph == NULL) return 0; - - sx = x; sy = y; - sw = glyph->width; - sh = glyph->height; - if (!gr_clip_xywh(sf, &sx, &sy, &sw, &sh)) return 0; - - gr_copy(sf, sx, sy, glyph, 0, 0, sw, sh); - - return sw; + + SDL_Rect r = {x, y, glyph->w, glyph->h}; + SDL_BlitSurface(glyph, NULL, sf, &r); + SDL_FreeSurface(glyph); + return glyph->w; } /** @@ -98,19 +92,20 @@ int dt_drawtext_col(surface_t *sf, int x, int y, char *buf, int r, int g, int b) int sx, sy, sw, sh; ags_setFont(ftype, fsize); - agsurface_t *glyph = ags_drawStringToSurface(buf); + SDL_Surface *glyph = ags_drawStringToSurface(buf); if (glyph == NULL) return 0; sx = x; sy = y; - sw = glyph->width; - sh = glyph->height; + sw = glyph->w; + sh = glyph->h; if (!gr_clip_xywh(sf, &sx, &sy, &sw, &sh)) return 0; // alpha map に文字そのものを描く - gr_draw_amap(sf, sx, sy, glyph->pixel, sw, sh, glyph->bytes_per_line); - + gr_draw_amap(sf, sx, sy, glyph->pixels, sw, sh, glyph->pitch); + // pixel map には色情報を矩形で描く gr_fill(sf, sx, sy, sw, sh, r, g, b); - + + SDL_FreeSurface(glyph); return sw; } diff --git a/modules/lib/drawtext.h b/modules/lib/drawtext.h index b01f362..75bf731 100644 --- a/modules/lib/drawtext.h +++ b/modules/lib/drawtext.h @@ -26,8 +26,10 @@ #include "graphics.h" +struct SDL_Surface; + void dt_setfont(FontType type, int size); -int dt_drawtext(surface_t *sf, int x, int y, char *buf); +int dt_drawtext(struct SDL_Surface *sf, int x, int y, char *buf); int dt_drawtext_col(surface_t *sf, int x, int y, char *buf, int r, int g, int b); #endif /* __DRAWTEXT_H__ */ diff --git a/modules/lib/graph_expandcolor_blend.c b/modules/lib/graph_expandcolor_blend.c index 5e8c860..c5e4e5d 100644 --- a/modules/lib/graph_expandcolor_blend.c +++ b/modules/lib/graph_expandcolor_blend.c @@ -1,21 +1,24 @@ // color expansion and blending // 8bppのモノクロをcolでブレンド +#include #include "portab.h" #include "surface.h" #include "ngraph.h" #include "ags.h" -void gr_expandcolor_blend(surface_t *dst, int dx, int dy, surface_t *src, int sx, int sy, int sw, int sh, int r, int g, int b) { +void gr_expandcolor_blend(surface_t *dst, int dx, int dy, SDL_Surface *src, int sx, int sy, int sw, int sh, int r, int g, int b) { uint8_t *sp, *dp; int x, y; int col; - if (!gr_clip(src, &sx, &sy, &sw, &sh, dst, &dx, &dy)) { + MyRectangle src_window = { 0, 0, src->w, src->h }; + MyRectangle dst_window = { 0, 0, dst->width, dst->height }; + if (!ags_clipCopyRect(&src_window, &dst_window, &sx, &sy, &dx, &dy, &sw, &sh)) { return; } - sp = GETOFFSET_PIXEL(src, sx, sy); + sp = PIXEL_AT(src, sx, sy); dp = GETOFFSET_PIXEL(dst, dx, dy); switch(dst->depth) { @@ -25,7 +28,7 @@ void gr_expandcolor_blend(surface_t *dst, int dx, int dy, surface_t *src, int sx uint16_t *yd; uint8_t *ys; for (y = 0; y < sh; y++) { - ys = (uint8_t *)(sp + y * src->bytes_per_line); + ys = (uint8_t *)(sp + y * src->pitch); yd = (uint16_t *)(dp + y * dst->bytes_per_line); for (x = 0; x < sw; x++) { if (*ys) { @@ -42,7 +45,7 @@ void gr_expandcolor_blend(surface_t *dst, int dx, int dy, surface_t *src, int sx uint8_t *ys; col = PIX24(r, g, b); for (y = 0; y < sh; y++) { - ys = (uint8_t *)(sp + y * src->bytes_per_line); + ys = (uint8_t *)(sp + y * src->pitch); yd = (uint32_t *)(dp + y * dst->bytes_per_line); for (x = 0; x < sw; x++) { if (*ys) { diff --git a/modules/lib/ngraph.h b/modules/lib/ngraph.h index 9190124..a81cbaf 100644 --- a/modules/lib/ngraph.h +++ b/modules/lib/ngraph.h @@ -17,7 +17,7 @@ bool gr_clip_xywh(surface_t *ss, int *sx, int *sy, int *sw, int *sh); /* in graph_expandcolor_blend.c */ // 8bppのモノクロをcolでブレンド -void gr_expandcolor_blend(surface_t *dst, int dx, int dy, surface_t *src, int sx, int sy, int sw, int sh, int r, int g, int b); +void gr_expandcolor_blend(surface_t *dst, int dx, int dy, struct SDL_Surface *src, int sx, int sy, int sw, int sh, int r, int g, int b); /* in graph_fillrect.c */ void gr_fill(surface_t *dst, int dx, int dy, int dw, int dh, int r, int g, int b); diff --git a/modules/lib/surface.c b/modules/lib/surface.c index 16363df..35266e2 100644 --- a/modules/lib/surface.c +++ b/modules/lib/surface.c @@ -22,9 +22,6 @@ static surface_t *create(int width, int height, int depth, bool has_pixel, bool if (has_pixel) { uint32_t format = 0; switch (s->depth) { - case 8: - format = SDL_PIXELFORMAT_INDEX8; - break; case 16: format = SDL_PIXELFORMAT_RGB565; break; diff --git a/src/ags.c b/src/ags.c index 92a8ef7..ff35911 100644 --- a/src/ags.c +++ b/src/ags.c @@ -448,25 +448,11 @@ int ags_drawString(int x, int y, const char *src, int col, MyRectangle *rect_out return r.w; } -agsurface_t *ags_drawStringToSurface(const char *str) { - static SDL_Surface *fs; - static agsurface_t result; - if (fs) { - SDL_FreeSurface(fs); - fs = NULL; - } - +SDL_Surface *ags_drawStringToSurface(const char *str) { char *utf8 = toUTF8(str); - fs = font_get_glyph(utf8); + SDL_Surface *sf = font_get_glyph(utf8); free(utf8); - - result.depth = fs->format->BitsPerPixel; - result.bytes_per_pixel = fs->format->BytesPerPixel; - result.bytes_per_line = fs->pitch; - result.pixel = fs->pixels; - result.width = fs->w; - result.height = fs->h; - return &result; + return sf; } void ags_drawCg(cgdata *cg, int x, int y, int brightness, int sprite_color, bool alpha_blend) { diff --git a/src/ags.h b/src/ags.h index 4719bde..1b0d299 100644 --- a/src/ags.h +++ b/src/ags.h @@ -221,7 +221,7 @@ extern void ags_setFont(FontType type, int size); extern void ags_setFontWithWeight(FontType type, int size, int weight); extern void ags_setTextDecorationType(TextDecorationType type); extern void ags_setTextDecorationColor(int color); -extern agsurface_t *ags_drawStringToSurface(const char *str); +extern struct SDL_Surface *ags_drawStringToSurface(const char *str); /* カーソル関係 */ extern void ags_setCursorType(int type);