From 99caef2c816d9d75c5d454640554becf75845ce4 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sun, 15 Mar 2020 12:11:57 +0100 Subject: [PATCH] separate mono and gray rendering to VRAM on fx-9860G --- README.md | 2 ++ libimg.h | 18 +++++++++++++++++- src/render.c | 26 ++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0976f00..2f81187 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,8 @@ img_render_vram(sprite, x, y); `img_render_vram()`, as every other libimg function, accounts for transparent pixels. Only opaque pixels will be copied to VRAM. This makes it very easy to blend images together by just rendering in sequence. +As a quick note, if you have an image with gray pixels on fx-9860G, you need to use `img_render_vram_gray()` instead (with the same parameters). When using `img_render_vram()`, gray pixels will be approximated as black and white. + ## Creating and destroying new images Because our converted sprite is read-only, we can't do much more for now. To start working on transforms and cool animations, we need to create new images that can be written to. There are two ways to do this; the first is to create an empty image of a fixed size, and the second is to duplicate an existing image. diff --git a/libimg.h b/libimg.h index ebc912c..59ca852 100644 --- a/libimg.h +++ b/libimg.h @@ -175,13 +175,29 @@ void img_render(img_t src, img_t dst); VRAM supports both monochrome and gray pixels. This function clips the rendered image onto the VRAM. - On fx-CG 50, this functions is exactly equivalent to: + This function only renders to black-and-white VRAM. Dark gray and light gray + pixels are replaced by black and while, respectively. To render imges under + the gray engine, use img_render_vram_gray(). + + On fx-CG 50, this function is exactly equivalent to: img_render(src, img_at(img_vram(), x, y)). @src Source image @x @y Render destination on VRAM */ void img_render_vram(img_t src, int x, int y); +#ifdef FX9860G +/* img_render_vram_gray(): Render a gray image to the VRAM + + On fx-9860G, mono and gray rendering code must be separated. This function + is the direct equivalent of img_render_vram(), but it renders to the gray + VRAM and is suitable for gray images. + + @src Source image + @x @y Render destination on gray VRAM */ +void img_render_vram_gray(img_t src, int x, int y); +#endif + //--- // Geometric transforms diff --git a/src/render.c b/src/render.c index 41802e7..f5e76ae 100644 --- a/src/render.c +++ b/src/render.c @@ -3,6 +3,10 @@ #include +#ifdef FX9860G +#include +#endif + void img_fill(img_t img, img_pixel_t color) { img_pixel_t *px = img.pixels; @@ -54,8 +58,26 @@ void img_render_vram(img_t img, int x, int y) for(int dy = 0; dy < img.height; dy++, px += img.stride) for(int dx = 0; dx < img.width; dx++) { - /* TODO: Render using gray? */ - dpixel(x+dx, y+dy, px[dx]); + img_pixel_t c = px[dx]; + + /* Provide a mildly useful approximation of gray rendering. + Seeing the black-and-white approximation might make it more + explicit to the user that mono rendering has been used, + rather than rendering nothing */ + if(c == C_DARK) c = C_BLACK; + if(c == C_LIGHT) c = C_WHITE; + dpixel(x+dx, y+dy, c); + } +} + +void img_render_vram_gray(img_t img, int x, int y) +{ + img_pixel_t *px = img.pixels; + + for(int dy = 0; dy < img.height; dy++, px += img.stride) + for(int dx = 0; dx < img.width; dx++) + { + gpixel(x+dx, y+dy, px[dx]); } } #endif /* FX9860G */