BitBlt和COLORREF内存使用情况

时间:2017-02-27 14:13:46

标签: c++ memory

我写了一个代码,通过一个hdc上的BitBlt将一个COLORREF数组打印到一个窗口。但它使用了大量的内存。不确定是否泄漏或仅仅因为不正确的渲染方法。

需要一些更有效且相对容易实现的替代方法(其已经完成的项目和下面的功能已经实现了数百次)或者如果在泄漏问题时如何解决它的想法。 (我不能使用directx,opengl等来渲染场景)。

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <time.h>

using namespace std;

//MODIFY AS YOU WISH
const int   MAX_FPS          = 0;    /*MAX FPS DISPLAYED SET TO 0 TO NO LIMIT
setting over 50 may cause performance drops */
const int   KEYBOARD_DELAY   = 2;     //DELAY IN MS OF THE KEYBOARD INPUT INCREASING IS FPS KILLER
const bool  DISPLAYFPS       = true;  //DISPLAY FPS COUNTER ON A CORNER OF THE SCREEN

//CODE START
const int   MAX_RES_X        = 1920; //MAX WINDOW WIDTH RESOLUTION. LESS USES LESS MEMORY
const int   MAX_RES_Y        = 1080; //HEIGHT RES

int MAP_WIDTH; //SCENE PROPERTIES
int MAP_HEIGHT;

HBITMAP map;
HWND hwnd; //HANDLE TO MAIN WINDOW
HDC hdc; //HANDLE TO HDC
MSG msg; //WINDOW MESSAGE

COLORREF *SCENE = (COLORREF*) calloc(MAX_RES_X*MAX_RES_Y, sizeof(COLORREF));    //MAIN SCENE BUFFER

void render_init_fullscreen(char const* title) {

  FreeConsole();

  HMONITOR hmon = MonitorFromWindow(GetActiveWindow(), MONITOR_DEFAULTTONEAREST);
  MONITORINFO mi = { sizeof(mi) };
  if (!GetMonitorInfo(hmon, &mi)) return;
  hwnd = CreateWindow(TEXT("static"),
        TEXT(title),
        (WS_POPUP | WS_VISIBLE),
        mi.rcMonitor.left,
        mi.rcMonitor.top,
        mi.rcMonitor.right - mi.rcMonitor.left,
        mi.rcMonitor.bottom - mi.rcMonitor.top,
        0, 0, GetModuleHandle(0), 0);
  MAP_WIDTH=mi.rcMonitor.right;
  MAP_HEIGHT=mi.rcMonitor.bottom;

  ShowWindow( hwnd, SW_SHOWDEFAULT ) ;
  while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
  }
  HDC hdc_tmp = GetDC(hwnd);
  hdc = hdc_tmp;

    SetCapture(hwnd);
}

void render_render() {
  map = CreateBitmap(MAP_WIDTH,
                             MAP_HEIGHT,
                             1,
                             4*8,
                             (void*) SCENE);

  HDC src = CreateCompatibleDC(hdc);
  SelectObject(src, map);
  BitBlt(hdc,
         0,
         0,
         MAP_WIDTH,
         MAP_HEIGHT,
         src,
         0,
         0,
         SRCCOPY);

  DeleteDC(src);
  while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
  }
}

long __stdcall WindowProcedure( HWND window, unsigned int msg, WPARAM wp, LPARAM lp ) {
    switch(msg)
    {
        case WM_DESTROY:
            DestroyWindow(hwnd);
            exit(0);
            return 0L ;
            break;
    }
    return DefWindowProc( window, msg, wp, lp ) ;

}

0 个答案:

没有答案