468,321 Members | 1,820 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

How to use DrawDibDraw

I'm trying to learn programming and I've got quite far so far, but I've run
into a few problems, the main ones involving DrawDibDraw. First, 12 bytes
seem to be missing from "bitmapinfoheader" that I see in the hex edittor.
Second, I'm puzzled about the difference with the Dst and Src values and what
they're for. Below my code is where my issues are explained in better detail.

Expand|Select|Wrap|Line Numbers
  1. // First Windows app.cpp : Defines the entry point for the application.
  2. /*
  3. To do before making the first useful Windows application - what background
  4. scaling is (or "The Supernatural Olympics" version 1.1):
  5. Draw images
  6. Syncronize drawing images with monitor refresh rate
  7. Call a function upon pressing a key
  8. */
  9.  
  10. /*    Trim fat from windows*/
  11. #define WIN32_LEAN_AND_MEAN
  12. #pragma comment(linker, "/subsystem:windows")
  13. /*    Pre-processor directives*/
  14. #include "stdafx.h"
  15. #include <stdio.h>
  16. #include <windows.h>
  17.  
  18. // defines and global vars
  19. #define x 0
  20. #define y 1
  21.  
  22. short screen_size[2]; // screen resolution (e.g. 1920x1440)
  23. short window_size_base[2]; // expected window size
  24. short window_size[2]; // actual window size
  25. short window_pos[2]; // window position
  26. short title_bar_height; // height of the title bar in pixels
  27. short window_border[2]; // size of the window border
  28.  
  29. int file_handle; // handle for reading/writing files
  30. char bmp_image[]; // data for the bitmap image
  31.  
  32. int *img_handle; // pointer for a handle
  33. int *HDC_handle; // pointer for HDC handle
  34.  
  35. BITMAPINFOHEADER bmp_head;
  36.  
  37. void draw_test_image()
  38. {
  39. img_handle = DrawDibOpen();
  40.  
  41. file_handle = fopen("C:\\My Documents\\My programs\\ulillilliacity.bmp",
  42. "rb"); // read the source BMP file to display, binary mode
  43. fseek(file_handle, 2, seek_set); // skip the first two bytes, the file
  44. identifier, the string "BM"
  45. // fill the struct reading from the file
  46. fread(&bmp_head.biSize, 4, 1, file_handle); // actual file size
  47. // 12 bytes missing here, according to what I'm seeing in the hex edittor
  48. fread(&bmp_head.biWidth, 4, 1, file_handle);
  49. fread(&bmp_head.biHeight, 4, 1, file_handle);
  50. fread(&bmp_head.biPlanes, 2, 1, file_handle);
  51. fread(&bmp_head.biBitCount, 2, 1, file_handle);
  52. fread(&bmp_head.biCompression, 4, 1, file_handle);
  53. fread(&bmp_head.biSizeImage, 4, 1, file_handle);
  54. fread(&bmp_head.biXPelsPerMeter, 4, 1, file_handle);
  55. fread(&bmp_head.biYPelsPerMeter, 4, 1, file_handle);
  56. fread(&bmp_head.biClrUsed, 4, 1, file_handle);
  57. fread(&bmp_head.biClrImportant, 4, 1, file_handle);
  58. fread(&bmp_head.biClrImportant, 4, 1, file_handle);
  59. fread(&bmp_image, 1, bmp_head.biSizeImage, file_handle); // read the
  60. remainder of the bits, the image data part
  61. fclose(file_handle);
  62.  
  63. DrawDibDraw(img_handle, hDC, 64, 48, 512, 384, bmp_head, bmp_image, 64, 48,
  64. 512, 384, 0); // still not sure about the first group of numbers
  65. DrawDibClose(img_handle); // free the resources
  66. }
  67.  
  68. /*    Windows Procedure Event Handler*/
  69. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM
  70. lParam)
  71. {
  72. PAINTSTRUCT paintStruct;
  73. /*    Device Context*/
  74. HDC hDC; // is this the DC declaration?
  75. /*    Text for display*/
  76. char string[] = "Image test - there should be an image below";
  77. /*    Switch message, condition that is met will execute*/
  78. switch(message)
  79. {
  80. /*    Window is being created*/
  81. case WM_CREATE:
  82. return 0;
  83. break;
  84. /*    Window is closing*/
  85. case WM_CLOSE:
  86. PostQuitMessage(0);
  87. return 0;
  88. break;
  89. /*    Window needs update*/
  90. case WM_PAINT:
  91. hDC = BeginPaint(hwnd,&paintStruct);
  92. /*    Set txt color to blue*/
  93. SetTextColor(hDC, COLORREF(0x00FF8040));
  94. /*    Display text in middle of window*/
  95. TextOut(hDC,0,2,string,sizeof(string)-1);
  96. EndPaint(hwnd, &paintStruct);
  97. return 0;
  98. break;
  99. default:
  100. break;
  101. }
  102. return (DefWindowProc(hwnd,message,wParam,lParam));
  103. }
  104. /*    Main function*/
  105. int APIENTRY WinMain(HINSTANCE hInstance,
  106. HINSTANCE hPrevInstance,
  107. LPSTR     lpCmdLine,
  108. int       nCmdShow)
  109. {
  110. WNDCLASSEX  windowClass;        //window class
  111. HWND        hwnd;                //window handle
  112. MSG            msg;                //message
  113. bool        done;                //flag saying when app is complete
  114. /*    Fill out the window class structure*/
  115. windowClass.cbSize = sizeof(WNDCLASSEX);
  116. windowClass.style = CS_HREDRAW | CS_VREDRAW;
  117. windowClass.lpfnWndProc = WndProc;
  118. windowClass.cbClsExtra = 0;
  119. windowClass.cbWndExtra = 0;
  120. windowClass.hInstance = hInstance;
  121. windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  122. windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  123. windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  124. windowClass.lpszMenuName = NULL;
  125. windowClass.lpszClassName = "MyClass";
  126. windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
  127. /*    Register window class*/
  128. if (!RegisterClassEx(&windowClass))
  129. {
  130. return 0;
  131. }
  132.  
  133. /* SM_CXBORDER is 1
  134. SM_CYBORDER is 1
  135. SM_CXEDGE is 2
  136. SM_CYEDGE is 2
  137. SM_CXFIXEDFRAME is 3
  138. SM_CYFIXEDFRAME is 3
  139. SM_CXSIZEFRAME is 4
  140. SM_CYSIZEFRAME is 4
  141. SM_CYCAPTION is 18
  142. */
  143.  
  144. window_border[x] = GetSystemMetrics(SM_CXSIZEFRAME); // obtain border
  145. sizes, title bar height, and operating resolution
  146. window_border[y] = GetSystemMetrics(SM_CYSIZEFRAME);
  147. title_bar_height = GetSystemMetrics(SM_CYCAPTION); // obtain height of the
  148. title bar
  149. screen_size[x] = GetSystemMetrics(SM_CXSCREEN); // operating resolution
  150. screen_size[y] = GetSystemMetrics(SM_CYSCREEN);
  151. window_size_base[x] = 640; // interior window size
  152. window_size_base[y] = 480;
  153. window_size[x] = window_size_base[x]+window_border[x]*2; // full window
  154. size, including borders and title bar
  155. window_size[y] = window_size_base[y]+window_border[y]*2+title_bar_height;
  156. window_pos[x] = (screen_size[x]/2)-(window_size[x]/2); // center the window
  157. window_pos[y] = (screen_size[y]/2)-(window_size[y]/2);
  158.  
  159. /*    Class registerd, so now create window*/
  160. hwnd = CreateWindowEx(NULL,        //extended style
  161. "MyClass",            //class name
  162. "First Windows app",        //app name
  163. WS_OVERLAPPEDWINDOW |        //window style
  164. WS_VISIBLE |
  165. WS_SYSMENU,
  166. window_pos[x],window_pos[y],            //x/y coords
  167. window_size[x],window_size[y],            //width,height
  168. NULL,                //handle to parent
  169. NULL,                //handle to menu
  170. hInstance,            //application instance
  171. NULL);                //no extra parameter's
  172. /*    Check if window creation failed*/
  173. if (!hwnd)
  174. return 0;
  175. done = false; //initialize loop condition variable
  176. /*    main message loop*/
  177.  
  178. draw_test_image();
  179.  
  180. while(!done)
  181. {
  182. PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE);
  183. if (msg.message == WM_QUIT) //check for a quit message
  184. {
  185. free(*img_handle);
  186. done = true; //if found, quit app
  187. }
  188. else
  189. {
  190. /*    Translate and dispatch to event queue*/
  191. TranslateMessage(&msg);
  192. DispatchMessage(&msg);
  193. }
  194. }
  195. return msg.wParam;
  196. }
  197.  
These are my issues:
1. While reading from the BMP file, there seems to be an extra 12 bytes in
the file, as seen in my hex edittor (XVI32). The location of the missing
bytes is commented where it is found. I saved the 24-bit uncompressed BMP
file with "The GIMP".
2. I don't know if I set up the DrawDibDraw function properly, the pointers
and handles especially. I'm still confused about the Dst and Src values.
3. I want the interior part of the window to be 640x480, which excludes
borders, menus, and the title bar. I don't have any menus at the moment
though. I also want the window itself to be centered. The method I have works
fine, but seems awfully complicated so I'm wondering if there's a better way.
4. Without the bitmap reading stuff, my program is using 100% of the CPU
when all other windows programs barely use any of the CPU. Why is this and
how do I fix it? I suspect it has something to do with the while loop in the
WinMain function.

--
Some programming:
void bluewater() {while(game_runs == 1)
{if fox.position == in_blue_water) {fox.health -= 1;}
else {fox.wants_to_get_wet = true;} wait_frames(1);}}
Dec 13 '06 #1
1 3585
1. While reading from the BMP file, there seems to be an extra 12 bytes in
the file, as seen in my hex edittor (XVI32). The location of the missing
bytes is commented where it is found. I saved the 24-bit uncompressed BMP
file with "The GIMP".
It is not "extra". It is the BITMAPFILEHEADER structure.
2. I don't know if I set up the DrawDibDraw function properly, the
pointers
and handles especially. I'm still confused about the Dst and Src values.
The Src refers to a rectangle that defines that portion of your bitmap that
you wish to render.
You may choose the entire bitmap or part of the bitmap to render(e.g., crop
your bitmap).

The Dst refers to a rectangle that defines that portion of your window that
you wish to render to.
Again, you may choose to render to the entire window or some part of the
window.

The DrawDibDraw API is antiquated. However, there are many examples
included in the Platform SDK and/or online at MSDN's website.

You may wish to investigate the gdiplus API. It allows your to load and
draw a bitmap with a few lines of code.


"ulillillia" <ul********@discussions.microsoft.comwrote in message
news:20**********************************@microsof t.com...
I'm trying to learn programming and I've got quite far so far, but I've
run
into a few problems, the main ones involving DrawDibDraw. First, 12 bytes
seem to be missing from "bitmapinfoheader" that I see in the hex edittor.
Second, I'm puzzled about the difference with the Dst and Src values and
what
they're for. Below my code is where my issues are explained in better
detail.

Expand|Select|Wrap|Line Numbers
  1. // First Windows app.cpp : Defines the entry point for the application.
  2. /*
  3. To do before making the first useful Windows application - what background
  4. scaling is (or "The Supernatural Olympics" version 1.1):
  5. Draw images
  6. Syncronize drawing images with monitor refresh rate
  7. Call a function upon pressing a key
  8. */
  9. /* Trim fat from windows*/
  10. #define WIN32_LEAN_AND_MEAN
  11. #pragma comment(linker, "/subsystem:windows")
  12. /* Pre-processor directives*/
  13. #include "stdafx.h"
  14. #include <stdio.h>
  15. #include <windows.h>
  16. // defines and global vars
  17. #define x 0
  18. #define y 1
  19. short screen_size[2]; // screen resolution (e.g. 1920x1440)
  20. short window_size_base[2]; // expected window size
  21. short window_size[2]; // actual window size
  22. short window_pos[2]; // window position
  23. short title_bar_height; // height of the title bar in pixels
  24. short window_border[2]; // size of the window border
  25. int file_handle; // handle for reading/writing files
  26. char bmp_image[]; // data for the bitmap image
  27. int *img_handle; // pointer for a handle
  28. int *HDC_handle; // pointer for HDC handle
  29. BITMAPINFOHEADER bmp_head;
  30. void draw_test_image()
  31. {
  32. img_handle = DrawDibOpen();
  33. file_handle = fopen("C:\\My Documents\\My programs\\ulillilliacity.bmp",
  34. "rb"); // read the source BMP file to display, binary mode
  35. fseek(file_handle, 2, seek_set); // skip the first two bytes, the file
  36. identifier, the string "BM"
  37. // fill the struct reading from the file
  38. fread(&bmp_head.biSize, 4, 1, file_handle); // actual file size
  39. // 12 bytes missing here, according to what I'm seeing in the hex edittor
  40. fread(&bmp_head.biWidth, 4, 1, file_handle);
  41. fread(&bmp_head.biHeight, 4, 1, file_handle);
  42. fread(&bmp_head.biPlanes, 2, 1, file_handle);
  43. fread(&bmp_head.biBitCount, 2, 1, file_handle);
  44. fread(&bmp_head.biCompression, 4, 1, file_handle);
  45. fread(&bmp_head.biSizeImage, 4, 1, file_handle);
  46. fread(&bmp_head.biXPelsPerMeter, 4, 1, file_handle);
  47. fread(&bmp_head.biYPelsPerMeter, 4, 1, file_handle);
  48. fread(&bmp_head.biClrUsed, 4, 1, file_handle);
  49. fread(&bmp_head.biClrImportant, 4, 1, file_handle);
  50. fread(&bmp_head.biClrImportant, 4, 1, file_handle);
  51. fread(&bmp_image, 1, bmp_head.biSizeImage, file_handle); // read the
  52. remainder of the bits, the image data part
  53. fclose(file_handle);
  54. DrawDibDraw(img_handle, hDC, 64, 48, 512, 384, bmp_head, bmp_image, 64,
  55. 48,
  56. 512, 384, 0); // still not sure about the first group of numbers
  57. DrawDibClose(img_handle); // free the resources
  58. }
  59. /* Windows Procedure Event Handler*/
  60. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM
  61. lParam)
  62. {
  63. PAINTSTRUCT paintStruct;
  64. /* Device Context*/
  65. HDC hDC; // is this the DC declaration?
  66. /* Text for display*/
  67. char string[] = "Image test - there should be an image below";
  68. /* Switch message, condition that is met will execute*/
  69. switch(message)
  70. {
  71. /* Window is being created*/
  72. case WM_CREATE:
  73. return 0;
  74. break;
  75. /* Window is closing*/
  76. case WM_CLOSE:
  77. PostQuitMessage(0);
  78. return 0;
  79. break;
  80. /* Window needs update*/
  81. case WM_PAINT:
  82. hDC = BeginPaint(hwnd,&paintStruct);
  83. /* Set txt color to blue*/
  84. SetTextColor(hDC, COLORREF(0x00FF8040));
  85. /* Display text in middle of window*/
  86. TextOut(hDC,0,2,string,sizeof(string)-1);
  87. EndPaint(hwnd, &paintStruct);
  88. return 0;
  89. break;
  90. default:
  91. break;
  92. }
  93. return (DefWindowProc(hwnd,message,wParam,lParam));
  94. }
  95. /* Main function*/
  96. int APIENTRY WinMain(HINSTANCE hInstance,
  97.                     HINSTANCE hPrevInstance,
  98.                     LPSTR     lpCmdLine,
  99.                     int       nCmdShow)
  100. {
  101. WNDCLASSEX  windowClass; //window class
  102. HWND hwnd; //window handle
  103. MSG msg; //message
  104. bool done; //flag saying when app is complete
  105. /* Fill out the window class structure*/
  106. windowClass.cbSize = sizeof(WNDCLASSEX);
  107. windowClass.style = CS_HREDRAW | CS_VREDRAW;
  108. windowClass.lpfnWndProc = WndProc;
  109. windowClass.cbClsExtra = 0;
  110. windowClass.cbWndExtra = 0;
  111. windowClass.hInstance = hInstance;
  112. windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  113. windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  114. windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  115. windowClass.lpszMenuName = NULL;
  116. windowClass.lpszClassName = "MyClass";
  117. windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
  118. /* Register window class*/
  119. if (!RegisterClassEx(&windowClass))
  120. {
  121. return 0;
  122. }
  123. /* SM_CXBORDER is 1
  124. SM_CYBORDER is 1
  125. SM_CXEDGE is 2
  126. SM_CYEDGE is 2
  127. SM_CXFIXEDFRAME is 3
  128. SM_CYFIXEDFRAME is 3
  129. SM_CXSIZEFRAME is 4
  130. SM_CYSIZEFRAME is 4
  131. SM_CYCAPTION is 18
  132. */
  133. window_border[x] = GetSystemMetrics(SM_CXSIZEFRAME); // obtain border
  134. sizes, title bar height, and operating resolution
  135. window_border[y] = GetSystemMetrics(SM_CYSIZEFRAME);
  136. title_bar_height = GetSystemMetrics(SM_CYCAPTION); // obtain height of the
  137. title bar
  138. screen_size[x] = GetSystemMetrics(SM_CXSCREEN); // operating resolution
  139. screen_size[y] = GetSystemMetrics(SM_CYSCREEN);
  140. window_size_base[x] = 640; // interior window size
  141. window_size_base[y] = 480;
  142. window_size[x] = window_size_base[x]+window_border[x]*2; // full window
  143. size, including borders and title bar
  144. window_size[y] = window_size_base[y]+window_border[y]*2+title_bar_height;
  145. window_pos[x] = (screen_size[x]/2)-(window_size[x]/2); // center the
  146. window
  147. window_pos[y] = (screen_size[y]/2)-(window_size[y]/2);
  148. /* Class registerd, so now create window*/
  149. hwnd = CreateWindowEx(NULL, //extended style
  150. "MyClass", //class name
  151. "First Windows app", //app name
  152. WS_OVERLAPPEDWINDOW | //window style
  153. WS_VISIBLE |
  154. WS_SYSMENU,
  155. window_pos[x],window_pos[y], //x/y coords
  156. window_size[x],window_size[y], //width,height
  157. NULL, //handle to parent
  158. NULL, //handle to menu
  159. hInstance, //application instance
  160. NULL); //no extra parameter's
  161. /* Check if window creation failed*/
  162. if (!hwnd)
  163. return 0;
  164. done = false; //initialize loop condition variable
  165. /* main message loop*/
  166. draw_test_image();
  167. while(!done)
  168. {
  169. PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE);
  170. if (msg.message == WM_QUIT) //check for a quit message
  171. {
  172. free(*img_handle);
  173. done = true; //if found, quit app
  174. }
  175. else
  176. {
  177. /* Translate and dispatch to event queue*/
  178. TranslateMessage(&msg);
  179. DispatchMessage(&msg);
  180. }
  181. }
  182. return msg.wParam;
  183. }
  184.  

These are my issues:
1. While reading from the BMP file, there seems to be an extra 12 bytes in
the file, as seen in my hex edittor (XVI32). The location of the missing
bytes is commented where it is found. I saved the 24-bit uncompressed BMP
file with "The GIMP".
2. I don't know if I set up the DrawDibDraw function properly, the
pointers
and handles especially. I'm still confused about the Dst and Src values.
3. I want the interior part of the window to be 640x480, which excludes
borders, menus, and the title bar. I don't have any menus at the moment
though. I also want the window itself to be centered. The method I have
works
fine, but seems awfully complicated so I'm wondering if there's a better
way.
4. Without the bitmap reading stuff, my program is using 100% of the CPU
when all other windows programs barely use any of the CPU. Why is this and
how do I fix it? I suspect it has something to do with the while loop in
the
WinMain function.

--
Some programming:
void bluewater() {while(game_runs == 1)
{if fox.position == in_blue_water) {fox.health -= 1;}
else {fox.wants_to_get_wet = true;} wait_frames(1);}}

Dec 14 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Murat Aykut | last post: by
reply views Thread by Qindong Zhang | last post: by
reply views Thread by Qindong Zhang | last post: by
9 posts views Thread by Qindong Zhang | last post: by
3 posts views Thread by Iwanow | last post: by
4 posts views Thread by =?Utf-8?B?dWxpbGxpbGxpYQ==?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.