-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
593 lines (467 loc) · 14.3 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <math.h>
#include "objects.h"
#include "movement.h"
#include "linked_list_obj.h"
#include "array_obj.h"
#include "enemies.h"
#include "portaudio.h"
#ifdef __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#elif __linux
#include <AL/al.h>
#include <AL/alc.h>
#endif
/*
Space game using c and Ncurses for the terminal
by Goran Topic
*/
#define NUMTHRDS 4
pthread_t callThd[NUMTHRDS];
pthread_mutex_t mutex;
void load_ships(void){
/* loads models for the ships */
char* model[] = { " -/|",
"<=[#]:",
" -\\|"
};
Point_ch start = { .x = 3, .y = 3, .ch = ' '};
Object* start_ship = make_obj("start_ship", model, 3, 1, start, "ship");
char* model2[] = {" |\\=",
"C==[X]",
" |/="};
Point_ch start2 = { .x = 8, .y = 5, .ch = ' '};
Object* mauler = make_obj("mauler", model2, 3, 1, start2, "ship");
char* model3[] = {"<=-=-==:",
" [H]D",
"<=-=-==:"};
Point_ch start3 = { .x = 15, .y = 5, .ch = ' '};
Object* floater = make_obj("floater", model3, 3, 1, start3, "ship");
char* model4[] = {" /\\",
"-<[+]:",
" \\/"};
Point_ch start4 = { .x = 7, .y = 5, .ch = ' '};
Object* quicky = make_obj("quicky", model4, 3, 1, start4, "ship");
char* model5[] = {" { |",
" <=[o][:",
" { |"};
Point_ch start5 = { .x = 15, .y = 5, .ch = ' '};
Object* falco = make_obj("falco", model5, 3, 1, start5, "ship");
List* list = list_make();
list_append(list, start_ship);
list_append(list, mauler);
list_append(list, floater);
list_append(list, quicky);
list_append(list, falco);
//list_print(list);
//list_remove_at(list, 3);
//list_remove_title(list, "start_ship");
//list_print(list);
//print_obj(*(list_get_at(list, 0)));
//print_obj(*(list_get_title(list, "falco")));
list_free(list);
}
void clear_screen(void){
/* rewrites the screen before rendeing the objects so that it deletes the old space*/
//get the minimun and max size of the screen
int x_min = 0;
int y_min =0;
int x_max, y_max;
getmaxyx(stdscr, y_max, x_max);
//draw cirlce around window
for(int y = y_min; y < y_max; y++)
for(int x = x_min; x < x_max; x++)
if(x == x_min || x == x_max -1 || y == y_min || y == y_max -1)
mvaddch(y, x, '*');
else
mvaddch(y, x, ' ');
}
void render_obj(Object* obj){
/*render obj in the ncruses initilizer*/
for(int i = 0; i < obj->points_c; i++)
mvaddch(obj->points[i].y, obj->points[i].x, obj->points[i].ch);
}
void render_obj_list(List* list){
/* for every member of the list, it rendes it into the screen */
if(list->first == NULL || list->count == 0){
//link list is empty
printf("ERROR: could not render empty link list\n");
return;
}
Node* current = list->first;
while(true) {
for(int line = 0; line < current->obj->model_c; line++)
render_obj(current->obj);
if(current->next != NULL) current = current->next;
else break;
}
}
void move_objs(List* list){
/*moves the objects acording the theri type */
if(list->first == NULL || list->count == 0){
//link list is empty
printf("ERROR: could not move objects of an empty list\n");
return;
}
Node* current = list->first;
while(true) {
for(int line = 0; line < current->obj->model_c; line++)
move_by_type(current->obj);
if(current->next != NULL) current = current->next;
else break;
}
}
void remove_the_dead(List* list){
/*removes from the list any obj which is dead*/
if(list->first == NULL || list->count == 0){
//link list is empty
printf("ERROR: could not move objects of an empty list\n");
return;
}
Node* current = list->first;
Node* prev = current;
Node* doomed = current;
while(true){
if(current->obj->life <= 0){
//node title found
if(prev == current) list->first = current->next;
else prev->next = current->next; //if is is not the first node
//eliminate remove the node from the list
doomed = current;
current = prev;
node_free(doomed);
list->count--;
}
if(current->next != NULL){
prev = current;
current = current->next;
}else break;
}
}
bool are_collited(Object* obj1, Object* obj2){
/* return true if objects have a point in comon,
or return false if they don't or are the same object */
if(obj1 == obj2) return false; //check if they are the same object
for(int i1 = 0; i1 < obj1->points_c; i1++)
for(int i2 = 0; i2 < obj2->points_c; i2++)
if(obj1->points[i1].x == obj2->points[i2].x
&& obj1->points[i1].y == obj2->points[i2].y){
//printf("x1: %d, x2: %d, y1: %d, y2: %d\n",
// obj1->points[i1].x, obj2->points[i2].x,
// obj1->points[i1].y, obj2->points[i2].x);
return true;
}
return false;
}
void collide_action(Object* obj1, Object* obj2){
/*take action arcding to the type of object which collied*/
bool has_ship = strcmp(obj1->type, "ship") == 0 ||
strcmp(obj2->type, "ship") == 0;
bool has_bullet = strcmp(obj1->type, "bullet") == 0 ||
strcmp(obj2->type, "bullet") == 0;
bool has_player = strcmp(obj1->type, "player") == 0 ||
strcmp(obj2->type, "player") == 0;
bool has_enemy = strcmp(obj1->type, "enemy") == 0 ||
strcmp(obj2->type, "enemy") == 0;
if(has_ship && has_bullet){
//collition with ship and bullet
obj1->life = 0;
obj2->life = 0;
}else if(has_ship && has_player){
//collition with ship and player
//obj1->life = 0;
//obj2->life = 0;
}else if(has_player && has_bullet){
//collition with a bullet and player
//obj1->life = 0;
//obj2->life = 0;
}else if(has_enemy && has_bullet){
//collition with a bullet and player
obj1->life = 0;
obj2->life = 0;
}else if(has_ship){
//collition with ship and ship
obj1->life = 0;
obj2->life = 0;
}else if(has_bullet){
//collition with two bullets
obj1->life = 0;
obj2->life = 0;
}else if(has_player){
//collition two players
}
}
void collition_check(List* list){
/*
check if there is an objs which have the same points
if ti does then it executes and action based on it's type
*/
int i_max = list->count;
Node* cur_node1 = list->first;
Node* cur_node2 = list->first;
while(true){
if(are_collited(cur_node1->obj, cur_node2->obj)){
collide_action(cur_node1->obj, cur_node2->obj);
break;
}
if(cur_node1->next == NULL){
break;
}else if(cur_node2->next == NULL){
cur_node2 = list->first;
cur_node1 = cur_node1->next;
}else{
cur_node2 = cur_node2->next;
}
}
}
void fire(Object* obj){
/* makes object fire a bullet which spans from the the ship */
List* list = obj->parent_list;
if(strcmp(obj->type, "player") == 0){
char* model[] = {"-"};
Point_ch start = { .x = obj->start.x, .y = obj->start.y, .ch = ' '};
Object* bullet = make_obj("fire1", model, 1, 1, start, "bullet");
list_append(list, bullet);
Point_ch start2 = { .x = obj->start.x, .y = obj->start.y+2, .ch = ' '};
bullet = make_obj("fire1", model, 1, 1, start2, "bullet");
list_append(list, bullet);
}else if(strcmp(obj->type, "ship") == 0){
char* model[] = {"-"};
Point_ch start = { .x = obj->start.x - 1 , .y = obj->start.y + 1 , .ch = ' '};
Object* bullet = make_obj("fire1", model, 1, 1, start, "bullet");
list_append(list, bullet);
}
}
void* player_movement(void* vargp){
List* list = (List*)vargp;
Object* ship = list_get_title(list, "ship");
char ch;
while(true){
ch = getch();
//lock mutex
pthread_mutex_lock(&mutex);
if(ch == 2 || ch == 'w'){
move_obj(ship, 180);
}else if(ch == 3){
move_obj(ship, 360);
}else if(ch == 4){
move_obj(ship, 270);
}else if(ch == 5){
move_obj(ship, 90);
}else if(ch == ' '){
fire(ship);
}else if(ch == KEY_LEFT){
move_obj(ship, 180);
}else if(ch == 'q'){
//exit game
}
//unlock mutex
pthread_mutex_unlock (&mutex);
}
return NULL;
}
#ifdef WIN32
#include <windows.h>
#elif _POSIX_C_SOURCE >= 199309L
#include <time.h> // for nanosleep
#else
#include <unistd.h> // for usleep
#endif
void sleep_ms(int milliseconds) // cross-platform sleep function
{
#ifdef WIN32
Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
usleep(milliseconds * 1000);
#endif
}
ALCdevice * openal_output_device;
ALCcontext * openal_output_context;
ALuint internal_buffer;
ALuint streaming_source[1];
int al_check_error(const char * given_label) {
ALenum al_error;
al_error = alGetError();
if(AL_NO_ERROR != al_error) {
printf("ERROR - %s (%s)\n", alGetString(al_error), given_label);
return al_error;
}
return 0;
}
void MM_init_al() {
const char * defname = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
openal_output_device = alcOpenDevice(defname);
openal_output_context = alcCreateContext(openal_output_device, NULL);
alcMakeContextCurrent(openal_output_context);
// setup buffer and source
alGenBuffers(1, & internal_buffer);
al_check_error("failed call to alGenBuffers");
}
void MM_exit_al() {
ALenum errorCode = 0;
// Stop the sources
alSourceStopv(1, & streaming_source[0]); // streaming_source
int ii;
for (ii = 0; ii < 1; ++ii) {
alSourcei(streaming_source[ii], AL_BUFFER, 0);
}
// Clean-up
alDeleteSources(1, &streaming_source[0]);
alDeleteBuffers(16, &streaming_source[0]);
errorCode = alGetError();
alcMakeContextCurrent(NULL);
errorCode = alGetError();
alcDestroyContext(openal_output_context);
alcCloseDevice(openal_output_device);
}
void MM_render_one_buffer() {
MM_init_al();
/* Fill buffer with Sine-Wave */
// float freq = 440.f;
float freq = 100.f;
float incr_freq = 0.2f;
float seconds = 0.5;
// unsigned sample_rate = 22050;
unsigned sample_rate = 44100;
double my_pi = 3.14159;
size_t buf_size = seconds * sample_rate;
// allocate PCM audio buffer
short * samples = malloc(sizeof(short) * buf_size);
//printf("\nhere is freq %f\n", freq);
int i=0;
//add frequncy to buffer
for(; i<buf_size; ++i) {
samples[i] = 32760 * sin( (2.f * my_pi * freq)/sample_rate * i );
freq += incr_freq;
//incr_freq += incr_freq;
//freq *= factor_freq;
}
/* upload buffer to OpenAL */
alBufferData( internal_buffer, AL_FORMAT_MONO16, samples, buf_size, sample_rate);
al_check_error("populating alBufferData");
free(samples);
/* Set-up sound source and play buffer */
ALuint src = 0;
//alGenSources(1, &src);
//alSourcei(src, AL_BUFFER, internal_buffer);
alGenSources(1, & streaming_source[0]);
alSourcei(streaming_source[0], AL_BUFFER, internal_buffer);
//alSourcePlay(src);
alSourcePlay(streaming_source[0]);
// ---------------------
ALenum current_playing_state;
alGetSourcei(streaming_source[0], AL_SOURCE_STATE, & current_playing_state);
al_check_error("alGetSourcei AL_SOURCE_STATE");
while (AL_PLAYING == current_playing_state) {
//printf("still playing ... so sleep\n");
//sleep(1); // should use a thread sleep NOT sleep() for a more responsive finish
alGetSourcei(streaming_source[0], AL_SOURCE_STATE, & current_playing_state);
al_check_error("alGetSourcei AL_SOURCE_STATE");
}
//printf("end of playing\n");
MM_exit_al();
} //MM_render_one_buffer
void make_sound(void){
pthread_t sound_thread;
pthread_create(&sound_thread, NULL, MM_render_one_buffer, NULL);
}
int ncurses_start(void){
//initialize an run ncurse
//the user sturcture
char ch;
int i = 0;
// ncruses initialation
initscr(); //start cruses mode
cbreak(); //be able to ctrl c break
keypad(stdscr, TRUE); // enable key pad
noecho(); //do not echo the input keys
curs_set(0); //hide cursor
//get the minimun and max size of the screen
int x_min = 0, y_min =0;
int x_max, y_max;
getmaxyx(stdscr, y_max, x_max);
int x_half = x_max/2;
int y_half = y_max/2;
char* model[] = {
" -/|",
"<=[#]:",
" -\\|"
};
Point_ch start = { .x = 70, .y = 8, .ch = ' '};
Object* ship = make_obj("ship", model, 3, 1, start, "player");
char* model2[] = {
" |\\=",
"C==[X]",
" |/="
};
Point_ch start2 = { .x = 8, .y = 5, .ch = ' '};
Object* mauler = make_obj("mauler", model2, 3, 1, start2, "ship");
char* model5[] = {
" { |",
" <=[o][",
" { |"
};
Point_ch start5 = { .x = 80, .y = 5, .ch = ' '};
Object* falco = make_obj("falco", model5, 3, 1, start5, "ship");
char* model4[] = {
" /\\",
"-<[+]:",
" \\/"
};
Point_ch start4 = { .x = 100, .y = 100, .ch = ' '};
Object* quicky = make_obj("quicky", model4, 3, 1, start4, "ship");
List* list = list_make();
list_append(list, ship);
list_append(list, mauler);
list_append(list, quicky);
list_append(list, falco);
Point_ch new_start = { .x = 30, .y = 14, .ch = ' '};
reposition_obj(list_get_title(list, "quicky"), new_start);
//initialize mutex for making chnages in th elist of objs
pthread_mutex_init(&mutex, NULL);
//make new thread which is listing for player inputs
pthread_t thread_id;
pthread_create(&thread_id, NULL, player_movement, list);
// create enemy
Obj_arr* enemies = load_enemies_ar();
//list_append(list, random_enemy(0,y_max, enemies));
int timer = 0;
while(true){
sleep_ms(70);
//lock mutex
pthread_mutex_lock(&mutex);
collition_check(list);
remove_the_dead(list);
if(timer % 10 == 0) fire(quicky);
if(timer % 40 ==0){
make_sound();
list_append(list, random_enemy(0,y_max, enemies));
}
move_objs(list);
//unlock mutex
pthread_mutex_unlock (&mutex);
clear_screen();
render_obj_list(list);
refresh(); //refresh screen
if(timer > 10000) timer = 0;
else timer++;
}
endwin(); //end sescion
return 0;
}
int main(void) {
ncurses_start();
}