Author Topic: Why wont GLU work with this framework?  (Read 10945 times)

0 Members and 1 Guest are viewing this topic.

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2756
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Why wont GLU work with this framework?
« Reply #20 on: July 19, 2009 »
Here's a new one, added mouse function and hopefully got window mode + mouse event fixed.


Code: [Select]
'---------------------------------------------------------------------------------
'
'   FreeBasic Window Framework by rbraz
'
'   19 Jul 2009
'
'---------------------------------------------------------------------------------


#include once "gl/gl.bi"
#include once "gl/glu.bi"
#include once "gl/glext.bi"
#include once "crt.bi"
#include once "windows.bi"

'DEVMODE bug, found the cure here --> http://www.freebasic.net/forum/viewtopic.php?t=5659&highlight=devmode
'Will be corrected by Victor in the next freebasic update
#undef DEVMODE
Type DEVMODE
    dmDeviceName As Zstring * CCHDEVICENAME
    dmSpecVersion As WORD
    dmDriverVersion As WORD
    dmSize As WORD
    dmDriverExtra As WORD
    dmFields As DWORD
    dmOrientation As Short
    dmPaperSize As Short
    dmPaperLength As Short
    dmPaperWidth As Short
    dmScale As Short
    dmCopies As Short
    dmDefaultSource As Short
    dmPrintQuality As Short
    dmColor As Short
    dmDuplex As Short
    dmYResolution As Short
    dmTTOption As Short
    dmCollate As Short
    dmFormName As Zstring * CCHFORMNAME
    dmLogPixels As WORD
    dmBitsPerPel As DWORD
    dmPelsWidth As DWORD
    dmPelsHeight As DWORD
    dmDisplayFlags As DWORD
    dmDisplayFrequency As DWORD
    dmICMMethod As DWORD
    dmICMIntent As DWORD
    dmMediaType As DWORD
    dmDitherType As DWORD
    dmReserved1 As DWORD
    dmReserved2 As DWORD
    dmPanningWidth As DWORD
    dmPanningHeight As DWORD
End Type



  'Win Main               
  Declare Function WinMain ( byval hInstance as HINSTANCE, _
                           byval hPrevInstance as HINSTANCE, _
                           szCmdLine as string, _
                           byval iCmdShow as integer ) as integer
    '
    ' Entry point   
    '
end WinMain( GetModuleHandle( null ), null, Command$, SW_NORMAL )   
   
'app window procedure
  Declare Function WndProc( byval hWnd as HWND, _
                   byval message as UINT, _
                   byval wParam as WPARAM, _
                   byval lParam as LPARAM ) as LRESULT   
                           
  'window creation helper
  Declare Function MakeWindow(byval iWidth as integer, _
                              byval iHeight as integer, _
                              byval hInstance as HINSTANCE) as integer
                             
  Declare Sub ChangeScreenResolution (byval wwidth as integer, byval hheight as integer, byval bitsPerPixel as integer)


  'OpenGL Draw sub
  Declare Sub glDraw()
 
  'OpenGL Init
  Declare Sub glInit()

  'Application name
  Const szAppName = "FreeBasic - OpenGL Framework"
 
  'app window handle
  Dim Shared hWndWin as HWND
 
  'app hdc
  Dim Shared hdcWin as HDC
 
  ' our window class
  Dim Shared wndWc as WNDCLASSEX
 
  'Mouse control
  Dim Shared mouseX as integer
  Dim Shared mouseY as integer
  Dim Shared leftbutton as integer
  Dim Shared rightbutton as integer 
 
  'Window resolution
  const XRES = 800
  const YRES = 600



' ----------------------------------
' name: WndProc
' desc: Processes windows messages
'
' ----------------------------------
function WndProc ( byval hWnd as HWND, _
                   byval message as UINT, _
                   byval wParam as WPARAM, _
                   byval lParam as LPARAM ) as LRESULT
         
    '
    ' Process messages
    '
    select case(message) 
   
case WM_POWERBROADCAST
function = BROADCAST_QUERY_DENY

case WM_SYSCOMMAND
select case(message and &hFFF0)

case SC_SCREENSAVE
function = 0
case SC_MONITORPOWER
function = 0 'disable screen/power savers
case else
function = DefWindowProc( hWnd, message, wParam, lParam )
end select
   
'
' Key pressed
'
case WM_KEYDOWN
if( lobyte( wParam ) = 27 ) then
PostMessage( hWnd, WM_CLOSE, 0, 0 )
end if
           
        'Left mouse button pressed
        case WM_LBUTTONDOWN
leftbutton = 1

        'Left mouse button up
        case WM_LBUTTONUP
leftbutton = 0

        'Right mouse button pressed
        case WM_RBUTTONDOWN
rightbutton = 1

        'Right mouse button up
        case WM_RBUTTONUP
rightbutton = 0

        'Check for mouse movement
        case WM_MOUSEMOVE
            'Detect mouse position
            mouseX = LOWORD(lParam)
            mouseY = HIWORD(lParam)

        '
        ' Window was closed
        '
    case WM_DESTROY
            PostQuitMessage( 0 )
            exit function
           
    end select
   
    '
    ' Message doesn't concern us, send it to the default handler
    ' and get result
    '
    function = DefWindowProc( hWnd, message, wParam, lParam )   
   
end function

' ---------------------------------------
' name: WinMain
' desc: A win2 gui program entry point
'
' ---------------------------------------
function WinMain ( byval hInstance as HINSTANCE, _
                   byval hPrevInstance as HINSTANCE, _
                   szCmdLine as string, _
                   byval iCmdShow as integer ) as integer   
     
    dim wMsg as MSG
       
    ' get module instance
    hInstance = GetModuleHandle(NULL)
   
    'Create our skinned window   
    if MakeWindow(XRES,YRES,hInstance) = 0 then
        MessageBox(NULL, "Failed to create window", szAppName, MB_ICONERROR )
        return -1
    end if
   
    ShowWindow( hWndWin, iCmdShow )
    UpdateWindow( hWndWin )
       
    'Get window device context
    hdcWin = GetDC(hWndWin)
   
    'Initialize OGL window
    dim pfd as PIXELFORMATDESCRIPTOR
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR)
    pfd.nVersion = 1
    pfd.dwFlags = PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER
    pfd.dwLayerMask = PFD_MAIN_PLANE
    pfd.iPixelType = PFD_TYPE_RGBA
    pfd.cColorBits = 32
    pfd.cDepthBits = 32
    SetPixelFormat ( hdcWin, ChoosePixelFormat ( hdcWin, @pfd) , @pfd )
    wglMakeCurrent ( hdcWin, wglCreateContext(hdcWin) )
   
    'Call opengl init
    glInit()
   
    '
    ' Process windows messages
    '
    while(wMsg.message <> WM_QUIT)
       
        if (PeekMessage (@wMsg,NULL,0,0,PM_REMOVE) <> 0) then
            DispatchMessage(@wMsg)
        else
            glDraw()
            SwapBuffers (hdcWin)
        end if
   
    wend
   
'Unregister window class
    UnregisterClass(wndWc.lpszClassName, hInstance)
   
    ' Ends a process and all its threads.
    ExitProcess(0)

end function

'Change The Screen Resolution
sub ChangeScreenResolution (byval wwidth as integer, byval hheight as integer, byval bitsPerPixel as integer)
dim dmScreenSettings as DEVMODE
ZeroMemory(@dmScreenSettings, sizeof (DEVMODE))
dmScreenSettings.dmSize = sizeof(DEVMODE)
dmScreenSettings.dmPelsWidth = wwidth
dmScreenSettings.dmPelsHeight = hheight
dmScreenSettings.dmBitsPerPel = bitsPerPixel
dmScreenSettings.dmFields = DM_BITSPERPEL or DM_PELSWIDTH or DM_PELSHEIGHT

    if (ChangeDisplaySettings (cast(LPDEVMODE,@dmScreenSettings), CDS_FULLSCREEN) <> DISP_CHANGE_SUCCESSFUL) then
        MessageBox(NULL,"Change Display Error!", szAppName,MB_OK or MB_ICONEXCLAMATION)
end -1
end if
   
end sub
' ------------------------------------------------------------------------
' Window creation function.
' ------------------------------------------------------------------------
Function MakeWindow(byval iWidth as integer, _
                              byval iHeight as integer, _
                              byval hInstance as HINSTANCE) as integer
 
  function = 0
   
  dim windowStyle as DWORD
  dim windowExtendedStyle as DWORD
  dim rc as RECT

  ' ---------------------------------------------------------
  ' fill window class members
  ' ---------------------------------------------------------
  ZeroMemory (@wndWc, sizeof(WNDCLASSEX))
  wndWc.cbSize        = sizeof (WNDCLASSEX)
  wndWc.style         = CS_HREDRAW or CS_VREDRAW or CS_OWNDC
  wndWc.lpfnWndProc   = Cast(WNDPROC, @WndProc)
  wndWc.hInstance     = hInstance
  wndWc.hbrBackground = GetStockObject(BLACK_BRUSH)
  wndWc.hCursor       = LoadCursor(NULL, IDC_ARROW)
  wndWc.lpszClassName = strptr( szAppName )
  wndWc.hIcon         = LoadIcon(hInstance, IDI_APPLICATION) ' Load our normal icon

  ' register class
  if( RegisterClassEx( @wndWc ) = FALSE ) then
      MessageBox(NULL, "Failed to register Window Class!", szAppName, MB_ICONERROR )
      exit function
  end if
   
  rc.left   = 0
  rc.top    = 0
  rc.right  = XRES
  rc.bottom = YRES
 
  'Setup video mode window - Windowed / Fullscreen
  dim winmode as integer
  if( MessageBox(NULL,"Full Screen ?", "Screen Mode",MB_YESNO or MB_ICONQUESTION) = IDYES ) then
    winmode = 0
  else
    winmode = 1
  end if

  if (winmode = 0) then
    windowStyle = WS_POPUP or WS_VISIBLE
    windowExtendedStyle = WS_EX_TOPMOST
    ChangeScreenResolution (XRES, YRES, 32)
  else
    windowStyle =  WS_SYSMENU or WS_POPUP or WS_VISIBLE or WS_MINIMIZEBOX
    windowExtendedStyle = WS_EX_APPWINDOW
    'Center window
    rc.left = (GetSystemMetrics(SM_CXSCREEN) - XRES) / 2
    rc.top =  (GetSystemMetrics(SM_CYSCREEN) - YRES) / 2
  end if
 
  ' create the window
  hWndWin = CreateWindowEx(windowExtendedStyle,   _
wndWc.lpszClassName, _
                     szAppName,           _
windowStyle,         _
                             rc.left,             _
rc.top,              _
                             rc.right,            _
         rc.bottom,           _
                             HWND_DESKTOP, 0,     _
GetModuleHandle(NULL), 0)
 
  ShowCursor(false)
 
  ' return result
  if (hWndWin) then
      return 1
  else
      return 0
  end if

end function


sub glInit()
    glClearColor(0.2f, 0.3f, 0.5f, 1.0f)
   
end sub

sub glDraw()
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)

    'Debug
    locate 1,1:print "Mouse X pos: ";mouseX
    locate 2,1:print "Mouse Y pos: ";mouseY
    locate 3,1:print "Mouse Left Button: ";leftbutton
    locate 4,1:print "Mouse Rigth Button: ";rightbutton
   
end sub

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #21 on: August 09, 2009 »
This seems to be working fine now Rbz :)

Thanks mate.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Re: Why wont GLU work with this framework?
« Reply #22 on: August 09, 2009 »
How small does the exe come out from your code example with the effect shockwave ?
I am betting its abit smaller than bmax but bmax handles all the windows setup for me and is alittle bloated for some of it, tho it is possible to code the windows stuff yourself probably making the code even smaller.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #23 on: August 09, 2009 »
Just a blank screen is 5kb crunched.

Adding the effect wont increase the size much at all, the exe already has crt.bi, windows.by, gl, glu and glext
So in all honesty its much smaller than Bmax.

Don't let that put you off using Bmax though, it's perfectly fine.

I intend to use this framework for 40kb intros though... My demo for Sundown will be a 40kb demo.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Re: Why wont GLU work with this framework?
« Reply #24 on: August 09, 2009 »
I am happy messing about in bmax but it's interesting to see how small your exe's are, I havent dug to deep into it but I suspect bmax's could be made smaller by rolling my own graphics driver but then i would likely be just as well to go to C++ which I am tempted to try and make some stuff in purely to see if I can.

Freebasic is a very nice setup for free, I think I still have version 0.15 on my harddrive with fbide but I havent looked at it in a while, i like using the oop side of bamx alot now rather than the Procedural way I use to code. Doesnt mean I arent tempted by the tiny exe's  ;D

[edit] Well smallest exe I got crunched is 36kb but I do get a basic font and a drawing routine for that as well. still bit big tho lol

« Last Edit: August 09, 2009 by TinDragon »

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Re: Why wont GLU work with this framework?
« Reply #25 on: August 10, 2009 »
Had a play about in VC++ 2008 express and can get using just upx to compress, a 7kb exe that asks for fullscreen or not and then opens a blank gl context and clears/flips it, with a simple fps display in the title bar of the window tho it doesnt seem perfect fullscreen yet, dont think thats to bad tbh but it does show just how good freebasic is at build small exe's.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Why wont GLU work with this framework?
« Reply #26 on: August 10, 2009 »
You can get a job with VC++ though so I'd stick with that if I were you :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Why wont GLU work with this framework?
« Reply #27 on: August 11, 2009 »
If you remove the default libraries you can get window+opengl+res switch down to a few hundred bytes, crinkled.

Jim
Challenge Trophies Won: