• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
vince0656

Issue With D3DXLoadSurfaceFromFile() In Loading Pngs

8 posts in this topic

Hello there,
I'm pulling out my hair here trying to understand what I am doing wrong but I am having issues in loading and rendering pngs using D3DXLoadSurfaceFromFile() using the D3D9 API. They load but not correctly because they seem to be corrupt after rendering when the files are OK. A good example is from the files attached. The one on the top is loaded into a surface and being rendered using StrechRect(). However as you can see from the original on the bottom, its not doing it correctly and that not to mention that the alpha channel is being used. Therefore, I should only be seeing the cleared back buffer and the silhouette of the heli but I'm not. I don't know if there is anything I'm not doing but here is my initialisation and render code from the sprite class I used to render:
[source lang="cpp"]void MySprite::Initialise(wstring fileName)
{
HRESULT hResult;

if( m_bitmap == nullptr)
{

D3DXIMAGE_INFO imgInfo;


hResult = D3DXGetImageInfoFromFile(
fileName.c_str(),
&imgInfo);

m_bitmapSize.X = imgInfo.Width;
m_bitmapSize.Y = imgInfo.Height;

hResult = Graphics::GetInstance()->getDevice()->CreateOffscreenPlainSurface(
m_bitmapSize.X,
m_bitmapSize.Y,
D3DFMT_A8R8G8B8,
D3DPOOL_DEFAULT,
&m_bitmap,
NULL);

if( FAILED( hResult ) )
{
MessageBox(NULL, L"Unable to create Offscreen surface", L"ERROR", MB_OK);
return;
}

if( fileName != L"" )
{
hResult = D3DXLoadSurfaceFromFile(
m_bitmap,
NULL,
NULL,
fileName.c_str(),
NULL,
D3DX_DEFAULT,
0,
NULL );
if( FAILED( hResult ) )
{
MessageBox(NULL, L"Unable to load image into memory", L"ERROR", MB_OK);
return;
}
}
}
.
if( m_backBuffer == nullptr)
{
hResult = Graphics::GetInstance()->getDevice()->GetBackBuffer(
0,
0,
D3DBACKBUFFER_TYPE_MONO,
&m_backBuffer);
if( FAILED( hResult ) )
{
MessageBox(NULL, L"Unable to Unable to obtain BackBuffer", L"ERROR", MB_OK);
return;
}
}
}

void MySprite::Render(const RECT& destRect)
{

if( m_bitmap != nullptr && m_backBuffer != nullptr)
{
Graphics::GetInstance()->getDevice()->StretchRect(
m_bitmap,
NULL,
m_backBuffer,
&destRect,
D3DTEXF_NONE);
}

}[/source]

I don't know if it is the format of the offscreen surface I'm using or what. BTW: the screen is being cleared but that is being called elsewhere. It is being cleared to D3DCOLOR_XRGB(255,100,0). Also bitmaps load fine so that is not an issue. Thing is I'm new to D3D9 and I'm happy something is rendering but its not doing it correctly.

Here are the images I mentioned:
[img]http://i46.tinypic.com/34g7qqo.jpg[/img]
[img]http://i47.tinypic.com/2r7239z.jpg[/img]
0

Share this post


Link to post
Share on other sites
My guess is that you're rendering with alpha blending disabled.

If you compare the original image with the messed up result, I believe the only areas that are 'wrong' are areas where the PNG can be expected to be fully transparent.

Try opening your PNG in an art package (e.g. GIMP) and removing the alpha channel, and see if you get a match.
0

Share this post


Link to post
Share on other sites
Just because a texture has data in the alpha channel, doesn't mean it will render transparently. You need to enable the alpha-test render-state.

The colours look like that because PNG files just save random RGB values for transparent pixels, because they assume that you'll never see these values.
0

Share this post


Link to post
Share on other sites
I see. I automatically assumed that the API would handle that. That's OK. I've done some research and I found something but when I use the following code the outcome is the same.
[source lang="cpp"]// Enable alpha blending.
lpD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,
TRUE);

// Set the source blend state.
lpD3DDevice->SetRenderState(D3DRS_SRCBLEND,
D3DBLEND_SRCCOLOR);

// Set the destination blend state.
lpD3DDevice->SetRenderState(D3DRS_DESTBLEND,
D3DBLEND_INVSRCCOLOR);[/source]


EDIT: The above states are not working. Question: Does the alpha blending not work if I use StrechRect()? Edited by vince0656
0

Share this post


Link to post
Share on other sites
StretchRect is not a true "rendering" function. I would strongly recommend using ID3DXSprite, which actually renders geometry and supports alpha-blending and transformations (rotation, scaling, etc.).
1

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1353535784' post='5003043']
StretchRect is not a true "rendering" function. I would strongly recommend using ID3DXSprite, which actually renders geometry and supports alpha-blending and transformations (rotation, scaling, etc.).
[/quote]

Thanks for that. I found a site that talked about D3DXSprite (http://celestialcoding.com/tutorials-25/d3d9-tutorial-3-loading-an-image-from-file-and-drawing-it/?wap2) and took the following code:

[source lang="cpp"]//member methods

LPDIRECT3DTEXTURE9 imagetex; //textute our image will be loaded into
LPD3DXSPRITE sprite; //sprite to display our image
D3DXVECTOR3 imagepos; //vector for the position of the sprite

//Initialise

D3DXCreateTextureFromFileW(Graphics::GetInstance()->getDevice(),fileName.c_str(),&imagetex);

D3DXCreateSprite(Graphics::GetInstance()->getDevice(),&sprite);

imagepos.x=0.0f; //coord x of our sprite
imagepos.y=0.0f; //coord y of out sprite

//Render code

Graphics::GetInstance()->getDevice()->BeginScene(); //Call the BeginScene function with our Device and see if it succeeded, all drawing/render code go behind BeginScene and EndScene

sprite->Begin(D3DXSPRITE_ALPHABLEND); //begin our sprite with alphablend so it support alpha, like png use alpha
sprite->Draw(imagetex,NULL,NULL,&imagepos,0xFFFFFFFF); //Draw the sprite, first parameter is the image texture, 4th parameter is the position, last parameter is the color change leave to 0xFFFFFFFF for no change
sprite->End(); //end the sprite
Graphics::GetInstance()->getDevice()->EndScene(); //Call the EndScene function with our Device[/source]

And it works!!!

However. I'm not sure if its best practice. Also its scaling my image now and is bigger than I want it to be which I'm annoyed about.Basically, its not rendering to the original image size but it is rendering correctly now which is good.
0

Share this post


Link to post
Share on other sites
D3DXCreateTextureFromFile will scale your image to power-of-2 dimensions by default, in order to support older GPU's with poor non-power-of-2 support. If you don't care about that, you can use D3DXCreateTextureFromFileEx and tell it not to scale the image.
2

Share this post


Link to post
Share on other sites
Also, about the render states, these should do the trick:

[code]
// Enable alpha blending.
lpD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);

// Set the source blend state.
lpD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
// Set the destination blend state.
lpD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
[/code]
0

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1353547457' post='5003088']
D3DXCreateTextureFromFile will scale your image to power-of-2 dimensions by default, in order to support older GPU's with poor non-power-of-2 support. If you don't care about that, you can use D3DXCreateTextureFromFileEx and tell it not to scale the image.
[/quote]

Thank you so much. I now have a working sprite blitting class as a result.

Thanks also to everyone else for their input. I am going to have fun with this now!
1

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  
Followers 0