Skip to main content
gamedev.net
🔒 Locked

Visual C++, Embedding images

Started by ovaezy Jan 18, 2005 at 6:26 AM 3 replies 850+ views
Original Post
ovaezy
ovaezy
Hi all! Just a newbie question as to how I can embed my images into a some sort of resource file. At the moment my images are all scattered in the root directory and they need to be moved to the Debug directory if I were to run the program (not through VS Debug). And also I've had some difficulty in making animated sprites with DirectX 9.0 - At the moment I've got 10 images that I change textures when rendering, I've looked up tutorials but they seem to use DirectDraw, but I'm using quads to display my images. Thanks!
Spudder
Spudder
You can only embed bitmap image files into your executable but to add any resource you can right-click on the project name (in solution explorer) then select Add->Add Resource, select the type of resource you want (eg bitmap) then import if you want to use an existing bitmap file. This is for VS 2003 but it should be roughly the same for most versions of VS.

You will then have a file called MyProject.rc which is the resource template containing details about all resources in your project and a header file typically called resource.h which contains the #defines for all the resource IDs, you will need these values for the MAKEINTRESOURCE macro which takes the ID number as the parameter for the resource to load - something like this:

LoadBitmap(AppInstance, MAKEINTRESOURCE(IDB_BITMAP1))

where IDB_BITMAP1 is the resource ID taken from resource.h after adding a new bitmap image.
darookie
darookie
Win2k and later also allow PNG and JPEG files to be embedded as resources. You can, however, embed any kind of binary data into your executable (e.g. DDS images, TGAs or others) and use D3DXLoadSurfaceFromResource to load these textures from resources.

HTH,
Pat.
ovaezy
ovaezy
Nice, got it working, thanks guys!

darookie, you suggested using the LoadSurfaceFromResource to load animated textures right? I've only been using CreateTextureFromResourceEx to load my bitmaps, and these functions don't have the rectangle that LoadSurfaceFromResource has... Is there a way to use CreateTextureFromResourceEx to load up a bitmap that contains animation?

Thanks
darookie
darookie
You can use different texture coordinates, e.g. save your animations in a power-of-two texture and address single frames using texture offsets.

e.g 512 x 256 image size

Texture coordinates:
(0,0)                      +---+---+---+---+---+  | 0 | 1 | 2 | 3 | 4 |  |   |   |   |   |   |  +---+---+---+---+---+  |   |   |   |   |   |  | 5 | 6 | 7 | 8 | 9 |  +---+---+---+---+---+                    (1,1)

So to address your single frames without changig the texture, you can use
tu = 1 / frames_per_row
tv = 1 / frames_per_col

From the above example you'll get tu = 1 / 5 = 0.2 and tv = 1 / 2 = 0.5
So your frames are 0.2 x 0.5 in texture space.
To find out the texture space coordinates of a given frame n, you need to do some simple math:

1) Find out column and row of the frame inside the texture
row = frame_number / frames_per_row
col = frame_number % frames_per_row

2) Find out the actual texture coordinates
u = col * tu (for the example image → col * 0.2)
v = row * tv (for the example image → row * 0.5)

So for your sprite you set the animation texture and set the (u,v) coordinates
for the current animation frame using the above formulas.

I hope thats what you meant,
Pat.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.