Advertisement
Guest User

Untitled

a guest
Jan 8th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 58.08 KB | None | 0 0
  1. /*
  2. * An SDL replacement for BUILD's VESA code.
  3. *
  4. * Written by Ryan C. Gordon. (icculus@clutteredmind.org)
  5. *
  6. * Please do NOT harrass Ken Silverman about any code modifications
  7. * (including this file) to BUILD.
  8. */
  9.  
  10. /*
  11. * "Build Engine & Tools" Copyright (c) 1993-1997 Ken Silverman
  12. * Ken Silverman's official web site: "http://www.advsys.net/ken"
  13. * See the included license file "BUILDLIC.TXT" for license info.
  14. * This file IS NOT A PART OF Ken Silverman's original release
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <stdarg.h>
  19. #include <assert.h>
  20. #include <string.h>
  21. #include "platform.h"
  22. #include "types.h"
  23. #if (!defined PLATFORM_SUPPORTS_SDL)
  24. #error This platform apparently does not use SDL. Do not compile this.
  25. #endif
  26.  
  27.  
  28. #define BUILD_NOMOUSEGRAB "BUILD_NOMOUSEGRAB"
  29. #define BUILD_WINDOWED "BUILD_WINDOWED"
  30. #define BUILD_SDLDEBUG "BUILD_SDLDEBUG"
  31. #define BUILD_RENDERER "BUILD_RENDERER"
  32. #define BUILD_GLLIBRARY "BUILD_GLLIBRARY"
  33. #define BUILD_USERSCREENRES "BUILD_USERSCREENRES"
  34. #define BUILD_MAXSCREENRES "BUILD_MAXSCREENRES"
  35. #define BUILD_HALLOFMIRRORS "BUILD_HALLOFMIRRORS"
  36. #define BUILD_GLDUMP "BUILD_GLDUMP"
  37. #define BUILD_SDLJOYSTICK "BUILD_SDLJOYSTICK"
  38.  
  39. #include "SDL.h"
  40. #include "build.h"
  41. #include "display.h"
  42. #include "fixedPoint_math.h"
  43. #include "engine.h"
  44. #include "network.h"
  45.  
  46. #include "mmulti_unstable.h"
  47. #include "mmulti_stable.h"
  48. #include "network.h"
  49. #include "icon.h"
  50.  
  51.  
  52.  
  53. // NATIVE TIMER FUNCTION DECLARATION
  54. /*
  55. FCS: The timer section sadly uses Native high precision calls to implement timer functions.
  56. QueryPerformanceFrequency and QueryPerformanceCounter
  57. it seems SDL precision was not good enough (or rather using unaccurate OS functions) to replicate
  58. a DOS timer.
  59. */
  60.  
  61. int TIMER_GetPlatformTicksInOneSecond(int64_t* t);
  62. void TIMER_GetPlatformTicks(int64_t* t);
  63.  
  64. //END // NATIVE TIMER FUNCTION DECLARATION
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. #if ((defined PLATFORM_WIN32))
  72. #include <windows.h>
  73. #endif
  74.  
  75. #include "draw.h"
  76. #include "cache.h"
  77.  
  78.  
  79. /*
  80. * !!! remove the surface_end checks, for speed's sake. They are a
  81. * !!! needed safety right now. --ryan.
  82. */
  83.  
  84.  
  85. #define DEFAULT_MAXRESWIDTH 1600
  86. #define DEFAULT_MAXRESHEIGHT 1200
  87.  
  88.  
  89. #define UNLOCK_SURFACE_AND_RETURN if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); return;
  90.  
  91.  
  92. /* !!! move these elsewhere? */
  93. int32_t xres, yres, bytesperline, imageSize, maxpages;
  94. uint8_t* frameplace;
  95.  
  96. //The frambuffer address
  97. uint8_t* frameoffset;
  98. uint8_t *screen, vesachecked;
  99. int32_t buffermode, origbuffermode, linearmode;
  100. uint8_t permanentupdate = 0, vgacompatible;
  101.  
  102. SDL_Surface *surface = NULL; /* This isn't static so that we can use it elsewhere AH */
  103.  
  104. static uint32_t sdl_flags = SDL_HWPALETTE;
  105. static int32_t mouse_relative_x = 0;
  106. static int32_t mouse_relative_y = 0;
  107. static short mouse_buttons = 0;
  108. static unsigned int lastkey = 0;
  109. /* so we can make use of setcolor16()... - DDOI */
  110. static uint8_t drawpixel_color=0;
  111.  
  112. static uint32_t scancodes[SDLK_LAST];
  113.  
  114. static int32_t last_render_ticks = 0;
  115. int32_t total_render_time = 1;
  116. int32_t total_rendered_frames = 0;
  117.  
  118. static char *titleNameLong = NULL;
  119. static char *titleNameShort = NULL;
  120.  
  121. void restore256_palette (void);
  122. void set16color_palette (void);
  123.  
  124.  
  125.  
  126. int controlsfd = -1;
  127.  
  128.  
  129. boolean leftpressed = false;
  130. boolean rightpressed = false;
  131. boolean uppressed = false;
  132. boolean downpressed = false;
  133. boolean buttonapressed = false;
  134. boolean buttonbpressed = false;
  135. boolean buttonhomepressed = false;
  136. boolean buttonquestionpressed = false;
  137.  
  138. boolean debug = false;
  139.  
  140.  
  141.  
  142. void ReadInputs()
  143. {
  144. //struct LFController LFC;
  145. if (controlsfd == -1) return;
  146. char cdata[0x60] = "";
  147.  
  148. ioctl (controlsfd,0x80604518, cdata);
  149. //printf("%p %p %d \n",a,data,length);
  150. //int i=0;
  151. //char* temp = (char*)b;
  152. /*
  153. for(i=0;i<length;i++)
  154. {
  155. printf("%0x, ",data[i]);
  156. }
  157.  
  158. */
  159.  
  160. if ((cdata[13] & 0x2) && (uppressed == false))
  161. {
  162. printf("UP\n");
  163. SDL_Event sdlevent;
  164. sdlevent.type=SDL_KEYDOWN;
  165. sdlevent.key.keysym.sym=SDLK_UP;
  166. SDL_PushEvent(&sdlevent);
  167. uppressed = true;
  168. }
  169. else
  170. {
  171. if (((cdata[13] & 0x2)== 0) && uppressed == true)
  172. {
  173. uppressed = false;
  174. SDL_Event sdlevent;
  175. sdlevent.type=SDL_KEYUP;
  176. sdlevent.key.keysym.sym=SDLK_UP;
  177. SDL_PushEvent(&sdlevent);
  178. }
  179.  
  180. }
  181. if ((cdata[13] & 0x4) && downpressed == false)
  182. {
  183. printf("DOWN\n");
  184. SDL_Event sdlevent;
  185. sdlevent.type=SDL_KEYDOWN;
  186. sdlevent.key.keysym.sym=SDLK_DOWN;
  187. SDL_PushEvent(&sdlevent);
  188. downpressed = true;
  189. }
  190. else
  191. {
  192. if (((cdata[13] & 0x4)== 0) && downpressed == true)
  193. {
  194. downpressed = false;
  195. SDL_Event sdlevent;
  196. sdlevent.type=SDL_KEYUP;
  197. sdlevent.key.keysym.sym=SDLK_DOWN;
  198. SDL_PushEvent(&sdlevent);
  199. }
  200.  
  201. }
  202. if ((cdata[12] & 0x80) && rightpressed == false)
  203. {
  204. printf("RIGHT\n");
  205. SDL_Event sdlevent;
  206. sdlevent.type=SDL_KEYDOWN;
  207. sdlevent.key.keysym.sym=SDLK_RIGHT;
  208. SDL_PushEvent(&sdlevent);
  209. rightpressed = true;
  210. }
  211. else
  212. {
  213. if (((cdata[12] & 0x80)== 0) && rightpressed == true)
  214. {
  215. rightpressed = false;
  216. SDL_Event sdlevent;
  217. sdlevent.type=SDL_KEYUP;
  218. sdlevent.key.keysym.sym=SDLK_RIGHT;
  219. SDL_PushEvent(&sdlevent);
  220. }
  221.  
  222. }
  223. if ((cdata[13] & 0x10) && leftpressed == false)
  224. {
  225. printf("LEFT\n");
  226. SDL_Event sdlevent;
  227. sdlevent.type=SDL_KEYDOWN;
  228. sdlevent.key.keysym.sym=SDLK_LEFT;
  229. SDL_PushEvent(&sdlevent);
  230. leftpressed = true;
  231. }
  232. else
  233. {
  234. if (((cdata[13] & 0x10)== 0) && leftpressed == true)
  235. {
  236. leftpressed = false;
  237. SDL_Event sdlevent;
  238. sdlevent.type=SDL_KEYUP;
  239. sdlevent.key.keysym.sym=SDLK_LEFT;
  240. SDL_PushEvent(&sdlevent);
  241. }
  242.  
  243. }
  244. if ((cdata[14] & 0x04) && (buttonapressed == false))
  245. {
  246. printf("BUTTONA\n");
  247. SDL_Event sdlevent;
  248. sdlevent.type=SDL_KEYDOWN;
  249. sdlevent.key.keysym.sym=SDLK_RETURN;
  250. SDL_PushEvent(&sdlevent);
  251. SDL_Event event;
  252. event.type=SDL_KEYDOWN;
  253. event.key.keysym.sym=SDLK_RCTRL;
  254. SDL_PushEvent(&event);
  255. buttonapressed = true;
  256. }
  257. else
  258. {
  259. if (((cdata[14] & 0x04)==0) && (buttonapressed == true))
  260. {
  261. buttonapressed = false;
  262. SDL_Event sdlevent;
  263. sdlevent.type=SDL_KEYUP;
  264. sdlevent.key.keysym.sym=SDLK_RETURN;
  265. SDL_PushEvent(&sdlevent);
  266. SDL_Event event;
  267. event.type=SDL_KEYUP;
  268. event.key.keysym.sym=SDLK_RCTRL;
  269. SDL_PushEvent(&event);
  270. }
  271.  
  272. }
  273. if ((cdata[14] & 0x08) && (buttonbpressed == false))
  274. {
  275. printf("BUTTONB Press\n");
  276. SDL_Event sdlevent;
  277. sdlevent.type=SDL_KEYDOWN;
  278. sdlevent.key.keysym.sym=SDLK_SPACE;
  279. SDL_PushEvent(&sdlevent);
  280. sdlevent.type=SDL_KEYDOWN;
  281. sdlevent.key.keysym.sym=SDLK_a;
  282. SDL_PushEvent(&sdlevent);
  283. buttonbpressed = true;
  284. }
  285. else
  286. {
  287. if (((cdata[14] & 0x08)==0) && (buttonbpressed == true))
  288. {
  289. printf("BUTTONB Down\n");
  290. buttonbpressed = false;
  291. SDL_Event sdlevent;
  292. sdlevent.type=SDL_KEYUP;
  293. sdlevent.key.keysym.sym=SDLK_SPACE;
  294. SDL_PushEvent(&sdlevent);
  295. sdlevent.type=SDL_KEYUP;
  296. sdlevent.key.keysym.sym=SDLK_a;
  297. SDL_PushEvent(&sdlevent);
  298. }
  299.  
  300. }
  301.  
  302. //if ((cdata[0] == 0x2) && buttonhomepressed == false)
  303. if ((cdata[0] & 0x02) && (buttonhomepressed == false))
  304. {
  305. printf("HomeButton Press\n");
  306. SDL_Event sdlevent;
  307. sdlevent.type=SDL_KEYDOWN;
  308. sdlevent.key.keysym.sym=SDLK_ESCAPE;
  309. SDL_PushEvent(&sdlevent);
  310. buttonhomepressed = true;
  311. }
  312. else
  313. {
  314. //if ((cdata[0] == 0x0) && buttonhomepressed == true)
  315. if (((cdata[0] & 0x02)==0) && (buttonhomepressed == true))
  316. {
  317. printf("HomeButton Up\n");
  318. buttonhomepressed = false;
  319. SDL_Event sdlevent;
  320. sdlevent.type=SDL_KEYUP;
  321. sdlevent.key.keysym.sym=SDLK_ESCAPE;
  322. SDL_PushEvent(&sdlevent);
  323. }
  324.  
  325. }
  326.  
  327. /* // key to change weapon.. we dont have one???
  328. if (LFC->QuestionButton == 0x01 && buttonquestionpressed == false)
  329. {
  330. printf("BUTTONQuestion\n");
  331. SDL_Event sdlevent;
  332. sdlevent.type=SDL_KEYDOWN;
  333. sdlevent.key.keysym.sym=SDLK_QUOTE;
  334. SDL_PushEvent(&sdlevent);
  335. buttonquestionpressed = true;
  336. }
  337. else
  338. {
  339. if (buttonquestionpressed == true && LFC->QuestionButton == 0x00)
  340. {
  341. buttonquestionpressed = false;
  342. SDL_Event sdlevent;
  343. sdlevent.type=SDL_KEYUP;
  344. sdlevent.key.keysym.sym=SDLK_QUOTE;
  345. SDL_PushEvent(&sdlevent);
  346. }
  347.  
  348. }
  349. */
  350. }
  351.  
  352.  
  353.  
  354.  
  355. static void __append_sdl_surface_flag(SDL_Surface *_surface, char *str,
  356. size_t strsize, Uint32 flag,
  357. const char *flagstr)
  358. {
  359. if (_surface->flags & flag)
  360. {
  361. if ( (strlen(str) + strlen(flagstr)) >= (strsize - 1) )
  362. strcpy(str + (strsize - 5), " ...");
  363. else
  364. strcat(str, flagstr);
  365. } /* if */
  366. }
  367.  
  368.  
  369. #define append_sdl_surface_flag(a, b, c, fl) __append_sdl_surface_flag(a, b, c, fl, " " #fl)
  370. #define print_tf_state(str, val) printf("%s: {%s}\n", str, (val) ? "true" : "false" )
  371.  
  372. static void output_surface_info(SDL_Surface *_surface)
  373. {
  374. const SDL_VideoInfo *info;
  375. char f[256];
  376.  
  377.  
  378. if (_surface == NULL)
  379. {
  380. printf("-WARNING- You've got a NULL screen surface!");
  381. }
  382. else
  383. {
  384. f[0] = '\0';
  385. printf("screen surface is (%dx%dx%dbpp).\n",_surface->w, _surface->h, _surface->format->BitsPerPixel);
  386.  
  387. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_SWSURFACE);
  388. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_HWSURFACE);
  389. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_ASYNCBLIT);
  390. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_ANYFORMAT);
  391. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_HWPALETTE);
  392. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_DOUBLEBUF);
  393. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_FULLSCREEN);
  394. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_OPENGL);
  395. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_OPENGLBLIT);
  396. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_RESIZABLE);
  397. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_NOFRAME);
  398. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_HWACCEL);
  399. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_SRCCOLORKEY);
  400. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_RLEACCELOK);
  401. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_RLEACCEL);
  402. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_SRCALPHA);
  403. append_sdl_surface_flag(_surface, f, sizeof (f), SDL_PREALLOC);
  404.  
  405. if (f[0] == '\0')
  406. strcpy(f, " (none)");
  407.  
  408. printf("New vidmode flags:%s.\n", f);
  409.  
  410. info = SDL_GetVideoInfo();
  411. assert(info != NULL);
  412. /*
  413. print_tf_state("hardware surface available", info->hw_available);
  414. print_tf_state("window manager available", info->wm_available);
  415. print_tf_state("accelerated hardware->hardware blits", info->blit_hw);
  416. print_tf_state("accelerated hardware->hardware colorkey blits", info->blit_hw_CC);
  417. print_tf_state("accelerated hardware->hardware alpha blits", info->blit_hw_A);
  418. print_tf_state("accelerated software->hardware blits", info->blit_sw);
  419. print_tf_state("accelerated software->hardware colorkey blits", info->blit_sw_CC);
  420. print_tf_state("accelerated software->hardware alpha blits", info->blit_sw_A);
  421. print_tf_state("accelerated color fills", info->blit_fill);
  422.  
  423. printf("video memory: (%d),\n", info->video_mem);
  424. */
  425. }
  426. }
  427.  
  428.  
  429. static void output_driver_info(void)
  430. {
  431. char buffer[256];
  432.  
  433. if (SDL_VideoDriverName(buffer, sizeof (buffer)) == NULL){
  434. printf("-WARNING- SDL_VideoDriverName() returned NULL!");
  435. } /* if */
  436. else
  437. {
  438. printf("Using SDL video driver \"%s\".", buffer);
  439. } /* else */
  440. } /* output_driver_info */
  441.  
  442.  
  443. void* get_framebuffer(void){
  444. return((Uint8 *) surface->pixels);
  445. }
  446.  
  447.  
  448.  
  449. /*
  450. * !!! This is almost an entire copy of the original setgamemode().
  451. * !!! Figure out what is needed for just 2D mode, and separate that
  452. * !!! out. Then, place the original setgamemode() back into engine.c,
  453. * !!! and remove our simple implementation (and this function.)
  454. * !!! Just be sure to keep the non-DOS things, like the window's
  455. * !!! titlebar caption. --ryan.
  456. */
  457. static uint8_t screenalloctype = 255;
  458. static void init_new_res_vars(int32_t davidoption)
  459. {
  460. int i = 0;
  461. int j = 0;
  462.  
  463. setupmouse();
  464.  
  465. SDL_WM_SetCaption(titleNameLong, titleNameShort);
  466.  
  467. xdim = xres = surface->w;
  468. ydim = yres = surface->h;
  469.  
  470. printf("init_new_res_vars %d %d\n",xdim,ydim);
  471.  
  472. bytesperline = surface->w;
  473. vesachecked = 1;
  474. vgacompatible = 1;
  475. linearmode = 1;
  476. qsetmode = surface->h;
  477. activepage = visualpage = 0;
  478.  
  479.  
  480. frameoffset = frameplace = (uint8_t*)surface->pixels;
  481.  
  482. if (screen != NULL)
  483. {
  484. if (screenalloctype == 0) kkfree((void *)screen);
  485. if (screenalloctype == 1) suckcache((int32_t *)screen);
  486. screen = NULL;
  487. } /* if */
  488.  
  489.  
  490. switch(vidoption)
  491. {
  492. case 1:i = xdim*ydim; break;
  493. case 2: xdim = 320; ydim = 200; i = xdim*ydim; break;
  494.  
  495. default: assert(0);
  496. }
  497. j = ydim*4*sizeof(int32_t); /* Leave room for horizlookup&horizlookup2 */
  498.  
  499. if(horizlookup)
  500. free(horizlookup);
  501.  
  502. if(horizlookup2)
  503. free(horizlookup2);
  504.  
  505. horizlookup = (int32_t*)malloc(j);
  506. horizlookup2 = (int32_t*)malloc(j);
  507.  
  508. j = 0;
  509.  
  510. //Build lookup table (X screespace -> frambuffer offset.
  511. for(i = 0; i <= ydim; i++)
  512. {
  513. ylookup[i] = j;
  514. j += bytesperline;
  515. }
  516.  
  517. horizycent = ((ydim*4)>>1);
  518.  
  519. /* Force drawrooms to call dosetaspect & recalculate stuff */
  520. oxyaspect = oxdimen = oviewingrange = -1;
  521.  
  522. //Let the Assembly module how many pixels to skip when drawing a column
  523. setBytesPerLine(bytesperline);
  524.  
  525.  
  526. setview(0L,0L,xdim-1,ydim-1);
  527.  
  528. setbrightness(curbrightness, palette);
  529.  
  530. if (searchx < 0) {
  531. searchx = halfxdimen;
  532. searchy = (ydimen>>1);
  533. }
  534.  
  535. }
  536.  
  537.  
  538.  
  539. static void go_to_new_vid_mode(int davidoption, int w, int h)
  540. {
  541. getvalidvesamodes();
  542. SDL_ClearError();
  543. // don't do SDL_SetVideoMode if SDL_WM_SetIcon not called. See sdl doc for SDL_WM_SetIcon
  544. surface = SDL_SetVideoMode(w, h, 8, sdl_flags);
  545. if (surface == NULL)
  546. {
  547. Error(EXIT_FAILURE, "BUILDSDL: Failed to set %dx%d video mode!\n"
  548. "BUILDSDL: SDL_Error() says [%s].\n",
  549. w, h, SDL_GetError());
  550. } /* if */
  551.  
  552. output_surface_info(surface);
  553. init_new_res_vars(davidoption); // dont be confused between vidoption (global) and davidoption
  554. }
  555.  
  556. static __inline int sdl_mouse_button_filter(SDL_MouseButtonEvent const *event)
  557. {
  558. /*
  559. * What bits BUILD expects:
  560. * 0 left button pressed if 1
  561. * 1 right button pressed if 1
  562. * 2 middle button pressed if 1
  563. *
  564. * (That is, this is what Int 33h (AX=0x05) returns...)
  565. *
  566. * additionally bits 3&4 are set for the mouse wheel
  567. */
  568. Uint8 button = event->button;
  569. if (button >= sizeof (mouse_buttons) * 8)
  570. return(0);
  571.  
  572. if (button == SDL_BUTTON_RIGHT)
  573. button = SDL_BUTTON_MIDDLE;
  574. else if (button == SDL_BUTTON_MIDDLE)
  575. button = SDL_BUTTON_RIGHT;
  576.  
  577. if (((const SDL_MouseButtonEvent*)event)->state)
  578. mouse_buttons |= 1<<(button-1);
  579. else if (button != 4 && button != 5)
  580. mouse_buttons ^= 1<<(button-1);
  581. #if 0
  582. Uint8 bmask = SDL_GetMouseState(NULL, NULL);
  583. mouse_buttons = 0;
  584. if (bmask & SDL_BUTTON_LMASK) mouse_buttons |= 1;
  585. if (bmask & SDL_BUTTON_RMASK) mouse_buttons |= 2;
  586. if (bmask & SDL_BUTTON_MMASK) mouse_buttons |= 4;
  587. #endif
  588.  
  589. return(0);
  590. } /* sdl_mouse_up_filter */
  591.  
  592.  
  593. static int sdl_mouse_motion_filter(SDL_Event const *event)
  594. {
  595. if (surface == NULL)
  596. return(0);
  597.  
  598. if (event->type == SDL_JOYBALLMOTION)
  599. {
  600. mouse_relative_x = event->jball.xrel/100;
  601. mouse_relative_y = event->jball.yrel/100;
  602. }
  603. else
  604. {
  605. if (SDL_WM_GrabInput(SDL_GRAB_QUERY)==SDL_GRAB_ON)
  606. {
  607. mouse_relative_x += event->motion.xrel;
  608. mouse_relative_y += event->motion.yrel;
  609. //printf("sdl_mouse_motion_filter: mrx=%d, mry=%d, mx=%d, my=%d\n",
  610. // mouse_relative_x, mouse_relative_y, event->motion.xrel, event->motion.yrel);
  611.  
  612. // mouse_relative_* is already reset in
  613. // readmousexy(). It must not be
  614. // reset here because calling this function does not mean
  615. // we always handle the mouse.
  616. // FIX_00001: Mouse speed is uneven and slower in windowed mode vs fullscreen mode.
  617. }
  618. else
  619. mouse_relative_x = mouse_relative_y = 0;
  620. }
  621.  
  622. return(0);
  623. } /* sdl_mouse_motion_filter */
  624.  
  625.  
  626. /*
  627. * The windib driver can't alert us to the keypad enter key, which
  628. * Ken's code depends on heavily. It sends it as the same key as the
  629. * regular return key. These users will have to hit SHIFT-ENTER,
  630. * which we check for explicitly, and give the engine a keypad enter
  631. * enter event.
  632. */
  633. static __inline int handle_keypad_enter_hack(const SDL_Event *event)
  634. {
  635. static int kp_enter_hack = 0;
  636. int retval = 0;
  637.  
  638. if (event->key.keysym.sym == SDLK_RETURN)
  639. {
  640. if (event->key.state == SDL_PRESSED)
  641. {
  642. if (event->key.keysym.mod & KMOD_SHIFT)
  643. {
  644. kp_enter_hack = 1;
  645. lastkey = scancodes[SDLK_KP_ENTER];
  646. retval = 1;
  647. } /* if */
  648. } /* if */
  649.  
  650. else /* key released */
  651. {
  652. if (kp_enter_hack)
  653. {
  654. kp_enter_hack = 0;
  655. lastkey = scancodes[SDLK_KP_ENTER];
  656. retval = 1;
  657. } /* if */
  658. } /* if */
  659. } /* if */
  660.  
  661. return(retval);
  662. } /* handle_keypad_enter_hack */
  663.  
  664. void fullscreen_toggle_and_change_driver(void)
  665. {
  666.  
  667. // FIX_00002: New Toggle Windowed/FullScreen system now simpler and will
  668. // dynamically change for Windib or Directx driver. Windowed/Fullscreen
  669. // toggle also made available from menu.
  670. // Replace attempt_fullscreen_toggle(SDL_Surface **surface, Uint32 *flags)
  671.  
  672. int32_t x,y;
  673. x = surface->w;
  674. y = surface->h;
  675.  
  676. BFullScreen =!BFullScreen;
  677. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  678. _platform_init(0, NULL, "Duke Nukem 3D", "Duke3D");
  679. _setgamemode(ScreenMode,x,y);
  680. //vscrn();
  681.  
  682. return;
  683. }
  684.  
  685. static int sdl_key_filter(const SDL_Event *event)
  686. {
  687. int extended;
  688.  
  689. if ( (event->key.keysym.sym == SDLK_m) &&
  690. (event->key.state == SDL_PRESSED) &&
  691. (event->key.keysym.mod & KMOD_CTRL) )
  692. {
  693.  
  694.  
  695. // FIX_00005: Mouse pointer can be toggled on/off (see mouse menu or use CTRL-M)
  696. // This is usefull to move the duke window when playing in window mode.
  697.  
  698. if (SDL_WM_GrabInput(SDL_GRAB_QUERY)==SDL_GRAB_ON)
  699. {
  700. SDL_WM_GrabInput(SDL_GRAB_OFF);
  701. SDL_ShowCursor(1);
  702. }
  703. else
  704. {
  705. SDL_WM_GrabInput(SDL_GRAB_ON);
  706. SDL_ShowCursor(0);
  707. }
  708.  
  709. return(0);
  710. } /* if */
  711.  
  712. else if ( ( (event->key.keysym.sym == SDLK_RETURN) ||
  713. (event->key.keysym.sym == SDLK_KP_ENTER) ) &&
  714. (event->key.state == SDL_PRESSED) &&
  715. (event->key.keysym.mod & KMOD_ALT) )
  716. { fullscreen_toggle_and_change_driver();
  717.  
  718. // hack to discard the ALT key...
  719. lastkey=scancodes[SDLK_RALT]>>8; // extended
  720. keyhandler();
  721. lastkey=(scancodes[SDLK_RALT]&0xff)+0x80; // Simulating Key up
  722. keyhandler();
  723. lastkey=(scancodes[SDLK_LALT]&0xff)+0x80; // Simulating Key up (not extended)
  724. keyhandler();
  725. SDL_SetModState(KMOD_NONE); // SDL doesnt see we are releasing the ALT-ENTER keys
  726.  
  727. return(0);
  728. }
  729.  
  730. if (!handle_keypad_enter_hack(event))
  731. lastkey = scancodes[event->key.keysym.sym];
  732.  
  733. //printf("key.keysym.sym=%d\n", event->key.keysym.sym);
  734.  
  735. if (lastkey == 0x0000) /* No DOS equivalent defined. */
  736. return(0);
  737.  
  738. extended = ((lastkey & 0xFF00) >> 8);
  739. if (extended != 0)
  740. {
  741. lastkey = extended;
  742. keyhandler();
  743. lastkey = (scancodes[event->key.keysym.sym] & 0xFF);
  744. } /* if */
  745.  
  746.  
  747. //if (event->key.state == SDL_RELEASED)
  748. // lastkey += 128; /* +128 signifies that the key is released in DOS. */
  749.  
  750. if (event->type==SDL_KEYUP) lastkey += 128;
  751.  
  752. keyhandler();
  753. return(0);
  754. } /* sdl_key_filter */
  755.  
  756.  
  757. static int root_sdl_event_filter(const SDL_Event *event)
  758. {
  759. switch (event->type)
  760. {
  761. case SDL_KEYUP:
  762. return(sdl_key_filter(event));
  763. // FIX_00003: Pause mode is now fully responsive - (Thx to Jonathon Fowler tips)
  764. //if(event->key.keysym.sym == SDLK_PAUSE)
  765. //break;
  766. case SDL_KEYDOWN:
  767. return(sdl_key_filter(event));
  768. case SDL_JOYBUTTONDOWN:
  769. case SDL_JOYBUTTONUP:
  770. {
  771. //Do Nothing
  772.  
  773. //printf("Joybutton UP/DOWN\n");
  774. //return(sdl_joystick_button_filter((const SDL_MouseButtonEvent*)event));
  775. return 0;
  776. }
  777. case SDL_JOYBALLMOTION:
  778. case SDL_MOUSEMOTION:
  779. return(sdl_mouse_motion_filter(event));
  780. case SDL_MOUSEBUTTONUP:
  781. case SDL_MOUSEBUTTONDOWN:
  782. return(sdl_mouse_button_filter((const SDL_MouseButtonEvent*)event));
  783. case SDL_QUIT:
  784. /* !!! rcg TEMP */
  785. Error(EXIT_SUCCESS, "Exit through SDL\n");
  786. default:
  787. //printf("This event is not handled: %d\n",event->type);
  788. break;
  789. } /* switch */
  790.  
  791. return(1);
  792. } /* root_sdl_event_filter */
  793.  
  794.  
  795. static void handle_events(void)
  796. {
  797. SDL_Event event;
  798.  
  799. while(SDL_PollEvent(&event))
  800. root_sdl_event_filter(&event);
  801. } /* handle_events */
  802.  
  803.  
  804. /* bleh...public version... */
  805. void _handle_events(void)
  806. {
  807. handle_events();
  808. } /* _handle_events */
  809.  
  810.  
  811. static SDL_Joystick *joystick = NULL;
  812. void _joystick_init(void)
  813. {
  814. const char *envr = getenv(BUILD_SDLJOYSTICK);
  815. int favored = 0;
  816. int numsticks;
  817. int i;
  818.  
  819. if (joystick != NULL)
  820. {
  821. printf("Joystick appears to be already initialized.\n");
  822. printf("...deinitializing for stick redetection...\n");
  823. _joystick_deinit();
  824. } /* if */
  825.  
  826. if ((envr != NULL) && (strcmp(envr, "none") == 0))
  827. {
  828. printf("Skipping joystick detection/initialization at user request\n");
  829. return;
  830. } /* if */
  831.  
  832. printf("Initializing SDL joystick subsystem...");
  833. printf(" (export environment variable BUILD_SDLJOYSTICK=none to skip)\n");
  834.  
  835. if (SDL_Init(SDL_INIT_JOYSTICK|SDL_INIT_NOPARACHUTE) != 0)
  836. {
  837. printf("SDL_Init(SDL_INIT_JOYSTICK) failed: [%s].\n", SDL_GetError());
  838. return;
  839. } /* if */
  840.  
  841. numsticks = SDL_NumJoysticks();
  842. printf("SDL sees %d joystick%s.\n", numsticks, numsticks == 1 ? "" : "s");
  843. if (numsticks == 0)
  844. return;
  845.  
  846. for (i = 0; i < numsticks; i++)
  847. {
  848. const char *stickname = SDL_JoystickName(i);
  849. if ((envr != NULL) && (strcmp(envr, stickname) == 0))
  850. favored = i;
  851.  
  852. printf("Stick #%d: [%s]\n", i, stickname);
  853. } /* for */
  854.  
  855. printf("Using Stick #%d.", favored);
  856. if ((envr == NULL) && (numsticks > 1))
  857. printf("Set BUILD_SDLJOYSTICK to one of the above names to change.\n");
  858.  
  859. joystick = SDL_JoystickOpen(favored);
  860. if (joystick == NULL)
  861. {
  862. printf("Joystick #%d failed to init: %s\n", favored, SDL_GetError());
  863. return;
  864. } /* if */
  865.  
  866. printf("Joystick initialized. %d axes, %d buttons, %d hats, %d balls.\n",
  867. SDL_JoystickNumAxes(joystick), SDL_JoystickNumButtons(joystick),
  868. SDL_JoystickNumHats(joystick), SDL_JoystickNumBalls(joystick));
  869.  
  870. SDL_JoystickEventState(SDL_QUERY);
  871. } /* _joystick_init */
  872.  
  873.  
  874. void _joystick_deinit(void)
  875. {
  876. if (joystick != NULL)
  877. {
  878. printf("Closing joystick device...\n");
  879. SDL_JoystickClose(joystick);
  880. printf("Joystick device closed. Deinitializing SDL subsystem...\n");
  881. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  882. printf("SDL joystick subsystem deinitialized.\n");
  883. joystick = NULL;
  884. } /* if */
  885. } /* _joystick_deinit */
  886.  
  887.  
  888. int _joystick_update(void)
  889. {
  890. if (joystick == NULL)
  891. return(0);
  892.  
  893. SDL_JoystickUpdate();
  894. return(1);
  895. } /* _joystick_update */
  896.  
  897.  
  898. int _joystick_axis(int axis)
  899. {
  900. if (joystick == NULL)
  901. {
  902. return(0);
  903. }
  904.  
  905. return(SDL_JoystickGetAxis(joystick, axis));
  906. } /* _joystick_axis */
  907.  
  908. int _joystick_hat(int hat)
  909. {
  910. if (joystick == NULL)
  911. {
  912. return(-1);
  913. }
  914.  
  915. return(SDL_JoystickGetHat(joystick, hat));
  916. } /* _joystick_axis */
  917.  
  918. int _joystick_button(int button)
  919. {
  920. if (joystick == NULL)
  921. return(0);
  922.  
  923. return(SDL_JoystickGetButton(joystick, button) != 0);
  924. } /* _joystick_button */
  925.  
  926.  
  927. uint8_t _readlastkeyhit(void)
  928. {
  929. return(lastkey);
  930. } /* _readlastkeyhit */
  931.  
  932.  
  933.  
  934. #if (!defined __DATE__)
  935. #define __DATE__ "a long, int32_t time ago"
  936. #endif
  937.  
  938. static void output_sdl_versions(void)
  939. {
  940. const SDL_version *linked_ver = SDL_Linked_Version();
  941. SDL_version compiled_ver;
  942.  
  943. SDL_VERSION(&compiled_ver);
  944.  
  945. printf("SDL display driver for the BUILD engine initializing.\n");
  946. printf(" sdl_driver.c by Ryan C. Gordon (icculus@clutteredmind.org).\n");
  947. printf("Compiled %s against SDL version %d.%d.%d ...\n", __DATE__,
  948. compiled_ver.major, compiled_ver.minor, compiled_ver.patch);
  949. printf("Linked SDL version is %d.%d.%d ...\n",
  950. linked_ver->major, linked_ver->minor, linked_ver->patch);
  951. } /* output_sdl_versions */
  952.  
  953.  
  954. /* lousy -ansi flag. :) */
  955. static char *string_dupe(const char *str)
  956. {
  957. char *retval = malloc(strlen(str) + 1);
  958. if (retval != NULL)
  959. strcpy(retval, str);
  960. return(retval);
  961. } /* string_dupe */
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969. void _platform_init(int argc, char **argv, const char *title, const char *iconName)
  970. {
  971. int i;
  972. int64_t timeElapsed;
  973. char dummyString[4096];
  974.  
  975. // FIX_00061: "ERROR: Two players have the same random ID" too frequent cuz of internet windows times
  976. TIMER_GetPlatformTicks(&timeElapsed);
  977. srand(timeElapsed&0xFFFFFFFF);
  978.  
  979. Setup_UnstableNetworking();
  980.  
  981. // Look through the command line args
  982. for(i = 0; i < argc; i++)
  983. {
  984. if(argv[i][0] == '-' )
  985. {
  986. if(strcmpi(argv[i], "-netmode_stable") == 0)
  987. {
  988. //fullscreen = 1;
  989. //TODO:
  990. //TODO ( "[Todo: handle -netmode <int>]" )
  991. Setup_StableNetworking();
  992.  
  993. }
  994. }
  995. }
  996.  
  997.  
  998.  
  999. #ifdef __APPLE__
  1000. SDL_putenv("SDL_VIDEODRIVER=Quartz");
  1001. #endif
  1002.  
  1003.  
  1004. if (SDL_Init(SDL_INIT_VIDEO) == -1){
  1005. Error(EXIT_FAILURE, "BUILDSDL: SDL_Init() failed!\nBUILDSDL: SDL_GetError() says \"%s\".\n", SDL_GetError());
  1006. }
  1007.  
  1008.  
  1009. // Set up the correct renderer
  1010. // Becarfull setenv can't reach dll in VC++
  1011. // A way to proceed is to integrate the SDL libs
  1012. // in the exe instead.
  1013.  
  1014. // FIX_00004: SDL.dll and SDL_Mixer.dll are now integrated within the exe
  1015. // (this also makes the Windib/Directx driver switching easier with SDL)
  1016.  
  1017. // This requires to recompile the whole sdl and sdl mixer with the lib
  1018. // switch instead of the default dll switch.
  1019.  
  1020. putenv("SDL_VIDEO_CENTERED=1");
  1021.  
  1022. if (title == NULL)
  1023. title = "BUILD";
  1024.  
  1025. if (iconName == NULL)
  1026. iconName = "BUILD";
  1027.  
  1028. titleNameLong = string_dupe(title);
  1029. titleNameShort = string_dupe(iconName);
  1030.  
  1031. sdl_flags = BFullScreen ? SDL_FULLSCREEN : 0;
  1032.  
  1033. sdl_flags |= SDL_HWPALETTE;
  1034.  
  1035.  
  1036. memset(scancodes, '\0', sizeof (scancodes));
  1037. scancodes[SDLK_ESCAPE] = 0x01;
  1038. scancodes[SDLK_1] = 0x02;
  1039. scancodes[SDLK_2] = 0x03;
  1040. scancodes[SDLK_3] = 0x04;
  1041. scancodes[SDLK_4] = 0x05;
  1042. scancodes[SDLK_5] = 0x06;
  1043. scancodes[SDLK_6] = 0x07;
  1044. scancodes[SDLK_7] = 0x08;
  1045. scancodes[SDLK_8] = 0x09;
  1046. scancodes[SDLK_9] = 0x0A;
  1047. scancodes[SDLK_0] = 0x0B;
  1048. scancodes[SDLK_MINUS] = 0x0C; /* was 0x4A */
  1049. scancodes[SDLK_EQUALS] = 0x0D; /* was 0x4E */
  1050. scancodes[SDLK_BACKSPACE] = 0x0E;
  1051. scancodes[SDLK_TAB] = 0x0F;
  1052. scancodes[SDLK_q] = 0x10;
  1053. scancodes[SDLK_w] = 0x11;
  1054. scancodes[SDLK_e] = 0x12;
  1055. scancodes[SDLK_r] = 0x13;
  1056. scancodes[SDLK_t] = 0x14;
  1057. scancodes[SDLK_y] = 0x15;
  1058. scancodes[SDLK_u] = 0x16;
  1059. scancodes[SDLK_i] = 0x17;
  1060. scancodes[SDLK_o] = 0x18;
  1061. scancodes[SDLK_p] = 0x19;
  1062. scancodes[SDLK_LEFTBRACKET] = 0x1A;
  1063. scancodes[SDLK_RIGHTBRACKET] = 0x1B;
  1064. scancodes[SDLK_RETURN] = 0x1C;
  1065. scancodes[SDLK_LCTRL] = 0x1D;
  1066. scancodes[SDLK_a] = 0x1E;
  1067. scancodes[SDLK_s] = 0x1F;
  1068. scancodes[SDLK_d] = 0x20;
  1069. scancodes[SDLK_f] = 0x21;
  1070. scancodes[SDLK_g] = 0x22;
  1071. scancodes[SDLK_h] = 0x23;
  1072. scancodes[SDLK_j] = 0x24;
  1073. scancodes[SDLK_k] = 0x25;
  1074. scancodes[SDLK_l] = 0x26;
  1075. scancodes[SDLK_SEMICOLON] = 0x27;
  1076. scancodes[SDLK_QUOTE] = 0x28;
  1077. scancodes[SDLK_BACKQUOTE] = 0x29;
  1078. scancodes[SDLK_LSHIFT] = 0x2A;
  1079. scancodes[SDLK_BACKSLASH] = 0x2B;
  1080. scancodes[SDLK_z] = 0x2C;
  1081. scancodes[SDLK_x] = 0x2D;
  1082. scancodes[SDLK_c] = 0x2E;
  1083. scancodes[SDLK_v] = 0x2F;
  1084. scancodes[SDLK_b] = 0x30;
  1085. scancodes[SDLK_n] = 0x31;
  1086. scancodes[SDLK_m] = 0x32;
  1087. scancodes[SDLK_COMMA] = 0x33;
  1088. scancodes[SDLK_PERIOD] = 0x34;
  1089. scancodes[SDLK_SLASH] = 0x35;
  1090. scancodes[SDLK_RSHIFT] = 0x36;
  1091. scancodes[SDLK_KP_MULTIPLY] = 0x37;
  1092. scancodes[SDLK_LALT] = 0x38;
  1093. scancodes[SDLK_SPACE] = 0x39;
  1094. scancodes[SDLK_CAPSLOCK] = 0x3A;
  1095. scancodes[SDLK_F1] = 0x3B;
  1096. scancodes[SDLK_F2] = 0x3C;
  1097. scancodes[SDLK_F3] = 0x3D;
  1098. scancodes[SDLK_F4] = 0x3E;
  1099. scancodes[SDLK_F5] = 0x3F;
  1100. scancodes[SDLK_F6] = 0x40;
  1101. scancodes[SDLK_F7] = 0x41;
  1102. scancodes[SDLK_F8] = 0x42;
  1103. scancodes[SDLK_F9] = 0x43;
  1104. scancodes[SDLK_F10] = 0x44;
  1105. scancodes[SDLK_NUMLOCK] = 0x45;
  1106. scancodes[SDLK_SCROLLOCK] = 0x46;
  1107. scancodes[SDLK_KP7] = 0x47;
  1108. scancodes[SDLK_KP8] = 0x48;
  1109. scancodes[SDLK_KP9] = 0x49;
  1110. scancodes[SDLK_KP_MINUS] = 0x4A;
  1111. scancodes[SDLK_KP4] = 0x4B;
  1112. scancodes[SDLK_KP5] = 0x4C;
  1113. scancodes[SDLK_KP6] = 0x4D;
  1114. scancodes[SDLK_KP_PLUS] = 0x4E;
  1115. scancodes[SDLK_KP1] = 0x4F;
  1116. scancodes[SDLK_KP2] = 0x50;
  1117. scancodes[SDLK_KP3] = 0x51;
  1118. scancodes[SDLK_KP0] = 0x52;
  1119. scancodes[SDLK_KP_PERIOD] = 0x53;
  1120. scancodes[SDLK_F11] = 0x57;
  1121. scancodes[SDLK_F12] = 0x58;
  1122. scancodes[SDLK_PAUSE] = 0x59; /* SBF - technically incorrect */
  1123.  
  1124. scancodes[SDLK_KP_ENTER] = 0xE01C;
  1125. scancodes[SDLK_RCTRL] = 0xE01D;
  1126. scancodes[SDLK_KP_DIVIDE] = 0xE035;
  1127. scancodes[SDLK_PRINT] = 0xE037; /* SBF - technically incorrect */
  1128. scancodes[SDLK_SYSREQ] = 0xE037; /* SBF - for windows... */
  1129. scancodes[SDLK_RALT] = 0xE038;
  1130. scancodes[SDLK_HOME] = 0xE047;
  1131. scancodes[SDLK_UP] = 0xE048;
  1132. scancodes[SDLK_PAGEUP] = 0xE049;
  1133. scancodes[SDLK_LEFT] = 0xE04B;
  1134. scancodes[SDLK_RIGHT] = 0xE04D;
  1135. scancodes[SDLK_END] = 0xE04F;
  1136. scancodes[SDLK_DOWN] = 0xE050;
  1137. scancodes[SDLK_PAGEDOWN] = 0xE051;
  1138. scancodes[SDLK_INSERT] = 0xE052;
  1139. scancodes[SDLK_DELETE] = 0xE053;
  1140.  
  1141.  
  1142.  
  1143. output_sdl_versions();
  1144. output_driver_info();
  1145.  
  1146.  
  1147. printf("Video Driver: '%s'.\n", SDL_VideoDriverName(dummyString, 20));
  1148.  
  1149. char device[] ="/dev/input/event3"; //our device
  1150. if ((controlsfd = open (device, O_RDONLY)) == -1)
  1151. printf ("%s is not a vaild device.n", device);
  1152.  
  1153. }
  1154.  
  1155. // Capture BMP of the current frame
  1156. int screencapture(char *filename, uint8_t inverseit)
  1157. {
  1158. // FIX_00006: better naming system for screenshots + message when pic is taken.
  1159. // Use ./screenshots folder. Screenshot code rerwritten. Faster and
  1160. // makes smaller files. Doesn't freeze or lag the game anymore.
  1161.  
  1162. SDL_SaveBMP(surface, filename);
  1163. return 0;
  1164. } /* screencapture */
  1165.  
  1166.  
  1167. void setvmode(int mode)
  1168. {
  1169.  
  1170. if (mode == 0x3) /* text mode. */
  1171. {
  1172. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  1173. return;
  1174. } else
  1175. printf("setvmode(0x%x) is unsupported in SDL driver.\n", mode);
  1176.  
  1177. }
  1178.  
  1179. int _setgamemode(uint8_t davidoption, int32_t daxdim, int32_t daydim)
  1180. {
  1181. int validated, i;
  1182. SDL_Surface *image;
  1183. Uint32 colorkey;
  1184.  
  1185. // don't override higher-res app icon on OS X
  1186. #ifndef PLATFORM_MACOSX
  1187. // Install icon
  1188. image = SDL_LoadBMP_RW(SDL_RWFromMem(iconBMP, sizeof(iconBMP)), 1);
  1189. colorkey = 0; // index in this image to be transparent
  1190. SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
  1191. SDL_WM_SetIcon(image,NULL);
  1192. #endif
  1193.  
  1194. if (daxdim > MAXXDIM || daydim > MAXYDIM)
  1195. {
  1196. printf("Resolution %dx%d is too high. Changed to %dx%d\n", daxdim, daydim, MAXXDIM,MAXYDIM);
  1197. daxdim = MAXXDIM;
  1198. daydim = MAXYDIM;
  1199. }
  1200.  
  1201. getvalidvesamodes();
  1202.  
  1203. validated = 0;
  1204. for(i=0; i<validmodecnt; i++){
  1205. if(validmodexdim[i] == daxdim && validmodeydim[i] == daydim)
  1206. validated = 1;
  1207. }
  1208.  
  1209. if(!validated){
  1210. printf("Resolution %dx%d unsupported. Changed to 640x480\n", daxdim, daydim);
  1211. daxdim = 640;
  1212. daydim = 480;
  1213. }
  1214.  
  1215. go_to_new_vid_mode((int) davidoption, daxdim, daydim);
  1216.  
  1217. qsetmode = 200;
  1218. last_render_ticks = getticks();
  1219.  
  1220. return(0);
  1221. } /* setgamemode */
  1222.  
  1223.  
  1224. static int get_dimensions_from_str(const char *str, int32_t *_w, int32_t *_h)
  1225. {
  1226. char *xptr = NULL;
  1227. char *ptr = NULL;
  1228. int32_t w = -1;
  1229. int32_t h = -1;
  1230.  
  1231. if (str == NULL)
  1232. return(0);
  1233.  
  1234. xptr = strchr(str, 'x');
  1235. if (xptr == NULL)
  1236. return(0);
  1237.  
  1238. w = strtol(str, &ptr, 10);
  1239. if (ptr != xptr)
  1240. return(0);
  1241.  
  1242. xptr++;
  1243. h = strtol(xptr, &ptr, 10);
  1244. if ( (*xptr == '\0') || (*ptr != '\0') )
  1245. return(0);
  1246.  
  1247. if ((w <= 1) || (h <= 1))
  1248. return(0);
  1249.  
  1250. if (_w != NULL)
  1251. *_w = w;
  1252.  
  1253. if (_h != NULL)
  1254. *_h = h;
  1255.  
  1256. return(1);
  1257. } /* get_dimensions_from_str */
  1258.  
  1259.  
  1260. static __inline void get_max_screen_res(int32_t *max_w, int32_t *max_h)
  1261. {
  1262. int32_t w = DEFAULT_MAXRESWIDTH;
  1263. int32_t h = DEFAULT_MAXRESHEIGHT;
  1264. const char *envr = getenv(BUILD_MAXSCREENRES);
  1265.  
  1266. if (envr != NULL)
  1267. {
  1268. if (!get_dimensions_from_str(envr, &w, &h))
  1269. {
  1270. printf("User's resolution ceiling [%s] is bogus!\n", envr);
  1271. w = DEFAULT_MAXRESWIDTH;
  1272. h = DEFAULT_MAXRESHEIGHT;
  1273. } /* if */
  1274. } /* if */
  1275.  
  1276. if (max_w != NULL)
  1277. *max_w = w;
  1278.  
  1279. if (max_h != NULL)
  1280. *max_h = h;
  1281. }
  1282.  
  1283.  
  1284. static void add_vesa_mode(const char *typestr, int w, int h)
  1285. {
  1286. //printf("Adding %s resolution (%dx%d).\n", typestr, w, h);
  1287. validmode[validmodecnt] = validmodecnt;
  1288. validmodexdim[validmodecnt] = w;
  1289. validmodeydim[validmodecnt] = h;
  1290. validmodecnt++;
  1291. } /* add_vesa_mode */
  1292.  
  1293.  
  1294. /* Let the user specify a specific mode via environment variable. */
  1295. static __inline void add_user_defined_resolution(void)
  1296. {
  1297. int32_t w;
  1298. int32_t h;
  1299. const char *envr = getenv(BUILD_USERSCREENRES);
  1300.  
  1301. if (envr == NULL)
  1302. return;
  1303.  
  1304. if (get_dimensions_from_str(envr, &w, &h))
  1305. add_vesa_mode("user defined", w, h);
  1306. else
  1307. printf("User defined resolution [%s] is bogus!\n", envr);
  1308. } /* add_user_defined_resolution */
  1309.  
  1310.  
  1311. static __inline SDL_Rect **get_physical_resolutions(void)
  1312. {
  1313. const SDL_VideoInfo *vidInfo = SDL_GetVideoInfo();
  1314. SDL_Rect **modes = SDL_ListModes(vidInfo->vfmt, sdl_flags | SDL_FULLSCREEN);
  1315. if (modes == NULL)
  1316. {
  1317. sdl_flags &= ~SDL_FULLSCREEN;
  1318. modes = SDL_ListModes(vidInfo->vfmt, sdl_flags); /* try without fullscreen. */
  1319. if (modes == NULL)
  1320. modes = (SDL_Rect **) -1; /* fuck it. */
  1321. } /* if */
  1322.  
  1323. if (modes == (SDL_Rect **) -1)
  1324. printf("Couldn't get any physical resolutions.\n");
  1325. else
  1326. {
  1327. printf("Highest physical resolution is (%dx%d).\n",
  1328. modes[0]->w, modes[0]->h);
  1329. } /* else */
  1330.  
  1331. return(modes);
  1332. } /* get_physical_resolutions */
  1333.  
  1334.  
  1335. static void remove_vesa_mode(int index, const char *reason)
  1336. {
  1337. int i;
  1338.  
  1339. assert(index < validmodecnt);
  1340. //printf("Removing resolution #%d, %dx%d [%s].\n",index, validmodexdim[index], validmodeydim[index], reason);
  1341.  
  1342. for (i = index; i < validmodecnt - 1; i++)
  1343. {
  1344. validmode[i] = validmode[i + 1];
  1345. validmodexdim[i] = validmodexdim[i + 1];
  1346. validmodeydim[i] = validmodeydim[i + 1];
  1347. } /* for */
  1348.  
  1349. validmodecnt--;
  1350. } /* remove_vesa_mode */
  1351.  
  1352.  
  1353. static __inline void cull_large_vesa_modes(void)
  1354. {
  1355. int32_t max_w;
  1356. int32_t max_h;
  1357. int i;
  1358.  
  1359. get_max_screen_res(&max_w, &max_h);
  1360. printf("Setting resolution ceiling to (%dx%d).\n", max_w, max_h);
  1361.  
  1362. for (i = 0; i < validmodecnt; i++)
  1363. {
  1364. if ((validmodexdim[i] > max_w) || (validmodeydim[i] > max_h))
  1365. {
  1366. remove_vesa_mode(i, "above resolution ceiling");
  1367. i--; /* list shrinks. */
  1368. } /* if */
  1369. } /* for */
  1370. } /* cull_large_vesa_modes */
  1371.  
  1372.  
  1373. static __inline void cull_duplicate_vesa_modes(void)
  1374. {
  1375. int i;
  1376. int j;
  1377.  
  1378. for (i = 0; i < validmodecnt; i++){
  1379. for (j = i + 1; j < validmodecnt; j++){
  1380. if ( (validmodexdim[i] == validmodexdim[j]) &&(validmodeydim[i] == validmodeydim[j]) ){
  1381. remove_vesa_mode(j, "duplicate");
  1382. j--; /* list shrinks. */
  1383. }
  1384. }
  1385. }
  1386. }
  1387.  
  1388.  
  1389. #define swap_macro(tmp, x, y) { tmp = x; x = y; y = tmp; }
  1390.  
  1391. /* be sure to call cull_duplicate_vesa_modes() before calling this. */
  1392. static __inline void sort_vesa_modelist(void)
  1393. {
  1394. int i;
  1395. int sorted;
  1396. int32_t tmp;
  1397.  
  1398. do
  1399. {
  1400. sorted = 1;
  1401. for (i = 0; i < validmodecnt - 1; i++)
  1402. {
  1403. if ( (validmodexdim[i] >= validmodexdim[i+1]) &&
  1404. (validmodeydim[i] >= validmodeydim[i+1]) )
  1405. {
  1406. sorted = 0;
  1407. swap_macro(tmp, validmode[i], validmode[i+1]);
  1408. swap_macro(tmp, validmodexdim[i], validmodexdim[i+1]);
  1409. swap_macro(tmp, validmodeydim[i], validmodeydim[i+1]);
  1410. } /* if */
  1411. } /* for */
  1412. } while (!sorted);
  1413. } /* sort_vesa_modelist */
  1414.  
  1415.  
  1416. static __inline void cleanup_vesa_modelist(void)
  1417. {
  1418. cull_large_vesa_modes();
  1419. cull_duplicate_vesa_modes();
  1420. sort_vesa_modelist();
  1421. } /* cleanup_vesa_modelist */
  1422.  
  1423.  
  1424. static __inline void output_vesa_modelist(void)
  1425. {
  1426. char buffer[256];
  1427. char numbuf[20];
  1428. int i;
  1429.  
  1430. buffer[0] = '\0';
  1431.  
  1432. for (i = 0; i < validmodecnt; i++)
  1433. {
  1434. sprintf(numbuf, " (%dx%d)",(int32_t) validmodexdim[i], (int32_t) validmodeydim[i]);
  1435.  
  1436. if ( (strlen(buffer) + strlen(numbuf)) >= (sizeof (buffer) - 1) )
  1437. strcpy(buffer + (sizeof (buffer) - 5), " ...");
  1438. else
  1439. strcat(buffer, numbuf);
  1440. } /* for */
  1441.  
  1442. printf("Final sorted modelist:%s", buffer);
  1443. }
  1444.  
  1445.  
  1446. void getvalidvesamodes(void)
  1447. {
  1448. static int already_checked = 0;
  1449. int i;
  1450. SDL_Rect **modes = NULL;
  1451. int stdres[][2] = {
  1452. {320, 200}, {640, 350}, {640, 480},
  1453. {800, 600}, {1024, 768}
  1454. };
  1455.  
  1456. if (already_checked)
  1457. return;
  1458.  
  1459. already_checked = 1;
  1460. validmodecnt = 0;
  1461. vidoption = 1; /* !!! tmp */
  1462.  
  1463. /* fill in the standard resolutions... */
  1464. for (i = 0; i < sizeof (stdres) / sizeof (stdres[0]); i++)
  1465. add_vesa_mode("standard", stdres[i][0], stdres[i][1]);
  1466.  
  1467. /* Anything the hardware can specifically do is added now... */
  1468. modes = get_physical_resolutions();
  1469. for (i = 0; (modes != (SDL_Rect **) -1) && (modes[i] != NULL); i++)
  1470. add_vesa_mode("physical", modes[i]->w, modes[i]->h);
  1471.  
  1472. /* Now add specific resolutions that the user wants... */
  1473. add_user_defined_resolution();
  1474.  
  1475. /* get rid of dupes and bogus resolutions... */
  1476. cleanup_vesa_modelist();
  1477.  
  1478. /* print it out for debugging purposes... */
  1479. output_vesa_modelist();
  1480. }
  1481.  
  1482. uint8_t lastPalette[768];
  1483. void WriteTranslucToFile(void){
  1484.  
  1485. uint8_t buffer[65535*4];
  1486. uint8_t tga_header[18];
  1487. uint8_t* transPointer = transluc;
  1488. uint8_t* bufferPointer = buffer;
  1489. int i;
  1490. FILE* file;
  1491.  
  1492. for (i=0; i < 65535; i++) {
  1493.  
  1494. bufferPointer[0] = (lastPalette[(*transPointer)*3+0]) / 63.0 * 255;
  1495. bufferPointer[1] = (lastPalette[(*transPointer)*3+1]) / 63.0 * 255;
  1496. bufferPointer[2] = (lastPalette[(*transPointer)*3+2]) / 63.0 * 255;
  1497. bufferPointer[3] = 255;
  1498.  
  1499. printf("%d,",*transPointer);
  1500. if (i%255 ==0)
  1501. printf("\n");
  1502.  
  1503. transPointer +=1;
  1504. bufferPointer+=4;
  1505. }
  1506.  
  1507.  
  1508.  
  1509. file = fopen("transluc.tga", "w");
  1510.  
  1511. memset(tga_header, 0, 18);
  1512. tga_header[2] = 2;
  1513. tga_header[12] = (256 & 0x00FF);
  1514. tga_header[13] = (256 & 0xFF00) / 256;
  1515. tga_header[14] = (256 & 0x00FF) ;
  1516. tga_header[15] =(256 & 0xFF00) / 256;
  1517. tga_header[16] = 32 ;
  1518.  
  1519. fwrite(&tga_header, 18, sizeof(uint8_t), file);
  1520. fwrite(buffer, 65535, 4, file);
  1521. fclose(file);
  1522. }
  1523.  
  1524. void WritePaletteToFile(uint8_t* palette,const char* filename,int width, int height){
  1525.  
  1526. uint8_t tga_header[18];
  1527. uint8_t* buffer;
  1528. uint8_t* palettePointer = palette;
  1529. uint8_t* bufferPointer ;
  1530. int i;
  1531.  
  1532. FILE* file = fopen(filename, "w");
  1533.  
  1534.  
  1535. memset(tga_header, 0, 18);
  1536. tga_header[2] = 2;
  1537. tga_header[12] = (width & 0x00FF);
  1538. tga_header[13] = (width & 0xFF00) / 256;
  1539. tga_header[14] = (height & 0x00FF) ;
  1540. tga_header[15] =(height & 0xFF00) / 256;
  1541. tga_header[16] = 32 ;
  1542.  
  1543. fwrite(&tga_header, 18, sizeof(uint8_t), file);
  1544.  
  1545. bufferPointer = buffer = malloc(width*height*4);
  1546.  
  1547. for (i = 0 ; i < width*height ; i++)
  1548. {
  1549. bufferPointer[0] = palettePointer[0] / 63.0 * 255;
  1550. bufferPointer[1] = palettePointer[1] / 63.0 * 255;
  1551. bufferPointer[2] = palettePointer[2] / 63.0 * 255;
  1552. bufferPointer[3] = 255;
  1553.  
  1554. bufferPointer += 4;
  1555. palettePointer+= 3;
  1556. }
  1557.  
  1558. fwrite(buffer, width*height, 4, file);
  1559. fclose(file);
  1560.  
  1561. free(buffer);
  1562. }
  1563.  
  1564.  
  1565. void WriteLastPaletteToFile(){
  1566. WritePaletteToFile(lastPalette,"lastPalette.tga",16,16);
  1567. }
  1568.  
  1569. int VBE_setPalette(uint8_t *palettebuffer)
  1570. /*
  1571. * (From Ken's docs:)
  1572. * Set (num) palette palette entries starting at (start)
  1573. * palette entries are in a 4-byte format in this order:
  1574. * 0: Blue (0-63)
  1575. * 1: Green (0-63)
  1576. * 2: Red (0-63)
  1577. * 3: Reserved
  1578. *
  1579. * Naturally, the bytes are in the reverse order that SDL wants them...
  1580. * More importantly, SDL wants the color elements in a range from 0-255,
  1581. * so we do a conversion.
  1582. */
  1583. {
  1584. SDL_Color fmt_swap[256];
  1585. SDL_Color *sdlp = fmt_swap;
  1586. uint8_t *p = palettebuffer;
  1587. int i;
  1588. //static updated=0;
  1589.  
  1590. //if (updated >=1 )
  1591. // return ;
  1592.  
  1593. //WritePaletteToFile(palettebuffer,"lastPalette.tga",16,16);
  1594. //updated++;
  1595.  
  1596.  
  1597. //CODE EXPLORATION
  1598. //Used only to write the last palette to file.
  1599. memcpy(lastPalette, palettebuffer, 768);
  1600.  
  1601. for (i = 0; i < 256; i++){
  1602. sdlp->b = (Uint8) ((((float) *p++) / 63.0) * 255.0);
  1603. sdlp->g = (Uint8) ((((float) *p++) / 63.0) * 255.0);
  1604. sdlp->r = (Uint8) ((((float) *p++) / 63.0) * 255.0);
  1605. sdlp->unused = *p++; /* This byte is unused in BUILD, too. */
  1606. sdlp++;
  1607. }
  1608.  
  1609. return(SDL_SetColors(surface, fmt_swap, 0, 256));
  1610. }
  1611.  
  1612.  
  1613. int VBE_getPalette(int32_t start, int32_t num, uint8_t *palettebuffer)
  1614. {
  1615. SDL_Color *sdlp = surface->format->palette->colors + start;
  1616. uint8_t *p = palettebuffer + (start * 4);
  1617. int i;
  1618.  
  1619. for (i = 0; i < num; i++)
  1620. {
  1621. *p++ = (Uint8) ((((float) sdlp->b) / 255.0) * 63.0);
  1622. *p++ = (Uint8) ((((float) sdlp->g) / 255.0) * 63.0);
  1623. *p++ = (Uint8) ((((float) sdlp->r) / 255.0) * 63.0);
  1624. *p++ = sdlp->unused; /* This byte is unused in both SDL and BUILD. */
  1625. sdlp++;
  1626. }
  1627.  
  1628. return(1);
  1629. }
  1630.  
  1631.  
  1632. void _uninitengine(void)
  1633. {
  1634. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  1635.  
  1636. if (controlsfd != -1) close (controlsfd);//mick close our controls fd
  1637. } /* _uninitengine */
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644. int setupmouse(void)
  1645. {
  1646.  
  1647. SDL_Event event;
  1648.  
  1649. if (surface == NULL)
  1650. return(0);
  1651.  
  1652. SDL_WM_GrabInput(SDL_GRAB_ON);
  1653. SDL_ShowCursor(0);
  1654.  
  1655. mouse_relative_x = mouse_relative_y = 0;
  1656.  
  1657. /*
  1658. * this global usually gets set by BUILD, but it's a one-shot
  1659. * deal, and we may not have an SDL surface at that point. --ryan.
  1660. */
  1661. moustat = 1;
  1662.  
  1663. // FIX_00063: Duke's angle changing or incorrect when using toggle fullscreen/window mode
  1664. while(SDL_PollEvent(&event)); // Empying the various pending events (especially the mouse one)
  1665.  
  1666. //SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  1667.  
  1668. return(1);
  1669. } /* setupmouse */
  1670.  
  1671.  
  1672. void readmousexy(short *x, short *y)
  1673. {
  1674. if (x)
  1675. *x = mouse_relative_x << 2;
  1676. if (y)
  1677. *y = mouse_relative_y << 2;
  1678.  
  1679. mouse_relative_x = mouse_relative_y = 0;
  1680. } /* readmousexy */
  1681.  
  1682.  
  1683. void readmousebstatus(short *bstatus)
  1684. {
  1685. if (bstatus)
  1686. *bstatus = mouse_buttons;
  1687.  
  1688. // special wheel treatment: make it like a button click
  1689. if(mouse_buttons&8)
  1690. mouse_buttons ^= 8;
  1691. if(mouse_buttons&16)
  1692. mouse_buttons ^= 16;
  1693.  
  1694. } /* readmousebstatus */
  1695.  
  1696.  
  1697. void _updateScreenRect(int32_t x, int32_t y, int32_t w, int32_t h)
  1698. {
  1699. SDL_UpdateRect(surface, x, y, w, h);
  1700. }
  1701.  
  1702. //int counter= 0 ;
  1703. //char bmpName[256];
  1704. void _nextpage(void)
  1705.  
  1706. {
  1707. Uint32 ticks;
  1708. ReadInputs();
  1709. _handle_events();
  1710.  
  1711.  
  1712. SDL_UpdateRect(surface, 0, 0, 0, 0);
  1713.  
  1714. //sprintf(bmpName,"%d.bmp",counter++);
  1715. //SDL_SaveBMP(surface,bmpName);
  1716.  
  1717.  
  1718. //if (CLEAR_FRAMEBUFFER)
  1719. // SDL_FillRect(surface,NULL,0);
  1720.  
  1721. ticks = getticks();
  1722. total_render_time = (ticks - last_render_ticks);
  1723. if (total_render_time > 1000){
  1724. total_rendered_frames = 0;
  1725. total_render_time = 1;
  1726. last_render_ticks = ticks;
  1727. }
  1728. total_rendered_frames++;
  1729. }
  1730.  
  1731.  
  1732. uint8_t readpixel(uint8_t * offset)
  1733. {
  1734. return *offset;
  1735. }
  1736.  
  1737. void drawpixel(uint8_t * location, uint8_t pixel)
  1738. {
  1739. *location = pixel;
  1740. }
  1741.  
  1742. /* Fix this up The Right Way (TM) - DDOI */
  1743. void setcolor16(uint8_t col)
  1744. {
  1745. drawpixel_color = col;
  1746. }
  1747.  
  1748. void drawpixel16(int32_t offset)
  1749. {
  1750. drawpixel((uint8_t*)surface->pixels + offset, drawpixel_color);
  1751. } /* drawpixel16 */
  1752.  
  1753.  
  1754. void fillscreen16(int32_t offset, int32_t color, int32_t blocksize)
  1755. {
  1756. Uint8 *surface_end;
  1757. Uint8 *wanted_end;
  1758. Uint8 *pixels;
  1759.  
  1760. if (SDL_MUSTLOCK(surface))
  1761. SDL_LockSurface(surface);
  1762.  
  1763. pixels = get_framebuffer();
  1764.  
  1765. /* Make this function pageoffset aware - DDOI */
  1766. if (!pageoffset) {
  1767. offset = offset << 3;
  1768. offset += 640*336;
  1769. }
  1770.  
  1771. surface_end = (pixels + (surface->w * surface->h)) - 1;
  1772. wanted_end = (pixels + offset) + blocksize;
  1773.  
  1774. if (offset < 0)
  1775. offset = 0;
  1776.  
  1777. if (wanted_end > surface_end)
  1778. blocksize = ((uint32_t) surface_end) - ((uint32_t) pixels + offset);
  1779.  
  1780. memset(pixels + offset, (int) color, blocksize);
  1781.  
  1782. if (SDL_MUSTLOCK(surface))
  1783. SDL_UnlockSurface(surface);
  1784.  
  1785. _nextpage();
  1786. } /* fillscreen16 */
  1787.  
  1788.  
  1789. /* Most of this line code is taken from Abrash's "Graphics Programming Blackbook".
  1790. Remember, sharing code is A Good Thing. AH */
  1791. static __inline void DrawHorizontalRun (uint8_t **ScreenPtr, int XAdvance, int RunLength, uint8_t Color)
  1792. {
  1793. int i;
  1794. uint8_t *WorkingScreenPtr = *ScreenPtr;
  1795.  
  1796. for (i=0; i<RunLength; i++)
  1797. {
  1798. *WorkingScreenPtr = Color;
  1799. WorkingScreenPtr += XAdvance;
  1800. }
  1801. WorkingScreenPtr += surface->w;
  1802. *ScreenPtr = WorkingScreenPtr;
  1803. }
  1804.  
  1805. static __inline void DrawVerticalRun (uint8_t **ScreenPtr, int XAdvance, int RunLength, uint8_t Color)
  1806. {
  1807. int i;
  1808. uint8_t *WorkingScreenPtr = *ScreenPtr;
  1809.  
  1810. for (i=0; i<RunLength; i++)
  1811. {
  1812. *WorkingScreenPtr = Color;
  1813. WorkingScreenPtr += surface->w;
  1814. }
  1815. WorkingScreenPtr += XAdvance;
  1816. *ScreenPtr = WorkingScreenPtr;
  1817. }
  1818.  
  1819. void drawline16(int32_t XStart, int32_t YStart, int32_t XEnd, int32_t YEnd, uint8_t Color)
  1820. {
  1821. int Temp, AdjUp, AdjDown, ErrorTerm, XAdvance, XDelta, YDelta;
  1822. int WholeStep, InitialPixelCount, FinalPixelCount, i, RunLength;
  1823. uint8_t *ScreenPtr;
  1824. int32_t dx, dy;
  1825.  
  1826. if (SDL_MUSTLOCK(surface))
  1827. SDL_LockSurface(surface);
  1828.  
  1829. dx = XEnd-XStart;
  1830. dy = YEnd-YStart;
  1831.  
  1832. //Analyse the slope
  1833. if (dx >= 0)
  1834. {
  1835. if ((XStart > 639) || (XEnd < 0)) return;
  1836. if (XStart < 0) { if (dy) YStart += scale(0-XStart,dy,dx); XStart = 0; }
  1837. if (XEnd > 639) { if (dy) YEnd += scale(639-XEnd,dy,dx); XEnd = 639; }
  1838. }
  1839. else
  1840. {
  1841. if ((XEnd > 639) || (XStart < 0)) return;
  1842. if (XEnd < 0) { if (dy) YEnd += scale(0-XEnd,dy,dx); XEnd = 0; }
  1843. if (XStart > 639) { if (dy) YStart += scale(639-XStart,dy,dx); XStart = 639; }
  1844. }
  1845. if (dy >= 0)
  1846. {
  1847. if ((YStart >= ydim16) || (YEnd < 0)) return;
  1848. if (YStart < 0) { if (dx) XStart += scale(0-YStart,dx,dy); YStart = 0; }
  1849. if (YEnd >= ydim16) { if (dx) XEnd += scale(ydim16-1-YEnd,dx,dy); YEnd = ydim16-1; }
  1850. }
  1851. else
  1852. {
  1853. if ((YEnd >= ydim16) || (YStart < 0)) return;
  1854. if (YEnd < 0) { if (dx) XEnd += scale(0-YEnd,dx,dy); YEnd = 0; }
  1855. if (YStart >= ydim16) { if (dx) XStart += scale(ydim16-1-YStart,dx,dy); YStart = ydim16-1; }
  1856. }
  1857.  
  1858. /* Make sure the status bar border draws correctly - DDOI */
  1859. if (!pageoffset) { YStart += 336; YEnd += 336; }
  1860.  
  1861. /* We'll always draw top to bottom */
  1862. if (YStart > YEnd) {
  1863. Temp = YStart;
  1864. YStart = YEnd;
  1865. YEnd = Temp;
  1866. Temp = XStart;
  1867. XStart = XEnd;
  1868. XEnd = Temp;
  1869. }
  1870.  
  1871. /* Point to the bitmap address first pixel to draw */
  1872. ScreenPtr = (uint8_t *) (get_framebuffer()) + XStart + (surface->w * YStart);
  1873.  
  1874. /* Figure out whether we're going left or right, and how far we're going horizontally */
  1875. if ((XDelta = XEnd - XStart) < 0)
  1876. {
  1877. XAdvance = (-1);
  1878. XDelta = -XDelta;
  1879. } else {
  1880. XAdvance = 1;
  1881. }
  1882.  
  1883. /* Figure out how far we're going vertically */
  1884. YDelta = YEnd - YStart;
  1885.  
  1886. /* Special cases: Horizontal, vertical, and diagonal lines */
  1887. if (XDelta == 0)
  1888. {
  1889. for (i=0; i <= YDelta; i++)
  1890. {
  1891. *ScreenPtr = Color;
  1892. ScreenPtr += surface->w;
  1893. }
  1894.  
  1895. UNLOCK_SURFACE_AND_RETURN;
  1896. }
  1897. if (YDelta == 0)
  1898. {
  1899. for (i=0; i <= XDelta; i++)
  1900. {
  1901. *ScreenPtr = Color;
  1902. ScreenPtr += XAdvance;
  1903. }
  1904. UNLOCK_SURFACE_AND_RETURN;
  1905. }
  1906. if (XDelta == YDelta)
  1907. {
  1908. for (i=0; i <= XDelta; i++)
  1909. {
  1910. *ScreenPtr = Color;
  1911. ScreenPtr += XAdvance + surface->w;
  1912. }
  1913. UNLOCK_SURFACE_AND_RETURN;
  1914. }
  1915.  
  1916. /* Determine whether the line is X or Y major, and handle accordingly */
  1917. if (XDelta >= YDelta) /* X major line */
  1918. {
  1919. WholeStep = XDelta / YDelta;
  1920. AdjUp = (XDelta % YDelta) * 2;
  1921. AdjDown = YDelta * 2;
  1922. ErrorTerm = (XDelta % YDelta) - (YDelta * 2);
  1923.  
  1924. InitialPixelCount = (WholeStep / 2) + 1;
  1925. FinalPixelCount = InitialPixelCount;
  1926.  
  1927. if ((AdjUp == 0) && ((WholeStep & 0x01) == 0)) InitialPixelCount--;
  1928. if ((WholeStep & 0x01) != 0) ErrorTerm += YDelta;
  1929.  
  1930. DrawHorizontalRun(&ScreenPtr, XAdvance, InitialPixelCount, Color);
  1931.  
  1932. for (i=0; i<(YDelta-1); i++)
  1933. {
  1934. RunLength = WholeStep;
  1935. if ((ErrorTerm += AdjUp) > 0)
  1936. {
  1937. RunLength ++;
  1938. ErrorTerm -= AdjDown;
  1939. }
  1940.  
  1941. DrawHorizontalRun(&ScreenPtr, XAdvance, RunLength, Color);
  1942. }
  1943.  
  1944. DrawHorizontalRun(&ScreenPtr, XAdvance, FinalPixelCount, Color);
  1945.  
  1946. UNLOCK_SURFACE_AND_RETURN;
  1947. } else { /* Y major line */
  1948. WholeStep = YDelta / XDelta;
  1949. AdjUp = (YDelta % XDelta) * 2;
  1950. AdjDown = XDelta * 2;
  1951. ErrorTerm = (YDelta % XDelta) - (XDelta * 2);
  1952. InitialPixelCount = (WholeStep / 2) + 1;
  1953. FinalPixelCount = InitialPixelCount;
  1954.  
  1955. if ((AdjUp == 0) && ((WholeStep & 0x01) == 0)) InitialPixelCount --;
  1956. if ((WholeStep & 0x01) != 0) ErrorTerm += XDelta;
  1957.  
  1958. DrawVerticalRun(&ScreenPtr, XAdvance, InitialPixelCount, Color);
  1959.  
  1960. for (i=0; i<(XDelta-1); i++)
  1961. {
  1962. RunLength = WholeStep;
  1963. if ((ErrorTerm += AdjUp) > 0)
  1964. {
  1965. RunLength ++;
  1966. ErrorTerm -= AdjDown;
  1967. }
  1968.  
  1969. DrawVerticalRun(&ScreenPtr, XAdvance, RunLength, Color);
  1970. }
  1971.  
  1972. DrawVerticalRun(&ScreenPtr, XAdvance, FinalPixelCount, Color);
  1973. UNLOCK_SURFACE_AND_RETURN;
  1974. }
  1975. } /* drawline16 */
  1976.  
  1977.  
  1978. void clear2dscreen(void)
  1979. {
  1980. SDL_Rect rect;
  1981.  
  1982. rect.x = rect.y = 0;
  1983. rect.w = surface->w;
  1984.  
  1985. if (qsetmode == 350)
  1986. rect.h = 350;
  1987. else if (qsetmode == 480)
  1988. {
  1989. if (ydim16 <= 336)
  1990. rect.h = 336;
  1991. else
  1992. rect.h = 480;
  1993. } /* else if */
  1994.  
  1995. SDL_FillRect(surface, &rect, 0);
  1996. } /* clear2dscreen */
  1997.  
  1998.  
  1999. void _idle(void)
  2000. {
  2001. if (surface != NULL)
  2002. _handle_events();
  2003. SDL_Delay(1);
  2004. } /* _idle */
  2005.  
  2006. void *_getVideoBase(void)
  2007. {
  2008. return((void *) surface->pixels);
  2009. }
  2010.  
  2011.  
  2012. //-------------------------------------------------------------------------------------------------
  2013. // TIMER
  2014. //=================================================================================================
  2015.  
  2016.  
  2017.  
  2018.  
  2019. // FIX_00007: game speed corrected. The game speed is now as the real
  2020. // DOS duke3d. Unloading a full 200 bullet pistol must take 45.1 sec.
  2021. // SDL timer was not fast/accurate enough and was slowing down the gameplay,
  2022. // so bad
  2023.  
  2024.  
  2025. static int64_t timerfreq=0;
  2026. static int32_t timerlastsample=0;
  2027. static int timerticspersec=0;
  2028. static void (*usertimercallback)(void) = NULL;
  2029.  
  2030. // This timer stuff is all Ken's idea.
  2031.  
  2032. //
  2033. // installusertimercallback() -- set up a callback function to be called when the timer is fired
  2034. //
  2035. void (*installusertimercallback(void (*callback)(void)))(void)
  2036. {
  2037. void (*oldtimercallback)(void);
  2038.  
  2039. oldtimercallback = usertimercallback;
  2040. usertimercallback = callback;
  2041.  
  2042. return oldtimercallback;
  2043. }
  2044.  
  2045.  
  2046. /*
  2047. inittimer() -- initialise timer
  2048. FCS: The tickspersecond parameter is a ratio value that helps replicating
  2049. oldschool DOS tick per seconds.
  2050.  
  2051. The way the timer work is:
  2052. float newSystemTickPerSecond = [0,1]
  2053. tickPerSecond on a DOS system = tickspersecond * newSystemTickPerSecond ;
  2054. */
  2055.  
  2056. int inittimer(int tickspersecond)
  2057. {
  2058. int64_t t;
  2059.  
  2060.  
  2061. if (timerfreq) return 0; // already installed
  2062.  
  2063. //printf("Initialising timer, with tickPerSecond=%d\n",tickspersecond);
  2064.  
  2065. // OpenWatcom seems to want us to query the value into a local variable
  2066. // instead of the global 'timerfreq' or else it gets pissed with an
  2067. // access violation
  2068. if (!TIMER_GetPlatformTicksInOneSecond(&t)) {
  2069. printf("Failed fetching timer frequency\n");
  2070. return -1;
  2071. }
  2072. timerfreq = t;
  2073. timerticspersec = tickspersecond;
  2074. TIMER_GetPlatformTicks(&t);
  2075. timerlastsample = (int32_t)(t*timerticspersec / timerfreq);
  2076.  
  2077. usertimercallback = NULL;
  2078.  
  2079. return 0;
  2080. }
  2081.  
  2082. //
  2083. // uninittimer() -- shut down timer
  2084. //
  2085. void uninittimer(void)
  2086. {
  2087. if (!timerfreq) return;
  2088.  
  2089. timerfreq=0;
  2090. timerticspersec = 0;
  2091. }
  2092.  
  2093. //
  2094. // sampletimer() -- update totalclock
  2095. //
  2096. void sampletimer(void)
  2097. {
  2098. int64_t i;
  2099. int32_t n;
  2100.  
  2101. if (!timerfreq) return;
  2102.  
  2103. TIMER_GetPlatformTicks(&i);
  2104.  
  2105.  
  2106. n = (int32_t)(i*timerticspersec / timerfreq) - timerlastsample;
  2107. if (n>0) {
  2108. totalclock += n;
  2109. timerlastsample += n;
  2110. }
  2111.  
  2112. if (usertimercallback) for (; n>0; n--) usertimercallback();
  2113. }
  2114.  
  2115.  
  2116. /*
  2117. getticks() -- returns the windows ticks count
  2118. FCS: This seeems to be only used in the multiplayer code
  2119. */
  2120. uint32_t getticks(void)
  2121. {
  2122. int64_t i;
  2123. TIMER_GetPlatformTicks(&i);
  2124. return (uint32_t)(i*(int32_t)(1000)/timerfreq);
  2125. }
  2126.  
  2127.  
  2128. //
  2129. // gettimerfreq() -- returns the number of ticks per second the timer is configured to generate
  2130. //
  2131. int gettimerfreq(void)
  2132. {
  2133. return timerticspersec;
  2134. }
  2135.  
  2136.  
  2137.  
  2138. void initkeys(void)
  2139. {
  2140. /* does nothing in SDL. Key input handling is set up elsewhere. */
  2141. /* !!! why not here? */
  2142. }
  2143.  
  2144. void uninitkeys(void)
  2145. {
  2146. /* does nothing in SDL. Key input handling is set up elsewhere. */
  2147. }
  2148.  
  2149.  
  2150. //unsigned int32_t getticks(void)
  2151. //{
  2152. // return(SDL_GetTicks());
  2153. //} /* getticks */
  2154.  
  2155.  
  2156. //Timer on windows 98 used to be really poor but now it is very accurate
  2157. // We can just use what SDL uses, no need for QueryPerformanceFrequency or QueryPerformanceCounter
  2158. // (which I bet SDL is using anyway).
  2159. #if 0//PLATFORM_WIN32
  2160. int TIMER_GetPlatformTicksInOneSecond(int64_t* t)
  2161. {
  2162. QueryPerformanceFrequency((LARGE_INTEGER*)t);
  2163. return 1;
  2164. }
  2165.  
  2166. void TIMER_GetPlatformTicks(int64_t* t)
  2167. {
  2168. QueryPerformanceCounter((LARGE_INTEGER*)t);
  2169. }
  2170. #else
  2171. //FCS: Let's try to use SDL again: Maybe SDL library is accurate enough now.
  2172. int TIMER_GetPlatformTicksInOneSecond(int64_t* t)
  2173. {
  2174. *t = 1000;
  2175. return 1;
  2176. }
  2177.  
  2178. void TIMER_GetPlatformTicks(int64_t* t)
  2179. {
  2180. *t = SDL_GetTicks();
  2181. }
  2182. #endif
  2183. /* end of sdl_driver.c ... */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement