Drawing a bitmap
-
I am new to c++. i understand programming i am a advanced vb programmer, but i am just havin problems learning and applying all these new keywords. i am a beginnering in c but i have had experience with the very basic fundamental things. i am reading through a book and they want me to draw a single bitmap on a client window and have it jump around on the window and not go out of the window boundries. The only thing that happens right now is it just draws it over and over all over the screen without removing the previous instance of the bitmap. THIS IS MY CODE.
void Game_Run() { //the is called once ever frame //do not include your own loop here! int x = 0, y = 0; RECT rect; GetClientRect(global_hwnd, &rect); if (rect.right > 0) { x = rand() % (rect.right - rect.left); y = rand() % (rect.bottom - rect.top); DrawBitmap(global_hdc, "c.bmp", x, y); } }
void DrawBitmap(HDC hdcDest, char *filename, int x, int y) { HBITMAP image; BITMAP bm; HDC hdcMem; //load the bitmap image image = LoadImage(0,"c.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); //read the bitmaps properties GetObject(image, sizeof(BITMAP), &bm); //create a device context for the bitmap hdcMem = CreateCompatibleDC(global_hdc); SelectObject(hdcMem, image); //draw the bitmap to the window ( bit block transfer) BitBlt( global_hdc, //destination device context x,y, //x,y location on destination bm.bmWidth, bm.bmHeight, //width, height of source bitmap hdcMem, //source bitmap device context 0,0, //start x,y on source bitmap SRCCOPY); //blit method //delete the device context and bitmap DeleteDC(hdcMem); DeleteObject((HBITMAP)image); ShowWindow(global_hwnd, SW_SHOW); }
all i need it to do is draw once and move around the screen with the rand() function. Any help would be great because this is not a essential task of the book but i like to know how to anything with the language i am learning. THANK YOU. -
I am new to c++. i understand programming i am a advanced vb programmer, but i am just havin problems learning and applying all these new keywords. i am a beginnering in c but i have had experience with the very basic fundamental things. i am reading through a book and they want me to draw a single bitmap on a client window and have it jump around on the window and not go out of the window boundries. The only thing that happens right now is it just draws it over and over all over the screen without removing the previous instance of the bitmap. THIS IS MY CODE.
void Game_Run() { //the is called once ever frame //do not include your own loop here! int x = 0, y = 0; RECT rect; GetClientRect(global_hwnd, &rect); if (rect.right > 0) { x = rand() % (rect.right - rect.left); y = rand() % (rect.bottom - rect.top); DrawBitmap(global_hdc, "c.bmp", x, y); } }
void DrawBitmap(HDC hdcDest, char *filename, int x, int y) { HBITMAP image; BITMAP bm; HDC hdcMem; //load the bitmap image image = LoadImage(0,"c.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); //read the bitmaps properties GetObject(image, sizeof(BITMAP), &bm); //create a device context for the bitmap hdcMem = CreateCompatibleDC(global_hdc); SelectObject(hdcMem, image); //draw the bitmap to the window ( bit block transfer) BitBlt( global_hdc, //destination device context x,y, //x,y location on destination bm.bmWidth, bm.bmHeight, //width, height of source bitmap hdcMem, //source bitmap device context 0,0, //start x,y on source bitmap SRCCOPY); //blit method //delete the device context and bitmap DeleteDC(hdcMem); DeleteObject((HBITMAP)image); ShowWindow(global_hwnd, SW_SHOW); }
all i need it to do is draw once and move around the screen with the rand() function. Any help would be great because this is not a essential task of the book but i like to know how to anything with the language i am learning. THANK YOU.to do this, you need to make a copy of the image underneath the bitmap before you draw it. something like this: 1. find out where you want to draw the bitmap 2. make a copy of the background where the bitmap will go to a new bitmap, before you draw there. (create a new bitmap, use BitBlt to copy) 3. when you need to move the bitmap, draw the background copy from step 2 at the place you copied it from. then go to step 1. to do this smoothly, you'll need to use a double-buffering technique, where you place replace the background and draw the new bitmap in one BitBlt to the main screen. otherwise, you could get a flicker effect. i think there are articles on CP that discuss how to do double buffering. Cleek | Image Toolkits | Thumbnail maker
-
to do this, you need to make a copy of the image underneath the bitmap before you draw it. something like this: 1. find out where you want to draw the bitmap 2. make a copy of the background where the bitmap will go to a new bitmap, before you draw there. (create a new bitmap, use BitBlt to copy) 3. when you need to move the bitmap, draw the background copy from step 2 at the place you copied it from. then go to step 1. to do this smoothly, you'll need to use a double-buffering technique, where you place replace the background and draw the new bitmap in one BitBlt to the main screen. otherwise, you could get a flicker effect. i think there are articles on CP that discuss how to do double buffering. Cleek | Image Toolkits | Thumbnail maker
ok i understand what i need to do but how do i go about making a copy of the background to a bitmap. If i was programming in vb this would take me like 2 seconds but i want to learn c because i have always known and can tell now that its a more powerful language. I appreciate any help thank you.
-
ok i understand what i need to do but how do i go about making a copy of the background to a bitmap. If i was programming in vb this would take me like 2 seconds but i want to learn c because i have always known and can tell now that its a more powerful language. I appreciate any help thank you.
making a copy of the background is just a BitBlt to an off-screen DC, using a CBitmap you've created specifically for that purpose. but, this article[^] should help. Cleek | Image Toolkits | Thumbnail maker -- modified at 8:09 Wednesday 7th June, 2006