使用SFML在屏幕上方绘制(Windows)

时间:2019-05-16 15:52:16

标签: c++ windows graphics sfml

在Windows上,您可以使用GDI在所有内容之上进行绘制,并将绘制上下文设为null:

HDC hdc = GetDC(NULL);

我希望对SFML做同样的事情,但是如果我尝试做一些等效的事情(创建一个以NULL作为参数的渲染窗口,将其转换为hwnd之后),则任何地方都不会绘制。 使用sfml我什至可以尝试吗?

1 个答案:

答案 0 :(得分:1)

好吧,如果您想要 OpenGL ,则需要一个窗口期。但是窗口不必在屏幕上可见。您可以将 GDI OpenGL 结合在一起以实现您的目标。

  1. 通过OpenGL在屏幕外将内容渲染为位图

    使用与您的桌面具有相同分辨率的不可见窗口。如果窗口不可见,它将不会对鼠标或键盘事件做出反应...

  2. 将GL映像复制到CPU侧存储器

    简单的glReadPixels就可以。

  3. 将图像复制到桌面(使用GDI位图)

    只需将原始图像数据转换/复制为GDI兼容位图,然后将其绘制到桌面画布上即可。因此,不再像标准GL应用程序那样SwapBuffers(hdc);

我在 C ++ / VCL 环境中进行编码,所以我没有纯粹的 WinAPI / GDI 知识(VCL为我做,但是代码应该与名称非常相似并且传递的操作数可能会有所不同,但不会太大。

这是我随附的:

//---------------------------------------------------------------------------
#include <vcl.h>
#include <gl\gl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1  *Form1;                 // VCL Application window object
TCanvas *scr=NULL;              // Desktop
DWORD   *txr=NULL;              // memory for GPU->CPU image transfer
Graphics::TBitmap *bmp=NULL;    // bitmap for CPU->Desktop image transfer
int     xs,ys;                  // desktop resolution
HDC     hdc=NULL;               // device context for GL
HGLRC   hrc=NULL;               // rendering context for GL
//---------------------------------------------------------------------------
void gl_draw()
    {
    if (scr==NULL) return;
    if (bmp==NULL) return;
    if (txr==NULL) return;

    glClearColor(0.0,0.0,0.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    // desktop pixel units
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glTranslatef(-1.0,+1.0,0.0);
    glScalef(2.0/float(xs),-2.0/float(ys),1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // render rectangle
    GLfloat fx=xs/2,fy=ys/2,fz=0.0,fa=(xs/2)-10,fb=(ys/2)-10;
    glColor3f(1.0,1.0,1.0);
    glBegin(GL_LINE_LOOP);
    glVertex3f(fx-fa,fy-fb,fz);
    glVertex3f(fx-fa,fy+fb,fz);
    glVertex3f(fx+fa,fy+fb,fz);
    glVertex3f(fx+fa,fy-fb,fz);
    glEnd();

    if (Form1->Visible)     // normal window GL render
        {
        glFlush();
        SwapBuffers(hdc);
        }
    else{                   // copy GL image directly to desktop
        // copy GL image to CPU side memory
        glFlush();
        glReadPixels(0,0,xs,ys,GL_RGBA,GL_UNSIGNED_BYTE,txr);
        // copy it to bitmap
        int x,y,a; DWORD *p;
        for (a=0,y=0;y<ys;y++)
         for (p=(DWORD*)bmp->ScanLine[y],x=0;x<xs;x++,a++)
          p[x]=txr[a];
        // render it to desktop
        scr->Draw(0,0,bmp);
        }

    }
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner):TForm(Owner)
    {
    // desktop
    scr=new TCanvas();
    scr->Handle=GetDC(NULL);
    xs=scr->ClipRect.Width();
    ys=scr->ClipRect.Height()-31;           // leave taskbar out of it
    // BMP
    bmp=new Graphics::TBitmap;
    bmp->HandleType=bmDIB;
    bmp->PixelFormat=pf32bit;
    bmp->SetSize(xs,ys);
    // txr buffer
    txr=new DWORD[xs*ys];
    // window
    BorderStyle=bsNone;
    SetBounds(0,0,xs,ys);
    // GL init
    hdc = GetDC(Handle);                    // get device context for this App window
    PIXELFORMATDESCRIPTOR pfd;
    ZeroMemory( &pfd, sizeof( pfd ) );      // set the pixel format for the DC
    pfd.nSize = sizeof( pfd );
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 24;
    pfd.iLayerType = PFD_MAIN_PLANE;
    SetPixelFormat(hdc,ChoosePixelFormat(hdc, &pfd),&pfd);
    hrc = wglCreateContext(hdc);            // create current rendering context
    if(hrc == NULL)
        {
        ShowMessage("Could not initialize OpenGL Rendering context !!!");
        Application->Terminate();
        }
    if(wglMakeCurrent(hdc, hrc) == false)
        {
        ShowMessage("Could not make current OpenGL Rendering context !!!");
        wglDeleteContext(hrc);          // destroy rendering context
        Application->Terminate();
        }
    glViewport(0,0,xs,ys);
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
    {
    // GL exit
    wglMakeCurrent(NULL, NULL);     // release current rendering context
    wglDeleteContext(hrc);          // destroy rendering context
    // release buffers
    if (scr){ delete scr; scr=NULL; }
    if (bmp){ delete bmp; bmp=NULL; }
    if (txr){ delete[] txr; txr=NULL; }
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
    {
    gl_draw();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
    {
    if (Visible) Visible=false; // hide
    gl_draw();
    }
//---------------------------------------------------------------------------

它具有单个计时器的单个Form VCL应用程序。它创建了GL上下文,并且在第一次机会时就变得不可见。然后它会定期用黑色背景和白色矩形边框覆盖桌面...

因此,您需要将VCL内容(Form1和事件)移植到您的环境中。另外,您可能想为行添加透明度:

scr->Draw(0,0,bmp);

read the desktop image并将其用作GL渲染的背景纹理。

PS。纯粹的GDI渲染比这要简单得多,并且可能更快,但是您需要GL,所以...