From becda2c61da7f2223733fc829e772c56f41b3389 Mon Sep 17 00:00:00 2001 From: Nunuhara Cabbage Date: Tue, 26 Nov 2019 19:22:13 -0800 Subject: [PATCH] Implement SACT2.CG_GetMetrics --- cg.c | 17 ++++++++++++++++ cg.h | 11 +++++++++++ hll/SACT2.c | 2 +- qnt.c | 52 ++++++++++++++++++++++++++----------------------- qnt.h | 1 + sact.h | 1 + sact_graphics.c | 15 ++++++++++++++ 7 files changed, 74 insertions(+), 25 deletions(-) diff --git a/cg.c b/cg.c index 4a7b8a5..dfed495 100644 --- a/cg.c +++ b/cg.c @@ -43,6 +43,23 @@ static enum cg_type check_cgformat(uint8_t *data) return ALCG_UNKNOWN; } +bool cg_get_metrics(int no, struct cg_metrics *dst) +{ + struct archive_data *dfile; + + if (!(dfile = ald_get(ald[ALDFILE_CG], no))) + return false; + + switch (check_cgformat(dfile->data)) { + case ALCG_QNT: + qnt_get_metrics(dfile->data, dst); + break; + default: + return false; + } + return true; +} + void _cg_free(struct cg *cg) { if (!cg) diff --git a/cg.h b/cg.h index 0bb8119..208ec43 100644 --- a/cg.h +++ b/cg.h @@ -43,7 +43,18 @@ struct cg { SDL_Surface *s; }; +struct cg_metrics { + int w; + int h; + int bpp; + bool has_pixel; + bool has_alpha; + int pixel_pitch; + int alpha_pitch; +}; + bool cg_exists(int no); +bool cg_get_metrics(int no, struct cg_metrics *dst); bool cg_load(struct cg *cg, int no); struct cg *cg_init(void); void cg_reinit(struct cg *cg); diff --git a/hll/SACT2.c b/hll/SACT2.c index 835657b..564175f 100644 --- a/hll/SACT2.c +++ b/hll/SACT2.c @@ -384,7 +384,7 @@ hll_defun_inline(Timer_Get, vm_time()); // int CG_IsExist(int nCG) hll_defun_inline(CG_IsExist, cg_exists(a[0].i - 1)); // int CG_GetMetrics(int nCG, ref struct cm) -hll_unimplemented(SACT2, CG_GetMetrics); +hll_defun_inline(CG_GetMetrics, sact_CG_GetMetrics(a[0].i - 1, heap[a[1].i].page->values)); // int CSV_Load(string pIStringFileName) hll_unimplemented(SACT2, CSV_Load); // int CSV_Save(void) diff --git a/qnt.c b/qnt.c index e0ee032..47bb463 100644 --- a/qnt.c +++ b/qnt.c @@ -46,14 +46,6 @@ */ #define ZLIBBUF_MARGIN 5*1024 -/* - static methods -*/ -static struct qnt_header *extract_header(uint8_t *b); -static void extract_pixel(struct qnt_header *qnt, uint8_t *pic, uint8_t *b); -static void extract_alpha(struct qnt_header *qnt, uint8_t *pic, uint8_t *b); - - /* * Get information from header * @@ -61,9 +53,8 @@ static void extract_alpha(struct qnt_header *qnt, uint8_t *pic, uint8_t *b); * * return: acquired qnt information object */ -static struct qnt_header *extract_header(uint8_t *b) +static void extract_header(uint8_t *b, struct qnt_header *qnt) { - struct qnt_header *qnt = malloc(sizeof(struct qnt_header)); int rsv0; rsv0 = LittleEndian_getDW(b, 4); @@ -90,8 +81,6 @@ static struct qnt_header *extract_header(uint8_t *b) } if (qnt->bpp != 24) ERROR("Unsupported bits-per-pixel: %d", qnt->bpp); - - return qnt; } /* @@ -239,6 +228,20 @@ bool qnt_checkfmt(uint8_t *data) return true; } +bool qnt_get_metrics(uint8_t *data, struct cg_metrics *dst) +{ + struct qnt_header qnt; + extract_header(data, &qnt); + dst->w = qnt.width; + dst->h = qnt.height; + dst->bpp = qnt.bpp; + dst->has_pixel = qnt.pixel_size > 0; + dst->has_alpha = qnt.alpha_size > 0; + dst->pixel_pitch = qnt.width * (qnt.bpp / 8); + dst->alpha_pitch = 1; + return true; +} + /* * Extract qnt header and pixel * @@ -248,19 +251,20 @@ bool qnt_checkfmt(uint8_t *data) */ SDL_Surface *qnt_extract(uint8_t *data) { - struct qnt_header *qnt = extract_header(data); - uint8_t *pixels = malloc(sizeof(uint8_t) * ((qnt->width+10) * (qnt->height+10) * 3)); - extract_pixel(qnt, pixels, data + qnt->hdr_size); + struct qnt_header qnt; + extract_header(data, &qnt); + uint8_t *pixels = malloc(sizeof(uint8_t) * ((qnt.width+10) * (qnt.height+10) * 3)); + extract_pixel(&qnt, pixels, data + qnt.hdr_size); - if (!qnt->alpha_size) - return SDL_CreateRGBSurfaceWithFormatFrom(pixels, qnt->width, qnt->height, 24, - qnt->width * 3, SDL_PIXELFORMAT_RGB24); + if (!qnt.alpha_size) + return SDL_CreateRGBSurfaceWithFormatFrom(pixels, qnt.width, qnt.height, 24, + qnt.width * 3, SDL_PIXELFORMAT_RGB24); // combine color/alpha data - uint8_t *alpha = malloc(sizeof(uint8_t) * ((qnt->width+10) * (qnt->height+10))); - uint8_t *tmp = malloc((qnt->width+10) * (qnt->height+10) * 4); - extract_alpha(qnt, alpha, data + qnt->hdr_size + qnt->pixel_size); - for (int src_i = 0, dst_i = 0, p = 0; p < qnt->width * qnt->height; p++) { + uint8_t *alpha = malloc(sizeof(uint8_t) * ((qnt.width+10) * (qnt.height+10))); + uint8_t *tmp = malloc((qnt.width+10) * (qnt.height+10) * 4); + extract_alpha(&qnt, alpha, data + qnt.hdr_size + qnt.pixel_size); + for (int src_i = 0, dst_i = 0, p = 0; p < qnt.width * qnt.height; p++) { tmp[dst_i++] = alpha[p]; tmp[dst_i++] = pixels[src_i++]; tmp[dst_i++] = pixels[src_i++]; @@ -270,6 +274,6 @@ SDL_Surface *qnt_extract(uint8_t *data) free(pixels); pixels = tmp; - return SDL_CreateRGBSurfaceWithFormatFrom(pixels, qnt->width, qnt->height, 32, - qnt->width * 4, SDL_PIXELFORMAT_ARGB32); + return SDL_CreateRGBSurfaceWithFormatFrom(pixels, qnt.width, qnt.height, 32, + qnt.width * 4, SDL_PIXELFORMAT_ARGB32); } diff --git a/qnt.h b/qnt.h index 1065f55..54cdb68 100644 --- a/qnt.h +++ b/qnt.h @@ -44,6 +44,7 @@ struct qnt_header { }; bool qnt_checkfmt(uint8_t *data); +bool qnt_get_metrics(uint8_t *data, struct cg_metrics *dst); SDL_Surface *qnt_extract(uint8_t *data); #endif /* __QNT_H__ */ diff --git a/sact.h b/sact.h index 70b59f6..5fcee35 100644 --- a/sact.h +++ b/sact.h @@ -59,6 +59,7 @@ int sact_SP_ExistsAlpha(int sp_no); int sact_SP_TextDraw(int sp_no, struct string *text, union vm_value *tm); int sact_SP_TextClear(int sp_no); int sact_SP_TextCopy(int dno, int sno); +int sact_CG_GetMetrics(int cg_no, union vm_value *cgm); void DrawGraph_Copy(int dno, int dx, int dy, int sno, int sx, int sy, int w, int h); void DrawGraph_CopyBright(int dno, int dx, int dy, int sno, int sx, int sy, int w, int h, int rate); diff --git a/sact_graphics.c b/sact_graphics.c index 76ebd79..0a018f6 100644 --- a/sact_graphics.c +++ b/sact_graphics.c @@ -283,6 +283,21 @@ int sact_SP_TextCopy(int dno, int sno) return 1; } +int sact_CG_GetMetrics(int cg_no, union vm_value *cgm) +{ + struct cg_metrics metrics; + if (!cg_get_metrics(cg_no, &metrics)) + return 0; + cgm[0].i = metrics.w; + cgm[1].i = metrics.h; + cgm[2].i = metrics.bpp; + cgm[3].i = metrics.has_pixel; + cgm[4].i = metrics.has_alpha; + cgm[5].i = metrics.pixel_pitch; + cgm[6].i = metrics.alpha_pitch; + return 1; +} + // // --- DrawGraph.dll --- //