xprintf.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //____________________________________________________________________________
  2. //
  3. // File: xprintf.cpp
  4. //
  5. // Description: Display a printf style message on the current video frame
  6. //
  7. // Author: Keith Looney
  8. //
  9. //____________________________________________________________________________
  10. // Revision History
  11. //
  12. //____________________________________________________________________________
  13. // Includes
  14. #include <stdio.h>
  15. #include <stdarg.h>
  16. #include <windows.h>
  17. #include "xprintf.h"
  18. //________ ____________________________________________________________________
  19. // Defines
  20. //____________________________________________________________________________
  21. // Declarations
  22. //____________________________________________________________________________
  23. // Definitions
  24. /****************************************************************************
  25. *
  26. * Function : xprintf
  27. *
  28. * Description : Display a printf style message on the current video frame
  29. *
  30. * INPUTS :
  31. *
  32. * OUTPUTS :
  33. *
  34. * RETURNS : void
  35. *
  36. * Notes :
  37. *
  38. * ERRORS :
  39. *
  40. ****************************************************************************/
  41. int vp5_xprintf(const PB_INSTANCE* ppbi, long nPixel, const char* format, ...)
  42. {
  43. HFONT hfont,hfonto;
  44. va_list arglist;
  45. char szFormatted[256] = "";
  46. UINT8* pDest = &ppbi->PostProcessBuffer[nPixel];
  47. long nSizeY = ppbi->HFragments * 8;
  48. long nStride = ppbi->Configuration.YStride;
  49. BOOL bRC;
  50. int rc = 0;
  51. // Format text
  52. va_start(arglist, format);
  53. _vsnprintf(szFormatted, sizeof(szFormatted), format, arglist);
  54. va_end(arglist);
  55. // Set up temporary bitmap
  56. HDC hdcMemory = NULL;
  57. HBITMAP hbmTemp = NULL;
  58. HBITMAP hbmOrig = NULL;
  59. RECT rect;
  60. rect.left = 0;
  61. rect.top = 0;
  62. rect.right = 8 * strlen(szFormatted);
  63. rect.bottom = 8;
  64. hdcMemory = CreateCompatibleDC(NULL);
  65. if (hdcMemory == NULL)
  66. {
  67. goto Exit;
  68. }
  69. hbmTemp = CreateBitmap(rect.right, rect.bottom, 1, 1, NULL);
  70. if (hbmTemp == NULL)
  71. {
  72. goto Exit;
  73. }
  74. hbmOrig = static_cast<HBITMAP>(SelectObject(hdcMemory, hbmTemp));
  75. if(!hbmOrig)
  76. {
  77. goto Exit;
  78. }
  79. // Write text into bitmap
  80. // font?
  81. hfont = CreateFont(8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,VARIABLE_PITCH | FF_SWISS, "");
  82. if(hfont == NULL)
  83. {
  84. goto Exit;
  85. }
  86. hfonto = static_cast<HFONT>(SelectObject(hdcMemory, hbmTemp));
  87. if(!hfonto)
  88. {
  89. goto Exit;
  90. }
  91. SelectObject (hdcMemory, hfont);
  92. SetTextColor(hdcMemory, 1);
  93. SetBkColor(hdcMemory, 0);
  94. SetBkMode(hdcMemory, TRANSPARENT);
  95. bRC = BitBlt(hdcMemory, rect.left, rect.top, rect.right, rect.bottom, hdcMemory, rect.left, rect.top, BLACKNESS);
  96. if (!bRC)
  97. {
  98. goto Exit;
  99. }
  100. bRC = ExtTextOut(hdcMemory, 0, 0, ETO_CLIPPED, &rect, szFormatted, strlen(szFormatted), NULL);
  101. if (!bRC)
  102. {
  103. goto Exit;
  104. }
  105. // Copy bitmap to video frame
  106. long x;
  107. long y;
  108. for (y = rect.top; y < rect.bottom; ++y)
  109. {
  110. for (x = rect.left; x < rect.right; ++x)
  111. {
  112. if (GetPixel(hdcMemory, x, rect.bottom - 1 - y))
  113. {
  114. pDest[x] = 255;
  115. }
  116. }
  117. pDest += nStride;
  118. }
  119. rc = strlen(szFormatted);
  120. Exit:
  121. if (hbmTemp != NULL)
  122. {
  123. if (hbmOrig != NULL)
  124. {
  125. SelectObject(hdcMemory, hbmOrig);
  126. }
  127. DeleteObject(hbmTemp);
  128. }
  129. if (hfont != NULL)
  130. {
  131. if (hfonto!= NULL)
  132. {
  133. SelectObject(hdcMemory, hfonto);
  134. }
  135. DeleteObject(hfont);
  136. }
  137. if (hdcMemory != NULL)
  138. {
  139. DeleteDC(hdcMemory);
  140. }
  141. hdcMemory = 0;
  142. return rc;
  143. }