在Windows上,您可以使用GDI在所有内容上绘制所有内容,以null的平局上下文:
HDC hdc = GetDC(NULL);
我要对SFML进行相同的操作,但是如果我尝试某些等效的东西(将其施放给HWND之后,以null为null创建渲染窗口(。SFML是否可以尝试什么?
,如果您想要 opengl 您需要一个窗口期。但是窗口在屏幕上不需要可见。您可以将 GDI 和 OpenGL 组合在一起以实现您的目标。
-
通过OpenGl
将屏幕上的东西从屏幕上渲染到位图使用与桌面相同的分辨率的隐形窗口。如果窗口是看不见的,它将不会对鼠标或键盘事件做出反应...
-
将GL图像复制到CPU侧内存
简单的
glReadPixels
会做。 -
将图像复制到桌面(使用GDI位图(
只需将原始图像数据转换为GDI兼容位图,然后将其绘制到桌面帆布上即可。因此,不再像标准GL应用中的
SwapBuffers(hdc);
一样。
我正在 c /vcl 环境中编码,所以我没有纯 winapi/gdi/gdi knwoledge(vcl为我做到这一点,但代码应该非常相似通过操作数可能会有所不同,但没有太多(。
这就是我所带来的:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <glgl.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();
}
//---------------------------------------------------------------------------
它的单个表单VCL应用程序上有单个计时器。它创建GL上下文,第一次机会变得不可见。然后,它会定期用黑色背景和白色矩形边框覆盖桌面...
因此,您需要将VCL东西(Form1
和事件(移植到您的环境中。另外,您可能需要在该行中添加透明度:
scr->Draw(0,0,bmp);
或读取桌面图像并将其用作GL渲染的背景纹理。
ps。纯GDI渲染要简单得多,并且可能比此更快,但是您需要GL,所以...