📄 ddrawcximage.cpp
字号:
// ddrawCximage.cpp : Defines the entry point for the application.
// tanzhongqiang1113@hotmail.com
#include "stdafx.h"
#include <ddraw.h> // include directdraw
////CXIMAGE /////////////////////////////////////////////
#include "ximage.h"
// DEFINES ////////////////////////////////////////////////
// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"
// size of window
#define WINDOW_WIDTH 480
#define WINDOW_HEIGHT 272
// basic unsigned types
typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned char UCHAR;
typedef unsigned char BYTE;
// tests if a key is up or down
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
// initializes a direct draw struct
#define DDRAW_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }
// GLOBALS ////////////////////////////////////////////////
HWND main_window_handle = NULL; // globally track main window
int window_closed = 0; // tracks if window is closed
HINSTANCE hinstance_app = NULL; // globally track hinstance
// directdraw stuff
LPDIRECTDRAW4 lpdd = NULL; // dd object
LPDIRECTDRAWSURFACE4 lpddsprimary = NULL; // dd primary surface
LPDIRECTDRAWSURFACE4 lpddsback = NULL; // dd back surface
DDSURFACEDESC2 ddsd; // a direct draw surface description struct
DDSCAPS2 ddscaps; // a direct draw surface capabilities struct
HDC hdc;
HRESULT hr;
// global to hold the bits per pixel
const GUID IID_IDirectDraw4 =
{
0x9c59509a, 0x39bd, 0x11d1, {0x8c, 0x4a, 0x00, 0xc0, 0x4f, 0xd9, 0x30, 0xc5}
};
//////////////////////////////////////////////////////////////////////////
int DDraw_Fill_Surface(LPDIRECTDRAWSURFACE4 lpdds,int color)
{
DDBLTFX ddbltfx; // this contains the DDBLTFX structure
// clear out the structure and set the size field
DDRAW_INIT_STRUCT(ddbltfx);
// set the dwfillcolor field to the desired color
ddbltfx.dwFillColor = color;
// ready to blt to surface
lpdds->Blt(NULL, // ptr to dest rectangle
NULL, // ptr to source surface, NA
NULL, // ptr to source rectangle, NA
DDBLT_COLORFILL | DDBLT_WAIT, // fill and wait
&ddbltfx); // ptr to DDBLTFX structure
// return success
return(1);
} // end DDraw_Fill_Surface
//////////////////////////////////////////////////////////////////////////
int Game_Init(void *parms = NULL, int num_parms = 0)
{
//CxImage cximage(_T("\\SD Memory\\photo\\http_imgload.jpg"),CXIMAGE_FORMAT_JPG);
CxImage cximage(_T("\\SD Memory\\photo\\053.jpg"),CXIMAGE_FORMAT_JPG);
//CxImage cximage(_T("\\SD Memory\\photo\\TIF.tif"),CXIMAGE_FORMAT_TIF);
//CxImage cximage(_T("\\SD Memory\\photo\\eMule.png"),CXIMAGE_FORMAT_PNG);
//CxImage cximage(_T("\\SD Memory\\photo\\GIF.GIF"),CXIMAGE_FORMAT_GIF);
//CxImage cximage(_T("\\SD Memory\\photo\\GPS_DTV_002.bmp"),CXIMAGE_FORMAT_BMP);
LPDIRECTDRAW lpdraw;
if (FAILED(DirectDrawCreate(NULL, &lpdraw, NULL)))
return(0);
lpdraw->QueryInterface(IID_IDirectDraw4,(LPVOID *)&lpdd);
// set cooperation to normal screen
#if 1
if (FAILED(lpdd->SetCooperativeLevel(main_window_handle,
DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
#else
if (FAILED(lpdd->SetCooperativeLevel(main_window_handle,
DDSCL_NORMAL)))
#endif
{
OutputDebugString(L"SetCooperativeLevel normal error");
return(0);
}
// init ddsd
DDRAW_INIT_STRUCT(ddsd);
// enable valid fields
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
// set the backbuffer count field to 1, use 2 for triple buffering
ddsd.dwBackBufferCount = 1;
// request a complex, flippable
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
// create the primary surface
if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsprimary, NULL)))
{
OutputDebugString(L"create the primary surface error");
return(0);
}
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
// get the attached back buffer surface
if (FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsback)))
{
OutputDebugString(L"get lpddsback error");
return(0);
}
// fill back color
DDraw_Fill_Surface(lpddsback,0);
//blt back surface //////////////////////////////////////////////////////////
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;
lpddsback->GetSurfaceDesc(&ddsd);
if ((hr = lpddsback->GetDC(&hdc)) == DD_OK)
{
// just x ,y , nWidth ,nHeitht;
int const nWidth = cximage.GetWidth();
int const nHeight = cximage.GetHeight();
int const nddWidth = ddsd.dwWidth;
int const nddHeitht = ddsd.dwHeight;
const float fImgRatio=(float)nHeight/nWidth;
const float fRatio = (float) nddHeitht/nddWidth;
int XDest, YDest, nDestWidth, nDestHeight;
if(fImgRatio > fRatio)
{
nDestWidth=nddHeitht/fImgRatio;
XDest=(nddWidth-nDestWidth)/2;
YDest=0;
nDestHeight=nddHeitht;
}
else
{
XDest=0;
nDestWidth=nddWidth;
nDestHeight=nddWidth*fImgRatio;
YDest=(nddHeitht-nDestHeight)/2;
}
// just if cximage < surface
//caluculate if pic < THUMB
if ( nWidth < nddWidth )
{
nDestWidth = nWidth;
XDest = (nddWidth - nWidth)/2;
}
if ( nHeight < nddHeitht )
{
nDestHeight = nHeight;
YDest = (nddHeitht - nHeight)/2;
}
cximage.Draw(hdc, XDest, YDest, nDestWidth, nDestHeight);
lpddsback->ReleaseDC(hdc);
}
return(0);
}
//////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
int Game_Main(void *parms = NULL, int num_parms = 0)
{
if (window_closed)
return(0);
// for now test if user is hitting ESC and send WM_CLOSE
if (KEYDOWN(VK_ESCAPE))
{
PostMessage(main_window_handle,WM_CLOSE,0,0);
window_closed = 1;
} // end if
while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
// wait a sec
Sleep(30);
return(0);
}
/////////////////////////////////////////////////////////////
int Game_Shutdown(void *parms = NULL, int num_parms = 0)
{
return(0);
}
////////////////////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
char buffer[80]; // used to print strings
// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
// return success
return(0);
} break;
case WM_PAINT:
{
// simply validate the window
hdc = BeginPaint(hwnd,&ps);
// end painting
EndPaint(hwnd,&ps);
// return success
return(0);
} break;
case WM_DESTROY:
{
// kill the application, this sends a WM_QUIT message
PostQuitMessage(0);
// return success
return(0);
} break;
default:break;
} // end switch
// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
//////////////////////////////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
HDC hdc; // graphics device context
// first fill in the window class stucture
winclass.style = CS_DBLCLKS |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hInstance;
winclass.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON1));
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = _T(WINDOW_CLASS_NAME);
//winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// save hinstance in global
hinstance_app = hInstance;
// register the window class
if (!RegisterClass(&winclass))
return(0);
// create the window
if (!(hwnd = CreateWindowEx(NULL, // extended style
_T(WINDOW_CLASS_NAME), // class
_T("DirectDraw Windowed Plotting Demo"), // title
WS_POPUP | WS_VISIBLE,
// WS_OVERLAPPED | WS_VISIBLE,
0,0, // initial x,y
WINDOW_WIDTH,WINDOW_HEIGHT, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hInstance,// instance of this application
NULL))) // extra creation parms
return(0);
// save main window handle
main_window_handle = hwnd;
// initialize game here
Game_Init();
//
Game_Main();
// enter main event loop
while(TRUE)
{
// for now test if user is hitting ESC and send WM_CLOSE
if (KEYDOWN(VK_ESCAPE))
{
PostMessage(main_window_handle,WM_CLOSE,0,0);
window_closed = 1;
}
// test if there is a message in queue, if so get it
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// test if this is a quit
if (msg.message == WM_QUIT)
break;
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);
} // end if
// main game processing goes here
// Game_Main();
} // end while
// closedown game here
Game_Shutdown();
// return to Windows like this
return(msg.wParam);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -