-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1394 lines (1269 loc) · 33.4 KB
/
main.cpp
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// main.cpp
/*
This file is part of L-Echo.
L-Echo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
L-Echo 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with L-Echo. If not, see <http://www.gnu.org/licenses/>.
*/
//standard libraries
#include <cstdlib>
#include <cstring>
#include <cmath>
//most of the headers
#include "echo_platform.h"
#include "echo_xml.h"
#include "echo_debug.h"
#include "echo_error.h"
#include "echo_gfx.h"
#include "echo_math.h"
#include "echo_loader.h"
#include "echo_ns.h"
#include "echo_sys.h"
#include "echo_stage.h"
#include "echo_ingame_loader.h"
#include "echo_prefs.h"
#include "echo_char_joints.h"
//various grids
#include "hole.h"
#include "grid.h"
#include "escgrid.h"
#include "t_grid.h"
//for Windows, or something
#define _STDCALL_SUPPORTED
#ifdef ECHO_NDS
//basic nds stuff
#include <nds.h>
//the video stuff for opengl
#include <nds/arm9/video.h>
//to initialize FAT for file reading
#include <fat.h>
//aslib: Advanced Sound LIBrary, for mp3 and sfx playback
//#include <as_lib9.h>
//resources: the console font, topscreen display, and the menu
//#include "font.h"
#include "topscreen.h"
#include "menu.h"
//header of the counter
#define COUNTER_HEAD "goals: " //# of goals on next line
//number of files displayed by the in-game loader
#define NUM_FILES_DISPLAYED 22
//From LiraNuna =D
// Macro to translate 2D coord into 1D map array
#define POS2IDX(x, y) ((x) + ((y) * 32))
// Set of defines for tile flipping
#define TILE_FLIP_NONE (0<<10)
#define TILE_FLIP_X (1<<10)
#define TILE_FLIP_Y (2<<10)
#define TILE_FLIP_XY (TILE_FLIP_X | TILE_FLIP_Y)
// Macro for palette selecting
#define TILE_PALETTE(n) ((n)<<12)
#define CHAR2TILE(c) ((c) - 32)
/* Modes for the topscreen; debug mode is gone because it's in a split
* screen next to the info and load screens (for now)
*/
#define NDS_INFO_MODE -2
#define NDS_LOAD_MODE -1
#define NDS_START_MODE 0
//the range of the modes
#define NDS_MODE_MIN -2
#define NDS_MODE_MAX 0
//the background values for the vaious topscreen modes
//load and info both use the console (BG1)
#define LOAD_BG DISPLAY_BG0_ACTIVE
#define INFO_BG DISPLAY_BG0_ACTIVE
#define START_BG DISPLAY_BG3_ACTIVE
#else
#ifdef ECHO_PC
//opengl
#ifdef ECHO_OSX //OS X
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif
//used to attach the signal handler
#include <signal.h>
//for directory stuff
#include <libgen.h>
//Key code for keyboard
#define ESCAPE 27
#define ENTER 13
#elif ECHO_GCN || ECHO_WII
//using gl2gx to save code and time and pain and blood and suffering and...
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif
//format of the counter
#define COUNTER_HEAD "goals: %i" //# of goals appended
//number of files displayed by the in-game loader
#define NUM_FILES_DISPLAYED 31
//the number of frames the loader can scroll
#define LOAD_MAX 10.0f
//multiply by real_width to get the width of the loader screen
#define LOADER_WIDTH 1.5f
#endif
//status messages
#define MSG_READY "ready"
#define MSG_START "start"
#define MSG_PAUSE "thinking"
#define MSG_BLANK ""
//...yeah...
#define SUCCESS "success"
#define NO_GOALS "no goals"
//the number of frames the name of the stage fades out after starting (no fading in n-echo)
#define NAME_DISPLAY_MAX 30
//multiply by depth to get the row spacing of the text in the loader
#define FILE_SPACE_PER_DEPTH 0.06f
//the number of frames the status "start" fades out after starting
#define START_MAX 50
//rotational interval in degrees
#define ROTATE_ANG 5.0f
//--VARIABLES
#ifdef ECHO_NDS
//mode of the top screen (subscreen)
static int sub_mode = NDS_START_MODE;
//address of the "second console", used by the info and loader screens
//static u16* string_map = NULL;
static int controls_bg = -1;
static PrintConsole console, info;
//basic flags of the top screen
static const uint32 basic_modes = MODE_5_2D;
//the temp address of the counter (holds number of goals)
static char counter[32];
//what are the directional buttons? defaults to right-scheme
static int left_key = KEY_LEFT, right_key = KEY_RIGHT, up_key = KEY_UP, down_key = KEY_DOWN, b_key = KEY_B;
#else
#ifdef ECHO_PC
//ID of the window
static int window;
#endif
//the previous time "display" was called
static int prev_time = 0;
//is the loader toggled?; which frame is the loader in?
static int loading = 0, load_frame = 0;
//the temp address of the counter (holds number of goals)
static char* counter = NULL;
//was the counter allocated?
static int counter_alloc = 0;
//was this paused before the loader was toggled?
static int was_paused = 0;
#endif
//are we in menu mode or playing mode
static int menu_mode = 1;
//dimensions of window or screen (256x192 in NDS)
static int my_width = 0, my_height = 0;
//dimensions of viewing field (important to text)
static float real_width = 0, real_height = 0;
//depth; FILE_SPACE_PER_DEPTH * depth; dividend of character width = 150 / depth
static float depth = 0, file_space = 0, font_div = 0;
//on which file does the loader load?; what file is the top of the loader?
static int file_index = 0, file_start = 0;
//frame of the fading "start" status; frame of the fading name display
static int start_frame = 0, name_display = NAME_DISPLAY_MAX;
//the status message
static char* message = MSG_READY;
//cache of the name of the stage
static char* name_cache = NULL;
//did the dragging start already?; where did the dragging start?
static int touch_started = 0, start_x = 0, start_y = 0;
//the angle when the dragging started
static vector3f real_angle(0, 0, 0);
//the current directory
static echo_files* files = NULL;
//--METHODS
#ifdef ECHO_NDS
//controller
static void get_key();
//change the subscreen mode according to sub_mode
static void refresh_sub_mode();
//draw str at (x, y)
static void console2_draw_string(int x, int y, char* str);
//draw num characters (or until end of string) of str at (x, y)
static void console2_draw_string(int x, int y, char* str, int num);
//clear the screen
static void console2_clear();
//clear row y
static void console2_clear_row(int y);
//update console2 with the loader
static void update_loader();
//start info mode
static void toggle_info();
//update console2 with # of goals
static void update_num_goals();
//update console2 with status
static void update_char_state();
//tries to refresh the handedness
static void refresh_hand(echo_xml* doc);
#elif ECHO_PC
//mouse event, calls pressed or handles releases.
static void mouse(int button, int state, int x, int y);
//normal alphanumeric keys, such as p, l, a
static void key(unsigned char key, int x, int y);
//special keys esp. arrow keys
static void spec_key(int key, int x, int y);
//draw a string on screen
static int draw_string(float x, float y, const char *string);
static int draw_string(float x, float y, char *string);
//draw a file name (using size 12 font instead of size 12)
static int draw_fname_string(float x, float y, char *string);
//draw the status (twice as spaced out)
static int draw_message_string(float x, float y, char *string);
#endif
//mouse dragged
static void pointer(int x, int y);
//mouse pressed down
static void pressed(int x, int y);
//load the file
static void load(const char* fname);
//initialize
static void init(int argc, char **argv, int w, int h);
//resize the screen
static void resize(int w, int h);
//display the screen
static void display();
//directional control
static void up();
static void down();
static void left();
static void right();
//toggle pause
static void echo_pause();
//starts or pauses (the 'p' key on pc/mac/linux, shoulder button on nds)
static void start_or_pause();
static void main_deallocate();
static void signal_handler(int signal);
int main(int argc, char **argv)
{
atexit(main_deallocate);
#ifdef ECHO_NDS
srand(time(0));
//initialize the file system
fatInitDefault();
char* root = NULL;
if(echo_genroot(&root) == FAIL)
echo_error("Cannot allocate path string!");
//get the files
files = get_files(root);
//initialize the screens
init(argc, argv, 255, 191);
//*
ECHO_PRINT("trying to load prefs...\n");
echo_xml** doc = new(echo_xml*);
if(open_prefs(doc) == WIN)
{
ECHO_PRINT("loaded prefs...\n");
refresh_hand(*doc);
close_prefs(*doc);
}
else
ECHO_PRINT("couldn't load prefs!\n");
delete doc;
// */
//load the menu
load(NULL);
defaultExceptionHandler();
//infinite loop
while(1)
{
//get the key presses and touch screen, and refresh topscreen if in info or loader mode
get_key();
//otherwise the 3D will paint anyways, so don't paint over.
if(!menu_mode)
{
//display bottom screen
display();
//draw the image
glFlush(0);
}
//otherwise we won't sync
swiWaitForVBlank();
//for 30 fps, I need to skip a frame
swiWaitForVBlank();
}
#elif ECHO_PC
//fill lookup tables
init_math();
char* dir = NULL;
if(echo_execdir(&dir) == FAIL)
echo_error("couldn't get executable directory!!!\n");
files = get_files(dir);
//attach the signal handler
signal(SIGINT, signal_handler);
//if there is a cli argument
if(argc >= 2)
{
//if it is -h
if(!strcmp(argv[1], "-h"))
{
//print usage and exit gracefully
ECHO_PRINT("Usage: %s [-h | -t] [stage file name]\n", argv[0]);
ECHO_PRINT("\t-h\tprints this help message\n");
ECHO_PRINT("\t-t\tjust tests the stage file\n");
ECHO_PRINT("if no stage is specified, sample1.xml is loaded.\n");
std::exit(0);
}
//if it is -t
else if(!strcmp(argv[1], "-t"))
{
const stage* st = load_stage(argv[2]);
//test the file
if(st != NULL)
{
//ok
ECHO_PRINT("stage file OK\n");
delete st;
std::exit(0);
}
else
{
//no
ECHO_PRINT("stage file has errors...\n");
std::exit(1);
}
}
//else, just load the stage
else
load(argv[1]);
}
//else, open up with menu
else
{
load(NULL);
}
//initialize opengl
init(argc, argv, 640, 480);
//initialize prev time
prev_time = glutGet(GLUT_ELAPSED_TIME);
//start main loop
glutMainLoop();
#elif ECHO_GCN || ECHO_WII
//TODO: Wii/GCN port?
#endif
//never gonna reach here
return(1);
}
void main_deallocate()
{
ECHO_PRINT("main_deallocate: deallocating echo_ns\n");
echo_ns::deallocate();
ECHO_PRINT("main_deallocate: finished deallocating echo_ns\n");
#ifndef ECHO_NDS
if(counter_alloc == 1 && counter != NULL)
delete[] counter;
#endif
ECHO_PRINT("main_deallocate: deallocating files\n");
delete_echo_files(files);
ECHO_PRINT("main_deallocate: exiting...\n");
}
void signal_handler(int signal)
{
//we have already called atexit, so we don't need to call main_deallocate again
std::exit(signal);
}
#ifdef ECHO_NDS
void refresh_hand(echo_xml* doc)
{
HAND hand = RIGHT_HAND;
ECHO_PRINT("get hand started\n");
if(get_hand(doc, &hand) == WIN)
{
ECHO_PRINT("get hand ended\n");
if(hand == RIGHT_HAND)
{
ECHO_PRINT("right handed\n");
left_key = KEY_LEFT;
right_key = KEY_RIGHT;
up_key = KEY_UP;
down_key = KEY_DOWN;
b_key = KEY_B;
}
else
{
ECHO_PRINT("left handed\n");
left_key = KEY_Y;
right_key = KEY_A;
up_key = KEY_X;
down_key = KEY_B;
b_key = KEY_DOWN;
}
}
else
ECHO_PRINT("couldn't get handedness!\n");
}
#endif
static void load(const char* fname)
{
ECHO_PRINT("start of load\n");
stage* s = NULL;
if(fname != NULL)
{
ECHO_PRINT("before load_stage:%s,%s\n", files->current_dir, fname);
char* abs_path = echo_merge(files->current_dir, fname);
//load stage
ECHO_PRINT("starting load\n");
s = load_stage(abs_path);
delete[] abs_path;
ECHO_PRINT("after load_stage\n");
}
//if the stage file is bad (fname != NULL && s == NULL), fuhgeddaboutit!
if(s || !fname)
{
//if stage is valid
if(s)
{
//initialize to that stage
echo_ns::init(s);
#ifdef ECHO_NDS
//set menu off (though the 3d buffer seems to have priority anyways)
videoSetMode(MODE_5_3D);
#endif
//not in menu mode
menu_mode = 0;
//the distance to the farthest point plus 2.8 for good measure.
depth = echo_ns::current_stage->get_farthest() + 2.8f;
//so i don't have to call it all the time
name_cache = const_cast<char*>(echo_ns::current_stage->get_name()->c_str());
}
//if we want our menu
else
{
//init to null
echo_ns::init(NULL);
#ifdef ECHO_NDS
//display the menu
videoSetMode(MODE_5_3D | DISPLAY_BG3_ACTIVE);
#endif
//in menu mode
menu_mode = 1;
//the distance to the farthest point, but there is no points!
depth = 5;
name_cache = NULL;
}
//reset start and name frames
start_frame = 0;
name_display = NAME_DISPLAY_MAX;
//set status to ready
message = MSG_READY;
//set loader display variables
file_space = FILE_SPACE_PER_DEPTH * depth;
font_div = 150 / depth;
//resize, since we change the depth of the stage
resize(my_width, my_height);
}
}
#ifdef ECHO_NDS
//refresh the subscreen mode
static void refresh_sub_mode()
{
//if out of bounds, correct the mode.
if(sub_mode > NDS_MODE_MAX) sub_mode = NDS_MODE_MIN;
else if(sub_mode < NDS_MODE_MIN) sub_mode = NDS_MODE_MAX;
switch(sub_mode)
{
case NDS_LOAD_MODE: videoSetModeSub(MODE_5_2D | DISPLAY_BG1_ACTIVE); bgHide(controls_bg); update_loader(); break;
case NDS_INFO_MODE: videoSetModeSub(MODE_5_2D | DISPLAY_BG1_ACTIVE); bgHide(controls_bg); toggle_info(); break;
case NDS_START_MODE: videoSetModeSub(MODE_5_2D | DISPLAY_BG3_ACTIVE); bgShow(controls_bg); break;
default:
break;
}
}
#endif
static void init(int argc, char **argv, int w, int h)
{
#ifdef ECHO_NDS
//assign banks (H is use for ext. palette)
vramSetBankA(VRAM_A_MAIN_BG);
vramSetBankB(VRAM_B_MAIN_BG);
vramSetBankC(VRAM_C_SUB_BG);
vramSetBankD(VRAM_D_LCD);
vramSetBankE(VRAM_E_LCD);
vramSetBankF(VRAM_F_LCD);
vramSetBankG(VRAM_G_LCD);
vramSetBankH(VRAM_H_LCD);
vramSetBankI(VRAM_I_LCD);
//otherwise the topscreen would be the mainscreen
lcdSwap();
//Main Screen
//set mode
videoSetMode(MODE_5_3D | DISPLAY_BG3_ACTIVE);
//init
glInit();
//i heard it's not good for aa + outline, but going with it anyways...
glEnable(GL_ANTIALIAS);
//hardware-based outlining
glEnable(GL_OUTLINE);
//set outline colors for all groups
glSetOutlineColor(0, RGB15(0, 0, 0)); //unrolled loops ftw
glSetOutlineColor(1, RGB15(0, 0, 0));
glSetOutlineColor(2, RGB15(0, 0, 0));
glSetOutlineColor(3, RGB15(0, 0, 0));
glSetOutlineColor(4, RGB15(0, 0, 0));
glSetOutlineColor(5, RGB15(0, 0, 0));
glSetOutlineColor(6, RGB15(0, 0, 0));
glSetOutlineColor(7, RGB15(0, 0, 0));
glClearColor(31,31,31,31); //BG must be opaque for AA to work
glClearPolyID(63); //BG must have a unique polygon ID for AA to work
glClearDepth(0x7FFF);
// the menu
int menu_bg = bgInit(3, BgType_Bmp8, BgSize_B8_256x256, 5, 0);
dmaCopy(menuBitmap, bgGetGfxPtr(menu_bg), menuBitmapLen);
dmaCopy(menuPal, BG_PALETTE, menuPalLen);
bgSetPriority(menu_bg, 3);
//Sub Screen
videoSetModeSub(MODE_5_2D);
controls_bg = bgInitSub(3, BgType_Bmp8, BgSize_B8_256x256, 5, 0);
bgSetPriority(controls_bg, 3);
dmaCopy(topscreenBitmap, bgGetGfxPtr(controls_bg), topscreenBitmapLen);
dmaCopy(topscreenPal, BG_PALETTE_SUB, topscreenPalLen);
console = *consoleInit(NULL, 1, BgType_Text4bpp, BgSize_T_256x256, 31, 2, false);
info = *consoleInit(NULL, 1, BgType_Text4bpp, BgSize_T_256x256, 31, 0, false);
consoleSetWindow(&info, 16, 0, 16, 24);
consoleSetWindow(&console, 0, 0, 16, 24);
char *border =
"----------------"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"| |"
"----------------";
consoleSelect(&info);
iprintf(border);
consoleSelect(&console);
iprintf(border);
consoleSetWindow(&info, 17, 1, 14, 22);
consoleSetWindow(&console, 1, 1, 14, 22);
refresh_sub_mode();
//make sure console works
ECHO_PRINT("console init\n");
//set height to local cache (even though it's always 256x192)
my_width = w;
my_height = h;
#elif ECHO_PC
//init
glutInit(&argc, argv);
//even thought no color is use, alpha is used for fading fonts and character guy
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
//init window
glutInitWindowSize(w, h);
glutInitWindowPosition(0, 0);
window = glutCreateWindow("L-Echo");
//assign handlers
glutDisplayFunc(&display);
glutIdleFunc(&display);
glutReshapeFunc(&resize);
glutKeyboardFunc(&key);
glutSpecialFunc(&spec_key);
glutMouseFunc(&mouse);
glutMotionFunc(&pointer);
//basic stuff
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
//set our line
glEnable(GL_LINE_SMOOTH);
glLineWidth(2.5);
//blending for fading effects
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
resize(w, h);
#elif ECHO_GCN || ECHO_WII
//TODO: Wii/GCN port?
#endif
}
static void resize(int w, int h)
{
//can't have div by 0
if(h == 0) h = 1;
//change our viewport (window)
glViewport(0, 0, w, h);
my_width = w;
my_height = h;
//figure out the dimensions of our projection
if(w <= h)
{
real_width = depth;
real_height = depth * w / h;
}
else
{
real_width = depth * w / h;
real_height = depth;
}
//change to projection mode
glMatrixMode(GL_PROJECTION);
//load an identity matrix
glLoadIdentity();
//orthogonal projection or else it would look weird and you couldn't play this game
glOrtho(-real_width, real_width, -real_height, real_height, -depth, depth);
//shift back
glMatrixMode(GL_MODELVIEW);
}
// ----DRAW MAIN----
#ifdef ECHO_NDS
static void console2_draw_string(int x, int y, char* str)
{
ECHO_PRINT("\x1b[%d;%dH%s", y, x, str);
}
static void console2_draw_string(int x, int y, char* str, int num)
{
// "Cheap" solution
char c = str[num];
str[num] = '\0';
ECHO_PRINT("\x1b[%d;%dH%s", y, x, str);
str[num] = c;
}
static void console2_clear()
{
ECHO_PRINT("\x1b[2J");
}
static void console2_clear_row(int y)
{
ECHO_PRINT("\x1b[%d;0H ", y);
}
static void update_loader()
{
consoleSelect(&info);
console2_clear();
//draw the current directory on top
console2_draw_string(0, 0, files->current_dir, 14);
int each_file = 0;
while(each_file < files->num_files && each_file < 20)
{
//display an arrow next to the currently selected file
if(file_start + each_file == file_index)
console2_draw_string(0, 2 + each_file, "->");
console2_draw_string(2, 2 + each_file, files->file_names[file_start + each_file], 12);
each_file++;
}
consoleSelect(&console);
}
static void toggle_info()
{
consoleSelect(&info);
console2_clear();
//draw the stage name, since it's not going to change unless you switch to the loader
if(echo_ns::current_stage != NULL)
{
console2_draw_string(0, 0, "stage: ");
console2_draw_string(0, 1, name_cache, 14);
update_num_goals();
update_char_state();
}
else
{
console2_draw_string(0, 0, "no stage loaded");
}
consoleSelect(&console);
}
static void update_num_goals()
{
//console2_clear_row(10); console2_clear_row(11);
//console2_clear_row(12); console2_clear_row(13);
if(echo_ns::current_stage != NULL)
{
int goals_left = echo_ns::goals_left();
if(goals_left > 0)
{
//hmms, unlikely to buffer overflow, but what can I do about it anyways?
//and who the hell will have more than 10^32 goals?
//can that even fit in an int?
sprintf(counter, "%i", goals_left);
console2_draw_string(0, 10, COUNTER_HEAD);
console2_draw_string(0, 12, counter, 14);
}
//yay you finished!
else if(echo_ns::num_goals() > 0)
console2_draw_string(0, 11, SUCCESS);
//the stage had no goals to begin with
else
console2_draw_string(0, 11, NO_GOALS);
}
else
console2_draw_string(0, 11, NO_GOALS);
}
static void update_char_state()
{
//console2_clear_row(21); //console2_clear_row(22);
if(echo_ns::current_stage != NULL)
console2_draw_string(0, 21, message);
else
console2_draw_string(0, 21, "please load a stage");
}
#elif ECHO_PC
//copied from http://lighthouse3d.com/opengl/glut/index.php?bmpfontortho
static int draw_message_string(float x, float y, char *string)
{
if(string)
{
while(*string != '\0')
{
glRasterPos2f(x, y);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *string);
//twice as spaced out as the other strings
x += glutBitmapWidth(GLUT_BITMAP_HELVETICA_18, *string) / font_div * 2;
string++;
}
return(WIN);
}
return(FAIL);
}
static int draw_string(float x, float y, const char *string)
{
if(string)
{
int i = 0;
while(string[i] != '\0')
{
glRasterPos2f(x, y);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
x += glutBitmapWidth(GLUT_BITMAP_HELVETICA_18, string[i]) / font_div;
i++;
}
return(WIN);
}
return(FAIL);
}
static int draw_string(float x, float y, char *string)
{
if(string)
{
while(*string != '\0')
{
glRasterPos2f(x, y);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *string);
//twice as spaced out as the other strings
x += glutBitmapWidth(GLUT_BITMAP_HELVETICA_18, *string) / font_div;
string++;
}
return(WIN);
}
return(FAIL);
}
static int draw_fname_string(float x, float y, char *string)
{
if(string)
{
while(*string != '\0')
{
glRasterPos2f(x, y);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *string);
//twice as spaced out as the other strings
x += glutBitmapWidth(GLUT_BITMAP_HELVETICA_12, *string) / font_div;
string++;
}
return(WIN);
}
return(FAIL);
}
static void draw_HUD()
{
//status
if(message == MSG_START)
{
//fade...
if(start_frame > START_MAX)
message = MSG_BLANK;
else
{
glColor4f(0, 0, 0, 1.0f - start_frame * 1.0f / START_MAX);
start_frame++;
}
}
else if(message == MSG_PAUSE)
{
//make it pulsate, reusing variables ftw!
glColor4f(0, 0, 0, 0.4f * sin(TO_RAD(start_frame)) + 0.6f);
start_frame += 10;
//prevent overflow
if(start_frame == 36)
start_frame = 0;
}
//near the bottom left of the stage
draw_message_string(-0.8f * real_width, -0.9f * real_height, message);
//name of the stage
if(name_display > 0)
{
glColor3f(0, 0, 0);
if(name_display < NAME_DISPLAY_MAX)
{
//fading
glColor4f(0, 0, 0, name_display * 1.0f / NAME_DISPLAY_MAX);
name_display--;
}
//left side
draw_string(-0.9f * real_width, 0, name_cache);
}
//num goals left
int goals_left = echo_ns::goals_left();
if(goals_left > 0)
{
//not very precise, but oh well
counter = new char[(int)log(goals_left) + 10];
counter_alloc = 1;
sprintf(counter, COUNTER_HEAD, goals_left);
}
else if(echo_ns::num_goals())
{
counter = SUCCESS;
counter_alloc = 0;
}
else
{
counter = NO_GOALS;
counter_alloc = 0;
}
glColor3f(0, 0, 0);
//bottom left, above status
draw_string(-0.6f * real_width, -0.8f * real_height, counter);
if(goals_left > 0)
{
if(counter_alloc == 1)
delete[] counter;
counter = NULL;
}
}
static void draw_loader()
{
//if loading or the loader just isn't fully tucked away yet
if(loading || load_frame > 0)
{
//loader is being tucked away
if(loading == 0)
load_frame--;
//loader is coming out
else if(load_frame < LOAD_MAX)
load_frame++;
glLoadIdentity();
//translate the whole thing
glTranslatef(real_width * (1 - LOADER_WIDTH * load_frame / LOAD_MAX)
, 0, depth - 0.1f);
//the background of the loader
glColor3f(0, 0, 0);
glBegin(GL_QUADS);
{
glVertex3f(0, real_height, 0);
glVertex3f(real_width * LOADER_WIDTH, real_height, 0);
glVertex3f(real_width * LOADER_WIDTH, -real_height, 0);
glVertex3f(0, -real_height, 0);
}
glEnd();
//slightly in front
glTranslatef(0, 0, 0.05f);
//the current directory
glColor3f(1, 1, 1);
draw_string(0, real_height - 0.4f, files->current_dir);
//grey for files that are not selected
glColor3f(0.5f, 0.5f, 0.5f);
int each_file = 0;
//start 3 spaces from the top.
float each_y = real_height - 3 * file_space;
//while the each_y is less than the bottom,
//each_file less than the number of files supposed to be displayed on the screen
//and less than the number of files we actually have
while(each_y >= -real_height && each_file < NUM_FILES_DISPLAYED
&& each_file < files->num_files)
{
//highlight the selected