From 347eb5647bc6c765911ff59d911a824ebf508535 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 9 Dec 2008 18:55:54 -0800 Subject: [PATCH] windows backend for ui.offscreen --- basis/ui/windows/windows.factor | 53 +++++++++++++++++++++++--- basis/windows/opengl32/opengl32.factor | 8 ++-- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/basis/ui/windows/windows.factor b/basis/ui/windows/windows.factor index f6f449625f..e696c3fa6f 100755 --- a/basis/ui/windows/windows.factor +++ b/basis/ui/windows/windows.factor @@ -69,9 +69,11 @@ M: pasteboard set-clipboard-contents drop copy ; clipboard set-global selection set-global ; -! world-handle is a -TUPLE: win hWnd hDC hRC world title ; +TUPLE: win-base hDC hRC ; +TUPLE: win hWnd < win-base world title ; +TUPLE: win-offscreen < win-base hBitmap bits ; C: win +C: win-offscreen SYMBOLS: msg-obj class-name-ptr mouse-captured ; @@ -478,7 +480,7 @@ M: windows-ui-backend do-events f class-name-ptr set-global f msg-obj set-global ; -: setup-pixel-format ( hdc -- ) +: setup-pixel-format ( hdc flags -- ) 16 make-pfd [ ChoosePixelFormat dup win32-error=0/f ] 2keep swapd SetPixelFormat win32-error=0/f ; @@ -489,7 +491,7 @@ M: windows-ui-backend do-events [ wglMakeCurrent win32-error=0/f ] keep ; : setup-gl ( hwnd -- hDC hRC ) - get-dc dup setup-pixel-format dup get-rc ; + get-dc dup windowed-pfd-dwFlags setup-pixel-format dup get-rc ; M: windows-ui-backend (open-window) ( world -- ) [ create-window dup setup-gl ] keep @@ -498,12 +500,51 @@ M: windows-ui-backend (open-window) ( world -- ) dupd (>>handle) hWnd>> show-window ; -M: win select-gl-context ( handle -- ) +M: win-base select-gl-context ( handle -- ) [ hDC>> ] keep hRC>> wglMakeCurrent win32-error=0/f ; -M: win flush-gl-context ( handle -- ) +M: win-base flush-gl-context ( handle -- ) hDC>> SwapBuffers win32-error=0/f ; +: (bitmap-info) ( dim -- BITMAPINFO ) + "BITMAPINFO" [ + BITMAPINFO-bmiHeader { + [ nip "BITMAPINFOHEADER" heap-size swap set-BITMAPINFOHEADER-biSize ] + [ [ first ] dip set-BITMAPINFOHEADER-biWidth ] + [ [ second ] dip set-BITMAPINFOHEADER-biHeight ] + [ nip 1 swap set-BITMAPINFOHEADER-biPlanes ] + [ nip 32 swap set-BITMAPINFOHEADER-biBitCount ] + [ nip BI_RGB swap set-BITMAPINFOHEADER-biCompression ] + [ [ first2 * 4 * ] dip swap set-BITMAPINFOHEADER-biSizeImage ] + [ nip 72 swap set-BITMAPINFOHEADER-biXPelsPerMeter ] + [ nip 72 swap set-BITMAPINFOHEADER-biYPelsPerMeter ] + [ nip 0 swap set-BITMAPINFOHEADER-biClrUsed ] + [ nip 0 swap set-BITMAPINFOHEADER-biClrImportant ] + } 2cleave + ] keep ; + +: make-offscreen-dc-and-bitmap ( dim -- hDC hBitmap bits ) + f CreateCompatibleDC + swap (bitmap-info) DIB_RGB_COLORS f + [ f 0 CreateDIBSection ] keep *void* + [ 2dup SelectObject drop ] dip ; + +: setup-offscreen-gl ( dim -- hDC hRC hBitmap bits ) + make-offscreen-dc-and-bitmap [ + [ dup offscreen-pfd-dwFlags setup-pixel-format ] + [ get-rc ] bi + ] 2dip ; + +M: windows-ui-backend (open-offscreen-buffer) ( world -- ) + dup dim>> setup-offscreen-gl + >>handle drop ; +M: windows-ui-backend (close-offscreen-buffer) ( handle -- ) + [ hDC>> DeleteObject drop ] + [ hBitmap>> DeleteObject drop ] bi ; + +M: win-offscreen offscreen-pixels ( handle -- alien ) + bits>> ; + ! Move window to front M: windows-ui-backend raise-window* ( world -- ) handle>> [ diff --git a/basis/windows/opengl32/opengl32.factor b/basis/windows/opengl32/opengl32.factor index df09d9327a..c09b30164f 100644 --- a/basis/windows/opengl32/opengl32.factor +++ b/basis/windows/opengl32/opengl32.factor @@ -71,15 +71,17 @@ IN: windows.opengl32 : WGL_SWAP_UNDERLAY14 HEX: 20000000 ; inline : WGL_SWAP_UNDERLAY15 HEX: 40000000 ; inline -: pfd-dwFlags ( -- n ) +: windowed-pfd-dwFlags ( -- n ) { PFD_DRAW_TO_WINDOW PFD_SUPPORT_OPENGL PFD_DOUBLEBUFFER } flags ; +: offscreen-pfd-dwFlags ( -- n ) + { PFD_DRAW_TO_BITMAP PFD_SUPPORT_OPENGL PFD_SUPPORT_GDI } flags ; ! TODO: compare to http://www.nullterminator.net/opengl32.html -: make-pfd ( bits -- pfd ) +: make-pfd ( flags bits -- pfd ) "PIXELFORMATDESCRIPTOR" "PIXELFORMATDESCRIPTOR" heap-size over set-PIXELFORMATDESCRIPTOR-nSize 1 over set-PIXELFORMATDESCRIPTOR-nVersion - pfd-dwFlags over set-PIXELFORMATDESCRIPTOR-dwFlags + rot over set-PIXELFORMATDESCRIPTOR-dwFlags PFD_TYPE_RGBA over set-PIXELFORMATDESCRIPTOR-iPixelType [ set-PIXELFORMATDESCRIPTOR-cColorBits ] keep 16 over set-PIXELFORMATDESCRIPTOR-cDepthBits -- 2.34.1