Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
dirdraw.cpp
Upload User: hukewei88
Upload Date: 2019-04-29
Package Size: 835k
Code Size: 14k
Category:
DirextX
Development Platform:
C/C++
- // dirdraw.cpp
- //******************************************
- // copyright 1998 Mickey Kawick
- // This file may be used anywhere for free, but this
- // comment info must be left here.
- // If you use this code in any shape or form or even
- // as a reference for profit send me a dollar.
- // Not a dollar per copy, just a single US dollar.
- // If you are not in the US, send me
- // a few different coins or the equivalent of a dollar.
- // I love foreign currency, and I have a little collection.
- // MKawick@sprintmail.com
- //******************************************
- #include "dirdraw.h"
- #include "asm.h"
- //****************************
- //****************************
- DIRECT_DRAW_MANAGER:: DIRECT_DRAW_MANAGER ()
- {
- // let's set all values to 0. This is a simple constructor that
- // simply initializes all of the member variables.
- WindowHandle = NULL;// pointers
- DDObject = NULL;
- DDObjectNew = NULL;
- Primary = NULL;
- BackBuffer = NULL;
- AvailableVideoMemory = 0;// numbers
- TotalVideoMemory = 0;
- Width = 0;
- Height = 0;
- WindowX = 0;
- WindowY = 0;
- FullScreenFlag = 0;
- NotInitializedFlag = true;
- //********* offscreen buffers for faster drawing, in video mem
- // we may use these later, but for now, let's just allocate them and
- // leave them alone
- NumOffSurfaces = 0;
- for (int i=0; i<MAXOFFSURFACES; i++)
- {
- OffSurface[i] = NULL;
- OffWidth[MAXOFFSURFACES] = 0, OffHeight[MAXOFFSURFACES] = 0;
- }
- }
- //****************************
- DIRECT_DRAW_MANAGER:: ~ DIRECT_DRAW_MANAGER ()
- {
- if (DDObjectNew != NULL)
- {
- DDObjectNew->Release();// takes care of all underlying objects
- if (DDClipper != NULL) DDClipper->Release();
- if (Primary != NULL) Primary->Release();// kills all associated surfaces
- if (BackBuffer != NULL) BackBuffer->Release();
- }
- }
- //****************************
- //****************************
- HWND DIRECT_DRAW_MANAGER:: CreateMainWindow (const int w, const int h,
- const char* ClassName,
- void* MessageLoop,
- void* hInst,
- const bool FullScreen)
- {
- if (NotInitializedFlag == false) return NULL;// already initialized
- WNDCLASS wc;
- FullScreenFlag = FullScreen;// store this value
- wc.style= CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = (long (__stdcall *)(void *,unsigned int,unsigned int,long)) MessageLoop;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInst;
- wc.hIcon = LoadIcon (hInst, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = NULL;
- wc.lpszMenuName = ClassName;//NULL;
- wc.lpszClassName = ClassName;
- RegisterClass(&wc);
- if (FullScreen)// there are some slight differences between
- //fullscreen and not
- {
- WindowX = 0, WindowY = 0;
- WindowHandle = CreateWindowEx(
- WS_EX_TOPMOST,
- ClassName,
- ClassName,
- WS_POPUP,
- WindowX,
- WindowY,
- 640, ///GetSystemMetrics( SM_CXSCREEN ),// full screen dimensions
- 480,//GetSystemMetrics( SM_CYSCREEN ),
- NULL,
- NULL,
- hInst,
- NULL);
- }
- else
- {
- WindowX = GetSystemMetrics (SM_CXSCREEN)/2-w/2, // center the window
- WindowY = GetSystemMetrics (SM_CYSCREEN)/2-h/2;
- WindowHandle = CreateWindowEx(
- 0,
- ClassName,
- ClassName,
- WS_POPUP,
- WindowX,
- WindowY,
- w,
- h,
- NULL,
- NULL,
- hInst,
- NULL);
- }
- // there is a problem creating the window
- if (WindowHandle == NULL) return NULL;
- // we now have a real window and we are ready to proceed
- Width = w;
- Height = h;
- ShowWindow (WindowHandle, 1);
- UpdateWindow (WindowHandle);
- SetFocus (WindowHandle);
- ShowCursor (FALSE);
- int err = CreateDirectDraw ();
- if (err == 0) return NULL;
- NotInitializedFlag = false;
- return WindowHandle;
- }
- //****************************
- void DIRECT_DRAW_MANAGER:: ErrorMessage (const char* str)
- {
- MessageBox (WindowHandle, str, "Error", MB_OK);
- DestroyWindow (WindowHandle);
- }
- //****************************
- int DIRECT_DRAW_MANAGER:: CreateDirectDraw ()
- {
- HRESULT ddret = DirectDrawCreate (NULL, &DDObject, NULL);
- if (ddret != DD_OK){ DestroyWindow (WindowHandle); return 0;}
- //***************
- ddret = DDObject->SetCooperativeLevel (WindowHandle,
- (FullScreenFlag) ? (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)
- : (DDSCL_NORMAL));
- if (ddret != DD_OK){ ErrorMessage ("Cooperative level not set"); return 0;}
- #if defined (DDVER6)
- ddret = DDObject->QueryInterface (IID_IDirectDraw4, (LPVOID *)
- &DDObjectNew);
- #else
- /* ddret = DDObject->QueryInterface (IID_IDirectDraw2, (LPVOID *)
- &DDObjectNew);*/
- DDObjectNew = DDObject;// simplifies the cross-version code
- #endif
- if(ddret != DD_OK)
- {
- ErrorMessage ("Problem initializing IID_IDirectDraw");
- return 0;
- }
- //***************
- if (FullScreenFlag)
- {
- // 16bit bit depth
- #if defined (DDVER6)
- ddret = DDObjectNew->SetDisplayMode (Width, Height, 16, 0, 0);
- #else
- ddret = DDObjectNew->SetDisplayMode (Width, Height, 16);
- #endif
- if (ddret != DD_OK){ ErrorMessage ("Display not set"); return 0;}
- }
- //***************Get primary surface
- ZeroMemory (&Description, sizeof(Description));
- ZeroMemory (&Description.ddpfPixelFormat, sizeof(DDPIXELFORMAT));
- Description.dwSize = sizeof (Description);
- Description.dwFlags = DDSD_CAPS;
- Description.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
- if (FullScreenFlag)
- {
- Description.ddsCaps.dwCaps |= DDSCAPS_FLIP | DDSCAPS_COMPLEX;
- Description.dwFlags |= DDSD_BACKBUFFERCOUNT;
- }
- // we can specify more than one back buffer
- Description.dwBackBufferCount = 1;
- ddret = DDObjectNew->CreateSurface (&Description, &Primary, NULL);
- if (ddret != DD_OK) { ErrorMessage ("Surface not created"); return 0;}
- //***************Get pixel format
- Description.dwSize = sizeof (Description);
- Description.dwFlags = DDSD_PIXELFORMAT;
- ddret = Primary->GetSurfaceDesc (&Description);
- if (ddret != DD_OK)
- {
- ErrorMessage ("pixel format not available");
- return 0;
- }
- //************* get our color type
- int g = Description.ddpfPixelFormat.dwGBitMask>>5;// assumes 5 for red
- if (g == 0x1F) ColorType = _5x5x5_;
- else if (g == 0x3F) ColorType = _5x6x5_;
- else { ErrorMessage ("color mode not supported"); return 0;}
- //*************** create clipper
- if (FullScreenFlag == false)
- {
- ddret = DDObjectNew->CreateClipper ( 0, &DDClipper, NULL);
- if (ddret != DD_OK){ ErrorMessage ("clipper not created"); return 0;}
- ddret = DDClipper->SetHWnd (0, WindowHandle);
- if (ddret != DD_OK)
- {
- ErrorMessage ("Surface not assigned to hwnd");
- return 0;
- }
- ddret = Primary->SetClipper (DDClipper);
- if (ddret != DD_OK)
- {
- ErrorMessage ("clipper not set on primary surface");
- return 0;
- }
- }
- //*************** Get BackBuffer surface
- if (FullScreenFlag)
- {
- ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
- ddret = Primary->GetAttachedSurface(&ddsCaps, &BackBuffer);
- if (ddret != DD_OK)
- {
- ErrorMessage ("primary surface not attached");
- return 0;
- }
- }
- else
- {
- Description.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
- Description.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
- Description.dwWidth = Width;
- Description.dwHeight = Height;
- ddret = DDObjectNew->CreateSurface(&Description, &BackBuffer, NULL);
- if (ddret != DD_OK){ ErrorMessage ("back buffer not made"); return 0;}
- }
- #if defined (DDVER6)
- ZeroMemory (&ddsCaps, sizeof (DDSCAPS2));
- #else
- ZeroMemory (&ddsCaps, sizeof (DDSCAPS));
- #endif
- ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
- #if defined (DDVER6)
- ddret = DDObjectNew->GetAvailableVidMem (&ddsCaps, &TotalVideoMemory,
- &AvailableVideoMemory);
- #else
- TotalVideoMemory = 0, AvailableVideoMemory = 0;
- #endif
- if(ddret != DD_OK) return 0;
- return 1;
- }
- //****************************
- //****************************
- int DIRECT_DRAW_MANAGER:: CreateBlankOffSurface (const int w, const int h)
- {
- if (NotInitializedFlag == true) return -1;
- if (NumOffSurfaces >= MAXOFFSURFACES) return -1;
- int which = NumOffSurfaces;
- CreateOffSurface(which, w, h);
- OffWidth[which] = w; OffHeight[which] = h;
- U16ptr ptr = (U16ptr) GetOffSurface(which);
- if (ptr != NULL) Memset16Bit (ptr, 0, w*h);
- ReleaseOffSurface (which);
- NumOffSurfaces++;// increment our count
- return which;
- }
- //****************************
- void DIRECT_DRAW_MANAGER:: CreateOffSurface (const int which, const int w, const int h)
- {
- if (NotInitializedFlag == true) return;
- ZeroMemory (&OffSurfaceDescription, sizeof (DDSURFACEDESC));
- OffSurfaceDescription.dwSize = sizeof (DDSURFACEDESC);
- OffSurfaceDescription.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
- OffSurfaceDescription.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
- OffSurfaceDescription.dwHeight = h;
- OffSurfaceDescription.dwWidth = w;
- DDObjectNew->CreateSurface (&OffSurfaceDescription,
- &OffSurface[which], NULL);
- }
- //****************************
- void DIRECT_DRAW_MANAGER:: DrawOffSurface (const int x, const int y, const int which)
- {
- if (NotInitializedFlag == true) return;
- if (Screen.Screen == NULL) return;
- BackBuffer->Unlock (NULL); // first unlock the back buffer
- RECT rect = {0, 0, OffWidth[which], OffHeight[which]};// draw
- BackBuffer->BltFast(x, y, OffSurface[which], &rect,
- DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
- if (Screen.Screen != NULL)// then, restore the lock
- {
- int ret;
- // keep going until it get it right
- do{
- ret = BackBuffer->Lock(NULL, &Description,
- DDLOCK_SURFACEMEMORYPTR, NULL);
- }while(ret!=DD_OK);
- }
- Screen.Width = Width, Screen.Height = Height;
- Screen.Screen = (U16ptr) Description.lpSurface;
- Screen.RealWidth = Description.lPitch/2;
- }
- //****************************
- //****************************
- // this function call must be followed by a call to "ReleasePointerToOffSurface"
- SCREEN_STRUCT*
- DIRECT_DRAW_MANAGER:: GetOffSurface (const int which)// must exist first
- {
- if (NotInitializedFlag == true) return NULL;
- if (which >= NumOffSurfaces || which<0 || NumOffSurfaces<1) return NULL;
- ZeroMemory(&OffSurfaceDescription, sizeof (DDSURFACEDESC));
- OffSurfaceDescription.dwSize = sizeof (DDSURFACEDESC);
- if (OffSurface[which]->IsLost()) OffSurface[which]->Restore();
- HRESULT rval = OffSurface[which]->Lock(NULL, &OffSurfaceDescription,
- 0, NULL);
- if(rval != DD_OK) return NULL;
- Screen.Width = OffWidth[which], Screen.Height = OffHeight[which],
- Screen.Screen = (U16ptr) OffSurfaceDescription.lpSurface;
- Screen.RealWidth = OffSurfaceDescription.lPitch/2;
- return &Screen;
- }
- //****************************
- void DIRECT_DRAW_MANAGER:: ReleaseOffSurface (const int which)
- {
- if (NotInitializedFlag == true) return;
- if (which >= NumOffSurfaces || which<0 || NumOffSurfaces<1) return;
- OffSurface[which]->Unlock(NULL);
- }
- //****************************
- //****************************
- void __fastcall DIRECT_DRAW_MANAGER:: ClearBackBuffer ()
- {
- if (NotInitializedFlag == true) return;
- PrepFrame();
- Memset16Bit (Screen.Screen, 0, Screen.Width*Screen.Height);
- }
- //****************************
- SCREEN_STRUCT*
- DIRECT_DRAW_MANAGER:: PrepFrame ()
- {
- if (NotInitializedFlag == true) return NULL;
- ZeroMemory (&Description, sizeof (DDSURFACEDESC));
- Description.dwSize = sizeof (Description);
- int ret;
- do{
- ret = BackBuffer->Lock (NULL, &Description,
- DDLOCK_SURFACEMEMORYPTR, NULL);
- }while(ret!=DD_OK);
- BackBuffer->Unlock (NULL);
- Screen.Width = Width;
- Screen.Height = Height;
- Screen.Screen = (U16ptr) Description.lpSurface;
- Screen.RealWidth = Description.lPitch/2;
- return &Screen;
- }
- //****************************
- void DIRECT_DRAW_MANAGER:: InterruptFrame ()
- {
- if (NotInitializedFlag == true) return;
- BackBuffer->Unlock (NULL);
- Screen.Screen = NULL;
- }
- //****************************
- int DIRECT_DRAW_MANAGER:: DrawFrame ()// swap
- {
- if (NotInitializedFlag == true) return 0;
- POINT pt = {0, 0};
- ClientToScreen (WindowHandle, &pt);
- RECT BuffRect = {pt.x, pt.y, pt.x+Width, pt.y+Height};
- RECT DRect = {0, 0, Width, Height};
- int pass = 0;//DDFLIP_WAIT;
- if (FullScreenFlag)
- {
- int ret = BackBuffer->GetFlipStatus (DDGFS_ISFLIPDONE);
- if (ret == DDERR_WASSTILLDRAWING) return 1;
- ret = Primary->Flip(NULL, pass);// too slow
- //int ret = Primary->Blt (&BuffRect, BackBuffer, &DRect,
- //pass, NULL);
- if (ret == DDERR_SURFACELOST)
- {
- #if defined (DDVER6)
- ret = DDObjectNew->RestoreAllSurfaces ();
- #else
- ret = Primary->Restore ();
- if (ret != DD_OK){ErrorMessage ("Surfaces lost"); return 1;}
- ret = BackBuffer->Restore ();
- #endif
- if (ret != DD_OK){ErrorMessage ("Surfaces lost"); return 1;}
- }
- }
- else
- {
- int ret = Primary->Blt (&BuffRect, BackBuffer, &DRect, pass, NULL);
- if (ret == DDERR_SURFACELOST)
- {
- #if defined (DDVER6)
- ret = DDObjectNew->RestoreAllSurfaces ();
- #else
- ret = Primary->Restore ();
- if (ret != DD_OK){ErrorMessage ("Surfaces lost"); return 1;}
- ret = BackBuffer->Restore ();
- #endif
- if (ret != DD_OK){ErrorMessage ("Surfaces lost"); return 1;}
- }
- }
- Screen.Screen = NULL;
- //DDObjectNew->WaitForVerticalBlank(DDWAITVB_BLOCKEND , NULL);
- return 0;
- }
- //****************************
- //****************************
- void DIRECT_DRAW_MANAGER:: PaintText (const int x, const int y, const char* string)
- {
- if (NotInitializedFlag == true) return;
- HDC hdc;
- BackBuffer->Unlock (NULL);
- BackBuffer-> GetDC(&hdc);
- SetBkMode (hdc, TRANSPARENT);
- SetTextColor (hdc, RGB(255, 255, 255));
- TextOut (hdc, x, y, string, strlen(string));
- BackBuffer->ReleaseDC (hdc);
- int ret;
- do{
- ret = BackBuffer->Lock (NULL, &Description,
- DDLOCK_SURFACEMEMORYPTR, NULL);
- }while (ret!=DD_OK);
- /* an older way to accomplish the same thing
- if (lpDDSBack->GetDC(&hdc) == DD_OK) // must be locked
- {
- SetBkColor(hdc, RGB(0, 0, 255));
- SetTextColor(hdc, RGB(255, 255, 0));
- TextOut(hdc, 0, 0, szBackMsg, lstrlen(szBackMsg));
- lpDDSBack->ReleaseDC(hdc);
- }
- */
- }
- //****************************
- //****************************
- //****************************