Thread: Ressources files

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006

    Ressources files


    Now I'm having some problem working with ressource files! I heard that we could use these to place files like pictures and audio inside a .rc file, so that we can then execute our application without having to carrying around dlls and others. Is this true? Could be interesting!

    But the problem here is that I can't get to place or use files in ressources! I'm using MS-VC++ 2003, and when I add a .rc file to the project, I can try to add images, audio inside of the .rc, but I don't know how to use them in my code! I know I have to use some special syntax depending on the type of file, but what if you have to import some files like .png?

    So do anyone here knows all to deal with ressources and give a syntax example, or are ressources files just serving for other things???


  2. #2
    Join Date
    Jan 2002
    Moved to Windows board.

    msdn: About Resource Files.

    msvc2003 has a gui resource editor so you don't need to fiddle around with resource scripts, unless you particularly want to. Check out your compiler documentation for precise help. Some resource types are supported directly, for example images such as bitmaps or icons. For those resource types not directly supported you can use RCDATA.

    Search this board for examples of the use of resource definition statements and how to use resources as this sort of thing comes up quite a lot.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    I still don't understand anything. I searched in the forums and it seems that resources files can only serve for windows specific functions like doing icons and listboxs and stuff. But the problem is that I'm using image loading functions from SDL, (2D kind of game engine) and the function actually receive a parameter of char*...

    So if I try to put this kind of code for the function, it can't work...

    So I believe I have to use the correct syntax:

    This way everything works fine, but is there a way to make the compiler "think" that "image1.png" is actually a file inside of itself(.exe)? Remember I have to pass a char*!

    (And I tried RCDATA but I think that this can't still pass a char* to the function...)

  4. #4
    train spotter
    Join Date
    Aug 2001
    near a computer
    The resource will not have a file name as such as it is included in your exe (and makes it bigger).

    If you have a lot of image/sound resources becareful not to make your exe too big (and hard to distribute).

    You could always write the resource data to a temp file and send to your image loader as required. Remembering that file I/O is _much_ slower than memory I/O.

    >>I'm using image loading functions from SDL,

    Why? Why not use LoadImage()

    //load a bitmap
    hImage=LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_IMAGE1), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);

    If you have to use PGN try GDI+

    Look here,

    This is some code I have hacked together from MSDN examples, don't expect it to WORK....

    #include <windows.h>
    #include <gdiplus.h>
    #include <stdio.h>
    using namespace Gdiplus;
    int WriteResourceAsPGN(int iResourceID,char *szFullFileName)
      // Initialize GDI+.
       GdiplusStartupInput gdiplusStartupInput;
       ULONG_PTR gdiplusToken;
       GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
       //load image from resource
       HBITMAP  hImage=LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(iResourceID), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
       CLSID   encoderClsid;
       Status  stat;
       Image*   image = new Image(hImage);
       // Get the CLSID of the PNG encoder.
       GetEncoderClsid(L"image/png", &encoderClsid);
       stat = image->Save(szFullFileName, &encoderClsid, NULL);
       if(stat == Ok)
       //Clean UP
       delete image;
       return 0;
    int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
       UINT  num = 0;          // number of image encoders
       UINT  size = 0;         // size of the image encoder array in bytes
       ImageCodecInfo* pImageCodecInfo = NULL;
       GetImageEncodersSize(&num, &size);
       if(size == 0)
          return -1;  // Failure
       pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
       if(pImageCodecInfo == NULL)
          return -1;  // Failure
       GetImageEncoders(num, size, pImageCodecInfo);
       for(UINT j = 0; j < num; ++j)
          if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
             *pClsid = pImageCodecInfo[j].Clsid;
             return j;  // Success
       return -1;  // Failure
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    well, I have to use the function IMG_Load... because I make ALL my game with the SDL library. I can't just switch between other ways of doing things, because I believe that "LoadImage" returns an "HBITMAP "?

    And SDL doesn't handle this type of variable... And I'm planning to keep up with working with this lib...

    But for memory I/O or file I/O, I believe it doesn't change things up, because at start I load up everything I need from my files, and I'll practically won't ever have other need of loading them after that, and it won't take much than 30 Mb...

    And for distribution of the app, if you have your files in other folders or inside the .exe, it won't change the size of the whole project (Unless we're talking about the .cpp, .lib, .h )

    Thanks anyway for taking time with this thread!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accessing files over wireless network
    By BobMcGee123 in forum Tech Board
    Replies: 4
    Last Post: 07-29-2006, 02:25 PM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM