Refactor text rendering functions in modules/

This commit is contained in:
kichikuou
2025-02-16 13:13:16 +09:00
parent e711d5786e
commit d551cdbb70
11 changed files with 52 additions and 65 deletions
+1 -1
View File
@@ -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
+10 -8
View File
@@ -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);
}
+6 -6
View File
@@ -26,6 +26,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#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++) {
+5 -3
View File
@@ -26,6 +26,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#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;
}
}
+14 -19
View File
@@ -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;
}
+3 -1
View File
@@ -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__ */
+8 -5
View File
@@ -1,21 +1,24 @@
// color expansion and blending
// 8bppのモノクロをcolでブレンド
#include <SDL.h>
#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) {
+1 -1
View File
@@ -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);
-3
View File
@@ -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;
+3 -17
View File
@@ -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) {
+1 -1
View File
@@ -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);