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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
// MFCDirectXDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MFCDirectX.h"
#include "MFCDirectXDlg.h"
#include "mmsystem.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
float CMFCDirectXDlg::xPos1 = 0;
float CMFCDirectXDlg::xPos2 = 0;
// CMFCDirectXDlg dialog
CMFCDirectXDlg::CMFCDirectXDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMFCDirectXDlg::IDD, pParent),_device(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
::ZeroMemory(&_d3dpp, sizeof(_d3dpp));
//on recherche la résolution de l'écran
_ScreenSize.cx = GetSystemMetrics(SM_CXSCREEN);
_ScreenSize.cy = GetSystemMetrics(SM_CYSCREEN);
m_dCurrentTime = 0;
m_dElpasedFrameTime = 0;
m_dElpasedAppTime = 0;
m_dLastTime = 0;
m_pd3dxFont=0;
m_iFrameRate = 60; // ne doit jamais être null;
}
CMFCDirectXDlg::~CMFCDirectXDlg()
{
if( _device )
_device->Release();
}
void CMFCDirectXDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMFCDirectXDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_SIZE()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
// CMFCDirectXDlg message handlers
BOOL CMFCDirectXDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//on met la fenêtre en plein écran au premier au premier plan
::SetWindowPos(m_hWnd, HWND_TOPMOST,0,0,_ScreenSize.cx,_ScreenSize.cy,NULL);
// On défini la région visible de la fenêtre-----------------------------------
HRGN TopRgn;
TopRgn = CreateRectRgn(0,0,(int)_ScreenSize.cx,BANDE_HEIGHT);
SetWindowRgn(TopRgn,false);
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CRect rect;
GetClientRect(&rect);
// Initialize Direct3D (e.g. acquire a IDirect3DDevice9 poniter).
HRESULT hr = initD3D(GetSafeHwnd(), rect.right, rect.bottom, true, D3DDEVTYPE_HAL);
if(FAILED(hr))
{
MessageBox("initD3D() - Failed", "Error");
::PostQuitMessage(0);
}
// Setup the application.
hr = setup(rect.right, rect.bottom);
if(FAILED(hr))
{
MessageBox("setup() - Failed", "Error");
::PostQuitMessage(0);
}
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMFCDirectXDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
m_dCurrentTime = timeGetTime();
m_dElpasedFrameTime = m_dCurrentTime - m_dLastTime; // How much time has passed since the last frame?
m_dElpasedAppTime += m_dElpasedFrameTime; // How much time has passed overall for the application?
m_dLastTime = m_dCurrentTime;
//CDialog::OnPaint();
// Compute the difference: time elapsed in seconds.
CalculateFrameRate();
render();
Invalidate();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMFCDirectXDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
HRESULT CMFCDirectXDlg::initD3D(HWND hwnd, int width, int height, bool windowed, D3DDEVTYPE deviceType)
{
HRESULT hr = 0;
// Step 1: Create the IDirect3D9 object.
IDirect3D9* d3d9 = 0;
d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
if( !d3d9 )
{
::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);
return E_FAIL;
}
D3DDISPLAYMODE d3ddm;
d3d9->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );
// Step 2: Check for hardware vp.
D3DCAPS9 caps;
d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps);
int vp = 0;
if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
{
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
else
{
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
// Step 3: Fill out the D3DPRESENT_PARAMETERS structure.
_d3dpp.Windowed = windowed;
_d3dpp.BackBufferCount = 1;
_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; //D3DSWAPEFFECT_FLIP; //D3DSWAPEFFECT_COPY ;//D3DSWAPEFFECT_DISCARD;
_d3dpp.BackBufferFormat = d3ddm.Format;
_d3dpp.EnableAutoDepthStencil = true;
//_d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
//_d3dpp.AutoDepthStencilFormat = D3DFMT_A8R8G8B8;
_d3dpp.AutoDepthStencilFormat = D3DFMT_R8G8B8;
/*
_d3dpp.BackBufferWidth = width;
_d3dpp.BackBufferHeight = height;
_d3dpp.BackBufferCount = 1;
_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
_d3dpp.MultiSampleQuality = 0;
_d3dpp.hDeviceWindow = hwnd;
*/
_d3dpp.Flags = D3DPRESENTFLAG_DEVICECLIP | D3DPRESENTFLAG_LOCKABLE_BACKBUFFER | D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL; //0;
_d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
//_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
//_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
// Step 4: Create the device.
hr = d3d9->CreateDevice(
D3DADAPTER_DEFAULT, // primary adapter
deviceType, // device type
hwnd, // window associated with device
vp, // vertex processing
&_d3dpp, // present parameters
&_device); // return created device
if( FAILED(hr) )
{
// try again using a safer configuration.
_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
hr = d3d9->CreateDevice(
D3DADAPTER_DEFAULT,
deviceType,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&_d3dpp,
&_device);
if( FAILED(hr) )
{
d3d9->Release(); // done with d3d9 object
::MessageBox(0, "CreateDevice() - FAILED", 0, 0);
return hr;
}
}
d3d9->Release(); // done with d3d9 object
return S_OK;
}
HRESULT CMFCDirectXDlg::setup(int width, int height)
{
if( _device )
{
// Set view matrix.
D3DXMATRIX V;
D3DXVECTOR3 pos (0.0f, 0.0f, -6.0f);
D3DXVECTOR3 target (0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up (0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&V, &pos, &target, &up);
_device->SetTransform(D3DTS_VIEW, &V);
// Use wireframe mode and turn off lighting.
_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
_device->SetRenderState(D3DRS_LIGHTING, false);
// Size the viewport based on window dimensions.
D3DVIEWPORT9 vp = {0, 0, width, height, 0.0f, 1.0f};
_device->SetViewport( &vp );
// Set the projection matrix based on the
// window dimensions.
D3DXMATRIX P;
D3DXMatrixPerspectiveFovLH(
&P,
D3DX_PI * 0.25f,//45-degree field of view
(float)width / (float)height,
1.0f,
1000.0f);
_device->SetTransform(D3DTS_PROJECTION, &P);
if(!m_pd3dxFont)
{
createFont();
}
}
return S_OK;
}
HRESULT CMFCDirectXDlg::cleanup()
{
// Nothing to Destroy.
return S_OK;
}
HRESULT CMFCDirectXDlg::render()
{
if( _device )
{
CString str1;// = "mon text qui défile";
str1.Format("Frame rate:%d",m_iFrameRate);
CString str2= " suite du text sdqsd sdqsd qsdqs dqsdqsd qsdqsd";
float speed1 = 6.0;
// Size the viewport based on window dimensions.
D3DVIEWPORT9 vp1;// = {0, 0, 0 , BANDE_HEIGHT, 0.0f, 1.0f};
vp1.MinZ = 0.0f;
vp1.MaxZ = 1.0f;
vp1.Width =_ScreenSize.cx;
vp1.Height = BANDE_HEIGHT;
vp1.X = 0;
vp1.Y = 0;
_device->SetViewport( &vp1 );
_device->Clear(0, 0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
_device->BeginScene();
DrawTextDefilant(str1, str2, speed1, 5, xPos1);
_device->EndScene();
_device->Present(0, 0, 0, 0);
}
return S_OK;
}
void CMFCDirectXDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if( _device )
{
HRESULT hr = 0;
// On a resize we must change the dimensions of the
// back buffers to match the new window size.
_d3dpp.BackBufferWidth = cx;
_d3dpp.BackBufferHeight = cy;
// We are about to call Reset, free any resources
// that need to be freed prior to a Reset.
hr = cleanup();
if(FAILED(hr))
{
MessageBox("destroy() - Failed", "Error");
::PostQuitMessage(0);
}
// Reset the flipping chain with the new window dimensions.
// Note that all device states are reset to the default
// after this call.
hr = _device->Reset(&_d3dpp);
if(FAILED(hr))
{
MessageBox("Reset() - Failed", "Error");
::PostQuitMessage(0);
}
// Reinitialize resource and device states since we
// Reset everything.
hr = setup(cx, cy);
if(FAILED(hr))
{
MessageBox("setup() - Failed", "Error");
::PostQuitMessage(0);
}
}
}
BOOL CMFCDirectXDlg::OnEraseBkgnd(CDC* pDC)
{
return FALSE;
}
void CMFCDirectXDlg::createFont( void )
{
//
// To create a Windows friendly font using only a point size, an
// application must calculate the logical height of the font.
//
// This is because functions like CreateFont() and CreateFontIndirect()
// only use logical units to specify height.
//
// Here's the formula to find the height in logical pixels:
//
// -( point_size * LOGPIXELSY )
// height = ----------------------------
// 72
//
HRESULT hr;
HDC hDC;
//HFONT hFont;
int nHeight;
int nPointSize = 36;
//char strFontName[] = "Arial";
hDC = ::GetDC( NULL );
nHeight = -( MulDiv( nPointSize, GetDeviceCaps(hDC , LOGPIXELSY), 72 ) );
::ReleaseDC( NULL, hDC );
// Create a font for statistics and help output
hr = D3DXCreateFont( _device, nHeight, 0, FW_BOLD, 0, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"),
&m_pd3dxFont );
if( FAILED( hr ) )
::MessageBox(NULL,"Call to D3DXCreateFont failed!", "ERROR",MB_OK|MB_ICONEXCLAMATION);
}
void CMFCDirectXDlg::DrawTextDefilant(CString & str1, CString & str2, float speed, int iVerticalPos, float & xPos)
{
//static float xPos = 0;
HDC dc = m_pd3dxFont->GetDC();
SIZE size;
SIZE size2;
GetTextExtentPoint32( dc, str1.GetBuffer(), str1.GetLength(), &size );
GetTextExtentPoint32( dc, str2.GetBuffer(), str2.GetLength(), &size2 );
if (xPos < - size.cx - size2.cx)
{
xPos = _ScreenSize.cx;
}
else
{
xPos = xPos - speed;//(speed / m_iFrameRate);
}
RECT destRect1;
SetRect( &destRect1, (int)xPos, iVerticalPos, 0, 0 );
RECT destRect2;
SetRect( &destRect2, (int)xPos + size.cx, iVerticalPos, 0, 0 );
m_pd3dxFont->DrawText( NULL, str1, -1, &destRect1, DT_NOCLIP,
D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
m_pd3dxFont->DrawText( NULL, str2, -1, &destRect2, DT_NOCLIP,
D3DXCOLOR( 0.0f, 0.0f, 1.0f, 1.0f ) );
}
void CMFCDirectXDlg::CalculateFrameRate(void)
{
//
// Report frames per second and the number of objects culled...
//
int iFrameCount = 0;
static int nFrameCount = 0;
++nFrameCount;
static double nTimeOfLastFPSUpdate = 0.0;
if( m_dElpasedAppTime - nTimeOfLastFPSUpdate > 1000 ) // Update once a second
{
m_iFrameRate = (int) (nFrameCount*1000.0/(m_dElpasedAppTime - nTimeOfLastFPSUpdate));
nTimeOfLastFPSUpdate = m_dElpasedAppTime;
nFrameCount = 0;
}
} |
Partager