File size: 24,081 Bytes
00e6582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <cstdlib>
#include <GL/glew.h>"
#include "code/engine/engine.h"
#include "code/engine/text/text.h"
#include <vorbis/vorbisfile.h>
#include <alsa/asoundlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include "code/engine/emulator/basic.h"
#include "code/engine/neural_network/lstm.h"
#include "code/engine/neural_network/dnn.h"
#include "code/engine/neural_network/cnn.h"
#include "code/engine/neural_network/attn.h"
#include "code/engine/npc/npc.h""
#define MAGNIFICATION 1

struct _Keypad Keypad;

const float DUTY_TABLE[4] = {0.125f, 0.25f, 0.50f, 0.75f};
const int NOISE_DIVISORS[] = {8, 16, 32, 48, 64, 80, 96, 112};

// --- Music Data ---
typedef struct { uint16_t freq; int duration; } Note;
Note melody[] = { {1500, 8}, {1500, 8}, {1700, 8}, {1500, 8}, {1900, 8}, {1800, 16}, {1500, 8}, {0, 8} };
int cur_note = 0, note_wait = 0;


void tick_env(Envelope *e) {
    if (e->period == 0) return;
    if (e->timer > 0) e->timer--;
    if (e->timer == 0) {
        e->timer = e->period;
        if (e->direction && e->current_vol < 15) e->current_vol++;
        else if (!e->direction && e->current_vol > 0) e->current_vol--;
    }
}



void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if(Keypad.Controller_Mode == CONTROL_MODE_KEYBOARD_AND_MOUSE || Keypad.Controller_Mode == CONTROL_MODE_KEYBOARD_AND_MOUSE_AND_CONTROLLER)
    {
    	if(key == GLFW_KEY_ENTER && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_Start = true;
    	}

    	else if(key == GLFW_KEY_ENTER && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_Start = false;
    	}

    	if(key == GLFW_KEY_A && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_A = true;
    	}

    	else if(key == GLFW_KEY_A && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_A = false;
    	}



    	if(key == GLFW_KEY_B && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_B = true;
    	}

    	else if(key == GLFW_KEY_B && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_B = false;
    	}


    	if(key == GLFW_KEY_C && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_C = true;
    	}

    	else if(key == GLFW_KEY_C && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_C = false;
    	}


    	if(key == GLFW_KEY_D && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_D = true;
    	}

    	else if(key == GLFW_KEY_D && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_D = false;
    	}



    	if(key == GLFW_KEY_E && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_E = true;
    	}

    	else if(key == GLFW_KEY_E && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_E = false;
    	}





    	if(key == GLFW_KEY_F && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_F = true;
    	}

    	else if(key == GLFW_KEY_F && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_F = false;
    	}



    	if(key == GLFW_KEY_G && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_G = true;
    	}

    	else if(key == GLFW_KEY_G && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_G = false;
    	}


    	if(key == GLFW_KEY_R && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_R = true;
    	}

    	else if(key == GLFW_KEY_R && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_R = false;
    	}

    	if(key == GLFW_KEY_T && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_T = true;
    	}

    	else if(key == GLFW_KEY_T && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_T = false;
    	}




    	if(key == GLFW_KEY_Y && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_Y = true;
    	}

    	else if(key == GLFW_KEY_Y && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_Y = false;
    	}


    	if(key == GLFW_KEY_U && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_U = true;
    	}

    	else if(key == GLFW_KEY_U && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_U = false;
    	}



    	if(key == GLFW_KEY_W && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_W = true;
    	}

    	else if(key == GLFW_KEY_W && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_W = false;
    	}



    	if(key == GLFW_KEY_LEFT && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_Left = true;
    	}

    	else if(key == GLFW_KEY_LEFT && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_Left = false;
    	}



    	if(key == GLFW_KEY_RIGHT && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_Right = true;
    	}

    	else if(key == GLFW_KEY_RIGHT && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_Right = false;
    	}



    	if(key == GLFW_KEY_UP && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_Up = true;
    	}

    	else if(key == GLFW_KEY_UP && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_Up = false;
    	}



    	if(key == GLFW_KEY_DOWN && action == GLFW_PRESS)
    	{
        	Keypad.Keyboard_Down = true;
    	}

    	else if(key == GLFW_KEY_DOWN && action == GLFW_RELEASE)
    	{
        	Keypad.Keyboard_Down = false;
    	}

    }
}

static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
    Keypad.Mouse_X = xpos;
    Keypad.Mouse_Y = ypos;
}

void window_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width*MAGNIFICATION,height*MAGNIFICATION);
    Keypad.Screen_Width = width;
    Keypad.Screen_Height = height;
}


void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
    if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
    {
        Keypad.Mouse_Is_Clicked = true;
    }
    else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE)
    {
        Keypad.Mouse_Is_Clicked = false;
    }
}

void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
    if(yoffset > 0)
    {
        Keypad.Mouse_Scroll_Up = true;
        Keypad.Mouse_Scroll_Down = false;
    }

    else if(yoffset < 0)
    {
        Keypad.Mouse_Scroll_Up = false;
        Keypad.Mouse_Scroll_Down = true;
    }
}

void joystick_callback(int jid, int event)
{
    if (event == GLFW_CONNECTED)
    {
        // The joystick/gamepad was connected
        if (glfwJoystickIsGamepad(jid))
        {
            const char* name = glfwGetGamepadName(jid);
            //printf("Connected Joystick ");
            //printf(name);
            //printf(".\n");
        }
    }
}




struct _Audio_Chip* Audio_Chip = (struct _Audio_Chip*)calloc(1,sizeof(struct _Audio_Chip));



// Main Audio Callback Function
void audio_callback(void *userdata, Uint8 *stream, int len) {
    float *buffer = (float *)stream;
    int samples = len / sizeof(float);
    float dt = 1.0f / SAMPLE_RATE;

    SDL_LockMutex(Audio_Chip->audio_mutex);

    for (int s = 0; s < samples; s++) {
        float mixed_sample = 0.0f;

        for (int i = 0; i < 3; i++) {
            SID_Channel *ch = &Audio_Chip->channels[i];
            if (ch->env_state == ENV_IDLE) continue;

            // 1. ADSR Envelope Update
            switch (ch->env_state) {
                case ENV_ATTACK:
                    ch->env_level += (dt / ch->attack);
                    if (ch->env_level >= 1.0f) {
                        ch->env_level = 1.0f;
                        ch->env_state = ENV_DECAY;
                    }
                    break;
                case ENV_DECAY:
                    ch->env_level -= (dt / ch->decay);
                    if (ch->env_level <= ch->sustain) {
                        ch->env_level = ch->sustain;
                        ch->env_state = ENV_SUSTAIN;
                    }
                    break;
                case ENV_SUSTAIN:
                    ch->env_level = ch->sustain;
                    break;
                case ENV_RELEASE:
                    ch->env_level -= (dt / ch->release);
                    if (ch->env_level <= 0.0f) {
                        ch->env_level = 0.0f;
                        ch->env_state = ENV_IDLE;
                    }
                    break;
                default: break;
            }

            // 2. Frequency Calculation (C64 Formula: Fout = (Fn * Clk) / 16777216)
            // C64 Clock is ~1MHz. Simplified here to standard Hz map:
            double freq_hz = (ch->frequency * 0.0596);
            ch->phase += freq_hz * dt;
            if (ch->phase >= 1.0) ch->phase -= 1.0;

            // 3. Waveform Generation
            float raw_sample = 0.0f;
            if (ch->waveform & WAVE_TRIANGLE) {
                raw_sample = (ch->phase < 0.5) ? (4.0f * ch->phase - 1.0f) : (3.0f - 4.0f * ch->phase);
            } else if (ch->waveform & WAVE_SAWTOOTH) {
                raw_sample = (2.0f * ch->phase) - 1.0f;
            } else if (ch->waveform & WAVE_PULSE) {
                double duty = (double)ch->pulse_width / 4095.0;
                raw_sample = (ch->phase < duty) ? 1.0f : -1.0f;
            } else if (ch->waveform & WAVE_NOISE) {
                // Galois LFSR for SID pseudo-random noise
                if (ch->phase < dt * freq_hz) { // clock shift register
                    unsigned int bit0 = ((ch->noise_reg >> 22) ^ (ch->noise_reg >> 17)) & 1;
                    ch->noise_reg = ((ch->noise_reg << 1) | bit0) & 0x7FFFFF;
                }
                raw_sample = ((float)((ch->noise_reg >> 15) & 0xFF) / 127.5f) - 1.0f;
            }

            mixed_sample += raw_sample * ch->env_level * AMP_SCALE;
        }

        buffer[s] = mixed_sample;
    }

    SDL_UnlockMutex(Audio_Chip->audio_mutex);
}





void Process_Audio_Chip(struct _Audio_Chip Audio_Chip)
{

}






int main(int argc, char** argv)
{
    Write_Cutscene("scene");

    //Write_Character("resources/data/characters/chris/character_rosemary.dat");
    //BMPtoSEI("resources/artwork/gui/demo.bmp","resources/artwork/gui/demo.sei");

   // Write_ROM_Memory_To_Program("test.rom");
  // char Text[256];
  // memset(Text,0,256);
 //  sprintf(Text, "The quick brown fox`jumps");

  //  Encode_Text_As_File(Text, 25, "test.aqbenc");

 //   Load_Image_And_Convert_To_Bitmap("test.bmp", "test.dat");
  //  Load_Image_And_Convert_To_Bitmap("test2.bmp", "test2.dat");
  //  Load_Image_And_Convert_To_Bitmap("eng_text_tilemap.bmp", "eng_text_tilemap.dat");
  //  if(!Compile_AQ_BASIC_Program("code.aqb", "test.rom"))
  //  {
  //      printf("failed\n");
 //  }


    SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO );

    Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 );
    glfwInit();

    BMPtoSEI("resources/data/arcs/the_days_of_nova/english/icons/test.bmp","resources/data/arcs/the_days_of_nova/english/icons/test.sei");

    struct _Engine* Engine = Create_Engine();
    Keypad.Controller_Mode = CONTROL_MODE_KEYBOARD_AND_MOUSE;
    Engine->Control_Mode = Keypad.Controller_Mode;

    Engine->W = glfwCreateWindow(640,360, "Cruiser Submarine Sonichu Demorgasbord", NULL, NULL); //SCREEN SIZE
    glfwMakeContextCurrent(Engine->W);
    glfwSwapInterval(1); //
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
    glfwWindowHint(GLFW_DOUBLEBUFFER, GL_TRUE);
    glViewport(0, 0, 640*MAGNIFICATION,360*MAGNIFICATION); //MAGNIFICATION
    glfwSetWindowSizeCallback(Engine->W, window_size_callback);

    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

    glewInit();

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 640,360, 0.0, 1.0, -1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glfwSetKeyCallback(Engine->W, key_callback);
    glfwSetCursorPosCallback(Engine->W, cursor_position_callback);
    glfwSetMouseButtonCallback(Engine->W, mouse_button_callback);
    glfwSetScrollCallback(Engine->W, scroll_callback);
    glfwSetJoystickCallback(joystick_callback);
    glClearColor(0.f,0.f, 0.f, 1.f );
    glClear(GL_COLOR_BUFFER_BIT);

    //BMPtoSEI("resources/models/texture_rosemary.bmp","resources/models/texture_rosemary.sei");








    init_sid(Audio_Chip);

    SDL_AudioSpec want, have;
    SDL_zero(want);
    want.freq = SAMPLE_RATE;
    want.format = AUDIO_F32SYS;
    want.channels = 1;
    want.samples = 512;
    want.callback = audio_callback;

    SDL_AudioDeviceID audio_device = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
    if (audio_device == 0) {
        printf("Failed to open audio: %s\n", SDL_GetError());
        return 1;
    }

    SDL_PauseAudioDevice(audio_device, 0); // Start audio

   // printf("--- C64 SID Chip Emulator Loaded ---\n");
   // printf("Keys Q, W, E -> Trigger Channel 1 (Sawtooth)\n");
   // printf("Keys A, S, D -> Trigger Channel 2 (Pulse/Square)\n");
   // printf("Keys Z, X, C -> Trigger Channel 3 (Noise Mode)\n");
   // printf("Press ESC to exit.\n");

    bool running = true;








    Initialize_Engine(Engine);
    //Engine->Interim_State = Create_Interim_State();
    printf("Build ID: %d\n", Engine->Settings.Build_ID);
    double xpos, ypos;

    const float* axes;
    const unsigned char* buttons;

    int joystickId = GLFW_JOYSTICK_1;
    if(glfwJoystickPresent(joystickId))
    {
        const char* name = glfwGetJoystickName(joystickId);
    }

    //CreateLSTM();
    //CreateDNN();
   // CreateCNN();
    //CreateAttn();
    //Test_Vision_Lobe();
    //Test_Enemy_Tracking_Lobe();
    //Test_Ally_Tracking_Lobe();
    //Test_Self_Status_Lobe();
   // Test_Parent_Status_Lobe();
    //CreateLSTM();

    //struct _Pilot_Artificial_Brain* PAB = Create_Pilot_Brain();
    //Initialize_Pilot_Brain(Engine, PAB);

    Write_File_System("disk.img");

    while (!glfwWindowShouldClose(Engine->W))
    {
        glfwGetCursorPos(Engine->W, &xpos, &ypos);
    	glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

    	switch(Engine->Current_State)
		{
            case STATE_TITLE_CUTSCENE:
    		Render_Title_Cutscene_State(Engine);
    		Input_Title_Cutscene_State(Engine, Keypad);
    		Process_Title_Cutscene_State(Engine);
    		break;

    		case STATE_TITLE:
    		Render_Title_State(Engine);
    		Input_Title_State(Engine, Keypad);
    		Process_Title_State(Engine);
    		break;

    		case STATE_MAIN_MENU:
    		Render_Main_Menu_State(Engine);
    		Input_Main_Menu_State(Engine, Keypad);
    		Process_Main_Menu_State(Engine);
    		break;

    		case STATE_ARC_SELECT:
    		Render_Arc_Select_State(Engine);
    		Input_Arc_Select_State(Engine, Keypad);
            Process_Arc_Select_State(Engine);
    		break;

    		case STATE_OVERWORLD:
    		Render_Overworld_State(Engine);
    		Input_Overworld_State(Engine, Keypad);
    		Process_Overworld_State(Engine);
    		break;

    		case STATE_ON_FOOT:
    		Render_On_Foot_State(Engine, Audio_Chip);
            Input_On_Foot_State(Engine, Keypad, Audio_Chip);
    		Process_On_Foot_State(Engine, Audio_Chip);
    		break;

    		case STATE_INTERIM:
    		//Process_Interim_State(Engine);
    		break;

    		case STATE_CUTSCENE:
    		Render_Cutscene_State(Engine);
            Input_Cutscene_State(Engine, Keypad);
    		Process_Cutscene_State(Engine);
    		break;

    		case STATE_ARC_COMPLETE:
    		Render_Arc_Complete_State(Engine);
            Handle_Arc_Complete_State_Inputs(Engine, Keypad);
    		Process_Arc_Complete_State(Engine);
    		break;

    		case STATE_NN_TRAIN:
    		Render_Neural_Network_Training_State(Engine);
            Handle_Neural_Network_Training_State_Inputs(Engine, Keypad);
    		Process_Neural_Network_Training_State(Engine);
    		break;

    		case STATE_EM_VIEW:
    		Render_Emulator_Viewer_State(Engine);
            Input_Emulator_Viewer_State(Engine, Keypad);
    		Process_Emulator_Viewer_State(Engine, Audio_Chip);
    		break;

    		case STATE_THE_END:
    		Render_The_End_State(Engine);
            Input_The_End_State(Engine, Keypad);
    		Process_The_End_State(Engine);
    		break;
		}
        Keypad.Mouse_Scroll_Up = false;
        Keypad.Mouse_Scroll_Down = false;
	glfwSwapBuffers(Engine->W);


            glfwPollEvents();
           // //printf("Controller Connected: %s\n\n", name);
            // 5. Read Axes (Thumbsticks, Analog Triggers)
            if (glfwJoystickPresent(joystickId))
	{
            int axesCount;
            axes = glfwGetJoystickAxes(joystickId, &axesCount);

            Keypad.Controller.Controller_Primary_Thumbpad[0] = axes[0]; //Axis 0 and 1
            Keypad.Controller.Controller_Primary_Thumbpad[1] = axes[1];

            Keypad.Controller.Controller_Camera_Thumbpad[0] = axes[3];  //Axis 3 and 4
            Keypad.Controller.Controller_Camera_Thumbpad[1] = axes[4];

            if(Keypad.Controller.Controller_Camera_Thumbpad[0] < 0.06 && Keypad.Controller.Controller_Camera_Thumbpad[0] > -0.06)
            {
                Keypad.Controller.Controller_Camera_Thumbpad[0] = 0;
            }

            if(Keypad.Controller.Controller_Camera_Thumbpad[1] < 0.06 && Keypad.Controller.Controller_Camera_Thumbpad[1] > -0.06)
            {
                Keypad.Controller.Controller_Camera_Thumbpad[1] = 0;
            }

            Keypad.Controller.Controller_Triggers[0] = axes[2];  //Axis 2 and 5
            Keypad.Controller.Controller_Triggers[1] = axes[5];

            ////printf("--- Axes (%d) ---\n", axesCount);
            for (int i = 0; i < axesCount; i++) {
                // Axes range from -1.0 to 1.0
               // //printf("  Axis %d: % .2f\n", i, axes[i]);
            }

            // 6. Read Buttons (A, B, X, Y, Bumpers, D-Pad, etc.)
            int buttonCount;
            buttons = glfwGetJoystickButtons(joystickId, &buttonCount);

            if(buttons[11] == GLFW_PRESS || Keypad.Controller.Controller_Primary_Thumbpad[1] <= -0.9)
            {
                ////printf("Up BUTTON BEING PRESSED!\n");
                Keypad.Controller.Controller_Up = true;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Keyboard_Up = true;
                }
            }

            else if(buttons[11] == GLFW_RELEASE)
            {
                ////printf("Up BUTTON NOT BEING PRESSED!\n");
                Keypad.Controller.Controller_Up = false;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Keyboard_Up = false;
                }
            }

            if(buttons[13] == GLFW_PRESS || Keypad.Controller.Controller_Primary_Thumbpad[1] >= 0.9)
            {
                ////printf("Down BUTTON BEING PRESSED!\n");
                Keypad.Controller.Controller_Down = true;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Keyboard_Down = true;
                }
            }

            else if(buttons[13] == GLFW_RELEASE)
            {
                ////printf("Down BUTTON NOT BEING PRESSED!\n");
                Keypad.Controller.Controller_Down = false;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Keyboard_Down = false;
                }
            }



            if(buttons[12] == GLFW_PRESS)
            {
                ////printf("RIGHT BUTTON BEING PRESSED!\n");
                Keypad.Controller.Controller_Right = true;
            }

            else if(buttons[12] == GLFW_RELEASE)
            {
                ////printf("RIGHT BUTTON NOT BEING PRESSED!\n");
                Keypad.Controller.Controller_Right = false;
            }

            if(buttons[14] == GLFW_PRESS)
            {
                ////printf("LEFT BUTTON BEING PRESSED!\n");
                Keypad.Controller.Controller_Left = true;
            }

            else if(buttons[14] == GLFW_RELEASE)
            {
                ////printf("Left BUTTON NOT BEING PRESSED!\n");
                Keypad.Controller.Controller_Left = false;
            }




            if(buttons[0] == GLFW_PRESS)
            {
                ////printf("B BUTTON BEING PRESSED!");
                Keypad.Controller.Controller_B = true;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    //Keypad.Keyboard_Start = true;
                }
            }
            else if(buttons[0] == GLFW_RELEASE)
            {
                ////printf("B BUTTON not BEING PRESSED!");
                Keypad.Controller.Controller_B = false;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    //Keypad.Keyboard_Start = false;
                }
            }
            if(buttons[1] == GLFW_PRESS)
            {
                ////printf("a BUTTON BEING PRESSED!");
                Keypad.Controller.Controller_A = true;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Keyboard_A = true;
                }
            }
            else if(buttons[1] == GLFW_RELEASE)
            {
                ////printf("a BUTTON not BEING PRESSED!");
                Keypad.Controller.Controller_A = false;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Keyboard_A = false;
                }
            }





            if(buttons[2] == GLFW_PRESS)
            {
                ////printf("Y BUTTON BEING PRESSED!");
                Keypad.Controller.Controller_Y = true;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Mouse_Is_Clicked = true;
                }
            }
            else if(buttons[2] == GLFW_RELEASE)
            {
                ////printf("Y BUTTON not BEING PRESSED!");
                Keypad.Controller.Controller_Y = false;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Mouse_Is_Clicked = false;
                }
            }
            if(buttons[3] == GLFW_PRESS)
            {
                ////printf("X BUTTON BEING PRESSED!");
                Keypad.Controller.Controller_X = true;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Keyboard_Start = true;
                }
            }
            else if(buttons[3] == GLFW_RELEASE)
            {
                ////printf("X BUTTON not BEING PRESSED!");
                Keypad.Controller.Controller_X = false;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Keyboard_Start = false;
                }
            }


            if(buttons[4] == GLFW_PRESS)
            {
                ////printf("L DIGITRIGGER BUTTON BEING PRESSED!");
                Keypad.Controller.Controller_LTrigger = true;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Mouse_Scroll_Down = true;
                }
            }
            else if(buttons[4] == GLFW_RELEASE)
            {
                ////printf("L DIGITRIGGER BUTTON not BEING PRESSED!");
                Keypad.Controller.Controller_LTrigger = false;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Mouse_Scroll_Down = false;
                }
            }

            if(buttons[5] == GLFW_PRESS)
            {
                ////printf("R DIGITRIGGER BUTTON BEING PRESSED!");
                Keypad.Controller.Controller_RTrigger = true;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Mouse_Scroll_Up = true;
                }
            }
            else if(buttons[5] == GLFW_RELEASE)
            {
                ////printf("R DIGITRIGGER BUTTON not BEING PRESSED!");
                Keypad.Controller.Controller_RTrigger = false;
                if(Keypad.Controller_Mode == CONTROL_MODE_CONTROLLER)
                {
                    Keypad.Mouse_Scroll_Up = false;
                }
            }


            ////printf("\n");
        }
    }
    Mix_Quit();
//    SDL_CloseAudioDevice(dev);
    SDL_Quit();
    return 0;
}