From f282c69266e7044cef4ebfe67dd867c212e82c80 Mon Sep 17 00:00:00 2001 From: Nunuhara Cabbage Date: Thu, 22 Apr 2021 19:05:44 -0700 Subject: [PATCH] Implement DrawGraph.CopyColorReverse Just a copy operation that inverts colors. Used in Rance VI Sonogo. --- include/gfx/gfx.h | 1 + shaders/copy_color_reverse.f.glsl | 27 +++++++++++++++++++++++++++ src/draw.c | 14 ++++++++++++++ src/hll/DrawGraph.c | 8 ++++++-- 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 shaders/copy_color_reverse.f.glsl diff --git a/include/gfx/gfx.h b/include/gfx/gfx.h index 6af21ac..6a02801 100644 --- a/include/gfx/gfx.h +++ b/include/gfx/gfx.h @@ -99,6 +99,7 @@ void gfx_copy(struct texture *dst, int dx, int dy, struct texture *src, int sx, void gfx_copy_bright(struct texture *dst, int dx, int dy, struct texture *src, int sx, int sy, int w, int h, int rate); void gfx_copy_amap(struct texture *dst, int dx, int dy, struct texture *src, int sx, int sy, int w, int h); void gfx_copy_sprite(struct texture *dst, int dx, int dy, struct texture *src, int sx, int sy, int w, int h, SDL_Color color); +void gfx_copy_color_reverse(struct texture *dst, int dx, int dy, struct texture *src, int sx, int sy, int w, int h); void gfx_copy_use_amap_under(struct texture *dst, int dx, int dy, struct texture *src, int sx, int sy, int w, int h, int threshold); void gfx_copy_use_amap_border(struct texture *dst, int dx, int dy, struct texture *src, int sx, int sy, int w, int h, int threshold); void gfx_copy_amap_max(struct texture *dst, int dx, int dy, struct texture *src, int sx, int sy, int w, int h); diff --git a/shaders/copy_color_reverse.f.glsl b/shaders/copy_color_reverse.f.glsl new file mode 100644 index 0000000..4160336 --- /dev/null +++ b/shaders/copy_color_reverse.f.glsl @@ -0,0 +1,27 @@ +/* Copyright (C) 2019 Nunuhara Cabbage + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#version 140 + +uniform sampler2D tex; + +in vec2 tex_coord; +out vec4 frag_color; + +void main() { + vec4 texel = texture(tex, tex_coord); + frag_color = vec4(1.0 - texel.r, 1.0 - texel.g, 1.0 - texel.b, 1); +} diff --git a/src/draw.c b/src/draw.c index c1ba280..4ce3204 100644 --- a/src/draw.c +++ b/src/draw.c @@ -55,6 +55,7 @@ struct copy_data { STRETCH_DATA(_dx, _dy, _w, _h, _sx, _sy, _w, _h) static struct copy_shader copy_shader; +static struct copy_shader copy_color_reverse_shader; static struct copy_shader copy_key_shader; static struct copy_shader copy_use_amap_under_shader; static struct copy_shader copy_use_amap_border_shader; @@ -90,6 +91,9 @@ void gfx_draw_init(void) // generic copy shader; discards fragments outside of a given rectangle load_copy_shader(©_shader, "shaders/render.v.glsl", "shaders/copy.f.glsl"); + // copy shader which inverts colors + load_copy_shader(©_color_reverse_shader, "shaders/render.v.glsl", "shaders/copy_color_reverse.f.glsl"); + // copy shader which also discards fragments matching a color key load_copy_shader(©_key_shader, "shaders/render.v.glsl", "shaders/copy_key.f.glsl"); @@ -206,6 +210,16 @@ void gfx_copy_sprite(Texture *dst, int dx, int dy, Texture *src, int sx, int sy, restore_blend_mode(); } +void gfx_copy_color_reverse(struct texture *dst, int dx, int dy, struct texture *src, int sx, int sy, int w, int h) +{ + glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ZERO, GL_ONE); + + struct copy_data data = COPY_DATA(dx, dy, sx, sy, w, h); + run_copy_shader(©_color_reverse_shader.s, dst, src, &data); + + restore_blend_mode(); +} + void gfx_copy_use_amap_under(Texture *dst, int dx, int dy, Texture *src, int sx, int sy, int w, int h, int threshold) { glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ZERO, GL_ONE); diff --git a/src/hll/DrawGraph.c b/src/hll/DrawGraph.c index f44bd44..2d62d16 100644 --- a/src/hll/DrawGraph.c +++ b/src/hll/DrawGraph.c @@ -42,6 +42,11 @@ static void DrawGraph_CopySprite(int dst, int dx, int dy, int src, int sx, int s gfx_copy_sprite(DTEX(dst), dx, dy, STEX(src), sx, sy, w, h, COLOR(r, g, b, 0)); } +static void DrawGraph_CopyColorReverse(int dst, int dx, int dy, int src, int sx, int sy, int w, int h) +{ + gfx_copy_color_reverse(DTEX(dst), dx, dy, STEX(src), sx, sy, w, h); +} + static void DrawGraph_CopyUseAMapUnder(int dst, int dx, int dy, int src, int sx, int sy, int w, int h, int alpha) { gfx_copy_use_amap_under(DTEX(dst), dx, dy, STEX(src), sx, sy, w, h, alpha); @@ -166,7 +171,6 @@ static void DrawGraph_SetFontName(struct string *text) gfx_set_font_name(text->text); } -//void DrawGraph_CopyColorReverse(int dst, int dx, int dy, int src, int sx, int sy, int w, int h); //void DrawGraph_BlendSrcBright(int dst, int dx, int dy, int src, int sx, int sy, int w, int h, int alpha, int rate); //void DrawGraph_BlendAddSatur(int dst, int dx, int dy, int src, int sx, int sy, int w, int h); //void DrawGraph_BlendAMapSrcOnly(int dst, int dx, int dy, int src, int sx, int sy, int w, int h); @@ -225,7 +229,7 @@ HLL_LIBRARY(DrawGraph, HLL_EXPORT(CopyBright, DrawGraph_CopyBright), HLL_EXPORT(CopyAMap, DrawGraph_CopyAMap), HLL_EXPORT(CopySprite, DrawGraph_CopySprite), - //HLL_EXPORT(CopyColorReverse, DrawGraph_CopyColorReverse), + HLL_EXPORT(CopyColorReverse, DrawGraph_CopyColorReverse), HLL_EXPORT(CopyUseAMapUnder, DrawGraph_CopyUseAMapUnder), HLL_EXPORT(CopyUseAMapBorder, DrawGraph_CopyUseAMapBorder), HLL_EXPORT(CopyAMapMax, DrawGraph_CopyAMapMax),