Advertisement
krishean

minimal.php

Mar 5th, 2020
12,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.19 KB | None | 0 0
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4. set_time_limit(120);
  5.  
  6. $kernel32 = FFI::cdef("
  7. typedef const char *    LPCSTR;
  8. typedef void *          HINSTANCE;
  9. typedef HINSTANCE       HMODULE;
  10. HMODULE GetModuleHandleA(LPCSTR lpModuleName);
  11. ", "kernel32.dll");
  12.  
  13. $user32 = FFI::cdef("
  14. typedef int             BOOL;
  15. typedef void *          LPVOID;
  16. typedef void *          HANDLE;
  17. typedef HANDLE          HWND;
  18. typedef HANDLE          HDC;
  19. typedef HANDLE          HMENU;
  20. typedef HANDLE          HINSTANCE;
  21. typedef const char      *LPCSTR, *PCSTR;
  22. typedef unsigned int    UINT;
  23. typedef unsigned char   BYTE;
  24. typedef long            LONG;
  25. typedef unsigned short  WORD;
  26. typedef unsigned long   DWORD;
  27. typedef WORD            ATOM;
  28. typedef unsigned int    UINT_PTR, *PUINT_PTR;
  29. typedef long            LONG_PTR, *PLONG_PTR;
  30. typedef UINT_PTR        WPARAM;
  31. typedef LONG_PTR        LPARAM;
  32. typedef LONG_PTR        LRESULT;
  33. typedef HANDLE          HBRUSH;
  34. typedef HANDLE          HICON;
  35. typedef HICON           HCURSOR;
  36. typedef LONG (__stdcall* WNDPROC)(HWND, UINT, WPARAM, LONG);
  37. typedef struct tagRECT {
  38.    LONG left;
  39.    LONG top;
  40.    LONG right;
  41.    LONG bottom;
  42. } RECT,*PRECT,*LPRECT;
  43. typedef struct tagPAINTSTRUCT {
  44.     HDC hdc;
  45.    BOOL fErase;
  46.    RECT rcPaint;
  47.    BOOL fRestore;
  48.    BOOL fIncUpdate;
  49.    BYTE rgbReserved[32];
  50. } PAINTSTRUCT,*LPPAINTSTRUCT;
  51. typedef struct tagPOINT {
  52.    LONG x;
  53.    LONG y;
  54. } POINT,POINTL,*PPOINT,*LPPOINT,*PPOINTL,*LPPOINTL;
  55. typedef struct tagMSG {
  56.    HWND hwnd;
  57.    UINT message;
  58.    WPARAM wParam;
  59.    LPARAM lParam;
  60.    DWORD time;
  61.    POINT pt;
  62. } MSG,*LPMSG,*PMSG;
  63. typedef struct tagWNDCLASSEXA {
  64.    UINT      cbSize;
  65.    UINT      style;
  66.    WNDPROC   lpfnWndProc;
  67.    int       cbClsExtra;
  68.    int       cbWndExtra;
  69.    HINSTANCE hInstance;
  70.    HICON     hIcon;
  71.    HCURSOR   hCursor;
  72.    UINT      hbrBackground;
  73.    LPCSTR    lpszMenuName;
  74.    HANDLE    lpszClassName;
  75.    HICON     hIconSm;
  76. } WNDCLASSEXA, *PWNDCLASSEXA, *NPWNDCLASSEXA, *LPWNDCLASSEXA;
  77. HDC BeginPaint(HWND,LPPAINTSTRUCT);
  78. HWND CreateWindowExA(DWORD,LPCSTR,LPCSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID);
  79. LRESULT DefWindowProcA(HWND,UINT,WPARAM,LPARAM);
  80. BOOL DestroyWindow(HWND);
  81. LONG DispatchMessageA(const MSG*);
  82. BOOL EndPaint(HWND,const PAINTSTRUCT*);
  83. HDC GetDC(HWND);
  84. BOOL GetMessageA(LPMSG,HWND,UINT,UINT);
  85. HCURSOR LoadCursorA(HINSTANCE,long);
  86. HICON LoadIconA(HINSTANCE,long);
  87. int MessageBoxA(HWND,LPCSTR,LPCSTR,UINT);
  88. BOOL PostMessageA(HWND,UINT,WPARAM,LPARAM);
  89. void PostQuitMessage(int);
  90. ATOM RegisterClassExA(const WNDCLASSEXA*);
  91. int ReleaseDC(HWND,HDC);
  92. BOOL ShowWindow(HWND,int);
  93. BOOL TranslateMessage(const MSG*);
  94. ", "user32.dll");
  95.  
  96. define('IDI_APPLICATION', 32512);
  97. define('CS_OWNDC', 0x0020);
  98. define('IDI_WINLOGO', 32517);
  99. define('IDC_ARROW', 32512);
  100. define('MB_OK', 0);
  101. define('WM_PAINT', 15);
  102. define('WM_SIZE', 5);
  103. define('WM_CHAR', 258);
  104. define('WM_CLOSE', 16);
  105. define('WS_OVERLAPPEDWINDOW', 0xcf0000);
  106. define('WS_CLIPSIBLINGS', 0x4000000);
  107. define('WS_CLIPCHILDREN', 0x2000000);
  108. define('SW_SHOW', 5);
  109. define('SW_SHOWDEFAULT', 10);
  110. define('SW_SHOWNORMAL', 1);
  111.  
  112. $opengl32 = FFI::cdef("
  113. typedef int             BOOL;
  114. typedef void *          HANDLE;
  115. typedef HANDLE          HDC;
  116. typedef HANDLE          HGLRC;
  117. typedef unsigned int    GLenum;
  118. typedef unsigned int    GLbitfield;
  119. typedef int             GLint;
  120. typedef int             GLsizei;
  121. typedef float           GLfloat;
  122. void glClear( GLbitfield mask );
  123. void glFlush( void );
  124. void glViewport( GLint x, GLint y, GLsizei width, GLsizei height );
  125. void glBegin( GLenum mode );
  126. void glEnd( void );
  127. void glVertex2i( GLint x, GLint y );
  128. void glColor3f( GLfloat red, GLfloat green, GLfloat blue );
  129. HGLRC wglCreateContext(HDC);
  130. BOOL wglDeleteContext(HGLRC);
  131. BOOL wglMakeCurrent(HDC,HGLRC);
  132. ", "opengl32.dll");
  133.  
  134. define('GL_TRIANGLES', 0x0004);
  135. define('GL_COLOR_BUFFER_BIT', 0x00004000);
  136.  
  137. $gdi32 = FFI::cdef("
  138. typedef unsigned long   DWORD;
  139. typedef int             BOOL;
  140. typedef unsigned char   BYTE;
  141. typedef unsigned short  WORD;
  142. typedef unsigned int    UINT;
  143. typedef void *          HANDLE;
  144. typedef HANDLE          HDC;
  145. typedef struct tagPIXELFORMATDESCRIPTOR {
  146.     WORD nSize;
  147.     WORD nVersion;
  148.     DWORD dwFlags;
  149.     BYTE iPixelType;
  150.     BYTE cColorBits;
  151.     BYTE cRedBits;
  152.     BYTE cRedShift;
  153.     BYTE cGreenBits;
  154.     BYTE cGreenShift;
  155.     BYTE cBlueBits;
  156.     BYTE cBlueShift;
  157.     BYTE cAlphaBits;
  158.     BYTE cAlphaShift;
  159.     BYTE cAccumBits;
  160.     BYTE cAccumRedBits;
  161.     BYTE cAccumGreenBits;
  162.     BYTE cAccumBlueBits;
  163.     BYTE cAccumAlphaBits;
  164.     BYTE cDepthBits;
  165.     BYTE cStencilBits;
  166.     BYTE cAuxBuffers;
  167.     BYTE iLayerType;
  168.     BYTE bReserved;
  169.     DWORD dwLayerMask;
  170.     DWORD dwVisibleMask;
  171.     DWORD dwDamageMask;
  172. } PIXELFORMATDESCRIPTOR,*PPIXELFORMATDESCRIPTOR,*LPPIXELFORMATDESCRIPTOR;
  173. int ChoosePixelFormat(HDC,const PIXELFORMATDESCRIPTOR*);
  174. int DescribePixelFormat(HDC,int,UINT,LPPIXELFORMATDESCRIPTOR);
  175. BOOL SetPixelFormat(HDC,int,const PIXELFORMATDESCRIPTOR*);
  176. ", "gdi32.dll");
  177.  
  178. define('PFD_TYPE_RGBA', 0);
  179. define('PFD_DRAW_TO_WINDOW', 4);
  180. define('PFD_SUPPORT_OPENGL', 32);
  181.  
  182. function LOWORD($l){return ($l)&0xFFFF;}
  183. function HIWORD($l){return ($l>>16)&0xFFFF;}
  184.  
  185. function display(){
  186.     global $opengl32;
  187.     /* rotate a triangle around */
  188.     $opengl32->glClear(GL_COLOR_BUFFER_BIT);
  189.     $opengl32->glBegin(GL_TRIANGLES);
  190.     $opengl32->glColor3f(1.0, 0.0, 0.0);
  191.     $opengl32->glVertex2i(0, 1);
  192.     $opengl32->glColor3f(0.0, 1.0, 0.0);
  193.     $opengl32->glVertex2i(-1, -1);
  194.     $opengl32->glColor3f(0.0, 0.0, 1.0);
  195.     $opengl32->glVertex2i(1, -1);
  196.     $opengl32->glEnd();
  197.     $opengl32->glFlush();
  198. }
  199.  
  200. $WindowProc = function($hWnd, $uMsg, $wParam, $lParam){
  201.     global $user32, $opengl32;
  202.    
  203.     $ps = $user32->new("PAINTSTRUCT");
  204.    
  205.     switch($uMsg){
  206.         case WM_PAINT:
  207.             display();
  208.             $user32->BeginPaint($hWnd, FFI::addr($ps));
  209.             $user32->EndPaint($hWnd, FFI::addr($ps));
  210.             return 0;
  211.  
  212.         case WM_SIZE:
  213.             $opengl32->glViewport(0, 0, LOWORD($lParam), HIWORD($lParam));
  214.             $user32->PostMessageA($hWnd, WM_PAINT, 0, 0);
  215.             return 0;
  216.  
  217.         case WM_CHAR:
  218.             switch($wParam){
  219.                 case 27:            /* ESC key */
  220.                     $user32->PostQuitMessage(0);
  221.                     break;
  222.             }
  223.             return 0;
  224.  
  225.         case WM_CLOSE:
  226.             $user32->PostQuitMessage(0);
  227.             return 0;
  228.     }
  229.    
  230.     return $user32->DefWindowProcA($hWnd, $uMsg, $wParam, $lParam);
  231. };
  232.  
  233. function CreateOpenGLWindow($title, $x, $y, $width, $height, $type, $flags){
  234.     global $kernel32, $user32, $gdi32, $WindowProc;
  235.    
  236.     $g_szClassName = "OpenGL";
  237.     $szClassName = FFI::new(FFI::arrayType(FFI::type("char"), [strlen($g_szClassName)]));
  238.     FFI::memcpy($szClassName, $g_szClassName, strlen($g_szClassName));
  239.    
  240.     $wc = $user32->new("WNDCLASSEXA");
  241.     $pfd = $gdi32->new("PIXELFORMATDESCRIPTOR");
  242.     $hInstance = 0;
  243.    
  244.     /* only register the window class once - use hInstance as a flag. */
  245.     if(!$hInstance){
  246.         $hInstance = $kernel32->GetModuleHandleA(NULL);
  247.         $wc->cbSize        = FFI::sizeof($wc);
  248.         $wc->style         = CS_OWNDC;
  249.         $wc->lpfnWndProc   = $WindowProc;
  250.         $wc->cbClsExtra    = 0;
  251.         $wc->cbWndExtra    = 0;
  252.         $wc->hInstance     = $hInstance;
  253.         $wc->hIcon         = $user32->LoadIconA(NULL, IDI_WINLOGO);
  254.         $wc->hCursor       = $user32->LoadCursorA(NULL, IDC_ARROW);
  255.         $wc->hbrBackground = NULL;
  256.         $wc->lpszMenuName  = NULL;
  257.         $wc->lpszClassName = FFI::addr($szClassName);
  258.         $wc->hIconSm       = $user32->LoadIconA(NULL, IDI_APPLICATION);
  259.        
  260.         if(!$user32->RegisterClassExA(FFI::addr($wc))){
  261.             $user32->MessageBoxA(NULL, "RegisterClassEx() failed:  ".
  262.                 "Cannot register window class.", "Error", MB_OK);
  263.             return NULL;
  264.         }
  265.     }
  266.    
  267.     $hWnd = $user32->CreateWindowExA(0, $g_szClassName, $title, WS_OVERLAPPEDWINDOW |
  268.         WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
  269.         $x, $y, $width, $height, NULL, NULL, $hInstance, NULL);
  270.  
  271.     if($hWnd == NULL){
  272.         $user32->MessageBoxA(NULL, "CreateWindow() failed:  Cannot create a window.",
  273.             "Error", MB_OK);
  274.         return NULL;
  275.     }
  276.    
  277.     $hDC = $user32->GetDC($hWnd);
  278.    
  279.     /* there is no guarantee that the contents of the stack that become
  280.     the pfd are zeroed, therefore _make sure_ to clear these bits. */
  281.     FFI::memset(FFI::addr($pfd), 0, FFI::sizeof($pfd));
  282.     $pfd->nSize        = FFI::sizeof($pfd);
  283.     $pfd->nVersion     = 1;
  284.     $pfd->dwFlags      = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | $flags;
  285.     $pfd->iPixelType   = $type;
  286.     $pfd->cColorBits   = 32;
  287.    
  288.     $pf = $gdi32->ChoosePixelFormat($hDC, FFI::addr($pfd));
  289.     if($pf == 0){
  290.         $user32->MessageBoxA(NULL, "ChoosePixelFormat() failed:  " .
  291.             "Cannot find a suitable pixel format.", "Error", MB_OK);
  292.         return 0;
  293.     }
  294.    
  295.     if($gdi32->SetPixelFormat($hDC, $pf, FFI::addr($pfd)) == FALSE){
  296.         $user32->MessageBoxA(NULL, "SetPixelFormat() failed:  " .
  297.             "Cannot set format specified.", "Error", MB_OK);
  298.         return 0;
  299.     }
  300.    
  301.     $gdi32->DescribePixelFormat($hDC, $pf, FFI::sizeof($pfd), FFI::addr($pfd));
  302.    
  303.     $user32->ReleaseDC($hWnd, $hDC);
  304.    
  305.     return $hWnd;
  306. }
  307.  
  308. exit(main());
  309. function main(){
  310.     global $kernel32, $user32, $opengl32;
  311.    
  312.     //$nCmdShow = SW_SHOW;
  313.     //$nCmdShow = SW_SHOWDEFAULT;
  314.     $nCmdShow = SW_SHOWNORMAL;
  315.    
  316.     $msg = $user32->new("MSG");
  317.    
  318.     $hWnd = CreateOpenGLWindow("minimal", 0, 0, 256, 256, PFD_TYPE_RGBA, 0);
  319.    
  320.     if($hWnd == NULL){
  321.         exit(1);
  322.     }
  323.    
  324.     $hDC = $user32->GetDC($hWnd);
  325.     $hRC = $opengl32->wglCreateContext($hDC);
  326.     $opengl32->wglMakeCurrent($hDC, $hRC);
  327.    
  328.     $user32->ShowWindow($hWnd, $nCmdShow);
  329.    
  330.     while($user32->GetMessageA(FFI::addr($msg), $hWnd, 0, 0) > 0){
  331.         $user32->TranslateMessage(FFI::addr($msg));
  332.         $user32->DispatchMessageA(FFI::addr($msg));
  333.     }
  334.    
  335.     $opengl32->wglMakeCurrent(NULL, NULL);
  336.     $user32->ReleaseDC($hWnd, $hDC);
  337.     $opengl32->wglDeleteContext($hRC);
  338.     $user32->DestroyWindow($hWnd);
  339.    
  340.     return $msg->wParam;
  341. }
  342.  
  343. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement