Sign in to follow this  

Using Win32 API

This topic is 4073 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

Which are the standard for Win32 style coding for drawing bitmaps and pictures? My book "Windows Game Programming for Dummies" doesn't cover it in chapter 5 as it should. Is there two kinds to use? BITMAP and HBITMAP? To use HBITMAP I think one write: <code> HBITMAP hBmp = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP_ID)); </code> Is HBITMAP used for bitmaps in resources? But how draw/blit it then I don't really know. Thanks. [Edited by - programering on October 18, 2006 10:05:29 PM]

Share this post


Link to post
Share on other sites
Just a hint - I tend to consider "for dummies" books to be crap just on general principles. if you google for LoadBitmap on the microsoft site you get all the information you need, probably a much better resource than the book. You sound as if you don't know if there is another type BITMAP. I would guess not, based on a quick skim-read of the above link(not really sure though).

Share this post


Link to post
Share on other sites
Thank you. rating++;

I've tried to search msdn for documentation, but never found any good.
But you've found that good documentation that I was looking for.
What did you search on? Edit: Ok. you searched for LoadBitmap

Share this post


Link to post
Share on other sites
I need to load a bitmap from a FILE *pFile ptr, how todo that?
And how to blit it? The prototype for blitting takes a HDC.
BOOL BitBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop // raster operation code
);
To convert it seems to be a bit complicated.

BTW, what the difference between BITMAP/HBITMAP and HDC?

The only image format that Win32 API seems to support is just *.bmp, why?

Share this post


Link to post
Share on other sites
Quote:
Original post by programering
I need to load a bitmap from a FILE *pFile ptr, how todo that?
And how to blit it? The prototype for blitting takes a HDC.
BOOL BitBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop // raster operation code
);
To convert it seems to be a bit complicated.

BTW, what the difference between BITMAP/HBITMAP and HDC?

The only image format that Win32 API seems to support is just *.bmp, why?


because figuring out how to load other formats is generally pretty trivial. (i don't even understand why it has functions for loading .bmp)

Share this post


Link to post
Share on other sites
Quote:
Original post by programering
Quote:
Original post by DaBookshah
General Tip: If you need info about win32 api method xxx, Google: site:microsoft.com xxx. Works every time.


Isn't that porn?


lol,

... where xxx is the phrase you are searching for. This minimises Googles search to just Microsoft.com, and msdn.microsoft.com is better too.

Share this post


Link to post
Share on other sites
Quote:
Original post by programering
I need to load a bitmap from a FILE *pFile ptr, how todo that?
And how to blit it? The prototype for blitting takes a HDC.
BOOL BitBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop // raster operation code
);
To convert it seems to be a bit complicated.

BTW, what the difference between BITMAP/HBITMAP and HDC?

The only image format that Win32 API seems to support is just *.bmp, why?


Do you really need to load from a FILE*? Or do you just want to laod from a file on disk? Loading from a FILE* will involve parsing the file youself, since the Win32 methods don't have any concept of FILE*'s. So you'd need to allocate an empty HBITMAP, then parse the file into it.
If you want to load from a file on disk, then you can use LoadImage and specify the LR_LOADFROMFILE flag.

I'd advise having a look around google for some Win32 GDI tutorials - I intend to write one myself soon.
Basically, all drawing operations use HDCs, which is a Handle to a Device Context. A HDC is basically where all the drawing commands go. The HDC usually represents a monitor, but it can also represent a printer and things too.
To draw onto a bitmap, you select the HBITMAP into the HDC using SelectObject(), which sort of attaches the bitmap to the HDC for use. You do the same with HPENs, HBRUSHes, and anything else if you want to draw.
Once you have the bitmap selected into the HDC you can use the BitBlt() command to blit rectangular regions of the device context to another device context, which copies chunks of the bitmap.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by programering
I need to load a bitmap from a FILE *pFile ptr, how todo that?
And how to blit it? The prototype for blitting takes a HDC.
BOOL BitBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop // raster operation code
);
To convert it seems to be a bit complicated.

BTW, what the difference between BITMAP/HBITMAP and HDC?

The only image format that Win32 API seems to support is just *.bmp, why?


Do you really need to load from a FILE*? Or do you just want to laod from a file on disk? Loading from a FILE* will involve parsing the file youself, since the Win32 methods don't have any concept of FILE*'s. So you'd need to allocate an empty HBITMAP, then parse the file into it.
If you want to load from a file on disk, then you can use LoadImage and specify the LR_LOADFROMFILE flag.

I'd advise having a look around google for some Win32 GDI tutorials - I intend to write one myself soon.
Basically, all drawing operations use HDCs, which is a Handle to a Device Context. A HDC is basically where all the drawing commands go. The HDC usually represents a monitor, but it can also represent a printer and things too.
To draw onto a bitmap, you select the HBITMAP into the HDC using SelectObject(), which sort of attaches the bitmap to the HDC for use. You do the same with HPENs, HBRUSHes, and anything else if you want to draw.
Once you have the bitmap selected into the HDC you can use the BitBlt() command to blit rectangular regions of the device context to another device context, which copies chunks of the bitmap.


I want to use the FILE *pointer cos I've written a directory filepath parser.
That open the FILE *.

The prototype:
FILE *LoadFile(FILEPATH *lpFilePath, LPCSTR mode);

The FILEPATH struct definition:
struct tagFILEPATH {
FILEDIR fileDir;
LPSTR fileDirs[MAX_FILEPATH_DIRS];
LPSTR fileName;
LPSTR fullPath;
};
typedef struct tagFILEPATH FILEPATH;

The code: File.h

#ifndef _File_h
#define _File_h

#include <stdio.h>

enum FILEDIR {
FILEDIR_APP,
FILEDIR_DATA,
FILEDIR_ENTITIES,
FILEDIR_HUMANS,
NUM_FILEDRIS,

FILEDIR_CUSTOM = NUM_FILEDRIS
};

#define MAX_FILEPATH_DIRS 15

struct tagFILEPATH {
FILEDIR fileDir;
LPSTR fileDirs[MAX_FILEPATH_DIRS];
LPSTR fileName;
LPSTR fullPath;
};

typedef struct tagFILEPATH FILEPATH;

FILE *LoadFile(FILEPATH *lpFilePath, LPCSTR mode);

#endif




File.cpp

#include "StdAfx.h"
#include "File.h"

/////////////////////////////////////

LPSTR fileDirs[NUM_FILEDIRS] =
{
NULL,
NULL, // To be assigned by LoadDataDir();
"Entities",
"Humans"
};

#define MAX_FILEPATH_CHARS 256


WORD fullPathPos;
CHAR fullPath[MAX_FILEPATH_CHARS];


BOOL AddToFullPath(LPSTR pasteStr)
{
WORD pasteStrLen = strlen(pasteStr);

for (WORD i = 0; i < pasteStrLen; i++)
{
if (fullPathPos < MAX_FILEPATH_CHARS)
fullPath[fullPathPos++] = pasteStr[i];
else break;
}
}


FILEPATH filePath;

/*
VOID InitFilePath(VOID)
{
fullPathPos = 0;
filePath.fullPath = fullPath;
}
*/



FILEPATH *GetFilePath(VOID)
{
filePath.fullPath = fullPath;
return &filePath;
}

/////////////////////////////////////

FILE *LoadFile(FILEPATH *lpFilePath, LPCSTR mode)
{
switch (lpFilePath->fileDir)
{
case FILEDIR_APP:
lpFilePath->nFileDirs = 0;
break;
case FILEDIR_DATA:
lpFilePath->nFileDirs = 1;
lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];
break;
case FILEDIR_ENTITIES:
lpFilePath->nFileDirs = 2;
lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];
lpFilePath->fileDirs[1] = fileDirs[FILEDIR_ENTITIES];
break;
case FILEDIR_HUMANS:
lpFilePath->nFileDirs = 3;
lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];
lpFilePath->fileDirs[1] = fileDirs[FILEDIR_ENTITIES];
lpFilePath->fileDirs[2] = fileDirs[FILEDIR_HUMANS];
break;
default:
break;
}

fullPathPos = 0;

UCHAR count = lpFilePath->nFileDirs;
if (count < MAX_FILEPATH_DIRS)
{
for (UCHAR i = 0; i < count; i++)
{
AddToFullPath(lpFilePath->fileDirs[i]);
AddToFullPath("\");
}
}
else fprintf(stderr,"Too many directories in file path. "
"The limit is %d directories.\n\n",MAX_FILEPATH_DIRS);

AddToFullPath(lpFilePath->fileName);
fullPath[fullPathPos] = '\0';

FILE *lpFile = fopen(fullPath,mode);

if (!lpFile)
{
LPSTR modeType;
if (strcmp(mode,"r") == 0) modeType = "for reading";
else
if (strcmp(mode,"w") == 0) modeType = "for writing";
else modeType = "in an unknown mode";

fprintf(stderr,"Couldn't open file \"%s\"\n",fullPath,modeType);
}

return lpFile;
}





[Edited by - programering on October 19, 2006 11:34:56 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by programering
What's the difference between the usual CreateWindow()/WNDCLASS and that Ex/EX version?


For right now, think of the Ex versions as extended versions. They allow you to do a little more customization than would normally be possible by the plain CreateWindow. Such as having a small icon and a large icon, etc. (Or more window styles, etc).

Share this post


Link to post
Share on other sites
Quote:
Original post by programering
What's the difference between the usual CreateWindow()/WNDCLASS and that Ex/EX version?

Read the documentation. Surprisingly, it actually describes the differences.

As for your loading a bitmap from a FILE *, that's just setting yourself up for a lot of pain. Yeah, you've written a directory file path parser... but Win32 already had one (FindFile, FindFileNext).

Before you start writing functions and bits of code that later force you to put things together in the least elegant manner possible, familiarize yourself with the facilities that come included in the box - what your language's standard library provides, what your platform's SDK provides.

Happy hacking.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
As for your loading a bitmap from a FILE *, that's just setting yourself up for a lot of pain. Yeah, you've written a directory file path parser... but Win32 already had one (FindFile, FindFileNext).


I think I solve that with:


LPSTR CalcFilePath(FILEPATH *lpFilePath)
{
switch (lpFilePath->fileDir)
{
case FILEDIR_APP:
lpFilePath->nFileDirs = 0;
break;
case FILEDIR_DATA:
lpFilePath->nFileDirs = 1;
lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];
break;
case FILEDIR_ENTITIES:
lpFilePath->nFileDirs = 2;
lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];
lpFilePath->fileDirs[1] = fileDirs[FILEDIR_ENTITIES];
break;
case FILEDIR_HUMANS:
lpFilePath->nFileDirs = 3;
lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];
lpFilePath->fileDirs[1] = fileDirs[FILEDIR_ENTITIES];
lpFilePath->fileDirs[2] = fileDirs[FILEDIR_HUMANS];
break;
default:
break;
}

fullPathPos = 0;

UCHAR count = lpFilePath->nFileDirs;
if (count < MAX_FILEPATH_DIRS)
{
for (UCHAR i = 0; i < count; i++)
{
AddToFullPath(lpFilePath->szFileDirs[i]);
AddToFullPath("\");
}
}
else fprintf(stderr,"Too many directories in file path. "
"The limit is %d directories.\n\n",MAX_FILEPATH_DIRS);

AddToFullPath(lpFilePath->szFileName);
szFullPath[wFullPathPos] = '\0';

return szFullPath;
}

...

// Usage:
handle = LoadImage(NULL,CalcFilePath(lpFilePath),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);



Share this post


Link to post
Share on other sites

This topic is 4073 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