From c090445ae53d0ee9134298392e9f7fafddfe48d3 Mon Sep 17 00:00:00 2001 From: Frank Villaro-Dixon Date: Sun, 4 Jan 2015 18:48:47 +0100 Subject: [PATCH] some refactoring --- src/game.c | 69 ++++------------------------------------------- src/game.h | 8 +----- src/graphics.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/graphics.h | 22 +++++++++++++++ src/main.c | 26 +++++++++++++++++- src/sdl.c | 2 +- 6 files changed, 126 insertions(+), 73 deletions(-) create mode 100644 src/graphics.c create mode 100644 src/graphics.h diff --git a/src/game.c b/src/game.c index 83f4684..cab3686 100644 --- a/src/game.c +++ b/src/game.c @@ -11,70 +11,11 @@ #include "game.h" #include "polygons.h" #include "obs.h" +#include "graphics.h" #include "SDL_prims.h" #include "SDL/SDL.h" -union color { - struct { - unsigned char col[4]; - } rgb; - int col; -}; - -int -divide_color(int color, int divider) { - union color col; - - col.col = color; - for(int i = 0; i < 4; i++) { - col.rgb.col[i] /= divider; - } - - return col.col; -} - -void -init_random_color(struct s_game *g) { - union color col; - col.col = 0; - col.rgb.col[0] = rand()%2 ? 0 : 255; - col.rgb.col[1] = rand()%2 ? 0 : 255; - col.rgb.col[2] = rand()%2 ? 0 : 255; - - g->color = col.col; -} - -void -change_game_color(struct s_game *g) { - static int from_canal = 0; - static int to_canal = 1; - - union color col; - - col.col = g->color; - - col.rgb.col[from_canal]--; - col.rgb.col[to_canal]++; - - if(col.rgb.col[from_canal] > 255) { - from_canal = to_canal; - to_canal = (to_canal + rand()%2)%3; - } - - g->color = col.col; -} - - -void -draw_background(struct s_game *g) { //{{{ - int face; - - for(face = 0; face < g->polygon_type; face++) { - draw_polygon_side(g, face, POLYGON_SIZE, SCREEN_DIAGONAL, - divide_color(g->color, 2 + 2*(face%2))); - } -} //}}} int get_face_from_cursor(struct s_game *g) { @@ -169,7 +110,7 @@ init_game(struct s_game *g) { //{{{ } //}}} void -update_game_time(struct s_game *g) { +update_game_time(struct s_game *g) { //{{{ struct timespec actual_time; clock_gettime(CLOCK_REALTIME, &actual_time); @@ -178,10 +119,10 @@ update_game_time(struct s_game *g) { (actual_time.tv_nsec - g->start_time.tv_nsec) / 1e9; g->elapsed_time = secs_passed; -} +} //}}} void -show_score(struct s_game *g) { +show_score(struct s_game *g) { //{{{ SDL_Surface *text; SDL_Color text_color = {255, 255, 255, 0}; char buffer[1024]; @@ -194,4 +135,4 @@ show_score(struct s_game *g) { SDL_BlitSurface(text, NULL, g->screen, NULL); SDL_FreeSurface(text); -} +}//}}} diff --git a/src/game.h b/src/game.h index 58eb9cd..f1f78f3 100644 --- a/src/game.h +++ b/src/game.h @@ -7,6 +7,7 @@ #ifndef GAME_H #define GAME_H +#include #include "SDL/SDL.h" #include "SDL/SDL_ttf.h" #include "SDL_prims.h" @@ -116,16 +117,9 @@ draw_game(struct s_game *g); void get_keys(struct s_game *g); -void -change_game_color(struct s_game *g); - -void -init_random_color(struct s_game *g); - void update_game_time(struct s_game *g); - void show_score(struct s_game *g); diff --git a/src/graphics.c b/src/graphics.c new file mode 100644 index 0000000..653eda8 --- /dev/null +++ b/src/graphics.c @@ -0,0 +1,72 @@ +/* +** graphics.c - <+DESC+> +** +** 2014 - Frank Villaro-Dixon +*/ + +#include +#include +#include "graphics.h" +#include "game.h" +#include "polygons.h" + +union color { + struct { + unsigned char col[4]; + } rgb; + int col; +}; + +int +divide_color(int color, int divider) { //{{{ + union color col; + + col.col = color; + for(int i = 0; i < 4; i++) { + col.rgb.col[i] /= divider; + } + + return col.col; +}//}}} + +void +init_random_color(struct s_game *g) { //{{{ + union color col; + col.col = 0; + col.rgb.col[0] = rand()%2 ? 0 : 255; + col.rgb.col[1] = rand()%2 ? 0 : 255; + col.rgb.col[2] = rand()%2 ? 0 : 255; + + g->color = col.col; +}//}}} + +void +change_game_color(struct s_game *g) { //{{{ + static int from_canal = 0; + static int to_canal = 1; + + union color col; + + col.col = g->color; + + col.rgb.col[from_canal]--; + col.rgb.col[to_canal]++; + + if(col.rgb.col[from_canal] > 255) { + from_canal = to_canal; + to_canal = (to_canal + rand()%2)%3; + } + + g->color = col.col; +}//}}} + + +void +draw_background(struct s_game *g) { //{{{ + int face; + + for(face = 0; face < g->polygon_type; face++) { + draw_polygon_side(g, face, POLYGON_SIZE, SCREEN_DIAGONAL, + divide_color(g->color, 2 + 2*(face%2))); + } +} //}}} diff --git a/src/graphics.h b/src/graphics.h new file mode 100644 index 0000000..145de2e --- /dev/null +++ b/src/graphics.h @@ -0,0 +1,22 @@ +/* +** graphics.h - <+DESC+> +** +** 2014 - Frank Villaro-Dixon +*/ + +#ifndef GRAPHICS_H +#define GRAPHICS_H + +#include "game.h" + +void +change_game_color(struct s_game *g); + +void +init_random_color(struct s_game *g); + +void +draw_background(struct s_game *g); + +#endif /* ndef GRAPHICS_H */ + diff --git a/src/main.c b/src/main.c index 1d38b05..3c47ef0 100644 --- a/src/main.c +++ b/src/main.c @@ -10,6 +10,7 @@ #include #include "SDL/SDL.h" #include "game.h" +#include "graphics.h" #include "sdl.h" #include "obs.h" #include "music.h" @@ -22,10 +23,33 @@ main() struct s_game ga; srand(time(NULL)); - init_music(&ga); init_SDL(&ga); +#if 0 + + printf("Num of joysticks: %d\n", SDL_NumJoysticks()); + SDL_Joystick *joy = SDL_JoystickOpen(0); + if(joy) { + printf("Opened Joystick 0\n"); + printf("Name: %s\n", SDL_JoystickName(0)); + printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy)); + printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy)); + printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy)); + } + + while(1) { + SDL_JoystickUpdate(); + printf("%d %d\n", SDL_JoystickGetAxis(joy, 0), + SDL_JoystickGetAxis(joy, 1)); + printf("%d %d\n", SDL_JoystickGetAxis(joy, 2), + SDL_JoystickGetAxis(joy, 3)); + } + + exit(0); +#endif + + start: init_game(&ga); music_loadnplay(&ga); diff --git a/src/sdl.c b/src/sdl.c index da4d490..a10d2a4 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -14,7 +14,7 @@ void init_SDL(struct s_game *g) { /* initialize SDL */ - if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) { perror("Could not init SDL !"); exit(EXIT_FAILURE); }