Version 2.0!
Features
Tutorials
Files
Glossary
Projects
Contact
Links
Message Board
Extras
LuckyCam
Old News
Sign Guestbook
View Guestbook
VB Horoscope
VB Photo Album
.
ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!

Drawing Large Surfaces in DirectX 8
By MetalWarrior aka Jeff Smith

So you've read the tutorials on DX8. You've dabbled a bit in 2D graphics with Direct3D. You're even starting to like this stuff. But alas, one big (and I do mean BIG!) problem remains: texture size constraints!

Okay, stop whining about the pun. Now, what about these texture size constraints? For those of you unfamiliar with the problem, Direct3D requires textures to have a width and/or height that is a power of 2 (2, 4, 8, 16, 32, 64, 128, 256, etc). And on top of that, some cards do not support textures larger than 256x256. This can be somewhat annoying! However, usually it's no big deal, as most sprites can fit into one of these sizes nicely. What happens, though, when we want to step outside these rules? This tutorial is going to show you how.

Before we begin, keep in mind that with this method, we lose the ability to stretch, color key, alpha blend, format convert or clip the image we're copying. Well, what good is it then, you might ask. The answer: Backgrounds. The one main thing that requires a large, non-power of 2 size, is a full screen bitmapped background. And, seeing as that this is a handy thing to use for many games, (or for game menus!), I thought this tutorial was worth writing.

Alright, enough palavering! On with the tutorial :)

Now, to do this trick, we're going to need a couple extra objects than normal. Namely, two surfaces: A surface to hold our background image, and a pointer to the backbuffer. (Yes, it does exist in DX8! And we CAN get to it!) So here's our declarations:


Dim surfBack		As Direct3DSurface8
Dim surfPic		As Direct3DSurface8

In case you didn't notice, they really are surfaces, and not textures! If you've just started with DX8, you may not have used, or even seen, the surface object yet. Well now you will. :)

Now that we have our objects, we just need to fill them. First, create the background image. You can find various ways to do this, but for now we'll use the simplest.


Set surfPic = dev.CreateImageSurface(800, 600, D3DFMT_R5G6B5)

d3dx.LoadSurfaceFromFile _
	surfPic, _
	ByVal 0, _
	ByVal 0, _
	picFile, _
	ByVal 0, _
	D3DX_DEFAULT, _
	0, _
	ByVal 0

These functions are fairly straightforward, for more information on them you can check the SDK. The only thing to note is the CreateImageSurface call, you want to be sure that the size and format matches the backbuffer exactly.

Now we get our backbuffer object:

Set surfBack = dev.GetRenderTarget

That's all there is to it. Now we simply have to copy our background onto the backbuffer, once each gameloop before rendering. And we can do it, in just one call:


dev.CopyRects surfPic, ByVal 0, 0, surfBack, ByVal 0

And that's it! You can place this call either before or after dev.BeginScene in your loop, it won't matter. If you'd like to see a full project with this code in action, as well as all the initialization, and a few added things, just download the sample project here. Have fun ;)

--MetalWarrior