1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
| #include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
// Texture !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
HRESULT loadimage(char *name,LPD3DXSPRITE &Spr,LPDIRECT3DTEXTURE9 &Texture);
void imax(LPD3DXSPRITE Spr,LPDIRECT3DTEXTURE9 Texture, int x, int y, int xx, int yy, DWORD c);
LPDIRECT3DSURFACE9 back;
LPDIRECT3DTEXTURE9 Map[10];
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPD3DXSPRITE g_Sprite = NULL;
LPDIRECT3DTEXTURE9 g_pTexture = NULL;
// Texture !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <commctrl.h>
#include <commdlg.h>
#include <ddraw.h>
#include <objbase.h>
#include <stdio.h>
HWND g_MainWindowHandle = 0;
IDirect3D9* g_pD3D = NULL;
IDirect3DDevice9* g_pD3DDevice = NULL;
UINT g_WindowWidth = 1280;
UINT g_WindowHeight = 720;
// Some buffers to render a cube
IDirect3DVertexBuffer9* g_CubeVertexBuffer = NULL;
IDirect3DIndexBuffer9* g_CubeIndexBuffer = NULL;
// Vertex definition for our vertex data
struct VertexXYZColor
{
float x, y, z; // Vertex position.
D3DCOLOR color;
static const DWORD VertexFormat = D3DFVF_XYZ|D3DFVF_DIFFUSE; // Flexible vertex format definition for this vertex type
};
const D3DXCOLOR BLACK ( D3DCOLOR_XRGB( 0, 0, 0 ) );
const D3DXCOLOR RED ( D3DCOLOR_XRGB( 255, 0, 0 ) );
const D3DXCOLOR GREEN ( D3DCOLOR_XRGB( 0, 255, 0 ) );
const D3DXCOLOR YELLOW ( D3DCOLOR_XRGB( 255, 255, 0 ) );
// Vertices of a unit cube
VertexXYZColor g_CubeVertexData[4] = {
{ -1.0f, -1.0f, -1.0f, (D3DCOLOR)BLACK },
{ -1.0f, 1.0f, -1.0f, (D3DCOLOR)GREEN },
{ 1.0f, 1.0f, -1.0f, (D3DCOLOR)YELLOW },
{ 1.0f, -1.0f, -1.0f, (D3DCOLOR)RED },
};
WORD g_CubeIndexData[12] =
{
0, 1, 2,
0, 2, 3,
4, 6, 5,
4, 7, 6,
};
// Initialize the windows application.
bool InitWindowsApp( HINSTANCE hInstance, int show );
// Initialize DirectX
bool InitDirectX( HINSTANCE hInstance, int width, int height, bool bWindowed, D3DDEVTYPE deviceType, IDirect3DDevice9** device );
// Setup the application resources
bool Setup();
// Handler for the windows message loop.
int Run();
// Render our scene
void Render();
// Release resources
void Cleanup();
// The windows procedure. This method is used to handle events
// that our window receives.
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
//---------------------------------------------------------------------------------------------------------------------------------------------------
// The main entry point for windows applications.
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nShowCmd )
{
// Create and initialize the windows application.
if ( !InitWindowsApp( hInstance, nShowCmd ) )
{
MessageBox(0, TEXT("Application Initialization Failed"), TEXT("ERROR"), MB_OK );
return 0;
}
if ( !InitDirectX( hInstance, g_WindowWidth, g_WindowHeight, true, D3DDEVTYPE_HAL, &g_pD3DDevice ) )
{
MessageBox( 0, TEXT("Failed to initilize DirectX"), TEXT("ERROR"), MB_OK );
}
if ( !Setup() )
{
MessageBox( 0, TEXT("Failed to setup application resources"), TEXT("ERROR"), MB_OK );
}
int retCode = Run();
Cleanup();
return retCode;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
bool InitWindowsApp( HINSTANCE hInstance, int show )
{
// Create a window description
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc; // Register the callback function for the window procedure
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(NULL));
wc.hCursor = LoadCursor( 0, IDC_ARROW );
wc.hbrBackground= static_cast<HBRUSH>( GetStockObject(WHITE_BRUSH) );
wc.lpszMenuName = NULL;
wc.lpszClassName= TEXT("DirectX_Template");
wc.hIconSm = NULL;
if ( !RegisterClassEx(&wc) )
{
MessageBox( 0, TEXT("Failed to register window class."), NULL, 0 );
return false;
}
// Create a new window using the class description we just registered.
g_MainWindowHandle = CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW, // DWORD dwExStyle
TEXT("DirectX_Template"), // LPCWSTR lpClassName
TEXT("DirectX Template"), // LPCWSTR lpWindowName
WS_OVERLAPPEDWINDOW, // DWORD dwStyle
CW_USEDEFAULT, // int X
CW_USEDEFAULT, // int Y
g_WindowWidth, // int nWidth
g_WindowHeight, // int nHeight
NULL, // HWND hWndParent
NULL, // HMENU hMenu
hInstance, // HINSTANCE hInstance
NULL // LPVOID lpParam
);
if ( g_MainWindowHandle == 0 )
{
MessageBox( 0, TEXT("Failed to create main window"), NULL, 0 );
return false;
}
// And show and update the window we just created
ShowWindow( g_MainWindowHandle, show );
UpdateWindow( g_MainWindowHandle );
return true;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
// This method encapsulates the windows message loop.
int Run()
{
MSG msg;
ZeroMemory( &msg, sizeof(MSG) );
// The message loop will run until the WM_QUIT message is received.
while ( true )
{
if ( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
{
if ( msg.message == WM_QUIT ) break;
// Translate the message and dispatch it to the appropriate
// window procedure.
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
RedrawWindow( g_MainWindowHandle, NULL, NULL, RDW_INTERNALPAINT );
}
}
return msg.wParam;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc( HWND windowHandle, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch ( msg )
{
case WM_PAINT:
{
Render(); // Redraw the window
ValidateRect( windowHandle, NULL );
return 0;
}
break;
case WM_KEYDOWN: // A key was pressed on the keyboard
{
switch ( wParam )
{
case VK_ESCAPE:
{
DestroyWindow( g_MainWindowHandle );
}
break;
}
return 0;
}
break;
case WM_DESTROY:
{
PostQuitMessage( 0 );
return 0;
}
break;
}
// Forward unhandled messages to the default window procedure
return DefWindowProc( windowHandle, msg, wParam, lParam );
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
bool InitDirectX( HINSTANCE hInstance, int width, int height, bool bWindowed, D3DDEVTYPE deviceType, IDirect3DDevice9** device )
{
// Create a Direct3D interface object
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if ( g_pD3D == NULL )
{
MessageBox( 0, TEXT("Failed to create Direct3D interface object."), TEXT("Error"), MB_OK );
return false;
}
// Check for hardware vertex processing
D3DCAPS9 deviceCaps;
g_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &deviceCaps );
int vertexProcessing = 0;
vertexProcessing = D3DCREATE_HARDWARE_VERTEXPROCESSING;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(D3DPRESENT_PARAMETERS) );
d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; // D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = g_MainWindowHandle;
d3dpp.Windowed = true;//false;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
if ( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, deviceType, g_MainWindowHandle, vertexProcessing, &d3dpp, device ) ) )
{
MessageBox( 0, TEXT("Failed to create Direct3D Device"), TEXT("Error"), MB_OK );
return false;
}
loadimage("0.png",g_Sprite,Map[0]); // Texture !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
return true;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
bool Setup()
{
if ( g_pD3DDevice == NULL )
{
MessageBox( 0, TEXT("NULL reference to D3DDevice"), TEXT("Error"), MB_OK );
return false;
}
// Create the vertex buffer and index buffer for our cube
g_pD3DDevice->CreateVertexBuffer( 8 * sizeof(VertexXYZColor), D3DUSAGE_WRITEONLY, VertexXYZColor::VertexFormat, D3DPOOL_MANAGED, &g_CubeVertexBuffer, NULL );
g_pD3DDevice->CreateIndexBuffer( 36 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &g_CubeIndexBuffer, NULL );
// Fill the buffers with the cube data
VertexXYZColor* vertices = NULL;
g_CubeVertexBuffer->Lock( 0, 0, (void**)&vertices, 0 );
memcpy_s(vertices, 8 * sizeof(VertexXYZColor), g_CubeVertexData, 8 * sizeof(VertexXYZColor) );
g_CubeVertexBuffer->Unlock();
// Fill the index buffer with the cube's index data
WORD* indices = NULL;
g_CubeIndexBuffer->Lock( 0, 0, (void**)&indices, 0 );
memcpy_s(indices, 36 * sizeof(WORD), g_CubeIndexData, 36 * sizeof(WORD) );
g_CubeIndexBuffer->Unlock();
// Setup the view matrix
D3DXVECTOR3 cameraPosition( 0.0f, 0.0f, -5.0f );
D3DXVECTOR3 cameraTarget( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 cameraUp( 0.0f, 1.0f, 0.0f );
D3DXMATRIX viewMatrix;
D3DXMatrixLookAtLH( &viewMatrix, &cameraPosition, &cameraTarget, &cameraUp );
g_pD3DDevice->SetTransform( D3DTS_VIEW, &viewMatrix );
// Setup the projection matrix
D3DXMATRIX projectionMatrix;
D3DXMatrixPerspectiveFovLH( &projectionMatrix, D3DX_PI / 4, (float)g_WindowWidth / (float)g_WindowHeight, 0.1f, 100.0f );
g_pD3DDevice->SetTransform( D3DTS_PROJECTION, &projectionMatrix );
// Disable lighting
g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, false );
return true;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
void Cleanup()
{
// Release our resources
if ( g_CubeVertexBuffer != NULL )
{
g_CubeVertexBuffer->Release();
g_CubeVertexBuffer = NULL;
}
if ( g_CubeIndexBuffer != NULL )
{
g_CubeIndexBuffer->Release();
g_CubeIndexBuffer = NULL;
}
// Release the Direct3D device
if ( g_pD3DDevice != NULL )
{
g_pD3DDevice->Release();
g_pD3DDevice = NULL;
}
// Release the Direct3D interface object
if ( g_pD3D != NULL )
{
g_pD3D->Release();
g_pD3D = NULL;
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
void Render()
{
if ( g_pD3DDevice == NULL)
{
return;
}
// Render the scene
g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(55,55,55), 1.0f, 0 );
g_pD3DDevice->BeginScene();
g_pD3DDevice->SetStreamSource( 0, g_CubeVertexBuffer, 0, sizeof(VertexXYZColor) );
g_pD3DDevice->SetIndices( g_CubeIndexBuffer );
g_pD3DDevice->SetFVF( VertexXYZColor::VertexFormat );
g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12 );
//imax(g_Sprite,Map[0],0,0,200,200, 0xFFFFFFFF); // Texture !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
g_pD3DDevice->EndScene();
g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
}
// Texture !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
HRESULT loadimage(char *name,LPD3DXSPRITE &Spr,LPDIRECT3DTEXTURE9 &Texture)
{
if(!g_pd3dDevice) return -1;
if(Spr) Spr->Release();
if(Texture)
{
Texture->Release();
Texture=NULL;
}
D3DXCreateSprite(g_pd3dDevice, &Spr);
return D3DXCreateTextureFromFile(g_pd3dDevice, name, &Texture);
}
// Texture !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void imax(LPD3DXSPRITE Spr,LPDIRECT3DTEXTURE9 Texture, int x, int y, int xx, int yy, DWORD c)
{
D3DXMATRIX scaling_matrix;
RECT r;
D3DXMATRIX Matrix;
D3DXVECTOR2 Translation((float)x,(float)y);
r.left=0;
r.top=0;
r.right=xx;
r.bottom=yy;
Spr->Begin(D3DXSPRITE_ALPHABLEND);//transparent
D3DXMatrixTransformation2D(&Matrix, NULL, NULL, NULL, NULL, 0, &Translation);
Spr->SetTransform(&Matrix);
Spr->Draw(Texture, &r, NULL, NULL, c);
Spr->End();
}
// Texture !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
Partager