Win32 API Troubles

Posted by Nick Cash on Tue 15 Feb 2005 12:04 AM — 4 posts, 18,599 views.

USA #0
I am having troubles, like always. For a few semester projects in school I decided to make a graphical game using the Win32 API. Things have progressed rather smoothly, but I've hit a big snag. Apparently I cannot load from a resource. Whats funny is I load my icons from the resource and that works just fine. Here is the function giving me trouble:

BMP *bitmap_from_resource(HINSTANCE hInstance, WORD resource)
{
  BMP *bitmap = NULL;
  HBITMAP bmp;

  CREATE( bitmap, BMP, 1 );
  
  {
    char buf[100];
    sprintf( buf, "Bitmap_from_resource: Beginning resource is %d", resource );
    write_log(buf);
    
    sprintf( buf, "IDB_IMAGE1 = %d", IDB_IMAGE1 );
    write_log(buf);
  }
  /* bmp = LoadImage(hInstance,
        MAKEINTRESOURCE(resource),
        IMAGE_BITMAP,
        0,
        0,
        LR_DEFAULTCOLOR);*/
   bmp = LoadBitmap(hInstance, MAKEINTRESOURCE(resource));

  bitmap->bitmap = bmp;

  bitmap->width = 640;
  bitmap->height = 480;

  if ( bmp == NULL )
  {
    long error = GetLastError();
    char string[100];

    sprintf(string, "Bitmap_from_resource: NULL bmp. Error %ld Res: %ld", error, resource );

    write_log( string );
    
    
    FreeBitmap( bitmap );
    
    return NULL;
  }

  /*everything ok, return */
  return bitmap;
}

The commented out LoadImage call was an alternate way of trying to do this, but neither works. As to why, I dont know, my log of this spews (btw, IDB_IMAGE1 is defined as 2000):

1. Bitmap_from_resource: Beginning resource is 2357
2. IDB_IMAGE1 = 2000
3. Bitmap_from_resource: NULL bmp. Error 1812 Res: 2357
4. Loading of Image1 failed!
5. Last error: 183

Yet the way I call it is this:

   if ( (Slide = (BMP *)bitmap_from_resource( game->hInstance, dc, IDB_IMAGE1 ) ) == NULL )
   {
     char buf[128];
     long error = GetLastError();

     write_log( "Loading of Slide1 failed!" );
     
     sprintf( buf, "Last error: %ld", error );
     write_log( buf );
   }
   else
    write_log( "Slide1 loaded successfully" );

So I have no idea as to why when I call the function with IDB_IMAGE1 it turns up 2357 when it is defined as 2000. The result is a resource error that will not go away.

Any ideas? See anything wrong with these functions? I'm not Win32 API expert since this is more or less my first -REAL- adventure through the Win32 API realm.

Thanks for anything and everything in advance, this is for school so I appreciate it doubly :P
USA #1
Sorry but I have no idea. I've never loaded bitmaps using resources, and since bitmap is a generally messy format I tend to do all my loading via TGA. And to make matters worse, I try to steer clear of the Win32 API and use OpenGL instead. (Haven't used DirectX in a while but that's ok too.) Good luck figuring out this beast...
USA #2
Yeah, thanks :P I've been meaning to jump into my OpenGL book for sometime now, but I figure that can be my project next year (some type of 3D thing). I decided to go for simplicity, but I guess its not too simple anymore. I wish this were as easy to do as my MUD :P. It just seems like it should work...

The strangest part is that after I call it resource is 2357 instead of 2000 like it should be....
Amended on Tue 15 Feb 2005 01:47 AM by Nick Cash
USA #3
Just for anyone who was curious...

Well, this was a strange problem. My friend EvilPuffBall figured out a way around it. Apparently if you call LoadBitmap or LoadImage in the function that contains all of the basic windoze crap (registering of the class, creating the window, etc..) it would work. So, I am able to use my resources but at the expense of a more dynamic system and cleaner code. Pfft, some trade off.

I hate you windows :P