-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.cpp
More file actions
359 lines (281 loc) · 7.6 KB
/
main.cpp
File metadata and controls
359 lines (281 loc) · 7.6 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include "SingleWorldConfiguration.h"
#include "World.h"
#include "luaInterface.h"
#include "luaCallbacks.h"
#include "lispInterface.h"
#include "lispCallbacks.h"
#include "forthInterface.h"
#include "forthCallbacks.h"
#include "ioInterface.h"
#include "ioCallbacks.h"
#include "AI_NeuralNetworkSystem.h"
#ifdef __PRAXIS_WINDOWS__
#include "fmod.h"
#include "fmod_errors.h" /* optional */
CRITICAL_SECTION g_cs;
#endif
#ifdef __PRAXIS_LINUX__
#include <signal.h>
struct sigaction new_act, old_act;
void handle_sigint(int ignored)
{
std::cout << "Praxis:SIGINT" << std::endl;
sigaction(SIGINT, &new_act, NULL);
}
#include <unistd.h>
#include <pthread.h>
pthread_t g_inputthread;
pthread_mutex_t g_inputthreadmutex;
std::string g_sInputThreadString;
bool g_bInputThreadNewString = false;
void * inputthread_function( void *ptr )
{
printf("\nHello from the input thread.\n\n");
char mystring [65536];
//std::string sCode;
while(true)
{
int n = read(STDIN_FILENO, mystring, 65535);
if ( n > 0 )
{
// n bytes read
mystring[n] = '\0';
//sCode = sCode + mystring;
//puts(sCode.c_str());
pthread_mutex_lock (&g_inputthreadmutex);
g_sInputThreadString = mystring;
g_bInputThreadNewString = true;
pthread_mutex_unlock (&g_inputthreadmutex);
}
}
}
#endif
// void MoveConsTest();
World * g_pWorld;
#ifdef __PRAXIS_WINDOWS__
FSOUND_STREAM * g_pMp3Stream = 0;
int g_nMp3Channel = 0;
extern "C" {
int g_nLastBreakTime = 0;
}
#endif
#ifdef __PRAXIS_LINUX__
int g_nLastBreakTime = 0;
#endif
#ifdef __PRAXIS_WINDOWS__
// This is never needed, just leaving it here for historical preservation reasons, or reference, or until I get sick of it.
//int CALLBACK WinMain( HINSTANCE hInstance,
// HINSTANCE hPrevInstance,
// LPSTR lpCmdLine,
// int nCmdShow)
//
// To turn off the console, uncomment this and comment out CONFIG += console in praxis.pro
//#ifdef _MSC_VER
//# pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
//#endif
#endif
int main()
{
#ifdef __PRAXIS_LINUX__
sigaction(SIGINT, NULL, &old_act);
if (old_act.sa_handler != SIG_IGN)
{
memset(&new_act, 0, sizeof(new_act));
new_act.sa_handler = &handle_sigint;
sigaction(SIGINT, &new_act, NULL);
}
pthread_mutex_init(&g_inputthreadmutex, NULL);
int iret1 = pthread_create( &g_inputthread, NULL, inputthread_function, NULL);
if(iret1)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
}
#endif
//MoveConsTest();
#ifdef __PRAXIS_WINDOWS__
// hInstance;hPrevInstance;lpCmdLine;nCmdShow;
InitializeCriticalSection(&g_cs);
#endif
aiNeuralNetworkSystem::Startup();
// Need to add lua callbacks for neural net system.
// Lua
luaInit();
luaInitCallbacks();
#ifdef __PRAXIS_WITH_LISP__
// Lisp
lispInit();
lispInitCallbacks();
#endif
#ifdef __PRAXIS_WITH_FORTH__
// Forth
forthInit();
forthInitCallbacks();
#endif
#ifdef __PRAXIS_WITH_IO__
// Io
ioInit();
ioInitCallbacks();
#endif
#ifdef __PRAXIS_WINDOWS__
// FMOD mp3 playing
// Can we have FMOD mp3 playing on top of synthesizing audio??
if (!FSOUND_Init(44100, 32, FSOUND_INIT_USEDEFAULTMIDISYNTH))
{
//qDebug("FMOD: FSOUND_Init error!\n");
//qDebug("%s\n", FMOD_ErrorString(FSOUND_GetError()));
}
else
{
g_pMp3Stream = FSOUND_Stream_Open("music.mp3", FSOUND_MPEGACCURATE, 0, 0);
if (!g_pMp3Stream)
{
//qDebug("FMOD: FSOUND_Stream_Open error!\n");
//qDebug("%s\n", FMOD_ErrorString(FSOUND_GetError()));
}
}
#endif
World * pWorld = new World();
g_pWorld = pWorld;
int argc = 0;
char ** argv = 0;
RunSingleWorldConfiguration(pWorld, "praxis", argc, argv);
delete pWorld;
luaClose();
#ifdef __PRAXIS_WITH_LISP__
lispClose();
#endif
#ifdef __PRAXIS_WITH_FORTH__
forthClose();
#endif
#ifdef __PRAXIS_WITH_IO__
ioClose();
#endif
#ifdef __PRAXIS_WINDOWS__
FSOUND_Stream_Close(g_pMp3Stream);
FSOUND_Close();
#endif
aiNeuralNetworkSystem::Shutdown();
#ifdef __PRAXIS_WINDOWS__
DeleteCriticalSection (&g_cs) ;
#endif
#ifdef __PRAXIS_WINDOWS__
return EXIT_SUCCESS;
#else
return 0;
#endif
}
#if 0
// MoveConsTest code
#include <iostream>
#include <algorithm>
class MemoryBlock
{
public:
// Simple constructor that initializes the resource.
explicit MemoryBlock(size_t length)
: _length(length)
, _data(new int[length])
{
std::cout << "In MemoryBlock(size_t). length = "
<< _length << "." << std::endl;
}
// Destructor.
~MemoryBlock()
{
std::cout << "In ~MemoryBlock(). length = "
<< _length << ".";
if (_data != NULL)
{
std::cout << " Deleting resource.";
// Delete the resource.
delete[] _data;
}
std::cout << std::endl;
}
// Copy constructor.
MemoryBlock(const MemoryBlock& other)
: _length(other._length)
, _data(new int[other._length])
{
std::cout << "In MemoryBlock(const MemoryBlock&). length = "
<< other._length << ". Copying resource." << std::endl;
std::copy(other._data, other._data + _length, _data);
}
// Copy assignment operator.
MemoryBlock& operator=(const MemoryBlock& other)
{
std::cout << "In operator=(const MemoryBlock&). length = "
<< other._length << ". Copying resource." << std::endl;
if (this != &other)
{
// Free the existing resource.
delete[] _data;
_length = other._length;
_data = new int[_length];
std::copy(other._data, other._data + _length, _data);
}
return *this;
}
// Retrieves the length of the data resource.
size_t Length() const
{
return _length;
}
#if 1
// Move constructor.
MemoryBlock(MemoryBlock&& other)
: _data(NULL)
, _length(0)
{
std::cout << "In MemoryBlock(MemoryBlock&&). length = "
<< other._length << ". Moving resource." << std::endl;
// Copy the data pointer and its length from the
// source object.
_data = other._data;
_length = other._length;
// Release the data pointer from the source object so that
// the destructor does not free the memory multiple times.
other._data = NULL;
other._length = 0;
}
// Move assignment operator.
MemoryBlock& operator=(MemoryBlock&& other)
{
std::cout << "In operator=(MemoryBlock&&). length = "
<< other._length << "." << std::endl;
if (this != &other)
{
// Free the existing resource.
delete[] _data;
// Copy the data pointer and its length from the
// source object.
_data = other._data;
_length = other._length;
// Release the data pointer from the source object so that
// the destructor does not free the memory multiple times.
other._data = NULL;
other._length = 0;
}
return *this;
}
#endif
private:
size_t _length; // The length of the resource.
int* _data; // The resource.
};
#include <vector>
using namespace std;
void MoveConsTest()
{
// Create a vector object and add a few elements to it.
vector<MemoryBlock> v;
v.push_back(MemoryBlock(25));
v.push_back(MemoryBlock(75));
// Insert a new element into the second position of the vector.
v.insert(v.begin() + 1, MemoryBlock(50));
}
#endif