summaryrefslogtreecommitdiff
path: root/apps/plugins/sdl/progs/wolf3d/wl_shade.c
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2019-07-07 22:00:20 -0400
committerFranklin Wei <git@fwei.tk>2019-07-09 11:20:55 -0400
commit3f59fc8b771625aca9c3aefe03cf1038d8461963 (patch)
treee0623a323613baa0b0993411b38bcaed144b27ed /apps/plugins/sdl/progs/wolf3d/wl_shade.c
parent439a0d1d91fa040d261fc39b87278bc9f5391dcc (diff)
downloadrockbox-3f59fc8b771625aca9c3aefe03cf1038d8461963.tar.gz
rockbox-3f59fc8b771625aca9c3aefe03cf1038d8461963.zip
Wolfenstein 3-D!
This is a port of Wolf4SDL, which is derived from the original id software source release. The port runs on top of the SDL plugin runtime and is loaded as an overlay. Licensing of the game code is not an issue, as discussed below (essentially, the Debian project treats Wolf4SDL as GPLv2, with an email from John Carmack backing it up): http://forums.rockbox.org/index.php?topic=52872 Included is a copy of MAME's Yamaha OPL sound chip emulator (fmopl_gpl.c). This file was not part of the original Wolf4SDL source (which includes a non-GPL'd version), but was rather rebased from from a later MAME source which had been relicensed to GPLv2. Change-Id: I64c2ba035e0be7e2f49252f40640641416613439
Diffstat (limited to 'apps/plugins/sdl/progs/wolf3d/wl_shade.c')
-rw-r--r--apps/plugins/sdl/progs/wolf3d/wl_shade.c143
1 files changed, 143 insertions, 0 deletions
diff --git a/apps/plugins/sdl/progs/wolf3d/wl_shade.c b/apps/plugins/sdl/progs/wolf3d/wl_shade.c
new file mode 100644
index 0000000000..3f1d5a7b3f
--- /dev/null
+++ b/apps/plugins/sdl/progs/wolf3d/wl_shade.c
@@ -0,0 +1,143 @@
1#include "version.h"
2
3#ifdef USE_SHADING
4#include "wl_def.h"
5#include "wl_shade.h"
6
7typedef struct {
8 uint8_t destRed, destGreen, destBlue; // values between 0 and 255
9 uint8_t fogStrength;
10} shadedef_t;
11
12shadedef_t shadeDefs[] = {
13 { 0, 0, 0, LSHADE_NOSHADING },
14 { 0, 0, 0, LSHADE_NORMAL },
15 { 0, 0, 0, LSHADE_FOG },
16 { 40, 40, 40, LSHADE_NORMAL },
17 { 60, 60, 60, LSHADE_FOG }
18};
19
20uint8_t shadetable[SHADE_COUNT][256];
21int LSHADE_flag;
22
23#ifdef USE_FEATUREFLAGS
24
25// The lower 8-bit of the upper left tile of every map determine
26// the used shading definition of shadeDefs.
27static int GetShadeDefID()
28{
29 int shadeID = ffDataTopLeft & 0x00ff;
30 assert(shadeID >= 0 && shadeID < lengthof(shadeDefs));
31 return shadeID;
32}
33
34#else
35
36static int GetShadeDefID()
37{
38 int shadeID;
39 switch(gamestate.episode * 10 + mapon)
40 {
41 case 0: shadeID = 4; break;
42 case 1:
43 case 2:
44 case 6: shadeID = 1; break;
45 case 3: shadeID = 0; break;
46 case 5: shadeID = 2; break;
47 default: shadeID = 3; break;
48 }
49 assert(shadeID >= 0 && shadeID < lengthof(shadeDefs));
50 return shadeID;
51}
52
53#endif
54
55
56// Returns the palette index of the nearest matching color of the
57// given RGB color in given palette
58byte GetColor(byte red, byte green, byte blue, SDL_Color *palette)
59{
60 byte mincol = 0;
61 double mindist = 200000.F, curdist, DRed, DGreen, DBlue;
62
63 SDL_Color *palPtr = palette;
64
65 for(int col = 0; col < 256; col++, palPtr++)
66 {
67 DRed = (double) (red - palPtr->r);
68 DGreen = (double) (green - palPtr->g);
69 DBlue = (double) (blue - palPtr->b);
70 curdist = DRed * DRed + DGreen * DGreen + DBlue * DBlue;
71 if(curdist < mindist)
72 {
73 mindist = curdist;
74 mincol = (byte) col;
75 }
76 }
77 return mincol;
78}
79
80// Fade all colors in 32 steps down to the destination-RGB
81// (use gray for fogging, black for standard shading)
82void GenerateShadeTable(byte destRed, byte destGreen, byte destBlue,
83 SDL_Color *palette, int fog)
84{
85 double curRed, curGreen, curBlue, redStep, greenStep, blueStep;
86 SDL_Color *palPtr = palette;
87
88 // Set the fog-flag
89 LSHADE_flag=fog;
90
91 // Color loop
92 for(int i = 0; i < 256; i++, palPtr++)
93 {
94 // Get original palette color
95 curRed = palPtr->r;
96 curGreen = palPtr->g;
97 curBlue = palPtr->b;
98
99 // Calculate increment per step
100 redStep = ((double) destRed - curRed) / (SHADE_COUNT + 8);
101 greenStep = ((double) destGreen - curGreen) / (SHADE_COUNT + 8);
102 blueStep = ((double) destBlue - curBlue) / (SHADE_COUNT + 8);
103
104 // Calc color for each shade of the current color
105 for (int shade = 0; shade < SHADE_COUNT; shade++)
106 {
107 shadetable[shade][i] = GetColor((byte) curRed, (byte) curGreen, (byte) curBlue, palette);
108
109 // Inc to next shade
110 curRed += redStep;
111 curGreen += greenStep;
112 curBlue += blueStep;
113 }
114 }
115}
116
117void NoShading()
118{
119 for(int shade = 0; shade < SHADE_COUNT; shade++)
120 for(int i = 0; i < 256; i++)
121 shadetable[shade][i] = i;
122}
123
124void InitLevelShadeTable()
125{
126 shadedef_t *shadeDef = &shadeDefs[GetShadeDefID()];
127 if(shadeDef->fogStrength == LSHADE_NOSHADING)
128 NoShading();
129 else
130 GenerateShadeTable(shadeDef->destRed, shadeDef->destGreen, shadeDef->destBlue, gamepal, shadeDef->fogStrength);
131}
132
133int GetShade(int scale)
134{
135 int shade = (scale >> 1) / (((viewwidth * 3) >> 8) + 1 + LSHADE_flag); // TODO: reconsider this...
136 if(shade > 32) shade = 32;
137 else if(shade < 1) shade = 1;
138 shade = 32 - shade;
139
140 return shade;
141}
142
143#endif