why I get a white screen ??

Started by
4 comments, last by cannonicus 18 years, 5 months ago
I just want to render a texture in screen with sprite for testing.

#include <d3d9.h>
#include <d3dx9.h>
#include <conio.h>
//#include <windows.h>

LRESULT CALLBACK WndProc  (HWND hwnd, UINT msg, WPARAM w,LPARAM l)
{
	return DefWindowProc(hwnd, msg ,w , l);
}

HWND createWin()
{
	//
	// Create the main application window.
	//

	WNDCLASS wc;

	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = (WNDPROC)WndProc; 
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = GetModuleHandle(0);
	wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
	wc.hCursor       = LoadCursor(0, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName  = 0;
	wc.lpszClassName = "Direct3D9App";

	if( !RegisterClass(&wc) ) 
	{
		::MessageBox(0, "RegisterClass() - FAILED", 0, 0);
		return false;
	}


	HWND hwnd = ::CreateWindow("Direct3D9App", "&#24352;&#38632;", 
		WS_EX_TOPMOST,
		0, 0, 800, 600,
		0 /*parent hwnd*/,
		0 /* menu */,
		GetModuleHandle(0), 
		0 /*extra*/);

	if( !hwnd )
	{
		::MessageBox(0, "CreateWindow() - FAILED", 0, 0);
		return false;
	}



	::ShowWindow(hwnd, SW_SHOW);
	::UpdateWindow(hwnd);
	return hwnd;
}

void texture(LPDIRECT3DDEVICE9 d , LPDIRECT3DTEXTURE9 * t)
{
	if(FAILED(D3DXCreateTextureFromFile(d,"sprite.jpg",t))) MessageBox(0,"sprite load failed",0,0);
	d->SetTexture(0,*t);
}

void sprite(IDirect3DDevice9 * d)
{
	LPD3DXSPRITE s = 0;
	if(FAILED(D3DXCreateSprite(d,&s))) return;
	
	LPDIRECT3DTEXTURE9 t;

	texture(d,&t);

	s->Begin(D3DXSPRITE_ALPHABLEND);
	s->Draw(t,0,0,0,0xFFFFFFFF);
	s->End();

	s->Release();
}


main()
{
	HWND hwnd = createWin();
	IDirect3D9 * d3d = Direct3DCreate9(D3D_SDK_VERSION);
	IDirect3DDevice9 * d = 0;

	D3DPRESENT_PARAMETERS d3dpp;
	d3dpp.BackBufferWidth            = 800;
	d3dpp.BackBufferHeight           = 600;
	d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
	d3dpp.BackBufferCount            = 1;
	d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
	d3dpp.MultiSampleQuality         = 0;
	d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
	d3dpp.hDeviceWindow              = hwnd;
	d3dpp.Windowed                   = 1;
	d3dpp.EnableAutoDepthStencil     = true; 
	d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
	d3dpp.Flags                      = 0;
	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
	d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;

	d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 0  , D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d);
	
	//d3d->Release();

	//d->BeginScene();
	//d->Clear(0, 0, D3DCLEAR_TARGET||D3DCLEAR_ZBUFFER, 0x0, 1, 0);
	
	sprite(d);

	//d->Present(0,0,0,0);
//	d->EndScene();
	getch();
	
//	d->Release();
}








another question How to rotate a sprite like below? [Edited by - derek7 on November 26, 2005 9:21:46 AM]
Advertisement
You are going to need to called Direct3DDevice9::BeginScene before you call sprite, and then Direct3DDevice9::EndScene and Direct3DDevice9::Present afterwards. You'll probably want to so that in a loop too:


while(true)
{
d->BeginScene();
d->Clear(0, 0, D3DCLEAR_TARGET||D3DCLEAR_ZBUFFER, 0x0, 1, 0);

sprite(d);

d->EndScene();
d->Present(0,0,0,0);
}

You'll want to change sprite to create the sprite and texture else where, as well as releasing them after the loop. I'd suggest you go through the direct x tutorials in the sdk.
thanks It work.

but the other one question still have no solution.

and a new question arise .

the sprite.jpg enlarge! It show ok in explorer or photoshop .but it enlarge in sprite.

why?

I use LCD screen. and resolution is 1280 x 1024. How to show real size in D3DXSPRITE?
d3dx9 library provides functionality for this one.

D3DXVECTOR2 scaling;float angle;D3DXVECTOR2 translation;D3DXMATRIX transform;D3DXMATRIXTransformation2D(&transform, //output matrix&D3DXVECTOR2(0,0), //scaling center0.0f, //Scaling rotation factor (not sure what this does)&scaling, //scaling &D3DXVECTOR2(0,0), //Rotation centerangle, //Rotation&translation  //translation);


This code outputs the matrix transform matrix with the desired scaling, rotation and translation (movement). In the above example the rotation center is the objects zero vector (0,0), but you can enter eny desired value.

To use the transformation matrix you must apply it to the directx rendering pipeline first like this (this must be done before drawing a spite):

s->SetTransform(&transform);//To reset the world transformation matrix:D3DXMATRIX I;D3DXMatrixIdentity(&I);s->SetTransform(&I);


//emil
Emil Jonssonvild
#include <d3d9.h>#include <d3dx9.h>#include <conio.h>//#include <windows.h>LRESULT CALLBACK WndProc  (HWND hwnd, UINT msg, WPARAM w,LPARAM l){	return DefWindowProc(hwnd, msg ,w , l);}HWND createWin(){	//	// Create the main application window.	//	WNDCLASS wc;	wc.style         = CS_HREDRAW | CS_VREDRAW;	wc.lpfnWndProc   = (WNDPROC)WndProc; 	wc.cbClsExtra    = 0;	wc.cbWndExtra    = 0;	wc.hInstance     = GetModuleHandle(0);	wc.hIcon         = LoadIcon(0, IDI_APPLICATION);	wc.hCursor       = LoadCursor(0, IDC_ARROW);	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);	wc.lpszMenuName  = 0;	wc.lpszClassName = "Direct3D9App";	if( !RegisterClass(&wc) ) 	{		::MessageBox(0, "RegisterClass() - FAILED", 0, 0);		return false;	}	HWND hwnd = ::CreateWindow("Direct3D9App", "&#24352;&#38632;", 		WS_EX_TOPMOST,		0, 0, 800, 600 ,		0 /*parent hwnd*/,		0 /* menu */,		GetModuleHandle(0), 		0 /*extra*/);	if( !hwnd )	{		::MessageBox(0, "CreateWindow() - FAILED", 0, 0);		return false;	}	::ShowWindow(hwnd, SW_SHOW);	::UpdateWindow(hwnd);	return hwnd;}void texture(LPDIRECT3DDEVICE9 d , LPDIRECT3DTEXTURE9 * t){	if(FAILED(D3DXCreateTextureFromFile(d,"sprite.bmp",t))) MessageBox(0,"sprite load failed",0,0);	d->SetTexture(0,*t);}LPD3DXSPRITE s = 0;LPDIRECT3DTEXTURE9 t;void sprite(IDirect3DDevice9 * d){		if(FAILED(D3DXCreateSprite(d,&s))) return;		texture(d,&t);	}main(){	HWND hwnd = createWin();	IDirect3D9 * d3d = Direct3DCreate9(D3D_SDK_VERSION);	IDirect3DDevice9 * d = 0;	D3DPRESENT_PARAMETERS d3dpp;	d3dpp.BackBufferWidth            = 800;	d3dpp.BackBufferHeight           = 600;	d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;	d3dpp.BackBufferCount            = 1;	d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;	d3dpp.MultiSampleQuality         = 0;	d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 	d3dpp.hDeviceWindow              = hwnd;	d3dpp.Windowed                   = 1;	d3dpp.EnableAutoDepthStencil     = true; 	d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;	d3dpp.Flags                      = 0;	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;	d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;	d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 0  , D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d);		d3d->Release();		sprite(d);	while(1)	{		d->BeginScene();		d->Clear(0, 0, D3DCLEAR_TARGET||D3DCLEAR_ZBUFFER, 0x0, 1, 0);		// Texture being used is 64 by 64:		D3DXVECTOR2 spriteCentre = D3DXVECTOR2(150,150);		// Screen position of the sprite		D3DXVECTOR2 trans=D3DXVECTOR2(0,0);		// Rotate based on the time passed		//float rotation=3.14f*timeGetTime()/1000;		float rotation=0;		// Build our matrix to rotate, scale and position our sprite		D3DXMATRIX mat;		//D3DXVECTOR2 scaling(sin(float(timeGetTime()/100)),sin(float(timeGetTime()/100)));		D3DXVECTOR2 scaling(1,1);		// out, scaling centre, scaling rotation, scaling, rotation centre, rotation, translation		D3DXMatrixTransformation2D(&mat,NULL,0.0,&scaling,&spriteCentre,rotation,&trans);		// Tell the sprite about the matrix		D3DXMatrixIdentity(&mat);	    s->SetTransform(&mat);		s->Begin(D3DXSPRITE_ALPHABLEND);		s->Draw(t,0,&D3DXVECTOR3(0,0,0), &D3DXVECTOR3(0,0,0), 0xFFFFFFFF);		s->End();			d->EndScene();		d->Present(0,0,0,0);	}	s->Release();	getch();	//	d->Release();}


It still has enlarged. You could compiler the code and see what happen!
This line of code:

D3DXMatrixIdentity(&mat);

Shouldnt be called before the settransform call. This function loads a identity matrix into mat (identity matrix = no transform at all), and thus discards the desired transform obtained from D3DXMatrixTransformation2D(...). Set the sprites transformationmatrix to the identity matrix only when you dont want any transformation anymore.

Or am i missing something?

//Emil
Emil Jonssonvild

This topic is closed to new replies.

Advertisement