This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 233
/
main.c
727 lines (605 loc) · 21.2 KB
/
main.c
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
/*
* Copyright (c) 2011-2012 Research In Motion Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <screen/screen.h>
#include <bps/navigator.h>
#include <bps/screen.h>
#include <bps/bps.h>
#include <bps/event.h>
#include <bps/orientation.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include "bbutil.h"
static GLfloat radio_btn_unselected_vertices[8], radio_btn_selected_vertices[8],
shadow_vertices[8];
static GLfloat radio_btn_unselected_tex_coord[8],
radio_btn_selected_tex_coord[8], shadow_tex_coord[8];
static GLuint radio_btn_unselected, radio_btn_selected, shadow;
static screen_context_t screen_cxt;
static font_t* font;
static float width, height, angle;
static int shutdown, menu_active, menu_hide_animation, menu_show_animation;
static int selected;
static float cube_color[4];
static float menu_animation, menu_height, button_size_x, button_size_y;
static float pos_x, pos_y;
static float cube_pos_x, cube_pos_y, cube_pos_z, shadow_pos_x, shadow_pos_y,
shadow_size_x, shadow_size_y;
GLfloat light_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat light_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat light_pos[] = { 0.0f, 25.0f, 0.0f, 1.0f };
GLfloat light_direction[] = { 0.0f, 0.0f, -30.0f, 1.0f };
static float cube_vertices[] = {
// FRONT
-2.0f, -2.0f, 2.0f, 2.0f, -2.0f, 2.0f, -2.0f,
2.0f,
2.0f,
2.0f,
2.0f,
2.0f,
// BACK
-2.0f, -2.0f, -2.0f, -2.0f, 2.0f, -2.0f, 2.0f, -2.0f,
-2.0f,
2.0f,
2.0f,
-2.0f,
// LEFT
-2.0f, -2.0f, 2.0f, -2.0f, 2.0f, 2.0f, -2.0f, -2.0f, -2.0f,
-2.0f,
2.0f,
-2.0f,
// RIGHT
2.0f, -2.0f, -2.0f, 2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f, 2.0f,
2.0f,
2.0f,
// TOP
-2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f, 2.0f,
-2.0f,
// BOTTOM
-2.0f, -2.0f, 2.0f, -2.0f, -2.0f, -2.0f, 2.0f, -2.0f, 2.0f, 2.0f, -2.0f,
-2.0f, };
float cube_normals[] = {
// FRONT
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f,
1.0f,
// BACK
0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
0.0f,
-1.0f,
// LEFT
-1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
0.0f,
// RIGHT
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
// TOP
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
// BOTTOM
0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,
0.0f };
int resize();
void update();
void render();
int read_from_file();
void save_to_file();
void handleClick(int x, int y) {
if (menu_active) {
if ((y > menu_height - 4 * button_size_y)
&& (y < menu_height - 3 * button_size_y) && (x > 0)
&& (x < button_size_x)) {
selected = 3;
cube_color[0] = 1.0f;
cube_color[1] = 0.0f;
cube_color[2] = 0.0f;
cube_color[3] = 1.0f;
} else if ((y > menu_height - 3 * button_size_y)
&& (y < menu_height - 2 * button_size_y) && (x > 0)
&& (x < button_size_x)) {
selected = 2;
cube_color[0] = 0.0f;
cube_color[1] = 1.0f;
cube_color[2] = 0.0f;
cube_color[3] = 1.0f;
} else if ((y > menu_height - 2 * button_size_y)
&& (y < menu_height - button_size_y) && (x > 0)
&& (x < button_size_x)) {
selected = 1;
cube_color[0] = 0.0f;
cube_color[1] = 0.0f;
cube_color[2] = 1.0f;
cube_color[3] = 1.0f;
} else if ((y > menu_height - button_size_y) && (y < menu_height)
&& (x > 0) && (x < button_size_x)) {
selected = 0;
cube_color[0] = 1.0f;
cube_color[1] = 1.0f;
cube_color[2] = 0.0f;
cube_color[3] = 1.0f;
} else {
menu_hide_animation = true;
menu_show_animation = false;
menu_active = false;
}
//Save current state to a file
save_to_file();
}
}
static void handleScreenEvent(bps_event_t *event) {
int screen_val, buttons;
int pair[2];
static bool mouse_pressed = false;
screen_event_t screen_event = screen_event_get_event(event);
//Query type of screen event and its location on the screen
screen_get_event_property_iv(screen_event, SCREEN_PROPERTY_TYPE,
&screen_val);
screen_get_event_property_iv(screen_event, SCREEN_PROPERTY_SOURCE_POSITION,
pair);
//There is a difference between touch screen events and mouse events
if (screen_val == SCREEN_EVENT_MTOUCH_RELEASE) {
//Handle touch screen event
handleClick(pair[0], pair[1]);
} else if (screen_val == SCREEN_EVENT_POINTER) {
//This is a mouse move event, it is applicable to a device with a usb mouse or simulator
screen_get_event_property_iv(screen_event, SCREEN_PROPERTY_BUTTONS,
&buttons);
if (buttons == SCREEN_LEFT_MOUSE_BUTTON) {
//Left mouse button is pressed
mouse_pressed = true;
} else {
if (mouse_pressed) {
//Left mouse button was released, handle left click
handleClick(pair[0], pair[1]);
mouse_pressed = false;
}
}
}
}
static void handleNavigatorEvent(bps_event_t *event) {
int rc;
bps_event_t *activation_event = NULL;
switch (bps_event_get_code(event)) {
case NAVIGATOR_ORIENTATION_CHECK:
//Signal navigator that we intend to resize
navigator_orientation_check_response(event, true);
break;
case NAVIGATOR_ORIENTATION:
if (EXIT_FAILURE == resize(event)) {
shutdown = true;
}
break;
case NAVIGATOR_SWIPE_DOWN:
menu_show_animation = true;
menu_hide_animation = false;
break;
case NAVIGATOR_EXIT:
shutdown = true;
break;
case NAVIGATOR_WINDOW_INACTIVE:
//Wait for NAVIGATOR_WINDOW_ACTIVE event
for (;;) {
rc = bps_get_event(&activation_event, -1);
assert(rc == BPS_SUCCESS);
if (bps_event_get_code(activation_event)
== NAVIGATOR_WINDOW_ACTIVE) {
break;
}
}
break;
}
}
static void handle_events() {
int rc = BPS_SUCCESS;
//Request and process available BPS events
for(;;) {
bps_event_t *event = NULL;
rc = bps_get_event(&event, 0);
assert(rc == BPS_SUCCESS);
if (event) {
int domain = bps_event_get_domain(event);
if (domain == screen_get_domain()) {
handleScreenEvent(event);
} else if (domain == navigator_get_domain()) {
handleNavigatorEvent(event);
}
} else {
break;
}
}
}
int resize(bps_event_t *event) {
//Query width and height of the window surface created by utility code
EGLint surface_width, surface_height;
if (event) {
int angle = navigator_event_get_orientation_angle(event);
//Let bbutil rotate current screen surface to this angle
if (EXIT_FAILURE == bbutil_rotate_screen_surface(angle)) {
fprintf(stderr, "Unable to handle orientation change\n");
return EXIT_FAILURE;
}
}
eglQuerySurface(egl_disp, egl_surf, EGL_WIDTH, &surface_width);
eglQuerySurface(egl_disp, egl_surf, EGL_HEIGHT, &surface_height);
EGLint err = eglGetError();
if (err != 0x3000) {
fprintf(stderr, "Unable to query EGL surface dimensions\n");
return EXIT_FAILURE;
}
width = (float) surface_width;
height = (float) surface_height;
if (width > height) {
shadow_pos_x = 365.0f;
shadow_pos_y = 0.0f;
cube_pos_x = 2.9f;
cube_pos_y = 0.3f;
cube_pos_z = -20.0f;
} else {
shadow_pos_x = 70.0f;
shadow_pos_y = 10.0f;
cube_pos_x = 0.5f;
cube_pos_y = -4.1f;
cube_pos_z = -30.0f;
}
shadow_vertices[0] = shadow_pos_x;
shadow_vertices[1] = shadow_pos_y;
shadow_vertices[2] = shadow_pos_x + shadow_size_x;
shadow_vertices[3] = shadow_pos_y;
shadow_vertices[4] = shadow_pos_x;
shadow_vertices[5] = shadow_pos_y + shadow_size_y;
shadow_vertices[6] = shadow_pos_x + shadow_size_x;
shadow_vertices[7] = shadow_pos_y + shadow_size_y;
update();
if (event) {
render();
navigator_done_orientation(event);
}
return EXIT_SUCCESS;
}
int initialize() {
//Load shadow and button textures
float tex_x = 1.0f, tex_y = 1.0f;
//Load textures for radio buttons
int size_x = 64, size_y = 64;
if (EXIT_SUCCESS
!= bbutil_load_texture("app/native/radio_btn_unselected.png",
NULL, NULL, &tex_x, &tex_y, &radio_btn_unselected)) {
fprintf(stderr, "Unable to load non-selected radio button texture\n");
}
radio_btn_unselected_vertices[0] = 0.0f;
radio_btn_unselected_vertices[1] = 0.0f;
radio_btn_unselected_vertices[2] = size_x;
radio_btn_unselected_vertices[3] = 0.0f;
radio_btn_unselected_vertices[4] = 0.0f;
radio_btn_unselected_vertices[5] = size_y;
radio_btn_unselected_vertices[6] = size_x;
radio_btn_unselected_vertices[7] = size_y;
radio_btn_unselected_tex_coord[0] = 0.0f;
radio_btn_unselected_tex_coord[1] = 0.0f;
radio_btn_unselected_tex_coord[2] = tex_x;
radio_btn_unselected_tex_coord[3] = 0.0f;
radio_btn_unselected_tex_coord[4] = 0.0f;
radio_btn_unselected_tex_coord[5] = tex_y;
radio_btn_unselected_tex_coord[6] = tex_x;
radio_btn_unselected_tex_coord[7] = tex_y;
if (EXIT_SUCCESS
!= bbutil_load_texture("app/native/radio_btn_selected.png", NULL,
NULL, &tex_x, &tex_y, &radio_btn_selected)) {
fprintf(stderr, "Unable to load selected radio button texture\n");
}
radio_btn_selected_vertices[0] = 0.0f;
radio_btn_selected_vertices[1] = 0.0f;
radio_btn_selected_vertices[2] = size_x;
radio_btn_selected_vertices[3] = 0.0f;
radio_btn_selected_vertices[4] = 0.0f;
radio_btn_selected_vertices[5] = size_y;
radio_btn_selected_vertices[6] = size_x;
radio_btn_selected_vertices[7] = size_y;
radio_btn_selected_tex_coord[0] = 0.0f;
radio_btn_selected_tex_coord[1] = 0.0f;
radio_btn_selected_tex_coord[2] = tex_x;
radio_btn_selected_tex_coord[3] = 0.0f;
radio_btn_selected_tex_coord[4] = 0.0f;
radio_btn_selected_tex_coord[5] = tex_y;
radio_btn_selected_tex_coord[6] = tex_x;
radio_btn_selected_tex_coord[7] = tex_y;
button_size_x = (float) size_x;
button_size_y = (float) size_y;
if (EXIT_SUCCESS
!= bbutil_load_texture("app/native/shadow.png", NULL, NULL,
&tex_x, &tex_y, &shadow)) {
fprintf(stderr, "Unable to load shadow texture\n");
}
shadow_size_x = (float) 512;
shadow_size_y = (float) 256;
shadow_tex_coord[0] = 0.0f;
shadow_tex_coord[1] = 0.0f;
shadow_tex_coord[2] = tex_x;
shadow_tex_coord[3] = 0.0f;
shadow_tex_coord[4] = 0.0f;
shadow_tex_coord[5] = tex_y;
shadow_tex_coord[6] = tex_x;
shadow_tex_coord[7] = tex_y;
angle = 0.0f;
pos_x = 0.0f;
pos_y = 0.0f;
//Load MyriadPro bold to use for our color menu
int dpi = bbutil_calculate_dpi(screen_cxt);
if (dpi == EXIT_FAILURE) {
fprintf(stderr, "Unable to calculate dpi\n");
return EXIT_FAILURE;
}
font = bbutil_load_font(
"/usr/fonts/font_repository/adobe/MyriadPro-Bold.otf", 15, dpi);
if (!font) {
return EXIT_FAILURE;
}
float text_width, text_height;
bbutil_measure_text(font, "Color Menu", &text_width, &text_height);
menu_height = text_height + 10.0f + button_size_y * 4;
//See if a savefile exists. If not, initialize to a hidden menu and a red cube.
if (!read_from_file()) {
selected = 3;
cube_color[0] = 1.0f;
cube_color[1] = 0.0f;
cube_color[2] = 0.0f;
cube_color[3] = 1.0f;
menu_animation = 0.0f;
menu_active = false;
menu_show_animation = false;
menu_hide_animation = false;
}
//Initialize positions of graphics assets on the screen, but don't resize the surface
if (EXIT_FAILURE == resize(NULL)) {
fprintf(stderr, "Initialize surface\n");
return EXIT_FAILURE;
}
//Common gl setup
glShadeModel(GL_SMOOTH);
glClearColor(0.775f, 0.775f, 0.775f, 1.0f);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_direction);
glEnable(GL_CULL_FACE);
return EXIT_SUCCESS;
}
void enable_2d() {
glViewport(0, 0, (int) width, (int) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0.0f, width / height, 0.0f, 1.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(1.0f / height, 1.0f / height, 1.0f);
}
void enable_3d() {
glViewport(0, 0, (int) width, (int) height);
GLfloat aspect_ratio = width / height;
GLfloat fovy = 45;
GLfloat zNear = 1.0f;
GLfloat zFar = 1000.0f;
GLfloat top = tan(fovy * 0.0087266462599716478846184538424431f) * zNear;
GLfloat bottom = -top;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(aspect_ratio * bottom, aspect_ratio * top, bottom, top, zNear,
zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void update() {
angle = fmod((angle + 1.0f), 360.0 );
if (menu_show_animation) {
if (menu_animation < menu_height) {
menu_animation += 7.0f;
} else {
menu_show_animation = false;
menu_active = true;
//Save current state to a file
save_to_file();
}
} else if (menu_hide_animation) {
if (menu_animation > 0.0f) {
menu_animation -= 7.0f;
} else {
menu_hide_animation = false;
}
}
pos_y = height - menu_animation;
}
void render() {
int i;
//Typical render pass
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//First render background and menu if it is enabled
enable_2d();
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glVertexPointer(2, GL_FLOAT, 0, shadow_vertices);
glTexCoordPointer(2, GL_FLOAT, 0, shadow_tex_coord);
glBindTexture(GL_TEXTURE_2D, shadow);
//glColor4f(0.85f, 0.85f, 0.85f, 1.0f);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
if (menu_active || menu_show_animation || menu_hide_animation) {
glTranslatef(pos_x, pos_y, 0.0f);
for (i = 0; i < 4; i++) {
if (i == selected) {
glVertexPointer(2, GL_FLOAT, 0, radio_btn_selected_vertices);
glTexCoordPointer(2, GL_FLOAT, 0, radio_btn_selected_tex_coord);
glBindTexture(GL_TEXTURE_2D, radio_btn_selected);
} else {
glVertexPointer(2, GL_FLOAT, 0, radio_btn_unselected_vertices);
glTexCoordPointer(2, GL_FLOAT, 0,
radio_btn_unselected_tex_coord);
glBindTexture(GL_TEXTURE_2D, radio_btn_unselected);
}
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glTranslatef(0.0f, 60.0f, 0.0f);
}
bbutil_render_text(font, "Color Menu", 10.0f, 10.0f, 0.35f, 0.35f, 0.35f, 1.0f);
bbutil_render_text(font, "Red", 70.0f, -40.0f, 0.35f, 0.35f, 0.35f, 1.0f);
bbutil_render_text(font, "Green", 70.0f, -100.0f, 0.35f, 0.35f, 0.35f, 1.0f);
bbutil_render_text(font, "Blue", 70.0f, -160.0f, 0.35f, 0.35f, 0.35f, 1.0f);
bbutil_render_text(font, "Yellow", 70.0f, -220.0f, 0.35f, 0.35f, 0.35f, 1.0f);
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);
//Then render the cube
enable_3d();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glTranslatef(cube_pos_x, cube_pos_y, cube_pos_z);
glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
glRotatef(15.0f, 0.0f, 0.0f, 1.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glColor4f(cube_color[0], cube_color[1], cube_color[2], cube_color[3]);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, cube_vertices);
glNormalPointer(GL_FLOAT, 0, cube_normals);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 12, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 16, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 20, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_DEPTH_TEST);
//Use utility code to update the screen
bbutil_swap();
}
int read_from_file() {
//open file as binary
FILE *fp = fopen("data/save.dat", "rb");
if (!fp) {
return false;
}
int rc = fscanf(fp, "%i %i", &selected, &menu_active);
if (rc == -1) {
return false;
} else {
if (selected == 0) {
cube_color[0] = 1.0f;
cube_color[1] = 1.0f;
cube_color[2] = 0.0f;
cube_color[3] = 1.0f;
} else if (selected == 1) {
cube_color[0] = 0.0f;
cube_color[1] = 0.0f;
cube_color[2] = 1.0f;
cube_color[3] = 1.0f;
} else if (selected == 2) {
cube_color[0] = 0.0f;
cube_color[1] = 1.0f;
cube_color[2] = 0.0f;
cube_color[3] = 1.0f;
} else if (selected == 3) {
cube_color[0] = 1.0f;
cube_color[1] = 0.0f;
cube_color[2] = 0.0f;
cube_color[3] = 1.0f;
} else {
return false;
}
}
if (menu_active) {
menu_animation = menu_height;
}
fclose(fp);
return true;
}
void save_to_file() {
//open file as binary
FILE *fp = fopen("data/save.dat", "wb");
if (!fp) {
return;
}
fprintf(fp, "%i %i", selected, menu_active);
fclose(fp);
}
int main(int argc, char *argv[]) {
//Create a screen context that will be used to create an EGL surface to to receive libscreen events
screen_create_context(&screen_cxt, 0);
//Initialize BPS library
bps_initialize();
//Use utility code to initialize EGL for rendering with GL ES 1.1
if (EXIT_SUCCESS != bbutil_init_egl(screen_cxt)) {
fprintf(stderr, "bbutil_init_egl failed\n");
bbutil_terminate();
screen_destroy_context(screen_cxt);
return 0;
}
//Initialize application logic
if (EXIT_SUCCESS != initialize()) {
fprintf(stderr, "initialize failed\n");
bbutil_terminate();
screen_destroy_context(screen_cxt);
return 0;
}
//Signal BPS library that navigator and screen events will be requested
if (BPS_SUCCESS != screen_request_events(screen_cxt)) {
fprintf(stderr, "screen_request_events failed\n");
bbutil_terminate();
screen_destroy_context(screen_cxt);
return 0;
}
if (BPS_SUCCESS != navigator_request_events(0)) {
fprintf(stderr, "navigator_request_events failed\n");
bbutil_terminate();
screen_destroy_context(screen_cxt);
return 0;
}
//Signal BPS library that navigator orientation is not to be locked
if (BPS_SUCCESS != navigator_rotation_lock(false)) {
fprintf(stderr, "navigator_rotation_lock failed\n");
bbutil_terminate();
screen_destroy_context(screen_cxt);
return 0;
}
while (!shutdown) {
// Handle user input and accelerometer
handle_events();
// Update scene contents
update();
// Draw Scene
render();
}
//Stop requesting events from libscreen
screen_stop_events(screen_cxt);
//Use utility code to terminate EGL setup
bbutil_terminate();
//Shut down BPS library for this process
bps_shutdown();
//Destroy libscreen context
screen_destroy_context(screen_cxt);
return 0;
}