@@ -198,7 +198,7 @@ std::chrono::milliseconds GetTimeSinceLastTtrade() {
198198 return std::chrono::duration_cast<std::chrono::milliseconds>(current - start);
199199}
200200
201- void executeTrade () {
201+ void ExecuteTrade () {
202202 if (GetTimeSinceLastTtrade () >= tradingFrequency) {
203203 for (const auto & [productName, info] : productSettings) {
204204 int sale = info.at (" sale" );
@@ -219,78 +219,25 @@ void executeTrade() {
219219 }
220220}
221221
222- void CopyRenderResult (uint16_t * destBuffer, uint32_t * srcBuffer, int surfaceWidth, int surfaceHeight, int pitch) {
223- RECT rectClip = Menu::GetClipRect ();
224-
225- int xStart = rectClip.left ;
226- int xEnd = rectClip.right ;
227- int yStart = rectClip.top ;
228- int yEnd = rectClip.bottom ;
229-
230- for (int y = yStart; y < yEnd; ++y) {
231- uint16_t * destRow = (uint16_t *)((BYTE*)destBuffer + y * pitch);
232- uint32_t * srcRow = srcBuffer + y * surfaceWidth;
233-
234- for (int x = xStart; x < xEnd; ++x) {
235- uint32_t argb = srcRow[x];
236- uint8_t r = (argb >> 16 ) & 0xFF ;
237- uint8_t g = (argb >> 8 ) & 0xFF ;
238- uint8_t b = argb & 0xFF ;
239-
240- uint16_t rgb565 = ((r >> 3 ) << 11 ) | ((g >> 2 ) << 5 ) | (b >> 3 );
241- destRow[x] = rgb565;
242- }
243- }
244- }
245-
246222HRESULT WINAPI MyBlt (IDirectDrawSurface* pDestSurface, LPCRECT lpDestRect, IDirectDrawSurface* pSrcSurface, LPCRECT lpSrcRect, DWORD dwFlags, LPDDBLTFX lpDDBltFx) {
247- executeTrade ();
223+ ExecuteTrade ();
248224
249225 HRESULT hr = oBlt (pDestSurface, lpDestRect, pSrcSurface, lpSrcRect, dwFlags, lpDDBltFx);
250226
251- if (FAILED (hr)) {
227+ if (FAILED (hr))
252228 return hr;
253- }
254229
255230 if (!Menu::IsOpenMenu ())
256231 return hr;
257232
258- DDSURFACEDESC ddsd;
259- ZeroMemory (&ddsd, sizeof (ddsd));
260- ddsd.dwSize = sizeof (ddsd);
233+ HDC hDC;
261234
262- if (FAILED (pDestSurface->Lock ( NULL , &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL ))) {
235+ if (! SUCCEEDED (pDestSurface->GetDC (&hDC )))
263236 return E_FAIL;
264- }
265-
266- BITMAPINFO bmi;
267- ZeroMemory (&bmi, sizeof (BITMAPINFO));
268- bmi.bmiHeader .biSize = sizeof (BITMAPINFOHEADER);
269- bmi.bmiHeader .biWidth = ddsd.dwWidth ;
270- bmi.bmiHeader .biHeight = -static_cast <int >(ddsd.dwHeight );
271- bmi.bmiHeader .biPlanes = 1 ;
272- bmi.bmiHeader .biBitCount = 32 ;
273- bmi.bmiHeader .biCompression = BI_RGB;
274-
275- void * pPixels = NULL ;
276- HBITMAP hBitmap = CreateDIBSection (NULL , &bmi, DIB_RGB_COLORS, &pPixels, NULL , 0 );
277- if (!hBitmap) {
278- pDestSurface->Unlock (NULL );
279- return E_FAIL;
280- }
281-
282- HDC hdc = CreateCompatibleDC (NULL );
283- if (!hdc) {
284- DeleteObject (hBitmap);
285- pDestSurface->Unlock (NULL );
286- return E_FAIL;
287- }
288-
289- HBITMAP hOldBitmap = (HBITMAP)SelectObject (hdc, hBitmap);
290237
291238 std::string currentProductName = availableProducts[indexCurrentProduct];
292239
293- Menu::SetContext (hdc );
240+ Menu::SetContext (hDC );
294241 Menu::DrawBackground (RECT{0 , 0 , 300 , 139 });
295242 Menu::DrawTextField (RECT{5 , 5 , 95 , 34 }, L" product" );
296243 Menu::DrawTextField (RECT{99 , 5 , 294 , 34 }, ConvertToWString (currentProductName).c_str (), TEXT_ALIGN_LEFT);
@@ -302,13 +249,7 @@ HRESULT WINAPI MyBlt(IDirectDrawSurface* pDestSurface, LPCRECT lpDestRect, IDire
302249 Menu::DrawButton (RECT{190 , 104 , 239 , 133 }, L" ←" , ChoosePrevProduct);
303250 Menu::DrawButton (RECT{243 , 104 , 292 , 133 }, L" →" , ChooseNextProduct);
304251
305- CopyRenderResult ((uint16_t *)ddsd.lpSurface , (uint32_t *)pPixels, ddsd.dwWidth , ddsd.dwHeight , ddsd.lPitch );
306-
307- SelectObject (hdc, hOldBitmap);
308- DeleteObject (hBitmap);
309- DeleteDC (hdc);
310-
311- pDestSurface->Unlock (NULL );
252+ pDestSurface->ReleaseDC (hDC);
312253
313254 return hr;
314255}
0 commit comments