summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLAMMJohnson <john_anthony@lavabit.com>2011-05-29 03:43:30 +0100
committerLAMMJohnson <john_anthony@lavabit.com>2011-05-29 03:43:30 +0100
commitd8b7cb2578182a4aca5b9ee0fab8187d0d471642 (patch)
treee2fa44706cd2425e6829239acabfeb66b3e7a13a
parentfa111159fd88e3ca128b4163efc14ccf9049d82b (diff)
downloadnyancat-d8b7cb2578182a4aca5b9ee0fab8187d0d471642.tar.gz
nyancat-d8b7cb2578182a4aca5b9ee0fab8187d0d471642.tar.bz2
nyancat-d8b7cb2578182a4aca5b9ee0fab8187d0d471642.zip
Broke main function into component functions (as good practise dictates)
-rw-r--r--nyan.c188
1 files changed, 103 insertions, 85 deletions
diff --git a/nyan.c b/nyan.c
index e73c111..923548f 100644
--- a/nyan.c
+++ b/nyan.c
@@ -42,17 +42,20 @@ struct sparkle_instance {
/* Predecs */
static void add_sparkle(void);
static void add_cat(unsigned int x, unsigned int y);
+static void cleanup(void);
static void clear_screen(void);
static void draw_cats(unsigned int frame);
static void draw_sparkles(void);
static void fillsquare(SDL_Surface* surf, int x, int y, int w, int h, Uint32 col);
static void handle_args(int argc, char** argv);
static void handle_input(void);
+static void init(void);
static void load_images(void);
static SDL_Surface* load_image(const char* path);
static void load_music(void);
static void putpix(SDL_Surface* surf, int x, int y, Uint32 col);
static void remove_sparkle(sparkle_instance* s);
+static void run(void);
static void update_sparkles(void);
#ifdef XINERAMA
static void xinerama_add_cats(void);
@@ -78,7 +81,7 @@ static SDL_Surface* sparkle_img[5];
static sparkle_instance* sparkles_list = NULL;
static Uint32 bgcolor;
-void
+static void
add_sparkle(void) {
sparkle_instance* s = sparkles_list;
sparkle_instance* new;
@@ -105,7 +108,7 @@ add_sparkle(void) {
s->next = new;
}
-void
+static void
add_cat(unsigned int x, unsigned int y) {
cat_instance* c = cat_list;
cat_instance* new;
@@ -127,7 +130,16 @@ add_cat(unsigned int x, unsigned int y) {
c->next = new;
}
-void
+static void
+cleanup(void) {
+ Mix_HaltMusic();
+ Mix_FreeMusic(music);
+ Mix_CloseAudio();
+ SDL_Quit();
+}
+
+
+static void
clear_screen(void) {
sparkle_instance *s = sparkles_list;
cat_instance *c = cat_list;
@@ -145,7 +157,7 @@ clear_screen(void) {
}
-void
+static void
draw_cats(unsigned int frame) {
cat_instance* c = cat_list;;
SDL_Rect pos;
@@ -162,7 +174,7 @@ draw_cats(unsigned int frame) {
}
}
-void
+static void
draw_sparkles() {
sparkle_instance* s = sparkles_list;
SDL_Rect pos;
@@ -175,7 +187,7 @@ draw_sparkles() {
}
}
-void
+static void
fillsquare(SDL_Surface* surf, int x, int y, int w, int h, Uint32 col) {
int i, e;
@@ -201,7 +213,7 @@ fillsquare(SDL_Surface* surf, int x, int y, int w, int h, Uint32 col) {
putpix(surf, i, e, col);
}
-void
+static void
handle_args(int argc, char **argv) {
int i;
for (i = 1; i < argc; i++) {
@@ -214,7 +226,7 @@ handle_args(int argc, char **argv) {
}
}
-void
+static void
handle_input(void) {
while( SDL_PollEvent( &event ) ) {
switch (event.type) {
@@ -227,21 +239,44 @@ handle_input(void) {
}
}
+static void
+init(void) {
+ int i;
+
+ srand( time(NULL) );
+
+ SDL_Init( SDL_INIT_EVERYTHING );
+ screen = SDL_SetVideoMode( 0, 0, SCREEN_BPP, SURF_TYPE | SDL_FULLSCREEN );
+ /* SDL_ShowCursor(0); */
+
+ load_images();
+ bgcolor = SDL_MapRGB(screen->format, 0x00, 0x33, 0x66);
+ fillsquare(screen, 0, 0, screen->w, screen->h, bgcolor);
+
+ if(sound) {
+ Mix_OpenAudio( 44100, AUDIO_S16, 2, 256 );
+ load_music();
+ Mix_PlayMusic(music, 0);
+ }
+
#ifdef XINERAMA
-void
-xinerama_add_cats(void) {
- int i, nn;
- XineramaScreenInfo* info = XineramaQueryScreens(dpy, &nn);
+ if (!(dpy = XOpenDisplay(NULL)))
+ puts("Failed to open Xinerama display information.");
+ else
+ xinerama_add_cats();
+#else
+ add_cat((screen->w - cat_img[0]->w) / 2 , (screen->h - cat_img[0]->h) / 2);
+#endif /* Xinerama */
- for (i = 0; i < nn; ++i)
- add_cat(info[i].x_org + ((info[i].width - cat_img[0]->w) / 2), info[i].y_org + ((info[i].height - cat_img[0]->h) / 2));
+ /* clear initial input */
+ while( SDL_PollEvent( &event ) ) {}
- XFree(info);
- XCloseDisplay(dpy);
+ /* Pre-populate with sparkles */
+ for (i = 0; i < 200; i++)
+ update_sparkles();
}
-#endif /* XINERAMA */
-void
+static void
load_images(void) {
/* This obviously needs work */
/* Needs to be replaced with a loop */
@@ -273,7 +308,7 @@ load_images(void) {
}
}
-SDL_Surface*
+static SDL_Surface*
load_image( const char* path ) {
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
@@ -286,7 +321,7 @@ load_image( const char* path ) {
return optimizedImage;
}
-void
+static void
load_music(void) {
music = Mix_LoadMUS("res/nyan.ogg");
if (!music)
@@ -295,13 +330,13 @@ load_music(void) {
printf("Unable to load Ogg file: %s\n", Mix_GetError());
}
-void
+static void
putpix(SDL_Surface* surf, int x, int y, Uint32 col) {
Uint32 *pix = (Uint32 *) surf->pixels;
pix [ ( y * surf->w ) + x ] = col;
}
-void
+static void
remove_sparkle(sparkle_instance* s) {
sparkle_instance* s2 = sparkles_list;
@@ -318,7 +353,36 @@ remove_sparkle(sparkle_instance* s) {
free(s);
}
-void
+static void
+run(void) {
+ unsigned int last_draw, draw_time;
+
+ while( running )
+ {
+ last_draw = SDL_GetTicks();
+
+ clear_screen();
+ update_sparkles();
+ draw_sparkles();
+ draw_cats(curr_frame);
+
+ handle_input();
+ SDL_Flip(screen);
+
+ /* Frame increment and looping */
+ curr_frame++;
+ if (curr_frame > 4)
+ curr_frame = 0;
+
+ draw_time = SDL_GetTicks() - last_draw;
+ if (draw_time < (1000 / FRAMERATE))
+ SDL_Delay((1000 / FRAMERATE) - draw_time);
+ }
+}
+
+
+
+static void
update_sparkles(void) {
sparkle_instance* next, *s = sparkles_list;
@@ -344,71 +408,25 @@ update_sparkles(void) {
}
}
-int main( int argc, char **argv )
-{
- int i, draw_time, last_draw;
-
- handle_args(argc, argv);
-
- srand( time(NULL) );
-
- SDL_Init( SDL_INIT_EVERYTHING );
- screen = SDL_SetVideoMode( 0, 0, SCREEN_BPP, SURF_TYPE | SDL_FULLSCREEN );
- /* SDL_ShowCursor(0); */
-
- load_images();
- bgcolor = SDL_MapRGB(screen->format, 0x00, 0x33, 0x66);
- fillsquare(screen, 0, 0, screen->w, screen->h, bgcolor);
-
- if(sound) {
- Mix_OpenAudio( 44100, AUDIO_S16, 2, 256 );
- load_music();
- Mix_PlayMusic(music, 0);
- }
-
#ifdef XINERAMA
- if (!(dpy = XOpenDisplay(NULL)))
- puts("Failed to open Xinerama display information.");
- else
- xinerama_add_cats();
-#else
- add_cat((screen->w - cat_img[0]->w) / 2 , (screen->h - cat_img[0]->h) / 2);
-#endif /* Xinerama */
-
- /* clear initial input */
- while( SDL_PollEvent( &event ) ) {}
-
- /* Pre-populate with sparkles */
- for (i = 0; i < 200; i++)
- update_sparkles();
-
- /* Main loop */
- while( running )
- {
- last_draw = SDL_GetTicks();
-
- clear_screen();
- update_sparkles();
- draw_sparkles();
- draw_cats(curr_frame);
-
- handle_input();
- SDL_Flip(screen);
-
- /* Frame increment and looping */
- curr_frame++;
- if (curr_frame > 4)
- curr_frame = 0;
+static void
+xinerama_add_cats(void) {
+ int i, nn;
+ XineramaScreenInfo* info = XineramaQueryScreens(dpy, &nn);
- draw_time = SDL_GetTicks() - last_draw;
- if (draw_time < (1000 / FRAMERATE))
- SDL_Delay((1000 / FRAMERATE) - draw_time);
- }
+ for (i = 0; i < nn; ++i)
+ add_cat(info[i].x_org + ((info[i].width - cat_img[0]->w) / 2), info[i].y_org + ((info[i].height - cat_img[0]->h) / 2));
- Mix_HaltMusic();
- Mix_FreeMusic(music);
- Mix_CloseAudio();
+ XFree(info);
+ XCloseDisplay(dpy);
+}
+#endif /* XINERAMA */
- SDL_Quit();
+int main( int argc, char **argv )
+{
+ handle_args(argc, argv);
+ init();
+ run();
+ cleanup();
return 0;
}