some refactoring

This commit is contained in:
Frank Villaro-Dixon 2015-01-04 18:48:47 +01:00
parent 2289ab91ea
commit c090445ae5
6 changed files with 126 additions and 73 deletions

View file

@ -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);
}
}//}}}

View file

@ -7,6 +7,7 @@
#ifndef GAME_H
#define GAME_H
#include <math.h>
#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);

72
src/graphics.c Normal file
View file

@ -0,0 +1,72 @@
/*
** graphics.c - <+DESC+>
**
** 2014 - Frank Villaro-Dixon <Frank@Villaro-Dixon.eu>
*/
#include <stdio.h>
#include <stdlib.h>
#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)));
}
} //}}}

22
src/graphics.h Normal file
View file

@ -0,0 +1,22 @@
/*
** graphics.h - <+DESC+>
**
** 2014 - Frank Villaro-Dixon <Frank@Villaro-Dixon.eu>
*/
#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 */

View file

@ -10,6 +10,7 @@
#include <math.h>
#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);

View file

@ -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);
}