summaryrefslogtreecommitdiff
path: root/beos/main.cpp
blob: 401d4e6e820c4b3335590ed33af81cf7305a2cb5 (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
//
// LeoCAD for BeOS
//

#include <stdlib.h>
#include <be/kernel/OS.h>
#include "opengl.h"
#include "project.h"
#include "globals.h"

// Flag to tell whether or not the Be application is active or not
int BeAppActive = 0;
static thread_id AppThread = 0;

static int32 RunThread (void *data)
{
	SDL_RunThread (data);
	return 0;
}

// Initialize the Be Application, if it's not already started
int InitBeApp ()
{
	// Create the BApplication that handles appserver interaction
	if (BeAppActive <= 0)
	{
		// Create the thread and go!
		AppThread = spawn_thread (RunThread, "LeoCAD", B_NORMAL_PRIORITY, NULL);
		if ((AppThread == B_NO_MORE_THREADS) ||
			(AppThread == B_NO_MEMORY))
		{
			printf ("Not enough resources to create thread\n");
			return -1;
		}
		resume_thread (AppThread);

		do
		{
			snooze (10);
		} while ((be_app == NULL) || be_app->IsLaunching ());

		// Mark the application active
		BeAppActive = 0;
	}

	// Increment the application reference count
	BeAppActive++;

	// The app is running, and we're ready to go
	return 0;
}

// Quit the Be Application, if there's nothing left to do
void QuitBeApp ()
{
	// Decrement the application reference count
	BeAppActive--;

	// If the reference count reached zero, clean up the app
	if (BeAppActive == 0)
	{
		if (AppThread != 0)
		{
			if (be_app != NULL)
			{
				// Not tested
				be_app->PostMessage(B_QUIT_REQUESTED);
			}

			status_t the_status;
			wait_for_thread (AppThread, &the_status);
			AppThread = 0;
		}
		// be_app should now be NULL since be_app has quit
	}
}
        
int main (int argc, char** argv)
{
  char* libgl = NULL;
  int i, j, k;

  // Parse and remove system arguments
  for (i = 1; i < argc; i++)
  {
    char* param = argv[i];

    if (param[0] == '-' && param[1] == '-')
    {
      param += 2;

      if ((strcmp (param, "libgl") == 0) && (i != argc))
      {
        libgl = argv[i+1];
        argv[i] = argv[i+1] = NULL;
        i++;
      }
    }
  }
  
  for (i = 1; i < argc; i++)
  {
    for (k = i; k < argc; k++)
      if (argv[k] != NULL)
        break;

    if (k > i)
    {
      k -= i;
      for (j = i + k; j < argc; j++)
        argv[j-k] = argv[j];
      argc -= k;
    }
  }
  
  if (!GL_Initialize (libgl))
    return 1;






  project = new Project();

  char* path;
  path = getenv("LEOCAD_LIB");
  if (path == NULL)
    path = "/boot/home/leocad/";

  if (project->Initialize(argc, argv, path) == false)
  {
    delete project;
    GL_Shutdown ();
    return 1;
  }

  delete project;
  GL_Shutdown ();

  return 0;
}