about summary refs log blame commit diff stats
path: root/filesystem.mu
blob: 6ea8e08c9af33278d63976964735784618366488 (plain) (tree)
1
2
3
4
5
6
7
8
9




                                                                     

             

                                                                                             
   
                                                       
                  
                                   

        



                                                                    
 
# example program: copy one file into another, character by character
# BEWARE: this will modify your file system
# before running it, put some text into /tmp/mu-x
# after running it, check /tmp/mu-y

def main [
  local-scope
  source-file:&:source:char <- start-reading null/real-filesystem, [/tmp/mu-x]
  sink-file:&:sink:char, write-routine:num <- start-writing null/real-filesystem, [/tmp/mu-y]
  {
    c:char, done?:bool, source-file <- read source-file
    break-if done?
    sink-file <- write sink-file, c
    loop
  }
  close sink-file
  # make sure to wait for the file to be actually written to disk
  # (Mu practices structured concurrency: http://250bpm.com/blog:71)
  wait-for-routine write-routine
]
0 Initial Commit' href='/charadon/games/Pong-C/commit/src/main.c?id=b1de6008bdfbd5bdeb62d2d3dad953019555556b'>b1de600
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

                   


                           




                      
                        


















                                               










                                                                           








                                                                    
                                                                               

                                                                
                                                                               

                                                          
                                                                                

                                                              
                                                                                    
                                                      
                                                              
                                                                                    

                                                             
                                                                                   
                                                      
                                                                  
                                                                    










                                                                                                                         
                   



                                  
                       




















                                                                  
                             



                                                     
                                    

                        
                                                                              
        
                              
                                              
                                   


                                                      





                                                          






                                                            



                                                            

                                      
                 
         
        
                                                                 
                                     
                                       


                      
#include "pong.h"
#include "sounds.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#include <SDL2/SDL_mixer.h>


int Difficulty = 1;
atomic_int Ticks = 0;
bool GameGoing = true;
char VersionString[256];

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;
	SDL_Init(SDL_INIT_AUDIO);
	Mix_Init(0);
	Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 1024);
	Mix_AllocateChannels(64);
	Mix_Volume(-1, 128);
	Mix_Chunk *Bounce = Mix_LoadWAV("resources/bounce.wav");
	Mix_Chunk *TitleScreen = Mix_LoadWAV("resources/title.wav");
	Mix_Chunk *Victory = Mix_LoadWAV("resources/victory.wav");
	Mix_Chunk *Defeat = Mix_LoadWAV("resources/defeat.wav");
	Mix_Chunk *PlayerScore = Mix_LoadWAV("resources/score_player.wav");
	Mix_Chunk *EnemyScore = Mix_LoadWAV("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.
						Mix_PlayChannel(-1, Bounce, 0);
						break;
					case 1: //Play game over
						Mix_PlayChannel(-1, Defeat, 0);
						break;
					case 2: //Play win
						Mix_PlayChannel(-1, Victory, 0);
						break;
					case 3: //Title Screen
						Mix_PlayChannel(-1, TitleScreen, 0);
						break;
					case 4: //Player Score
						Mix_PlayChannel(-1, PlayerScore, 0);
						break;
					case 5: //Enemy Score
						Mix_PlayChannel(-1, EnemyScore, 0);
						break;
					case 99: //Stop All Sounds
						Mix_HaltChannel(-1);
						break;
					default:
						break;
				}
				while(mtx_trylock(&AudioQueueBeingModified) == thrd_busy) thrd_sleep(&Delay, &Remaining);
				AudioQueue[i] = -1;
				mtx_unlock(&AudioQueueBeingModified);
			}
		}
		thrd_sleep(&Delay, &Remaining);
	}
	SDL_Quit();
	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
	strncpy(VersionString, "Version 0.2 - AEOLUS", sizeof(VersionString));
	
	//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);
	thrd_join(InternalClock, NULL);
	CloseWindow();
	return(0);
}