home *** CD-ROM | disk | FTP | other *** search
/ The Elite Hackers Toolkit / TheEliteHackersToolkitVolume1_1998.rar / HACKERS.BIN / appcraks / P5WGLIDE.ZIP / MAIN.C < prev    next >
C/C++ Source or Header  |  1998-01-21  |  6KB  |  246 lines

  1. // main.c
  2.  
  3.  
  4. // Contains the .dll entry point and overridden functions
  5. // uses imports.c for loading symbols, dib.c for getting framebuffer access.
  6.  
  7. // Copr. 1998 - Justin Frankel/Nullsoft, Inc. (http://www.nullsoft.com)
  8.  
  9. // Version 0.5 - 1/4/1998
  10.  
  11. //Pentium optimized 3Dfx to DIB memory copy added by Chris Dohnal (cwdohnal@ucdavis.edu)
  12. //This dll will not work with scanline interleaved systems
  13.  
  14. #include <windows.h>
  15.  
  16. #define FX_GLIDE_NO_FUNC_PROTO
  17. #include <glide.h>
  18. #include <glideutl.h>
  19.  
  20. #include "pointers.h"
  21.  
  22. HINSTANCE hGlide, hDllInst;
  23.  
  24. #define ENTRY __declspec(dllexport)
  25. #define CALL __stdcall
  26.  
  27. // dib.c functions
  28. extern void dibinit(HWND hwnd, int w, int h);
  29. extern void dibquit();
  30. extern short *dibgetbuf();
  31. extern void dibupdate();
  32. extern int dibgetpitch();
  33.  
  34. // imports.c functions
  35. extern int LoadSymbols(HINSTANCE *hGlide, HINSTANCE hDllInst);
  36.  
  37. // DLL entry point. Calls on load/unload of the .dll. Loads or frees the real glide2x.dll.
  38. BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved) {
  39.  
  40.     hDllInst = hInstDll;
  41.     
  42.     if (fdwReason == DLL_PROCESS_ATTACH) {
  43.         if (!LoadSymbols(&hGlide,hDllInst)) {
  44.             hGlide = NULL;
  45.             return FALSE;
  46.         }
  47.     }
  48.  
  49.     if (fdwReason == DLL_PROCESS_DETACH) {
  50.         FreeModule(hGlide);
  51.         hGlide = NULL;
  52.     }
  53.     return TRUE;
  54. }
  55.  
  56. int enable=1, // are we running windowed?
  57.     sw, // screen width (window width)
  58.     sh; // screen height (window height)
  59. // quake 2 320x240 and 400x300 hack.
  60. int iQuake2LowResOffset;
  61.  
  62.  
  63. // Our new, overridden functions go here. 
  64.  
  65.  
  66. // override grSstWinOpen, for opening the 3dfx hardware.
  67. ENTRY FxBool CALL grSstWinOpen(FxU32 hFxWnd, GrScreenResolution_t grResolution,
  68.     GrScreenRefresh_t grRefresh, GrColorFormat_t grColorFormat,
  69.     GrOriginLocation_t grOriginLocation, int nColBuffers, int nAuxBuffers) {
  70.     
  71.     HWND hWnd;
  72.     
  73.     //Get hFxWnd as a HWND.
  74.     hWnd = (HWND)hFxWnd;
  75.  
  76.     if (_grSstWinOpen(hFxWnd, grResolution, grRefresh, grColorFormat,
  77.         grOriginLocation, nColBuffers, nAuxBuffers) == FXFALSE) {
  78.         
  79.         return FXFALSE;
  80.     }
  81.  
  82.     sw = _grSstScreenWidth();
  83.     sh = _grSstScreenHeight();
  84.     
  85.     // decide if we are running fullscreen or windowed
  86.     if (hWnd && IsWindow(hWnd)) {
  87.         RECT rect;
  88.         GetWindowRect(hWnd, &rect);
  89.         if ((rect.left == 0) && (rect.top == 0) && (rect.right == sw) &&
  90.             (rect.bottom == sh)) {
  91.             
  92.             enable = 0;
  93.         }
  94.         else {
  95.             enable = 1;
  96.         }
  97.     } 
  98.     else {
  99.         enable = 0;
  100.     }
  101.     
  102.     // if enabled, initialize us
  103.     if (enable) {
  104.         RECT rect;
  105.  
  106.         GetClientRect(hWnd, &rect);
  107.  
  108.         if (grOriginLocation == GR_ORIGIN_LOWER_LEFT && sh == 384) {
  109.             iQuake2LowResOffset = 384 - rect.bottom;
  110.         }
  111.         else {
  112.             iQuake2LowResOffset = 0;
  113.         }
  114.  
  115.         sw = rect.right;
  116.         sh = rect.bottom;
  117.         _grSstControl(GR_CONTROL_DEACTIVATE);
  118.         _grRenderBuffer(GR_BUFFER_BACKBUFFER);
  119.         dibinit(hWnd, sw, sh);
  120.     }
  121.     return FXTRUE;
  122. }
  123.  
  124. // override grBufferSwap to copy the hardware's data into our DIBSection
  125. ENTRY void CALL grBufferSwap(int swapInterval) {
  126.     FxBool fxbRet;
  127.     GrLfbInfo_t grLFBInfo;
  128.     
  129.     if (enable) {
  130.         short *data = dibgetbuf();
  131.         if (data) {
  132.             long lLine;
  133.             LPVOID pDIB;
  134.             LPVOID p3DfxScanlineStart;
  135.             int swTimesTwo, swDivFour;
  136.  
  137.  
  138.             //Lock the 3Dfx LFB
  139.             ZeroMemory(&grLFBInfo, sizeof(grLFBInfo));
  140.             grLFBInfo.size = sizeof(grLFBInfo);
  141.             fxbRet = _grLfbLock(GR_LFB_READ_ONLY, GR_BUFFER_BACKBUFFER, 0,
  142.                 GR_ORIGIN_ANY, FXFALSE, &grLFBInfo);
  143.             if (fxbRet != FXTRUE) {
  144.                 //Could not lock the 3Dfx LFB
  145.                 return;
  146.             }
  147.             
  148.             //Setup pointers to the 3Dfx LFB and the DIB
  149.             p3DfxScanlineStart = (BYTE *)grLFBInfo.lfbPtr +
  150.                 grLFBInfo.strideInBytes * (sh - 1);
  151.             pDIB = data;
  152.             swTimesTwo = sw * 2;
  153.             swDivFour = sw >> 2;
  154.  
  155.             //Transfer the data from the 3Dfx LFB to the DIB
  156.             for (lLine = 0; lLine < sh; lLine++) {
  157.                 //Transfer a scanline
  158.                 //CopyMemory(pDIB, p3DfxScanlineStart, sw * 2);
  159.  
  160.              _asm {
  161.                     mov esi, p3DfxScanlineStart
  162.                     mov edi, pDIB
  163.                     mov ecx, swDivFour
  164.                     
  165.                     /*sub edi, 8
  166.                 copyloop:
  167.                     fild qword ptr [esi]
  168.                     add esi, 8
  169.                     add edi, 8
  170.                     fistp qword ptr [edi]
  171.                     dec ecx
  172.                     jnz copyloop*/
  173.                 
  174.                     xor edx, edx
  175.                 copyloop:
  176.                     fild qword ptr [esi + edx*8]
  177.                     fistp qword ptr [edi + edx*8]
  178.                     inc edx
  179.                     dec ecx
  180.                     jnz copyloop
  181.                     
  182.                     /*xor edx, edx
  183.                 copyloop:
  184.                     mov eax, [esi + edx*8]
  185.                     mov ebx, [esi + edx*8 + 4]
  186.                     mov [edi + edx*8], eax
  187.                     mov [edi + edx*8 + 4], ebx
  188.                     inc edx
  189.                     dec ecx
  190.                     jnz copyloop*/
  191.  
  192.                 /*copyloop:
  193.                     mov eax, [esi]
  194.                     add esi, 4
  195.                     mov ebx, [esi]
  196.                     add esi, 4
  197.                     mov [edi], eax
  198.                     add edi, 4
  199.                     mov [edi], ebx
  200.                     add edi, 4
  201.                     dec ecx
  202.                     jnz copyloop*/
  203.                 }
  204.                 
  205.                 //Calculate the address of the next scanline
  206.                 (BYTE *)p3DfxScanlineStart -= grLFBInfo.strideInBytes;
  207.                 (BYTE *)pDIB += swTimesTwo;
  208.             }
  209.             
  210.             //Unlock the 3Dfx LFB
  211.             fxbRet = _grLfbUnlock(GR_LFB_READ_ONLY, GR_BUFFER_BACKBUFFER);
  212.             if (fxbRet != FXTRUE) {
  213.                 //Could not unlock the 3Dfx LFB
  214.                 return;
  215.             }
  216.  
  217.             //Old 3Dfx LFB Read function
  218.             //_grLfbReadRegion(GR_BUFFER_BACKBUFFER, 0, iQuake2LowResOffset, sw, sh, dibgetpitch(), data);
  219.             
  220.             dibupdate();
  221.  
  222.         }
  223.     }
  224.     else {
  225.         _grBufferSwap(swapInterval);
  226.     }
  227.     return;
  228. }
  229.  
  230. // override grSstWinClose to delete our DIB
  231. ENTRY void CALL grSstWinClose(void) {
  232.     _grSstWinClose();
  233.     if (enable) {
  234.         dibquit();
  235.     }
  236.     return;
  237. }
  238.  
  239. // override grSstControl to keep the calling app from reenabling the hardware's video output
  240. ENTRY FxBool CALL grSstControl(FxU32 code) {
  241.     if (!enable) {
  242.         return _grSstControl(code);
  243.     }
  244.     return 0;
  245. }
  246.