CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2022
    Posts
    11

    Rendering a sprite in directx9

    Here a basic code for rendering a sprite in a window but it doesn't work, could you help me?

    Code:
    #include <windows.h>
    #include <d3d9.h>
    #include <D3dx9math.h>
    #include <assert.h>
    #define WIN32_LEAN_AND_MEAN
    #define LP_3DDEVICE LPDIRECT3DDEVICE9
    #define LP_3D LPDIRECT3D9
    #define COLOR_ARGB DWORD
    #define SETCOLOR_ARGB(a,r,g,b) ((COLOR_ARGB)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b) & 0xff)))
    #define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
    #define ESC_KEY 0x1B
    
    
    // Global variable
    HINSTANCE hinst;
    HWND hwnd;
    LP_3D direct3d;
    D3DPRESENT_PARAMETERS d3dpp;
    LP_3DDEVICE device3d;
    LPDIRECT3DTEXTURE9 SpImg;
    ID3DXSprite* Sprite;
    
    // Constants
    wchar_t CLASS_NAME[] = L"WinMain";
    wchar_t  APP_TITLE[] = L"Hello World"; // Title bar text
    int Width = 640;
    int Height = 480;
    
    // Function prototypes
    int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
    bool CreateMainWindow(HINSTANCE, int);
    LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);
    
    
    int WINAPI WinMain(HINSTANCE hInstance,
    	HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine,
    	int nCmdShow)
    {
    	MSG msg;
    	if (!CreateMainWindow(hInstance, nCmdShow))
    		return false;
    	int done = 0;
    
    	direct3d = Direct3DCreate9(D3D_SDK_VERSION);
    	ZeroMemory(&d3dpp, sizeof(d3dpp)); // Fill the structure with 0
    	
    	d3dpp.BackBufferWidth = Width;
    	d3dpp.BackBufferHeight = Height;
    	d3dpp.BackBufferCount = 1;
    	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    	d3dpp.hDeviceWindow = hwnd;
    	d3dpp.Windowed = 1;
    	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    
    	direct3d->CreateDevice(
    		D3DADAPTER_DEFAULT,
    		D3DDEVTYPE_HAL,
    		hwnd,
    		D3DCREATE_MIXED_VERTEXPROCESSING,
    		&d3dpp,
    		&device3d);
    	
    	assert(D3DXCreateTextureFromFile(device3d, L"Sprite_002.png", &SpImg) == D3D_OK);
    
    	assert(D3DXCreateSprite(device3d, &Sprite) == S_OK);
    
    	static float lastTime = (float)timeGetTime();
    	while (!done)
    	{
    		// PeekMessage is a non-blocking test for Windows messages
    		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		{
    			// Look for quit message
    			if (msg.message == WM_QUIT)
    				done = 1;
    			// Decode and pass messages on to WinProc
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    		else {
    			float currTime= (float)timeGetTime();
    			float timeDelta = (currTime -lastTime) * 0.001f;
    			D3DXVECTOR3 const SpPos(100, 100, 0);
    			if (device3d)
    			{
    				device3d->Clear(0, NULL, D3DCLEAR_TARGET,D3DCOLOR_XRGB(0, 120, 120), 1.0f, 0L);
    				assert(Sprite->Begin(D3DXSPRITE_ALPHABLEND) == S_OK);
    				assert(Sprite->Draw(SpImg, NULL, NULL, &SpPos, D3DCOLOR_RGBA(255, 255, 255, 255)) == S_OK);
    				Sprite->End();
    
    				device3d->Present(0, 0, 0, 0);
    			}
    			lastTime = currTime;
    			
    		}
    	}
    	return msg.wParam;
    }
    
    
    LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    
    	switch (msg)
    	{
    	case WM_DESTROY:
    		// Tell Windows to kill this program
    		PostQuitMessage(0);
    		return 0;
    	case WM_CHAR: // A character was entered by the
    // keyboard
    		switch (wParam) // The character is in wParam
    		{
    		case ESC_KEY: // Exit program key
    		// Tell Windows to kill this program
    			PostQuitMessage(0);
    			return 0;
    		}
    	}
    	return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    
    
    bool CreateMainWindow(HINSTANCE hInstance, int nCmdShow)
    {
    	WNDCLASSEX wcx;
    	// Fill in the window class structure with parameters
    	// that describe the main window.
    	wcx.cbSize = sizeof(wcx); // Size of structure
    	wcx.style = CS_HREDRAW | CS_VREDRAW;// Redraw if size changes
    	wcx.lpfnWndProc = WinProc; // Points to window procedure
    	wcx.cbClsExtra = 0; // No extra class memory
    	wcx.cbWndExtra = 0; // No extra window memory
    	wcx.hInstance = hInstance; // Handle to instance
    	wcx.hIcon = NULL;
    	wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // Predefined arrow
    	// Background brush
    	wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    	wcx.lpszMenuName = NULL; // Name of menu resource
    	wcx.lpszClassName = CLASS_NAME; // Name of window class
    	wcx.hIconSm = NULL; // Small class icon
    	// Register the window class
    	// RegisterClassEx returns 0 on error
    	if (RegisterClassEx(&wcx) == 0) // If error
    		return false;
    	// Create window
    	hwnd = CreateWindow(
    		CLASS_NAME, // Name of the window class
    		APP_TITLE, // Title bar text
    		WS_OVERLAPPEDWINDOW, // Window style
    		CW_USEDEFAULT, // Default horizontal position of window
    		CW_USEDEFAULT, // Default vertical position of window
    		Width, // Width of window
    		Height, // Height of the window
    		(HWND)NULL, // No parent window
    		(HMENU)NULL, // No menu
    		hInstance, // Handle to application instance
    		(LPVOID)NULL); // No window parameters
    		// If there was an error creating the window
    	if (!hwnd)
    		return false;
    
    	ShowWindow(hwnd, nCmdShow);
    
    	UpdateWindow(hwnd);
    	return true;
    }

  2. #2
    Join Date
    Feb 2022
    Posts
    11

    Re: Rendering a sprite in directx9

    Quote Originally Posted by Quasar999 View Post
    Here a basic code for rendering a sprite in a window but it doesn't work, could you help me?

    Code:
    #include <windows.h>
    #include <d3d9.h>
    #include <D3dx9math.h>
    #include <assert.h>
    #define WIN32_LEAN_AND_MEAN
    #define LP_3DDEVICE LPDIRECT3DDEVICE9
    #define LP_3D LPDIRECT3D9
    #define COLOR_ARGB DWORD
    #define SETCOLOR_ARGB(a,r,g,b) ((COLOR_ARGB)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b) & 0xff)))
    #define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
    #define ESC_KEY 0x1B
    
    
    ..........................................
    
    	UpdateWindow(hwnd);
    	return true;
    }
    Solved:

    Code:
    #include <windows.h>
    #include <d3d9.h>
    #include <D3dx9math.h>
    #include <assert.h>
    #define WIN32_LEAN_AND_MEAN
    #define LP_3DDEVICE LPDIRECT3DDEVICE9
    #define LP_3D LPDIRECT3D9
    #define COLOR_ARGB DWORD
    #define SETCOLOR_ARGB(a,r,g,b) ((COLOR_ARGB)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b) & 0xff)))
    #define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
    #define ESC_KEY 0x1B
    
    // Global variable
    HINSTANCE hinst;
    HWND hwnd;
    LP_3D direct3d;
    D3DPRESENT_PARAMETERS d3dpp;
    LP_3DDEVICE device3d;
    LPDIRECT3DTEXTURE9 SpImg;
    ID3DXSprite* Sprite;
    
    // Constants
    wchar_t CLASS_NAME[] = L"WinMain";
    wchar_t  APP_TITLE[] = L"Hello World"; // Title bar text
    int Width = 640;
    int Height = 480;
    
    // Function prototypes
    int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
    bool CreateMainWindow(HINSTANCE, int);
    LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);
    
    
    int WINAPI WinMain(HINSTANCE hInstance,
    	HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine,
    	int nCmdShow)
    {
    	MSG msg;
    	if (!CreateMainWindow(hInstance, nCmdShow))
    		return false;
    	int done = 0;
    
    	direct3d = Direct3DCreate9(D3D_SDK_VERSION);
    	ZeroMemory(&d3dpp, sizeof(d3dpp)); // Fill the structure with 0
    	
    	d3dpp.BackBufferWidth = Width;
    	d3dpp.BackBufferHeight = Height;
    	d3dpp.BackBufferCount = 1;
    	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    	d3dpp.hDeviceWindow = hwnd;
    	d3dpp.Windowed = 1;
    	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    
    	direct3d->CreateDevice(
    		D3DADAPTER_DEFAULT,
    		D3DDEVTYPE_HAL,
    		hwnd,
    		D3DCREATE_MIXED_VERTEXPROCESSING,
    		&d3dpp,
    		&device3d);
    	
    	assert(D3DXCreateTextureFromFile(device3d, L"Sprite_002.png", &SpImg) == D3D_OK);
    	assert(D3DXCreateSprite(device3d, &Sprite) == S_OK);
    	D3DXMATRIX Mat;
    	D3DXMatrixTransformation2D(&Mat, NULL, 0, NULL, NULL, 0, NULL);
    	static float lastTime = (float)timeGetTime();
    	while (!done)
    	{
    		// PeekMessage is a non-blocking test for Windows messages
    		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		{
    			// Look for quit message
    			if (msg.message == WM_QUIT)
    				done = 1;
    			// Decode and pass messages on to WinProc
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    		else {
    			float currTime= (float)timeGetTime();
    			float timeDelta = (currTime -lastTime) * 0.001f;
    			D3DXVECTOR3 const SpPos(100, 100, 0);
    			if (device3d)
    			{
    				device3d->Clear(0, NULL, D3DCLEAR_TARGET,D3DCOLOR_XRGB(0, 120, 120), 1.0f, 0);
    				device3d->BeginScene();
    				assert(Sprite->Begin(D3DXSPRITE_ALPHABLEND) == S_OK);
    				Sprite->SetTransform(&Mat);
    				assert(Sprite->Draw(SpImg, NULL, NULL, NULL, D3DCOLOR_RGBA(255, 255, 255, 255)) == S_OK);
    				Sprite->End();
    				device3d->EndScene();
    				device3d->Present(NULL, NULL, NULL, NULL);
    			}
    			lastTime = currTime;		
    		}
    	}
    	return msg.wParam;
    }
    
    
    LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch (msg)
    	{
    	case WM_DESTROY:
    		// Tell Windows to kill this program
    		PostQuitMessage(0);
    		return 0;
    	case WM_CHAR: // A character was entered by the
    // keyboard
    		switch (wParam) // The character is in wParam
    		{
    		case ESC_KEY: // Exit program key
    		// Tell Windows to kill this program
    			PostQuitMessage(0);
    			return 0;
    		}
    	}
    	return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    
    
    bool CreateMainWindow(HINSTANCE hInstance, int nCmdShow)
    {
    	WNDCLASSEX wcx;
    	// Fill in the window class structure with parameters
    	// that describe the main window.
    	wcx.cbSize = sizeof(wcx); // Size of structure
    	wcx.style = CS_HREDRAW | CS_VREDRAW;// Redraw if size changes
    	wcx.lpfnWndProc = WinProc; // Points to window procedure
    	wcx.cbClsExtra = 0; // No extra class memory
    	wcx.cbWndExtra = 0; // No extra window memory
    	wcx.hInstance = hInstance; // Handle to instance
    	wcx.hIcon = NULL;
    	wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // Predefined arrow
    	// Background brush
    	wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    	wcx.lpszMenuName = NULL; // Name of menu resource
    	wcx.lpszClassName = CLASS_NAME; // Name of window class
    	wcx.hIconSm = NULL; // Small class icon
    	// Register the window class
    	// RegisterClassEx returns 0 on error
    	if (RegisterClassEx(&wcx) == 0) // If error
    		return false;
    	// Create window
    	hwnd = CreateWindow(
    		CLASS_NAME, // Name of the window class
    		APP_TITLE, // Title bar text
    		WS_OVERLAPPEDWINDOW, // Window style
    		CW_USEDEFAULT, // Default horizontal position of window
    		CW_USEDEFAULT, // Default vertical position of window
    		Width, // Width of window
    		Height, // Height of the window
    		(HWND)NULL, // No parent window
    		(HMENU)NULL, // No menu
    		hInstance, // Handle to application instance
    		(LPVOID)NULL); // No window parameters
    		// If there was an error creating the window
    	if (!hwnd)
    		return false;
    
    	ShowWindow(hwnd, nCmdShow);
    
    	UpdateWindow(hwnd);
    	return true;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured