vid_next.m 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. // vid_next.m -- NEXTSTEP video driver
  2. #define INTERCEPTOR
  3. #import <appkit/appkit.h>
  4. #import <string.h>
  5. #import "intercep.h"
  6. #include "quakedef.h"
  7. #include "d_local.h"
  8. int BASEWIDTH = 320;
  9. int BASEHEIGHT = 200;
  10. void SetupBitmap (void);
  11. void SetupFramebuffer (void);
  12. void UpdateBitmap (void);
  13. void UpdateFramebuffer (vrect_t *vrect);
  14. void SetVideoEncoding (char *encoding);
  15. void Update8_1 (pixel_t *src, byte *dest, int width,
  16. int height, int destrowbytes);
  17. void Update16_1 (pixel_t *src, unsigned short *dest, int width,
  18. int height, int destrowbytes);
  19. void Update32_1 (pixel_t *src, unsigned *dest, int width,
  20. int height, int destrowbytes);
  21. @interface QuakeView : View
  22. @end
  23. @interface FrameWindow:Window
  24. @end
  25. unsigned short d_8to16table[256]; // not used in 8 bpp mode
  26. unsigned d_8to24table[256]; // not used in 8 bpp mode
  27. /*
  28. ==========================================================================
  29. API FUNCTIONS
  30. ==========================================================================
  31. */
  32. typedef enum {disp_bitmap, disp_framebuffer} display_t;
  33. pixel_t *vid_buffer;
  34. pixel_t *buffernative;
  35. unsigned pcolormap[4][256]; // map from quake pixels to native pixels
  36. unsigned pixbytesnative;
  37. unsigned rowbytesnative;
  38. int dither;
  39. int drawdirect = 0;
  40. int d_con_indirect = 0;
  41. display_t vid_display;
  42. byte vid_palette[768]; // saved for restarting vid system
  43. id vid_window_i;
  44. id vid_view_i;
  45. #ifdef INTERCEPTOR
  46. NXDirectBitmap *vid_dbitmap_i;
  47. NXFramebuffer *vid_framebuffer_i;
  48. #endif
  49. NXRect screenBounds; // only valid in framebuffer mode
  50. int vid_scale;
  51. char *vid_encodingstring;
  52. int vid_fullscreen;
  53. int vid_screen;
  54. int vid_high_hunk_mark;
  55. typedef enum
  56. {
  57. enc_24_rgba,
  58. enc_24_0rgb,
  59. enc_24_rgb0,
  60. enc_12_rgba,
  61. enc_12_rgb0,
  62. enc_15_0rgb,
  63. enc_564,
  64. enc_8_gray,
  65. enc_8_rgb
  66. } vid_encoding_t;
  67. typedef struct
  68. {
  69. char *string;
  70. int pixelbytes;
  71. void (*colormap) (void);
  72. vid_encoding_t name;
  73. } vidtype_t;
  74. vid_encoding_t vid_encoding;
  75. void Table8 (void);
  76. void Table15 (void);
  77. void Table12 (void);
  78. void Table12Swap (void);
  79. void Table24 (void);
  80. void Table24Swap (void);
  81. vidtype_t vid_encodingtable[]=
  82. {
  83. {"RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA",4, Table24Swap, enc_24_rgba},
  84. {"--------RRRRRRRRGGGGGGGGBBBBBBBB",4, Table24, enc_24_0rgb},
  85. {"RRRRRRRRGGGGGGGGBBBBBBBB--------",4, Table24Swap, enc_24_rgb0},
  86. {"RRRRGGGGBBBBAAAA",2, Table12Swap, enc_12_rgba},
  87. {"RRRRGGGGBBBB----",2, Table12, enc_12_rgb0},
  88. {"-RRRRRGGGGGBBBBB",2, Table15, enc_15_0rgb},
  89. {"WWWWWWWW",1, Table8, enc_8_gray},
  90. {"PPPPPPPP",1, Table8, enc_8_rgb},
  91. {NULL,0, 0, 0}
  92. };
  93. vidtype_t *vid_type;
  94. void InitNS8Bit (void);
  95. /*
  96. ================
  97. D_BeginDirectRect
  98. ================
  99. */
  100. void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
  101. {
  102. // direct drawing of the "accessing disk" icon isn't supported under Nextstep
  103. }
  104. /*
  105. ================
  106. D_EndDirectRect
  107. ================
  108. */
  109. void D_EndDirectRect (int x, int y, int width, int height)
  110. {
  111. // direct drawing of the "accessing disk" icon isn't supported under Nextstep
  112. }
  113. /*
  114. ==============
  115. VID_Restart
  116. internal call only
  117. ===============
  118. */
  119. void VID_Restart (display_t mode, int scale)
  120. {
  121. vid_display = mode;
  122. vid_scale = scale;
  123. [NXApp activateSelf:YES];
  124. if (vid_display == disp_framebuffer)
  125. SetupFramebuffer ();
  126. else
  127. SetupBitmap ();
  128. vid.recalc_refdef = 1;
  129. }
  130. /*
  131. =================
  132. VID_Scale_f
  133. Keybinding command
  134. =================
  135. */
  136. void VID_Scale_f (void)
  137. {
  138. int scale;
  139. if (Cmd_Argc () != 2)
  140. return;
  141. scale = atoi (Cmd_Argv(1));
  142. if (scale != 1 && scale != 2)
  143. {
  144. Con_Printf ("scale must be 1 or 2\n");
  145. return;
  146. }
  147. VID_Shutdown ();
  148. VID_Restart (vid_display, scale);
  149. }
  150. /*
  151. =================
  152. VID_Mode_f
  153. Keybinding command
  154. =================
  155. */
  156. void VID_Mode_f (void)
  157. {
  158. int mode;
  159. if (Cmd_Argc () != 2)
  160. return;
  161. mode = atoi (Cmd_Argv(1));
  162. VID_Shutdown ();
  163. if (mode == 0)
  164. {
  165. drawdirect = 0;
  166. VID_Restart (disp_bitmap, vid_scale);
  167. }
  168. else if (mode == 1)
  169. {
  170. drawdirect = 0;
  171. VID_Restart (disp_framebuffer, vid_scale);
  172. }
  173. else
  174. {
  175. drawdirect = 1;
  176. VID_Restart (disp_framebuffer, vid_scale);
  177. }
  178. }
  179. /*
  180. =================
  181. VID_Size_f
  182. Keybinding command
  183. =================
  184. */
  185. void VID_Size_f (void)
  186. {
  187. if (Cmd_Argc () != 3)
  188. return;
  189. VID_Shutdown ();
  190. BASEWIDTH = atoi (Cmd_Argv(1));
  191. BASEHEIGHT = atoi (Cmd_Argv(2));
  192. VID_Restart (vid_display, vid_scale);
  193. }
  194. /*
  195. ================
  196. VID_Init
  197. ================
  198. */
  199. void VID_Init (unsigned char *palette)
  200. {
  201. InitNS8Bit (); // fixed palette lookups
  202. Q_memcpy (vid_palette, palette, sizeof(vid_palette));
  203. if (COM_CheckParm ("-bitmap"))
  204. vid_display = disp_bitmap;
  205. else
  206. vid_display = disp_framebuffer;
  207. if (COM_CheckParm ("-screen2"))
  208. vid_screen = 1;
  209. else
  210. vid_screen = 0;
  211. if (COM_CheckParm ("-direct"))
  212. drawdirect = 1;
  213. Cmd_AddCommand ("vid_scale", VID_Scale_f);
  214. Cmd_AddCommand ("vid_mode", VID_Mode_f);
  215. Cmd_AddCommand ("vid_size", VID_Size_f);
  216. vid.width = BASEWIDTH;
  217. vid.height = BASEHEIGHT;
  218. vid.aspect = 1.0;
  219. vid.numpages = 1;
  220. vid.colormap = host_colormap;
  221. vid.fullbright = 256 - LittleLong (*((int *)vid.colormap + 2048));
  222. vid.maxwarpwidth = WARP_WIDTH;
  223. vid.maxwarpheight = WARP_HEIGHT;
  224. if (COM_CheckParm ("-scale2"))
  225. vid_scale = 2;
  226. else
  227. vid_scale = 1;
  228. [Application new];
  229. VID_Restart (vid_display, vid_scale);
  230. }
  231. /*
  232. ================
  233. VID_Shutdown
  234. ================
  235. */
  236. void VID_Shutdown (void)
  237. {
  238. #ifdef INTERCEPTOR
  239. if (vid_dbitmap_i)
  240. {
  241. [vid_dbitmap_i free];
  242. vid_dbitmap_i = 0;
  243. }
  244. if (vid_framebuffer_i)
  245. {
  246. [vid_framebuffer_i free];
  247. vid_framebuffer_i = 0;
  248. }
  249. #endif
  250. [vid_window_i close];
  251. [vid_window_i free];
  252. }
  253. /*
  254. ================
  255. VID_Update
  256. ================
  257. */
  258. void VID_Update (vrect_t *rects)
  259. {
  260. if (drawdirect)
  261. return;
  262. while (rects)
  263. {
  264. UpdateFramebuffer (rects);
  265. rects = rects->pnext;
  266. }
  267. if (vid_display == disp_bitmap)
  268. UpdateBitmap ();
  269. }
  270. /*
  271. ================
  272. VID_SetPalette
  273. ================
  274. */
  275. void VID_SetPalette (unsigned char *palette)
  276. {
  277. Q_memcpy (vid_palette, palette, sizeof(vid_palette));
  278. vid_type->colormap ();
  279. }
  280. /*
  281. ================
  282. VID_ShiftPalette
  283. ================
  284. */
  285. void VID_ShiftPalette (unsigned char *palette)
  286. {
  287. VID_SetPalette (palette);
  288. }
  289. /*
  290. ==========================================================================
  291. NS STUFF
  292. ==========================================================================
  293. */
  294. /*
  295. =================
  296. SetVideoEncoding
  297. =================
  298. */
  299. void SetVideoEncoding (char *encoding)
  300. {
  301. vidtype_t *type;
  302. Sys_Printf ("SetVideoEncoding: %s\n",encoding);
  303. vid_encodingstring = encoding;
  304. for (type = vid_encodingtable ; type->string ; type++)
  305. {
  306. if (strcmp(type->string, encoding) == 0)
  307. {
  308. pixbytesnative = type->pixelbytes;
  309. vid_encoding = type->name;
  310. type->colormap ();
  311. vid_type = type;
  312. return;
  313. }
  314. }
  315. Sys_Error ("Unsupported video encoding: %s\n",encoding);
  316. }
  317. /*
  318. =================
  319. AllocBuffers
  320. =================
  321. */
  322. void AllocBuffers (qboolean withnative)
  323. {
  324. int surfcachesize;
  325. void *surfcache;
  326. int pixels;
  327. int pixbytes;
  328. int vid_buffersize;
  329. if (vid_buffer)
  330. {
  331. D_FlushCaches ();
  332. Hunk_FreeToHighMark (vid_high_hunk_mark);
  333. vid_high_hunk_mark = 0;
  334. vid_buffer = NULL;
  335. }
  336. pixels = vid.width * vid.height;
  337. pixbytes = 1 +sizeof (*d_pzbuffer);
  338. if (withnative)
  339. pixbytes += pixbytesnative;
  340. surfcachesize = D_SurfaceCacheForRes (vid.width, vid.height);
  341. vid_buffersize = pixels * pixbytes + surfcachesize;
  342. vid_high_hunk_mark = Hunk_HighMark ();
  343. vid_buffer = Hunk_HighAllocName (vid_buffersize, "video");
  344. if (!vid_buffer)
  345. Sys_Error ("Couldn't alloc video buffers");
  346. vid.buffer = vid_buffer;
  347. d_pzbuffer = (unsigned short *)((byte *)vid_buffer + pixels);
  348. surfcache = (byte *)d_pzbuffer + pixels * sizeof (*d_pzbuffer);
  349. if (withnative)
  350. buffernative = (byte *)surfcache + surfcachesize;
  351. D_InitCaches (surfcache, surfcachesize);
  352. }
  353. /*
  354. =================
  355. SetupFramebuffer
  356. =================
  357. */
  358. void SetupFramebuffer (void)
  359. {
  360. #ifdef INTERCEPTOR
  361. int windowNum;
  362. NXRect cont;
  363. NXScreen const *screens;
  364. int screencount;
  365. //
  366. // get the screen list
  367. //
  368. [NXApp getScreens:&screens count:&screencount];
  369. //
  370. // create vid_framebuffer_i
  371. //
  372. vid_framebuffer_i = [[NXFramebuffer alloc]
  373. initFromScreen:screens[vid_screen].screenNumber andMapIfPossible:YES];
  374. [vid_framebuffer_i screenBounds:&screenBounds];
  375. SetVideoEncoding ([vid_framebuffer_i pixelEncoding]);
  376. buffernative = [vid_framebuffer_i data];
  377. rowbytesnative = [vid_framebuffer_i bytesPerRow];
  378. //
  379. // create window
  380. //
  381. if (vid_fullscreen)
  382. {
  383. vid.height = screenBounds.size.height / vid_scale;
  384. vid.width = screenBounds.size.width / vid_scale;
  385. cont.origin.x = 0;
  386. cont.origin.y = 0;
  387. cont.size.width = screenBounds.size.width;
  388. cont.size.height = screenBounds.size.height;
  389. }
  390. else
  391. {
  392. buffernative = (unsigned char *)buffernative + 8 * rowbytesnative +
  393. 8 * pixbytesnative;
  394. vid.width = BASEWIDTH;
  395. vid.height = BASEHEIGHT;
  396. cont.origin.x = 8;
  397. cont.origin.y = screenBounds.size.height - (vid.height*vid_scale) - 8;
  398. cont.size.width = vid.width * vid_scale;
  399. cont.size.height = vid.height * vid_scale;
  400. }
  401. vid_window_i = [[FrameWindow alloc]
  402. initContent: &cont
  403. style: NX_PLAINSTYLE
  404. backing: NX_NONRETAINED
  405. buttonMask: 0
  406. defer: NO
  407. screen: screens+vid_screen];
  408. windowNum = [vid_window_i windowNum];
  409. PSsetwindowlevel(40, windowNum);
  410. PSsetautofill(YES, windowNum);
  411. PSgsave();
  412. PSwindowdeviceround(windowNum);
  413. PSsetgray(NX_BLACK);
  414. PSsetexposurecolor();
  415. PSgrestore();
  416. //
  417. // create view
  418. //
  419. vid_view_i = [[QuakeView alloc] initFrame: &screenBounds];
  420. [[vid_window_i setContentView: vid_view_i] free];
  421. [vid_window_i makeFirstResponder: vid_view_i];
  422. [vid_window_i setDelegate: vid_view_i];
  423. [vid_window_i display];
  424. [vid_window_i makeKeyAndOrderFront: nil];
  425. NXPing ();
  426. AllocBuffers (false); // no native buffer
  427. if (drawdirect)
  428. { // the direct drawing mode to NeXT colorspace
  429. vid.buffer = buffernative;
  430. vid.rowbytes = rowbytesnative;
  431. }
  432. else
  433. vid.rowbytes = vid.width;
  434. vid.conbuffer = vid.buffer;
  435. vid.conrowbytes = vid.rowbytes;
  436. vid.conwidth = vid.width;
  437. vid.conheight = vid.height;
  438. #endif
  439. }
  440. /*
  441. =================
  442. SetupBitmap
  443. =================
  444. */
  445. void SetupBitmap (void)
  446. {
  447. int depth;
  448. NXRect content;
  449. //
  450. // open a window
  451. //
  452. NXSetRect (&content, 8,136, vid.width*vid_scale, vid.height*vid_scale);
  453. vid_window_i = [[Window alloc]
  454. initContent: &content
  455. style: NX_RESIZEBARSTYLE
  456. backing: NX_RETAINED
  457. buttonMask: 0
  458. defer: NO
  459. ];
  460. [vid_window_i display];
  461. [vid_window_i makeKeyAndOrderFront: nil];
  462. NXPing ();
  463. content.origin.x = content.origin.y = 0;
  464. vid_view_i = [[QuakeView alloc] initFrame: &content];
  465. [[vid_window_i setContentView: vid_view_i] free];
  466. [vid_window_i makeFirstResponder: vid_view_i];
  467. [vid_window_i setDelegate: vid_view_i];
  468. [vid_window_i addToEventMask: NX_FLAGSCHANGEDMASK];
  469. //
  470. // find video info
  471. //
  472. depth = [Window defaultDepthLimit];
  473. switch (depth) {
  474. case NX_EightBitGrayDepth:
  475. SetVideoEncoding ("WWWWWWWW");
  476. break;
  477. case NX_TwelveBitRGBDepth:
  478. SetVideoEncoding ("RRRRGGGGBBBBAAAA");
  479. break;
  480. default:
  481. case NX_TwentyFourBitRGBDepth:
  482. SetVideoEncoding ("RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA");
  483. break;
  484. // default: // 8 bit color shows up as an unknown...
  485. Sys_Error ("Unsupported window depth");
  486. }
  487. [vid_window_i setTitle: "Bitmap Quake Console"];
  488. //
  489. // allocate memory for the back and translation buffers
  490. //
  491. vid.rowbytes = vid.width;
  492. rowbytesnative = vid.width * pixbytesnative;
  493. AllocBuffers (true);
  494. vid.conbuffer = vid.buffer;
  495. vid.conrowbytes = vid.rowbytes;
  496. vid.conwidth = vid.width;
  497. vid.conheight = vid.height;
  498. }
  499. /*
  500. =================
  501. UpdateFramebuffer
  502. =================
  503. */
  504. void UpdateFramebuffer (vrect_t *vrect)
  505. {
  506. byte *psourcebase;
  507. byte *pdestbase;
  508. int scale;
  509. psourcebase = vid.buffer + vrect->x + vrect->y * vid.rowbytes;
  510. if (vid_display == disp_bitmap)
  511. scale = 1; // let NS do the scaling
  512. else
  513. scale = vid_scale;
  514. pdestbase = buffernative + scale *
  515. (vrect->x * pixbytesnative + vrect->y * rowbytesnative);
  516. //
  517. // translate from ideal to native (except 8 bpp direct) and copy to screen
  518. //
  519. if (pixbytesnative == 1)
  520. Update8_1 (psourcebase, pdestbase, vrect->width, vrect->height,
  521. rowbytesnative);
  522. else if (pixbytesnative == 2)
  523. Update16_1 (psourcebase, (unsigned short *)pdestbase, vrect->width, vrect->height,
  524. rowbytesnative);
  525. else
  526. Update32_1 (psourcebase, (unsigned *)pdestbase, vrect->width, vrect->height,
  527. rowbytesnative);
  528. }
  529. /*
  530. =================
  531. UpdateBitmap
  532. =================
  533. */
  534. void UpdateBitmap (void)
  535. {
  536. unsigned char *planes[5];
  537. NXRect bounds;
  538. int bpp, spp, bps, bpr, colorspace;
  539. //
  540. // flush the screen with an image call
  541. //
  542. if (pixbytesnative == 1)
  543. {
  544. bps = 8;
  545. spp = 1;
  546. bpp = 8;
  547. bpr = vid.width;
  548. colorspace = NX_OneIsWhiteColorSpace;
  549. planes[0] = vid.buffer;
  550. }
  551. else if (pixbytesnative == 2)
  552. {
  553. bps = 4;
  554. spp = 3;
  555. bpp = 16;
  556. bpr = vid.width * 2;
  557. colorspace = NX_RGBColorSpace;
  558. planes[0] = buffernative;
  559. }
  560. else
  561. {
  562. bps = 8;
  563. spp = 3;
  564. bpp = 32;
  565. bpr = vid.width * 4;
  566. colorspace = NX_RGBColorSpace;
  567. planes[0] = buffernative;
  568. }
  569. [vid_view_i getBounds: &bounds];
  570. [vid_view_i lockFocus];
  571. NXDrawBitmap(
  572. &bounds,
  573. vid.width,
  574. vid.height,
  575. bps,
  576. spp,
  577. bpp,
  578. bpr,
  579. NO,
  580. NO,
  581. colorspace,
  582. planes
  583. );
  584. [vid_view_i unlockFocus];
  585. NXPing ();
  586. }
  587. /*
  588. ==========================================================================
  589. TRANSLATION TABLE BUILDING
  590. ==========================================================================
  591. */
  592. int redramp[] = {0, 19, 59, 113, 178, 255, 300};
  593. int greenramp[] = {0, 11, 34, 66, 104, 149, 199, 255, 300};
  594. int blueramp[] = {0, 28, 84, 161, 255, 300};
  595. int greyramp[] = { 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204,
  596. 221, 238, 255, 300};
  597. byte greytable[256];
  598. byte redtable[256];
  599. byte greentable[256];
  600. byte bluetable[256];
  601. void FillTable (byte *table, int *ramp, int base)
  602. {
  603. int i, j, o;
  604. o = 0;
  605. for (i=0 ; i<16 && o < 256; i++)
  606. {
  607. j = ramp[i];
  608. for ( ; o<=j ; o++)
  609. table[o] = base + i;
  610. }
  611. }
  612. void InitNS8Bit (void)
  613. {
  614. FillTable (greytable, greyramp, 240);
  615. FillTable (redtable, redramp, 0);
  616. FillTable (greentable, greenramp, 0);
  617. FillTable (bluetable, blueramp, 0);
  618. }
  619. byte ns8trans[256] = // FIXME: dynamically calc this so palettes work
  620. {
  621. 0,241,242,243,244,244,245,246,247,248,249,250,251,252,253,254,
  622. 45,241,241,242,91,91,91,96,96,136,136,136,141,141,141,141,
  623. 241,46,242,243,243,97,97,97,245,246,143,143,143,143,148,148,
  624. 0,5,45,45,50,50,90,90,95,95,95,95,95,140,140,141,
  625. 0,40,40,40,40,80,80,80,80,80,120,120,120,120,120,120,
  626. 45,50,50,90,90,95,95,135,135,135,136,141,141,181,181,181,
  627. 45,90,91,91,131,131,136,136,136,176,181,181,186,226,231,236,
  628. 45,45,91,91,96,96,136,136,137,142,182,182,187,188,188,233,
  629. 188,249,248,247,246,137,137,137,244,243,243,91,242,241,241,45,
  630. 183,183,183,247,137,137,137,137,137,244,91,91,91,241,241,45,
  631. 252,251,188,188,248,248,142,142,142,244,244,243,91,242,241,45,
  632. 247,247,246,246,245,245,244,244,243,243,242,242,51,241,241,5,
  633. 236,231,231,191,186,185,185,140,140,135,135,95,90,90,45,45,
  634. 4,49,49,53,53,93,93,93,93,92,92,92,243,242,46,241,
  635. 239,239,239,239,239,239,239,239,239,239,239,239,239,239,239,239,
  636. 239,239,239,239,239,239,239,239,239,239,239,239,239,239,239,182
  637. };
  638. /*
  639. ===================
  640. Table8
  641. ===================
  642. */
  643. void Table8 (void)
  644. {
  645. byte *pal;
  646. int r,g,b,v;
  647. int i;
  648. byte *table;
  649. pal = vid_palette;
  650. table = (byte *)pcolormap[0];
  651. for (i=0 ; i<256 ; i++)
  652. {
  653. r = pal[0];
  654. g = pal[1];
  655. b = pal[2];
  656. pal += 3;
  657. // use the grey ramp if all indexes are close
  658. if (r-g < 16 && r-g > -16 && r-b < 16 && r-b > -16)
  659. {
  660. v = (r+g+b)/3;
  661. *table++ = greytable[v];
  662. continue;
  663. }
  664. r = redtable[r];
  665. g = greentable[g];
  666. b = bluetable[b];
  667. // otherwise use the color cube
  668. *table++ = r*(8*5) + g*5 + b;
  669. }
  670. }
  671. /*
  672. ===================
  673. Table24
  674. ===================
  675. */
  676. void Table24 (void)
  677. {
  678. byte *pal;
  679. int r,g,b,v;
  680. int i;
  681. unsigned *table;
  682. //
  683. // 8 8 8 encoding
  684. //
  685. pal = vid_palette;
  686. table = (unsigned *)pcolormap[0];
  687. for (i=0 ; i<256 ; i++)
  688. {
  689. r = pal[0];
  690. g = pal[1];
  691. b = pal[2];
  692. pal += 3;
  693. v = (r<<16) + (g<<8) + b;
  694. *table++ = v;
  695. }
  696. }
  697. /*
  698. ===================
  699. Table24Swap
  700. ===================
  701. */
  702. void Table24Swap (void)
  703. {
  704. byte *pal;
  705. int r,g,b,v;
  706. int i;
  707. unsigned *table;
  708. //
  709. // 8 8 8 encoding
  710. //
  711. pal = vid_palette;
  712. table = (unsigned *)pcolormap[0];
  713. for (i=0 ; i<256 ; i++)
  714. {
  715. r = pal[0];
  716. g = pal[1];
  717. b = pal[2];
  718. pal += 3;
  719. v = (r<<24) + (g<<16) + (b<<8) /*+ 255*/;
  720. v = NXSwapBigLongToHost (v);
  721. *table++ = v;
  722. }
  723. }
  724. /*
  725. ===================
  726. Table15
  727. ===================
  728. */
  729. void Table15 (void)
  730. {
  731. byte *pal;
  732. int r,g,b,v;
  733. int i, k;
  734. unsigned char *palette;
  735. unsigned short *table;
  736. int dadj;
  737. int ditheradjust[4] = {(1 << 9) * 3 / 8,
  738. (1 << 9) * 5 / 8,
  739. (1 << 9) * 7 / 8,
  740. (1 << 9) * 1 / 8};
  741. palette = vid_palette;
  742. table = (unsigned short *)pcolormap;
  743. //
  744. // 5 5 5 encoding
  745. //
  746. for (k=0 ; k<4 ; k++)
  747. {
  748. dadj = ditheradjust[k];
  749. pal = vid_palette;
  750. for (i=0 ; i<256 ; i++)
  751. {
  752. // shift 6 bits to get back to 0-255, & 3 more for 5 bit color
  753. // FIXME: scale intensity levels properly
  754. r = (pal[0] + dadj) >> 3;
  755. g = (pal[1] + dadj) >> 3;
  756. b = (pal[2] + dadj) >> 3;
  757. pal += 3;
  758. v = (r<<10) + (g<<5) + b;
  759. *table++ = v;
  760. }
  761. }
  762. }
  763. /*
  764. ===================
  765. Table12
  766. ===================
  767. */
  768. void Table12 (void)
  769. {
  770. byte *pal;
  771. int r,g,b,v;
  772. int i, k;
  773. unsigned short *table;
  774. int dadj;
  775. static int ditheradjust[4] = {(1 << 9) * 3 / 8,
  776. (1 << 9) * 5 / 8,
  777. (1 << 9) * 7 / 8,
  778. (1 << 9) * 1 / 8};
  779. table = (unsigned short *)pcolormap;
  780. //
  781. // 4 4 4 encoding
  782. //
  783. for (k=0 ; k<4 ; k++)
  784. {
  785. dadj = ditheradjust[k];
  786. pal = vid_palette;
  787. for (i=0 ; i<256 ; i++)
  788. {
  789. // shift 5 bits to get back to 0-255, & 4 more for 4 bit color
  790. // FIXME: scale intensity levels properly
  791. r = (pal[0] + dadj) >> 4;
  792. g = (pal[1] + dadj) >> 4;
  793. b = (pal[2] + dadj) >> 4;
  794. pal += 3;
  795. v = ((r<<12) + (g<<8) + (b<<4) /*+ 15*/);
  796. *table++ = v;
  797. }
  798. }
  799. }
  800. /*
  801. ===================
  802. Table12Swap
  803. ===================
  804. */
  805. void Table12Swap (void)
  806. {
  807. byte *pal;
  808. int r,g,b,v;
  809. int i, k;
  810. unsigned short *table;
  811. int dadj;
  812. static int ditheradjust[4] = {(1 << 9) * 3 / 8,
  813. (1 << 9) * 5 / 8,
  814. (1 << 9) * 7 / 8,
  815. (1 << 9) * 1 / 8};
  816. table = (unsigned short *)pcolormap;
  817. //
  818. // 4 4 4 encoding
  819. //
  820. for (k=0 ; k<4 ; k++)
  821. {
  822. dadj = ditheradjust[k];
  823. pal = vid_palette;
  824. for (i=0 ; i<256 ; i++)
  825. {
  826. // shift 5 bits to get back to 0-255, & 4 more for 4 bit color
  827. // FIXME: scale intensity levels properly
  828. r = (pal[0] + dadj) >> 4;
  829. g = (pal[1] + dadj) >> 4;
  830. b = (pal[2] + dadj) >> 4;
  831. pal += 3;
  832. v = ((r<<12) + (g<<8) + (b<<4) /*+ 15*/);
  833. v = NXSwapBigShortToHost (v);
  834. *table++ = v;
  835. }
  836. }
  837. }
  838. /*
  839. ==========================================================================
  840. GENERIC IMAGING FUNCTIONS
  841. ==========================================================================
  842. */
  843. /*
  844. ===================
  845. Update8_1
  846. ===================
  847. */
  848. void Update8_1 (pixel_t *src, byte *dest, int width, int height,
  849. int destrowbytes)
  850. {
  851. int x,y;
  852. unsigned rowdelta, srcdelta;
  853. unsigned xcount;
  854. byte *pdest;
  855. int xwidth;
  856. pdest = dest;
  857. xcount = width >> 3;
  858. srcdelta = vid.width - width;
  859. xwidth = width - (xcount << 3);
  860. if (xwidth)
  861. Sys_Error ("Width not multiple of 8");
  862. if ((vid_display == disp_framebuffer) && (vid_scale == 2))
  863. {
  864. int nextrow = destrowbytes;
  865. rowdelta = destrowbytes - (width << 1) + destrowbytes;
  866. if (dither)
  867. {
  868. unsigned short *psrc;
  869. psrc = (unsigned short *)src;
  870. for (y = height ; y ; y--)
  871. {
  872. for (x = xcount ; x ;x--)
  873. {
  874. unsigned temp;
  875. temp = psrc[0];
  876. pdest[0] = ((byte *)pcolormap[0])[temp];
  877. pdest[1] = ((byte *)pcolormap[1])[temp];
  878. pdest[nextrow] = ((byte *)pcolormap[2])[temp];
  879. pdest[nextrow + 1] = ((byte *)pcolormap[3])[temp];
  880. temp = psrc[1];
  881. pdest[2] = ((byte *)pcolormap[0])[temp];
  882. pdest[3] = ((byte *)pcolormap[1])[temp];
  883. pdest[nextrow + 2] = ((byte *)pcolormap[2])[temp];
  884. pdest[nextrow + 3] = ((byte *)pcolormap[3])[temp];
  885. temp = psrc[2];
  886. pdest[4] = ((byte *)pcolormap[0])[temp];
  887. pdest[5] = ((byte *)pcolormap[1])[temp];
  888. pdest[nextrow + 4] = ((byte *)pcolormap[2])[temp];
  889. pdest[nextrow + 5] = ((byte *)pcolormap[3])[temp];
  890. temp = psrc[3];
  891. pdest[6] = ((byte *)pcolormap[0])[temp];
  892. pdest[7] = ((byte *)pcolormap[1])[temp];
  893. pdest[nextrow + 6] = ((byte *)pcolormap[2])[temp];
  894. pdest[nextrow + 7] = ((byte *)pcolormap[3])[temp];
  895. temp = psrc[4];
  896. pdest[8] = ((byte *)pcolormap[0])[temp];
  897. pdest[9] = ((byte *)pcolormap[1])[temp];
  898. pdest[nextrow + 8] = ((byte *)pcolormap[2])[temp];
  899. pdest[nextrow + 9] = ((byte *)pcolormap[3])[temp];
  900. temp = psrc[5];
  901. pdest[10] = ((byte *)pcolormap[0])[temp];
  902. pdest[11] = ((byte *)pcolormap[1])[temp];
  903. pdest[nextrow + 10] = ((byte *)pcolormap[2])[temp];
  904. pdest[nextrow + 11] = ((byte *)pcolormap[3])[temp];
  905. temp = psrc[6];
  906. pdest[12] = ((byte *)pcolormap[0])[temp];
  907. pdest[13] = ((byte *)pcolormap[1])[temp];
  908. pdest[nextrow + 12] = ((byte *)pcolormap[2])[temp];
  909. pdest[nextrow + 13] = ((byte *)pcolormap[3])[temp];
  910. temp = psrc[7];
  911. pdest[14] = ((byte *)pcolormap[0])[temp];
  912. pdest[15] = ((byte *)pcolormap[1])[temp];
  913. pdest[nextrow + 14] = ((byte *)pcolormap[2])[temp];
  914. pdest[nextrow + 15] = ((byte *)pcolormap[3])[temp];
  915. pdest += 16; psrc += 8;
  916. }
  917. psrc += srcdelta;
  918. pdest += rowdelta;
  919. }
  920. }
  921. else
  922. {
  923. byte *psrc;
  924. psrc = (byte *)src;
  925. for (y = height ; y ; y--)
  926. {
  927. for (x = xcount ; x ;x--)
  928. {
  929. pdest[0] = pdest[1] = pdest[nextrow] =
  930. pdest[nextrow + 1] = ((byte *)pcolormap[0])[psrc[0]];
  931. pdest[2] = pdest[3] = pdest[nextrow + 2] =
  932. pdest[nextrow + 3] = ((byte *)pcolormap[0])[psrc[1]];
  933. pdest[4] = pdest[5] = pdest[nextrow + 4] =
  934. pdest[nextrow + 5] = ((byte *)pcolormap[0])[psrc[2]];
  935. pdest[6] = pdest[7] = pdest[nextrow + 6] =
  936. pdest[nextrow + 7] = ((byte *)pcolormap[0])[psrc[3]];
  937. pdest[8] = pdest[9] = pdest[nextrow + 8] =
  938. pdest[nextrow + 9] = ((byte *)pcolormap[0])[psrc[4]];
  939. pdest[10] = pdest[11] = pdest[nextrow + 10] =
  940. pdest[nextrow + 11] = ((byte *)pcolormap[0])[psrc[5]];
  941. pdest[12] = pdest[13] = pdest[nextrow + 12] =
  942. pdest[nextrow + 13] = ((byte *)pcolormap[0])[psrc[6]];
  943. pdest[14] = pdest[15] = pdest[nextrow + 14] =
  944. pdest[nextrow + 15] = ((byte *)pcolormap[0])[psrc[7]];
  945. pdest += 16; psrc += 8;
  946. }
  947. psrc += srcdelta;
  948. pdest += rowdelta;
  949. }
  950. }
  951. }
  952. else
  953. {
  954. rowdelta = destrowbytes - width;
  955. if (dither)
  956. {
  957. unsigned short *psrc;
  958. psrc = (unsigned short *)src;
  959. for (y = height ; y>0 ; y -= 2)
  960. {
  961. for (x = xcount ; x ;x--)
  962. {
  963. pdest[0] = ((byte *)pcolormap[0])[psrc[0]];
  964. pdest[1] = ((byte *)pcolormap[1])[psrc[1]];
  965. pdest[2] = ((byte *)pcolormap[0])[psrc[2]];
  966. pdest[3] = ((byte *)pcolormap[1])[psrc[3]];
  967. pdest[4] = ((byte *)pcolormap[0])[psrc[4]];
  968. pdest[5] = ((byte *)pcolormap[1])[psrc[5]];
  969. pdest[6] = ((byte *)pcolormap[0])[psrc[6]];
  970. pdest[7] = ((byte *)pcolormap[1])[psrc[7]];
  971. pdest += 8; psrc += 8;
  972. }
  973. psrc += srcdelta;
  974. pdest += rowdelta;
  975. for (x = xcount ; x ;x--)
  976. {
  977. pdest[0] = ((byte *)pcolormap[2])[psrc[0]];
  978. pdest[1] = ((byte *)pcolormap[3])[psrc[1]];
  979. pdest[2] = ((byte *)pcolormap[2])[psrc[2]];
  980. pdest[3] = ((byte *)pcolormap[3])[psrc[3]];
  981. pdest[4] = ((byte *)pcolormap[2])[psrc[4]];
  982. pdest[5] = ((byte *)pcolormap[3])[psrc[5]];
  983. pdest[6] = ((byte *)pcolormap[2])[psrc[6]];
  984. pdest[7] = ((byte *)pcolormap[3])[psrc[7]];
  985. pdest += 8; psrc += 8;
  986. }
  987. psrc += srcdelta;
  988. pdest += rowdelta;
  989. }
  990. }
  991. else
  992. {
  993. byte *psrc;
  994. psrc = (byte *)src;
  995. // srcdelta += width;
  996. // rowdelta += width;
  997. for (y = height ; y ; y--)
  998. {
  999. for (x = xcount ; x ;x--)
  1000. {
  1001. pdest[0] = ((byte *)pcolormap[0])[psrc[0]];
  1002. pdest[1] = ((byte *)pcolormap[0])[psrc[1]];
  1003. pdest[2] = ((byte *)pcolormap[0])[psrc[2]];
  1004. pdest[3] = ((byte *)pcolormap[0])[psrc[3]];
  1005. pdest[4] = ((byte *)pcolormap[0])[psrc[4]];
  1006. pdest[5] = ((byte *)pcolormap[0])[psrc[5]];
  1007. pdest[6] = ((byte *)pcolormap[0])[psrc[6]];
  1008. pdest[7] = ((byte *)pcolormap[0])[psrc[7]];
  1009. pdest += 8; psrc += 8;
  1010. }
  1011. psrc += srcdelta;
  1012. pdest += rowdelta;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. /*
  1018. ===================
  1019. Update16_1
  1020. ===================
  1021. */
  1022. void Update16_1 (pixel_t *src, unsigned short *dest, int width,
  1023. int height, int destrowbytes)
  1024. {
  1025. int x,y;
  1026. unsigned rowdelta, srcdelta;
  1027. unsigned xcount;
  1028. pixel_t *psrc;
  1029. unsigned short *pdest;
  1030. int xwidth;
  1031. psrc = src;
  1032. pdest = dest;
  1033. xcount = width >> 3;
  1034. srcdelta = vid.width - width;
  1035. xwidth = width - (xcount << 3);
  1036. if (xwidth)
  1037. Sys_Error ("Width not multiple of 8");
  1038. if ((vid_display == disp_framebuffer) && (vid_scale == 2))
  1039. {
  1040. int nextrow = destrowbytes >> 1;
  1041. rowdelta = (destrowbytes - ((width << 1) << 1) + destrowbytes) >> 1;
  1042. if (dither)
  1043. {
  1044. for (y = height ; y ; y--)
  1045. {
  1046. for (x = xcount ; x ;x--)
  1047. {
  1048. unsigned temp;
  1049. temp = psrc[0];
  1050. pdest[0] = ((unsigned short *)pcolormap[0])[temp];
  1051. pdest[1] = ((unsigned short *)pcolormap[1])[temp];
  1052. pdest[nextrow] = ((unsigned short *)pcolormap[2])[temp];
  1053. pdest[nextrow + 1] = ((unsigned short *)pcolormap[3])[temp];
  1054. temp = psrc[1];
  1055. pdest[2] = ((unsigned short *)pcolormap[0])[temp];
  1056. pdest[3] = ((unsigned short *)pcolormap[1])[temp];
  1057. pdest[nextrow + 2] = ((unsigned short *)pcolormap[2])[temp];
  1058. pdest[nextrow + 3] = ((unsigned short *)pcolormap[3])[temp];
  1059. temp = psrc[2];
  1060. pdest[4] = ((unsigned short *)pcolormap[0])[temp];
  1061. pdest[5] = ((unsigned short *)pcolormap[1])[temp];
  1062. pdest[nextrow + 4] = ((unsigned short *)pcolormap[2])[temp];
  1063. pdest[nextrow + 5] = ((unsigned short *)pcolormap[3])[temp];
  1064. temp = psrc[3];
  1065. pdest[6] = ((unsigned short *)pcolormap[0])[temp];
  1066. pdest[7] = ((unsigned short *)pcolormap[1])[temp];
  1067. pdest[nextrow + 6] = ((unsigned short *)pcolormap[2])[temp];
  1068. pdest[nextrow + 7] = ((unsigned short *)pcolormap[3])[temp];
  1069. temp = psrc[4];
  1070. pdest[8] = ((unsigned short *)pcolormap[0])[temp];
  1071. pdest[9] = ((unsigned short *)pcolormap[1])[temp];
  1072. pdest[nextrow + 8] = ((unsigned short *)pcolormap[2])[temp];
  1073. pdest[nextrow + 9] = ((unsigned short *)pcolormap[3])[temp];
  1074. temp = psrc[5];
  1075. pdest[10] = ((unsigned short *)pcolormap[0])[temp];
  1076. pdest[11] = ((unsigned short *)pcolormap[1])[temp];
  1077. pdest[nextrow + 10] = ((unsigned short *)pcolormap[2])[temp];
  1078. pdest[nextrow + 11] = ((unsigned short *)pcolormap[3])[temp];
  1079. temp = psrc[6];
  1080. pdest[12] = ((unsigned short *)pcolormap[0])[temp];
  1081. pdest[13] = ((unsigned short *)pcolormap[1])[temp];
  1082. pdest[nextrow + 12] = ((unsigned short *)pcolormap[2])[temp];
  1083. pdest[nextrow + 13] = ((unsigned short *)pcolormap[3])[temp];
  1084. temp = psrc[7];
  1085. pdest[14] = ((unsigned short *)pcolormap[0])[temp];
  1086. pdest[15] = ((unsigned short *)pcolormap[1])[temp];
  1087. pdest[nextrow + 14] = ((unsigned short *)pcolormap[2])[temp];
  1088. pdest[nextrow + 15] = ((unsigned short *)pcolormap[3])[temp];
  1089. pdest += 16; psrc += 8;
  1090. }
  1091. psrc += srcdelta;
  1092. pdest += rowdelta;
  1093. }
  1094. }
  1095. else
  1096. {
  1097. for (y = height ; y ; y--)
  1098. {
  1099. for (x = xcount ; x ;x--)
  1100. {
  1101. pdest[0] = pdest[1] = pdest[nextrow] =
  1102. pdest[nextrow + 1] = pcolormap[0][psrc[0]];
  1103. pdest[2] = pdest[3] = pdest[nextrow + 2] =
  1104. pdest[nextrow + 3] = pcolormap[0][psrc[1]];
  1105. pdest[4] = pdest[5] = pdest[nextrow + 4] =
  1106. pdest[nextrow + 5] = pcolormap[0][psrc[2]];
  1107. pdest[6] = pdest[7] = pdest[nextrow + 6] =
  1108. pdest[nextrow + 7] = pcolormap[0][psrc[3]];
  1109. pdest[8] = pdest[9] = pdest[nextrow + 8] =
  1110. pdest[nextrow + 9] = pcolormap[0][psrc[4]];
  1111. pdest[10] = pdest[11] = pdest[nextrow + 10] =
  1112. pdest[nextrow + 11] = pcolormap[0][psrc[5]];
  1113. pdest[12] = pdest[13] = pdest[nextrow + 12] =
  1114. pdest[nextrow + 13] = pcolormap[0][psrc[6]];
  1115. pdest[14] = pdest[15] = pdest[nextrow + 14] =
  1116. pdest[nextrow + 15] = pcolormap[0][psrc[7]];
  1117. pdest += 16; psrc += 8;
  1118. }
  1119. psrc += srcdelta;
  1120. pdest += rowdelta;
  1121. }
  1122. }
  1123. }
  1124. else
  1125. {
  1126. rowdelta = (destrowbytes - (width<<1))>>1;
  1127. if (dither)
  1128. {
  1129. for (y = height ; y>0 ; y -= 2)
  1130. {
  1131. for (x = xcount ; x ;x--)
  1132. {
  1133. pdest[0] = ((unsigned short *)pcolormap[0])[psrc[0]];
  1134. pdest[1] = ((unsigned short *)pcolormap[1])[psrc[1]];
  1135. pdest[2] = ((unsigned short *)pcolormap[0])[psrc[2]];
  1136. pdest[3] = ((unsigned short *)pcolormap[1])[psrc[3]];
  1137. pdest[4] = ((unsigned short *)pcolormap[0])[psrc[4]];
  1138. pdest[5] = ((unsigned short *)pcolormap[1])[psrc[5]];
  1139. pdest[6] = ((unsigned short *)pcolormap[0])[psrc[6]];
  1140. pdest[7] = ((unsigned short *)pcolormap[1])[psrc[7]];
  1141. pdest += 8; psrc += 8;
  1142. }
  1143. psrc += srcdelta;
  1144. pdest += rowdelta;
  1145. for (x = xcount ; x ;x--)
  1146. {
  1147. pdest[0] = ((unsigned short *)pcolormap[2])[psrc[0]];
  1148. pdest[1] = ((unsigned short *)pcolormap[3])[psrc[1]];
  1149. pdest[2] = ((unsigned short *)pcolormap[2])[psrc[2]];
  1150. pdest[3] = ((unsigned short *)pcolormap[3])[psrc[3]];
  1151. pdest[4] = ((unsigned short *)pcolormap[2])[psrc[4]];
  1152. pdest[5] = ((unsigned short *)pcolormap[3])[psrc[5]];
  1153. pdest[6] = ((unsigned short *)pcolormap[2])[psrc[6]];
  1154. pdest[7] = ((unsigned short *)pcolormap[3])[psrc[7]];
  1155. pdest += 8; psrc += 8;
  1156. }
  1157. psrc += srcdelta;
  1158. pdest += rowdelta;
  1159. }
  1160. }
  1161. else
  1162. {
  1163. for (y = height ; y ; y--)
  1164. {
  1165. for (x = xcount ; x ;x--)
  1166. {
  1167. pdest[0] = pcolormap[0][psrc[0]];
  1168. pdest[1] = pcolormap[0][psrc[1]];
  1169. pdest[2] = pcolormap[0][psrc[2]];
  1170. pdest[3] = pcolormap[0][psrc[3]];
  1171. pdest[4] = pcolormap[0][psrc[4]];
  1172. pdest[5] = pcolormap[0][psrc[5]];
  1173. pdest[6] = pcolormap[0][psrc[6]];
  1174. pdest[7] = pcolormap[0][psrc[7]];
  1175. pdest += 8; psrc += 8;
  1176. }
  1177. psrc += srcdelta;
  1178. pdest += rowdelta;
  1179. }
  1180. }
  1181. }
  1182. }
  1183. /*
  1184. ===================
  1185. Update32_1
  1186. ===================
  1187. */
  1188. void Update32_1 (pixel_t *src, unsigned *dest, int width, int height,
  1189. int destrowbytes)
  1190. {
  1191. int x,y;
  1192. unsigned rowdelta, srcdelta;
  1193. unsigned xcount;
  1194. pixel_t *psrc;
  1195. unsigned *pdest;
  1196. int xwidth;
  1197. psrc = src;
  1198. pdest = dest;
  1199. xcount = width >> 3;
  1200. srcdelta = vid.width - width;
  1201. xwidth = width - (xcount << 3);
  1202. if (xwidth)
  1203. Sys_Error ("Width not multiple of 8");
  1204. if ((vid_display == disp_framebuffer) && (vid_scale == 2))
  1205. {
  1206. int nextrow = destrowbytes >> 2;
  1207. rowdelta = ((destrowbytes - ((width << 1) << 2)) >> 2) +
  1208. (destrowbytes >> 2);
  1209. for (y = height ; y ; y--)
  1210. {
  1211. for (x = xcount ; x ;x--)
  1212. {
  1213. pdest[0] = pdest[1] = pdest[nextrow] =
  1214. pdest[nextrow + 1] = pcolormap[0][psrc[0]];
  1215. pdest[2] = pdest[3] = pdest[nextrow + 2] =
  1216. pdest[nextrow + 3] = pcolormap[0][psrc[1]];
  1217. pdest[4] = pdest[5] = pdest[nextrow + 4] =
  1218. pdest[nextrow + 5] = pcolormap[0][psrc[2]];
  1219. pdest[6] = pdest[7] = pdest[nextrow + 6] =
  1220. pdest[nextrow + 7] = pcolormap[0][psrc[3]];
  1221. pdest[8] = pdest[9] = pdest[nextrow + 8] =
  1222. pdest[nextrow + 9] = pcolormap[0][psrc[4]];
  1223. pdest[10] = pdest[11] = pdest[nextrow + 10] =
  1224. pdest[nextrow + 11] = pcolormap[0][psrc[5]];
  1225. pdest[12] = pdest[13] = pdest[nextrow + 12] =
  1226. pdest[nextrow + 13] = pcolormap[0][psrc[6]];
  1227. pdest[14] = pdest[15] = pdest[nextrow + 14] =
  1228. pdest[nextrow + 15] = pcolormap[0][psrc[7]];
  1229. pdest += 16; psrc += 8;
  1230. }
  1231. psrc += srcdelta;
  1232. pdest += rowdelta;
  1233. }
  1234. }
  1235. else
  1236. {
  1237. rowdelta = (destrowbytes - (width<<2))>>2;
  1238. for (y = height ; y ; y--)
  1239. {
  1240. for (x = xcount ; x ;x--)
  1241. {
  1242. pdest[0] = pcolormap[0][psrc[0]];
  1243. pdest[1] = pcolormap[0][psrc[1]];
  1244. pdest[2] = pcolormap[0][psrc[2]];
  1245. pdest[3] = pcolormap[0][psrc[3]];
  1246. pdest[4] = pcolormap[0][psrc[4]];
  1247. pdest[5] = pcolormap[0][psrc[5]];
  1248. pdest[6] = pcolormap[0][psrc[6]];
  1249. pdest[7] = pcolormap[0][psrc[7]];
  1250. pdest += 8; psrc += 8;
  1251. }
  1252. psrc += srcdelta;
  1253. pdest += rowdelta;
  1254. }
  1255. }
  1256. }
  1257. /*
  1258. ==========================================================================
  1259. NEXTSTEP VIEW CLASS
  1260. ==========================================================================
  1261. */
  1262. @implementation QuakeView
  1263. /*
  1264. =================
  1265. windowDidMove
  1266. =================
  1267. */
  1268. - windowDidMove:sender
  1269. {
  1270. NXPoint aPoint;
  1271. NXRect winframe;
  1272. aPoint.x = aPoint.y = 0;
  1273. [self convertPoint:&aPoint toView:nil];
  1274. [window convertBaseToScreen: &aPoint];
  1275. [window getFrame: &winframe];
  1276. if ((int)aPoint.x & 7)
  1277. {
  1278. [window moveTo:winframe.origin.x - ((int)aPoint.x&7)
  1279. :winframe.origin.y];
  1280. [window getFrame: &winframe];
  1281. }
  1282. return self;
  1283. }
  1284. - windowWillResize:sender toSize:(NXSize *)frameSize
  1285. {
  1286. NXRect fr, cont;
  1287. fr.origin.x = fr.origin.y = 0;
  1288. fr.size = *frameSize;
  1289. [Window getContentRect:&cont forFrameRect: &fr style:[window style]];
  1290. cont.size.width = (int)cont.size.width & ~15;
  1291. if (cont.size.width < 128)
  1292. cont.size.width = 128;
  1293. cont.size.height = (int)cont.size.height & ~3;
  1294. if (cont.size.height < 32)
  1295. cont.size.height = 32;
  1296. [Window getFrameRect:&fr forContentRect: &cont style:[window style]];
  1297. *frameSize = fr.size;
  1298. return self;
  1299. }
  1300. - windowDidResize:sender
  1301. {
  1302. if (vid_display == disp_framebuffer)
  1303. Sys_Error ("How the heck are you resizing a framebuffer window?!?");
  1304. vid.width = bounds.size.width/vid_scale;
  1305. vid.height = bounds.size.height/vid_scale;
  1306. //
  1307. // allocate memory for the back and translation buffers
  1308. //
  1309. vid.rowbytes = vid.width;
  1310. rowbytesnative = vid.width * pixbytesnative;
  1311. AllocBuffers (true);
  1312. vid.conbuffer = vid.buffer;
  1313. vid.conrowbytes = vid.rowbytes;
  1314. vid.conwidth = vid.width;
  1315. vid.conheight = vid.height;
  1316. vid.recalc_refdef = 1;
  1317. return self;
  1318. }
  1319. -(BOOL) acceptsFirstResponder
  1320. {
  1321. return YES;
  1322. }
  1323. typedef struct
  1324. {
  1325. int source, dest;
  1326. } keymap_t;
  1327. keymap_t keymaps[] =
  1328. {
  1329. {103, K_RIGHTARROW},
  1330. {102, K_LEFTARROW},
  1331. {100, K_UPARROW},
  1332. {101, K_DOWNARROW},
  1333. {111, K_PAUSE},
  1334. {59, K_F1},
  1335. {60, K_F2},
  1336. {61, K_F3},
  1337. {62, K_F4},
  1338. {63, K_F5},
  1339. {64, K_F6},
  1340. {65, K_F7},
  1341. {66, K_F8},
  1342. {67, K_F9},
  1343. {68, K_F10},
  1344. {87, K_F11},
  1345. {88, K_F12},
  1346. {-1,-1}
  1347. };
  1348. keymap_t flagmaps[] =
  1349. {
  1350. {NX_SHIFTMASK, K_SHIFT},
  1351. {NX_CONTROLMASK, K_CTRL},
  1352. {NX_ALTERNATEMASK, K_ALT},
  1353. {NX_COMMANDMASK, K_ALT},
  1354. {-1,-1}
  1355. };
  1356. /*
  1357. ===================
  1358. keyboard methods
  1359. ===================
  1360. */
  1361. - keyDown:(NXEvent *)theEvent
  1362. {
  1363. int ch;
  1364. keymap_t *km;
  1365. PSobscurecursor ();
  1366. // check for non-ascii first
  1367. ch = theEvent->data.key.keyCode;
  1368. for (km=keymaps;km->source!=-1;km++)
  1369. if (ch == km->source)
  1370. {
  1371. Key_Event (km->dest, true);
  1372. return self;
  1373. }
  1374. ch = theEvent->data.key.charCode;
  1375. if (ch >= 'A' && ch <= 'Z')
  1376. ch += 'a' - 'A';
  1377. if (ch>=256)
  1378. return self;
  1379. Key_Event (ch, true);
  1380. return self;
  1381. }
  1382. - flagsChanged:(NXEvent *)theEvent
  1383. {
  1384. static int oldflags;
  1385. int newflags;
  1386. int delta;
  1387. keymap_t *km;
  1388. int i;
  1389. PSobscurecursor ();
  1390. newflags = theEvent->flags;
  1391. delta = newflags ^ oldflags;
  1392. for (i=0 ; i<32 ; i++)
  1393. {
  1394. if ( !(delta & (1<<i)))
  1395. continue;
  1396. // changed
  1397. for (km=flagmaps;km->source!=-1;km++)
  1398. if ( (1<<i) == km->source)
  1399. {
  1400. if (newflags & (1<<i))
  1401. Key_Event (km->dest, true);
  1402. else
  1403. Key_Event (km->dest, false);
  1404. }
  1405. }
  1406. oldflags = newflags;
  1407. return self;
  1408. }
  1409. - keyUp:(NXEvent *)theEvent
  1410. {
  1411. int ch;
  1412. keymap_t *km;
  1413. // check for non-ascii first
  1414. ch = theEvent->data.key.keyCode;
  1415. for (km=keymaps;km->source!=-1;km++)
  1416. if (ch == km->source)
  1417. {
  1418. Key_Event (km->dest, false);
  1419. return self;
  1420. }
  1421. ch = theEvent->data.key.charCode;
  1422. if (ch >= 'A' && ch <= 'Z')
  1423. ch += 'a' - 'A';
  1424. if (ch>=256)
  1425. return self;
  1426. Key_Event (ch, false);
  1427. return self;
  1428. }
  1429. - tiffShot
  1430. {
  1431. id imagerep, image;
  1432. NXRect r;
  1433. NXStream *stream;
  1434. int fd;
  1435. int i;
  1436. char tiffname[80];
  1437. [vid_window_i getFrame: &r];
  1438. r.origin.x = r.origin.y = 0;
  1439. image = [[NXImage alloc] initSize: &r.size];
  1440. imagerep = [[NXCachedImageRep alloc] initFromWindow:vid_window_i rect:&r];
  1441. [image lockFocus];
  1442. [imagerep draw];
  1443. [image unlockFocus];
  1444. //
  1445. // find a file name to save it to
  1446. //
  1447. strcpy(tiffname,"quake00.tif");
  1448. for (i=0 ; i<=99 ; i++)
  1449. {
  1450. tiffname[5] = i/10 + '0';
  1451. tiffname[6] = i%10 + '0';
  1452. if (Sys_FileTime(tiffname) == -1)
  1453. break; // file doesn't exist
  1454. }
  1455. if (i==100)
  1456. Sys_Error ("SCR_ScreenShot_f: Couldn't create a tiff");
  1457. fd = open (tiffname, O_RDWR|O_CREAT|O_TRUNC, 0666);
  1458. stream = NXOpenFile (fd, NX_READWRITE);
  1459. [image writeTIFF: stream];
  1460. NXClose (stream);
  1461. close (fd);
  1462. printf ("wrote %s\n", tiffname);
  1463. [image free];
  1464. [imagerep free];
  1465. return self;
  1466. }
  1467. - screenShot: sender
  1468. {
  1469. return [self tiffShot];
  1470. }
  1471. - setScaleFullScreen: sender
  1472. {
  1473. VID_Shutdown ();
  1474. if (vid_fullscreen)
  1475. {
  1476. vid_fullscreen = 0;
  1477. VID_Restart (vid_display, vid_scale);
  1478. }
  1479. else
  1480. {
  1481. vid_fullscreen = 1;
  1482. VID_Restart (vid_display, vid_scale);
  1483. }
  1484. return self;
  1485. }
  1486. @end
  1487. //============================================================================
  1488. @implementation FrameWindow
  1489. - windowExposed:(NXEvent *)theEvent
  1490. {
  1491. return self;
  1492. }
  1493. @end