• 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
kkrz

loading bitmaps into memory D2D

3 posts in this topic

this question pertains to loading bitmaps into memory with Direct2D. basically i am planning to have a huge bitmap be the terrain for my 2d world. it's going to be too massive to load the entire thing into memory at once.

i know that you can load a large bitmap and then use source rectangles to only render a certain section of it, but i believe that still requires loading the entire bitmap at once.

i'm currently using this technique to load a bitmap from a file into a ID2D1Bitmap*:
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd756686%28v=vs.85%29.aspx"]http://msdn.microsof...v=vs.85%29.aspx[/url]

Is it possible to only load a small part of the bitmap into memory at once? How would I go about doing this?
0

Share this post


Link to post
Share on other sites
Is it really needed to have the texture in one large bitmap?

Why not chunk it into that kind of parts that you can handle well (from a performance view)?

When drawing/using the bitmap you simply use the chunks.


I don't know a way to do this with WIC, maybe doing a special copy or so (telling WIC about the big image, which isn't loaded at that time, then copying a part of it where WIC has to decode/open it anyway)... but I don't think that you can get WIC to a point where it doesn't load the whole image into memory, especially think about how e.g. jpeg works... or are we still talking about raw bitmaps?

If you are only using raw bitmaps there is an option, you can bypass the file loading of WIC and manually load you WIC Image (later used for D2D, where you may make a copy to a d2d hardware render target for better performance, since everything with WIC and D2D uses d2d software). So manually loading the WIC image can be done with the stream com interface (at least), you simply have to load the bitmap by yourself. That shouldn't be that problem. You read the file by yourself (look up bitmap format), then you get the pixels (only that part that you want, this is your primary requirement) into your WIC image (maybe you will lock the image instead of using the stream, which may be better) and finally use it with d2d. When using the stream you have to manually adding the correct bitmap header (this may also be easily possible, but some additional work). With lock or so it should be straightforward I think.

Sorry if my thoughts are a little bit confusing, just some ideas that I got when writing the text.

Tell us what you have chosen and we may be able to help you with the implementation.

Vertex
0

Share this post


Link to post
Share on other sites
It doesn't have to be one large bitmap, I'm still trying to figure out the best way to accomplish this.

The Direct2D documentation says:
[i]Video cards typically have a minimum memory allocation size. If an allocation is requested that is smaller than this, a resource of this minimum size is allocated and the surplus memory is wasted and unavailable for other things. ... We recommend that you try to keep most bitmaps to at least 64 KB and limit the number of bitmaps that are smaller than 4 KB.[/i]

I understand this to mean that if the image is over 64 KB memory will not be wasted. What is the advantage to breaking down the images into parts? For example, let's say I have 200 PNG files representing the terrrain and load them separately as so:

[CODE]
ID2D1Bitmap* m_terrainBitmap[200];

HRESULT hr = LoadBitmapFromFile(
m_renderTarget,
m_WICFactory,
L"terrain0.png",
0,
0,
&m_terrainBitmap[0]);

hr = LoadBitmapFromFile(
m_renderTarget,
m_WICFactory,
L"terrain1.png",
0,
0,
&m_terrainBitmap[1]);

//Repeat for all 200 of the terrain PNG files.
//...

[/CODE]

LoadBitmapFromFile is the same exact one shown in the MSDN samples:

[CODE]
HRESULT LoadBitmapFromFile(
ID2D1RenderTarget *pRenderTarget,
IWICImagingFactory *pIWICFactory,
PCWSTR uri,
UINT destinationWidth,
UINT destinationHeight,
ID2D1Bitmap **ppBitmap
)
{
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICStream *pStream = NULL;
IWICFormatConverter *pConverter = NULL;
IWICBitmapScaler *pScaler = NULL;

HRESULT hr = pIWICFactory->CreateDecoderFromFilename(
uri,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pDecoder
);

if (SUCCEEDED(hr))
{
// Create the initial frame.
hr = pDecoder->GetFrame(0, &pSource);
}
if (SUCCEEDED(hr))
{

// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = pIWICFactory->CreateFormatConverter(&pConverter);

}


if (SUCCEEDED(hr))
{
// If a new width or height was specified, create an
// IWICBitmapScaler and use it to resize the image.
if (destinationWidth != 0 || destinationHeight != 0)
{
UINT originalWidth, originalHeight;
hr = pSource->GetSize(&originalWidth, &originalHeight);
if (SUCCEEDED(hr))
{
if (destinationWidth == 0)
{
FLOAT scalar = static_cast<FLOAT>(destinationHeight) / static_cast<FLOAT>(originalHeight);
destinationWidth = static_cast<UINT>(scalar * static_cast<FLOAT>(originalWidth));
}
else if (destinationHeight == 0)
{
FLOAT scalar = static_cast<FLOAT>(destinationWidth) / static_cast<FLOAT>(originalWidth);
destinationHeight = static_cast<UINT>(scalar * static_cast<FLOAT>(originalHeight));
}

hr = pIWICFactory->CreateBitmapScaler(&pScaler);
if (SUCCEEDED(hr))
{
hr = pScaler->Initialize(
pSource,
destinationWidth,
destinationHeight,
WICBitmapInterpolationModeCubic
);
}
if (SUCCEEDED(hr))
{
hr = pConverter->Initialize(
pScaler,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
}
}
else // Don't scale the image.
{
hr = pConverter->Initialize(
pSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
}
if (SUCCEEDED(hr))
{

// Create a Direct2D bitmap from the WIC bitmap.
hr = pRenderTarget->CreateBitmapFromWicBitmap(
pConverter,
NULL,
ppBitmap
);
}

SafeRelease(&pStream);
SafeRelease(&pScaler);
SafeRelease(&pConverter);
SafeRelease(&pSource);
SafeRelease(&pDecoder);

return hr;
}
[/CODE]

Won't they still all be loaded into memory the same as if I had just used one huge PNG file? I would only like to load them into memory as needed (like when the player reaches the edge of the screen and I need to load a different area of the terrain.)

To tell you the truth, I really don't know much about WIC. I've only learned to load an image this way using the MSDN tutorials. I'm not an expert with image formats, but I would like to have a high quality image for the terrain, and I've been told that PNG is a high-quality image format.

Do you think that manually reading the bytes of the image file and processing it myself would be the only way to accomplish what I need?
0

Share this post


Link to post
Share on other sites
Another concern I have is that the Direct2D documentation also states:

[i]Resource creation and deletion on hardware are expensive operations because they require lots of overhead for communicating with the video card.[/i]

Let's assume that I created many different PNG files for the terrain using the code shown above. When the player gets the edge of the screen, I would need to load in another bitmap and free the memory being used by the old bitmap. Wouldn't all of this creation/deletion be expensive and inefficient?

This must be a common problem for people who create 2D games where a player moves around a world. Could anyone shed some light on the best way to go about this?
0

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