dx5.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. #include "video/win32/dx5.hh"
  9. #include "image/image.hh"
  10. #include "time/profile.hh"
  11. #include "main/win_main.hh"
  12. #include "image/context.hh"
  13. #include "video/win32/dx5_util.hh"
  14. #include "video/win32/dx5_error.hh"
  15. #include "threads/threads.hh"
  16. static i4_profile_class pf_dx5_mouse("dx5::mouse draw"),
  17. pf_dx5_copy("dx5::copy dirty"),
  18. pf_dx5_flip("dx5::flip"),
  19. pf_dx5_lock("dx5::lock");
  20. i4_dx5_display_class i4_dx5_display_class_instance;
  21. i4_dx5_display_class *i4_dx5_display=0;
  22. struct gr_buf_status_type
  23. {
  24. sw8 state; // -1 = not locked, else
  25. // I4_FRAME_BUFFER_READ,
  26. // or I4_FRAME_BUFFER_WRITE
  27. i4_image_class *im;
  28. } dx_buf[2]; // I4_FRONT_FRAME_BUFFER=0, I4_BACK_FRAME_BUFFER=1
  29. int dx5_error_function(const char *str)
  30. {
  31. dx5_common.cleanup();
  32. i4_dx5_display->error_handler.old_error_handler(str);
  33. return 0;
  34. }
  35. i4_refresh_type i4_dx5_display_class::update_model()
  36. {
  37. if (use_page_flip)
  38. return I4_PAGE_FLIP_REFRESH;
  39. else
  40. return I4_BLT_REFRESH;
  41. }
  42. i4_image_class *i4_dx5_display_class::lock_frame_buffer(i4_frame_buffer_type type,
  43. i4_frame_access_type access)
  44. {
  45. if (access==dx_buf[type].state)
  46. return dx_buf[type].im;
  47. else if (dx_buf[type].state!=-1)
  48. unlock_frame_buffer(type);
  49. HRESULT res=0;
  50. DDSURFACEDESC cur_dx5_lock_info;
  51. memset(&cur_dx5_lock_info,0,sizeof(DDSURFACEDESC));
  52. cur_dx5_lock_info.dwSize = sizeof(DDSURFACEDESC);
  53. IDirectDrawSurface3 *surf=type==I4_FRONT_FRAME_BUFFER ?
  54. dx5_common.front_surface : dx5_common.back_surface;
  55. int flags=access==I4_FRAME_BUFFER_READ ? DDLOCK_READONLY :
  56. DDLOCK_WRITEONLY;
  57. if (!surf)
  58. return 0;
  59. res = surf->Lock(NULL,&cur_dx5_lock_info, flags | DDLOCK_WAIT,0);
  60. if (res == DD_OK)
  61. {
  62. #ifdef DEBUG
  63. surf->Unlock(NULL);
  64. #endif
  65. dx_buf[type].im->data=cur_dx5_lock_info.lpSurface;
  66. dx_buf[type].im->bpl=cur_dx5_lock_info.lPitch;
  67. dx_buf[type].state=access;
  68. }
  69. return dx_buf[type].im;
  70. }
  71. void i4_dx5_display_class::unlock_frame_buffer(i4_frame_buffer_type type)
  72. {
  73. IDirectDrawSurface3 *surf=type==I4_FRONT_FRAME_BUFFER ?
  74. dx5_common.front_surface : dx5_common.back_surface;
  75. #ifndef DEBUG
  76. if (surf)
  77. surf->Unlock(NULL);
  78. #endif
  79. dx_buf[type].im->data=0;
  80. dx_buf[type].state=-1;
  81. }
  82. // i4_image_class *i4_dx5_display_class::grab_framebuffer()
  83. // {
  84. // if (!fake_screen) return 0;
  85. // i4_image_class *im=i4_create_image(width(), height(), fake_screen->get_pal());
  86. // void *mem=dx5_lock(DX5_LOCK_READ);
  87. // for (int y=0; y<height(); y++)
  88. // {
  89. // w16 *p=(w16 *)((w8 *)im->data + y*im->bpl);
  90. // for (int x=0; x<width(); x++)
  91. // {
  92. // p[x]=*(((w16 *)mem)+x);
  93. // }
  94. // mem=(w8 *)mem + cur_dx5_lock_info.lPitch;
  95. // }
  96. // dx5_unlock();
  97. // return im;
  98. // }
  99. i4_dx5_display_class::i4_dx5_display_class():input(0)
  100. {
  101. mode_list=0;
  102. i4_dx5_display=this;
  103. mouse=0;
  104. fake_screen=0;
  105. context=0;
  106. #ifdef DEBUG
  107. use_exclusive_mode = i4_F;
  108. use_page_flip = i4_F;
  109. #else
  110. use_exclusive_mode = i4_T;
  111. use_page_flip = i4_F;
  112. #endif
  113. }
  114. i4_image_class *i4_dx5_display_class::get_screen()
  115. {
  116. return fake_screen;
  117. }
  118. // this will add all the drivers that directx find into the display list
  119. void i4_dx5_display_class::init()
  120. {
  121. int id=0;
  122. dx5_driver *list=dx5_common.get_driver_list();
  123. char *n=name_buffer;
  124. for (dx5_driver *d=list; d; d=d->next, id++)
  125. {
  126. IDirectDraw2 *ddraw=dx5_common.initialize_driver(d);
  127. if (ddraw)
  128. {
  129. // no hardware acceleration for this demo
  130. if (dx5_common.get_driver_hardware_info(ddraw))
  131. {
  132. sprintf(n, "%s : 3d Accelerated", d->DriverName);
  133. i4_display_list_struct *s=new i4_display_list_struct;
  134. s->add_to_list(n, id | 0x8000, this, i4_display_list);
  135. n+=strlen(n)+1;
  136. }
  137. //only want software rendering stuffs for the primary display driver to be listed
  138. if (!stricmp(d->DriverName,"display"))
  139. {
  140. sprintf(n, "%s : Software Rendered", d->DriverName);
  141. i4_display_list_struct *s=new i4_display_list_struct;
  142. s->add_to_list(n, id, this, i4_display_list);
  143. n+=strlen(n)+1;
  144. }
  145. ddraw->Release();
  146. }
  147. }
  148. dx5_common.free_driver_list(list);
  149. }
  150. // find the driver, then get it's modes and return the first one
  151. i4_display_class::mode *i4_dx5_display_class::get_first_mode(int driver_id)
  152. {
  153. int find_id = driver_id & (~0x8000); // top bit indicates hardware accelerated
  154. if (mode_list)
  155. {
  156. dx5_common.free_mode_list(mode_list);
  157. mode_list=0;
  158. }
  159. dx5_driver *driver_list=dx5_common.get_driver_list(), *d;
  160. for (d=driver_list; find_id && d; d=d->next, find_id--);
  161. IDirectDraw2 *ddraw=dx5_common.initialize_driver(d);
  162. dx5_common.free_driver_list(driver_list);
  163. if (!ddraw) return 0;
  164. mode_list=dx5_common.get_mode_list(ddraw);
  165. ddraw->Release();
  166. amode.dx5=mode_list;
  167. amode.driver_id=driver_id;
  168. return get_next_mode();
  169. }
  170. i4_dx5_display_class::~i4_dx5_display_class()
  171. {
  172. ;
  173. }
  174. i4_display_class::mode *i4_dx5_display_class::get_next_mode()
  175. {
  176. if (!amode.dx5)
  177. return 0;
  178. DDSURFACEDESC *desc=&amode.dx5->desc;
  179. sprintf(amode.name, "%d X %d 16bit", desc->dwWidth, desc->dwHeight);
  180. amode.xres=desc->dwWidth;
  181. amode.yres=desc->dwHeight;
  182. amode.bits_per_pixel=16;
  183. amode.red_mask = desc->ddpfPixelFormat.dwRBitMask;
  184. amode.green_mask = desc->ddpfPixelFormat.dwGBitMask;
  185. amode.blue_mask = desc->ddpfPixelFormat.dwBBitMask;
  186. amode.flags =0;
  187. last_mode=amode;
  188. amode.dx5=amode.dx5->next;
  189. return &amode;
  190. }
  191. i4_bool i4_dx5_display_class::initialize_mode()
  192. {
  193. if (!i4_win32_startup_options.fullscreen)
  194. use_page_flip = i4_F;
  195. if (!input.create_window(0,0,
  196. last_mode.xres,
  197. last_mode.yres, this, i4_win32_startup_options.fullscreen))
  198. return i4_F;
  199. int x,y,w,h;
  200. if (!i4_win32_startup_options.fullscreen)
  201. input.get_window_area(x,y,w,h);
  202. else
  203. {
  204. w=last_mode.xres;
  205. h=last_mode.yres;
  206. }
  207. last_mode.xres=w;
  208. last_mode.yres=h;
  209. // find the driver and initialize it
  210. int find_id = last_mode.driver_id & (~0x8000);
  211. dx5_driver *driver_list=dx5_common.get_driver_list(), *d;
  212. for (d=driver_list; find_id && d; d=d->next, find_id--);
  213. dx5_common.ddraw=dx5_common.initialize_driver(d);
  214. dx5_common.free_driver_list(driver_list);
  215. if (!dx5_common.ddraw)
  216. return i4_F;
  217. if (i4_win32_startup_options.fullscreen && use_exclusive_mode)
  218. {
  219. // change the error handling function so that the screen is restored before
  220. // a dialog box is displayed, or they won't be able to read the message!
  221. if (i4_dx5_check(dx5_common.ddraw->SetCooperativeLevel(input.get_window_handle(),
  222. DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
  223. {
  224. error_handler.old_error_handler=i4_get_error_function();
  225. error_handler.need_restore_old=i4_T;
  226. i4_set_error_function(dx5_error_function);
  227. }
  228. else return i4_F;
  229. }
  230. else if (!i4_dx5_check(dx5_common.ddraw->SetCooperativeLevel(input.get_window_handle(),
  231. DDSCL_NORMAL)))
  232. return i4_F;
  233. //win95 function to hide the mouse cursor. our display uses our own cursor
  234. if (i4_win32_startup_options.fullscreen &&
  235. !i4_dx5_check(dx5_common.ddraw->SetDisplayMode(last_mode.xres, last_mode.yres,16,0,0)))
  236. return i4_F;
  237. if (use_page_flip)
  238. {
  239. if (!dx5_common.create_surface(DX5_PAGE_FLIPPED_PRIMARY_SURFACE,
  240. last_mode.xres, last_mode.yres))
  241. return i4_F;
  242. }
  243. else if (!dx5_common.create_surface(DX5_BACKBUFFERED_PRIMARY_SURFACE,
  244. last_mode.xres, last_mode.yres))
  245. return i4_F;
  246. // find the size of the primary surface
  247. DDSURFACEDESC ddsd;
  248. dx5_common.get_surface_description(dx5_common.primary_surface, ddsd);
  249. fake_screen=new i4_dx5_image_class(last_mode.xres, last_mode.yres);
  250. fake_screen->surface->PageLock(0);
  251. i4_pixel_format fmt;
  252. fmt.pixel_depth = I4_16BIT;
  253. fmt.red_mask = ddsd.ddpfPixelFormat.dwRBitMask;
  254. fmt.green_mask = ddsd.ddpfPixelFormat.dwGBitMask;
  255. fmt.blue_mask = ddsd.ddpfPixelFormat.dwBBitMask;
  256. fmt.alpha_mask = 0;
  257. fmt.calc_shift();
  258. pal = i4_pal_man.register_pal(&fmt);
  259. dx_buf[0].im=i4_create_image(last_mode.xres, last_mode.yres, pal, 0, 0);
  260. dx_buf[0].state=-1;
  261. dx_buf[1].im=i4_create_image(last_mode.xres, last_mode.yres, pal, 0, 0);
  262. dx_buf[1].state=-1;
  263. context = new i4_draw_context_class(0,0, last_mode.xres-1, last_mode.yres-1);
  264. context->both_dirty = new i4_rect_list_class;
  265. memcpy(&cur_mode, &last_mode, sizeof(last_mode));
  266. if (1) //!i4_win32_startup_options.fullscreen)
  267. mouse = new dx5_mouse_class(use_page_flip);
  268. else
  269. {
  270. thread_mouse = new ddraw_thread_cursor_class(pal,input.get_window_handle(),
  271. 0,0,last_mode.xres,last_mode.yres);
  272. input.set_async_mouse(thread_mouse);
  273. }
  274. return i4_T;
  275. }
  276. i4_bool i4_dx5_display_class::close()
  277. {
  278. if (mouse)
  279. {
  280. delete mouse;
  281. mouse = 0;
  282. }
  283. input.set_async_mouse(0);
  284. if (thread_mouse)
  285. {
  286. delete thread_mouse;
  287. thread_mouse = 0;
  288. }
  289. if (error_handler.need_restore_old)
  290. {
  291. i4_set_error_function(error_handler.old_error_handler);
  292. error_handler.need_restore_old=i4_F;
  293. }
  294. input.destroy_window();
  295. for (int i=0; i<2; i++)
  296. {
  297. if (dx_buf[i].im)
  298. {
  299. delete dx_buf[i].im;
  300. dx_buf[i].im=0;
  301. }
  302. }
  303. if (fake_screen)
  304. {
  305. delete fake_screen;
  306. fake_screen=0;
  307. }
  308. if (context)
  309. {
  310. delete context;
  311. context=0;
  312. }
  313. if (mode_list)
  314. {
  315. dx5_common.free_mode_list(mode_list);
  316. mode_list=0;
  317. }
  318. next_frame_copy.delete_list();
  319. dx5_common.cleanup();
  320. return i4_T;
  321. }
  322. i4_bool i4_dx5_display_class::set_mouse_shape(i4_cursor_class *cursor)
  323. {
  324. if (mouse)
  325. {
  326. mouse->set_cursor(cursor);
  327. return i4_T;
  328. }
  329. else
  330. if (thread_mouse)
  331. {
  332. thread_mouse->set_cursor(cursor);
  333. return i4_T;
  334. }
  335. return i4_F;
  336. }
  337. void i4_dx5_display_class::flush()
  338. {
  339. RECT src;
  340. if (!input.get_active())
  341. {
  342. context->both_dirty->delete_list();
  343. context->both_dirty->add_area(0,0,width()-1, height()-1);
  344. i4_thread_yield();
  345. return ;
  346. }
  347. //start of copy stuff
  348. pf_dx5_copy.start();
  349. context->both_dirty->intersect_area(0,0,width()-1,height()-1);
  350. // Step 1 : copy middle buffer information to the back-buffer
  351. i4_rect_list_class::area_iter a,b;
  352. i4_rect_list_class *use_list;
  353. // if page flipped we need to make add the current dirty to the stuff left over from last frame
  354. if (use_page_flip)
  355. {
  356. a=context->both_dirty->list.begin();
  357. for (;a!=context->both_dirty->list.end();++a)
  358. next_frame_copy.add_area(a->x1, a->y1, a->x2, a->y2);
  359. use_list=&next_frame_copy;
  360. }
  361. else
  362. use_list=context->both_dirty;
  363. int wx,wy,ww,wh;
  364. input.get_window_area(wx,wy,ww,wh);
  365. for (a=use_list->list.begin(); a!=use_list->list.end(); ++a)
  366. {
  367. src.left = a->x1;
  368. src.top = a->y1;
  369. src.right = a->x2+1;
  370. src.bottom = a->y2+1;
  371. if (!i4_dx5_check(dx5_common.back_surface->BltFast(a->x1, a->y1, fake_screen->surface,
  372. &src,DDBLTFAST_NOCOLORKEY |
  373. DDBLTFAST_WAIT)))
  374. i4_error("dx5 blt failed on area %d %d %d %d ", a->x1, a->y1,a->x2, a->y2);
  375. }
  376. pf_dx5_copy.stop();
  377. //end of copy stuff
  378. //start of mouse stuff
  379. pf_dx5_mouse.start();
  380. mouse_x = input.mouse_x;
  381. mouse_y = input.mouse_y;
  382. if (mouse)
  383. mouse->save_and_draw(mouse_x, mouse_y);
  384. pf_dx5_mouse.stop();
  385. //start of flip stuff
  386. pf_dx5_flip.start();
  387. // now flip or copy the backbuffer to video memory
  388. if (use_page_flip)
  389. {
  390. // if (thread_mouse)
  391. // {
  392. // thread_mouse->draw_lock.lock();
  393. // thread_mouse->use_backbuffer(i4_F);
  394. // thread_mouse->remove(); //remove from front buffer
  395. // thread_mouse->use_backbuffer(i4_T);
  396. // thread_mouse->display();//display on back buffer
  397. // }
  398. dx5_common.primary_surface->Flip(NULL,DDFLIP_WAIT);
  399. // if (thread_mouse)
  400. // {
  401. // thread_mouse->use_backbuffer(i4_F);
  402. // thread_mouse->draw_lock.unlock();
  403. // }
  404. }
  405. else
  406. {
  407. // if (thread_mouse)
  408. // {
  409. // thread_mouse->draw_lock.lock();
  410. // thread_mouse->use_backbuffer(i4_T);
  411. // thread_mouse->display();
  412. // }
  413. if (!i4_dx5_check(dx5_common.primary_surface->BltFast(0,0,dx5_common.back_surface, NULL,
  414. DDBLTFAST_NOCOLORKEY |
  415. DDBLTFAST_WAIT)))
  416. i4_error("dx5 blt failed");
  417. // if (thread_mouse)
  418. // {
  419. // thread_mouse->remove();
  420. // thread_mouse->set_visible(i4_T);
  421. // thread_mouse->use_backbuffer(i4_F);
  422. // thread_mouse->draw_lock.unlock();
  423. // }
  424. }
  425. pf_dx5_flip.stop();
  426. //end of flip stuff
  427. pf_dx5_mouse.start();
  428. if (mouse)
  429. mouse->restore();
  430. pf_dx5_mouse.stop();
  431. if (use_page_flip)
  432. next_frame_copy.swap(context->both_dirty);
  433. context->both_dirty->delete_list();
  434. }