From ad89d3eea0ede0b96bf4432e24ca12c6a3c6eefe Mon Sep 17 00:00:00 2001 From: Gaerzi Date: Sun, 5 Mar 2017 21:03:36 +0100 Subject: [PATCH] Added SLADE3's colourise and tint translations. Includes support for ACS. Added tokens for '#' and '@' in sc_man_scanner.re. --- src/p_acs.cpp | 29 ++++++++++ src/p_acs.h | 2 + src/r_data/r_translate.cpp | 112 +++++++++++++++++++++++++++++++++---- src/r_data/r_translate.h | 2 + src/sc_man_scanner.re | 2 + 5 files changed, 135 insertions(+), 12 deletions(-) diff --git a/src/p_acs.cpp b/src/p_acs.cpp index 8c2859479..e2070083d 100644 --- a/src/p_acs.cpp +++ b/src/p_acs.cpp @@ -9088,6 +9088,35 @@ scriptwait: } break; + case PCD_TRANSLATIONRANGE4: + { // Colourise translation + int start = STACK(5); + int end = STACK(4); + int r = STACK(3); + int g = STACK(2); + int b = STACK(1); + sp -= 5; + + if (translation != NULL) + translation->AddColourisation(start, end, r, g, b); + } + break; + + case PCD_TRANSLATIONRANGE5: + { // Tint translation + int start = STACK(6); + int end = STACK(5); + int a = STACK(4); + int r = STACK(3); + int g = STACK(2); + int b = STACK(1); + sp -= 6; + + if (translation != NULL) + translation->AddTint(start, end, r, g, b, a); + } + break; + case PCD_ENDTRANSLATION: if (translation != NULL) { diff --git a/src/p_acs.h b/src/p_acs.h index 09d2ca686..dae4103ca 100644 --- a/src/p_acs.h +++ b/src/p_acs.h @@ -779,6 +779,8 @@ public: /*380*/ PCD_STRCPYTOSCRIPTCHRANGE, PCD_LSPEC5EX, PCD_LSPEC5EXRESULT, + PCD_TRANSLATIONRANGE4, + PCD_TRANSLATIONRANGE5, /*381*/ PCODE_COMMAND_COUNT }; diff --git a/src/r_data/r_translate.cpp b/src/r_data/r_translate.cpp index fd1bf5d21..e8ffda7ef 100644 --- a/src/r_data/r_translate.cpp +++ b/src/r_data/r_translate.cpp @@ -487,6 +487,56 @@ void FRemapTable::AddDesaturation(int start, int end, double r1, double g1, doub // //---------------------------------------------------------------------------- +void FRemapTable::AddColourisation(int start, int end, int r, int g, int b) +{ + for (int i = start; i < end; ++i) + { + float br = GPalette.BaseColors[i].r; + float bg = GPalette.BaseColors[i].g; + float bb = GPalette.BaseColors[i].b; + float grey = (br * 0.299 + bg * 0.587 + bb * 0.114) / 255.0f; + if (grey > 1.0) grey = 1.0; + br = r * grey; + bg = g * grey; + bb = b * grey; + + int j = GPalette.Remap[i]; + Palette[j] = PalEntry(j == 0 ? 0 : 255, int(br), int(bg), int(bb)); + Remap[j] = ColorMatcher.Pick(Palette[j]); + } +} + +//---------------------------------------------------------------------------- +// +// +// +//---------------------------------------------------------------------------- + +void FRemapTable::AddTint(int start, int end, int r, int g, int b, int amount) +{ + for (int i = start; i < end; ++i) + { + float br = GPalette.BaseColors[i].r; + float bg = GPalette.BaseColors[i].g; + float bb = GPalette.BaseColors[i].b; + float a = amount * 0.01f; + float ia = 1.0f - a; + br = br * ia + r * a; + bg = bg * ia + g * a; + bb = bb * ia + b * a; + + int j = GPalette.Remap[i]; + Palette[j] = PalEntry(j == 0 ? 0 : 255, int(br), int(bg), int(bb)); + Remap[j] = ColorMatcher.Pick(Palette[j]); + } +} + +//---------------------------------------------------------------------------- +// +// +// +//---------------------------------------------------------------------------- + void FRemapTable::AddToTranslation(const char *range) { int start,end; @@ -511,19 +561,9 @@ void FRemapTable::AddToTranslation(const char *range) } sc.MustGetAnyToken(); + Printf(0, "token type: %d", sc.TokenType); - if (sc.TokenType != '[' && sc.TokenType != '%') - { - int pal1,pal2; - - sc.TokenMustBe(TK_IntConst); - pal1 = sc.Number; - sc.MustGetToken(':'); - sc.MustGetToken(TK_IntConst); - pal2 = sc.Number; - AddIndexRange(start, end, pal1, pal2); - } - else if (sc.TokenType == '[') + if (sc.TokenType == '[') { // translation using RGB values int r1,g1,b1,r2,g2,b2; @@ -596,6 +636,54 @@ void FRemapTable::AddToTranslation(const char *range) AddDesaturation(start, end, r1, g1, b1, r2, g2, b2); } + else if (sc.TokenType == '#') + { + // Colourise translation + int r, g, b; + sc.MustGetToken('['); + sc.MustGetToken(TK_IntConst); + r = sc.Number; + sc.MustGetToken(','); + sc.MustGetToken(TK_IntConst); + g = sc.Number; + sc.MustGetToken(','); + sc.MustGetToken(TK_IntConst); + b = sc.Number; + sc.MustGetToken(']'); + + AddColourisation(start, end, r, g, b); + } + else if (sc.TokenType == '@') + { + // Tint translation + int a, r, g, b; + + sc.MustGetToken(TK_IntConst); + a = sc.Number; + sc.MustGetToken('['); + sc.MustGetToken(TK_IntConst); + r = sc.Number; + sc.MustGetToken(','); + sc.MustGetToken(TK_IntConst); + g = sc.Number; + sc.MustGetToken(','); + sc.MustGetToken(TK_IntConst); + b = sc.Number; + sc.MustGetToken(']'); + + AddTint(start, end, r, g, b, a); + } + else + { + int pal1, pal2; + + sc.TokenMustBe(TK_IntConst); + pal1 = sc.Number; + sc.MustGetToken(':'); + sc.MustGetToken(TK_IntConst); + pal2 = sc.Number; + AddIndexRange(start, end, pal1, pal2); + } } catch (CRecoverableError &err) { diff --git a/src/r_data/r_translate.h b/src/r_data/r_translate.h index 1680f4df0..6c0fe8c77 100644 --- a/src/r_data/r_translate.h +++ b/src/r_data/r_translate.h @@ -42,6 +42,8 @@ struct FRemapTable void AddIndexRange(int start, int end, int pal1, int pal2); void AddColorRange(int start, int end, int r1,int g1, int b1, int r2, int g2, int b2); void AddDesaturation(int start, int end, double r1, double g1, double b1, double r2, double g2, double b2); + void AddColourisation(int start, int end, int r, int g, int b); + void AddTint(int start, int end, int r, int g, int b, int amount); void AddToTranslation(const char * range); int StoreTranslation(int slot); diff --git a/src/sc_man_scanner.re b/src/sc_man_scanner.re index 1c98e9272..0c3da78bf 100644 --- a/src/sc_man_scanner.re +++ b/src/sc_man_scanner.re @@ -255,6 +255,8 @@ std2: "^" { RET('^'); } "|" { RET('|'); } "?" { RET('?'); } + "#" { RET('#'); } + "@" { RET('@'); } [ \t\v\f\r]+ { goto std1; } "\n" { goto newline; }