Skip to content

Commit 8b67a62

Browse files
authored
Merge pull request #4 from Ciptik/feature/improved-version
Fixed bug with distorted menu display
2 parents 8d853de + d8f7aa0 commit 8b67a62

File tree

2 files changed

+15
-63
lines changed

2 files changed

+15
-63
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

src/dllmain.cpp

Lines changed: 14 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -219,70 +219,26 @@ void executeTrade() {
219219
}
220220
}
221221

222-
void DrawRedSquare(IDirectDrawSurface* pSurface, int x, int y, int width, int height) {
223-
// Блокируем поверхность, чтобы получить доступ к пиксельным данным
224-
DDSURFACEDESC ddsd;
225-
ZeroMemory(&ddsd, sizeof(ddsd));
226-
ddsd.dwSize = sizeof(ddsd);
227-
228-
HRESULT hr = pSurface->Lock(NULL, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL);
229-
if (FAILED(hr)) {
230-
// Ошибка при блокировке поверхности
231-
return;
232-
}
233-
234-
// Получаем указатель на пиксельный буфер
235-
uint16_t* pixelBuffer = static_cast<uint16_t*>(ddsd.lpSurface);
236-
int pitch = ddsd.lPitch / sizeof(uint16_t); // Количество пикселей в одной строке (Pitch)
237-
238-
// Определяем цвет красного в формате RGB565
239-
uint16_t redColor = 0xF800; // RGB565 для красного цвета
240-
241-
// Рисуем квадрат
242-
for (int py = y; py < y + height; ++py) {
243-
if (py < 0 || py >= ddsd.dwHeight) continue; // Проверяем границы по высоте
244-
uint16_t* row = pixelBuffer + py * pitch;
245-
for (int px = x; px < x + width; ++px) {
246-
if (px < 0 || px >= ddsd.dwWidth) continue; // Проверяем границы по ширине
247-
row[px] = redColor;
248-
}
249-
}
250-
251-
// Разблокируем поверхность после работы с буфером
252-
pSurface->Unlock(NULL);
253-
}
254-
255-
void CopyRenderResult(uint16_t* destBuffer, uint32_t* srcBuffer) {
222+
void CopyRenderResult(uint16_t* destBuffer, uint32_t* srcBuffer, int surfaceWidth, int surfaceHeight, int pitch) {
256223
RECT rectClip = Menu::GetClipRect();
257224

258-
// Извлекаем координаты области для копирования
259225
int xStart = rectClip.left;
260226
int xEnd = rectClip.right;
261227
int yStart = rectClip.top;
262228
int yEnd = rectClip.bottom;
263229

264-
int width, height;
265-
GetWindowSize(width, height);
266-
267-
// Определяем ширину и высоту буферов
268-
int bufferWidth = width; // Предполагается, что эта функция возвращает ширину буфера
269-
270230
for (int y = yStart; y < yEnd; ++y) {
271-
for (int x = xStart; x < xEnd; ++x) {
272-
// Индекс текущего пикселя
273-
int index = y * bufferWidth + x;
231+
uint16_t* destRow = (uint16_t*)((BYTE*)destBuffer + y * pitch);
232+
uint32_t* srcRow = srcBuffer + y * surfaceWidth;
274233

275-
// Извлекаем цвет из srcBuffer (ARGB формат)
276-
uint32_t argb = srcBuffer[index];
234+
for (int x = xStart; x < xEnd; ++x) {
235+
uint32_t argb = srcRow[x];
277236
uint8_t r = (argb >> 16) & 0xFF;
278237
uint8_t g = (argb >> 8) & 0xFF;
279238
uint8_t b = argb & 0xFF;
280239

281-
// Преобразуем цвет в формат RGB565
282240
uint16_t rgb565 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
283-
284-
// Записываем преобразованный цвет в destBuffer
285-
destBuffer[index] = rgb565;
241+
destRow[x] = rgb565;
286242
}
287243
}
288244
}
@@ -304,32 +260,30 @@ HRESULT WINAPI MyBlt(IDirectDrawSurface* pDestSurface, LPCRECT lpDestRect, IDire
304260
ddsd.dwSize = sizeof(ddsd);
305261

306262
if (FAILED(pDestSurface->Lock(NULL, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL))) {
307-
return E_FAIL; // Не удалось заблокировать поверхность
263+
return E_FAIL;
308264
}
309265

310266
BITMAPINFO bmi;
311267
ZeroMemory(&bmi, sizeof(BITMAPINFO));
312268
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
313269
bmi.bmiHeader.biWidth = ddsd.dwWidth;
314-
bmi.bmiHeader.biHeight = -static_cast<int>(ddsd.dwHeight); // Отрицательная высота для верхнего левого начала
270+
bmi.bmiHeader.biHeight = -static_cast<int>(ddsd.dwHeight);
315271
bmi.bmiHeader.biPlanes = 1;
316-
bmi.bmiHeader.biBitCount = 32; // 32 бита на пиксель (8 бит на канал RGB + 8 бит на альфа-канал или просто 0)
317-
bmi.bmiHeader.biCompression = BI_RGB; // BI_RGB означает отсутствие сжатия
272+
bmi.bmiHeader.biBitCount = 32;
273+
bmi.bmiHeader.biCompression = BI_RGB;
318274

319-
// Создаем HBITMAP и привязываем его к пиксельному буферу
320-
void* pPixels = NULL; // Передаем NULL для выделения памяти
275+
void* pPixels = NULL;
321276
HBITMAP hBitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, &pPixels, NULL, 0);
322277
if (!hBitmap) {
323278
pDestSurface->Unlock(NULL);
324-
return E_FAIL; // Не удалось создать HBITMAP
279+
return E_FAIL;
325280
}
326281

327-
// Создаем совместимый HDC и выбираем HBITMAP
328282
HDC hdc = CreateCompatibleDC(NULL);
329283
if (!hdc) {
330284
DeleteObject(hBitmap);
331285
pDestSurface->Unlock(NULL);
332-
return E_FAIL; // Не удалось создать совместимый HDC
286+
return E_FAIL;
333287
}
334288

335289
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdc, hBitmap);
@@ -340,18 +294,15 @@ HRESULT WINAPI MyBlt(IDirectDrawSurface* pDestSurface, LPCRECT lpDestRect, IDire
340294
Menu::DrawBackground(RECT{0, 0, 300, 139});
341295
Menu::DrawTextField(RECT{5, 5, 95, 34}, L"product");
342296
Menu::DrawTextField(RECT{99, 5, 294, 34}, ConvertToWString(currentProductName).c_str(), TEXT_ALIGN_LEFT);
343-
344297
Menu::DrawTextField(RECT{5, 38, 95, 67}, L"sale >");
345298
Menu::DrawNumericField(RECT{99, 38, 294, 67}, RECT{210, 40, 249, 65}, RECT{253, 40, 292, 65}, productSettings[currentProductName]["sale"], 0, 500);
346-
347299
Menu::DrawTextField(RECT{5, 71, 95, 100}, L"buy <");
348300
Menu::DrawNumericField(RECT{99, 71, 294, 100}, RECT{210, 73, 249, 98}, RECT{253, 73, 292, 98}, productSettings[currentProductName]["buy"], 0, 500);
349-
350301
Menu::DrawButton(RECT{5, 104, 186, 133}, L"reset all settings", ResetAllSettings);
351302
Menu::DrawButton(RECT{190, 104, 239, 133}, L"", ChoosePrevProduct);
352303
Menu::DrawButton(RECT{243, 104, 292, 133}, L"", ChooseNextProduct);
353304

354-
CopyRenderResult((uint16_t*)ddsd.lpSurface, (uint32_t*)pPixels);
305+
CopyRenderResult((uint16_t*)ddsd.lpSurface, (uint32_t*)pPixels, ddsd.dwWidth, ddsd.dwHeight, ddsd.lPitch);
355306

356307
SelectObject(hdc, hOldBitmap);
357308
DeleteObject(hBitmap);

0 commit comments

Comments
 (0)