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
| //-----------------------------------------------------------------------------
// tripotage de source sample microzoft pour afficher une bête image 2d dans directx
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning
#include <strsafe.h>
#pragma warning( default : 4996 )
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
const int texW = 512, texH = 512;
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices
LPDIRECT3DTEXTURE9 g_pTexture = NULL; // Our texture
LPDIRECT3DTEXTURE9 g_pTextureOffA= NULL; // Our doublebuffer textures
LPDIRECT3DTEXTURE9 g_pTextureOffB= NULL;
LPDIRECT3DTEXTURE9 g_pTextureOff= NULL; // reference to the off texture
bool swap = false;
D3DLOCKED_RECT rect = {NULL,NULL}; // the lockrect
unsigned long int pixTable[texW*texH]; // the pixel table
int scrollX = 0;
RECT myrec = {0,0,texW,texH};
//IDirect3DSurface9 srf = NULL;
IDirect3DSurface9 *psrf = 0;
// vert structure
struct CUSTOMVERTEX
{
D3DXVECTOR3 position; // The position
D3DCOLOR color; // The color
FLOAT tu, tv; // The texture coordinates
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice. Since we are now
// using more complex geometry, we will create a device with a zbuffer.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
// Create the D3DDevice
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
// Turn off D3D lighting
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Create the Textures and vertex buffers
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
// create ze textures
if( FAILED( D3DXCreateTexture(g_pd3dDevice,texW,texH,0,D3DUSAGE_DYNAMIC,D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&g_pTexture) ) )
{
MessageBox( NULL, L"Echec texture creation", L"Textures.exe", MB_OK );
return E_FAIL;
}
if( FAILED( D3DXCreateTexture(g_pd3dDevice,texW,texH,0,D3DUSAGE_DYNAMIC,D3DFMT_X8R8G8B8,D3DPOOL_SYSTEMMEM,&g_pTextureOffA) ) )
{
MessageBox( NULL, L"Echec texture creation", L"Textures.exe", MB_OK );
return E_FAIL;
}
if( FAILED( D3DXCreateTexture(g_pd3dDevice,texW,texH,0,D3DUSAGE_DYNAMIC,D3DFMT_X8R8G8B8,D3DPOOL_SYSTEMMEM,&g_pTextureOffB) ) )
{
MessageBox( NULL, L"Echec texture creation", L"Textures.exe", MB_OK );
return E_FAIL;
}
// Create the vertex buffer.
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4 * sizeof( CUSTOMVERTEX ),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
{
return E_FAIL;
}
// replit le vertex buffer avec mon rectangle. coordonnées pour trianglestrip: 0,2,1 - 2,3,1 - ...
CUSTOMVERTEX* pVertices;
if( FAILED( g_pVB->Lock( 0, 0, ( void** )&pVertices, 0 ) ) )
return E_FAIL;
pVertices[0].position = D3DXVECTOR3( -1.0f , -1.0f , 0.0f );
pVertices[0].color = 0xffffffff;
pVertices[0].tu = 0.0f;
pVertices[0].tv = 1.0f;
pVertices[2].position = D3DXVECTOR3( 1.0f , -1.0f , 0.0f );
pVertices[2].color = 0xffffffff;
pVertices[2].tu = 1.0f;
pVertices[2].tv = 1.0f;
pVertices[3].position = D3DXVECTOR3( 1.0f , 1.0f , 0.0f );
pVertices[3].color = 0xffffffff;
pVertices[3].tu = 1.0f;
pVertices[3].tv = 0.0f;
pVertices[1].position = D3DXVECTOR3( -1.0f , 1.0f , 0.0f );
pVertices[1].color = 0xffffffff;
pVertices[1].tu = 0.0f;
pVertices[1].tv = 0.0f;
g_pVB->Unlock();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( g_pTexture != NULL )
g_pTexture->Release();
if( g_pVB != NULL )
g_pVB->Release();
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
// Define a function that matches the prototype of LPD3DXFILL3D
VOID WINAPI ColorFill (D3DXVECTOR4* pOut, const D3DXVECTOR2* pTexCoord,
const D3DXVECTOR2* pTexelSize, LPVOID pData)
{
*pOut = D3DXVECTOR4(pTexCoord->x, 0.0f, pTexCoord->y, 0.0f);
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
// Clear the backbuffer and the zbuffer
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
// call game routine here
scrollX+=2;
int i,j;
for ( j = 0 ; j < texH ; j++ ) {
for ( i = 0 ; i < texW ; i++ ) {
pixTable[ i + ( j << 9 ) ] = ((i+scrollX)&255)|((j&255)<<8);
}
}
// update one texture
swap = !swap;
if ( swap ) g_pTextureOff = g_pTextureOffA; else g_pTextureOff = g_pTextureOffB;
// essai avec lockrect
//g_pTextureOff->LockRect( 0, &rect, NULL, NULL );
//memcpy( rect.pBits , &pixTable , rect.Pitch * texH );
//g_pTextureOff->UnlockRect( 0 );
// ==> CONSO CPU TROP ELEVEE
// essai avec loadsurfacefrommemory
g_pTextureOff->GetSurfaceLevel( 0, &psrf );
D3DXLoadSurfaceFromMemory( psrf, NULL, &myrec, &pixTable, D3DFMT_X8R8G8B8, texW<<2, NULL , &myrec , D3DX_DEFAULT , 0x00000000 );
// ==> CONSO CPU TROP ELEVEE AUSSI
// display the other texture
if ( swap ) g_pTextureOff = g_pTextureOffB; else g_pTextureOff = g_pTextureOffA;
g_pd3dDevice->UpdateTexture(g_pTextureOff, g_pTexture);
// Setup the world
g_pd3dDevice->SetTexture( 0, g_pTexture );
// Render the vertex buffer contents
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
// End the scene
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Win32 Entry
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
// Register the window class
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
L"D3D Tutorial", NULL
};
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( L"D3D Tutorial", L"D3D Tutorial 05: Textures",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
NULL, NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the scene geometry
if( SUCCEEDED( InitGeometry() ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
while( msg.message != WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
Render();
}
}
}
UnregisterClass( L"D3D Tutorial", wc.hInstance );
return 0;
} |
Partager