Surface managing

Started by
3 comments, last by ColdfireV 24 years ago
Hi everyone... I have a quick question for all of you. How do you all manage your DX off-screen surfaces, such as ones that hold all of your sprite frames, etc.? Do you just create a billion global variables like "lpddsMan", "lpddsTree", etc? It seems rather messy to do it that way, even though I''ve been doing that for some time now. I was wondering if anyone had a better way. ColdfireV
[email=jperegrine@customcall.com]ColdfireV[/email]
Advertisement
I prefer creating a surface managing class that handles the surfaces internally. So the code looks something like

ImageRef * man_image = ImageManager::Get(ID_MAN_IMAGE);

The manager class handles allocation, deallocation, reference counting, etc. Inside the ImageRef classes there''s pointers to the surfaces and offsets within the surface.
Does your image manager store the images using arrays or linked lists, or some other storage method?


ColdfireV
[email=jperegrine@customcall.com]ColdfireV[/email]
It stores surface structures in a hashtable.
Surface structures contain pointers to surfaces and other bookkeeping information.
Each resource bitmap has an ID associated with it.
Each image has an ID of a resource bitmap and an offset on the bitmap.
We have a initialization file that specifies image IDs, the bitmap ID of the image, the offset of the image on the bitmap, and the image size. So the first few lines might be:

#include "image_ids.h"
#include "bitmap_ids.h"
ID_MAN_IMAGE_1 ID_BITMAP_1 0 0 32 32
ID_MAN_IMAGE_2 ID_BITMAP_1 32 0 32 32
ID_COW_IMAGE_1 ID_BITMAP_2 0 0 64 64

So the first time image ID_MAN_IMAGE_1 is requested, the ImageManager checks if bitmap ID_BITMAP_1 has been loaded. If it hasn''t been loaded it creates a new surface structure and initializes it''s reference count, loads the bitmap onto the surface, and hashes into the hash table using ID_BITMAP_1 as the hash key. A new image reference structure is returned pointing to the surface structure and listing the offset as per the data file. Whenever an image reference is deleted, it decrements the surface structure reference count. When a surface structure has no references, it''s considered dead, and can be deleted.

Furthermore the id tables are stored in hash tables as well.

So to answer your question: images are stored in ImageRef objects which refer to surfaces in a hash table.
Hi everyone,
We (Antimatter Research Group) are producing a space invaders clone (due out in like 2-3 weeks now) and well, we had this problem, until we produced a surface manager.

goto this URL to download the source code to the game (as it stands, no final artwork and much game logic missing)

http://members.xoom.com/KoshHome

it''s the Invaders.zip file ok

Now, if you open it up, you''ll see the project as it stands, the project that handles the surface management is the EyeOf TheBeholder (name of our rendering engine).

In there, you''ll find a file called SurfaceManager.cpp

thats the surface manager code.

It''s quite simple, you define a **array of surfaces, like

IDirectDrawSurface4 **surfaces

then, you allocate to that the number of surfaces you need, if you want two, you do this

surfaces = (IDirectDrawSurface4 **)malloc(num_surfaces * sizeof(IDirectDrawSurface4 *));

that will then allow you to access the surfaces like they were an array, so !!

surfaces[0]

is actually a IDirectDrawSurface4 * to a surface, or to most of you

LPDIRECTDRAWSURFACE4 surface

When you add a surface, you just copy out the pointers to a temp array, equalling all the pointers in the array to zero as you do (cause if you dont, the array will still contain the addresses of the surfaces and then when you free it (to stop memory leaks) you''ll end up fucking up the surfaces, free() follows pointers). Create a new list, with num_surfaces one more than it was last time, copy back the pointers, equal the temp pointers to zero, and free them.

easy eh ?

it''s all there in the code, please ask if you need help understanding what''s going on, or if you have anymore problems or questions.

kosh
if understanding it made and born with time, then time, is understandings greatest enemy.....

This topic is closed to new replies.

Advertisement