Here's a new one, added mouse function and hopefully got window mode + mouse event fixed.
'---------------------------------------------------------------------------------
'
' 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