I've patched together this extremely messy app by looking through a number of tutorials. The implementation is embedded in the 'gui', which is horrible practice, I know.. but right now it's simply to learn the commands.
Import maxgui.drivers
SuperStrict
SetGraphicsDriver(GLGraphicsDriver())
Local style:Int = WINDOW_TITLEBAR | WINDOW_CLIENTCOORDS | WINDOW_RESIZABLE | WINDOW_STATUS
'DebugLog "Stye Value: "+style
Global MyWindow:TGadget=CreateWindow("OpenGL in a MaxGUI Canvas Gadget", 400,400,640,480,Null,style)
Global MyCanvas:TGadget=CreateCanvas(0,0,GadgetWidth(MyWindow)-200,GadgetHeight(MyWindow),MyWindow)
Local GraphicContext:TGraphics=CanvasGraphics(MyCanvas)
SetGraphics GraphicContext
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0,GadgetWidth(mycanvas),GadgetHeight(mycanvas),0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glClearColor(0 , 0 , 0 , 1)
Global Vertices:Float[] = [-20.0, -20.0, 0.0, -20.0, 20.0, 0.0, 20.0, 20.0, 0.0, 20.0, -20.0, 0.0]
Global Colors:Float[] = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0]
Global x:Int=0
Global y:Int=0
ActivateGadget(MyCanvas)
AddHook EmitEventHook , HookManager
Repeat
WaitEvent()
Forever
Function HookManager:Object(id:Int , data:Object , context:Object)
Local event:TEvent = TEvent(data)
If event = Null Then Return Null
Select event.source
Case MyCanvas
Select event.id
Case EVENT_MOUSEENTER
HideMouse
Case EVENT_MOUSELEAVE
ShowMouse
Case EVENT_MOUSEMOVE
x=event.x
y=event.y
RedrawGadget(MyCanvas)
Case EVENT_GADGETPAINT
drawcanvas()
End Select
Case MyWindow
Select event.id
Case EVENT_WINDOWCLOSE
End
Case EVENT_WINDOWSIZE
SetGadgetShape(MyCanvas,0,0,event.x-200,event.y )
End Select
End Select
End Function
Function drawcanvas()
SetGraphics CanvasGraphics (MyCanvas)
glLoadIdentity()
glClear(GL_COLOR_BUFFER_BIT)
glTranslatef(x,y,0)
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_COLOR_ARRAY)
glColorPointer(3, GL_FLOAT, 0, Colors)
glVertexPointer(3, GL_FLOAT, 0, Vertices)
glDrawArrays(GL_QUADS, 0, 4)
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_COLOR_ARRAY)
Flip
End Function
It works, but for all I know I'm doing things more complex than they need to be. what modifications (other than making it clean and object-oriented) would you present to the above methods? Or do I have it somewhat right?
Lastly, I also noticed that if there is no "waitevent()" between the repeat and forever, then my program freezes upon execution. Can anyone tell me why that is, and how to live without it?