Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / 3D Theory & Graphics / Transparency don't work with D3DXLoadSurfaceFromFile Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
larjun

March 10, 2005, 03:03 PM

I'm trying to do a 2D game with DirectX9 using only Device and Surfaces no Texture and Vertex. It works fine except...
The game shows a background (3 different bitmapfiles) who moves to the left and my person who walk to the right (Donkey Kong game). Only problem is that my person have a black rectangle around him. Because I can't get the transparency in order??? I will give up soon! Anyone who have any good idea?

This is my pseudocode:

IDirect3D9* g_pDirect3D = NULL;
IDirect3DDevice9* g_pDirect3DDevice = NULL;
IDirect3DSurface9* g_pBackgroundSurface;
IDirect3DSurface9* g_pBackgroundSurface2 = NULL;
IDirect3DSurface9* g_pBackgroundSurface3 = NULL;
IDirect3DSurface9* g_pBackBuffer = NULL;

typedef struct Mr_Type
{
int curr_frame;
...
IDirect3DSurface9* images[Mr_FRAMES];
} *MrX_Ptr;


Create_Mr(MrX_Ptr Mr, int width, int height, int frames)
// Creates Surfaces (not same width and height)
{
for (i=0; iCreateOffscreenPlainSurface(width, height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &Mr->images, NULL); } ... } Load_Frame(MrX_Ptr Mr, int frame, int cx, int cy, char bitmapfile[]) //load only the part of bitmapfile who represent frame (frame=row number) { cx = cx*(Mr->width+1) + 1; cy = cy*(Mr->height+1) + 1; RECT SrcRect; SrcRect.left = cx; SrcRect.top = cy; SrcRect.bottom = cy + bob->height; SrcRect.right = cx + bob->width; //*** Problem or not? *** g_hResult = D3DXLoadSurfaceFromFile(Mr->images[frame], NULL, NULL, bitmapfile, &SrcRect, D3DX_DEFAULT, D3DCOLOR_ARGB(255,0,0,0), NULL); } Render() { g_pDirect3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0x00,0xf0,20), 1.0f, 0 ); g_pDirect3DDevice->BeginScene(); g_pDirect3DDevice->StretchRect(g_pBackgroundSurface, &g_SrcRect[0], g_pBackBuffer, &g_DstRect[0], D3DTEXF_NONE); //background HRESULT hResult = g_pDirect3DDevice->StretchRect(Mr->images[Mr->curr_frame], NULL, g_pBackBuffer, &DstRect, D3DTEXF_NONE); //walking person g_pDirect3DDevice->EndScene(); g_pDirect3DDevice->Present( NULL, NULL, NULL, NULL ); }

 
Nils Pipenbrinck

March 10, 2005, 04:28 PM

well.. I never really worked with off screen surfaces myself. Just here and there where it was nessesary (getting the D3D lag down and playing back videos)

I suggest, that you start to use textures and vertices. It's really easy if you only want to do 2D stuff, and you'll get a lot of extra features.

It's almost weekend. Why don't you dedicate this weekend to quads, triangles and explore the wonders of render and texture stages. Afterwards I'm sure the problem is no problem anymore..

Nils



 
Rui Martins

March 11, 2005, 05:09 AM

How do you set transparency on your Sprite ?

You seem to be using BMPs, which last time I checked doesn't have an Alpha or Mask channel!

Do you do color replacing ?
Reserving one specific RGB code to be swapped by another RGBA code where A is 0 (zero) ?

 
larjun

March 11, 2005, 08:58 AM

Yes bmp-files! Thanks! Faster loading dds. Have saved them now in dds-fileformat with DirectX Texture tool and bring them to Format A8R8G8B8. How do I get black as transparent?
Every color I believe should be transparent only disappear into black color???
---
Load_Frame()
g_hResult = D3DXLoadSurfaceFromFile(Mr->images[frame], NULL, NULL, bitmapfile, &SrcRect, D3DX_DEFAULT, D3DCOLOR_ARGB(0,0,0,0), NULL);
---
Changes of colorvalues:
orig.color after
---------- ------
D3DCOLOR_ARGB(255,0,0,0) black - > black (nothing happens)
D3DCOLOR_ARGB(0,0,0,0) black - > black ""
D3DCOLOR_ARGB(255,0,0,255) blue - > black
D3DCOLOR_ARGB(0,0,0,255) blue - > black
D3DCOLOR_ARGB(255,255,255,255) white - > black
D3DCOLOR_ARGB(0,255,255,255) white - > black

 
juhnu

March 11, 2005, 09:04 AM

actually .bmp supports 32-bit RGBA mode and the d3dx loading functions can load 32-bit bmps too.



juhani

 
Rui Martins

March 11, 2005, 11:12 AM

The format you are using now DDS and aparently BMPs too (according to Juhani) support a Alpha channel.

An Alpha channel is usually explained as a "opaquiness" channel, 255 is fully opaque, 0 is fully transparent.

So wherever you want it to be transparent (fully) just set Alpha of those pixels to 0 (zero).

So everywhere you have your black ABGR=(255,0,0,0) change it to ABGR=(0,0,0,0).

 
Chris

March 11, 2005, 12:53 PM

Did you actually enable alpha blending ?

 
larjun

March 11, 2005, 01:09 PM

It don't work! Have try with

D3DCOLOR_ARGB(255,0,0,0)
D3DCOLOR_ARGB(0,0,0,0)
D3DCOLOR_ARGB(255,0,0,255)
D3DCOLOR_ARGB(0,0,0,255)
D3DCOLOR_ARGB(255,255,255,255)
D3DCOLOR_ARGB(0,255,255,255)

nothing happens except every color who not is black became black when I specified that color.....

 
Illco

March 11, 2005, 03:24 PM

Almost all DX functions return their error or succes as a HRESULT value. Start checking them and see if something fails, as for now you're best guess is... well, a guess. Perhaps it helps you some. You can use the error lookup tool to see which returned value means what.

Also, read the documentation well. StretchRect, as most functions, has a lot of remarks shown. One of which says that the src (offscreen) and target (backbuffer or screen) surfaces should match in format -- did you check that? Another says that both surfaces need to be created with the D3DXPOOL_DEFAULT parameter. Did you check that as well?

There are some more, look it up. And go with checking the return values. It helps. Really.

 
Illco

March 11, 2005, 03:25 PM

Sorry; I misread the documentation a bit (hey it happens). But you should still check all the remarks made in the documentation.

 
larjun

March 12, 2005, 04:01 AM

Sorry but I can only explain with samples. This is from the book 'Microsoft Direct3D Programming'. Sample 'SurfaceApp'.

If you are interesting in solving this problem (or someone else):

Do a projekt called test. Copy the code below into a new cppfile.
-Do one bitmap or dds 640x480 in some color
-Another bitmap or dds 40x80 a small circle filled with green and black outside.
Run and you se what happens. Try to do the small bitmap transparant.

//////////////////////////////////////////////////////
// SurfaceApp.cpp
//////////////////////////////////////////////////////

#include
#include
#include
#include

// 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;
IDirect3DSurface9* g_pBitmapSurface2 = 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(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 = "SurfaceApp";
wc.hIconSm = NULL;

RegisterClassEx(&wc);
}

//////////////////////////////////////////////////////
// CreateAppWindow()
//////////////////////////////////////////////////////
void CreateAppWindow(HINSTANCE hInstance)
{
g_hWnd = CreateWindowEx(
NULL,
"SurfaceApp",
"Direct3D Surface Application",
WS_OVERLAPPEDWINDOW,
100,
100,
648,
514,
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
{
// Use idle time here.
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, "This program won'tnrun 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_REF,
g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DPresentParams, &g_pDirect3DDevice);
if (FAILED(hResult))
{
MessageBox(g_hWnd, "Failed to create Direct3D device.", "Direct3D 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, "image.bmp", NULL, D3DX_DEFAULT, 0, NULL);
if (FAILED(g_hResult))
{
strcpy(g_szErrorMsg, "Couldn't load bitmap file.");
PostQuitMessage(WM_QUIT);
}
g_hResult = g_pDirect3DDevice->CreateOffscreenPlainSurface(40, 80, D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT, &g_pBitmapSurface2, NULL);
if (FAILED(g_hResult))
{
strcpy(g_szErrorMsg, "Error creating bitmap surface.");
PostQuitMessage(WM_QUIT);
}
g_hResult = D3DXLoadSurfaceFromFile(g_pBitmapSurface2, NULL, NULL, "figure.bmp",
NULL, D3DX_DEFAULT, D3DCOLOR(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_pBitmapSurface2)
g_pBitmapSurface2->Release();
if (g_pDirect3DDevice)
g_pDirect3DDevice->Release();
if (g_pDirect3D)
g_pDirect3D->Release();

}
//////////////////////////////////////////////////////
// Render()
//////////////////////////////////////////////////////
void Render()
{
RECT DstRect;
DstRect.left=100;
DstRect.top=200;
DstRect.right=140;
DstRect.bottom=280;
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 image buffer");
PostQuitMessage(WM_QUIT);
}
g_hResult = g_pDirect3DDevice->StretchRect(g_pBitmapSurface2, NULL, pBackBuffer, &DstRect,
D3DTEXF_NONE);
if (FAILED(g_hResult))
{
strcpy(g_szErrorMsg, "Error copying image buffer");
PostQuitMessage(WM_QUIT);
}

g_pDirect3DDevice->Present(NULL, NULL, NULL, NULL);
if (pBackBuffer)
pBackBuffer->Release();

}

 
larjun

March 14, 2005, 12:51 PM

Now I realized that Sprit;-)e:s build on Textures (D3DXCreateTextureFromFileEx) is perfekt when making 2D games.
They have the right properties to make the life easy for those (me) who only live in a life with 2 dimensions.....and are little to old to work in this bransch. Mostly of Direct3D is overkill for 2D games I believe.
Mission complete ... I have found transparency (time to ...;-)


 
This thread contains 12 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.