如何使用屏幕外DC来一次性渲染位图的编辑? [英] How do I use an offscreen DC to render a compilation of bitmaps all at once?

查看:173
本文介绍了如何使用屏幕外DC来一次性渲染位图的编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一些代码:

PAINTSTRUCT ps;
HDC hidden = CreateCompatibleDC(NULL);
HBITMAP hiddenbmp = CreateBitmap(288,288,1,24,NULL);
HBITMAP hiddenold = (HBITMAP)SelectObject(hidden,hiddenbmp);
HDC other = GetDC(NULL);
HDC otherhdc = CreateCompatibleDC(other);
HBITMAP sprites;
if (color)
    sprites = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_COLOR_SPRITES));
else sprites = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BLACKWHITE_SPRITES));
HBITMAP otherold = (HBITMAP)SelectObject(otherhdc, sprites);


// Find x and y coordinate for the top left of the visible screen
int x = game.Player_x, y = game.Player_y, ypos = 0;
if (x < 4)  x = 4;
if (x > 27) x = 27;
if (y < 4)  y = 4;
if (y > 27) y = 27;
if (modx == -100) modx = x; // modx and mody are initialized to -100
else x = modx;
if (mody == -100) mody = y;
else y = mody;
x -= 4;
y -= 4;

// Draw lower layer
for (int i = 0; i < 9; i++)
{
    for (int j = 0; j < 9; j++)
    {
        if (game.Layer_Two[x + i][y + j] != 0)
        {
            int xpos = game.get_pos(game.Layer_Two[x + i][y + j], ypos, false);
            BitBlt(hidden, (i * 32), (j * 32), 32, 32, otherhdc, xpos, ypos, SRCCOPY);
        }
    }
}

// Draw upper layer
for (int i = 0; i < 9; i++)
{
    for (int j = 0; j < 9; j++)
    {
        if ((game.Layer_Two[x + i][y + j] != 0 && game.Layer_One[x + i][y + j] >= 64 && game.Layer_One[x + i][y + j] <= 111))
        {
            int xpos = game.get_pos(game.Layer_One[x + i][y + j], ypos, true);
            TransparentBlt(hidden, (i * 32), (j * 32), 32, 32, otherhdc, xpos, ypos, 32, 32, RGB(255, 255, 255));
        } else {
            int xpos = game.get_pos(game.Layer_One[x + i][y + j], ypos, false);
            BitBlt(hidden, (i * 32), (j * 32), 32, 32, otherhdc, xpos, ypos, SRCCOPY);
        }
    }
}

    // Draw the compiled image to the main window
HDC hdc = GetDC(hWnd);
BitBlt(hdc, 32, 32, 288, 288, hidden, 0, 0, SRCCOPY);

SelectObject(hidden,hiddenold);
DeleteDC(hidden);
DeleteObject(hiddenbmp);
SelectObject(other,otherold);
DeleteObject(other);
DeleteDC(otherhdc);

ReleaseDC(hWnd, hdc);

这是一个名为DrawMap()的函数 - 你知道什么 - 的9×9,32×32像素的2层)。我想要做的是在屏幕外(即不可见)DC中编译9乘9个图块,然后立即将其渲染到主窗口,这样就不可能看到图块被绘制的方式实际上是 - 从左到右,从上到下。有了这个代码,没有什么绘制到主窗口。

This is in a function called DrawMap() which - what do you know - draws a map (consisting of 2 layers of 9 by 9, 32 by 32 pixels tiles). What I'm trying to do is compile the 9 by 9 tiles in an offscreen (ie invisible) DC and then render it to the main window all at once so that it is impossible to see the tiles being drawn the way they actually are - left to right, top to bottom. With this code, nothing is drawn to the main window.

甚至weirder,我试图只使用'其他'hdc是为了隐藏otherhdc)。我在第35,48和51行使用了'otherhdc'作为源hdc和'其他'作为目的地hdc的BitBlt()和TransparentBlt()函数。然后'other'被复制到第56行的'hdc'(hWnd的DC)。这完全按照我想要的方式工作,EXCEPT'其他'被渲染到0,0在屏幕上(SCREEN,而不是WINDOW- 0,0在实际,物理屏幕上)。奇怪的。虽然我猜它基本上是我的目标,减去有'其他'绘制。

Even weirder, I tried using only the 'other' hdc (not the 'hidden' one - although my intention was for the 'otherhdc' to be hidden). I had the BitBlt() and TransparentBlt() functions on lines 35, 48 and 51 using 'otherhdc' as the source hdc and 'other' as the destination hdc. Then 'other' was copied to 'hdc' (hWnd's DC) on line 56. This worked exactly the way I wanted it to, EXCEPT 'other' was rendered to 0, 0 on the screen (the SCREEN, not the WINDOW - like 0, 0 on the ACTUAL, PHYSICAL SCREEN). Weird. Although I guess it's basically what I'm aiming for, minus having 'other' drawn.

我意识到,因为这将被使用很多,以最大限度地提高效率析构对于DC,并且不应该在函数中调用,而是应该在应用结束时调用(即仅一次)。

I realize that since this will be used a lot, to maximize efficiency the destructors for the DCs and such shouldn't be called in the function, but should be instead called at the end of the application (ie only one time). I just included them to give a better picture of the function.

推荐答案

我想出来了!

我找到给了我正确的方法。我只是使用一个hdc,我初始化为GetDC(hWnd),我设置为透明,然后再绘制任何东西。然后我把它设置为不透明的。容易吃。

I found this which gave me the right method. I just used one an hdc which I initialized to GetDC(hWnd), which I set to transparent before drawing anything to it. Then I set it to opaque afterwards. Easy-peasy.

这篇关于如何使用屏幕外DC来一次性渲染位图的编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆