Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Drawing a bitmap

Drawing a bitmap

Scheduled Pinned Locked Moved C / C++ / MFC
graphicslearningc++helptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pugman812
    wrote on last edited by
    #1

    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.

    C 1 Reply Last reply
    0
    • P Pugman812

      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.

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      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

      P 1 Reply Last reply
      0
      • C Chris Losinger

        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

        P Offline
        P Offline
        Pugman812
        wrote on last edited by
        #3

        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.

        C 1 Reply Last reply
        0
        • P Pugman812

          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.

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups