|
31 | 31 | #include "doomstat.h"
|
32 | 32 | #include "i_printf.h"
|
33 | 33 | #include "i_system.h"
|
| 34 | +#include "i_video.h" |
34 | 35 | #include "info.h"
|
35 | 36 | #include "m_argv.h" // M_CheckParm()
|
36 | 37 | #include "m_fixed.h"
|
@@ -919,133 +920,160 @@ int R_ColormapNumForName(const char *name)
|
919 | 920 |
|
920 | 921 | int tran_filter_pct = 66; // filter percent
|
921 | 922 |
|
922 |
| -#define TSC 12 /* number of fixed point digits in filter percent */ |
923 |
| - |
924 | 923 | void R_InitTranMap(int progress)
|
925 | 924 | {
|
926 |
| - int lump = W_CheckNumForName("TRANMAP"); |
927 |
| - //! |
928 |
| - // @category mod |
929 |
| - // |
930 |
| - // Forces a (re-)building of the translucency and color translation tables. |
931 |
| - // |
932 |
| - int force_rebuild = M_CheckParm("-tranmap"); |
| 925 | + //! |
| 926 | + // @category mod |
| 927 | + // |
| 928 | + // Forces a (re-)building of the translucency and color translation tables. |
| 929 | + // |
933 | 930 |
|
934 |
| - // If a tranlucency filter map lump is present, use it |
| 931 | + int force_rebuild = M_CheckParm("-tranmap"); |
935 | 932 |
|
936 |
| - if (lump != -1 && !force_rebuild) // Set a pointer to the translucency filter maps. |
937 |
| - main_tranmap = W_CacheLumpNum(lump, PU_STATIC); // killough 4/11/98 |
938 |
| - else |
939 |
| - { // Compose a default transparent filter map based on PLAYPAL. |
940 |
| - unsigned char *playpal = W_CacheLumpName("PLAYPAL", PU_STATIC); |
941 |
| - char *fname = M_StringJoin(D_DoomPrefDir(), DIR_SEPARATOR_S, "tranmap.dat"); |
942 |
| - struct { |
943 |
| - unsigned char pct; |
944 |
| - unsigned char playpal[256*3]; // [FG] a palette has 256 colors saved as byte triples |
945 |
| - } cache; |
946 |
| - FILE *cachefp = M_fopen(fname,"r+b"); |
947 |
| - |
948 |
| - if (main_tranmap == NULL) // [FG] prevent memory leak |
949 |
| - { |
950 |
| - main_tranmap = Z_Malloc(256*256, PU_STATIC, 0); // killough 4/11/98 |
951 |
| - } |
| 933 | + // If a tranlucency filter map lump is present, use it |
| 934 | + int lump = W_CheckNumForName("TRANMAP"); |
952 | 935 |
|
953 |
| - // Use cached translucency filter if it's available |
| 936 | + if (lump != -1 && W_LumpLength(lump) == 256 * 256 && !force_rebuild) |
| 937 | + { |
| 938 | + // Set a pointer to the translucency filter maps. |
| 939 | + main_tranmap = W_CacheLumpNum(lump, PU_STATIC); // killough 4/11/98 |
| 940 | + add_tranmap = main_tranmap; |
| 941 | + } |
| 942 | + else |
| 943 | + { |
| 944 | + // Compose a default transparent filter map based on PLAYPAL. |
| 945 | + unsigned char *playpal = W_CacheLumpName("PLAYPAL", PU_STATIC); |
| 946 | + char *fname = |
| 947 | + M_StringJoin(D_DoomPrefDir(), DIR_SEPARATOR_S, "tranmap.dat"); |
954 | 948 |
|
955 |
| - if (!cachefp ? cachefp = M_fopen(fname,"w+b") , 1 : // [FG] open for writing and reading |
956 |
| - fread(&cache, 1, sizeof cache, cachefp) != sizeof cache || |
957 |
| - cache.pct != tran_filter_pct || |
958 |
| - memcmp(cache.playpal, playpal, sizeof cache.playpal) || |
959 |
| - fread(main_tranmap, 256, 256, cachefp) != 256 || // killough 4/11/98 |
960 |
| - force_rebuild) |
| 949 | + struct |
961 | 950 | {
|
962 |
| - long pal[3][256], tot[256], pal_w1[3][256]; |
963 |
| - long w1 = ((unsigned long) tran_filter_pct<<TSC)/100; |
964 |
| - long w2 = (1l<<TSC)-w1; |
| 951 | + unsigned char pct; |
| 952 | + unsigned char playpal[256 * 3]; // [FG] a palette has 256 colors |
| 953 | + // saved as byte triples |
| 954 | + } cache; |
965 | 955 |
|
966 |
| - // First, convert playpal into long int type, and transpose array, |
967 |
| - // for fast inner-loop calculations. Precompute tot array. |
| 956 | + FILE *cachefp = M_fopen(fname, "r+b"); |
968 | 957 |
|
969 |
| - { |
970 |
| - register int i = 255; |
971 |
| - register const unsigned char *p = playpal+255*3; |
972 |
| - do |
973 |
| - { |
974 |
| - register long t,d; |
975 |
| - pal_w1[0][i] = (pal[0][i] = t = p[0]) * w1; |
976 |
| - d = t*t; |
977 |
| - pal_w1[1][i] = (pal[1][i] = t = p[1]) * w1; |
978 |
| - d += t*t; |
979 |
| - pal_w1[2][i] = (pal[2][i] = t = p[2]) * w1; |
980 |
| - d += t*t; |
981 |
| - p -= 3; |
982 |
| - tot[i] = d << (TSC-1); |
983 |
| - } |
984 |
| - while (--i>=0); |
985 |
| - } |
| 958 | + if (main_tranmap == NULL) // [FG] prevent memory leak |
| 959 | + { |
| 960 | + main_tranmap = |
| 961 | + Z_Malloc(256 * 256 * 2, PU_STATIC, 0); // killough 4/11/98 |
| 962 | + add_tranmap = main_tranmap + 256 * 256; |
| 963 | + } |
986 | 964 |
|
987 |
| - // Next, compute all entries using minimum arithmetic. |
| 965 | + // Use cached translucency filter if it's available |
988 | 966 |
|
989 |
| - { |
990 |
| - int i,j; |
991 |
| - byte *tp = main_tranmap; |
992 |
| - for (i=0;i<256;i++) |
993 |
| - { |
994 |
| - long r1 = pal[0][i] * w2; |
995 |
| - long g1 = pal[1][i] * w2; |
996 |
| - long b1 = pal[2][i] * w2; |
| 967 | + if (!cachefp ? cachefp = M_fopen(fname, "w+b"), |
| 968 | + true : // [FG] open for writing and reading |
| 969 | + fread(&cache, 1, sizeof cache, cachefp) != sizeof cache |
| 970 | + || cache.pct != tran_filter_pct |
| 971 | + || memcmp(cache.playpal, playpal, sizeof cache.playpal) |
| 972 | + || fread(main_tranmap, 256, 256 * 2, cachefp) != 256 * 2 |
| 973 | + || // killough 4/11/98 |
| 974 | + force_rebuild) |
| 975 | + { |
| 976 | + byte *tp = main_tranmap, *ap = add_tranmap; |
997 | 977 |
|
| 978 | + for (int i = 0; i < 256; i++) |
| 979 | + { |
998 | 980 | if (!(i & 31) && progress)
|
999 |
| - I_PutChar(VB_INFO, '.'); |
| 981 | + { |
| 982 | + I_PutChar(VB_INFO, '.'); |
| 983 | + } |
| 984 | + |
| 985 | + if (!(~i & 15)) |
| 986 | + { |
| 987 | + if (i & 32) // killough 10/98: display flashing disk |
| 988 | + { |
| 989 | + I_EndRead(); |
| 990 | + } |
| 991 | + else |
| 992 | + { |
| 993 | + I_BeginRead(DISK_ICON_THRESHOLD); |
| 994 | + } |
| 995 | + } |
| 996 | + |
| 997 | + for (int j = 0; j < 256; j++) |
| 998 | + { |
| 999 | + byte *bg = playpal + 3 * i; |
| 1000 | + byte *fg = playpal + 3 * j; |
| 1001 | + byte blend[3]; |
| 1002 | + enum {r, g, b}; |
| 1003 | + |
| 1004 | + blend[r] = MIN(fg[r] + bg[r], 255); |
| 1005 | + blend[g] = MIN(fg[g] + bg[g], 255); |
| 1006 | + blend[b] = MIN(fg[b] + bg[b], 255); |
| 1007 | + |
| 1008 | + *ap++ = I_GetNearestColor(playpal, blend[r], blend[g], |
| 1009 | + blend[b]); |
| 1010 | + |
| 1011 | + // [crispy] shortcut: identical foreground and background |
| 1012 | + if (i == j) |
| 1013 | + { |
| 1014 | + *tp++ = i; |
| 1015 | + continue; |
| 1016 | + } |
| 1017 | + |
| 1018 | + // [crispy] blended color - emphasize blues |
| 1019 | + // Colour matching in RGB space doesn't work very well with |
| 1020 | + // the blues in Doom's palette. Rather than do any colour |
| 1021 | + // conversions, just emphasize the blues when building the |
| 1022 | + // translucency table. |
| 1023 | + int btmp = fg[b] * 1.666 < (fg[r] + fg[g]) ? 0 : 50; |
| 1024 | + blend[r] = (tran_filter_pct * fg[r] |
| 1025 | + + (100 - tran_filter_pct) * bg[r]) |
| 1026 | + / (100 + btmp); |
| 1027 | + blend[g] = (tran_filter_pct * fg[g] |
| 1028 | + + (100 - tran_filter_pct) * bg[g]) |
| 1029 | + / (100 + btmp); |
| 1030 | + blend[b] = (tran_filter_pct * fg[b] |
| 1031 | + + (100 - tran_filter_pct) * bg[b]) |
| 1032 | + / 100; |
| 1033 | + |
| 1034 | + *tp++ = I_GetNearestColor(playpal, blend[r], blend[g], |
| 1035 | + blend[b]); |
| 1036 | + } |
| 1037 | + } |
1000 | 1038 |
|
1001 |
| - if (!(~i & 15)) |
1002 |
| - { |
1003 |
| - if (i & 32) // killough 10/98: display flashing disk |
1004 |
| - I_EndRead(); |
1005 |
| - else |
1006 |
| - I_BeginRead(DISK_ICON_THRESHOLD); |
1007 |
| - } |
1008 |
| - |
1009 |
| - for (j=0;j<256;j++,tp++) |
1010 |
| - { |
1011 |
| - register int color = 255; |
1012 |
| - register long err; |
1013 |
| - long r = pal_w1[0][j] + r1; |
1014 |
| - long g = pal_w1[1][j] + g1; |
1015 |
| - long b = pal_w1[2][j] + b1; |
1016 |
| - long best = LONG_MAX; |
1017 |
| - do |
1018 |
| - if ((err = tot[color] - pal[0][color]*r |
1019 |
| - - pal[1][color]*g - pal[2][color]*b) < best) |
1020 |
| - best = err, *tp = color; |
1021 |
| - while (--color >= 0); |
1022 |
| - } |
1023 |
| - } |
1024 | 1039 | // [FG] finish progress line
|
1025 | 1040 | if (progress)
|
1026 |
| - I_PutChar(VB_INFO, '\n'); |
1027 |
| - } |
1028 |
| - if (cachefp && !force_rebuild) // write out the cached translucency map |
1029 | 1041 | {
|
1030 |
| - cache.pct = tran_filter_pct; |
1031 |
| - memcpy(cache.playpal, playpal, sizeof cache.playpal); // [FG] a palette has 256 colors saved as byte triples |
1032 |
| - fseek(cachefp, 0, SEEK_SET); |
1033 |
| - fwrite(&cache, 1, sizeof cache, cachefp); |
1034 |
| - fwrite(main_tranmap, 256, 256, cachefp); |
| 1042 | + I_PutChar(VB_INFO, '\n'); |
| 1043 | + } |
| 1044 | + |
| 1045 | + if (cachefp |
| 1046 | + && !force_rebuild) // write out the cached translucency map |
| 1047 | + { |
| 1048 | + cache.pct = tran_filter_pct; |
| 1049 | + memcpy(cache.playpal, playpal, |
| 1050 | + sizeof cache.playpal); // [FG] a palette has 256 colors |
| 1051 | + // saved as byte triples |
| 1052 | + fseek(cachefp, 0, SEEK_SET); |
| 1053 | + fwrite(&cache, 1, sizeof cache, cachefp); |
| 1054 | + fwrite(main_tranmap, 256, 256 * 2, cachefp); |
| 1055 | + } |
| 1056 | + } |
| 1057 | + else |
| 1058 | + { |
| 1059 | + if (progress) |
| 1060 | + { |
| 1061 | + I_Printf(VB_INFO, "........"); |
1035 | 1062 | }
|
1036 | 1063 | }
|
1037 |
| - else |
1038 |
| - if (progress) |
1039 |
| - I_Printf(VB_INFO, "........"); |
1040 | 1064 |
|
1041 |
| - if (cachefp) // killough 11/98: fix filehandle leak |
1042 |
| - fclose(cachefp); |
| 1065 | + if (cachefp) // killough 11/98: fix filehandle leak |
| 1066 | + { |
| 1067 | + fclose(cachefp); |
| 1068 | + } |
1043 | 1069 |
|
1044 |
| - Z_ChangeTag(playpal, PU_CACHE); |
1045 |
| - free(fname); |
| 1070 | + Z_ChangeTag(playpal, PU_CACHE); |
| 1071 | + free(fname); |
1046 | 1072 | }
|
1047 |
| -} |
1048 | 1073 |
|
| 1074 | + fullbright_tranmap = |
| 1075 | + (translucency == TRANSLUCENCY_ADD) ? add_tranmap : main_tranmap; |
| 1076 | +} |
1049 | 1077 | //
|
1050 | 1078 | // R_InitData
|
1051 | 1079 | // Locates all the lumps
|
|
0 commit comments