Object and Array Queestion.

J

JoeC

I am still working in my maze game and I am making improvments. The
main sticking problem that I have is passing my graphic library. This
probram uses windows apis but my question has nothing to do with those.
So please dont jump on me for that. I am making this graphics library
and my bitmap data is stored in an array. This library works some time
and when I have to use the copy constructor it dosn't seem to work. I
did my best to use the rule of three when using this object. This
object seems to work better if I use dynmaic memory. Could I get some
advice in this object to make it work bette.

In header:
graphic gr;

In Constructor:
gr.SetGr(pgr);

The Graphic Object:

class graphic{
int btmap;
int lr,ud; //Diminsion (size) of the graphic
HBITMAP hbitmap;
HDC hdc, hdcmem;
BYTE gData[32];
void gCopy(BYTE in[], BYTE out[]);
BYTE dOut(const int n) const {return gData[n];}

public:
graphic();
graphic(BYTE c[]);
graphic(const graphic&);
graphic& operator = (const graphic&);
void SetGr(BYTE c[]);
void display(HWND, const int, const int);

};

void graphic::gCopy(BYTE in[], BYTE out[]){
for(int lp = 0; lp != 32; lp++){
out[lp] = in[lp];
}
}

graphic::graphic(){
lr = ud = 16;
BYTE c[32] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};


gCopy(c, gData);
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = gData;
hbitmap = CreateBitmapIndirect(&bitmap);
}

#include"graphic.h"

graphic::graphic(BYTE c[]){
//constructor need an array 32 int or hex elements to create graphic

ud = lr = 16; // size in bits
gCopy(c, gData);
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = gData;
hbitmap = CreateBitmapIndirect(&bitmap);
//MessageBox(NULL, "Graphic Created" , "Error!", MB_OK);
}

graphic::graphic(const graphic& gr){
lr = ud = 16;
for(int lp = 0; lp != 32; lp++){
gData[lp] = gr.dOut(lp);
}

BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = gData;
hbitmap = CreateBitmapIndirect(&bitmap);
}

graphic& graphic::eek:perator = (const graphic& gr){
lr = ud = 16;
if(&gr != this){
for(int lp = 0; lp != 32; lp++){
gData[lp] = gr.dOut(lp);
}

BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = gData;
hbitmap = CreateBitmapIndirect(&bitmap);
}
return *this;
}

void graphic::SetGr(BYTE c[]){
lr = ud = 16;
//Changing the graphic
gCopy(c, gData);
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = gData;
hbitmap = CreateBitmapIndirect(&bitmap);
}

void graphic::display(HWND hwnd, const int x, const int y){
//prints the graphic on the screen
hdc = GetDC(hwnd);
hdcmem = CreateCompatibleDC(hdc);
SelectObject(hdcmem, hbitmap);
BitBlt(hdc, x, y, ud, lr, hdcmem, 0, 0, SRCCOPY);
DeleteDC(hdcmem);
ReleaseDC(hwnd, hdc);

}
 
P

Puppet_Sock

JoeC wrote:
[snips]
I am making this graphics library
and my bitmap data is stored in an array. This library works some time
and when I have to use the copy constructor it dosn't seem to work. I
did my best to use the rule of three when using this object. This
object seems to work better if I use dynmaic memory. Could I get some
advice in this object to make it work bette.
[code snipped]

In what way "dosn't seem to work"? That is, what does it do?
What does it not do? What is it supposed to do?

If you sit down and write out explicitly what it is supposed to
do, you may well be most of the way towards debugging all
your code. (Well, likely type it out in a doc so that you can
refer to it later.) It's a lot easier to find out where a code is
not doing what it is supposed to do if you actually define
what it is supposed to do.
Socks
 
H

Howard

JoeC said:
I am still working in my maze game and I am making improvments. The
main sticking problem that I have is passing my graphic library. This
probram uses windows apis but my question has nothing to do with those.
So please dont jump on me for that. I am making this graphics library
and my bitmap data is stored in an array. This library works some time
and when I have to use the copy constructor it dosn't seem to work.

What doesn't work? What goes wrong?
I did my best to use the rule of three when using this object.

Did you? Then where's the destructor?
This object seems to work better if I use dynmaic memory. Could I get
some
advice in this object to make it work bette.

In header:
graphic gr;

Is that a global variable, or a member of some class?
In Constructor:
gr.SetGr(pgr);

In what constructor? And what's 'pgr'?

Without knowing what the problem is, we're not going to be able to tell you
what might be wrong, unless we try to analyze the code you provided and see
if something MIGHT be wrong. Better if you tell us the problem.

-Howard
 
J

JoeC

I understand: When I create the object like this:
gchar = g;
if(gchar == '#'){gr = new graphic(wall);}
if(gchar == '.'){gr = new graphic(floor);}
if(gchar == 'X'){gr = new graphic(end);}

It work fine:
But when I create it like this:

In header:
graphic gr;

In Constructor:
gr.SetGr(pgr);

It dosn't display the grahics. It calls the copy constructor when I
send the graphics out of the player class to the proc loop for display.
The other graphics display fine. I am wondering if there is somthing
wrong with my code in the constructors or copy constructors.
 
P

Puppet_Sock

JoeC said:
I understand: When I create the object like this:
gchar = g;
if(gchar == '#'){gr = new graphic(wall);}
if(gchar == '.'){gr = new graphic(floor);}
if(gchar == 'X'){gr = new graphic(end);}

It work fine:
But when I create it like this:

In header:
graphic gr;

In Constructor:
gr.SetGr(pgr);

It dosn't display the grahics. It calls the copy constructor when I
send the graphics out of the player class to the proc loop for display.
The other graphics display fine. I am wondering if there is somthing
wrong with my code in the constructors or copy constructors.

Well, I think you need to read through the manuals on the
APIs you are using. But, a wild stab in the very dim light
(it's been two versions of windows since I did this stuff)
are you using CreateBitmapIndirect(&bitmap); correctly?
I see a call to it in the default ctor with what looks like a
black bitmap (all bits on). But where is this resource given
back? Or does it need to be? So, if things are going differently
when you do your default ctor from when you do your ctor
with a value, then you need to check that you are doing things
exactly correctly in each case. Check *both* ways of doing
things, and make sure you have not messed up either.

In any case, without going into a huge amount of detail to do
with the custom libraries you are using (hint: it's off topic in'
this group) it's hard to see what's going on here in any more
detail than that.
Socks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top