wxMax, Mac and BRL.GLGraphics

BlitzMax Modules Forums/Brucey's Modules/wxMax, Mac and BRL.GLGraphics

Well, I've managed to get a "normal" GLGraphics window working *with* wxMax on OS X.

This is interesting because it's mixing Carbon (wxMax) and Cocoa (BRL.GLGraphics) APIs in the same app.

The main reason I was playing around with this was to try to get a full-screen graphics mode working from a wxMax application - that's rather than a full-screen Window with a GLCanvas in it.

I'm having problems with full-screen still, but at least as a proof of concept for mixing APIs the basics are working well so far, it seems.

Here are the parts for playing :

graphicstest.bmx
SuperStrict

Framework wx.wxApp
Import wx.wxFrame
Import wx.wxButton
Import wx.wxPanel
Import wx.wxCheckBox

Import BRL.GLMax2D

?macos
Import "cocoa_glue.bmx"
?

New MyApp.run()


Type MyApp Extends wxAppMain
	Global frame:wxFrame
	Global shouldExit:Int = False
	
	Global showingGraphics:Int = False
	Global fullScreenGaphics:Int = False
	
	Method OnInit:Int()

		frame = MyFrame(New MyFrame.Create(,,"Groovy Graphics test", 100, 100))
		SetTopWindow(frame)
		frame.show()
	
		'# Connect exit event
		frame.Connect(, wxEVT_CLOSE, OnClose)

		Return True
	End Method
	
	Method MainLoop:Int() 

		While True
			While Not Pending() And ProcessIdle() ; Wend
			While Pending()
				If Not Dispatch() Then
					shouldExit = True
					Exit
				End If
			Wend
			If shouldExit Then
				While pending() 
					dispatch() 
				Wend
				Return 0
			End If

			If showingGraphics Then
DebugLog "graphics.... " + MouseX() + " : " + MouseY()
				If KeyDown(KEY_ESCAPE) Then
					DisableGraphics()
					Continue
				End If

				SetClsColor(255, 255, 255)

				Cls
				
				SetColor(0, 0, 0)
				
				DrawLine 50, 50, MouseX(), MouseY()
	
				Flip
			
			End If

		Wend
	End Method
	
	Function OnClose(event:wxEvent)
		MyApp.DisableGraphics()
		MyApp.shouldExit = True
	End Function
	
	Function EnableGraphics()
	
		If Not showingGraphics Then
			
			?macos
			initCocoa()
			?
			
			frame.Hide()

			If fullScreenGaphics Then
				Graphics 800, 600, 16
			Else
				Graphics 800, 600, 0
			End If
			
			SetBlend alphablend
			
			showingGraphics = True
		End If
	
	End Function
	
	Function DisableGraphics()
	
		If showingGraphics Then
		
			EndGraphics()
			
			frame.Show()
		
			showingGraphics = False
		End If
	
	End Function

End Type


Type MyFrame Extends wxFrame

	Const ID_BUTTON:Int = 1000
	Const ID_CBOX:Int = 1001

	Field btn:wxButton
	Field chk:wxCheckBox

	Method OnInit()
	
		' create a menu bar
	
		Local fileMenu:wxMenu = wxMenu.CreateMenu()
		
		' the "About" item should be in the help menu
		Local helpMenu:wxMenu = wxMenu.CreateMenu()
		
		helpMenu.Append(wxID_ABOUT, "&About...~tF1", "Show about dialog")
		
		fileMenu.Append(wxID_EXIT, "&Quit~tAlt-Q", "Quit this program")
		
		' now append the freshly created menu to the menu bar...
		Local menuBar:wxMenuBar = wxMenuBar.CreateMenuBar()
		menuBar.Append(fileMenu, "&File")
		menuBar.Append(helpMenu, "&Help")

		SetMenuBar(menuBar)
		
		' create a status bar just for fun
		CreateStatusBar(2)
		SetStatusText("Welcome to wxWidgets!")
		
		Local panel:wxPanel = New wxPanel.Create(Self)
		
		btn = New wxButton.Create(panel, ID_BUTTON, "Go!", 50, 50)
		chk = New wxCheckBox.Create(panel, ID_CBOX, "FullScreen", 50, 80)
		
		Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, OnQuit)
		Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, OnAbout)
		Connect(ID_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED, OnButton)
		Connect(ID_CBOX, wxEVT_COMMAND_CHECKBOX_CLICKED, OnCheckBox)
		
	End Method

	Function OnQuit(event:wxEvent)
		' true is to force the frame to close
		wxWindow(event.parent).Close(True)
	End Function
	
	Function OnAbout(event:wxEvent)
		
		wxMessageBox("Groovy Graphics test.", ..
                 "About Groovy Graphics test", ..
                 wxOK | wxICON_INFORMATION, wxWindow(event.parent))
		
	End Function
	
	Function OnButton(evt:wxEvent)
	
		MyApp.EnableGraphics()
	
	End Function
	
	Function OnCheckBox(evt:wxEvent)
		MyApp.fullScreenGaphics = wxCommandEvent(evt).IsChecked()
	End Function

	
End Type


cocoa_glue.bmx
SuperStrict

Import "cocoa_glue.m"

Extern

	Function initCocoa()

End Extern


cocoa_glue.m
#import <Cocoa/Cocoa.h>
#include <brl.mod/blitz.mod/blitz.h>

static NSAutoreleasePool * cocoaPool;

void initCocoa() {

	if (! cocoaPool) {
		cocoaPool = [[NSAutoreleasePool alloc] init];
		NSApplicationLoad();
	}

}


If you try the full-screen mode you'll find that it doesn't process any events (or at least, rarely!!). To get out of it, use cmd-Q to quit the app.
The windowed mode works well, and you can close it by pressing Escape.

Notice that I'm using the wxAppMain type instead of the usual wxApp. This provides you with direct access to the "main loop", rather than firing events to update your graphics window. This should, in theory, give you more control over everything.

I'm not sure why there are no events processed after the mode change. I can only assume that something else is going on underneath that's not passing them thru to where I need them.

If anyone has any bright ideas....

;-)