Sign in to follow this  

Visual C++, Embedding images

This topic is 4712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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