Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL);
- set_time_limit(120);
- $kernel32 = FFI::cdef("
- typedef const char * LPCSTR;
- typedef void * HINSTANCE;
- typedef HINSTANCE HMODULE;
- HMODULE GetModuleHandleA(LPCSTR lpModuleName);
- ", "kernel32.dll");
- $user32 = FFI::cdef("
- typedef int BOOL;
- typedef void * LPVOID;
- typedef void * HANDLE;
- typedef HANDLE HWND;
- typedef HANDLE HDC;
- typedef HANDLE HMENU;
- typedef HANDLE HINSTANCE;
- typedef const char *LPCSTR, *PCSTR;
- typedef unsigned int UINT;
- typedef unsigned char BYTE;
- typedef long LONG;
- typedef unsigned short WORD;
- typedef unsigned long DWORD;
- typedef WORD ATOM;
- typedef unsigned int UINT_PTR, *PUINT_PTR;
- typedef long LONG_PTR, *PLONG_PTR;
- typedef UINT_PTR WPARAM;
- typedef LONG_PTR LPARAM;
- typedef LONG_PTR LRESULT;
- typedef HANDLE HBRUSH;
- typedef HANDLE HICON;
- typedef HICON HCURSOR;
- typedef LONG (__stdcall* WNDPROC)(HWND, UINT, WPARAM, LONG);
- typedef struct tagRECT {
- LONG left;
- LONG top;
- LONG right;
- LONG bottom;
- } RECT,*PRECT,*LPRECT;
- typedef struct tagPAINTSTRUCT {
- HDC hdc;
- BOOL fErase;
- RECT rcPaint;
- BOOL fRestore;
- BOOL fIncUpdate;
- BYTE rgbReserved[32];
- } PAINTSTRUCT,*LPPAINTSTRUCT;
- typedef struct tagPOINT {
- LONG x;
- LONG y;
- } POINT,POINTL,*PPOINT,*LPPOINT,*PPOINTL,*LPPOINTL;
- typedef struct tagMSG {
- HWND hwnd;
- UINT message;
- WPARAM wParam;
- LPARAM lParam;
- DWORD time;
- POINT pt;
- } MSG,*LPMSG,*PMSG;
- typedef struct tagWNDCLASSEXA {
- UINT cbSize;
- UINT style;
- WNDPROC lpfnWndProc;
- int cbClsExtra;
- int cbWndExtra;
- HINSTANCE hInstance;
- HICON hIcon;
- HCURSOR hCursor;
- UINT hbrBackground;
- LPCSTR lpszMenuName;
- HANDLE lpszClassName;
- HICON hIconSm;
- } WNDCLASSEXA, *PWNDCLASSEXA, *NPWNDCLASSEXA, *LPWNDCLASSEXA;
- HDC BeginPaint(HWND,LPPAINTSTRUCT);
- HWND CreateWindowExA(DWORD,LPCSTR,LPCSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID);
- LRESULT DefWindowProcA(HWND,UINT,WPARAM,LPARAM);
- BOOL DestroyWindow(HWND);
- LONG DispatchMessageA(const MSG*);
- BOOL EndPaint(HWND,const PAINTSTRUCT*);
- HDC GetDC(HWND);
- BOOL GetMessageA(LPMSG,HWND,UINT,UINT);
- HCURSOR LoadCursorA(HINSTANCE,long);
- HICON LoadIconA(HINSTANCE,long);
- int MessageBoxA(HWND,LPCSTR,LPCSTR,UINT);
- BOOL PostMessageA(HWND,UINT,WPARAM,LPARAM);
- void PostQuitMessage(int);
- ATOM RegisterClassExA(const WNDCLASSEXA*);
- int ReleaseDC(HWND,HDC);
- BOOL ShowWindow(HWND,int);
- BOOL TranslateMessage(const MSG*);
- ", "user32.dll");
- define('IDI_APPLICATION', 32512);
- define('CS_OWNDC', 0x0020);
- define('IDI_WINLOGO', 32517);
- define('IDC_ARROW', 32512);
- define('MB_OK', 0);
- define('WM_PAINT', 15);
- define('WM_SIZE', 5);
- define('WM_CHAR', 258);
- define('WM_CLOSE', 16);
- define('WS_OVERLAPPEDWINDOW', 0xcf0000);
- define('WS_CLIPSIBLINGS', 0x4000000);
- define('WS_CLIPCHILDREN', 0x2000000);
- define('SW_SHOW', 5);
- define('SW_SHOWDEFAULT', 10);
- define('SW_SHOWNORMAL', 1);
- $opengl32 = FFI::cdef("
- typedef int BOOL;
- typedef void * HANDLE;
- typedef HANDLE HDC;
- typedef HANDLE HGLRC;
- typedef unsigned int GLenum;
- typedef unsigned int GLbitfield;
- typedef int GLint;
- typedef int GLsizei;
- typedef float GLfloat;
- void glClear( GLbitfield mask );
- void glFlush( void );
- void glViewport( GLint x, GLint y, GLsizei width, GLsizei height );
- void glBegin( GLenum mode );
- void glEnd( void );
- void glVertex2i( GLint x, GLint y );
- void glColor3f( GLfloat red, GLfloat green, GLfloat blue );
- HGLRC wglCreateContext(HDC);
- BOOL wglDeleteContext(HGLRC);
- BOOL wglMakeCurrent(HDC,HGLRC);
- ", "opengl32.dll");
- define('GL_TRIANGLES', 0x0004);
- define('GL_COLOR_BUFFER_BIT', 0x00004000);
- $gdi32 = FFI::cdef("
- typedef unsigned long DWORD;
- typedef int BOOL;
- typedef unsigned char BYTE;
- typedef unsigned short WORD;
- typedef unsigned int UINT;
- typedef void * HANDLE;
- typedef HANDLE HDC;
- typedef struct tagPIXELFORMATDESCRIPTOR {
- WORD nSize;
- WORD nVersion;
- DWORD dwFlags;
- BYTE iPixelType;
- BYTE cColorBits;
- BYTE cRedBits;
- BYTE cRedShift;
- BYTE cGreenBits;
- BYTE cGreenShift;
- BYTE cBlueBits;
- BYTE cBlueShift;
- BYTE cAlphaBits;
- BYTE cAlphaShift;
- BYTE cAccumBits;
- BYTE cAccumRedBits;
- BYTE cAccumGreenBits;
- BYTE cAccumBlueBits;
- BYTE cAccumAlphaBits;
- BYTE cDepthBits;
- BYTE cStencilBits;
- BYTE cAuxBuffers;
- BYTE iLayerType;
- BYTE bReserved;
- DWORD dwLayerMask;
- DWORD dwVisibleMask;
- DWORD dwDamageMask;
- } PIXELFORMATDESCRIPTOR,*PPIXELFORMATDESCRIPTOR,*LPPIXELFORMATDESCRIPTOR;
- int ChoosePixelFormat(HDC,const PIXELFORMATDESCRIPTOR*);
- int DescribePixelFormat(HDC,int,UINT,LPPIXELFORMATDESCRIPTOR);
- BOOL SetPixelFormat(HDC,int,const PIXELFORMATDESCRIPTOR*);
- ", "gdi32.dll");
- define('PFD_TYPE_RGBA', 0);
- define('PFD_DRAW_TO_WINDOW', 4);
- define('PFD_SUPPORT_OPENGL', 32);
- function LOWORD($l){return ($l)&0xFFFF;}
- function HIWORD($l){return ($l>>16)&0xFFFF;}
- function display(){
- global $opengl32;
- /* rotate a triangle around */
- $opengl32->glClear(GL_COLOR_BUFFER_BIT);
- $opengl32->glBegin(GL_TRIANGLES);
- $opengl32->glColor3f(1.0, 0.0, 0.0);
- $opengl32->glVertex2i(0, 1);
- $opengl32->glColor3f(0.0, 1.0, 0.0);
- $opengl32->glVertex2i(-1, -1);
- $opengl32->glColor3f(0.0, 0.0, 1.0);
- $opengl32->glVertex2i(1, -1);
- $opengl32->glEnd();
- $opengl32->glFlush();
- }
- $WindowProc = function($hWnd, $uMsg, $wParam, $lParam){
- global $user32, $opengl32;
- $ps = $user32->new("PAINTSTRUCT");
- switch($uMsg){
- case WM_PAINT:
- display();
- $user32->BeginPaint($hWnd, FFI::addr($ps));
- $user32->EndPaint($hWnd, FFI::addr($ps));
- return 0;
- case WM_SIZE:
- $opengl32->glViewport(0, 0, LOWORD($lParam), HIWORD($lParam));
- $user32->PostMessageA($hWnd, WM_PAINT, 0, 0);
- return 0;
- case WM_CHAR:
- switch($wParam){
- case 27: /* ESC key */
- $user32->PostQuitMessage(0);
- break;
- }
- return 0;
- case WM_CLOSE:
- $user32->PostQuitMessage(0);
- return 0;
- }
- return $user32->DefWindowProcA($hWnd, $uMsg, $wParam, $lParam);
- };
- function CreateOpenGLWindow($title, $x, $y, $width, $height, $type, $flags){
- global $kernel32, $user32, $gdi32, $WindowProc;
- $g_szClassName = "OpenGL";
- $szClassName = FFI::new(FFI::arrayType(FFI::type("char"), [strlen($g_szClassName)]));
- FFI::memcpy($szClassName, $g_szClassName, strlen($g_szClassName));
- $wc = $user32->new("WNDCLASSEXA");
- $pfd = $gdi32->new("PIXELFORMATDESCRIPTOR");
- $hInstance = 0;
- /* only register the window class once - use hInstance as a flag. */
- if(!$hInstance){
- $hInstance = $kernel32->GetModuleHandleA(NULL);
- $wc->cbSize = FFI::sizeof($wc);
- $wc->style = CS_OWNDC;
- $wc->lpfnWndProc = $WindowProc;
- $wc->cbClsExtra = 0;
- $wc->cbWndExtra = 0;
- $wc->hInstance = $hInstance;
- $wc->hIcon = $user32->LoadIconA(NULL, IDI_WINLOGO);
- $wc->hCursor = $user32->LoadCursorA(NULL, IDC_ARROW);
- $wc->hbrBackground = NULL;
- $wc->lpszMenuName = NULL;
- $wc->lpszClassName = FFI::addr($szClassName);
- $wc->hIconSm = $user32->LoadIconA(NULL, IDI_APPLICATION);
- if(!$user32->RegisterClassExA(FFI::addr($wc))){
- $user32->MessageBoxA(NULL, "RegisterClassEx() failed: ".
- "Cannot register window class.", "Error", MB_OK);
- return NULL;
- }
- }
- $hWnd = $user32->CreateWindowExA(0, $g_szClassName, $title, WS_OVERLAPPEDWINDOW |
- WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
- $x, $y, $width, $height, NULL, NULL, $hInstance, NULL);
- if($hWnd == NULL){
- $user32->MessageBoxA(NULL, "CreateWindow() failed: Cannot create a window.",
- "Error", MB_OK);
- return NULL;
- }
- $hDC = $user32->GetDC($hWnd);
- /* there is no guarantee that the contents of the stack that become
- the pfd are zeroed, therefore _make sure_ to clear these bits. */
- FFI::memset(FFI::addr($pfd), 0, FFI::sizeof($pfd));
- $pfd->nSize = FFI::sizeof($pfd);
- $pfd->nVersion = 1;
- $pfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | $flags;
- $pfd->iPixelType = $type;
- $pfd->cColorBits = 32;
- $pf = $gdi32->ChoosePixelFormat($hDC, FFI::addr($pfd));
- if($pf == 0){
- $user32->MessageBoxA(NULL, "ChoosePixelFormat() failed: " .
- "Cannot find a suitable pixel format.", "Error", MB_OK);
- return 0;
- }
- if($gdi32->SetPixelFormat($hDC, $pf, FFI::addr($pfd)) == FALSE){
- $user32->MessageBoxA(NULL, "SetPixelFormat() failed: " .
- "Cannot set format specified.", "Error", MB_OK);
- return 0;
- }
- $gdi32->DescribePixelFormat($hDC, $pf, FFI::sizeof($pfd), FFI::addr($pfd));
- $user32->ReleaseDC($hWnd, $hDC);
- return $hWnd;
- }
- exit(main());
- function main(){
- global $kernel32, $user32, $opengl32;
- //$nCmdShow = SW_SHOW;
- //$nCmdShow = SW_SHOWDEFAULT;
- $nCmdShow = SW_SHOWNORMAL;
- $msg = $user32->new("MSG");
- $hWnd = CreateOpenGLWindow("minimal", 0, 0, 256, 256, PFD_TYPE_RGBA, 0);
- if($hWnd == NULL){
- exit(1);
- }
- $hDC = $user32->GetDC($hWnd);
- $hRC = $opengl32->wglCreateContext($hDC);
- $opengl32->wglMakeCurrent($hDC, $hRC);
- $user32->ShowWindow($hWnd, $nCmdShow);
- while($user32->GetMessageA(FFI::addr($msg), $hWnd, 0, 0) > 0){
- $user32->TranslateMessage(FFI::addr($msg));
- $user32->DispatchMessageA(FFI::addr($msg));
- }
- $opengl32->wglMakeCurrent(NULL, NULL);
- $user32->ReleaseDC($hWnd, $hDC);
- $opengl32->wglDeleteContext($hRC);
- $user32->DestroyWindow($hWnd);
- return $msg->wParam;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement