SCREEN.CPP 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #include <io.h>
  2. #include <fcntl.h>
  3. #include <i86.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include "typedefs.h"
  7. #include "misc.h"
  8. #include "globals.h"
  9. #include "textio.h"
  10. #include "palette.h"
  11. #include "debug4g.h"
  12. #include "resource.h"
  13. #include "error.h"
  14. #include "screen.h"
  15. #include "helix.h"
  16. #include "trig.h"
  17. #include "gfx.h"
  18. typedef uchar GAMMA_SUBTABLE[256];
  19. static GAMMA_SUBTABLE *gammaTable;
  20. static RGB baseDAC[256], curDAC[256], fromDAC[256], toRGB;
  21. static RGB *palTable[kMaxPalettes];
  22. static int effectR = 0, effectG = 0, effectB = 0;
  23. static BOOL DacInvalid = TRUE;
  24. static long messageTime = 0;
  25. static char message[256];
  26. static int curPalette = kPalNormal;
  27. static int curGamma = 0;
  28. static RGB StdPal[32] =
  29. {
  30. { 0, 0, 0},
  31. { 0, 0,170},
  32. { 0,170, 0},
  33. { 0,170,170},
  34. {170, 0, 0},
  35. {170, 0,170},
  36. {170, 85, 0},
  37. {170,170,170},
  38. { 85, 85, 85},
  39. { 85, 85,255},
  40. { 85,255, 85},
  41. { 85,255,255},
  42. {255, 85, 85},
  43. {255, 85,255},
  44. {255,255, 85},
  45. {255,255,255},
  46. {241,241,241},
  47. {226,226,226},
  48. {211,211,211},
  49. {196,196,196},
  50. {181,181,181},
  51. {166,166,166},
  52. {151,151,151},
  53. {136,136,136},
  54. {120,120,120},
  55. {105,105,105},
  56. { 90, 90, 90},
  57. { 75, 75, 75},
  58. { 60, 60, 60},
  59. { 45, 45, 45},
  60. { 30, 30, 30},
  61. { 15, 15, 15},
  62. };
  63. struct LOADITEM {
  64. int index;
  65. char *name;
  66. };
  67. static LOADITEM PLU[] =
  68. {
  69. { kPLUNormal, "NORMAL" },
  70. { kPLUFlame, "FLAME" },
  71. { kPLUCold, "COLD" },
  72. { kPLURed, "BEAST" },
  73. { kPLUGray, "GRAY" },
  74. { kPLUGrayish, "GRAYISH" },
  75. { kPLUCultist2, "TOMMY" },
  76. { kPLUSpider1, "SPIDER1" },
  77. { kPLUSpider2, "SPIDER2" },
  78. { kPLUSpider3, "SPIDER3" },
  79. { kPLUPlayer1, "P0" }, // also kPLUPlayer5
  80. { kPLUPlayer2, "P1" }, // also kPLUPlayer6
  81. { kPLUPlayer3, "P2" }, // also kPLUPlayer7
  82. { kPLUPlayer4, "P3" }, // also kPLUPlayer8
  83. };
  84. static LOADITEM PAL[] =
  85. {
  86. { kPalNormal, "BLOOD" },
  87. { kPalWater, "WATER" },
  88. { kPalBeast, "BEAST" },
  89. };
  90. // exported variables
  91. int gGammaLevels;
  92. BOOL gFogMode = FALSE;
  93. BYTE gStdColor[32]; // standard 32 colors
  94. // Weight coefficients for color matching
  95. #define kWeightR 1
  96. #define kWeightG 1
  97. #define kWeightB 1
  98. BYTE scrFindClosestColor( int r, int g, int b )
  99. {
  100. int i;
  101. int dr, dg, db, dist, matchDist, match;
  102. matchDist = 0x7FFFFFFF;
  103. for ( i = 0; i < 256; i++ )
  104. {
  105. dist = 0;
  106. dg = (int)palette[i].g - g;
  107. dist += kWeightG * dg * dg;
  108. if ( dist >= matchDist )
  109. continue;
  110. dr = (int)palette[i].r - r;
  111. dist += kWeightR * dr * dr;
  112. if ( dist >= matchDist )
  113. continue;
  114. db = (int)palette[i].b - b;
  115. dist += kWeightB * db * db;
  116. if ( dist >= matchDist )
  117. continue;
  118. matchDist = dist;
  119. match = i;
  120. if (dist == 0)
  121. break;
  122. }
  123. return (BYTE)match;
  124. }
  125. void scrCreateStdColors( void )
  126. {
  127. for (int i = 0; i < LENGTH(StdPal); i++)
  128. gStdColor[i] = scrFindClosestColor(StdPal[i].r, StdPal[i].g, StdPal[i].b);
  129. }
  130. void scrLoadPLUs( void )
  131. {
  132. int i;
  133. if ( gFogMode )
  134. {
  135. RESHANDLE hRemap;
  136. hRemap = gSysRes.Lookup("FOG", ".FLU");
  137. if (hRemap == NULL)
  138. ThrowError("FOG.FLU not found", ES_ERROR);
  139. palookup[0] = (BYTE *)gSysRes.Lock(hRemap);
  140. for ( i = 0; i < LENGTH(PLU); i++ )
  141. {
  142. palookup[PLU[i].index] = (BYTE *)palookup[0];
  143. }
  144. parallaxvisibility = 8192;
  145. }
  146. else
  147. {
  148. for ( i = 0; i < LENGTH(PLU); i++ )
  149. {
  150. RESHANDLE hRemap;
  151. hRemap = gSysRes.Lookup(PLU[i].name, ".plu");
  152. if (hRemap == NULL)
  153. {
  154. char buffer[128];
  155. sprintf(buffer, "%s.PLU not found", PLU[i].name);
  156. ThrowError(buffer, ES_ERROR);
  157. }
  158. if ( gSysRes.Size(hRemap) / 256 != kPalLookups )
  159. ThrowError("Incorrect PLU size", ES_ERROR);
  160. palookup[PLU[i].index] = (uchar *)gSysRes.Lock(hRemap);
  161. }
  162. }
  163. }
  164. void scrLoadPalette()
  165. {
  166. int i;
  167. tioPrint("Loading palettes");
  168. for ( i = 0; i < LENGTH(PAL); i++ )
  169. {
  170. RESHANDLE hRemap;
  171. hRemap = gSysRes.Lookup(PAL[i].name, ".pal");
  172. if (hRemap == NULL)
  173. {
  174. char buffer[128];
  175. sprintf(buffer, "%s.PAL not found", PAL[i].name);
  176. ThrowError(buffer, ES_ERROR);
  177. }
  178. palTable[PAL[i].index] = (RGB *)gSysRes.Lock(hRemap);
  179. }
  180. // copy the default palette into ken's variable so Std color stuff still works
  181. memcpy(palette, palTable[kPalNormal], sizeof(palette));
  182. paletteloaded = 1;
  183. numpalookups = kPalLookups;
  184. scrLoadPLUs();
  185. RESHANDLE hTransluc;
  186. tioPrint("Loading translucency table");
  187. hTransluc = gSysRes.Lookup("trans", ".tlu");
  188. if (hTransluc == NULL)
  189. ThrowError("TRANS.TLU not found", ES_ERROR);
  190. transluc = (uchar *)gSysRes.Lock(hTransluc);
  191. fixtransluscence(transluc);
  192. }
  193. void scrSetMessage(char *s)
  194. {
  195. messageTime = gFrameClock;
  196. strcpy(message, s);
  197. }
  198. void scrDisplayMessage( int nColor )
  199. {
  200. if (gFrameClock < messageTime + kTimerRate * 2)
  201. gfxDrawText(windowx1, windowy1, nColor, message, 0);
  202. }
  203. void scrSetPalette( int nPalette )
  204. {
  205. curPalette = nPalette;
  206. scrSetGamma(curGamma); // reset baseline colors
  207. }
  208. void scrSetGamma( int nGamma )
  209. {
  210. int i;
  211. dassert(nGamma < gGammaLevels);
  212. curGamma = nGamma;
  213. for ( i = 0; i < 256; i++ )
  214. {
  215. baseDAC[i].r = (uchar)(gammaTable[nGamma][palTable[curPalette][i].r]);
  216. baseDAC[i].g = (uchar)(gammaTable[nGamma][palTable[curPalette][i].g]);
  217. baseDAC[i].b = (uchar)(gammaTable[nGamma][palTable[curPalette][i].b]);
  218. }
  219. // force a DAC update
  220. DacInvalid = TRUE;
  221. }
  222. void scrDacRelEffect( int r, int g, int b )
  223. {
  224. effectR = ClipHigh(effectR + r, 384);
  225. effectG = ClipHigh(effectG + g, 384);
  226. effectB = ClipHigh(effectB + b, 384);
  227. // effectR += r;
  228. // effectG += g;
  229. // effectB += b;
  230. DacInvalid = TRUE;
  231. }
  232. void scrDacAbsEffect( int r, int g, int b )
  233. {
  234. effectR = r;
  235. effectG = g;
  236. effectB = b;
  237. DacInvalid = TRUE;
  238. }
  239. void scrSetupFade( BYTE r, BYTE g, BYTE b )
  240. {
  241. memcpy(fromDAC, curDAC, sizeof(fromDAC));
  242. toRGB.r = r;
  243. toRGB.g = g;
  244. toRGB.b = b;
  245. }
  246. void scrSetupUnfade( void )
  247. {
  248. memcpy(fromDAC, baseDAC, sizeof(fromDAC));
  249. }
  250. void scrFadeAmount( int nFrac )
  251. {
  252. for ( int i = 0; i < 256; i++ )
  253. {
  254. curDAC[i].r = (BYTE)dmulscale16(0x10000 - nFrac, fromDAC[i].r, nFrac, toRGB.r);
  255. curDAC[i].g = (BYTE)dmulscale16(0x10000 - nFrac, fromDAC[i].g, nFrac, toRGB.g);
  256. curDAC[i].b = (BYTE)dmulscale16(0x10000 - nFrac, fromDAC[i].b, nFrac, toRGB.b);
  257. }
  258. gSetDACRange(0, 256, curDAC);
  259. }
  260. void scrSetDac( ulong nTicks )
  261. {
  262. int i;
  263. // don't set the palette in stereo red/blue mode
  264. if ( vidoption == 6 )
  265. return;
  266. // don't update DACs if no effect necessary
  267. if ( DacInvalid )
  268. {
  269. for (i = 0; i < 256; i++)
  270. {
  271. curDAC[i].r = (uchar)ClipRange(baseDAC[i].r + effectR, 0, 255);
  272. curDAC[i].g = (uchar)ClipRange(baseDAC[i].g + effectG, 0, 255);
  273. curDAC[i].b = (uchar)ClipRange(baseDAC[i].b + effectB, 0, 255);
  274. }
  275. gSetDACRange(0, 256, curDAC);
  276. }
  277. DacInvalid = FALSE;
  278. if ( effectR | effectG | effectB )
  279. {
  280. DacInvalid = TRUE;
  281. if (effectR > 0)
  282. effectR = ClipLow(effectR - nTicks, 0);
  283. else if (effectR < 0)
  284. effectR = ClipHigh(effectR + nTicks, 0);
  285. if (effectG > 0)
  286. effectG = ClipLow(effectG - nTicks, 0);
  287. else if (effectG < 0)
  288. effectG = ClipHigh(effectG + nTicks, 0);
  289. if (effectB > 0)
  290. effectB = ClipLow(effectB - nTicks, 0);
  291. else if (effectB < 0)
  292. effectB = ClipHigh(effectB + nTicks, 0);
  293. }
  294. }
  295. void scrInit( void )
  296. {
  297. RESHANDLE hRes;
  298. curPalette = kPalNormal;
  299. curGamma = 0;
  300. tioPrint("Loading gamma correction table");
  301. hRes = gSysRes.Lookup("gamma", ".dat");
  302. if ( hRes == NULL )
  303. ThrowError("Gamma table not found", ES_ERROR);
  304. gGammaLevels = gSysRes.Size(hRes) / 256;
  305. gammaTable = (GAMMA_SUBTABLE *)gSysRes.Lock(hRes);
  306. }
  307. void scrSetGameMode( void )
  308. {
  309. setgamemode();
  310. // initialize y lookups for helix driver
  311. for (int i = 0; i < kMaxRows; i++)
  312. gYLookup[i] = ylookup[i];
  313. if ( vidoption == 1 || vidoption == 2 )
  314. {
  315. gPageTable[0].bytesPerRow = ylookup[1];
  316. gPageTable[0].begin = (unsigned)frameplace;
  317. }
  318. clearview(0);
  319. scrNextPage();
  320. scrSetDac(0);
  321. }
  322. void scrNextPage( void )
  323. {
  324. nextpage();
  325. switch ( vidoption )
  326. {
  327. case 0: // non-chained
  328. if (chainnumpages > 1)
  329. gPageTable[0].begin = (unsigned)chainplace;
  330. break;
  331. case 1: // VESA 2.0 linear buffer
  332. gPageTable[0].begin = (unsigned)frameplace;
  333. case 2: // memory buffered VESA/13h
  334. break;
  335. case 3: // Tseng ET4000
  336. break;
  337. case 4: // Paradise
  338. break;
  339. case 5: // S3
  340. break;
  341. case 6: // Crystal Eyes
  342. break;
  343. case 7: // Red/Blue Stereogram
  344. break;
  345. }
  346. }