Sign in to follow this  
sarysa

Transparent color suddenly stopped working

Recommended Posts

Hi, I've just started messing with DirectX programming. I'm learning from a not-so-great book...heh. I'm starting with a 2D game. Anyway, I have this bizarre problem where my transparent color stopped working for no apparent reason. It worked two nights ago, then I merely added code that reads the game's map from a file. (before I just used a non-pointer array as a test case) I haven't changed the code below, which to my knowledge is the only important code when it comes to transparency. I load all bitmaps with the following:
IDirect3DSurface9* getSurfaceFromBitmap(std::string filename, int width, int height)
{
	HRESULT hResult;
	IDirect3DSurface9* surface = NULL;

	// verify that the image exists...the hard way.
	D3DXIMAGE_INFO imageInfo;
	hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);
	if (FAILED(hResult))
		return NULL;

	hResult = pd3dDevice->CreateOffscreenPlainSurface(width, height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
	if (FAILED(hResult))
		return NULL;

	hResult = D3DXLoadSurfaceFromFile(surface, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, D3DCOLOR_XRGB(40, 0, 20), NULL);
	if (FAILED(hResult))
		return NULL;

	return surface;
}
Yesterday, the transparent color worked. Today, it turns black. So now sprites and tiles have the ugly "black box" effect. I tried setting the backbuffer to A8R8G8B8 instead of unknown, that didn't help either. Is there anything I'm missing? Was it just a fluke in DirectX's design that made it work fine before? One more question: Is there anywhere I can find a downloadable DirectX API listing?

Share this post


Link to post
Share on other sites
Have you tried taking out the code that reads the game's map from a file to check if that change is actually what broke it?

Quote:
Is there anywhere I can find a downloadable DirectX API listing?


You mean like the help docs for the API? That is included with the SDK (or was the last time I downloaded it). I think it ussually gets added to your start menu with the rest of the SDK stuff (sample browser etc).

Matt

Share this post


Link to post
Share on other sites
Quote:
Original post by matthughson
Have you tried taking out the code that reads the game's map from a file to check if that change is actually what broke it?

Quote:
Is there anywhere I can find a downloadable DirectX API listing?


You mean like the help docs for the API? That is included with the SDK (or was the last time I downloaded it). I think it ussually gets added to your start menu with the rest of the SDK stuff (sample browser etc).

Matt


Ah, it's there. :) I didn't notice it because it's like a typical help manual...I'm used to the web-based API's like Sun's for Java.

I tried taking out the new code too...that didn't help at all. Is there anything else I need to do (besides the code I already posted) to make them properly transparent? I made sure that all the images do load with that code as well. I've also tested others' DirectX games to make sure it's not an issue with my system.

I tried (also) to use D3DCOLOR_ARGB() and a variety of alpha settings...no dice.

Share this post


Link to post
Share on other sites
Ok, I tried making a test case exactly like the original one I thought worked, retracing my significant steps...it doesn't work. My transparent color turns black.

Here's the entire code for the test case...runs off one .cpp file. I added d3d9.lib and d3dx9.lib. I tested with a image with some red (255,0,0) in it. The red turns black, it doesn't become the background sky blue color. I looked through the API and it didn't help explain why this won't work. What could possibly be wrong with this code? Is it a global feature with DirectX I've forgotten to do?

#include <windows.h>
#include <mmsystem.h>
#include <d3d9.h>
#include <d3dx9tex.h>
#include <string>
using namespace std;

HINSTANCE hInst; // holds the instance for this app
HWND wndHandle; // global window handle

LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;

////////////////////////////////////////////// forward declarations
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

IDirect3DSurface9* character;

// direct3D functions
HRESULT SetupVB();
bool initDirect3D(HWND hwnd);
void shutdown(void);
void render();
IDirect3DSurface9* getSurfaceFromBitmap(std::string filename, int length, int width);

IDirect3DSurface9* getSurfaceFromBitmap(std::string filename, int height, int width)
{
HRESULT hResult;
IDirect3DSurface9* surface = NULL;
D3DXIMAGE_INFO imageInfo;

hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);

if FAILED(hResult)
return NULL;

hResult = pd3dDevice->CreateOffscreenPlainSurface(width, height,
D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
if FAILED(hResult)
return NULL;

hResult = D3DXLoadSurfaceFromFile(surface, NULL, NULL, filename.c_str(),
NULL, D3DX_DEFAULT, D3DCOLOR_ARGB(0xff, 255, 0, 0), NULL);
if FAILED(hResult)
return NULL;

return surface;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
// call our function to init and create our window
if (!initWindow(hInstance))
{
MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
return false;
}

// init direct3d
initDirect3D(wndHandle);

// setup the vertex buffer and add the triangle to it.
SetupVB();

character = getSurfaceFromBitmap("./char.bmp", 720, 192);

// Main message loop:
// Enter the message loop
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
// check for messages
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
// this is called when no messages are pending
else
{
// call our render function
pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0,255,255), 1.0f, 0 );

pd3dDevice->BeginScene();

// render
render();

pd3dDevice->EndScene();

pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
}

// shutdown the directx manager
shutdown();

return (int) msg.wParam;
}

HRESULT SetupVB()
{
return S_OK;
}

bool initDirect3D(HWND hwnd)
{
HRESULT hr;

if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
{
return false;
}

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = 480;
d3dpp.BackBufferWidth = 640;
d3dpp.hDeviceWindow = hwnd;

hr = pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &pd3dDevice );
if FAILED (hr)
{
return false;
}

return true;
}

void render()
{
//pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 255), 1.0f, 0);

RECT dest;
RECT src;

src.top = 0; src.left = 0; src.bottom = 72; src.right = 48;
dest.top = 0; dest.left = 0; dest.bottom = 72; dest.right = 48;

IDirect3DSurface9* backbuffer = NULL;

pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);

pd3dDevice->StretchRect(character, &src, backbuffer, &dest, D3DTEXF_NONE);
}

void shutdown(void)
{
if( pd3dDevice != NULL)
{
pd3dDevice->Release();
pd3dDevice = NULL;
}
if( pD3D != NULL)
{
pD3D->Release();
pD3D = NULL;
}
}

bool initWindow(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "DirectXExample";
wcex.hIconSm = 0;
RegisterClassEx(&wcex);

wndHandle = CreateWindow("DirectXExample",
"DirectXExample",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,
NULL,
hInstance,
NULL);
if (!wndHandle)
return false;

ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);

return true;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}

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