Sign in to follow this  
d3dxwespe

problem with loading background texture

Recommended Posts

Hi everybody.My name is Tom.Im absolutely begginer at programming.I just started to work on my new and first project,which is called Arkanoid_X.It's going to be a clone of very well known arcade game Arkanoid.I have written "device" and I just can't load the background texture.Does anybody know what is wrong to this code.I can debug code without having any problems.when I click on the EXE file it starts and exits a 1 second after without appearing anything on the screen.I tried to load bmp,dds and tga texture and still nothing.I also tried to change buffer's width and height and textures size....still the same(nothing appears on the screen).Thanks for any help.... Below is the code which a have written....Ps.sory for my english(I'm Polish) #include<windows.h> #include<d3d9.h> #include<d3dx9.h> #include<d3dx9tex.h> #include"resource.h" #include<dxerr9.h> //Function Prototypes LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam); void RegisterWindowClass(HINSTANCE hInstance); void CreateAppWindow(HINSTANCE hInstance); WPARAM StartMessageLoop(); HRESULT InitDirect3D(); void Render(); void CleanUpDirect3D(); //Global Variables HWND g_hWnd; IDirect3D9* g_pDirect3D = NULL; IDirect3DDevice9* g_pDirect3DDevice = NULL; IDirect3DSurface9* g_pBitmapSurface = NULL; HRESULT g_hResult = D3D_OK; char g_szErrorMsg[256]; //////////WinMain()///////// INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,INT) { RegisterWindowClass(hInstance); CreateAppWindow(hInstance); ShowWindow(g_hWnd,SW_SHOWDEFAULT); UpdateWindow(g_hWnd); HRESULT hResult = InitDirect3D(); if (SUCCEEDED(hResult)) WPARAM result = StartMessageLoop(); CleanUpDirect3D(); CloseWindow(g_hWnd); if (g_hResult!= D3D_OK) DXTRACE_ERR(g_szErrorMsg,g_hResult); return 0; } ///////////WndProc()////////// LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { switch(wParam) case WM_KEYDOWN: { case VK_ESCAPE: PostQuitMessage(WM_QUIT); break ; } switch(msg) { case WM_CREATE: return 0; case WM_DESTROY: PostQuitMessage( 0 ); return 0; case WM_PAINT: ValidateRect(g_hWnd,NULL); return 0; case WM_KEYDOWN: switch(wParam) { case VK_ESCAPE: PostQuitMessage(WM_QUIT); break; } } return DefWindowProc(hWnd,msg,wParam,lParam); } //////////RegisterWindowClass()///////// void RegisterWindowClass(HINSTANCE hInstance) { WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = (HCURSOR)LoadCursor(NULL,IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "GameEngine"; wc.hIconSm = NULL; RegisterClassEx(&wc); } //////////CreateAppWindow()///////// void CreateAppWindow(HINSTANCE hInstance) { g_hWnd = CreateWindowEx( NULL, "GameEngine", "GameEngine", WS_OVERLAPPEDWINDOW, 100, 100, 500, 400, GetDesktopWindow(), NULL, hInstance, NULL); } /////////StartMessageLoop()///////// WPARAM StartMessageLoop() { MSG msg; while(1) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { Render(); } } return msg.wParam; } ////////InitDirect3D()///////// HRESULT InitDirect3D() { g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION); if(g_pDirect3D ==NULL) { MessageBox(g_hWnd, "Couldn't create DirectX object.", "DirectX Error",MB_OK); return E_FAIL; } HRESULT hResult = g_pDirect3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF,D3DFMT_X8R8G8B8,D3DFMT_X8R8G8B8,FALSE); if(hResult !=D3D_OK) { MessageBox(g_hWnd, "Sorry,this application won't run on your system", "DirectX Error",MB_OK); return E_FAIL; } D3DPRESENT_PARAMETERS D3DPresentParams; ZeroMemory(&D3DPresentParams,sizeof(D3DPRESENT_PARAMETERS)); D3DPresentParams.Windowed = FALSE; D3DPresentParams.BackBufferCount = 1; D3DPresentParams.BackBufferWidth = 640; D3DPresentParams.BackBufferHeight = 480; D3DPresentParams.BackBufferFormat = D3DFMT_X8R8G8B8; D3DPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD; D3DPresentParams.hDeviceWindow = g_hWnd; hResult = g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,g_hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DPresentParams,&g_pDirect3DDevice); if(FAILED(hResult)) { MessageBox(g_hWnd,"Failed to create Direct3D Device.", "DirectX Error",MB_OK); return E_FAIL; } g_hResult = g_pDirect3DDevice->CreateOffscreenPlainSurface(640,480, D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&g_pBitmapSurface,NULL); if(FAILED(g_hResult)) { strcpy(g_szErrorMsg,"Error creating bitmap surface."); PostQuitMessage(WM_QUIT); } g_hResult = D3DXLoadSurfaceFromFile(g_pBitmapSurface,NULL,NULL, "background.tga",NULL,D3DX_DEFAULT,0,NULL); if(FAILED(g_hResult)); { strcpy(g_szErrorMsg,"Couldn't load bitmap file."); PostQuitMessage(WM_QUIT); } return D3D_OK; } //////CleanUpDirect3D()/////// void CleanUpDirect3D() { if (g_pBitmapSurface) g_pBitmapSurface->Release(); if (g_pDirect3DDevice) g_pDirect3DDevice->Release(); if (g_pDirect3D) g_pDirect3D->Release(); } ////////Render()///////// void Render() { IDirect3DSurface9* pBackBuffer = NULL; g_hResult = g_pDirect3DDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&pBackBuffer); if(FAILED(g_hResult)); { strcpy(g_szErrorMsg,"Error getting back buffer."); PostQuitMessage(WM_QUIT); } g_hResult = g_pDirect3DDevice->StretchRect(g_pBitmapSurface, NULL,pBackBuffer,NULL,D3DTEXF_NONE); if(FAILED(g_hResult)); { strcpy(g_szErrorMsg,"Error copying back buffer."); PostQuitMessage(WM_QUIT); } g_pDirect3DDevice->Present(NULL,NULL,NULL,NULL); if(pBackBuffer) pBackBuffer->Release(); }

Share this post


Link to post
Share on other sites
I noticed a few things:
D3DPresentParams.Windowed = TRUE; //was set to false before
The bitmap might not have been loading and strcpy has caused me trouble in the past. I just use static strings that the messagebox can type when the program is done. Also there are some fail-safe shutdown tricks like using safe release - so you don't have to abort everything all at once. I also noticed some strange things like semicolons that would exclude the enclosed {} response and noticed a couple errors in WndProc. I made some modifications to your code. It should work fine like this:

#define _CRT_SECURE_NO_DEPRECATE
#include<windows.h>
#include<d3d9.h>
#include<d3dx9.h>
#include<d3dx9tex.h>
#include"resource.h"
#include<dxerr9.h>
#define SAFE_RELEASE(x) {if (x) x->Release(); x=NULL;}
#define SAFE_DELETE(p) {if(p) {delete (p); (p)=NULL;}}


LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
void RegisterWindowClass(HINSTANCE hInstance);
void CreateAppWindow(HINSTANCE hInstance);
WPARAM StartMessageLoop();
HRESULT InitDirect3D();
bool Render();
void CleanUpDirect3D();


extern "C" char * Problem = "0 errors ";
HWND g_hWnd;
IDirect3D9* g_pDirect3D = NULL;
IDirect3DDevice9* g_pDirect3DDevice = NULL;
IDirect3DSurface9* g_pBitmapSurface = NULL;
HRESULT g_hResult = D3D_OK;
char g_szErrorMsg[256];



/////////// M A I N ////////////
INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,INT)
{
RegisterWindowClass(hInstance);
CreateAppWindow(hInstance);
ShowWindow(g_hWnd,SW_SHOWDEFAULT);
UpdateWindow(g_hWnd);
HRESULT hResult = InitDirect3D();
if (SUCCEEDED(hResult))
WPARAM result = StartMessageLoop();
CleanUpDirect3D();
CloseWindow(g_hWnd);
if (g_hResult!= D3D_OK)
DXTRACE_ERR(g_szErrorMsg,g_hResult);
MessageBox(g_hWnd,Problem,"DirectX Error",MB_OK);
return 0;
}



///////////WndProc()//////////
LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch (msg) {
case WM_KEYDOWN:
switch(wParam) {
case VK_ESCAPE:
PostQuitMessage(0);
break ;
}
case WM_CREATE:
break;

case WM_PAINT:
ValidateRect(g_hWnd,NULL);
break;

case WM_DESTROY:
PostQuitMessage(1) ;
break ;

default:
return DefWindowProc(hWnd,msg,wParam,lParam);
}
return 0;
}


//////////RegisterWindowClass()/////////
void RegisterWindowClass(HINSTANCE hInstance)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = (HCURSOR)LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GameEngine";
wc.hIconSm = NULL;
RegisterClassEx(&wc);
}


//////////CreateAppWindow()/////////
void CreateAppWindow(HINSTANCE hInstance)
{
g_hWnd = CreateWindowEx(
NULL,
"GameEngine",
"GameEngine",
WS_OVERLAPPEDWINDOW,
100,
100,
500,
400,
GetDesktopWindow(),
NULL,
hInstance,
NULL);
}


/////////StartMessageLoop()/////////
WPARAM StartMessageLoop()
{
MSG msg;
while(1)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
if (msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
if (!Render()) PostMessage(g_hWnd, WM_DESTROY, 0, 0);
}
}
return msg.wParam;
}



////////InitDirect3D()/////////
HRESULT InitDirect3D()
{
g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_pDirect3D ==NULL) {
MessageBox(g_hWnd,
"Couldn't create DirectX object.",
"DirectX Error",MB_OK);
return E_FAIL;
}
HRESULT hResult = g_pDirect3D->CheckDeviceType(D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,D3DFMT_X8R8G8B8,D3DFMT_X8R8G8B8,FALSE);
if(hResult !=D3D_OK) {
MessageBox(g_hWnd,
"Sorry,this application won't run on your system",
"DirectX Error",MB_OK);
return E_FAIL;
}
D3DPRESENT_PARAMETERS D3DPresentParams;
ZeroMemory(&D3DPresentParams,sizeof(D3DPRESENT_PARAMETERS));
D3DPresentParams.Windowed = TRUE;
D3DPresentParams.BackBufferCount = 1;
D3DPresentParams.BackBufferWidth = 640;
D3DPresentParams.BackBufferHeight = 480;
D3DPresentParams.BackBufferFormat = D3DFMT_X8R8G8B8;
D3DPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DPresentParams.hDeviceWindow = g_hWnd;
hResult = g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,g_hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&D3DPresentParams,&g_pDirect3DDevice);
if(FAILED(hResult)) {
MessageBox(g_hWnd,"Failed to create Direct3D Device.",
"DirectX Error",MB_OK);
return E_FAIL;
}
g_hResult = g_pDirect3DDevice->CreateOffscreenPlainSurface(640,480,
D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&g_pBitmapSurface,NULL);
if(FAILED(g_hResult)) {
Problem="Error creating bitmap surface.";
return g_hResult;
}
//make sure this bitmap is in the same directory as your source code
g_hResult = D3DXLoadSurfaceFromFile(g_pBitmapSurface,NULL,NULL,
"background.tga",NULL,D3DX_DEFAULT,0,NULL);
if(FAILED(g_hResult)) {
Problem="Couldn't load bitmap file.";
return g_hResult;
}
return D3D_OK;
}


//////CleanUpDirect3D()///////
void CleanUpDirect3D() {
SAFE_RELEASE(g_pBitmapSurface);
SAFE_RELEASE(g_pDirect3DDevice);
SAFE_RELEASE(g_pDirect3D);
}



////////Render()/////////
bool Render()
{
IDirect3DSurface9* pBackBuffer = NULL;
g_hResult = g_pDirect3DDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&pBackBuffer);
if(FAILED(g_hResult)) {
Problem="Error getting back buffer.";
return false;
}
g_hResult = g_pDirect3DDevice->StretchRect(g_pBitmapSurface,
NULL,pBackBuffer,NULL,D3DTEXF_NONE);
if(FAILED(g_hResult)) {
Problem="Error copying back buffer.";
return false;
}
g_pDirect3DDevice->Present(NULL,NULL,NULL,NULL);
if(pBackBuffer)
pBackBuffer->Release();
return true;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this