1
0

ddraw.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <windows.h>
  2. #include <ddraw.h>
  3. #include "dd.h"
  4. #include "../Agave/Language/api_language.h"
  5. #include "resource.h"
  6. extern HINSTANCE visDll;
  7. #pragma intrinsic(memset)
  8. C_DD::C_DD()
  9. {
  10. g_lpDD=NULL;
  11. cursurface=0;
  12. g_ddp=NULL;
  13. g_lpDDSPrim=NULL;
  14. g_lpPrimSurfBack=NULL;
  15. g_RenderSurface[0]=g_RenderSurface[1]=NULL;
  16. g_RenderSurfaceLocked[0]=g_RenderSurfaceLocked[1]=NULL;
  17. memset(g_palette_seek,0,sizeof(g_palette_seek));
  18. memset(g_palette_orig,0,sizeof(g_palette_orig));
  19. memset(g_palette_last,0,sizeof(g_palette_last));
  20. g_pal_lastv=0;
  21. g_palette_starttime=g_palette_endtime=0;
  22. }
  23. int C_DD::lock(unsigned char **input, unsigned char **output)
  24. {
  25. DDSURFACEDESC d={sizeof(d),};
  26. if (g_RenderSurface[cursurface]->Lock(NULL,&d,DDLOCK_WAIT,NULL) != DD_OK)
  27. {
  28. g_RenderSurfaceLocked[cursurface]=0;
  29. return 0;
  30. }
  31. *input=(unsigned char*)(g_RenderSurfaceLocked[cursurface]=d.lpSurface);
  32. DDSURFACEDESC e={sizeof(e),};
  33. if (g_RenderSurface[cursurface^1]->Lock(NULL,&e,DDLOCK_WAIT,NULL) != DD_OK)
  34. {
  35. g_RenderSurface[cursurface]->Unlock(g_RenderSurfaceLocked[cursurface]);
  36. g_RenderSurfaceLocked[cursurface^1]=0;
  37. g_RenderSurfaceLocked[cursurface]=0;
  38. return 0;
  39. }
  40. *output=(unsigned char*)(g_RenderSurfaceLocked[cursurface^1]=e.lpSurface);
  41. return 1;
  42. }
  43. int C_DD::palette_fadeleft()
  44. {
  45. return g_palette_endtime-GetTickCount();
  46. }
  47. void C_DD::unlock(void)
  48. {
  49. if (g_RenderSurfaceLocked[0])
  50. {
  51. g_RenderSurface[0]->Unlock(g_RenderSurfaceLocked[0]);
  52. g_RenderSurface[1]->Unlock(g_RenderSurfaceLocked[1]);
  53. if (g_palette_starttime)
  54. {
  55. PALETTEENTRY palette[256] = {0};
  56. int x = 0;
  57. int len=g_palette_endtime-g_palette_starttime;
  58. int pos=GetTickCount()-g_palette_starttime;
  59. int do_it=1;
  60. if (pos >= len || !len)
  61. {
  62. g_palette_starttime=0;
  63. g_pal_lastv=-128;
  64. memcpy(g_palette_last,g_palette_seek,768);
  65. }
  66. else
  67. {
  68. int adj=(pos<<8)/len;
  69. int poop=(255*adj)>>8;
  70. if (poop != g_pal_lastv)
  71. {
  72. g_pal_lastv=poop;
  73. for (x = 0; x < 768; x ++)
  74. {
  75. int c=(((int)g_palette_seek[x])*adj + ((int)g_palette_orig[x])*(255-adj))>>8;
  76. if (c > 255) c = 255;
  77. g_palette_last[x]=c;
  78. }
  79. }
  80. else do_it=0;
  81. }
  82. if (do_it)
  83. {
  84. int y=0;
  85. for (x = 0; x < 256; x ++)
  86. {
  87. palette[x].peRed=g_palette_last[y];
  88. palette[x].peGreen=g_palette_last[y+1];
  89. palette[x].peBlue=g_palette_last[y+2];
  90. palette[x].peFlags = PC_NOCOLLAPSE;
  91. y+=3;
  92. }
  93. g_ddp->SetEntries(0,0,256,palette);
  94. }
  95. }
  96. if (g_lpPrimSurfBack)
  97. {
  98. if (g_lpPrimSurfBack->Blt(NULL,g_RenderSurface[cursurface^1],NULL,DDBLT_WAIT,NULL) == DDERR_SURFACELOST)
  99. {
  100. g_lpDDSPrim->Restore();
  101. g_lpPrimSurfBack->Restore();
  102. }
  103. else
  104. g_lpDDSPrim->Flip(NULL,DDFLIP_WAIT);
  105. }
  106. else
  107. {
  108. if (g_lpDDSPrim->Blt(NULL,g_RenderSurface[cursurface^1],NULL,DDBLT_WAIT,NULL) == DDERR_SURFACELOST)
  109. {
  110. g_lpDDSPrim->Restore();
  111. }
  112. else g_lpDD->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN,0);
  113. }
  114. g_RenderSurfaceLocked[0]=0;
  115. g_RenderSurfaceLocked[1]=0;
  116. cursurface^=1;
  117. }
  118. }
  119. char *C_DD::open(int w, int h, HWND hwnd)
  120. {
  121. static char errbuf[128];
  122. if (DirectDrawCreate(NULL,&g_lpDD,NULL) != DD_OK) return WASABI_API_LNGSTRING_BUF(IDS_DIRECTDRAWCREATE_FAILED,errbuf,128);
  123. if (g_lpDD->SetCooperativeLevel(hwnd,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN) != DD_OK)
  124. {
  125. g_lpDD->Release();
  126. return WASABI_API_LNGSTRING_BUF(IDS_COULD_NOT_SET_EXCLUSIVE_FULLSCREEN,errbuf,128);
  127. }
  128. if (g_lpDD->SetDisplayMode(w,h,8) != DD_OK)
  129. {
  130. g_lpDD->Release();
  131. return WASABI_API_LNGSTRING_BUF(IDS_COULD_NOT_SET_DISPLAY_MODE,errbuf,128);
  132. }
  133. DDSURFACEDESC DDsd = {0};
  134. DDsd.dwSize = sizeof(DDsd);
  135. DDsd.dwFlags = DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
  136. DDsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_COMPLEX|DDSCAPS_FLIP;
  137. DDsd.dwBackBufferCount = 1;
  138. if (g_lpDD->CreateSurface(&DDsd, &g_lpDDSPrim, NULL) != DD_OK)
  139. {
  140. g_lpPrimSurfBack=NULL;
  141. DDsd.dwSize = sizeof(DDsd);
  142. DDsd.dwFlags = DDSD_CAPS;
  143. DDsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  144. if (g_lpDD->CreateSurface(&DDsd, &g_lpDDSPrim, NULL) != DD_OK)
  145. {
  146. g_lpDD->Release();
  147. return WASABI_API_LNGSTRING_BUF(IDS_COULD_NOT_CREATE_PRIMARY_SURFACE,errbuf,128);
  148. }
  149. }
  150. else
  151. {
  152. DDSCAPS ddscaps;
  153. ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
  154. if (g_lpDDSPrim->GetAttachedSurface(&ddscaps, &g_lpPrimSurfBack) != DD_OK) // no page flipping
  155. g_lpPrimSurfBack=NULL;
  156. }
  157. DDsd.dwFlags = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT|DDSD_PITCH|DDSD_PIXELFORMAT;
  158. DDsd.dwWidth=w;
  159. DDsd.dwHeight=h;
  160. DDsd.lPitch=w;
  161. DDsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN|DDSCAPS_SYSTEMMEMORY;
  162. DDsd.ddpfPixelFormat.dwSize = sizeof(DDsd.ddpfPixelFormat);
  163. DDsd.ddpfPixelFormat.dwFlags=DDPF_PALETTEINDEXED8|DDPF_RGB;
  164. DDsd.ddpfPixelFormat.dwRGBBitCount = 8;
  165. if (g_lpDD->CreateSurface(&DDsd, &g_RenderSurface[0], NULL) != DD_OK ||
  166. g_lpDD->CreateSurface(&DDsd, &g_RenderSurface[1], NULL) != DD_OK)
  167. {
  168. g_lpDD->Release();
  169. return WASABI_API_LNGSTRING_BUF(IDS_COULD_NOT_CREATE_RENDER_SURFACE,errbuf,128);
  170. }
  171. PALETTEENTRY palette[256] = {0};
  172. for (int x = 0; x < 256; x ++)
  173. {
  174. palette[x].peRed=palette[x].peGreen=palette[x].peBlue=0;
  175. palette[x].peFlags = PC_NOCOLLAPSE;
  176. }
  177. if (g_lpDD->CreatePalette(DDPCAPS_8BIT|DDPCAPS_ALLOW256,palette,&g_ddp,NULL) != DD_OK)
  178. {
  179. g_lpDD->Release();
  180. return WASABI_API_LNGSTRING_BUF(IDS_COULD_NOT_CREATE_PALETTE,errbuf,128);
  181. }
  182. if (g_lpDDSPrim->SetPalette(g_ddp) != DD_OK)
  183. {
  184. g_lpDD->Release();
  185. return WASABI_API_LNGSTRING_BUF(IDS_COULD_NOT_SET_PALETTE,errbuf,128);
  186. }
  187. g_pal_lastv=-128;
  188. g_palette_starttime=0;
  189. return 0;
  190. }
  191. void C_DD::setpalette(unsigned char palette[768], unsigned int time_ms)
  192. {
  193. memcpy(g_palette_seek,palette,768);
  194. memcpy(g_palette_orig,g_palette_last,768);
  195. g_palette_starttime=GetTickCount();
  196. g_palette_endtime=g_palette_starttime+time_ms;
  197. }
  198. void C_DD::close(void)
  199. {
  200. if (g_lpDD) g_lpDD->Release();
  201. g_lpDD=0;
  202. cursurface=0;
  203. g_ddp=NULL;
  204. g_lpDDSPrim=NULL;
  205. g_lpPrimSurfBack=NULL;
  206. g_RenderSurface[0]=g_RenderSurface[1]=NULL;
  207. g_RenderSurfaceLocked[0]=g_RenderSurfaceLocked[1]=NULL;
  208. memset(g_palette_seek,0,sizeof(g_palette_seek));
  209. memset(g_palette_orig,0,sizeof(g_palette_orig));
  210. memset(g_palette_last,0,sizeof(g_palette_last));
  211. g_pal_lastv=0;
  212. g_palette_starttime=g_palette_endtime=0;
  213. }