DSDisplayOverlay.VMR.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #if defined(VMR)
  23. #include "internals/DSDisplayOverlay.h"
  24. #include "internals/DSDisplayGraph.h"
  25. #include "internals/DSUtils.h"
  26. #include "../../resource.h"
  27. using namespace std;
  28. #define ALPHA_VALUE_START 0.8f
  29. #define ALPHA_VALUE_STOP 0.0f
  30. // Hack to get module of the current code
  31. // Only works with Microsoft Linker and could break in the future
  32. EXTERN_C IMAGE_DOS_HEADER __ImageBase;
  33. DSDisplayOverlay::DSDisplayOverlay()
  34. {
  35. this->window = NULL;
  36. this->hdcBmp = NULL;
  37. this->hbmOld = NULL;
  38. }
  39. DSDisplayOverlay::~DSDisplayOverlay()
  40. {
  41. }
  42. void DSDisplayOverlay::attach(HWND parent, DSDisplayGraph *graph)
  43. {
  44. HRESULT hr;
  45. // Gets the handle of the parent and the graph
  46. this->window = parent;
  47. this->displayGraph = graph;
  48. if (this->window) {
  49. // Hack to get module of the current code
  50. TCHAR *modulePath = (TCHAR *) calloc(255, sizeof(TCHAR));
  51. GetModuleFileName((HINSTANCE)&__ImageBase, modulePath, 255);
  52. HMODULE module = GetModuleHandle(modulePath);
  53. delete[] modulePath;
  54. if (!module) {
  55. cout << "Failed to get current module";
  56. return;
  57. }
  58. HBITMAP bitmap = LoadBitmap(module, MAKEINTRESOURCE(IDB_BITMAP_OVERLAY));
  59. if (!bitmap) {
  60. cout << "Failed to load overlay bitmap" << endl;
  61. return;
  62. }
  63. RECT rect;
  64. hr = GetWindowRect(this->window, &rect);
  65. if (FAILED(hr)) {
  66. cout << "Failed to get window size" << endl;
  67. return;
  68. }
  69. BITMAP bm;
  70. HDC hdc = GetDC(this->window);
  71. this->hdcBmp = CreateCompatibleDC(hdc);
  72. ReleaseDC(this->window, hdc);
  73. GetObject(bitmap, sizeof(bm), &bm);
  74. this->hbmOld= (HBITMAP) SelectObject(this->hdcBmp, bitmap);
  75. ZeroMemory(&this->alphaBitmap, sizeof(VMRALPHABITMAP));
  76. this->alphaBitmap.dwFlags = VMRBITMAP_HDC | VMRBITMAP_SRCCOLORKEY;
  77. this->alphaBitmap.hdc = this->hdcBmp;
  78. this->alphaBitmap.clrSrcKey = 0x00FF00FF;
  79. // Source rectangle
  80. this->alphaBitmap.rSrc.left = 0;
  81. this->alphaBitmap.rSrc.top = 0;
  82. this->alphaBitmap.rSrc.right = bm.bmWidth;
  83. this->alphaBitmap.rSrc.bottom = bm.bmHeight;
  84. // Destination rectangle
  85. this->alphaBitmap.rDest.left = (rect.right - rect.left - bm.bmWidth) / 2.0;
  86. this->alphaBitmap.rDest.top = (rect.bottom - rect.top - bm.bmHeight) / 2.0;
  87. this->alphaBitmap.rDest.right = this->alphaBitmap.rDest.left + bm.bmWidth;
  88. this->alphaBitmap.rDest.bottom = this->alphaBitmap.rDest.top + bm.bmHeight;
  89. this->alphaBitmap.rDest.left /= (rect.right - rect.left);
  90. this->alphaBitmap.rDest.top /= (rect.bottom - rect.top);
  91. this->alphaBitmap.rDest.right /= (rect.right - rect.left);
  92. this->alphaBitmap.rDest.bottom /= (rect.bottom - rect.top);
  93. // Alpha value for start
  94. this->alphaBitmap.fAlpha = ALPHA_VALUE_START;
  95. }
  96. }
  97. void DSDisplayOverlay::detach()
  98. {
  99. // Clean up
  100. DeleteObject(SelectObject(this->hdcBmp, this->hbmOld));
  101. DeleteDC(this->hdcBmp);
  102. this->hdcBmp = NULL;
  103. this->hbmOld = NULL;
  104. this->displayGraph = NULL;
  105. this->window = NULL;
  106. }
  107. void DSDisplayOverlay::show(int value)
  108. {
  109. // Store the ticks to count down
  110. this->ticks = value;
  111. // Compute alpha value decrement
  112. this->alphaStep = (this->ticks > 0) ? ((ALPHA_VALUE_START - ALPHA_VALUE_STOP) / this->ticks) : 0;
  113. this->alphaBitmap.fAlpha = ALPHA_VALUE_START;
  114. this->internalUpdate();
  115. }
  116. void DSDisplayOverlay::update()
  117. {
  118. if (this->displayGraph && (this->ticks > 0)) {
  119. this->ticks--;
  120. // Be sure alpha is in 0.0 .. 1.0 range.
  121. float value = this->alphaBitmap.fAlpha;
  122. value -= this->alphaStep;
  123. this->alphaBitmap.fAlpha = (value >= 0.0f) ? value : 0.0f;
  124. this->internalUpdate();
  125. }
  126. }
  127. void DSDisplayOverlay::internalUpdate()
  128. {
  129. HRESULT hr;
  130. if (this->ticks > 0) {
  131. this->alphaBitmap.dwFlags = VMRBITMAP_HDC | VMRBITMAP_SRCCOLORKEY;
  132. }
  133. else {
  134. this->alphaBitmap.dwFlags = VMRBITMAP_DISABLE;
  135. }
  136. hr = this->displayGraph->getMixerBitmap()->SetAlphaBitmap(&this->alphaBitmap);
  137. if (FAILED(hr)) {
  138. cout << "Failed to mix overylay (" << hr << ")" << endl;
  139. return;
  140. }
  141. }
  142. #endif