Archived

This topic is now archived and is closed to further replies.

drarem

got class?

Recommended Posts

drarem    100
I need some help (mentally too..) I am using Dev-C++.. here is the class: //hBmpCls.h class hBmpCls { private: HBITMAP hMemDC; BITMAP bm; HWND hwnd; HDC hdc; char * fname; public: int InitBMP(HBITMAP, BITMAP, HWND, HDC, char *); }; int hBmpCls::InitBMP(HBITMAP HMemDC, BITMAP Bm, HWND Hwnd, HDC Hdc, char *Fname) { hMemDC = HMemDC; bm = Bm; hwnd = Hwnd; hdc = Hdc; strcpy(fname, Fname); return 0; } I do the following within the paint proc: hBmpCls castlebck; castlebck.InitBMP(MemDC, bm, hwnd, hdc, "castle.bmp"); I get the following errors: no matching function for call to hBmpCls::InitBMP(....) Please point out what I am doing wrong, as I need serious help with classes. Anything else I might be doing wrong, please help. Like is it a good idea to include the bm, hdc, etc with each bitmap loaded; and why can''t i put the loadbitmap in the WM_CREATE section instead.. never seems to work there, only in the WM_PAINT section. thnx I fseek, therefore I fam.

Share this post


Link to post
Share on other sites
Cyberdrek    100
quote:
Original post by drarem
I need some help (mentally too..)

I am using Dev-C++.. here is the class:

//hBmpCls.h

class hBmpCls {
private:
HBITMAP hMemDC;
BITMAP bm;
HWND hwnd;
HDC hdc;
char * fname;
public:
int InitBMP(HBITMAP, BITMAP, HWND, HDC, char *);
};

int hBmpCls::InitBMP(HBITMAP HMemDC, BITMAP Bm, HWND Hwnd, HDC Hdc, char *Fname) {
hMemDC = HMemDC;
bm = Bm;
hwnd = Hwnd;
hdc = Hdc;
strcpy(fname, Fname);
return 0;
}

I do the following within the paint proc:
hBmpCls castlebck;
castlebck.InitBMP(MemDC, bm, hwnd, hdc, "castle.bmp");

I get the following errors:
no matching function for call to hBmpCls::InitBMP(....)

Please point out what I am doing wrong, as I need serious help with classes. Anything else I might be doing wrong, please help. Like is it a good idea to include the bm, hdc, etc with each bitmap loaded; and why can't i put the loadbitmap in the WM_CREATE section instead.. never seems to work there, only in the WM_PAINT section.

thnx




I fseek, therefore I fam.


I'm not sure about this so don't take me for granted, but you should have a constructor in your class. Basicly what's happening is that your class isn't initilized and therefore, the member functions can't be accessed. I'm not sure if that's really the problem but you might want to try it just for fun. I would try it out but I don't have a compiler set up as I just finished my routine re-installation of windows.

Hope this helps...




"And that's the bottom line cause I said so!"

Cyberdrek
danielc@iquebec.com
Founder
Laval Linux

/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash

[edited by - cyberdrek on May 6, 2002 10:18:36 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
class hBmpCls {
private:
HBITMAP hMemDC;
BITMAP bm;
HWND hwnd;
HDC hdc;
char * fname;
public:
// *********** CHANGE HERE *******************
int InitBMP(HBITMAP HMemDC, BITMAP Bm, HWND Hwnd, HDC Hdc, char *Fname)};
// *******************************************

int hBmpCls::InitBMP(HBITMAP HMemDC, BITMAP Bm, HWND Hwnd, HDC Hdc, char *Fname) {
hMemDC = HMemDC;
bm = Bm;
hwnd = Hwnd;
hdc = Hdc;
strcpy(fname, Fname);
return 0;
}

The problem might be that your function declaration in your class should be the same as the implementation

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
class hBmpCls {
private:
HBITMAP hMemDC;
BITMAP bm;
HWND hwnd;
HDC hdc;
char * fname;
public:
// *********** CHANGE HERE *******************
int InitBMP(HBITMAP HMemDC, BITMAP Bm, HWND Hwnd, HDC Hdc, char *Fname);
// *******************************************

int hBmpCls::InitBMP(HBITMAP HMemDC, BITMAP Bm, HWND Hwnd, HDC Hdc, char *Fname) {
hMemDC = HMemDC;
bm = Bm;
hwnd = Hwnd;
hdc = Hdc;
strcpy(fname, Fname);
return 0;
}

The problem might be that your function declaration in your class should be the same as the implementation

Share this post


Link to post
Share on other sites
petewood    819
the above posts won''t help

quote:

I''m not sure about this so don''t take me for granted, but you should have a constructor in your class. Basicly what''s happening is that your class isn''t initilized and therefore, the member functions can''t be accessed. I''m not sure if that''s really the problem but you might want to try it just for fun


this isn''t true. it''s false.

quote:

The problem might be that your function declaration in your class should be the same as the implementation.


they are the same. you don''t have to have the variable names in the declaration.

what type is your variable MemDC? is it BITMAP or HBITMAP? you may need to dereference it &MemDC.

everything else looks okay to me

Share this post


Link to post
Share on other sites
drarem    100
ty ty my problem was in declaring hMemDC as a HBITMAP and in my main program it should have been HDC.. so I changed it to HDC in the class definition and it worked great

This leads to another question:
below is some of the main code and I am confused.

1) Is the WM_PAINT the only place I should be creating/loading bitmaps?

I'm pretty sure I need to create a destructor at the end of the WM_PAINT and will write one that does a release(hdc)/deleteobject(hBmp),etc.. then call ~hBmpCls(); I don't think I can create the bitmap in the WM_CREATE because the hwnd is not defined yet until the window is opened? It would be nice tho, if I only had to create a class instance one time and load it one time..



switch (message) /* handle the messages */
{
case WM_CREATE:
int i;
char buff[256];
RECT pa;
RECT pb;
RECT rc;
HDC hdc;
HDC MemDC;
BITMAP bm;
HBITMAP hBmp;
PAINTSTRUCT ps;
InitRect(&pa, 0,0,800,600);
InitRect(&pb, 16,32,64,128);
// dumpwindow("%s %d", "parm1: ", pa.right);
break;
case WM_PAINT:
hdc = GetDC(hwnd);
hBmpCls castlebck;
castlebck.InitBMP(MemDC, bm, hwnd, hdc, hBmp, "castle.bmp");
BeginPaint(hwnd, &ps);
MainLoop(hwnd, hdc);
castlebck.BMPStretch(16,16,256,256,0,0);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}



I fseek, therefore I fam.

[edited by - drarem on May 7, 2002 4:28:16 PM]

[edited by - drarem on May 7, 2002 4:30:02 PM]

Share this post


Link to post
Share on other sites
IndirectX    122
When you get WM_CREATE, the window is already created, and HWND is valid. You can load the bitmap in WM_CREATE and delete it in WM_DESTROY.

Share this post


Link to post
Share on other sites
drarem    100
ok.. i was able to initiate the classes in the WM_CREATE but had to keep the castlebck.InitBMP(....) in the WM_PAINT section for some reason.. in the WM_CREATE it would just show a blank window.
I''m doing a createdc, selectobject, loadimage, etc in the InitBMP class function.

As for my next question.. i can understand when writing a game i might want to destroy some objects when they are no longer needed, or perhaps reuse them, but is it necessary to destroy them at the end of the program (or is it good programming practice)?



I fseek, therefore I fam.

Share this post


Link to post
Share on other sites
JohnBSmall    881
You should always destroy everything that you create. In some cases the OS may let you get away with it, but never rely on that - leaving the OS to clean up after you is a bad habit - don''t get into it.

John B

Share this post


Link to post
Share on other sites
drarem    100
Alright thanks I'll remember that. I suppose I could set the bitmaps up as a linked-list and when done, go thru and release/delete all - creating a destructor function for the class and throwing it in there.

Here is some sample source and the class so far, if anyone has a use for it. It seems to function well so far, if you need a quick bitmap or more loaded in on the fly..

************************************************
switch (message) /* handle the messages */
{
case WM_CREATE:
int i;
char buff[256];
RECT rc;
HDC hdc;
HDC MemDC;
BITMAP bm;
HBITMAP hBmp;
PAINTSTRUCT ps;
hBmpCls castlebck;
hBmpCls test;
hdc = GetDC(hwnd);
break;
case WM_PAINT:
hdc = GetDC(hwnd);
castlebck.InitBMP(MemDC, bm, hwnd, hdc, hBmp, "castle.bmp");
test.InitBMP(MemDC, bm, hwnd, hdc, hBmp, "test.bmp");
BeginPaint(hwnd, &ps);
MainLoop(hwnd, hdc, &test);
test.BMPStretch(0,0,800,600,0,0);
castlebck.BMPBlt(16,16,64,64,0,0);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:

***************************************************



//hBmpCls.h

class hBmpCls {
private:
HDC hMemDC;
BITMAP bm;
HWND hwnd;
HDC hdc;
HBITMAP hBmp;
char fname[255];
public:
int InitBMP(HDC, BITMAP, HWND, HDC, HBITMAP, char *);
int BMPStretch(int, int, int, int, int, int);
int BMPBlt(int, int, int, int, int, int);
};

int hBmpCls::InitBMP(HDC HMemDC, BITMAP Bm, HWND Hwnd, HDC Hdc, HBITMAP HBmp, char *Fname) {
RECT rc;
hMemDC = HMemDC;
bm = Bm;
hwnd = Hwnd;
hdc = Hdc;
hBmp = HBmp;
strcpy(fname, Fname);
GetClientRect(hwnd, &rc);
hMemDC = CreateCompatibleDC(hdc);
hBmp = (HBITMAP) LoadImage( NULL, Fname, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
CreateCompatibleBitmap(hMemDC, rc.right, rc.bottom);
GetObject(hBmp, sizeof(BITMAP), &bm);
SelectObject(hMemDC,hBmp);
return 0;
}

int hBmpCls::BMPStretch(int x1, int y1, int x2, int y2, int xc, int yc) {
StretchBlt(hdc,x1,y1,x2,y2,hMemDC,xc,yc,
bm.bmWidth,bm.bmHeight,SRCCOPY);
}

int hBmpCls::BMPBlt(int x1, int y1, int x2, int y2, int xc, int yc) {
BitBlt(hdc, x1, y1, x2, y2, hMemDC, xc, yc, SRCCOPY);
}






I fseek, therefore I fam.

[edited by - drarem on May 7, 2002 5:47:33 PM]

Share this post


Link to post
Share on other sites