Handmade Hero»Forums»Code
90 posts
Time problem
Edited by C_Worm on Reason: Initial post
Hey i have a Timer and Clock struct and try to implement a timer.

The Clock just returns the seconds elapsed from start to the Time that does the calculation.

I have created 2 Timers where i set the first to 1.0f startTime and the second to like 5.0f or 10000.0f start time.

My problem is that the timers dont count down synchronous if they have different start times. I checked in the debbuger that the floating points changed differently and there four when i cast them to (int) they dont always loose time at the same rate.

The timers do work if they have the same start value.

any ideas how to solve this?

Clock:

 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
struct Clock
{
	double startTime;
	double endTime;
	double dt_s;
	double dt_ms;

	char txt_CountDown[100];

	double timeSinceStart()
	{
		return startTime;
	}

	double getMiliSeconds()
	{
		return dt_ms;
	}
	double getSeconds()
	{
		return dt_s;
	}
	
	void start()
	{
		dt_s = startTime - endTime;
		dt_ms = dt_s * 1000.0f;
		endTime = startTime;
		startTime = glfwGetTime();
	}
};



Timer:

 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
struct Timer
{
	float startTime = 0;
	float timeLeft = 0;
	float timePassed = 0;
	int waitBreak = 50;

	void SetStartTime(float in_startTime)
	{
		startTime = in_startTime + 0.999f;
	}

	void Begin(double in_timePassed)
	{

		if(timeLeft < 0.001f)
		{
			timeLeft = startTime;
		}
		else
		{
			timeLeft -= in_timePassed;
		}

	}
	void Reset()
	{
		timeLeft = startTime;
	}

};



code excerpts from 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
        ...... Line 532
	Timer timer1 = {};
	Timer timer2 = {};
	timer1.SetStartTime(1.0f);
	timer2.SetStartTime(5.0f);

	while(!glfwWindowShouldClose(window) && running) 
	{
        	
		clock.start();

                ...... Line 750
	        char txt_timer1[50] = {};
		char txt_timer2[50] = {};
		char txt_PlayerTurn[50] = {};
		char txt_PlayerInfo[50] = {};
		static i32 playerTurn = 1;

		timer1.Begin(clock.getSeconds());
		timer2.Begin(clock.getSeconds());
                
                ..... Line 787
	        sprintf(txt_timer1, 			"Time1 Left: %d", 	(i32)timer1.timeLeft);
	        sprintf(txt_timer2, 			"Time2 Left: %d", 	(i32)timer2.timeLeft);

            
	        printText(BCD, gameBuffer, &currTxtIndex, 		0.0f, 100.0f, 1.0f, txt_timer1, color4f(1.0f, 1.0f, 1.0f, 1.0f));
		printText(BCD, gameBuffer, &currTxtIndex, 		0.0f, 150.0f, 1.0f, txt_timer2, color4f(1.0f, 1.0f, 1.0f, 1.0f));

        }


Full code from 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
#include "glad\glad.c"
#include "GLFW\glfw3.h"

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <errno.h>
#include <stdint.h>

// 1 byte = 8 bits 		1111 1111
// 1 signed byte (char)
typedef int8_t 		i8;
// 1 unsigned byte (unsigned char)
typedef uint8_t 	u8;

// 2 signed bytes (short) 	1111 1111 1111 1111
typedef int16_t 	i16;
// 2 unsigned bytes (unsigned short) 
typedef uint16_t 	u16;

// 4 signed bytes (int) 	1111 1111 1111 1111	1111 1111 1111 1111
typedef int32_t 	i32;
// 4 unsigned bytes (unsigned int)
typedef uint32_t 	u32;

// 8 signed bytes (long long) 	1111 1111 1111 1111	1111 1111 1111 1111 	1111 1111 1111 1111	1111 1111 1111 1111
typedef int64_t 	i64;
// 8 unsigned bytes (unsigned long long)
typedef uint64_t 	u64;

#define STB_TRUETYPE_IMPLEMENTATION
#include "include\stb_truetype.h"

#define STB_IMAGE_IMPLEMENTATION
#include "include\stb_image.h"

#include <CJGL_Input.h>
#include "include\CJ_Structs.h"
#include "include\CJ_GameBuffer.h"
#include "include\CJ_Window.h"
#include "include\CJ_Shader.h"
#include "include\CJ_Defines.h"


GLenum err;
bool running = true;

struct GameObject
{
	float x;
	float y;
	float w;
	float h;

	u32 renderIndex;

	CJ_Vector2f texDiv;
	CJ_Vector2f texFrag;

	CJ_Vector4f solidColor;

	void init(float in_x, float in_y, float in_w, float in_h) 
	{
		x = in_x;
		y = in_y;
		w = in_w;
		h = in_h;
	}
};

#include "include\CJ_FuncDecl.h"
#include "include\CJ_FuncDef.h"

struct Player
{
	char txt_Territories[50] = {};
	char txt_Tanks[50] = {};
	char txt_GoldCollector[50] = {};
	char txt_Barracks[50] = {};

	i32 Territories = 0;
	i32 Tanks = 0;
	i32 GoldCollector = 0;
	i32 Barracks = 0;



};
void writer(int *cursor, char *input, int textBufferSize)
{
	// Upper case Chars, not using tolower();
	for(int character = 0; character <= 512; character++)
	{
		if(keyPressed[character] && keyPressed[LEFT_SHIFT])
		{
			if((int)isPressed[character] == 0)
			{
				if(*cursor < textBufferSize)
				{
					if(keyPressed[SPACEBAR])
					{
						input[*cursor] = ' ';
						++*cursor;
					}
					else if(keyPressed[BACKSPACE])
					{
						if(*cursor > 0)
						{
							*cursor = *cursor - 1;
							input[*cursor] = ' ';
						}
					}
					else if(keyPressed[UP_ARROW])
					{
					}
					else if(keyPressed[DOWN_ARROW])
					{
					}
					else if(keyPressed[LEFT_ARROW])
					{
					}
					else if(keyPressed[RIGHT_ARROW])
					{
					}
					else
					{
						input[*cursor] = (char)character;
						++*cursor;
					}
				}

				isPressed[character] = 1;
			}
		}
		// Same code as above but with tolower((char)character); for lower case chars.
		else if(keyPressed[character])
		{
			if((int)isPressed[character] == 0)
			{
				if(*cursor < textBufferSize)
				{
					if(keyPressed[SPACEBAR])
					{
						input[*cursor] = ' ';
						++*cursor;
					}
					else if(keyPressed[BACKSPACE])
					{
						if(*cursor > 0)
						{
							*cursor = *cursor - 1;
							input[*cursor] = ' ';
						}
					}
					else if(keyPressed[UP_ARROW])
					{
					}
					else if(keyPressed[DOWN_ARROW])
					{
					}
					else if(keyPressed[LEFT_ARROW])
					{
					}
					else if(keyPressed[RIGHT_ARROW])
					{
					}
					else
					{
						input[*cursor] = tolower((char)character);
						++*cursor;
					}
				}

				isPressed[character] = 1;
			}

		}
		else
		{
			isPressed[character] = 0;
		}

	}
}




int main()
{
	printf("GROUND_TILE_FIRST: %d\n", GROUND_TILE_FIRST);
	printf("GROUND_TILE_LAST: %d\n", GROUND_TILE_LAST);
	printf("TEXT_FIRST: %d\n", TEXT_FIRST);
	printf("TEXT_LAST: %d\n", TEXT_LAST);
	printf("SELECT_MENU_FIRST: %d\n", SELECT_MENU_FIRST);
	printf("SELECT_MENU_LAST: %d\n", SELECT_MENU_LAST);

	printf("char: %d\n", 		sizeof(char));
	printf("short: %d\n",	 	sizeof(short));
	printf("int: %d\n",  	   	sizeof(int));
	printf("long long: %d\n", 	sizeof(long long));

	printf("i8: %d\n",  sizeof(i8));
	printf("i16: %d\n", sizeof(i16));
	printf("i32: %d\n", sizeof(i32));
	printf("i64: %d\n", sizeof(i64));


	srand(time(NULL));
	

	// SET LAST PARAMETER TO 1 FOR FULLSCREN
	GLFWwindow *window = windowSetup(SCR_WIDTH, SCR_HEIGHT, "BoardGame", 0);

	unsigned int shaderProgram 		= compileShaderProgram("shader\\vertexShader.glsl", "shader\\fragmentShader.glsl");
	unsigned int textShaderProgram 		= compileShaderProgram("shader\\vertexShader.glsl", "shader\\textFS.glsl");
	unsigned int solidColorRectProgram 	= compileShaderProgram("shader\\vertexShader.glsl", "shader\\solidColorFS.glsl");

	VBOBundle vboB = {};

	GLfloat MAIN_GAMEBUFFER[NUMOBJECTS * 16] = {};

	GLuint indexBuffer[NUMOBJECTS * 6] = {};

	//0, 1, 2, 2, 3, 0    4, 5, 6, 6, 7, 4

	for(int i = 0; i < NUMOBJECTS; i++)
	{
		indexBuffer[0 + i * 6] = i * 4 + 0;
		indexBuffer[1 + i * 6] = i * 4 + 1;
		indexBuffer[2 + i * 6] = i * 4 + 2;
		indexBuffer[3 + i * 6] = i * 4 + 2;
		indexBuffer[4 + i * 6] = i * 4 + 3;
		indexBuffer[5 + i * 6] = i * 4 + 0;
	}

	glGenVertexArrays(1, &vboB.VAO);
	glGenBuffers(1, &vboB.posVBO);
	glGenBuffers(1, &vboB.colVBO);
	glGenBuffers(1, &vboB.texVBO);
	glGenBuffers(1, &vboB.IBO);

	glBindVertexArray(vboB.VAO);

	glBindBuffer(GL_ARRAY_BUFFER, vboB.posVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(MAIN_GAMEBUFFER), 0, GL_STREAM_DRAW);

	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);

	glBindBuffer(GL_ARRAY_BUFFER, vboB.colVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(MAIN_GAMEBUFFER), 0, GL_DYNAMIC_DRAW);

	glEnableVertexAttribArray(3);
	glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);

	glBindBuffer(GL_ARRAY_BUFFER, vboB.texVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(MAIN_GAMEBUFFER), 0, GL_DYNAMIC_DRAW);

	glEnableVertexAttribArray(7);
	glVertexAttribPointer(7, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
	
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboB.IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexBuffer), indexBuffer, GL_STATIC_DRAW);

	GameBuffer gameBuffer[NUMOBJECTS] = {};
	for(int i = 0; i < NUMOBJECTS; i++)
	{
		gameBuffer[i].bufferIndex = i;
	}

	// LOADING TEXTURE FILES
	u8 *data[10] = {};
	TextureInfo textureInfo[10] = {};
	textureInfo[0].filePath = "..\\assets\\mount.bmp";
	textureInfo[1].filePath = "..\\assets\\myPng.png";
	textureInfo[2].filePath = "..\\assets\\Bases.png";
	
	for(int i = 0; i < 3; i++)
	{
		loadTextures(&textureInfo[i], &data[i]);
	}

	stbtt_bakedchar BCD[96] = {};
	unsigned char fontBuffer[30000000] = {};
	const float fontMapWidth  = SCR_WIDTH;
	const float fontMapHeight = 200;
	unsigned char fontData[SCR_WIDTH*SCR_HEIGHT] = {};

//	FILE *hFontFile = fopen("C:\\windows\\fonts\\times.ttf", "rb");
	FILE *hFontFile = fopen("C:\\windows\\fonts\\cour.ttf", "rb");
//	FILE *hFontFile = fopen("C:\\CJ_FONTS\\FFF_Tusj.ttf", "rb");
	if(!hFontFile)
	{
		printf("fail open file\n");
	}

	fread(fontBuffer, 1, sizeof(fontBuffer), hFontFile);
	stbtt_BakeFontBitmap(fontBuffer, 0, 24.0f, fontData, fontMapWidth, fontMapHeight, 32, 126, BCD);

	fclose(hFontFile);
	
	// LOADING TEXTURE ID'S
	GLuint textureID[10] = {};
	for(int i = 0; i < 10; i++)
	{
		glGenTextures(1, &textureID[i]);
	}


	glBindTexture(GL_TEXTURE_2D, textureID[0]);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureInfo[0].width, textureInfo[0].height, 0, GL_RGB, GL_UNSIGNED_BYTE, data[0]);
	glGenerateMipmap(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, textureID[1]);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureInfo[1].width, textureInfo[1].height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data[1]);
	glGenerateMipmap(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, textureID[2]);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, fontMapWidth, fontMapHeight, 0, GL_RED, GL_UNSIGNED_BYTE, fontData);

	glBindTexture(GL_TEXTURE_2D, textureID[3]);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureInfo[2].width, textureInfo[2].height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data[2]);
	glGenerateMipmap(GL_TEXTURE_2D);



	stbi_image_free(data[0]);
	stbi_image_free(data[1]);
	stbi_image_free(data[2]);

	double mouseCurPosX = 0;
	double mouseCurPosY = 0;
	char DEBUGMouseCurPos[220] = {};
	char DEBUGFramTime[220] = {};
	char DEBUGWindowDim[220] = {};
	char DEBUGTimeSinceStart[220] = {};
	float *pPosData = 0;
	float *pColData = 0;
	float *pTexData = 0;
	int winWidth = 0;
	int winHeight = 0;
	int winX = 0;
	int winY = 0;
	int iColorLoc = glGetUniformLocation(shaderProgram, "iColorData");
	int winDimLoc = glGetUniformLocation(shaderProgram, "windowDimensions");
	int textWinDimLoc = glGetUniformLocation(textShaderProgram, "windowDimensions");
	int solidColWindDimLoc = glGetUniformLocation(solidColorRectProgram, "windowDimensions");
	float textX = 0.0f;

	gameBuffer[GROUND_MAP].createRect(0.0f, 0.0f, SCR_WIDTH * 1, SCR_HEIGHT * 1);
	gameBuffer[GROUND_MAP].setTextureDivision(1.0f, 1.0f);
	gameBuffer[GROUND_MAP].pickTextureFragment(0.0f, 0.0f);
	gameBuffer[GROUND_MAP].setSolidColor(1.0f, 1.0f, 1.0f, 0.5f); 


	for ( int y = 0; y < NUMTILES_16_y; y++)
	{
		for ( int x = 0; x < NUMTILES_16_x; x++)
		{
			static int index = 0;
			gameBuffer[GROUND_TILE_FIRST + index].createRect(0.0f + x * TILE_WIDTH, 0.0f +  y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
			gameBuffer[GROUND_TILE_FIRST + index].setTextureDivision(14.0f, 1.0f);
			gameBuffer[GROUND_TILE_FIRST + index].pickTextureFragment(3.0f, 0.0f);
			gameBuffer[GROUND_TILE_FIRST + index].setSolidColor(1.0f, 1.0f, 1.0f, 1.0f);
			index++;
		}
	}

	gameBuffer[SELECT_MENU_FIRST].createRect(0.0f, 0.0f, 100.0f, 100.0f);
	gameBuffer[SELECT_MENU_FIRST].setTextureDivision(14.0f, 1.0f);
	gameBuffer[SELECT_MENU_FIRST].pickTextureFragment(0.0f, 0.0f);
	gameBuffer[SELECT_MENU_FIRST].setSolidColor(1.0f, 1.0f, 1.0f, 1.0f); 

	float texPosX = 0.0f;
	float texPosY = 0.0f;

	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	Clock clock = {};
	int MENU_STATE = MENU_STATE_MAIN;

	// Store the index range for the text quads
	textIndexRange DBGStartGametxt = {};
	textIndexRange DBGWinDim = {};
	textIndexRange DBGMouseCur = {};

	int rect1 = 10000;
	int rect1MovX = 0.0f;
	int rect1MovY = 0.0f;
	int rect2 = 10001;
	int rect2MovX = 0.0f;
	int rect2MovY = 0.0f;

	gameBuffer[rect1].createRect(0.0f, 0.0f, 100.0f, 200.0f);
	gameBuffer[rect1].setSolidColor(1.0, 0.0f, 0.0f, 1.0f);
	gameBuffer[rect2].createRect(600.0f, 600.0f, 100.0f, 100.0f);
	gameBuffer[rect2].setSolidColor(0.0, 1.0f, 0.0f, 1.0f);
	
	mapObject(rect1, rect2, vboB, gameBuffer);  

	char txt_ENTER[200] = {};
	char txt_INPUT[200] = {};
	int txt_CURSOR = 0;
	int textBufferSize = 200;

	bool pressed = false;
	char trueSTATE[10] = "false";


	int stateOfTheGame 	= 0;
	const int mainMenu 	= 1;
	const int newGame 	= 2;
	const int sandBox 	= 9;
	stateOfTheGame = mainMenu;
	

	GameObject mainBase[2] = {};
	GameObject smallBase[9] = {};
	GameObject road[14] = {};
	GameObject rightSideMenu = {};
	GameObject rightSideMenuCollisionRect[2] = {};

	Player player[2] = {};


	mainBase[0].init(200.0f, 450.f, 128.0f, 128.0f);
	mainBase[0].texDiv 	= cj_vec2f(8.0f, 16.0f);
	mainBase[0].texFrag 	= cj_vec2f(0.0f, 2.0f);

	mainBase[1].init(SCR_WIDTH - (128.0f + 200.0f), 450.f, 128.0f, 128.0f);
	mainBase[1].texDiv 	= cj_vec2f(8.0f, 16.0f);
	mainBase[1].texFrag 	= cj_vec2f(0.0f, 3.0f);


	i32 increment = 0;
	
	for(i32 i = 0; i < 3; i++)
	{
		smallBase[i].init((mainBase[0].x + mainBase[0].w) * 1.7, 200.0f + (increment * 280.0f), 64.0f, 64.0f);
		smallBase[i].texDiv = cj_vec2f(8.0f, 16.0f);
		smallBase[i].texFrag = cj_vec2f(0.0f, 0.0f);

		increment++;
	}
	increment = 0;

	for(i32 i = 3; i < 6; i++)
	{
		smallBase[i].init((mainBase[0].x + mainBase[0].w) * 2.8, 200.0f + (increment * 280.0f), 64.0f, 64.0f);
		smallBase[i].texDiv = cj_vec2f(8.0f, 16.0f);
		smallBase[i].texFrag = cj_vec2f(0.0f, 0.0f);

		increment++;
	}
	increment = 0;

	for(i32 i = 6; i < 9; i++)
	{
		smallBase[i].init((mainBase[0].x + mainBase[0].w) * 3.9f, 200.0f + (increment * 280.0f), 64.0f, 64.0f);
		smallBase[i].texDiv = cj_vec2f(8.0f, 16.0f);
		smallBase[i].texFrag = cj_vec2f(0.0f, 0.0f);

		increment++;
	}
	increment = 0;

	for(i32 i = 0; i < 3; i++)
	{
		road[i].init((mainBase[0].x + mainBase[0].w)  + 100.0f, 270.0f + (increment * 160.0f), 40.0f, 165.0f);
		road[i].texDiv = cj_vec2f(16.0f, 8.0f);            
		road[i].texFrag = cj_vec2f(2.0f, 0.0f);            

		increment++;                                       
	}                                                          
	increment = 0;                                             
                                                                   
	for(i32 i = 3; i < 6; i++)                                 
	{                                                          
		road[i].init((smallBase[0].x + smallBase[0].w) + 130.0f, (smallBase[increment].y) - (smallBase[increment].w/1.3), 40.0f, 165.0f);
		road[i].texDiv = cj_vec2f(16.0f, 8.0f);
		road[i].texFrag = cj_vec2f(2.0f, 0.0f);

		increment++;
	}
	increment = 0;

	for(i32 i = 6; i < 9; i++)
	{
		road[i].init((smallBase[4].x + smallBase[4].w) + 130.0f, (smallBase[increment].y) - (smallBase[increment].w/1.3), 40.0f, 165.0f);
		road[i].texDiv = cj_vec2f(16.0f, 8.0f);
		road[i].texFrag = cj_vec2f(2.0f, 0.0f);

		increment++;
	}
	increment = 0;

	for(i32 i = 9; i < 12; i++)
	{
		road[i].init(mainBase[1].x - road[0].h/1.3, 270.0f + (increment * 160.0f), 40.0f, 165.0f);
		road[i].texDiv = cj_vec2f(16.0f, 8.0f);
		road[i].texFrag = cj_vec2f(2.0f, 0.0f);

		increment++;
	}

	rightSideMenu.init(SCR_WIDTH - 64.0f, 0.0f, 64.0f, SCR_HEIGHT - 300.0f);
	rightSideMenu.texDiv = cj_vec2f(16.0f, 2.0f);
	rightSideMenu.texFrag = cj_vec2f(11.0f, 1.0f);

	rightSideMenuCollisionRect[0].init(SCR_WIDTH - 64.0f, 0.0f, 64.0f, SCR_HEIGHT);
	rightSideMenuCollisionRect[1].init(SCR_WIDTH - (5.0f * 64.0f), 0.0f, 64.0f, SCR_HEIGHT);

	increment = 0;
	i32 menuOut = 0;

	Timer timer1 = {};
	Timer timer2 = {};
	timer1.SetStartTime(1.0f);
	timer2.SetStartTime(5.0f);

	while(!glfwWindowShouldClose(window) && running) 
	{
        	int currTxtIndex = TEXT_FIRST; 
		clock.start();


		glfwGetWindowSize(window, &winWidth, &winHeight);

		glUseProgram(shaderProgram);
		glUniform2f(winDimLoc, (float)winWidth, (float)winHeight);

		glUseProgram(textShaderProgram);
		glUniform2f(textWinDimLoc, (float)winWidth, (float)winHeight);

		glUseProgram(solidColorRectProgram);
		glUniform2f(solidColWindDimLoc, (float)winWidth, (float)winHeight);

		glClearColor(0.0f, 0.08f, 0.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		checkInput(window);
		glfwGetCursorPos(window, &mouseCurPosX, &mouseCurPosY);

		sprintf(DEBUGWindowDim, "Window Dimensions: %d/%d", winWidth, winHeight);
		sprintf(DEBUGMouseCurPos, "MouseCursor: %d/%d", (int)mouseCurPosX, (int)mouseCurPosY);
		sprintf(DEBUGFramTime, "miliSecs/frame: %0.2f", clock.getMiliSeconds());
		sprintf(DEBUGTimeSinceStart, "Time Since Start: %0.2fs", clock.timeSinceStart());

		glViewport(0.0f, 0.0f, winWidth, winHeight);


		if(mouseCurPosX >= winWidth - 1)
		{
			winX -= 10;
		}
		if(mouseCurPosY >= winHeight - 1)
		{
			winY += 10;
		}
		if(mouseCurPosX <= 1)
		{
			winX += 10;
		}
		if(mouseCurPosY <= 1)
		{
			winY -= 10;
		}

		if(winX >= 30.0f)
		{
			winX = 29.0f;
		}
	       	if(winX <= -400.0f)
		{
			winX = -399.0f;
		}
		if(winY >= 30.0f)
		{
			winY = 29.0f;
		}
	       	else if(winY <= -30.0f)
		{
			winY = -29.0f;
		}
		
		

		switch(stateOfTheGame)
		{ 
			// MAINMENU
			case mainMenu:
			{
				if(keyPressed[ESC])
				{
					if(isPressed[ESC] == 0)
					{
						running = false;
						isPressed[ESC] = 1;
					}

				}
				else
				{
					isPressed[ESC] = 0; 
				}

				if(keyPressed['S'])
				{
					stateOfTheGame = sandBox;
				}

				clearTextBuffer(gameBuffer);

				DBGStartGametxt 	  = printText(BCD, gameBuffer, &currTxtIndex, winWidth/2.5f, 400.0f, 1.0f, "START GAME", color4f(1.0f, 1.0f, 1.0f, 1.0f));
				textIndexRange DBGSandBox = printText(BCD, gameBuffer, &currTxtIndex, winWidth/2.5f, 450.0f, 1.0f, "SANDBOX", color4f(1.0f, 1.0f, 1.0f, 1.0f));
				printText(BCD, gameBuffer, &currTxtIndex, 1200.0f, 0.0f, 1.0f, DEBUGTimeSinceStart, color4f(1.0f, 1.0f, 1.0f, 1.0f));

				gameBuffer[TEXT_HIGHLIGHT_RECT_FIRST].createRect(	 gameBuffer[DBGStartGametxt.start].getX(), 
							   				 gameBuffer[DBGStartGametxt.start].getY(), 
							   				(gameBuffer[DBGStartGametxt.end].getX() + gameBuffer[DBGStartGametxt.end].getWidth()) - gameBuffer[DBGStartGametxt.start].getX(), 
							   				 gameBuffer[DBGStartGametxt.start].getHeight()
										);

				gameBuffer[TEXT_HIGHLIGHT_RECT_FIRST + 1].createRect(	 gameBuffer[DBGSandBox.start].getX(), 
							   				 gameBuffer[DBGSandBox.start].getY(), 
							   				(gameBuffer[DBGSandBox.end].getX() + gameBuffer[DBGSandBox.end].getWidth()) - gameBuffer[DBGSandBox.start].getX(), 
							   				 gameBuffer[DBGSandBox.start].getHeight()
										    );


				if( checkMouseCursorCollision(	mouseCurPosX , 
								mouseCurPosY , 
								gameBuffer, TEXT_HIGHLIGHT_RECT_FIRST))  
				{
					clearTextBuffer(gameBuffer);

					gameBuffer[TEXT_HIGHLIGHT_RECT_FIRST].setSolidColor(0.0f, 0.0f, 0.0f, 1.0f);

					printText(BCD, gameBuffer, &currTxtIndex, winWidth/2.5f, 400.0f, 1.0f, "START GAME", color4f(0.0f, 1.0f, 0.0f, 1.0f));
					printText(BCD, gameBuffer, &currTxtIndex, winWidth/2.5f, 450.0f, 1.0f, "SANDBOX", color4f(1.0f, 1.0f, 1.0f, 1.0f));
					printText(BCD, gameBuffer, &currTxtIndex, 1200.0f, 0.0f, 1.0f, DEBUGTimeSinceStart, color4f(1.0f, 1.0f, 1.0f, 1.0f));

				}
				else
				{
					gameBuffer[TEXT_HIGHLIGHT_RECT_FIRST].setSolidColor(0.0f, 0.0f, 0.0f, 0.4f);
				}

				if( checkMouseCursorCollision(	mouseCurPosX , 
								mouseCurPosY , 
								gameBuffer, TEXT_HIGHLIGHT_RECT_FIRST + 1))  
				{
					clearTextBuffer(gameBuffer);

					gameBuffer[TEXT_HIGHLIGHT_RECT_FIRST + 1].setSolidColor(0.0f, 0.0f, 0.0f, 1.0f);

					printText(BCD, gameBuffer, &currTxtIndex, winWidth/2.5f, 400.0f, 1.0f, "START GAME", color4f(1.0f, 1.0f, 1.0f, 1.0f));
					printText(BCD, gameBuffer, &currTxtIndex, winWidth/2.5f, 450.0f, 1.0f, "SANDBOX", color4f(0.0f, 1.0f, 0.0f, 1.0f));
					printText(BCD, gameBuffer, &currTxtIndex, 1200.0f, 0.0f, 2.0f, DEBUGTimeSinceStart, color4f(1.0f, 1.0f, 1.0f, 1.0f));

				}
				else
				{
					gameBuffer[TEXT_HIGHLIGHT_RECT_FIRST + 1].setSolidColor(0.0f, 0.0f, 0.0f, 0.4f);
				}


				printText(BCD, gameBuffer, &currTxtIndex, textX + 100.0f, 10.0f, 0.8f, DEBUGWindowDim, color4f(1.0f, 1.0f, 1.0f, 1.0f)); 
				printText(BCD, gameBuffer, &currTxtIndex, textX + 600.0f, 10.0f, 0.8f, DEBUGMouseCurPos, color4f(1.0f, 1.0f, 1.0f, 1.0f)); 
				printText(BCD, gameBuffer, &currTxtIndex, textX + 900.0f, 10.0f, 0.8f, DEBUGFramTime, color4f(1.0f, 1.0f, 1.0f, 1.0f)); 

				mapObject(TEXT_HIGHLIGHT_RECT_FIRST, TEXT_HIGHLIGHT_RECT_LAST, vboB, gameBuffer);
				mapObject(TEXT_FIRST, TEXT_LAST, vboB, gameBuffer);

				if(keyPressed[LEFT_MOUSE_BUTTON])
				{
					if(checkMouseCursorCollision(mouseCurPosX , mouseCurPosY , gameBuffer, TEXT_HIGHLIGHT_RECT_FIRST))
					{
						stateOfTheGame = newGame;
					}
				}

				if(keyPressed[LEFT_MOUSE_BUTTON])
				{
					if(checkMouseCursorCollision(mouseCurPosX , mouseCurPosY , gameBuffer, TEXT_HIGHLIGHT_RECT_FIRST + 1))
					{
						stateOfTheGame = sandBox;
					}
				}

				gameBuffer[MAIN_BASE_01].createRect(0.0f, 0.0f, 0.0f, 0.0f);
				gameBuffer[MAIN_BASE_02].createRect(0.f, 0.0f, 0.0f, 0.0f);

				menuOut = 0;
				clearBuffer(gameBuffer, MAIN_BASE_START, MAIN_BASE_END); 
				clearBuffer(gameBuffer, SMALL_BASE_START, SMALL_BASE_END); 
				clearBuffer(gameBuffer, ROAD_START, RIGHT_SIDE_MENU); 

				
				mapObject(MAIN_BASE_START, MAIN_BASE_END, vboB, gameBuffer);
				mapObject(SMALL_BASE_START, SMALL_BASE_END, vboB, gameBuffer);
				mapObject(ROAD_START, RIGHT_SIDE_MENU, vboB, gameBuffer);

				render(solidColorRectProgram, 0, TEXT_HIGHLIGHT_RECT_FIRST, TEXT_HIGHLIGHT_RECT_LAST); 
				render(textShaderProgram, textureID[2], TEXT_FIRST, 2000); 
				render(shaderProgram, textureID[3], MAIN_BASE_START, RIGHT_SIDE_MENU); 




				timer1.Reset();
				timer2.Reset();
			} break;

			case newGame:
			{
				if(keyPressed[ESC])
				{
					if(isPressed[ESC] == 0)
					{
						stateOfTheGame = mainMenu;
						isPressed[ESC] = 1;
					}
				}
				else
				{
					isPressed[ESC] = 0; 
				}




				char txt_timer1[50] = {};
				char txt_timer2[50] = {};
				char txt_PlayerTurn[50] = {};
				char txt_PlayerInfo[50] = {};
				static i32 playerTurn = 1;

				timer1.Begin(clock.getSeconds());
				timer2.Begin(clock.getSeconds());

				static bool changed = false;
				player[0].Territories = 1;
				player[1].Territories = 2;
				
				if(timer1.timeLeft <= 0.0001)
				{
					if(!changed)
					{
						if(playerTurn == 1)
						playerTurn = 2;
						else if(playerTurn == 2)
						playerTurn = 1;
						changed = true;
					}
				}
				else
				{
					changed = false;
				}


				if(playerTurn == 1)
				{


				}


				sprintf(txt_timer1, 			"Time1 Left: %d", 	(i32)timer1.timeLeft);
				sprintf(txt_timer2, 			"Time2 Left: %d", 	(i32)timer2.timeLeft);
				sprintf(txt_PlayerTurn, 		"Player %d Turn", 	playerTurn);
				sprintf(txt_PlayerInfo, 		"Player %d Stats", 	playerTurn);
				sprintf(player[0].txt_Territories, 	"Territories: %d", 	player[0].Territories);
				sprintf(player[0].txt_Tanks, 		"Tanks: %d", 		player[0].Tanks);
				sprintf(player[0].txt_GoldCollector, 	"GoldCollector: %d", 	player[0].GoldCollector);
				sprintf(player[0].txt_Barracks, 	"Barracks: %d", 	player[0].Barracks);

				sprintf(player[1].txt_Territories, 	"Territories: %d", 	player[1].Territories);
				sprintf(player[1].txt_Tanks, 		"Tanks: %d", 		player[1].Tanks);
				sprintf(player[1].txt_GoldCollector, 	"GoldCollector: %d", 	player[1].GoldCollector);
				sprintf(player[1].txt_Barracks, 	"Barracks: %d", 	player[1].Barracks);
				
				clearTextBuffer(gameBuffer);
				//printText(BCD, gameBuffer, &currTxtIndex, textX + 	100.0f, 	10.0f, 	1.0f, DEBUGWindowDim, 	color4f(1.0f, 1.0f, 1.0f, 1.0f)); 
				//printText(BCD, gameBuffer, &currTxtIndex, textX + 	600.0f, 10.0f, 	1.0f, DEBUGMouseCurPos, color4f(1.0f, 1.0f, 1.0f, 1.0f)); 
				//printText(BCD, gameBuffer, &currTxtIndex, textX + 	900.0f, 10.0f, 	1.0f, DEBUGFramTime, 	color4f(1.0f, 1.0f, 1.0f, 1.0f)); 


				printText(BCD, gameBuffer, &currTxtIndex, 		0.0f, 100.0f, 1.0f, txt_timer1, color4f(1.0f, 1.0f, 1.0f, 1.0f));
				printText(BCD, gameBuffer, &currTxtIndex, 		0.0f, 150.0f, 1.0f, txt_timer2, color4f(1.0f, 1.0f, 1.0f, 1.0f));
				printText(BCD, gameBuffer, &currTxtIndex, 		0.0f, 50.0f, 1.3f, txt_PlayerTurn, color4f(1.0f, 1.0f, 1.0f, 1.0f));

				mapObject(TEXT_FIRST, TEXT_LAST, vboB, gameBuffer);

				if(menuOut == 0 && !checkMouseCursorGameObjectCollision(mouseCurPosX, mouseCurPosY, rightSideMenuCollisionRect[0]))
				{
					float newMenuWidth = 64.0f;
					float newTexDivX = 16.0f;

					rightSideMenu.x = winWidth - 64.0f;
					rightSideMenu.w = newMenuWidth;

					rightSideMenu.texDiv.x = newTexDivX;
					rightSideMenu.texFrag.x = 10.0f;

				}
				else if (menuOut == 0 && checkMouseCursorGameObjectCollision(mouseCurPosX, mouseCurPosY, rightSideMenuCollisionRect[0]))
				{
					rightSideMenu.texFrag.x = 10.0f;

					if(keyPressed[LEFT_MOUSE_BUTTON] && menuOut == 0) 
					{
						menuOut = 1;

						float newMenuWidth = 64.0f * 5.0f + 150.0f;
						float newTexDivX = 16.0f / 5.0f;
						rightSideMenu.w = newMenuWidth;
						rightSideMenu.x = winWidth - newMenuWidth;
						rightSideMenuCollisionRect[1].x = rightSideMenu.x;

						rightSideMenu.texDiv.x = newTexDivX;
						rightSideMenu.texFrag.x = 0.0f;

					}
				} 
				else if(menuOut == 1)
				{

					if(playerTurn == 1)
					{
						printText(BCD, gameBuffer, &currTxtIndex, rightSideMenu.x + 130.0f, 30.0f, 1.3f, txt_PlayerInfo, 		cj_vec4f(1.0f, 0.0f, 0.0f, 1.0f));
						printText(BCD, gameBuffer, &currTxtIndex, rightSideMenu.x + 30.0f, 70.0f,  1.0f,  player[0].txt_Territories, 	cj_vec4f(1.0f, 0.0f, 0.0f, 1.0f));
						printText(BCD, gameBuffer, &currTxtIndex, rightSideMenu.x + 30.0f, 100.0f, 1.0f, player[0].txt_Tanks, 		cj_vec4f(1.0f, 0.0f, 0.0f, 1.0f));
						printText(BCD, gameBuffer, &currTxtIndex, rightSideMenu.x + 30.0f, 130.0f, 1.0f, player[0].txt_GoldCollector, 	cj_vec4f(1.0f, 0.0f, 0.0f, 1.0f));
						printText(BCD, gameBuffer, &currTxtIndex, rightSideMenu.x + 30.0f, 160.0f, 1.0f, player[0].txt_Barracks, 	cj_vec4f(1.0f, 0.0f, 0.0f, 1.0f));
					}
					else if(playerTurn == 2)
					{
						printText(BCD, gameBuffer, &currTxtIndex, rightSideMenu.x + 130.0f, 30.0f, 1.3f, txt_PlayerInfo, 		cj_vec4f(0.3f, 0.3f, 1.0f, 1.0f));
						printText(BCD, gameBuffer, &currTxtIndex, rightSideMenu.x + 30.0f, 70.0f, 1.0f,  player[1].txt_Territories, 	cj_vec4f(0.3f, 1.0f, 1.0f, 1.0f));
						printText(BCD, gameBuffer, &currTxtIndex, rightSideMenu.x + 30.0f, 100.0f, 1.0f, player[1].txt_Tanks, 		cj_vec4f(0.3f, 0.3f, 1.0f, 1.0f));
						printText(BCD, gameBuffer, &currTxtIndex, rightSideMenu.x + 30.0f, 130.0f, 1.0f, player[1].txt_GoldCollector, 	cj_vec4f(0.3f, 0.3f, 1.0f, 1.0f));
						printText(BCD, gameBuffer, &currTxtIndex, rightSideMenu.x + 30.0f, 160.0f, 1.0f, player[1].txt_Barracks, 	cj_vec4f(0.3f, 0.3f, 1.0f, 1.0f));
					}
					if(checkMouseCursorGameObjectCollision(mouseCurPosX, mouseCurPosY, rightSideMenuCollisionRect[1]))
					{
						rightSideMenu.texFrag.x = 0.0f;

						if(keyPressed[LEFT_MOUSE_BUTTON])
						{
							menuOut = 0;
							float newMenuWidth = 64.0f;
							float newTexDivX = 16.0f;

							rightSideMenu.x = winWidth - 64.0f;
							rightSideMenu.w = newMenuWidth;

							rightSideMenu.texDiv.x = newTexDivX;
							rightSideMenu.texFrag.x = 10.0f;
						}

					}
					else
					{
						rightSideMenu.texFrag.x = 0.0f;
					}
				}
				rightSideMenu.h = winHeight;
				rightSideMenuCollisionRect[0].h = winHeight;
				rightSideMenuCollisionRect[1].h = winHeight;

				i32 mainBaseIndex = 0;
				for(i32 i = MAIN_BASE_01; i <= MAIN_BASE_02; i++)
				{
					gameBuffer[i].createRect(mainBase[mainBaseIndex].x + winX, mainBase[mainBaseIndex].y - winY, mainBase[mainBaseIndex].w, mainBase[mainBaseIndex].h);
					gameBuffer[i].setTextureDivision(mainBase[mainBaseIndex].texDiv);
					gameBuffer[i].pickTextureFragment(mainBase[mainBaseIndex].texFrag);
					mainBaseIndex++;
				}	

				i32 smallBaseIndex = 0;
				for (i32 i = SMALL_BASE_01; i <= SMALL_BASE_09; i++)
				{

					gameBuffer[i].createRect(smallBase[smallBaseIndex].x + winX, smallBase[smallBaseIndex].y - winY, smallBase[smallBaseIndex].w, smallBase[smallBaseIndex].h); 
					gameBuffer[i].setTextureDivision(smallBase[smallBaseIndex].texDiv);
					gameBuffer[i].pickTextureFragment(smallBase[smallBaseIndex].texFrag);
					smallBaseIndex++;

				}
				i32 roadIndex = 0;
				for(i32 i = ROAD_01; i <= ROAD_12; i++)
				{
					gameBuffer[i].createRect(road[roadIndex].x + winX, road[roadIndex].y - winY, road[roadIndex].w, road[roadIndex].h); 
					gameBuffer[i].setTextureDivision(road[roadIndex].texDiv);
					gameBuffer[i].pickTextureFragment(road[roadIndex].texFrag);
					roadIndex++;

				}

				gameBuffer[ROAD_01].rotate(1.0f);
				gameBuffer[ROAD_02].rotate(1.5708f);
				gameBuffer[ROAD_03].rotate(2.0f);

				gameBuffer[ROAD_04].rotate(1.5708f);
				gameBuffer[ROAD_05].rotate(1.5708f);
				gameBuffer[ROAD_06].rotate(1.5708f);

				gameBuffer[ROAD_07].rotate(1.5708f);
				gameBuffer[ROAD_08].rotate(1.5708f);
				gameBuffer[ROAD_09].rotate(1.5708f);

				gameBuffer[ROAD_10].rotate(2.2f);
				gameBuffer[ROAD_11].rotate(1.5708f);
				gameBuffer[ROAD_12].rotate(1.0f);

				gameBuffer[RIGHT_SIDE_MENU].createRect(rightSideMenu.x, rightSideMenu.y, rightSideMenu.w, rightSideMenu.h);
				gameBuffer[RIGHT_SIDE_MENU].setTextureDivision(rightSideMenu.texDiv);
				gameBuffer[RIGHT_SIDE_MENU].pickTextureFragment(rightSideMenu.texFrag);


				mapObject(MAIN_BASE_01, 	MAIN_BASE_07, 		vboB, gameBuffer);
				mapObject(SMALL_BASE_01, 	SMALL_BASE_09, 		vboB, gameBuffer);
				mapObject(ROAD_01, 		ROAD_14, 		vboB, gameBuffer);
				mapObject(RIGHT_SIDE_MENU, 	RIGHT_SIDE_MENU, 	vboB, gameBuffer);
				mapObject(TEXT_FIRST, 	TEXT_LAST, 	vboB, gameBuffer);

				render(shaderProgram, 		textureID[3], 	MAIN_BASE_01, 	7); 
				render(0, 			textureID[3], 	SMALL_BASE_01, 	9); 
				render(0, 			textureID[3], 	ROAD_01, 	14); 
				render(0, 			textureID[3],	RIGHT_SIDE_MENU, 1); 
				render(textShaderProgram, 	textureID[2], 	TEXT_FIRST, 	2000); 

			} break;

			// SANDBOX
			case sandBox:
			{
				// SET ALL OBJECTS X AND Y TO (+ winX) ( +winY) IF OBJECTS MOVE
				if(mouseCurPosX >= winWidth - 1)
				{
					winX -= 1000;
				}
				if(mouseCurPosY >= winHeight + 1)
				{
					winY += 1000;
				}
				if(mouseCurPosX <= 1)
				{
					winX += 1000;
				}
				if(mouseCurPosY <= 1)
				{
					winY -= 1000;
				}
				if(keyPressed[ESC])
				{
					if(isPressed[ESC] == 0)
					{
						stateOfTheGame = mainMenu;
						isPressed[ESC] = 1;
					}
				}
				else
				{
					isPressed[ESC] = 0; 
				}

				if(keyPressed[LEFT_MOUSE_BUTTON])
				{
					for(int i = 0; i < (GROUND_TILE_LAST); i++)
					{
						if(checkMouseCursorCollision(mouseCurPosX - winX, mouseCurPosY + winY, gameBuffer, GROUND_TILE_FIRST + i))
						{
							gameBuffer[GROUND_TILE_FIRST + i].pickTextureFragment(5.0f, 0.0f);
						}
					}
				}

				for(int i = 0; i < (GROUND_TILE_LAST); i++)
				{
					gameBuffer[i].setX(gameBuffer[i].getX() + winX * clock.getSeconds());
					gameBuffer[i].setY(gameBuffer[i].getY() - winY * clock.getSeconds());
				}
				winX = 0.0f;
				winY = 0.0f;

				gameBuffer[SELECT_MENU_FIRST].setX(-winX);
				gameBuffer[SELECT_MENU_FIRST].setY(winY);

				if(keyPressed['S'])
				{
					MENU_STATE = MENU_STATE_MAIN;
				}

				if(pressed)
				{
					sprintf(trueSTATE, "true");
				}
				else
				{
					sprintf(trueSTATE, "false");
				}

				clearTextBuffer(gameBuffer);

				char txt_rectCorners[100] = "";
				CJ_rectCorners_X rect1Corners = gameBuffer[rect1].getCornersX();
				sprintf(txt_rectCorners, "x1: %0.2f x2: %0.2f, x3: %0.2f x4: %0.2f\0", rect1Corners.x1, rect1Corners.x2, rect1Corners.x3, rect1Corners.x4);  

				writer(&txt_CURSOR, txt_INPUT, textBufferSize);
				sprintf(txt_ENTER, txt_INPUT);

				DBGStartGametxt = printText(BCD, gameBuffer, &currTxtIndex, 400.0f, 400.0f, 1.0f, "START GAME", color4f(1.0f, 1.0f, 1.0f, 1.0f));
				printText(BCD, gameBuffer, &currTxtIndex, textX + 	100.0f, 	10.0f, 	1.0f, DEBUGWindowDim, 	color4f(1.0f, 1.0f, 1.0f, 1.0f)); 
				printText(BCD, gameBuffer, &currTxtIndex, textX + 	600.0f, 10.0f, 	1.0f, DEBUGMouseCurPos, color4f(1.0f, 1.0f, 1.0f, 1.0f)); 
				printText(BCD, gameBuffer, &currTxtIndex, textX + 	900.0f, 10.0f, 	1.0f, DEBUGFramTime, 	color4f(1.0f, 1.0f, 1.0f, 1.0f)); 
				printText(BCD, gameBuffer, &currTxtIndex, 		1200.0f,   0.0f, 1.0f, DEBUGTimeSinceStart, color4f(1.0f, 1.0f, 1.0f, 1.0f));
				printText(BCD, gameBuffer, &currTxtIndex, 		900.0f, 500.0f, 1.0f, trueSTATE, 	color4f(1.0f, 1.0f, 1.0f, 1.0f)); 
				printText(BCD, gameBuffer, &currTxtIndex, 		900.0f, 200.0f, 1.0f, txt_ENTER, 	color4f(1.0f, 1.0f, 1.0f, 1.0f)); 
				printText(BCD, gameBuffer, &currTxtIndex, 		900.0f, 600.0f, 1.0f, txt_rectCorners, 	color4f(1.0f, 1.0f, 1.0f, 1.0f)); 

				rect1Corners = gameBuffer[rect2].getCornersX();
				sprintf(txt_rectCorners, "x1: %0.2f x2: %0.2f, x3: %0.2f x4: %0.2f\0", rect1Corners.x1, rect1Corners.x2, rect1Corners.x3, rect1Corners.x4);  
				printText(BCD, gameBuffer, &currTxtIndex, 		900.0f, 700.0f, 1.0f, txt_rectCorners, 	color4f(1.0f, 1.0f, 1.0f, 1.0f)); 


				gameBuffer[TEXT_HIGHLIGHT_RECT_FIRST].createRect(gameBuffer[DBGStartGametxt.start].getX(), 
										   gameBuffer[DBGStartGametxt.start].getY(), 
										   (gameBuffer[DBGStartGametxt.end].getX() + gameBuffer[DBGStartGametxt.end].getWidth()) - gameBuffer[DBGStartGametxt.start].getX(), 
										   gameBuffer[DBGStartGametxt.start].getHeight());



				if( checkMouseCursorCollision(	mouseCurPosX - winX, 
								mouseCurPosY + winY, 
								gameBuffer, TEXT_HIGHLIGHT_RECT_FIRST))  
				{
					gameBuffer[TEXT_HIGHLIGHT_RECT_FIRST].setSolidColor(0.0f, 0.0f, 0.0f, 1.0f);
					DBGStartGametxt = printText(BCD, gameBuffer, &currTxtIndex, 400.0f, 400.0f, 1.0f, "START GAME", color4f(0.0f, 1.0f, 0.0f, 1.0f));
					mapObject(TEXT_FIRST, TEXT_LAST, vboB, gameBuffer);
				}
				else
				{
					gameBuffer[TEXT_HIGHLIGHT_RECT_FIRST].setSolidColor(0.0f, 0.0f, 0.0f, 0.0f);
				}

				mapObject(TEXT_FIRST, TEXT_LAST, vboB, gameBuffer);
				mapObject(TEXT_HIGHLIGHT_RECT_FIRST, TEXT_HIGHLIGHT_RECT_LAST, vboB, gameBuffer);
				mapObject(GROUND_MAP, GROUND_MAP, vboB, gameBuffer);


				if(keyPressed[UP_ARROW])
				{
					rect1MovY -= 3;
				}
				if(keyPressed[DOWN_ARROW])
				{
					rect1MovY += 3;
				}

				static float radian = 0.0f;

				if(keyPressed[RIGHT_ARROW])
				{
					rect1MovX += 3;
				}
				if(keyPressed[LEFT_ARROW])
				{
					rect1MovX -= 3;
				}
				if(keyPressed[SPACEBAR])
				{
					radian += 0.05f;
					printf("%s\n", txt_ENTER);
				}

				gameBuffer[rect1].setX(gameBuffer[rect1].getX() + rect1MovX);
				gameBuffer[rect1].setY(gameBuffer[rect1].getY() + rect1MovY);
				gameBuffer[rect1].rotate(radian);
				gameBuffer[rect2].rotate(radian);

				gameBuffer[GROUND_TILE_FIRST + 30].setX(gameBuffer[GROUND_TILE_FIRST + 30].getX() + rect1MovX);
				gameBuffer[GROUND_TILE_FIRST + 30].setY(gameBuffer[GROUND_TILE_FIRST + 30].getY() + rect1MovY);

				for(int i = 1; i < GROUND_TILE_LAST; i++)
				{
					gameBuffer[i].rotate(radian);
				}

				

				if(checkRectCollision(rect1, rect2, gameBuffer))
				{
					gameBuffer[rect1].setSolidColor(0.0f, 1.0f, 0.0f, 1.0f);
					gameBuffer[rect1].setColorOfCorners(1.0f, 1.0f, 1.0f, 1.0f);
					
				}
				else
				{
					gameBuffer[rect1].setSolidColor(1.0f, 0.0f, 0.0f, 1.0f);
					gameBuffer[rect1].setColorOfCorners(1.0f, 1.0f, 1.0f, 1.0f);
				}

				rect1MovY = 0;
				rect1MovX = 0;

				mapObject(0, 20000, vboB, gameBuffer);

				render(shaderProgram, textureID[1], GROUND_MAP, 1);
				render(0, textureID[0], GROUND_TILE_FIRST, GROUND_TILE_LAST);
				render(solidColorRectProgram, 0, TEXT_HIGHLIGHT_RECT_FIRST, 1);
				render(shaderProgram, textureID[0], SELECT_MENU_FIRST, 1);
				render(textShaderProgram, textureID[2], TEXT_FIRST, 1000);
				render(solidColorRectProgram, 0, rect1, 2);

			} break;

		}




		// In the render function, use shaderProgram ( pass 0 to keep active shaderProgram ), textureID to bind, start Index to draw and N objects to draw
		// Always render the lowest index first and the highest index last

		glfwSwapBuffers(window);
		glfwPollEvents();
	}


	for(int i = 0; i < sizeof(textureID) / sizeof(GLuint); i++)
	{
		glDeleteTextures(1, &textureID[i]);
	}

	glDeleteProgram(shaderProgram);
	glDeleteProgram(textShaderProgram);
	glDeleteProgram(solidColorRectProgram);
	glDeleteBuffers(1, &vboB.posVBO);
	glDeleteBuffers(1, &vboB.colVBO);
	glDeleteBuffers(1, &vboB.texVBO);
	glDeleteBuffers(1, &vboB.IBO);
	glDeleteVertexArrays(1, &vboB.VAO);
	
	glfwDestroyWindow(window);
	glfwTerminate();


	return 0;
}
Simon Anciaux
1051 posts
Time problem
Edited by Simon Anciaux on Reason: clock start issue
Could you be more specific about what is it you're seeing that is not correct ? I suppose you're talking about the printouts
1
2
sprintf(txt_timer1,  "Time1 Left: %d", (i32)timer1.timeLeft);
sprintf(txt_timer2,  "Time2 Left: %d", (i32)timer2.timeLeft);


A quick solution would be to use double instead of float in your Timer struct.

When you add floating point numbers together (double or float) there is an precision error. I'm not the best person to explain that, but a simple way to visualize it is that you can have big numbers with not much precision after the decimal point, or you can have more precision after the decimal point but you can't represent big numbers. When you add/subtract those two "types" of number, you will loose precision in the small part of the number (1000000.0f + 0.0001234f will not give 1000000.0001234f but more likely something like 1000000.0f).

In addition to that (and maybe the real cause of the problem) is that you can't precisely represent every value in floating point since there is an infinity of values.

In your case, even the difference between the precision of 5.0f and 1.0f gives a small difference in precision after the first operation. Using double instead of float will give you more precision, but the problem will still be there so it's probably not the best solution.

I wrote this small test code. If you replace the float with double the precision will be better and the printout will show similar value after the decimal point.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <stdio.h>

int main( int argc, char** argv ) {
    
    float a = 1.0f + 0.999f;
    float b = 5.0f + 0.999f;
    
    float c = 1.0 / 60.0;
    
    while ( a > 0 ) {
        printf( "a: %f\nb: %f\n\n", a, b );
        a -= ( r32 ) c;
        b -= ( r32 ) c;
    }
}


If your goal is to print the number of second left, you could use roundf (msdn) to get the closest integer value to your numbers.

EDIT: Also I believe there is an issue with your clock start function: it will set dt_s to 0 on the first frame, dt_s to the time elapsed since glfw intialization on the second frame, and after that the result will be correct but with a frame of lag.
90 posts
Time problem
I've changed all the values to double but that won't do it.

The problem is still as you say and as i also mentioned earlier that after a Timer reaches < 0 and resets the timeLeft,
the floating points differ and therefor makes the different timers tick down at different paces.

frame 1:
timer1 = 1.9990000128746033
timer2 = 5.9990000128746033

frame 2:
timer1 = 0.39573061287460320
timer2 = 4.3957306128746030

frame 3: (decimals start to get out of sync)
timer1 = -1.3388824871253968
timer2 = 2.6611175128746032

frame 4: (timer1 is reset but now decimals is totally out of sync)
timer1 = 1.9990000128746033
timer2 = -14.272393287125396

frame 5:
timer1 = -5.4249719871253959
timer2 = 5.9990000128746033


Also changeing :

1
2
sprintf(txt_timer1,  "Time1 Left: %d", (i32)timer1.timeLeft);
sprintf(txt_timer2,  "Time2 Left: %d", (i32)timer2.timeLeft);



to :
1
2
sprintf(txt_timer1,  "Time1 Left: %d", (i32)roundf(timer1.timeLeft));
sprintf(txt_timer2,  "Time2 Left: %d", (i32)roundf(timer2.timeLeft));



Did not work.

My goal is indeed to print the seconds left and different timers should be able to start at differents times and tick down at the same rate even after they reset (reached below 0).