  uses Ogl

  % TRUE  1
  % FALSE 0

  int PixelForm
  '
  function SelectPixelFormat(sys hDC,optional n) as int
  =====================================================

  static PIXELFORMATDESCRIPTOR pfd

  'setup pixel format
  pfd.nSize           = sizeof PIXELFORMATDESCRIPTOR 'Size of UDT structure
  pfd.nVersion        = 1                            'Version. Always set to 1.
  pfd.dwFlags         = PFD_DRAW_TO_WINDOW or _      'Support Window
                        PFD_SUPPORT_OPENGL or _      'Support OpenGL
                        PFD_DOUBLEBUFFER             'Support Double Buffering
  pfd.iPixelType      = PFD_TYPE_RGBA                'Red, Green, Blue, & Alpha Mode
  pfd.cColorBits      = 32                           '32-Bit Color Mode
  pfd.cRedBits        = 0                            'Ignore Color and Shift Bits...
  pfd.cRedShift       = 0                            '...
  pfd.cGreenBits      = 0                            '...
  pfd.cGreenShift     = 0                            '...
  pfd.cBlueBits       = 0                            '...
  pfd.cBlueShift      = 0                            '...
  pfd.cAlphaBits      = 0                            'No Alpha Buffer
  pfd.cAlphaShift     = 0                            'Ignore Shift Bit.
  pfd.cAccumBits      = 0                            'No Accumulation Buffer
  pfd.cAccumRedBits   = 0                            'Ignore Accumulation Bits...
  pfd.cAccumGreenBits = 0                            '...
  pfd.cAccumBlueBits  = 0                            '...
  pfd.cAccumAlphaBits = 0                            '... Good Cereal! ;)
  pfd.cDepthBits      = 24                           ' bits z-buffer depth 8 16 24
  pfd.cStencilBits    = 8                            'Stencil Buffer
  pfd.cAuxBuffers     = 0                            'No Auxiliary Buffer
  pfd.iLayerType      = PFD_MAIN_PLANE               'Main Drawing Plane
  pfd.bReserved       = 0                            'Reserved
  pfd.dwLayerMask     = 0                            'Ignore Layer Masks...
  pfd.dwVisibleMask   = 0                            '...
  pfd.dwDamageMask    = 0                            '...
  if n then pixelForm = ChoosePixelFormat(hDC, @pfd) 'First without multisampling
  '
  SetPixelFormat(hDC, PixelForm, @pfd)
  end function 'set antialias
  '
  function SelectMultiSample(sys hDC,ns)
  ======================================
  '
  'IDENTIFY MULTISAMPLING FORMAT
  'http://www.opengl.org/wiki/Creating_an_OpenGL_Context#Proper_Context_Creation
  int attribs[]=
  {
    0x2001,01,     'WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
    0x2010,01,     'WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
    0x2011,01,     'WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
    0x2013,0x202B, 'WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
    0x2014,24,     'WGL_COLOR_BITS_ARB, 24,
    0x201B,08,     'WGL_ALPHA_BITS_ARB, 08,
    0x2022,24,     'WGL_DEPTH_BITS_ARB, 24,
    0x2023,08,     'WGL_STENCIL_BITS_ARB, 08, 
    0x2041,01,     'WGL_SAMPLE_BUFFERS_ARB, 01, //Number of buffers (must be 1 at time of writing)
    0x2042,ns,     'WGL_SAMPLES_ARB, 04,        //Number of samples
    0x0000,00      'terminate list
  }
  sys p,a,n
  p=wglGetProcAddress "wglChoosePixelFormatARB"
  if p
    call p(hdc, &attribs, null, 1, &a, &n)
    if n then pixelForm=a : return -1 'supporting multisample / antialias
  end if
  end function


  macro CreateMainWindowPass
  ==========================
  hwnd = CreateWindowEx( 
    0,                  'no extended styles           
    "wins",             'class name                   
    "Main Window",      'window name  
    style,              '                
    hpos,               'horizontal position  
    vpos,               'vertical position    
    width,              'width                
    height,             'height               
    null,               'no parent or owner window    
    null,               'class menu used              
    hinst,              'instance handle              
    null);              'no window creation data
  if not hWnd then
    MessageBox 0,"Unable to create window","problem",MB_ICONERROR
    exit function
  end if
  end macro
  '

  macro CreateMainWindow
  ======================

  minCreate=1 'pass for pixel format selection
  CreateMainWindowPass
  hDC=GetDC hwnd
  SelectPixelformat hDC,1 'Setup pfd then set pixelformat
  hRC = wglCreateContext hDC
  wglMakeCurrent hDC, hRC
  %% MultiSamples 4 'default
  if Multisamples
    SelectMultiSample hDC,MultiSamples
  end if
  wglDeleteContext hRC
  ReleaseDC hWnd,hDC
  DestroyWindow hWnd
  '
  'Recreate the main window with full initialisation
  '
  minCreate=0
  CreateMainWindowPass
  '
  end macro

