Sign in to follow this  

alpha sprites still wont work

This topic is 4490 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

I tried // Set up alpha testing d3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE); d3dDevice->SetRenderState(D3DRS_ALPHAREF, 0); d3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER); // Intialize Alpha Blending d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); d3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); d3dDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); i set d3dfmt_A8R8G8B8 instead of X8R8G8B8 I made 32 bit bmp with A8R8G8B8 settings from photoshops. still in directX it wouldn't make it tranparent it still an image with an opaque background rather than invisible one

Share this post


Link to post
Share on other sites
now that i tried what you asked, it still doesn't work.

Compiler accepts them anywhere but in initDirect3D function, just weird!

it doesn't work with setTextureStageState

also for an info this is 2D image. just plain image of a fish and i want the background to be INVISIBLE!!!!

how do you accomplish such a feat?

Share this post


Link to post
Share on other sites
Hi there busyMe,
How are you doing?

The Problem
Alpha sprites not rendering.

The Solution
Here are a few suggestions

1) Have you created an image with some transparency/alpha component included and saved it in the correct format. try. .png, .bmp

2) Are you rendering the sprites in the correct order. First you should render the solid objects and then the sprites.

3) First try some alpha blending such as
d3dDevice->SetRenderState(ALPHABLENDENABLE, TRUE);
d3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);


I hope some of this helps. It's a bit early here. so I might have said some twisted things ;) have a great day bud.

Share this post


Link to post
Share on other sites
how do I post my code in seperate window?

I just don't understand, thank you for your feedback and help but it's still not working.

I will include my code here... CPP

[SOURCE] 
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <string>

HRESULT lastResult;
LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;
IDirect3DSurface9* bkgrd;
IDirect3DSurface9* sprite;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool initDirect3D(void);
bool initSprites(void);
void render(void);

IDirect3DSurface9* getSurfaceFromBitmap(std::string filename);
IDirect3DSurface9* getSpriteFromBitmap(std::string filename);

#define SPRITE_WIDTH 64
#define SPRITE_HEIGHT 23
#define SCRN_WIDTH 640
#define SCRN_HEIGHT 480
#define MAX_SPRITES 10



struct
{
RECT srcRect;
//position
int posX;
int posY;

int moveX;
int moveY;

int numFrames;
int curFrame;

}spriteStruct[MAX_SPRITES];

HINSTANCE hInst;
HWND wndHandle;
bool initWindow(HINSTANCE hInstance);



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
if(!initWindow(hInstance))
return false;

if(!initDirect3D())
return false;

if(!initSprites())
return false;

bkgrd = getSurfaceFromBitmap("./bkgrd.bmp");

sprite = getSpriteFromBitmap("./fishes.bmp");

MSG msg;

ZeroMemory(&msg, sizeof(msg));
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{

render();
}
}

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

return (int) msg.wParam;
}

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,
SCRN_WIDTH,
SCRN_HEIGHT,
NULL,
NULL,
hInstance,
NULL);

if(!wndHandle)
return false;

ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}

bool initDirect3D(void)
{

pD3D = NULL;
pd3dDevice = NULL;

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

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

if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
wndHandle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pd3dDevice)))
{
lastResult = E_FAIL;
return false;
}
return true;
}

bool initSprites(void)
{
for(int i = 0; i < MAX_SPRITES; i++)
{
spriteStruct[i].srcRect.top = 0;
spriteStruct[i].srcRect.left = i * SPRITE_WIDTH;
spriteStruct[i].srcRect.right = spriteStruct[i].srcRect.left + SPRITE_WIDTH;
spriteStruct[i].srcRect.bottom = SPRITE_HEIGHT;
spriteStruct[i].posX = rand()% SCRN_WIDTH - SPRITE_WIDTH;
spriteStruct[i].posY = rand()% SCRN_HEIGHT - SPRITE_HEIGHT;

spriteStruct[i].curFrame = 0;
spriteStruct[i].numFrames = 4;

spriteStruct[i].moveX = 1;
spriteStruct[i].moveY = 0;
}
return true;
}
IDirect3DSurface9* getSurfaceFromBitmap(std::string filename)
{
HRESULT hResult;
IDirect3DSurface9* surface = NULL;
D3DXIMAGE_INFO imageInfo;

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

if (FAILED(hResult))
return NULL;

hResult = pd3dDevice->CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);

if(FAILED(hResult))
return NULL;

hResult = D3DXLoadSurfaceFromFile(surface, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL);

if(FAILED(hResult))
return NULL;

return surface;
}

IDirect3DSurface9* getSpriteFromBitmap(std::string filename)
{
HRESULT hResult;
IDirect3DSurface9* surface = NULL;
D3DXIMAGE_INFO imageInfo;


hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);
if(FAILED(hResult))
return NULL;

hResult = pd3dDevice->CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
if(FAILED(hResult))
return NULL;

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

return surface;
}

void render(void)
{
IDirect3DSurface9* backbuffer = NULL;

if(NULL == pd3dDevice)
return;

pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

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

pd3dDevice->StretchRect(bkgrd,
NULL,
backbuffer,
NULL,
D3DTEXF_NONE);



for(int i = 0; i < 10; i++)
{

if(spriteStruct[i].curFrame < spriteStruct[i].numFrames-1)
spriteStruct[i].curFrame++;
else
spriteStruct[i].curFrame = 0;


spriteStruct[i].srcRect.left = spriteStruct[i].curFrame * SPRITE_WIDTH;
spriteStruct[i].srcRect.right = spriteStruct[i].srcRect.left + SPRITE_WIDTH;

RECT destRect;
destRect.left = spriteStruct[i].posX;
destRect.top = spriteStruct[i].posY;
destRect.bottom = destRect.top + SPRITE_HEIGHT;
destRect.right = destRect.left + SPRITE_WIDTH;

spriteStruct[i].posX += spriteStruct[i].moveX;

if(spriteStruct[i].posX > SCRN_WIDTH - SPRITE_WIDTH)
{
spriteStruct[i].moveX *= -1;
}
else if(spriteStruct[i].posX < 0)
{
spriteStruct[i].moveX *= -1;
}

spriteStruct[i].posY += spriteStruct[i].moveY;

if(spriteStruct[i].posY > SCRN_HEIGHT - SPRITE_HEIGHT)
{
spriteStruct[i].moveY *= -1;
}
else if(spriteStruct[i].posY < 0)
{
spriteStruct[i].moveY *= -1;
}

pd3dDevice->StretchRect(sprite,
&spriteStruct[i].srcRect,
backbuffer,
&destRect,
D3DTEXF_NONE);
}

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

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);
}
[/SOURCE]
[/source][/source]

THATS it, notice that i removed the ALPHATESTENABLE because they don't work, I tried in initDirect3D and it won't let me compile. If I put it in other functions it will let me compile but it doesn't do anything.

[Edited by - busyme on August 30, 2005 1:03:42 AM]

Share this post


Link to post
Share on other sites
maybe ID3DXSprite library will help

so i included ID3DXSprite spDraw;

then i included spDraw->Begin(D3DXSPRITE_ALPHABLEND);

the thing is that i can't compile the Begin function what am i missing here

Share this post


Link to post
Share on other sites
my initDirect3D looks like this..


[SOURCE]

bool initDirect3D(void)
{

pD3D = NULL;
pd3dDevice = NULL;

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

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

if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
wndHandle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pd3dDevice)))
{
lastResult = E_FAIL;
return false;
}
return true;
}

[/SOURCE]
[/source]

where should I the ALPHABLENDENABLE or where should I ID3DXSprite functions?

Share this post


Link to post
Share on other sites

This topic is 4490 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