summaryrefslogtreecommitdiff
path: root/macosx/main.cpp
blob: a21072da96c7bad516fed7a88944c7fae3ec45d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "glwindow.h"
#include "project.h"
#include "view.h"
#include "globals.h"
#include "opengl.h"
#include "mainwnd.h"
#include "lc_application.h"

#include <Carbon.h>

View* view;

#include <math.h>
#include <stdio.h>
#include <string.h>

#include <OpenGL/glext.h>

Boolean SetUp();
pascal void IdleTimer (EventLoopTimerRef inTimer, void* userData);
EventLoopTimerUPP GetTimerUPP();
void CleanUp();

EventLoopTimerRef gTimer = NULL;

Boolean SetUp()
{
	InitCursor();
  DrawMenuBar();

  InstallEventLoopTimer(GetCurrentEventLoop(), 0, 0.01, GetTimerUPP(), 0, &gTimer);

	return true;
}

pascal void IdleTimer(EventLoopTimerRef inTimer, void* userData)
{
//	view->Redraw();
}

EventLoopTimerUPP GetTimerUPP()
{
  static EventLoopTimerUPP  sTimerUPP = NULL;
  
  if (sTimerUPP == NULL)
    sTimerUPP = NewEventLoopTimerUPP (IdleTimer);
  
  return sTimerUPP;
}

void CleanUp()
{
  RemoveEventLoopTimer(gTimer);
  gTimer = NULL;  
}

// --------------------------------------------------------------------------

pascal OSStatus lcAppEventHandler(EventHandlerCallRef inHandlerCallRef, EventRef Event, void* UserData)
{
	return eventNotHandledErr;
}

DEFINE_ONE_SHOT_HANDLER_GETTER(lcAppEventHandler);

int main(int argc, char* argv[])
{
	UInt32 response;

	// Check the Mac OS version.
	if ((Gestalt (gestaltSystemVersion, (SInt32 *) &response) == noErr) && (response < 0x01020))
	{
		printf("Must have Mac OS v10.2 or later.\n");
		return 1;
	}

	// Initialize the application.
	g_App = new lcApplication();
	main_window = new MainWnd();

	// Get the pieces library path (bundle/Contents/Resources/).
	CFBundleRef Bundle = CFBundleGetMainBundle();
	if (Bundle == NULL)
		return 1;
	CFURLRef BundleURL = CFBundleCopyResourcesDirectoryURL(Bundle);
	if (BundleURL == NULL)
		return 1;

	UInt8 ResourcesPath[LC_MAXPATH];
	CFURLGetFileSystemRepresentation(BundleURL, true, ResourcesPath, LC_MAXPATH);

	if (!g_App->Initialize(argc, argv, (char*)ResourcesPath))
		return 1;

	// Install the application event handler.
	EventTypeSpec AppEventList[] = { { kEventClassCommand, kEventProcessCommand } };
	InstallApplicationEventHandler(GetlcAppEventHandlerUPP(), GetEventTypeCount(AppEventList), AppEventList, NULL, NULL);

	IBNibRef nib = NULL;
	CreateNibReference(CFSTR("main"), &nib);

	WindowRef win = NULL;
	CreateWindowFromNib(nib, CFSTR("MainWindow"), &win);
	ShowWindow(win);

	SetMenuBarFromNib(nib, CFSTR("MainMenu"));

	if (SetUp())
	{
		view = new View(lcGetActiveProject(), NULL);

		HIViewRef userPane; 
		static const HIViewID userPaneID = { 'Moof', 127 };
		HIViewFindByID(HIViewGetRoot(win), userPaneID, &userPane);

		if (!view->Create(userPane))
		{
			delete main_window;
			main_window = NULL;
			delete view;
			g_App->Shutdown();
			delete g_App;
			return 1;
		}

		RunApplicationEventLoop();

		view->DestroyContext();
		DisposeWindow(win);
	}

	CleanUp();

	delete main_window;
	main_window = NULL;
	delete view;
	g_App->Shutdown();
	delete g_App;

	DisposeNibReference(nib);

	return 0;
}