简化Winapi中的图像创建

时间:2017-04-07 21:56:50

标签: c++ winapi

根据我的理解,Winapi中的图像需要:

  • One HDC
  • 加载文件的一个HBITMAP
  • 一个“旧”HBITMAP

跟踪这些变量将是一项非常繁琐的工作,这意味着这是一个很好的情况,可以让它成为一个类。因此,从绘制调用到动画制作的所有内容都可以做得更顺畅。

这是我到目前为止所得到的:

class vec2i {    //Generic class expressing two integers
public:
    vec2i() {};
    vec2i(int i, int j) : x(i), y(j) {};
    bool operator==(vec2i & ptr) { return x == ptr.x && y == ptr.y; };
    bool operator!=(vec2i & ptr) { return x != ptr.x && y != ptr.y; };
    int x = 0, y = 0;
};

class sprite {    //Sprite class
public:
    sprite() {};

    sprite(HDC hdc, const std::string str, const vec2i d) : dim(d) {    //Full constructor
        handle = hdc;
        bmp = (HBITMAP)LoadImage(NULL, str.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
        old = (HBITMAP)SelectObject(handle, bmp);
    };

    ~sprite() {
        SelectObject(handle, old);
        DeleteObject(bmp);
        DeleteDC(handle);
   };

    void draw(HDC & param) {    //Simplified draw call
        TransparentBlt(param, pos.x, pos.y, dim.x, dim.y, handle, 0, 0, dim.x, dim.y, COLORREF(RGB(255, 0, 255)));
    };

private:
    vec2i       pos, dim;
    HDC         handle;
    HBITMAP     bmp, old;
};

主档案:

bool    running = true;
HDC     hDC;
sprite sprites;

//---------------------------------------------------------------------------
LRESULT CALLBACK    WndProcedure(HWND, UINT, WPARAM, LPARAM);
void    init(HWND);
void    cleanUp(HWND);
void    render();
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR 
lpCmdLine, int nCmdShow) {
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
    MyRegisterClass(hInstance);
    if (!InitInstance(hInstance, nCmdShow)) {
    MessageBox(NULL, (LPCSTR)"Application could not be initialised", (LPCSTR)"ERROR", MB_ICONERROR | MB_OK);
        return false;
    }

    MSG Msg;
    while (running == true) {
        if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        render(); 
    }
    return Msg.wParam;
}
//---------------------------------------------------------------------------
void render()
{
    Rectangle(hDC, 0, 0, 800, 600);
    sprites.draw(hDC);
}
//---------------------------------------------------------------------------
void init(HWND hWnd) {
    hDC     = GetDC(hWnd);
    sprites = sprite(CreateCompatibleDC(hDC), "landscape.bmp", vec2i(224, 150));
}
//---------------------------------------------------------------------------
void cleanUp(HWND hWnd) {
    ReleaseDC(hWnd, hDC);
    running = false;
    PostQuitMessage(WM_QUIT);
}
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch (Msg) {
    case WM_CREATE:
        init(hWnd);
        break;
    case WM_DESTROY:
        cleanUp(hWnd);
        break;
    default:
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    return 0;
}
//---------------------------------------------------------------------------

主文件有点冗长,但我试图删除所有不相关的内容。

然而,这堂课似乎没有画出来。加载图像,因为HBITMAP变量包含某种数据,但TransparentBlt函数返回false。在同一个cpp中编写所有内容似乎都有效,所以我看不出问题所在。

编辑:当我删除析构函数时,一切正常。将析构函数作为单独的函数并在程序退出之前调用它解决了我的问题。

0 个答案:

没有答案