After much headache I finally tracked down most of the problems.
For anyone interested, I've updated the script and 2 include files below (be sure to grab the newest include files below or you will be missing some needed constants if you want to test it).
It is now a working example of OpenGL in AHK. The code is a mess and all. If I ever do anything useful with it, I'll clean it up and put it in the Scripts section.
To test it you'll need:
GL.ahk
WINDERS.ahk
Here is the script:
; This script is based off of Nehe's First Polygon Tutrorial, which can be found at ; http://nehe.gamedev.net ;///////////////////////////////////////////////////////////////////////////////////////////// #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. ;// SendMode Input ; Recommended for new scripts due to its superior speed and reliability. ;// SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory. ;// ;///////////////////////////////////////////////////////////////////////////////////////////// #include WINDERS.ahk #include GL.ahk OnExit, Exit hOpenGL := DllCall("LoadLibrary", Str, "opengl32.dll") hGlu32 := DllCall("LoadLibrary", Str, "glu32.dll") hGdi32 := DllCall("LoadLibrary", Str, "gdi32.dll") hInstance := 0 hWnd := 0 ATOM := 0 hDC := 0 hRC := 0 F1 := 0 active := 1 fullscreen := 1 lpszClassName := "OpenGL" WinMain() ; ============================================================================================== ReSizeGLScene() { Global If height = 0 ;Prevent divide by 0 height := 1 DllCall("opengl32.dll\glViewport", Int, 0, Int, 0, Int, width, Int, height) DllCall("opengl32.dll\glMatrixMode", UInt, GL_PROJECTION) DllCall("opengl32.dll\glLoadIdentity") ; Calculate aspect ratio of the window DllCall("glu32.dll\gluPerspective", Double, 45.0, Double, width/height, Double, 0.1, Double, 100.0) DllCall("opengl32.dll\glMatrixMode", UInt, GL_MODELVIEW) DllCall("opengl32.dll\glLoadIdentity") Return } InitGL() { Global DllCall("opengl32.dll\glShadeModel", UInt, GL_SMOOTH) DllCall("opengl32.dll\glClearColor", Float, 0.0, Float, 0.0, Float, 0.0, Float, 0.5) DllCall("opengl32.dll\glClearDepth", Double, 1.0) DllCall("opengl32.dll\glEnable", UInt, GL_DEPTH_TEST) DllCall("opengl32.dll\glDepthFunc", UInt, GL_LEQUAL) DllCall("opengl32.dll\glHint", UInt, GL_PERSPECTIVE_CORRECTION_HINT, UInt, GL_NICEST) Return 1 } DrawGLScene() { Global DllCall("opengl32.dll\glClear", Int, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) DllCall("opengl32.dll\glLoadIdentity") DllCall("opengl32.dll\glTranslatef", Float, -1.5, Float, 0.0, Float, -6.0) DllCall("opengl32.dll\glBegin", UInt, GL_TRIANGLES) DllCall("opengl32.dll\glVertex3f", Float, 0.0, Float, 1.0, Float, 0.0) DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 0.0) DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 0.0) DllCall("opengl32.dll\glEnd") DllCall("opengl32.dll\glTranslatef", Float, 3.0, Float, 0.0, Float, 0.0) DllCall("opengl32.dll\glBegin", UInt, GL_QUADS) DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, 1.0, Float, 0.0) DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, 1.0, Float, 0.0) DllCall("opengl32.dll\glVertex3f", Float, 1.0, Float, -1.0, Float, 0.0) DllCall("opengl32.dll\glVertex3f", Float, -1.0, Float, -1.0, Float, 0.0) DllCall("opengl32.dll\glEnd") DllCall("gdi32.dll\SwapBuffers", UInt, hDC) Return 1 } KillGLWindow() { Global If fullscreen { DllCall("ChangeDisplaySettings", UInt, 0, UInt, 0) DllCall("ShowCursor", Int, 1) } If hRC { If !DllCall("opengl32.dll\wglMakeCurrent", UInt, 0, UInt, 0) MsgBox, Release of DC and RC failed.`nError: %A_LastError% If !DllCall("opengl32.dll\wglDeleteContext", UInt, hRC) MsgBox, Release rendering context failed.`nError: %A_LastError% } If hDC { If !DllCall("ReleaseDC", UInt, hWnd, UInt, hDC) MsgBox, Release device context failed.`nError: %A_LastError% } If hWnd { If !DllCall("DestroyWindow", UInt, hWnd) MsgBox, Could not release hWnd.`nError: %A_LastError% } If ATOM { If !DllCall("UnregisterClass", UInt, &lpszClassName, UInt, hInstance) MsgBox, Could not unregister class.`nError: %A_LastError% } hInstance := 0 hWnd := 0 ATOM := 0 hDC := 0 hRC := 0 } CreateGLWindow(title, width, height, bits) { Global VarSetCapacity(wc, 40, 0) VarSetCapacity(WindowRect, 16, 0) VarSetCapacity(pfd, 40, 0) NumPut(0, WindowRect, 0, "Int") NumPut(0, WindowRect, 4, "Int") NumPut(width, WindowRect, 8, "Int") NumPut(height, WindowRect, 12, "Int") Style := CS_HREDRAW | CS_VREDRAW | CS_OWNDC lpfnWndProc := RegisterCallback("WndProc", "", 4) cbClsExtra := 0 cbWndExtra := 0 hInstance := DllCall("GetModuleHandle", UInt, 0) hbrBackground := 0 hCursor := DllCall("LoadCursor", UInt, 0, UInt, IDC_ARROW) hIcon := DllCall("LoadIcon", UInt, 0, UInt, IDI_APPLICATION) lpszMenuName := 0 NumPut(Style, wc, 0, "UInt") NumPut(lpfnWndProc, wc, 4, "UInt") NumPut(cbClsExtra, wc, 8, "UInt") NumPut(cbWndExtra, wc, 12, "UInt") NumPut(hInstance, wc, 16, "UInt") NumPut(hIcon, wc, 20, "UInt") NumPut(hCursor, wc, 24, "UInt") NumPut(hbrBackground, wc, 28, "UInt") NumPut(&lpszMenuName, wc, 32, "UInt") NumPut(&lpszClassName, wc, 36, "UInt") ATOM := DllCall("RegisterClass", UInt, &wc) If !ATOM { MsgBox, Failed to register class.`nError: %A_LastError% ExitApp } If fullscreen { ; DEVMODE size: 156 ANSI, 220 UNICODE VarSetCapacity(dmScreenSettings, 156, 0) dmFields := DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT NumPut(156, dmScreenSettings, 0, "UShort") Result := DllCall("EnumDisplaySettings", UInt, 0, UInt, 0, UInt, &dmScreenSettings) NumPut(width, dmScreenSettings, 108, "UInt") NumPut(height, dmScreenSettings, 112, "UInt") NumPut(bits, dmScreenSettings, 104, "UInt") ;bits NumPut(dmFields, dmScreenSettings, 40, "UInt") Result := DllCall("ChangeDisplaySettings", UInt, &dmScreenSettings, UInt, CDS_FULLSCREEN) If Result != %DISP_CHANGE_SUCCESSFUL% { MsgBox,4, Error, The requested fullscreen mode is not supported by`nyour video card. Use windowed mode instead? IfMsgBox Yes fullscreen := 0 Else { ; Let the user know the program is closing MsgBox, The program will now close. ExitApp } } } If fullscreen { dwExStyle := WS_EX_APPWINDOW dwStyle := WS_POPUP DllCall("ShowCursor", Int, 0) } Else { dwExStyle := WS_EX_APPWINDOW | WS_EX_WINDOWEDGE dwStyle := WS_OVERLAPPEDWINDOW } DllCall("AdjustWindowRectEx", UInt, &WindowRect, UInt, dwStyle, UInt, 0, UInt, dwExStyle) hWnd := DllCall("CreateWindowEx", UInt, dwExStyle , UInt, &lpszClassName , "Str", title , UInt, dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN , Int, 0 , Int, 0 , Int, NumGet(WindowRect, 8, "Int")-NumGet(WindowRect, 0, "Int") , Int, NumGet(WindowRect, 12, "Int")-NumGet(WindowRect, 4, "Int") , UInt, 0 , UInt, 0 , UInt, hInstance , UInt, 0 , UInt) If !hWnd { MsgBox, Window creation error.`nError: %A_LastError% ExitApp } dwFlags := PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER NumPut(40, pfd, 0, "UShort") NumPut(1, pfd, 2, "UShort") NumPut(dwFlags, pfd, 4, "UInt") NumPut(PFD_TYPE_RGBA, pfd, 8, "UChar") NumPut(bits, pfd, 9, "UChar") NumPut(16, pfd, 23, "UChar") NumPut(PFD_MAIN_PLANE, pfd, 26, "UChar") hDC := DllCall("GetDC", UInt, hWnd) If !hDC { MsgBox, Can't create GL device context.`nError: %A_LastError% ExitApp } PixelFormat := DllCall("gdi32.dll\ChoosePixelFormat", UInt, hDC, UInt, &pfd) If !PixelFormat { MsgBox, Can't find a suitable pixel format.`nError: %A_LastError% ExitApp } If !DllCall("gdi32.dll\SetPixelFormat", UInt, hDC, UInt, PixelFormat, UInt, &pfd) { MsgBox, Can't set the pixel format.`nError: %A_LastError% ExitApp } hRC := DllCall("opengl32.dll\wglCreateContext", UInt, hDC) If !hRC { MsgBox, Can't create GL rendering context.`nError: %A_LastError% ExitApp } If !DllCall("opengl32.dll\wglMakeCurrent", UInt, hDC, UInt, hRC) { MsgBox, Can't activate the GL rendering context.`nError: %A_LastError% ExitApp } DllCall("ShowWindow", UInt, hWnd, Int, SW_SHOW) DllCall("SetForegroundWindow", UInt, hWnd) DllCall("SetFocus", UInt, hWnd) ReSizeGLScene() If !InitGL() { MsgBox, Initialization failed. ExitApp } Return 1 } WndProc(hwnd, uMsg, wParam, lParam) { Global If uMsg = %WM_ACTIVATE% { If !(wParam >> 16) active := 1 Else active := 0 Return 0 } If uMsg = %WM_SYSCOMMAND% { If wParam = %SC_SCREENSAVE% Return 0 If wParam = %SC_MONITORPOWER% Return 0 } If uMsg = %WM_CLOSE% ExitApp If uMsg = %WM_SIZE% { width := lParam & 0x0ffff height := lParam >> 16 ReSizeGLScene() Return 0 } Return DllCall("DefWindowProc", UInt, hwnd, UInt, uMsg, UInt, wParam, UInt, lParam) } WinMain() { Global MsgBox, 4, Start Fullscreen?, Would you like to run in fullscreen mode? IfMsgBox No fullscreen := 0 CreateGLWindow("Nehe's First Polygon Tutorial", 640, 480, 16) Loop { If F1 { KillGLWindow() F1 := 0 fullscreen := !fullscreen CreateGLWindow("Nehe's First Polygon Tutorial", 640, 480, 16) } If active If !DrawGLScene() Break } ExitApp } ; ============================================================================================== Esc::ExitApp F1::F1++ Exit: KillGLWindow() If hOpenGL DllCall("FreeLibrary", UInt, hOpenGL) If hGlu32 DllCall("FreeLibrary", UInt, hGlu32) If hGdi32 DllCall("FreeLibrary", UInt, hGdi32) ExitApp
F1 toggles fullscreen, Escape to exit.