#include<windows.h>
#include<string>
using namespace std;
bool gameover = false;
int move = 0;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam);
class MyBitmap{
private:
HWND window;
HDC hdc;
HDC imagedc;
HBITMAP image;
HBITMAP oldimage;
int height;
int width;
int posx;
int posy;
char* filename;
public:
MyBitmap();
MyBitmap(HWND hwnd, char* file,int xwidth,int yheight);
int GetImageX();
int GetImageY();
void MoveImage(int newx,int newy);
void Draw();
void ChangeImage(char* file, int xwidth,int xheight);
void Cleanup();
char* GetImage();
};
class cPlayer{
private:
int x;
int y;
MyBitmap bitmap;
int direction;
int directionpos;
public:
cPlayer(HWND hwnd);
int GetX();
int GetY();
int GetDirection();
int GetDirectionPosition();
void Move(int dir);
void Cleanup();
void Draw();
};
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdline,int iCmd){
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "class";
wc.lpszMenuName = NULL;
wc.style = CS_VREDRAW|CS_HREDRAW;
RegisterClass(&wc);
HWND hwnd = CreateWindow("class","My Window",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
cPlayer player1(hwnd);
ShowWindow(hwnd,iCmd);
UpdateWindow(hwnd);
MSG msg;
while(!gameover){
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
player1.Draw();
}
}
player1.Cleanup();
UnregisterClass("class",hInstance);
return 0;
}
MyBitmap::MyBitmap(){
}
MyBitmap::MyBitmap(HWND hwnd, char* file,int xwidth,int yheight){
hdc = GetDC(hwnd);
imagedc = CreateCompatibleDC(hdc);
image = (HBITMAP)LoadImage(NULL,file,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
width = xwidth;
height = yheight;
window = hwnd;
posx = 0;
posy = 0;
filename = file;
}
int MyBitmap::GetImageX(){
return posx;
}
int MyBitmap::GetImageY(){
return posy;
}
void MyBitmap::MoveImage(int newx,int newy){
posx = posx + newx;
posy = posy + newy;
}
void MyBitmap::Draw(){
SelectObject(imagedc,image);
BitBlt(hdc,posx,posy,width,height,imagedc,0,0,SRCCOPY);
}
void MyBitmap::ChangeImage(char* file, int xwidth,int xheight){
image = (HBITMAP)LoadImage(NULL,file,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
width = xwidth;
height = xheight;
}
void MyBitmap::Cleanup(){
oldimage = (HBITMAP)SelectObject(imagedc,image);
DeleteObject(oldimage);
DeleteDC(imagedc);
ReleaseDC(window, hdc);
}
char* MyBitmap::GetImage(){
return filename;
}
cPlayer::cPlayer(HWND hwnd){
bitmap = MyBitmap(hwnd,"front1.bmp",100,100);
x = 0;
y = 0;
direction = 1;
directionpos = 1;
}
int cPlayer::GetX(){
return x;
}
int cPlayer::GetY(){
return y;
}
int cPlayer::GetDirection(){
return direction;
}
int cPlayer::GetDirectionPosition(){
return directionpos;
}
void cPlayer::Move(int dir){
if(dir == 1){
if(direction == 1){
bitmap.MoveImage(0,10);
if(directionpos == 1){
directionpos = 2;
bitmap.ChangeImage("pacfront2.bmp",100,100);
}
if(directionpos == 2){
directionpos = 1;
bitmap.ChangeImage("pacfront1.bmp",100,100);
}
}
else{
directionpos = 1;
direction = 1;
bitmap.ChangeImage("pacfront1.bmp",100,100);
}
}
if(dir == 2){
if(direction == 2){
bitmap.MoveImage(0,-10);
if(directionpos == 1){
directionpos = 2;
bitmap.ChangeImage("pacback2.bmp",100,100);
}
if(directionpos == 2){
directionpos = 1;
bitmap.ChangeImage("pacback1.bmp",100,100);
}
}
else{
directionpos = 1;
direction = 2;
bitmap.ChangeImage("pacback1.bmp",100,100);
}
}
if(dir == 3){
if(direction == 3){
bitmap.MoveImage(0,10);
if(directionpos == 1){
directionpos = 2;
bitmap.ChangeImage("pacright2.bmp",100,100);
}
if(directionpos == 2){
directionpos = 1;
bitmap.ChangeImage("pacright1.bmp",100,100);
}
}
else{
direction = 3;
directionpos = 1;
bitmap.ChangeImage("pacright1.bmp",100,100);
}
}
if(dir == 4){
if(direction == 4){
bitmap.MoveImage(0,-10);
if(directionpos == 1){
directionpos = 2;
bitmap.ChangeImage("pacleft2.bmp",100,100);
}
if(directionpos == 2){
directionpos = 1;
bitmap.ChangeImage("pacleft1.bmp",100,100);
}
}
else{
direction = 4;
directionpos = 1;
bitmap.ChangeImage("pacleft1.bmp",100,100);
}
}
}
void cPlayer::Cleanup(){
bitmap.Cleanup();
}
void cPlayer::Draw(){
bitmap.Draw();
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
switch(iMsg){
case WM_DESTROY:
PostQuitMessage(0);
gameover = true;
break;
case WM_KEYDOWN:
switch(wParam){
case VK_UP:
move = 1;
break;
case VK_DOWN:
move = 2;
break;
case VK_RIGHT:
move = 3;
break;
case VK_LEFT:
move = 4;
break;
}
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
I need some help.
Can anyone figure out why my program isn't drawing my bitmaps to the screen? The bitmaps I specified are all valid bitmaps I don't see why it doesn't work. Sorry it's so sloppy:
THANKS VERY MUCH FOR ANY HELP,
It can't be my bitmap class I know that because I used it seperately before. Maybe my player class i'm not sure.
thanks,
thanks,
I'm not sure (I haven't used the WinAPI stuff for drawing) - but you're not updating hwnd with UpdateWindow in your game loop. If bitBlt draws to the back-buffer, then you need to update during your loop.
Might help, might not!
Jim.
Might help, might not!
Jim.
Like I said - wasn't sure if the WinAPI did double buffering.
Oh well, I'll have a trawl through your code.
Jim.
Oh well, I'll have a trawl through your code.
Jim.
You do know that you never call player1.move, don't you?
And when do you call a WM_PAINT message once you've hit the message loop? (Or am I still barking up the wrong tree!)
Jim.
And when do you call a WM_PAINT message once you've hit the message loop? (Or am I still barking up the wrong tree!)
Jim.
Well you can double buffer but i haven't set that up yet.
Thanks for the friendly input thought,
Thanks for the friendly input thought,
Thanks lol. I forgot to call move. Also I have never used the wm_paint thing. I saw others use it with painstructs but I never knew why.
Well - I just cut and pasted it into VC2003 with a dummy bitmap and it worked fine.
Is your front1.bmp non-empty? If it is empty, because you never run player1.move, you'll never see it.
Other than that - sorry, got no idea!
Regards,
Jim.
Is your front1.bmp non-empty? If it is empty, because you never run player1.move, you'll never see it.
Other than that - sorry, got no idea!
Regards,
Jim.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement