Announcement

Collapse
No announcement yet.

How to put the number of bitmaps in a resource file into the resource file?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to put the number of bitmaps in a resource file into the resource file?

    I'm making a little program that has a lot of small bitmaps loaded from resources and I'm always adding some bitmaps and sometimes removing some and I have to update the program each time with the new count. I'd like to either automate the counting, assuming there's a way to do that, or put the number of bitmaps into the resource itself, probably as a string.

    I've been looking for the answer to that in the help and in various samples and on MSDN but I don't find anything useful, or, if I do, I don't recognize it.

    Is there a way to determine the number of bitmaps? They are numerically named, BMP0001, etc. and they're sequential, so I thought about loading till I get an error but I thought there might be a better way. Also I'd like to know how many there will be before they're loaded if that's possible.

    It seems like putting the count into the .rc file as a string should be the simplest answer but I have no idea how to do that. I guess that's really my question. How can I do that?

    Thanks for any suggestions. By the way, I'd prefer either an explanation or a pointer to one to help me understand this stuff, to code that does it for me. Although I'll take whatever I can get.

    Barry

  • #2
    Code:
    #include "Resource.h"
    
    //-----------------------------------------------------------------------------
    //  ** RC Data **
    //-----------------------------------------------------------------------------
    BMP0001 BITMAP DISCARDABLE "BMP0001.bmp"
    BMP0002 BITMAP DISCARDABLE "BMP0002.bmp"
    BMP0003 BITMAP DISCARDABLE "BMP0003.bmp"
    BMP0003 BITMAP DISCARDABLE "BMP0004.bmp"
    //-----------------------------------------------------------------------------
    
    // in the program
    // GRAPHIC BITMAP LOAD "BMP0001",bmpX,bmpY TO hBMP1&   'load a bitmap
    // similar statements for image lists
    Last edited by Richard Angell; 7 Sep 2008, 03:04 PM.
    Rick Angell

    Comment


    • #3
      Barry,

      Another way to do this is to place all the pictures into one bitmap. This is called a picture clip. It's called that because you load from the resource into the executable's memory, then extract (clip out) the portion needed on demand. This is useful for things like 2D games where you might have several characters that have several poses depending on the direction they are going and the activity being pursued.

      What is your end objective here?
      Rick Angell

      Comment


      • #4
        Originally posted by Richard Angell View Post
        Code:
        #include "Resource.h"
        
        //-----------------------------------------------------------------------------
        //  ** RC Data **
        //-----------------------------------------------------------------------------
        BMP0001 BITMAP DISCARDABLE "BMP0001.bmp"
        BMP0002 BITMAP DISCARDABLE "BMP0002.bmp"
        BMP0003 BITMAP DISCARDABLE "BMP0003.bmp"
        BMP0003 BITMAP DISCARDABLE "BMP0004.bmp"
        //-----------------------------------------------------------------------------
        
        // in the program
        // GRAPHIC BITMAP LOAD "BMP0001",bmpX,bmpY TO hBMP1&   'load a bitmap
        // similar statements for image lists
        That loads the bitmaps. I'm already doing that. I want to be able to have a count of the bitmaps in the resource file. I'm loading one at random and I want to know where to set the max for the random number generator. As it stands now, every time I add or subtract a bitmap I have to change a constant in the program as well.

        This isn't a real problem but this is about learning and I thought this would be good to learn.

        Barry

        Comment


        • #5
          Originally posted by Richard Angell View Post
          Barry,

          Another way to do this is to place all the pictures into one bitmap. This is called a picture clip. It's called that because you load from the resource into the executable's memory, then extract (clip out) the portion needed on demand. This is useful for things like 2D games where you might have several characters that have several poses depending on the direction they are going and the activity being pursued.

          What is your end objective here?
          The program I'm doing is a little coloring book program. I did it some time ago and now I want to redo it using some of the new features of PB9 such as the tab control.

          Having the pictures as seperate bitmaps makes more sense in my case since I'll be adding to them as I find more pictures that fit the context and removing some of the lesser ones as more apropriate ones come along. Hopefully the number of them will increase till my interests move on to something else.

          When the program opens it loads a bitmap at random. In the new program I'm also going to have a couple of tab pages showing thumbnails of the available bitmaps so that one of those can be loaded instead.

          I use RND with string catenation to generate the bitmap name and for that I need the number of bitmaps. But I'd also like to know how many there'll be ahead of time so the program can determine how many tab pages to make for that.

          None of this has to be done this way. I just use this program to keep my hands busy while I'm listening to audiobooks so I don't drift off to sleep and the program is fine as it is, but I thought this might be a way to learn to use the tab control and also to learn more about resources. But I find that I don't know how to do this part. Maybe I can't. I don't know. That won't be a tragedy but it seems so basic that I'd like to find out how or that I can't.

          Barry

          Comment


          • #6
            It seems like putting the count into the .rc file as a string should be the simplest answer but I have no idea how to do that. I guess that's really my question. How can I do that?
            String table resource

            Code:
            STRINGTABLE
            BEGIN
            1,   "0099"
            END
            (will post some retrieval code in a bit or you can find in POFFS some examples)
            Last edited by Richard Angell; 7 Sep 2008, 04:28 PM. Reason: simplified code
            Rick Angell

            Comment


            • #7
              Example code to retrieve the string (1) from the string table (see edited code above)
              Code:
                   DIM hCurInstance  AS LOCAL LONG
                   DIM lStringNumber AS LOCAL LONG
                   DIM sBuffer       AS LOCAL STRING
                   DIM MaxBmps       AS LONG
                   hCurInstance = GetModuleHandle("")
                   sBuffer = STRING$(8,0)  'create empty buffer of 1024 characters
                   lStringNumber = 1 'or whatever string number you want to retrieve
                   LoadString hCurInstance, lStringNumber, BYVAL STRPTR(sBuffer), LEN(sBuffer)
                   MaxBmps = VAL(sBuffer)
                   ? FORMAT$(MaxBmps)
              Rick Angell

              Comment


              • #8
                Thanks. That does just what I wanted and I kind of see how it works after looking up LoadString, which I didn't know about. I also didn't know about string tables. I ran into them looking for information on strings in resources but I didn't recognize it as what I was looking for. Silly me!

                Barry

                Comment


                • #9
                  If you want to know the number of bitmaps there are in a resource file,
                  use one of the following functions:

                  EnumResourceNames
                  EnumResourceTypes
                  Dominic Mitchell
                  Phoenix Visual Designer
                  http://www.phnxthunder.com

                  Comment


                  • #10
                    For example, to get just the number of bitmaps, you can use code similar to the following.
                    Code:
                    '-------------------------------------------------------------------------------
                    
                    FUNCTION EnumBitmapsProc ALIAS "EnumBitmapsProc" _
                      ( _
                      BYVAL hModule   AS DWORD, _ ' handle of module with bitmaps
                      BYVAL lpszType  AS DWORD, _ ' resource type
                      BYVAL lpszName  AS DWORD, _ ' resource name or id
                      BYVAL lParam    AS LONG _   ' application-defined value
                      ) EXPORT AS LONG
                    
                      LOCAL pcBitmaps AS LONG PTR
                    
                      pcBitmaps = lParam
                    
                      INCR @pcBitmaps
                    
                      FUNCTION = %TRUE
                    
                    END FUNCTION
                    
                    '----------------------------------------------------------------------
                    
                    FUNCTION GetBitmapCount _
                      ( _
                      BYVAL hInstance AS DWORD _  ' handle of module with bitmaps
                      ) AS LONG
                    
                      LOCAL cBitmaps  AS LONG 
                    
                      EnumResourceNames hInstance,  BYVAL %RT_BITMAP, CODEPTR(EnumBitmapsProc), VARPTR(cBitmaps)
                    
                      FUNCTION = cBitmaps
                    
                    END FUNCTION
                    Dominic Mitchell
                    Phoenix Visual Designer
                    http://www.phnxthunder.com

                    Comment


                    • #11
                      Thanks for that. I'll play with it. I've already inserted the other code but since this is about learning I'll probably try using this too.

                      Barry

                      Comment


                      • #12
                        Using the API's Dominic posted would be a better way. I've never had the need to enumerate them, since the number of bitmaps is usually low for my needs.
                        Rick Angell

                        Comment


                        • #13
                          I currently have 34 bitmaps. I seem to be adding 3 or 4 a week. I doubt that it'll get anywhere near 100. Actually there are enough of them now but it's kind of fun looking for more.

                          Barry

                          Comment


                          • #14
                            Using enumeration you would not have to keep track of the number of bmps in a stringtable resource. You could also build a table of names in your program with EnumResourceNames. Then randomly generate an index number, get the name and load the resource. That way you might not have to use BMPxxxxx naming as the reference names could be more content oriented. Just a thought.
                            Rick Angell

                            Comment


                            • #15
                              Originally posted by Richard Angell View Post
                              Using enumeration you would not have to keep track of the number of bmps in a stringtable resource. You could also build a table of names in your program with EnumResourceNames. Then randomly generate an index number, get the name and load the resource. That way you might not have to use BMPxxxxx naming as the reference names could be more content oriented. Just a thought.
                              That would work out in general but in this case there are other bitmaps. Currently there's one for the palette. I'm going to either replace that with a changable palette or add more palette bitmaps and make that user-selectable.

                              However, I do like the idea of using an index to select a bitmap name instead of generating the name. I may do that.

                              Barry

                              Comment


                              • #16
                                That would work out in general but in this case there are other bitmaps.
                                In which case you could probably segregate them as their names are enumerated. By that I mean if you had a naming scheme for the other bmps, then what you might do is start the program's initialization process by creating resource bmp name arrays for pallete maps, others for the bmps which were the original thread objective, etc. All these name arrays can be be DIM'd to the size of the EnumResourceTypes to start with, then use counters to track as you inspect and add a name to its appropriate array. Finally REDIM PRESERVE them before your program's initialization phase is done. Just another thought.
                                Last edited by Richard Angell; 8 Sep 2008, 09:26 AM. Reason: improved
                                Rick Angell

                                Comment


                                • #17
                                  You can put anything you want into a program resource...
                                  User-Defined Resource Demo January 26 2003
                                  ... but for getting a count of bitmaps EnumResourceNames() is IMO the superior solution.
                                  Michael Mattias
                                  Tal Systems (retired)
                                  Port Washington WI USA
                                  [email protected]
                                  http://www.talsystems.com

                                  Comment


                                  • #18
                                    Originally posted by Michael Mattias View Post
                                    You can put anything you want into a program resource...
                                    User-Defined Resource Demo January 26 2003
                                    ... but for getting a count of bitmaps EnumResourceNames() is IMO the superior solution.
                                    Thanks for that link. I'll play with it. Maybe this is a good time to get more generally familiar with resource files.

                                    Barry

                                    Comment

                                    Working...
                                    X