This repository was archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathEdenMessage.c
More file actions
executable file
·740 lines (633 loc) · 25.4 KB
/
EdenMessage.c
File metadata and controls
executable file
·740 lines (633 loc) · 25.4 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//
// EdenMessage.h
//
// Copyright (c) 2001-2013 Philip Lamb (PRL) phil@eden.net.nz. All rights reserved.
// Font loading code based on code by Jeff Molofee, 1999, http://nehe.gamedev.net/
//
// Rev Date Who Changes
// 1.0.0 2001-12-04 PRL Initial version for The SRMS simulator.
// 1.0.1 2005-09-28 PRL Added headerDoc.
// 1.1.0 2013-02-19 PRL Quick update for OpenGL ES.
//
// @@BEGIN_EDEN_LICENSE_HEADER@@
//
// This file is part of The Eden Library.
//
// The Eden Library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Eden Library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with The Eden Library. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules, and to
// copy and distribute the resulting executable under terms of your choice,
// provided that you also meet, for each linked independent module, the terms and
// conditions of the license of that module. An independent module is a module
// which is neither derived from nor based on this library. If you modify this
// library, you may extend this exception to your version of the library, but you
// are not obligated to do so. If you do not wish to do so, delete this exception
// statement from your version.
//
// @@END_EDEN_LICENSE_HEADER@@
// ============================================================================
// Includes
// ============================================================================
#include <Eden/EdenMessage.h>
#include <Eden/EdenMath.h> // MIN()
#include <stdio.h>
#include <string.h>
#include <stdlib.h> // malloc(), calloc(), free(), exit()
#ifndef _MSC_VER
# include <stdbool.h>
#else
typedef unsigned char bool;
# define false 0
# define true 1
# include <sys/timeb.h> // struct _timeb
#endif
#include <pthread.h>
#include <errno.h> // ETIMEDOUT
#include <Eden/EdenTime.h> // EdenTimeAbsolutePlusOffset(), struct timespec, EdenTime_sleep()
#include <Eden/EdenSurfaces.h> // TEXTURE_INFO_t, TEXTURE_INDEX_t, SurfacesTextureLoad(), SurfacesTextureSet(), SurfacesTextureUnload()
#include <Eden/EdenGLFont.h>
#if EDEN_USE_GL
# define USE_GL_STATE_CACHE 0
# include <Eden/glStateCache.h>
#elif EDEN_USE_GLES2
# include <AR6/ARG/glStateCache2.h>
# include <AR6/ARG/arg_shader_gl.h>
# include <AR6/ARG/arg_mtx.h>
#endif
// ============================================================================
// Types and constants
// ============================================================================
#define BOX_LINES_MAX 80
#define BOX_LINE_LENGTH_MAX 1023
typedef struct _boxSettings {
pthread_mutex_t lock;
float boxWidth; // Pixel width of box.
float boxHeight; // Pixel height of box (calculated).
float boxPaddingH; // Pixels of padding between the left and right border and the text.
float boxPaddingV; // Pixels of padding between the top and bottom border and the text.
float boxCornerRadius; // Radius in pixels of the rounded corners. Typically set to MIN(boxPaddingH, boxPaddinV).
float softwrapRatio; // Percentage of the maximum text width at which to start soft-wrapping text.
unsigned char *text;
unsigned char *lines[BOX_LINES_MAX];
int lineCount;
} boxSettings_t;
//#define DEBUG_MESSAGE // Uncomment to show extra debugging info.
#pragma mark -
#pragma mark [GLOBAL VARIABLES]
// ============================================================================
// Global variables
// ============================================================================
// Sanity checks.
static EDEN_BOOL gMessageInited = FALSE; // Set to TRUE once EdenMessageInit() has succesfully completed.
static boxSettings_t *gBoxSettings = NULL;
// Screen size.
static float gScreenWidth = 640.0f;
static float gScreenHeight = 480.0f;
static float gScreenScale = 1.0f;
#if EDEN_USE_GLES2
// Indices of GL ES program uniforms.
enum {
UNIFORM_MODELVIEW_PROJECTION_MATRIX,
UNIFORM_COLOR,
UNIFORM_COUNT
};
// Indices of of GL ES program attributes.
enum {
ATTRIBUTE_VERTEX,
ATTRIBUTE_COUNT
};
static GLint uniforms[UNIFORM_COUNT] = {0};
static GLuint program = 0;
#endif
// Use of gDrawLock allows EdenMessageDraw() to be called in a separate thread
// by protecting the global static data (below) that it uses.
//static pthread_mutex_t gDrawLock;
EDEN_BOOL gEdenMessageDrawRequired = FALSE; // EdenMessageDraw() should be called if this is set to TRUE;
static pthread_mutex_t gInputLock; // Protects gInput.
static unsigned char *gInput; // Pointer to buffer for input string, including prompt.
static unsigned char *gInputPtr; // Pointer into buffer to start of input string.
static unsigned int gInputLength; // Holds length of input string including prompt.
static unsigned int gInputPromptLength; //
static int gInputCursorState;
static pthread_cond_t gInputCompleteCond;
static EDEN_BOOL gInputComplete; // Set to TRUE once EdenMessageInputKeyboard() has captured a response.
// Input constraints.
static unsigned int gInputLengthMin; // Minimum length of user input string.
static unsigned int gInputLengthMax; // Maximum length of user input string.
static int gInputIntOnly;
static int gInputFPOnly;
static int gInputAlphaOnly;
EDEN_BOOL gEdenMessageKeyboardRequired = FALSE; // EdenMessageInputKeyboard should be called with keystrokes if this is set to TRUE;
//
// Private functions.
//
// Can pass NULL for parameter 'text' in which case previous text is reused.
// Depends on settings 'text', 'boxWidth', 'boxPaddingH'.
// Sets settings 'text', 'lines', 'lineCount', 'boxHeight'.
static void boxSetText(boxSettings_t *settings, const unsigned char *text)
{
int i;
int textIndex;
bool done;
float boxTextWidth;
float boxTextWidthSoftwrap;
float hyphenWidth;
float interCharSpacingWidth;
unsigned char c = '\0', c0, c1; // current, previous, next char.
int lineLength = 0;
unsigned char lineBuf[BOX_LINE_LENGTH_MAX + 1] = ""; // +1 for null terminator.
float lineWidth = 0;
if (!settings) return;
pthread_mutex_lock(&settings->lock);
if (text) {
free(settings->text);
settings->text = (unsigned char *)strdup((char *)text);
}
// Free old lines.
if (settings->lineCount) {
for (i = 0; i < settings->lineCount; i++) {
free(settings->lines[i]);
settings->lines[i] = NULL;
}
settings->lineCount = 0;
}
boxTextWidth = settings->boxWidth - 2*settings->boxPaddingH;
boxTextWidthSoftwrap = boxTextWidth*settings->softwrapRatio;
hyphenWidth = EdenGLFontGetCharacterWidth('-');
interCharSpacingWidth = EdenGLFontGetLineWidth((const unsigned char *)"--") - 2.0f*hyphenWidth;
if (settings->text) {
// Split text into lines, softwrapping on whitespace if possible.
textIndex = 0;
done = false;
do {
bool newline = false;
c0 = c;
c = settings->text[textIndex];
if (!c) {
if (lineLength) newline = true;
done = true;
} else if (c == '\n' || (c == ' ' && lineWidth >= boxTextWidthSoftwrap)) {
textIndex++;
newline = true;
} else if (c < ' ') {
textIndex++;
} else {
bool addChar = false;
// Is there still room for a hyphen after this character?
float predictedLineWidth = lineWidth + interCharSpacingWidth + EdenGLFontGetCharacterWidth(settings->text[textIndex]);
if (predictedLineWidth < (boxTextWidth - hyphenWidth)) {
addChar = true;
} else {
// No. But two exceptions:
// 1) this character is a space.
// 2) this character doesn't overflow and the next character is whitespace.
c1 = settings->text[textIndex + 1];
if (c == ' ') {
textIndex++;
newline = true;
} else if (predictedLineWidth <= boxTextWidth && (!c1 || c1 == ' ' || c1 == '\n')) {
addChar = true;
} else {
// Exception didn't apply, so insert hyphen, then newline, then continue with same char on next line (unless previous char was space, in which case no hyphen).
if (c0 != ' ') {
lineBuf[lineLength++] = '-';
lineBuf[lineLength] = '\0';
}
newline = true;
}
}
if (addChar) {
lineBuf[lineLength++] = c;
lineBuf[lineLength] = '\0';
lineWidth = EdenGLFontGetLineWidth(lineBuf);
if (lineLength == BOX_LINE_LENGTH_MAX) newline = true; // Next char would overflow buffer, so break now.
textIndex++;
}
}
if (newline) {
// Start a new line.
settings->lines[settings->lineCount] = (unsigned char *)strdup((const char *)lineBuf);
settings->lineCount++;
if (settings->lineCount == BOX_LINES_MAX) done = true;
lineLength = 0;
lineBuf[0] = '\0';
lineWidth = 0.0f;
}
} while (!done);
}
settings->boxHeight = EdenGLFontGetBlockHeight((const unsigned char **)settings->lines, settings->lineCount) + 2.0f*settings->boxPaddingV;
pthread_mutex_unlock(&settings->lock);
}
static boxSettings_t *boxCreate(float width, float paddingH, float paddingV, float cornerRadius, float softwrapRatio)
{
boxSettings_t *settings = (boxSettings_t *)calloc(1, sizeof(boxSettings_t));
if (!settings) return (NULL);
pthread_mutex_init(&settings->lock, NULL);
settings->boxWidth = (width > 0.0f ? width : 400.0f)*gScreenScale;
settings->boxPaddingH = (paddingH >= 0.0f ? paddingH : 20.0f)*gScreenScale;
settings->boxPaddingV = (paddingV >= 0.0f ? paddingV : 20.0f)*gScreenScale;
settings->boxCornerRadius = (cornerRadius >= 0.0f ? cornerRadius*gScreenScale : MIN(settings->boxPaddingH, settings->boxPaddingV));
settings->softwrapRatio = (softwrapRatio > 0.0f ? softwrapRatio : 0.9f);
return (settings);
}
static void boxDestroy(boxSettings_t **settings_p)
{
int i;
if (!settings_p || !*settings_p) return;
pthread_mutex_destroy(&(*settings_p)->lock);
// Free lines.
if ((*settings_p)->lineCount) {
for (i = 0; i < (*settings_p)->lineCount; i++) free((*settings_p)->lines[i]);
(*settings_p)->lineCount = 0;
}
free((*settings_p)->text);
free(*settings_p);
(*settings_p) = NULL;
}
// ============================================================================
// Public functions
// ============================================================================
EDEN_BOOL EdenMessageInit(const int contextsActiveCount)
{
if (gMessageInited) return (FALSE);
// OpenGL setup.
#if EDEN_USE_GLES2
if (!program) {
GLuint vertShader = 0, fragShader = 0;
// A simple shader pair which accepts just a vertex position. Fixed color, no lighting.
const char vertShaderString[] =
"attribute vec4 position;\n"
"uniform vec4 color;\n"
"uniform mat4 modelViewProjectionMatrix;\n"
"varying vec4 colorVarying;\n"
"void main()\n"
"{\n"
"gl_Position = modelViewProjectionMatrix * position;\n"
"colorVarying = color;\n"
"}\n";
const char fragShaderString[] =
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n"
"varying vec4 colorVarying;\n"
"void main()\n"
"{\n"
"gl_FragColor = colorVarying;\n"
"}\n";
if (program) arglGLDestroyShaders(0, 0, program);
program = glCreateProgram();
if (!program) {
EDEN_LOGe("draw: Error creating shader program.\n");
return (FALSE);
}
if (!arglGLCompileShaderFromString(&vertShader, GL_VERTEX_SHADER, vertShaderString)) {
EDEN_LOGe("draw: Error compiling vertex shader.\n");
arglGLDestroyShaders(vertShader, fragShader, program);
program = 0;
return (FALSE);
}
if (!arglGLCompileShaderFromString(&fragShader, GL_FRAGMENT_SHADER, fragShaderString)) {
EDEN_LOGe("draw: Error compiling fragment shader.\n");
arglGLDestroyShaders(vertShader, fragShader, program);
program = 0;
return (FALSE);
}
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glBindAttribLocation(program, ATTRIBUTE_VERTEX, "position");
if (!arglGLLinkProgram(program)) {
EDEN_LOGe("draw: Error linking shader program.\n");
arglGLDestroyShaders(vertShader, fragShader, program);
program = 0;
return (FALSE);
}
arglGLDestroyShaders(vertShader, fragShader, 0); // After linking, shader objects can be deleted.
// Retrieve linked uniform locations.
uniforms[UNIFORM_MODELVIEW_PROJECTION_MATRIX] = glGetUniformLocation(program, "modelViewProjectionMatrix");
uniforms[UNIFORM_COLOR] = glGetUniformLocation(program, "color");
}
#endif
gBoxSettings = boxCreate(-1.0f, -1.0f, -1.0f, -1.0f, -1.0f); // Use defaults.
pthread_mutex_init(&gInputLock, NULL);
gInput = gInputPtr = NULL;
gInputLength = 0;
gInputPromptLength = 0;
pthread_cond_init(&gInputCompleteCond, NULL);
gInputComplete = FALSE;
gMessageInited = TRUE;
return (TRUE);
}
EDEN_BOOL EdenMessageFinal(void)
{
EDEN_BOOL ok = TRUE;
if (!gMessageInited) return (FALSE);
pthread_mutex_destroy(&gInputLock);
free(gInput);
gInput = gInputPtr = NULL;
gInputLength = 0;
gInputPromptLength = 0;
pthread_cond_destroy(&gInputCompleteCond);
gInputComplete = FALSE;
boxDestroy(&gBoxSettings);
#if EDEN_USE_GLES2
arglGLDestroyShaders(0, 0, program);
#endif
gMessageInited = FALSE;
return (ok);
}
unsigned char *EdenMessageInputGetInput(void)
{
unsigned char *ret;
pthread_mutex_lock(&gInputLock);
if (gInputPtr && gInputComplete) {
gInputPtr[gInputLength] = '\0'; // Overwrite any cursor character.
ret = (unsigned char *)strdup((char *)gInputPtr);
} else ret = NULL;
pthread_mutex_unlock(&gInputLock);
return (ret);
}
#pragma mark -
// ----------------------------------------------------------------------------
// These functions should only be called directly in single- threaded apps.
// In multi-threaded apps, they will be called indirectly.
// ----------------------------------------------------------------------------
EDEN_E_t EdenMessageShow(const unsigned char *msg)
{
if (!gMessageInited) return (EDEN_E_INVALID_COMMAND);
if (msg) {
boxSetText(gBoxSettings, msg);
}
gEdenMessageDrawRequired = TRUE;
return (EDEN_E_NONE);
}
EDEN_E_t EdenMessageHide(void)
{
if (!gMessageInited) return (EDEN_E_INVALID_COMMAND);
gEdenMessageDrawRequired = FALSE;
return (EDEN_E_NONE);
}
EDEN_E_t EdenMessageInputShow(const unsigned char *prompt, const unsigned int minLength, const unsigned int maxLength, int intOnly, int fpOnly, int alphaOnly)
{
EDEN_E_t messageErr = EDEN_E_NONE;
if (!gMessageInited) return (EDEN_E_INVALID_COMMAND);
pthread_mutex_lock(&gInputLock);
if (minLength > maxLength) {
messageErr = EDEN_E_OVERFLOW;
goto done;
}
gInputLengthMin = minLength;
gInputLengthMax = maxLength;
if (prompt) {
gInputPromptLength = (unsigned int)strlen((const char *)prompt);
}
if (gInput) free(gInput);
gInput = (unsigned char *)malloc(gInputPromptLength + gInputLengthMax + 2); // +1 for cursor and +1 for nul-terminator.
if (!gInput) {
messageErr = EDEN_E_OUT_OF_MEMORY;
goto done;
}
if (prompt) {
strncpy((char *)gInput, (const char *)prompt, gInputPromptLength);
}
gInputPtr = gInput + gInputPromptLength;
gInputPtr[0] = '\0';
gInputLength = 0;
gInputCursorState = -1;
gInputComplete = FALSE;
gInputIntOnly = intOnly;
gInputFPOnly = fpOnly;
gInputAlphaOnly = alphaOnly;
if ((messageErr = EdenMessageShow(gInput)) != EDEN_E_NONE) {
goto done;
}
// This signals that keys should start being sent to EdenMessageInputKeyboard()
gEdenMessageKeyboardRequired = TRUE;
done:
pthread_mutex_unlock(&gInputLock);
return (messageErr);
}
EDEN_E_t EdenMessageInputHide(void)
{
if (!gMessageInited) return (EDEN_E_INVALID_COMMAND);
gEdenMessageKeyboardRequired = FALSE;
return (EdenMessageHide());
}
EDEN_BOOL EdenMessageInputIsComplete(void)
{
int ret;
if (!gMessageInited) return (FALSE);
pthread_mutex_lock(&gInputLock);
ret = gInputComplete;
pthread_mutex_unlock(&gInputLock);
return (ret);
}
#pragma mark -
// ----------------------------------------------------------------------------
// Functions for use in multi-threaded apps only.
// ----------------------------------------------------------------------------
EDEN_E_t EdenMessage(unsigned char *msg, const unsigned int secs)
{
EDEN_E_t err;
err = EdenMessageShow(msg);
if (err != EDEN_E_NONE) return (err);
EdenTime_sleep(secs);
err = EdenMessageHide();
if (err != EDEN_E_NONE) return (err);
return (EDEN_E_NONE);
}
EDEN_E_t EdenMessageInput(const unsigned char *prompt, const unsigned int minLength, const unsigned int maxLength, int intOnly, int fpOnly, int alphaOnly)
{
EDEN_E_t messageErr = EDEN_E_NONE;
if ((messageErr = EdenMessageInputShow(prompt, minLength, maxLength, intOnly, fpOnly, alphaOnly)) != EDEN_E_NONE) {
goto done;
}
// Wait for signal from EdenMessageInputKeyboard that input is complete.
pthread_mutex_lock(&gInputLock);
if (!gInputComplete) {
pthread_cond_wait(&gInputCompleteCond, &gInputLock);
}
pthread_mutex_unlock(&gInputLock);
messageErr = EdenMessageInputHide();
done:
return (messageErr);
}
#pragma mark -
// ----------------------------------------------------------------------------
// Functions for use in single- and double-threaded apps.
// ----------------------------------------------------------------------------
void EdenMessageSetViewSize(const float width, const float height)
{
EDEN_BOOL changed = FALSE;
if (gScreenWidth != width) {
gScreenWidth = width;
changed = TRUE;
}
if (gScreenHeight != height) {
gScreenHeight = height;
changed = TRUE;
}
if (changed) boxSetText(gBoxSettings, NULL);
}
void EdenMessageSetBoxParams(const float width, const float padding)
{
EDEN_BOOL changed = FALSE;
if (gBoxSettings->boxWidth != width) {
gBoxSettings->boxWidth = width;
changed = TRUE;
}
if (gBoxSettings->boxPaddingH != padding || gBoxSettings->boxPaddingV != padding) {
gBoxSettings->boxPaddingH = padding;
gBoxSettings->boxPaddingV = padding;
changed = TRUE;
}
if (changed) boxSetText(gBoxSettings, NULL);
}
void EdenMessageDraw(const int contextIndex, const float viewProjection[16])
{
GLfloat boxcentrex, boxcentrey, boxwd2, boxhd2;
GLfloat boxVertices[4][2];
#ifdef _WIN32
struct _timeb sys_time;
#else
struct timeval time;
#endif
int cursorState;
if (!gBoxSettings) return;
// Check if we need to show a blinking cursor, and if so, what state it is in.
pthread_mutex_lock(&gInputLock);
if (gInputPtr && !gInputComplete) {
#ifdef _WIN32
_ftime(&sys_time);
if (sys_time.millitm < 500ul) cursorState = 1;
else cursorState = 0;
#else
# if defined(__linux) || defined(__APPLE__)
gettimeofday( &time, NULL );
# else
gettimeofday( &time );
# endif
if (time.tv_usec < 500000) cursorState = 1;
else cursorState = 0;
#endif
if (cursorState != gInputCursorState) {
gInputCursorState = cursorState;
if (cursorState == 1) {
gInputPtr[gInputLength] = '|';
gInputPtr[gInputLength + 1] = '\0';
} else {
gInputPtr[gInputLength] = ' ';
gInputPtr[gInputLength + 1] = '\0';
}
EdenMessageShow(gInput);
}
}
pthread_mutex_unlock(&gInputLock);
boxcentrex = gScreenWidth / 2.0f;
boxwd2 = gBoxSettings->boxWidth / 2;
boxcentrey = gScreenHeight / 2.0f;
boxhd2 = gBoxSettings->boxHeight / 2;
boxVertices[0][0] = boxcentrex - boxwd2; boxVertices[0][1] = boxcentrey - boxhd2;
boxVertices[1][0] = boxcentrex + boxwd2; boxVertices[1][1] = boxcentrey - boxhd2;
boxVertices[2][0] = boxcentrex + boxwd2; boxVertices[2][1] = boxcentrey + boxhd2;
boxVertices[3][0] = boxcentrex - boxwd2; boxVertices[3][1] = boxcentrey + boxhd2;
// Draw box.
pthread_mutex_lock(&gBoxSettings->lock);
if (gBoxSettings->lineCount) {
// Draw the semi-transparent black shaded box and white outline.
#if EDEN_USE_GL
glStateCacheBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glStateCacheEnableBlend();
glVertexPointer(2, GL_FLOAT, 0, boxVertices);
glStateCacheEnableClientStateVertexArray();
glStateCacheDisableClientStateNormalArray();
glStateCacheClientActiveTexture(GL_TEXTURE0);
glStateCacheDisableClientStateTexCoordArray();
glColor4f(0.0f, 0.0f, 0.0f, 0.5f); // 50% transparent black.
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glStateCacheDisableBlend();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // Opaque white.
glDrawArrays(GL_LINE_LOOP, 0, 4);
EdenGLFontDrawBlock(contextIndex, NULL, (const unsigned char **)gBoxSettings->lines, gBoxSettings->lineCount, 0.0f, 0.0f, H_OFFSET_VIEW_CENTER_TO_TEXT_CENTER, V_OFFSET_VIEW_CENTER_TO_TEXT_CENTER);
#elif EDEN_USE_GLES2
glUseProgram(program);
glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEW_PROJECTION_MATRIX], 1, GL_FALSE, viewProjection);
glStateCacheBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glStateCacheEnableBlend();
glStateCacheDisableDepthTest();
glVertexAttribPointer(ATTRIBUTE_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, boxVertices);
glEnableVertexAttribArray(ATTRIBUTE_VERTEX);
glUniform4f(uniforms[UNIFORM_COLOR], 0.0f, 0.0f, 0.0f, 0.5f); // 50% transparent black.
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glStateCacheDisableBlend();
glUniform4f(uniforms[UNIFORM_COLOR], 1.0f, 1.0f, 1.0f, 1.0f); // Opaque white.
glDrawArrays(GL_LINE_LOOP, 0, 4);
EdenGLFontDrawBlock(contextIndex, viewProjection, (const unsigned char **)gBoxSettings->lines, gBoxSettings->lineCount, 0.0f, 0.0f, H_OFFSET_VIEW_CENTER_TO_TEXT_CENTER, V_OFFSET_VIEW_CENTER_TO_TEXT_CENTER);
#endif
}
pthread_mutex_unlock(&gBoxSettings->lock);
return;
}
EDEN_BOOL EdenMessageInputKeyboard(const unsigned char keyAsciiCode)
{
EDEN_BOOL ret = TRUE;
pthread_mutex_lock(&gInputLock);
if (gInputPtr && !gInputComplete) {
switch (keyAsciiCode) {
case EDEN_ASCII_ESC:
free(gInput);
gInput = gInputPtr = NULL;
gInputLength = 0;
gInputComplete = TRUE;
pthread_cond_signal(&gInputCompleteCond);
break;
case EDEN_ASCII_CR:
if (gInputLength >= gInputLengthMin) {
gInputComplete = TRUE;
pthread_cond_signal(&gInputCompleteCond);
}
break;
case EDEN_ASCII_BS:
case EDEN_ASCII_DEL:
if (gInputLength > 0) {
gInputLength--;
gInputPtr[gInputLength] = '\0';
if (EdenMessageShow(gInput) != EDEN_E_NONE) {
ret = FALSE;
goto done;
}
}
break;
default:
if (keyAsciiCode < ' ') break; // Throw away all other control characters.
if (gInputIntOnly && (keyAsciiCode < '0' || keyAsciiCode > '9')) break;
if (gInputFPOnly && (keyAsciiCode < '0' || keyAsciiCode > '9') && keyAsciiCode != '.') break;
if (gInputAlphaOnly && (keyAsciiCode < 'A' || keyAsciiCode > 'Z') && (keyAsciiCode < 'a' || keyAsciiCode > 'z')) break;
if (gInputLength < gInputLengthMax) {
gInputPtr[gInputLength] = keyAsciiCode;
gInputLength++;
gInputPtr[gInputLength] = '\0';
if (EdenMessageShow(gInput) != EDEN_E_NONE) {
ret = FALSE;
goto done;
}
}
break;
}
}
done:
pthread_mutex_unlock(&gInputLock);
return (ret);
}