summary refs log blame commit diff stats
path: root/src/main.c
blob: a63824824f946d360b9f32866af45f13f867e1ca (plain) (tree)
1
2
3
4
5
6
7
8

                   




                      
                    























                                                             

                                                                    



















                                                                            





                                                                            
















                                                                                                                         
                       




















                                                                  
                             



                                                     
                                    


                                                      
        
                              
                                              
                                   


                                                      





                                                          






                                                            



                                                            

                                      
                 
         
        
                                                                 



                                     
#include "pong.h"
#include "sounds.h"


int Difficulty = 1;
atomic_int Ticks = 0;
bool GameGoing = true;
char *VersionString;

mtx_t AudioQueueBeingModified;

int AudioQueue[20];

int internal_clock() {
	const struct timespec Delay = {
		0, 20000000
	};
	struct timespec Remaining;
	while(GameGoing == true) {
		atomic_fetch_add(&Ticks, 1);
		thrd_sleep(&Delay, &Remaining);
	}
	thrd_exit(0);
}

int audio() {
	int i;
	InitAudioDevice();
	Sound Bounce = LoadSound("resources/bounce.wav");
	Sound TitleScreen = LoadSound("resources/title.wav");
	Sound Victory = LoadSound("resources/victory.wav");
	Sound Defeat = LoadSound("resources/defeat.wav");
	Sound PlayerScore = LoadSound("resources/score_player.wav");
	Sound EnemyScore = LoadSound("resources/score_enemy.wav");
	const struct timespec Delay = {
		0, 20000000
	};
	struct timespec Remaining;
	while(GameGoing == true) {
		for(i = 0; i < 20; i++) {
			if(AudioQueue[i] != -1){
				switch(AudioQueue[i]) {
					case 0: //Play bounce sound.
						PlaySoundMulti(Bounce);
						break;
					case 1: //Play game over
						PlaySoundMulti(Defeat);
						break;
					case 2: //Play win
						PlaySoundMulti(Victory);
						break;
					case 3: //Title Screen
						PlaySoundMulti(TitleScreen);
						break;
					case 4: //Player Score
						PlaySoundMulti(PlayerScore);
						break;
					case 5: //Enemy Score
						PlaySoundMulti(EnemyScore);
						break;
					case 99: //Stop All Sounds
						StopSoundMulti();
						break;
					default:
						break;
				}
				while(mtx_trylock(&AudioQueueBeingModified) == thrd_busy) thrd_sleep(&Delay, &Remaining);
				AudioQueue[i] = -1;
				mtx_unlock(&AudioQueueBeingModified);
			}
		}
		thrd_sleep(&Delay, &Remaining);
	}
	thrd_exit(0);
}

bool play_audio(int SoundEffect) {
	unsigned int i;
	while(mtx_trylock(&AudioQueueBeingModified) == thrd_busy);
	for(i = 1; i != 20; i++) {
		if (AudioQueue[i-1] == -1) {
			AudioQueue[i-1] = AudioQueue[i];
		}
	}
	for(i = 0; AudioQueue[i] != -1; i++) {
		if(i > sizeof(AudioQueue)) {
			mtx_unlock(&AudioQueueBeingModified);
			return false;
		}
	}
	AudioQueue[i] = SoundEffect;
	mtx_unlock(&AudioQueueBeingModified);
	return true;
}

int main() {
	//Raylib Init
	InitWindow(1280, 720, "Pong");
	SetTargetFPS(60);
	SetExitKey(KEY_NULL);
	Image Icon = LoadImage("resources/ball.png");
	SetWindowIcon(Icon);
	SetWindowState(FLAG_VSYNC_HINT);
	SetWindowState(FLAG_WINDOW_RESIZABLE);
	SetWindowMinSize(1280, 720);

	//Init Variables
	strcpy(VersionString, "Version 0.2 - AEOLUS");
	
	//Populate Audio Queue
	for(unsigned int i = 0; i < 20; i++) {
		AudioQueue[i] = -1;
	}
	mtx_init(&AudioQueueBeingModified, mtx_plain);

	// Initialize Internal Clock
	thrd_t InternalClock;
	thrd_create(&InternalClock, internal_clock, NULL);
	thrd_t AudioThread;
	thrd_create(&AudioThread, audio, NULL);
	
	// Launch Game
	while (GameGoing == true) {
		switch(title_screen()) {
			case 0: //Versus
				play_audio(STOP_ALL_SOUNDS);
				versus_main();
				break;
			case 1: //Marathon
				play_audio(STOP_ALL_SOUNDS);
				marathon_main();
				break;
			default:
				break;
		}
	}
	
	GameGoing = false; // Make sure the game is going to end.
	thrd_join(AudioThread, NULL);
	CloseWindow();
	return(0);
}