Skip to content

Commit

Permalink
Some changes for compilance with c99
Browse files Browse the repository at this point in the history
  • Loading branch information
Santurysim committed Nov 5, 2018
1 parent ada5b79 commit 831f03f
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 109 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CFLAGS =-Wall -Wextra -I ./include -g
CFLAGS =-Wall -Wextra -I ./include -g -std=c99
LIBS=`pkg-config --cflags --libs allegro-5 allegro_primitives-5`

/bin/blasteroids: obj/blast.o obj/spaceship.o obj/asteroid.o obj/blasteroids.o
gcc $^ -lm $(CFLAGS) `pkg-config --cflags --libs allegro-5 allegro_primitives-5` -o bin/blasteroids
echo "./blasteroids" > ./bin/run.sh && chmod 777 ./bin/run.sh
bin/blasteroids: obj/blast.o obj/spaceship.o obj/asteroid.o obj/blasteroids.o
gcc $^ -lm $(CFLAGS) $(LIBS) -o bin/blasteroids
printf "#!/bin/sh\n./blasteroids" > ./bin/run.sh && chmod 777 ./bin/run.sh #Temp script for opening the game from Nautilus since it thinks that game is a shared lib

obj/%.o: src/%.c
gcc -c $^ $(CFLAGS) -o $@
Empty file modified README.md
100644 → 100755
Empty file.
7 changes: 6 additions & 1 deletion include/blast.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
#define BLASTEROIDS_BLAST
#include<allegro5/allegro.h>

typedef struct Blast{
#define BLAST_SPEED 50.0f

typedef struct Blast{
float x;
float y;
float rotation;
ALLEGRO_COLOR color;
} Blast;

#endif
19 changes: 0 additions & 19 deletions include/blasteroids.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,6 @@

#define FPS 60

typedef enum Objects{
SPACESHIP,
ASTEROID,
BLAST
} Objects;

typedef union Object{
Spaceship s;
Asteroid a;
Blast b;
} Object;

typedef struct GameObject{
Object object;
Objects type;
} GameObject;

void detect_overlap();

float clamp(float val, float min_val, float max_val);
float repeat(float val, float module);
#endif
29 changes: 29 additions & 0 deletions include/linkedlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef BLASTEROIDS_LINKED_LIST
#define BLASTEROIDS_LINKED_LIST
#include "blast.h"

//Linked list. At this time for blasts

typedef struct Node{
Blast *data;
Node *next;
Node *previous;
} Node;

typedef struct LinkedList{
int size;
Node *begin;
Node* end;
} LinkedList;

LinkedList* create_linked_list();

Node* insert_new_node(LinkedList* list, Node* before);

Node* add_new_node(LinkedList* list);

void delete_node(LinkedList* list, Node* which);

void destroy_list(LinkedList* list);

#endif
8 changes: 4 additions & 4 deletions include/spaceship.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef BLASTEROIDS_SPACESHIP
#define BLASTEROIDS_SPACESHIP

#include<math.h>
#include<allegro5/allegro.h>
#include<allegro5/allegro_primitives.h>

#define M_PI 3.1415926535897932384626433832795L

#define SPACESHIP_ACCELERATION 10.0f
#define SPACESHIP_ANGULAR_VELOCITY M_PI
#define SPACESHIP_MAX_SPEED 40.0f
#define SPACESHIP_ACCELERATION 20.0f
#define SPACESHIP_ANGULAR_VELOCITY 2*M_PI
#define SPACESHIP_MAX_SPEED 60.0f
#define SPACESHIP_MIN_SPEED 0.0f

typedef struct Spaceship {
Expand Down
Empty file modified src/asteroid.c
100644 → 100755
Empty file.
Empty file modified src/blast.c
100644 → 100755
Empty file.
160 changes: 79 additions & 81 deletions src/blasteroids.c
Original file line number Diff line number Diff line change
@@ -1,94 +1,92 @@
#include "blasteroids.h"
#include <stdio.h>

int main(/*int argc, char const *argv[]*/) {
al_init();
al_init_primitives_addon();
al_install_keyboard();
ALLEGRO_DISPLAY* display = al_create_display(800, 640);
al_clear_to_color(al_map_rgba(0,0,0,255));
al_flip_display();
int main(void) {
ALLEGRO_DISPLAY *display;
int display_width, display_height;
Spaceship s;
ALLEGRO_EVENT_QUEUE *q;
ALLEGRO_TIMER *timer;
ALLEGRO_EVENT event;

int display_width = al_get_display_width(display);
int display_height = al_get_display_height(display);
al_init();
al_init_primitives_addon();
al_install_keyboard();
display = al_create_display(800, 640);
al_clear_to_color(al_map_rgba(0,0,0,255));
al_flip_display();

Spaceship s;
s.x = display_width/2;
s.y = display_height/2;
s.rotation = M_PI;
display_width = al_get_display_width(display);
display_height = al_get_display_height(display);

s.rotating = 0;
s.speed = 0;
s.accelerating = 0;
s.lifes = 3;
s.color = al_map_rgb(255,255,255);
s.x = display_width/2;
s.y = display_height/2;
s.rotation = M_PI;

ALLEGRO_EVENT_QUEUE *q = al_create_event_queue();
al_register_event_source(q, al_get_keyboard_event_source());
ALLEGRO_TIMER *timer = al_create_timer(1.0f/FPS);
al_register_event_source(q, al_get_timer_event_source(timer));
ALLEGRO_EVENT event;
al_start_timer(timer);
while (true) {
al_wait_for_event(q, &event);
if(event.type == ALLEGRO_EVENT_KEY_DOWN){
switch (event.keyboard.keycode) {
case ALLEGRO_KEY_LEFT:
s.rotating = 1;
break;
case ALLEGRO_KEY_RIGHT:
s.rotating = -1;
break;
case ALLEGRO_KEY_UP:
s.accelerating = 1;
break;
case ALLEGRO_KEY_DOWN:
s.accelerating = -1;
break;
case ALLEGRO_KEY_SPACE:
//TODO
break;
}
}else if(event.type == ALLEGRO_EVENT_KEY_UP){
switch (event.keyboard.keycode) {
case ALLEGRO_KEY_LEFT: //fall through
case ALLEGRO_KEY_RIGHT:
s.rotating = 0;
break;
case ALLEGRO_KEY_UP: //fall through
case ALLEGRO_KEY_DOWN:
s.accelerating = 0;
break;
case ALLEGRO_KEY_SPACE:
//TODO
break;
}
}else if (event.type == ALLEGRO_EVENT_TIMER){
//Redraw scene, ...
al_clear_to_color(al_map_rgb(0,0,0));
draw_spaceship(&s);
al_flip_display();
s.rotating = 0;
s.speed = 0;
s.accelerating = 0;
s.lifes = 3;
s.color = al_map_rgb(255,255,255);

q = al_create_event_queue();
al_register_event_source(q, al_get_keyboard_event_source());
timer = al_create_timer(1.0f/FPS);
al_register_event_source(q, al_get_timer_event_source(timer));
al_start_timer(timer);
while (true) {
al_wait_for_event(q, &event);
if(event.type == ALLEGRO_EVENT_KEY_DOWN){
switch (event.keyboard.keycode) {
case ALLEGRO_KEY_LEFT:
s.rotating = 1;
break;
case ALLEGRO_KEY_RIGHT:
s.rotating = -1;
break;
case ALLEGRO_KEY_UP:
s.accelerating = 1;
break;
case ALLEGRO_KEY_DOWN:
s.accelerating = -1;
break;
case ALLEGRO_KEY_SPACE:
//TODO
break;
}
}else if(event.type == ALLEGRO_EVENT_KEY_UP){
switch (event.keyboard.keycode) { //TODO: need to get adequate input. Considering using multiple threads
case ALLEGRO_KEY_LEFT: //fall through
case ALLEGRO_KEY_RIGHT:
s.rotating = 0;
break;
case ALLEGRO_KEY_UP: //fall through
case ALLEGRO_KEY_DOWN:
s.accelerating = 0;
break;
case ALLEGRO_KEY_SPACE:
//TODO
break;
}
}else if (event.type == ALLEGRO_EVENT_TIMER){
//Redraw scene, ...
al_clear_to_color(al_map_rgb(0,0,0));
draw_spaceship(&s);
al_flip_display();

//... then compute physics
//s.rotation += SPACESHIP_ANGULAR_VELOCITY*s.rotating/FPS;
s.rotation = repeat(s.rotation + SPACESHIP_ANGULAR_VELOCITY*s.rotating/FPS, 2*M_PI);
s.speed = clamp(s.speed + SPACESHIP_ACCELERATION*s.accelerating*(1.0f/FPS), SPACESHIP_MIN_SPEED, SPACESHIP_MAX_SPEED);
//... then compute physics
//s.rotation += SPACESHIP_ANGULAR_VELOCITY*s.rotating/FPS;
s.rotation = fmod(s.rotation + SPACESHIP_ANGULAR_VELOCITY*s.rotating/FPS, 2*M_PI);
s.speed = clamp(s.speed + SPACESHIP_ACCELERATION*s.accelerating*(1.0f/FPS), SPACESHIP_MIN_SPEED, SPACESHIP_MAX_SPEED);

s.x = clamp(s.x + s.speed*sin(s.rotation)*(1.0f/FPS), 0, (float)display_width);
s.y = clamp(s.y + s.speed*cos(s.rotation)*(1.0f/FPS), 0, (float)display_height);
s.x = clamp(s.x + s.speed*sin(s.rotation)*(1.0f/FPS), 0, (float)display_width);
s.y = clamp(s.y + s.speed*cos(s.rotation)*(1.0f/FPS), 0, (float)display_height);
}
}
}
return 0;
return 0;
}

float clamp(float val, float min_val, float max_val){
if(val > max_val) val = max_val;
else if(val < min_val) val = min_val;
return val;
}

float repeat(float val, float module){
if(val < 0) val += module;
else if(val > module) val -= module;
return val;
if(val > max_val) val = max_val;
else if(val < min_val) val = min_val;
return val;
}
29 changes: 29 additions & 0 deletions src/linkedlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "linkedlist.h"
#include<stdlib.h>

LinkedList* create_linked_list(){
LinkedList* list = malloc(sizeof(LinkedList));
list->size = 0;
list->begin = NULL;
list->end = NULL;
return list;
}

Node* add_new_node(LinkedList* list){
Node* new_node = malloc(sizeof(Node));
new_node->data = NULL;
new_node->next = NULL;
new_node->previous = list->end;
if(list->size == 0){
list->start = new_node;
list->end = new_node;
} else list->end->next = new_node;
list->size += 1;
return new_node;
}

Node* insert_new_node(LinkedList* list, Node* before){
Node* new_node = malloc(sizeof(Node));
new_node->data = NULL;
new_node->next = before;
}
Empty file modified src/spaceship.c
100644 → 100755
Empty file.

0 comments on commit 831f03f

Please sign in to comment.