J
JoeC
Why does this crash? I am passing pointers around but I am confused
why this dosn't work.
This is a fairy long program and I hope I am posting relevent parts
class space{
char gchar;
graphic *gr; //symbol of the square
graphic *grDefault; //symol if not seen (blank)
player * play; //pointer to the player object
bool seen;
public:
space(); //constructor
~space(); //destructor
void graphicIn(char g); loads the graphic type
graphic& graphicOut();//returns pointer to the graphic object.
void playIn(player*); //sets the player
bool isPlay(); //checks of a player is present in the space.
void see(){seen = true;} //makes spaces seen so they are displayed
on the screen
bool been(){return seen;} //checks to see if the space has been
seen
void playOut(); //removes the player from the space
bool canMove(); //checks if the space is open for moving
bool winspace(); //checks if the player reaced the end of the maze.
};
graphic& board::display(int y, int x){
return spaces[y][x].graphicOut(); //returns the graphic object in
a space.
}
graphic& space::graphicOut(){
//if(play){return play->gOut();} <-- crashes here.
//gets the graphic from the player if it is in that space
if(seen){return *gr;} returns seen graphics, wall or floor
else {return *grDefault;} //if not seen return a blank space.
}
class player{
string name;
graphic * gr; // holds the player graphic
void create();
public:
player();
graphic& gOut() {return *gr;} //returns the graphic from the
player.
};
Here is my graphic class. It could be designed better and I am still
working on it. I will take suggestions. My program compiles but crashs
and I don't understand why. I realize I am using a lot of pointers but
it seems the best way to make my program work.
Graphic class:
class graphic{
int btmap;
int lr,ud; //Diminsion (size) of the graphic
HBITMAP hbitmap;
BITMAP bitmap;
HDC hdc, hdcmem;
public:
graphic();
graphic(BYTE c[]);
graphic& operator = (const graphic&);
void SetGr(BYTE c[]);
void display(HWND, int, int);
};
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
};
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = c;
hbitmap = CreateBitmapIndirect(&bitmap);
}
void graphic::SetGr(BYTE c[]){
//Changing the graphic
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = c;
hbitmap = CreateBitmapIndirect(&bitmap);
}
graphic::graphic(BYTE c[]){
//constructor need an array 32 int or hex elements to create graphic
ud = lr = 16; // size in bits
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = c;
hbitmap = CreateBitmapIndirect(&bitmap);
//MessageBox(NULL, "Graphic Created" , "Error!", MB_OK);
}
graphic& graphic:perator = (const graphic& gr){
if(&gr != this){
lr = ud = 16;
BITMAP bitmap = {0,ud,lr,2,1,1};
hbitmap = gr.hbitmap;
hbitmap = CreateBitmapIndirect(&bitmap);
}
return *this;
}
void graphic::display(HWND hwnd, int x, 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);
}
why this dosn't work.
This is a fairy long program and I hope I am posting relevent parts
class space{
char gchar;
graphic *gr; //symbol of the square
graphic *grDefault; //symol if not seen (blank)
player * play; //pointer to the player object
bool seen;
public:
space(); //constructor
~space(); //destructor
void graphicIn(char g); loads the graphic type
graphic& graphicOut();//returns pointer to the graphic object.
void playIn(player*); //sets the player
bool isPlay(); //checks of a player is present in the space.
void see(){seen = true;} //makes spaces seen so they are displayed
on the screen
bool been(){return seen;} //checks to see if the space has been
seen
void playOut(); //removes the player from the space
bool canMove(); //checks if the space is open for moving
bool winspace(); //checks if the player reaced the end of the maze.
};
graphic& board::display(int y, int x){
return spaces[y][x].graphicOut(); //returns the graphic object in
a space.
}
graphic& space::graphicOut(){
//if(play){return play->gOut();} <-- crashes here.
//gets the graphic from the player if it is in that space
if(seen){return *gr;} returns seen graphics, wall or floor
else {return *grDefault;} //if not seen return a blank space.
}
class player{
string name;
graphic * gr; // holds the player graphic
void create();
public:
player();
graphic& gOut() {return *gr;} //returns the graphic from the
player.
};
Here is my graphic class. It could be designed better and I am still
working on it. I will take suggestions. My program compiles but crashs
and I don't understand why. I realize I am using a lot of pointers but
it seems the best way to make my program work.
Graphic class:
class graphic{
int btmap;
int lr,ud; //Diminsion (size) of the graphic
HBITMAP hbitmap;
BITMAP bitmap;
HDC hdc, hdcmem;
public:
graphic();
graphic(BYTE c[]);
graphic& operator = (const graphic&);
void SetGr(BYTE c[]);
void display(HWND, int, int);
};
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
};
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = c;
hbitmap = CreateBitmapIndirect(&bitmap);
}
void graphic::SetGr(BYTE c[]){
//Changing the graphic
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = c;
hbitmap = CreateBitmapIndirect(&bitmap);
}
graphic::graphic(BYTE c[]){
//constructor need an array 32 int or hex elements to create graphic
ud = lr = 16; // size in bits
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = c;
hbitmap = CreateBitmapIndirect(&bitmap);
//MessageBox(NULL, "Graphic Created" , "Error!", MB_OK);
}
graphic& graphic:perator = (const graphic& gr){
if(&gr != this){
lr = ud = 16;
BITMAP bitmap = {0,ud,lr,2,1,1};
hbitmap = gr.hbitmap;
hbitmap = CreateBitmapIndirect(&bitmap);
}
return *this;
}
void graphic::display(HWND hwnd, int x, 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);
}