How to create big surfaces?

Liu Junfeng

Newcomer
Joined
Aug 11, 2003
Messages
12
I want to use a big map in my little game, but the srolling is rather slow.
How to create a surface whose width and height are larger than the display device?
 
In general, you don't want to do that. A surface is essentially a texture which has limits on most cards (256x256 is not uncommon, though you might find 2048x2048 on *some* cards). If you're talking about 2D or 3D with a background, you'll need to piece together multiple surfaces, usually. DirectDraw has some built-in methods, if I remember correctly, for handling large surfaces more easily thand Direct3D. But if you're talking about a game map that's bigger than the screen, you'll need some kind of tile-based layout (the most common).

-Nerseus
 
Thanks for your explanation, currently my code like this:
//------------------------------------------------------------------------
public void DrawTile(int x,int y,Tile pic)
{
int w=pic.Width;
int h=pic.Height;
Bitmap bm=pic.ImageData;
bool cut=false;
if(pic.Width>ScreenWidth-x)
{
w=ScreenWidth-x;
cut=true;
}
if(pic.Height>ScreenHeight-y)
{
h=ScreenHeight-y;
cut=true;
}
if(cut)
{
Rectangle srcRect=new Rectangle(0,0,w,h);
bm=pic.ImageData.Clone(srcRect,pic.ImageData.PixelFormat);//GDI+ Function
}
description.Clear();
description.SurfaceCaps.OffScreenPlain=true;
Surface surface=new Surface(bm,description,display);
back.DrawFast(x,y,surface,DrawFastFlags.Wait);

surface.Dispose();
}
//---------------------------------------------------------------------------
The mixed use of DirectDraw and GDI+ make the draw speed
very slow.
How to solve this problem?
Is there a similar problem in Direct3D?
 
Last edited:
Well, one thing you want to avoid is cloning an image and creating a new surface everytime you draw a tile. :) Create an array of surface tiles (lookup table) that's created when the class is instantiated, then you can just snag the already built surfaces without having to go through the pain of creating them each time you draw.
 
But the x and y position are not foreseeable;
if I use 8 pixels as the step of x and y, then
how many surfaces should I create ?
 
You should create a Surface, not a Bitmap. Put the surface in an array an use:
C#:
public void DrawTile(int x, int y, int SurfNum)
{
    back.DrawFast(x,y,SurfaceArr(SurfNum),DrawFastFlags.Wait);
}
Just load the pictures in to surfaces at the start of the program and then Blt the surfaces instead of creating it each time.
 
I tried so, the image file is 1024*768;divided into 16*16 cells
and create surfaces of 640*480, then more than 100M memory
is used; if use 8*8 cell, outofmemory exception occured.

SO, for a scrolling game, you can't create surfaces for each frame at the same time.

If I create surfaces of size 16*16, the memory is ok, but the drawing speed is even slower.
 
Ah, yeah memory usage. Forgot about that. :)

Instead of holding surfaces in the array, just hold Rectangle structures that describe the image itself, so you can draw it directly off your tile bitmap and not have to create any extra surfaces.

You may also want to take a look at my Map class in my Sharp Invader game. What I did was create a two-dimensional array, which basically was my map. The two-dim array was made up of objects, each describing a tile on the map. They didn't hold surfaces or bitmaps, just info that you could draw based off of. That way you have no extra surface, but just 1 single image of your tiles.

There are also several tutorials on GameDev.net about how to make tile and isometric map engines. You may find them useful. http://www.gamedev.net/reference/list.asp?categoryid=44
 
But there isn't a function such as
Code:
public void Draw(
    Rectangle destRectangle,
    Bitmap source,
    Rectangle srcRectangle,
    DrawFlags flags
);
There only exists:
Code:
public void Draw(
    Rectangle destRectangle,
    Surface sourceSurface,
    Rectangle srcRectangle,
    DrawFlags flags
);
After reading "Using Direct3D For 2D Tile Rendering ", I think I should learn Direct3D first.
 
Well, even so the logic of a tile based map engine is the same (so your map isn't drawn so slowly.. which was one of the problems you were having I believe). You'll just need to convert the Bitmap into a Surface. For that I can't help you. :) I don't know DirectX. Hopefully someone else will pop in and help you out with that.

Also.. have you considered learning DirectDraw instead of Direct3D? If you're doing a simple game then Direct3D would be overkill and is much harder to learn IMO.
 
Back
Top