Sign in to follow this  

Direct3DX9 - D3DXLoadSurfaceFromFile

This topic is 3378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I've been having some problems with the function D3DXLoadSurfaceFromFile. When I load any file that's having a valid file-format the screen looks like this: http://i36.tinypic.com/2r2tzzp.gif Code: #include <d3d9.h> #include <d3dx9.h> #include <time.h> #include <D3dx9tex.h> #define APPTITLE "Direct3D_Windowed" #define SCREEN_WIDTH 1280 #define SCREEN_HEIGHT 1024 #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) LRESULT WINAPI WinProc(HWND,UINT,WPARAM,LPARAM); ATOM MyRegisterClass(HINSTANCE); int Game_Init(HWND); void Game_Run(HWND); void Game_End(HWND); LPDIRECT3D9 d3d = NULL; LPDIRECT3DDEVICE9 d3ddev = NULL; LPDIRECT3DSURFACE9 backbuffer = NULL; LPDIRECT3DSURFACE9 surface = NULL; LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_DESTROY: Game_End(hWnd); PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, msg, wParam, lParam); } ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WinProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = 0; wc.hCursor = LoadCursor(NULL,IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW +1); wc.lpszMenuName = NULL; wc.lpszClassName = "WindowClass"; return RegisterClass(&wc); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; MyRegisterClass(hInstance); HWND hWnd; hWnd = CreateWindow( "Windowclass", APPTITLE, WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL); if (!hWnd) { MessageBox(NULL, "FEL HÄR! rad 69", "lol", MB_OK); return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); if (!Game_Init(hWnd)) return 0; int done = 0; while(!done) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if(msg.message == WM_QUIT) { MessageBox(hWnd, "Received WM_QUIT message", "WinMain", MB_OK); done = 1; } TranslateMessage(&msg); DispatchMessage(&msg); } else Game_Run(hWnd); } return msg.wParam; } int Game_Init(HWND hwnd) { d3d = Direct3DCreate9(D3D_SDK_VERSION); if(d3d == NULL) { MessageBox(hwnd, "Error initializing Direct3D", "Error", MB_OK); return 0; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = FALSE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.BackBufferWidth = SCREEN_WIDTH; d3dpp.BackBufferHeight = SCREEN_HEIGHT; d3dpp.hDeviceWindow = hwnd; d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev); if(d3ddev == NULL) { MessageBox(hwnd, "Error creating Direct3D device", "Error", MB_OK); return 0; } srand(time(0)); d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0); d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); int result = d3ddev->CreateOffscreenPlainSurface( 100, 100, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL); if(result != D3D_OK) return 1; LPCTSTR lol = "lol.dds"; result = D3DXLoadSurfaceFromFile( surface, NULL, NULL, lol, NULL, D3DX_DEFAULT, 0, NULL); if(result !=D3D_OK) return 1; d3ddev->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE); return 1; } void Game_Run(HWND hwnd) { RECT rect; rect.bottom = 1024; rect.top = 0; rect.left = 0; rect.right = 1280; if(d3ddev == NULL) return; d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0); if(d3ddev->BeginScene()) { d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); d3ddev->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE); d3ddev->EndScene(); } d3ddev->Present(NULL, NULL, NULL, NULL); if(KEY_DOWN(VK_ESCAPE)) PostMessage(hwnd, WM_DESTROY, 0, 0); } void Game_End(HWND hwnd) { if(d3ddev != NULL) d3ddev->Release(); if(d3d != NULL) d3d->Release(); } What's causing this?

Share this post


Link to post
Share on other sites
Quote:
Original post by Hannesnisula
1. Nope
2. It's exactly like the image I linked in my first post, except that's it a lot smaller. But I can open it like a normal .dds file.
Then there's something wrong with the loading rather than the displaying. What format is the file? Is it the same format as the surface you're loading it into (D3DFMT_X8R8G8B8)? What if you use a .bmp file or some other format?

Share this post


Link to post
Share on other sites
Quote:
Original post by Hannesnisula
OMFG, I feel so stupid and I'm really really sorry for wasting your time. The problem was that the image was in the wrong folder... Sorry.
[smile] so was it loading another image or something then?

Share this post


Link to post
Share on other sites
Hmm, I thought D3DXLoadSurfaceFromFile() would fail in that case?

EDIT: Ah, I see - you're returning 1 if it fails instead of 0 [smile]
This is another case where the debug runtimes are invaluable; they'll fill the surface with magenta or green if it's not initialized.

Share this post


Link to post
Share on other sites

This topic is 3378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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