Ejemplo n.º 1
0
/*
 * DDColorMatch:
 *    Matches a color (RGB) with a surface
 */
DWORD DDColorMatch(LPDIRECTDRAWSURFACE pdds, COLORREF rgb)
{
    COLORREF rgbT;
    HDC hdc;
    DWORD dw = CLR_INVALID;
    DDSURFACEDESC ddsd;
    HRESULT hres;
    
    //  use GDI SetPixel to color match for us
    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK) {
        rgbT = GetPixel(hdc, 0, 0);             // save current pixel value
        SetPixel(hdc, 0, 0, rgb);               // set our value
        pdds->ReleaseDC(hdc);
    }
    
    // now lock the surface so we can read back the converted color
    ddsd.dwSize = sizeof(ddsd);
    while ((hres = pdds->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING);
    
    if (hres == DD_OK) {
        dw = *(DWORD *)ddsd.lpSurface; // get DWORD
        if (ddsd.ddpfPixelFormat.dwRGBBitCount < 32) {
            dw &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount) - 1;  // mask it to bpp
        }
        pdds->Unlock(NULL);
    }
    
    //  now put the color that was there back.
    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK) {
        SetPixel(hdc, 0, 0, rgbT);
        pdds->ReleaseDC(hdc);
    }
    
    return dw;
}
Ejemplo n.º 2
0
void Welcome(LPDIRECTDRAWSURFACE lpdds) {
  const UINT MAXLEN = 85;
  const UINT NUMLINES = 17;
  char message[NUMLINES][MAXLEN] = {
      "Mandlebrot and Julia Set Viewer",
      "",
      "Controls:",
      "Z lets you define a new window to zoom into by clicking and dragging the mouse",
      "UP, DOWN, LEFT, RIGHT arrows scroll the current view",
      "SPACEBAR resets the view to the default window [(-2,-2i),(2,2i)]",
      "ENTER refreshes the current view (also wipes out any orbit diagrams)",
      "M switches to Mandlebrot Set",
      "J switches to Julia Set (after getting the c value for z^2 + c)",
      "C chooses a point in the complex plane to plot in the orbit diagram",
      "O (the letter O, not the number 0) plots the orbit diagram of the specified point ",
      "     (warning... plotting the orbit in a zoomed view will cause unexpected results.",
      "     In order to plot the orbit of a point you choose from a zoomed view, you must",
      "     reset the view with SPACEBAR before pressing O to plot the orbit)",
      "ESCAPE exits the program",
      "",
      "Press SPACEBAR to start"};

  HDC hdc;
  lpdds->GetDC(&hdc);
  for (UINT i = 0; i < NUMLINES; ++i)
    TextOut(hdc, 0, i * 16, message[i], strlen(message[i]));

  lpdds->ReleaseDC(hdc);
}
Ejemplo n.º 3
0
HDC __getScreenDC() {
	HDC hdc;
	if (g_pDDSBack->GetDC(&hdc) == DD_OK) {
		return hdc;
	}
	return NULL;
}
Ejemplo n.º 4
0
void PutText(const char* text,LPDIRECTDRAWSURFACE surface){
  HDC hdc;
  if(SUCCEEDED(surface->GetDC(&hdc))){
    RECT rect;
    rect.left=10; rect.right=g_nScreenWidth-10; 
    rect.top= 10; rect.bottom=g_nScreenHeight; 
    DrawText(hdc,text,-1,&rect,0);
    surface->ReleaseDC(hdc);
  }
}
Ejemplo n.º 5
0
/*
 * DDCopyBitmap:
 *     draw a bitmap into a DirectDrawSurface
 */
HRESULT DDCopyBitmap(LPDIRECTDRAWSURFACE pdds, HBITMAP hbm, int x, int y, int dx, int dy)
{
    HDC                 hdcImage;
    HDC                 hdc;
    BITMAP              bm;
    DDSURFACEDESC       ddsd;
    HRESULT             hr;
    
    if (hbm == NULL || pdds == NULL) {
        return E_FAIL;
    }
    
    // make sure this surface is restored.
    pdds->Restore();
    
    //  select bitmap into a memoryDC so we can use it.
    hdcImage = CreateCompatibleDC(NULL);
    if (!hdcImage) {
        OutputDebugString("createcompatible dc failed\n");
    }
    SelectObject(hdcImage, hbm);
    
    // get size of the bitmap
    GetObject(hbm, sizeof(bm), &bm);    // get size of bitmap
    dx = dx == 0 ? bm.bmWidth  : dx;    // use the passed size, unless zero
    dy = dy == 0 ? bm.bmHeight : dy;
    
    // get size of surface.
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;
    pdds->GetSurfaceDesc(&ddsd);
    
    if ((hr = pdds->GetDC(&hdc)) == DD_OK) {
        StretchBlt(hdc, 0, 0, ddsd.dwWidth, ddsd.dwHeight, hdcImage, x, y, dx, dy, SRCCOPY);
        pdds->ReleaseDC(hdc);
    }
    
    DeleteDC(hdcImage);
    
    return hr;
}
Ejemplo n.º 6
0
//
// CDDrawObject::DrawOnSurface(): Private method to draw stuff on back buffer
//
void CDDrawObject::DrawOnSurface(LPDIRECTDRAWSURFACE pSurface)
{
    DbgLog((LOG_TRACE, 5, TEXT("CDDrawObject::DrawOnSurface() entered"))) ;

    ASSERT(pSurface);
    if (!pSurface) 
        return;
    
    HDC     hDC = 0;
    TCHAR   achBuffer[30] ;
    
    if (DD_OK == pSurface->GetDC(&hDC))
    {
        // write fonr/back to show buffer flipping
        SetBkColor(hDC, RGB(0, 0, 255)) ;
        SetTextColor(hDC, RGB(255, 255, 0)) ;

        wsprintf(achBuffer, TEXT(" %-12s: %6.6d "), 
                 m_bFrontBuff ? m_szFrontMsg : m_szBackMsg, m_iCount) ;
        TextOut(hDC, 20, 10, achBuffer, lstrlen(achBuffer)) ;
        
        // Write the instructions
        SetBkColor(hDC, RGB(0, 0, 0)) ;
        SetTextColor(hDC, RGB(0, 255, 255)) ;
        TextOut(hDC, m_RectScrn.left + 100, m_RectScrn.bottom - 20, m_szDirection, lstrlen(m_szDirection)) ;
        
        // Draw the ball on screen now
        HBRUSH hBrush = (HBRUSH) SelectObject(hDC, m_hBrush) ; // select special brush
        HPEN   hPen   = (HPEN) SelectObject(hDC, m_hPen) ;     // select special pen 

        Ellipse(hDC, m_iBallCenterX - BALL_RADIUS, m_iBallCenterY - BALL_RADIUS, 
                m_iBallCenterX + BALL_RADIUS, m_iBallCenterY + BALL_RADIUS) ;

        SelectObject(hDC, hBrush) ;  // restore original brush
        SelectObject(hDC, hPen) ;    // restore original pen
        
        pSurface->ReleaseDC(hDC) ;  // done now; let go of the DC
    }
}
Ejemplo n.º 7
0
/**
 * Use DirectDraw to return an HDC object for invoking GDI functions
 * on the pixel buffer (could be a screen back buffer or a
 * MIDP off-screen image). The horizontal pitch is assumed to
 * be the same as the (width * sizeof(gxj_pixel_type).
 */
HDC getScreenBufferHDC(gxj_pixel_type *buffer, int width, int height) {
#if JWC_WINCE_USE_DIRECT_DRAW
    /*  pDDS and cachedHDC must both be NULL or both be non-NULL */
    static LPDIRECTDRAWSURFACE pDDS = NULL;
    static HDC cachedHDC = NULL;
    static gxj_pixel_type *cachedBuffer;

    DDSURFACEDESC ddsd;
    HRESULT hRet;

    if (buffer == cachedBuffer && cachedHDC != NULL && !pDDS->IsLost()) {
        /* Note: after screen rotation has happened, the pDDS surface may
         * be lost, even if it's using a client-defined pixel buffer!
         */
        return cachedHDC;
    }

    if (pDDS != NULL && (buffer != cachedBuffer || pDDS->IsLost())) {
        pDDS->ReleaseDC(cachedHDC);
        pDDS->Release();
        pDDS = NULL;
        cachedHDC = NULL;
    }

    ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_LPSURFACE |
                   DDSD_PITCH | DDSD_PIXELFORMAT | DDSD_CAPS;
    ddsd.dwWidth = width;
    ddsd.dwHeight= height;
    ddsd.lPitch  = (LONG)sizeof(gxj_pixel_type) * width;
    ddsd.lpSurface = buffer;
    ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY;

    /* Set up the pixel format for 16-bit RGB (5-6-5). */
    ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
    ddsd.ddpfPixelFormat.dwFlags= DDPF_RGB;
    ddsd.ddpfPixelFormat.dwRGBBitCount = 16;
    ddsd.ddpfPixelFormat.dwRBitMask    = 0x1f << 11;
    ddsd.ddpfPixelFormat.dwGBitMask    = 0x3f << 5;
    ddsd.ddpfPixelFormat.dwBBitMask    = 0x1f;

    /* Create the surface */
    hRet = g_pDD->CreateSurface(&ddsd, &pDDS, NULL);
    if (hRet != DD_OK) {
        pDDS = NULL;
        cachedHDC = NULL;
        return NULL;
    }

    hRet = pDDS->GetDC(&cachedHDC);
    if (hRet != DD_OK) {
        pDDS->Release();
        pDDS = NULL;
        cachedHDC = NULL;
        return NULL;
    }

    cachedBuffer = buffer;
    return cachedHDC;
#endif /* JWC_WINCE_USE_DIRECT_DRAW */
    return NULL;
}
Ejemplo n.º 8
0
/********************************************************************
* Function : SimLoop()
* Purpose : Performs a single Simulation Loop iteration. Includes
*           drawing.
********************************************************************/
int SimLoop(void)
{
	static int nFramesPerSecond = 0;
	static int nFramesSinceLastTick;
	static DWORD LastTicks = 0;
	DWORD Ticks;
	HDC hDC;
	HFONT hOldFont;
	char s[80];
	int slen;

	DDSURFACEDESC ddsd;
	DDBLTFX	BltFx;
	HRESULT ddreturn;

	/* Perform a single step in our world. */
	if (StepWorld(bForwardKey, bBackKey, bLeftKey, bRightKey, nState, nGauge))
	{
		if (lpPrimary->IsLost() == DDERR_SURFACELOST)
			lpPrimary->Restore();

		/* Clear the backbuffer. */
#if CLEARBCKGRND
		BltFx.dwSize = sizeof(BltFx);
		BltFx.dwFillColor = 255;
		ddreturn = lpBackbuffer->Blt(NULL,
											  NULL,
											  NULL,
											  DDBLT_COLORFILL | DDBLT_WAIT,
											  &BltFx);
#else
		ddreturn = DD_OK;
#endif
		if (ddreturn == DD_OK)
		{	/* While this is running, prepare
			 * the drawing. */
			if (PrepDrawWorld())
			{
				/* Lock the surface. */
				memset(&ddsd, 0, sizeof(DDSURFACEDESC));
				ddsd.dwSize = sizeof(ddsd);
				ddreturn = lpBackbuffer->Lock(NULL, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL);
				if (ddreturn == DD_OK)
				{
					DrawWorld((unsigned char *)ddsd.lpSurface, (int)ddsd.lPitch);

					int nX, nY;
					static unsigned char dummy;
					unsigned char ni;
					ni = 0;
					for (nY = 0; nY < 16; nY++)
						for (nX = 0; nX < 16; nX++)
						{
							/* Draw a small block at (nX * 3, nY * 3) */
							((unsigned char *)ddsd.lpSurface)[(nY * 3 * ddsd.lPitch) + (nX * 3)] = ni;
							((unsigned char *)ddsd.lpSurface)[(nY * 3 * ddsd.lPitch) + (nX * 3 + 1)] = ni;
							((unsigned char *)ddsd.lpSurface)[((nY * 3 + 1) * ddsd.lPitch) + (nX * 3)] = ni;
							((unsigned char *)ddsd.lpSurface)[((nY * 3 + 1) * ddsd.lPitch) + (nX * 3 + 1)] = ni;
							ni++;
						}
					lpBackbuffer->Unlock(NULL);

					/* And now write Frames per second. */
					/* Increment Frame counter. */
					nFramesSinceLastTick++;
					/* Get system tick count. */
					Ticks = GetTickCount();
					/* Update fps value every second. */
					if (Ticks > (LastTicks + 1000))
					{	nFramesPerSecond = nFramesSinceLastTick;
						nFramesSinceLastTick = 0;
						LastTicks = Ticks;
					}

					/* Get a DC to the buffer & write count. */
					if (DD_OK == lpBackbuffer->GetDC(&hDC))
					{	
						SetBkMode(hDC, TRANSPARENT);
						hOldFont = SelectObject(hDC, AppFont);
						/* Build a string for display. */
						slen = wsprintf(s, "FPS : %d", nFramesPerSecond);
						/* And draw the text. */
						SetTextColor(hDC, RGB(0,0,0));
						SIZE sz;
						GetTextExtentPoint32(hDC, s, slen, &sz);
						RECT rc;
						rc.top = 0;
						rc.left = 16 * 3;
						rc.right = 16 * 3 + sz.cx + 10;
						rc.bottom = sz.cy + 10;
						DrawFrameControl(hDC, &rc, DFC_BUTTON, DFCS_BUTTONPUSH);
						TextOut(hDC, 16*3 + 5, 5, s, slen);
						SelectObject(hDC, hOldFont);
						lpBackbuffer->ReleaseDC(hDC);
					}
					/* Perform required pageflipping to make the surface
					 * we drawed visible. */
					ddreturn = lpPrimary->Flip(NULL, DDFLIP_WAIT);
					if (ddreturn == DD_OK)
					{
						return 1;
					}
				} 
			}
		}
	}

	return 0;
}