Using bitmap resources (*.rc) in Visual Studio 2010

Started by
7 comments, last by adeyblue 11 years, 4 months ago

Hi, I am trying to find a way to use bitmaps in my game without having to load files from the hard disk at runtime. I can include bitmap files into a *.rc, but I have searched on the web for hours and cannot find a way to access the images from code. Nothing I have tried has worked.

This is the "name" that my file "TryRes.rc" has given my bitmap:

IDB_BITMAP1

This identified throws a compiler error, saying it is "undeclared".

I am using DirectX10 for graphics; my intention is to use "CreateTextureFromResource" once I am able to access the images...unless someone knows a better way to do this. The idea is to be able to port the executable over to another computer and still be able to run it without all the extra graphics files.

Advertisement

I think it would help the reader if you provided some specific code or compling errors ;) It's quite hard right now to see the problem.

Hi, I am trying to find a way to use bitmaps in my game without having to load files from the hard disk at runtime. I can include bitmap files into a *.rc, but I have searched on the web for hours and cannot find a way to access the images from code. Nothing I have tried has worked.

This is the "name" that my file "TryRes.rc" has given my bitmap:

IDB_BITMAP1

This identified throws a compiler error, saying it is "undeclared".

I am using DirectX10 for graphics; my intention is to use "CreateTextureFromResource" once I am able to access the images...unless someone knows a better way to do this. The idea is to be able to port the executable over to another computer and still be able to run it without all the extra graphics files.

Maybe this could be useful

http://www.winprog.org/tutorial/resources.html

http://msdn.microsoft.com/en-us/library/cc194804.aspx

But using bitmaps instead of compressed formats are going to increase the game size, right?

Ravnock:

You are undoubtedly right about the game size but right now I am just trying to learn how to use the resources. This is SOOOOOOO frustrating. I looked at the links you posted and followed http://www.winprog.o.../resources.html the best I could (using "BITMAP" as a resource type instead of "ICON") but it did not work.

Nick C.:

Noting the above, this is the result of what I get:

In my resource file, "resource3.h"
#define IDB_BITMAP1 101

In my main file:

#include "resource3.h"
~
~
~
~
IDB_BITMAP1 BITMAP "Image.bmp";

^^^these are the compiler errors that I get
1>DirectX10 Exp Part 2.cpp(214): error C2146: syntax error : missing ';' before identifier 'BITMAP'
1>DirectX10 Exp Part 2.cpp(214): error C2143: syntax error : missing ';' before 'string'1>DirectX10 Exp Part 2.cpp(214): error C2275: 'BITMAP' : illegal use of this type as an expression

I haven't even gotten to creating textures yet. Bottom line is I want to be able to use these bitmap resources to create textures through Direct3D10 functionality. This seems like such a simple issue but I cannot find ANY helpful documentation on this!

Ok so I have seemingly made some progress. Here is what the code looks like now:

In my resource file, "resource3.h":

#define IDB_BITMAP1 101

In my main file:

#include "resource3.h"

hResult = D3DX10CreateTextureFromResource(pD3DDevice,0,MAKEINTRESOURCE(IDB_BITMAP1),0,0,&pD3D10Resource,0);

This example compiles, but the hResult always returns as "E_FAIL". Conveniently enough, this is the "unspecified" code. Some of the

tutorials I have read have indicated coding things this way but it just isn't working. Any thoughts?

Ok so I have seemingly made some progress. Here is what the code looks like now:

In my resource file, "resource3.h":

#define IDB_BITMAP1 101

In my main file:

#include "resource3.h"

Ok so I have seemingly made some progress. Here is what the code looks like now:

In my resource file, "resource3.h":

#define IDB_BITMAP1 101

In my main file:

#include "resource3.h"

hResult = D3DX10CreateTextureFromResource(pD3DDevice,0,MAKEINTRESOURCE(IDB_BITMAP1),0,0,&pD3D10Resource,0);

This example compiles, but the hResult always returns as "E_FAIL". Conveniently enough, this is the "unspecified" code. Some of the

tutorials I have read have indicated coding things this way but it just isn't working. Any thoughts?

hResult = D3DX10CreateTextureFromResource(pD3DDevice,0,MAKEINTRESOURCE(IDB_BITMAP1),0,0,&pD3D10Resource,0);

This example compiles, but the hResult always returns as "E_FAIL". Conveniently enough, this is the "unspecified" code. Some of the

tutorials I have read have indicated coding things this way but it just isn't working. Any thoughts?

But D3DX10CreateTextureFromResource is expecting a pointer to the path string, I think you need D3DX10CreateTextureFromMemory. The question is: how to get the bitmap defined in the resource file as a pointer to the memory?

I think this thread could be useful for you:

http://social.msdn.microsoft.com/forums/en-US/vcgeneral/thread/07318565-7733-4c3d-b885-85dfeaa9b73b

Some msdn doc:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd145033(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/aa380680(v=vs.85).aspx

Well Ravnock I looked into the links you provided. Having investigated further I still cannot see why D3DX10CreateTextureFromResource is not working. The second parameter should be a handle to the instance creating it, which in my case should be the .exe. Having made that change it still does not work.

hResult=D3DX10CreateTextureFromResource(pD3DDevice,GetModuleHandle(0), MAKEINTRESOURCE(IDB_BITMAP1), 0 , 0 , &pD3D10Resource , 0 );

I tried using D3DX10CreateTextureFromMemory as follows:

HBITMAP hBit = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP1));

hResult=D3DX10CreateTextureFromMemory(pD3DDevice,hBit, 0 , 0 , 0 , &pD3D10Resource , 0 );

It did not work, even if I cast hBit to "HMODULE". LoadBitmap() -->DOES<-- return a valid pointer to a bitmap, but now I am left with how to access it through Direct3D10. The second paramter to the function call is an LPCVOID pointer but I can't seem to make it work for me. Still, all I am getting here is E_FAIL in the hResult return value.

Bout ready to pull the hair out of my head here. What on earth could I be missing? Is it maybe a linker issue?

Well Ravnock I looked into the links you provided. Having investigated further I still cannot see why D3DX10CreateTextureFromResource is not working. The second parameter should be a handle to the instance creating it, which in my case should be the .exe. Having made that change it still does not work.

hResult=D3DX10CreateTextureFromResource(pD3DDevice,GetModuleHandle(0), MAKEINTRESOURCE(IDB_BITMAP1), 0 , 0 , &pD3D10Resource , 0 );

I tried using D3DX10CreateTextureFromMemory as follows:

HBITMAP hBit = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP1));

hResult=D3DX10CreateTextureFromMemory(pD3DDevice,hBit, 0 , 0 , 0 , &pD3D10Resource , 0 );

It did not work, even if I cast hBit to "HMODULE". LoadBitmap() -->DOES<-- return a valid pointer to a bitmap, but now I am left with how to access it through Direct3D10. The second paramter to the function call is an LPCVOID pointer but I can't seem to make it work for me. Still, all I am getting here is E_FAIL in the hResult return value.

Bout ready to pull the hair out of my head here. What on earth could I be missing? Is it maybe a linker issue?

Sorry :( I only have a few experience with these resource files, I have only used them to load icons and things like that.

I guess some guy from the msdn forum must know this. Maybe not (LOL)

These functions don't like BITMAP type resource data, only RCDATA type resource data.

Change the

IDB_BITMAP1 BITMAP "Image.bmp";

bit in the rc file to

IDB_BITMAP1 RCDATA "Image.bmp";

The second stores a copy of the file as is; the first one strips off the BITMAPFILEHEADER from the start of the file. The WIC functions the D3DX functions use (specifically CreateDecoderFromStream) seem to require that header be present or they fail. It ain't there, so they fail which make the D3DX function fail.

This topic is closed to new replies.

Advertisement