code wrap-up, code wrap-uuuuup!

This commit is contained in:
Sebastian Krzyszkowiak 2012-02-25 22:26:31 +01:00
parent c215af797d
commit cb2d44d3e5
6 changed files with 171 additions and 162 deletions

View file

@ -10,7 +10,7 @@ void About_Draw(struct Game *game) {
if (x<0) x=0;
al_draw_bitmap_region(game->about.text_bitmap, 0, x*al_get_bitmap_height(game->about.text_bitmap), al_get_bitmap_width(game->about.text_bitmap), al_get_display_height(game->display)*0.8, al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.1, 0);
game->about.x+=0.00025;
if (game->about.x>1) {
if (game->about.x>1) {
UnloadGameState(game);
game->gamestate = GAMESTATE_LOADING;
game->loadstate = GAMESTATE_MENU;
@ -29,6 +29,7 @@ void About_Load(struct Game *game) {
al_destroy_bitmap(game->about.fade_bitmap);
About_Draw(game);
}
int About_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
if (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
UnloadGameState(game);
@ -37,6 +38,7 @@ int About_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
}
return 0;
}
void About_Preload(struct Game *game) {
game->about.image = al_load_bitmap( "data/table.png" );
game->about.letter = al_load_bitmap( "data/letter.png" );
@ -111,6 +113,7 @@ void About_Preload(struct Game *game) {
al_set_target_bitmap(al_get_backbuffer(game->display));
}
void About_Unload(struct Game *game) {
if (game->about.x<0) game->about.x=0;
ALLEGRO_EVENT ev;

View file

@ -43,6 +43,7 @@ void Intro_Load(struct Game *game) {
}
Intro_Draw(game);
}
int Intro_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
if (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
UnloadGameState(game);
@ -57,6 +58,7 @@ int Intro_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
}
return 0;
}
void Intro_Preload(struct Game *game) {
game->intro.position = 0;
game->intro.page = 0;
@ -125,6 +127,7 @@ void Intro_Preload(struct Game *game) {
PrintConsole(game, "Chainpreloading GAMESTATE_MAP...");
Map_Preload(game);
}
void Intro_Unload(struct Game *game) {
ALLEGRO_EVENT ev;
for(int fadeloop=255; fadeloop>=0; fadeloop-=10){

View file

@ -36,6 +36,7 @@ void Level_Load(struct Game *game) {
al_destroy_bitmap(game->level.fade_bitmap);
Level_Draw(game);
}
int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
if (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
UnloadGameState(game);
@ -44,6 +45,7 @@ int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
}
return 0;
}
void Level_Preload(struct Game *game) {
game->level.derpy_frame = 0;
game->level.derpy_frame_tmp = 0;
@ -66,6 +68,7 @@ void Level_Preload(struct Game *game) {
al_draw_textf(game->font, al_map_rgb(255,255,255), al_get_display_width(game->display)/2, al_get_display_height(game->display)/2, ALLEGRO_ALIGN_CENTRE, "Level %d: Not implemented yet!", game->level.current_level);
al_set_target_bitmap(al_get_backbuffer(game->display));
}
void Level_Unload(struct Game *game) {
ALLEGRO_EVENT ev;
game->level.fade_bitmap = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display));

View file

@ -100,174 +100,172 @@ void LoadGameState(struct Game *game) {
}
int main(int argc, char **argv){
srand(time(NULL));
srand(time(NULL));
InitConfig();
InitConfig();
//ALLEGRO_DISPLAY_MODE disp_data;
bool redraw = true;
bool redraw = true;
struct Game game;
struct Game game;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR^ALLEGRO_MAG_LINEAR);
game.timer = al_create_timer(ALLEGRO_BPS_TO_SECS(FPS));
if(!game.timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}
if(!al_init_image_addon()) {
fprintf(stderr, "failed to initialize image addon!\n");
//al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!",
// NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}
if(!al_init_acodec_addon()){
fprintf(stderr, "failed to initialize audio codecs!\n");
return -1;
}
if(!al_install_audio()){
fprintf(stderr, "failed to initialize audio!\n");
return -1;
}
if(!al_install_keyboard()){
fprintf(stderr, "failed to initialize keyboard!\n");
return -1;
}
if (!al_reserve_samples(10)){
fprintf(stderr, "failed to reserve samples!\n");
return -1;
}
al_init_font_addon();
if(!al_init_ttf_addon()){
fprintf(stderr, "failed to initialize fonts!\n");
return -1;
}
if (FULLSCREEN) al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST);
game.display = al_create_display(DISPLAY_WIDTH, DISPLAY_HEIGHT);
if(!game.display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_set_window_title(game.display, "Super Derpy: Muffin Attack");
if (FULLSCREEN) al_hide_mouse_cursor(game.display);
game.font = al_load_ttf_font("data/ShadowsIntoLight.ttf",al_get_display_height(game.display)*0.09,0 );
game.font_console = al_load_ttf_font("data/DejaVuSansMono.ttf",al_get_display_height(game.display)*0.018,0 );
game.event_queue = al_create_event_queue();
if(!game.event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
al_destroy_display(game.display);
return -1;
}
al_register_event_source(game.event_queue, al_get_display_event_source(game.display));
al_register_event_source(game.event_queue, al_get_timer_event_source(game.timer));
al_register_event_source(game.event_queue, al_get_keyboard_event_source());
game.showconsole = DEBUG_MODE;
game.console = al_create_bitmap(al_get_display_width(game.display), al_get_display_height(game.display)*0.12);
al_set_target_bitmap(game.console);
al_clear_to_color(al_map_rgba(0,0,0,80));
al_set_target_bitmap(al_get_backbuffer(game.display));
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_start_timer(game.timer);
game.loadstate = GAMESTATE_LOADING;
PreloadGameState(&game);
LoadGameState(&game);
game.loadstate = GAMESTATE_MENU;
while(1)
{
ALLEGRO_EVENT ev;
al_wait_for_event(game.event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_TIMER) {
redraw = true;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
}
else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (ev.keyboard.keycode == ALLEGRO_KEY_TILDE)) {
game.showconsole = !game.showconsole;
}
KEYDOWN_STATE(GAMESTATE_MENU, Menu)
KEYDOWN_STATE(GAMESTATE_LOADING, Loading)
KEYDOWN_STATE(GAMESTATE_ABOUT, About)
KEYDOWN_STATE(GAMESTATE_INTRO, Intro)
KEYDOWN_STATE(GAMESTATE_MAP, Map)
KEYDOWN_STATE(GAMESTATE_LEVEL, Level)
else {
game.showconsole = true;
PrintConsole(&game, "ERROR: Keystroke in unknown (%d) gamestate! (5 sec sleep)", game.gamestate);
DrawConsole(&game);
al_flip_display();
al_rest(5.0);
PrintConsole(&game, "Returning to menu...");
game.gamestate = GAMESTATE_LOADING;
game.loadstate = GAMESTATE_MENU;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
}
if(redraw && al_is_event_queue_empty(game.event_queue)) {
redraw = false;
switch (game.gamestate) {
DRAW_STATE(GAMESTATE_MENU, Menu)
DRAW_STATE(GAMESTATE_LOADING, Loading)
DRAW_STATE(GAMESTATE_ABOUT, About)
DRAW_STATE(GAMESTATE_INTRO, Intro)
DRAW_STATE(GAMESTATE_MAP, Map)
DRAW_STATE(GAMESTATE_LEVEL, Level)
default:
game.showconsole = true;
PrintConsole(&game, "ERROR: Unknown gamestate %d reached! (5 sec sleep)", game.gamestate);
al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR^ALLEGRO_MAG_LINEAR);
game.timer = al_create_timer(ALLEGRO_BPS_TO_SECS(FPS));
if(!game.timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}
if(!al_init_image_addon()) {
fprintf(stderr, "failed to initialize image addon!\n");
//al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!",
// NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}
if(!al_init_acodec_addon()){
fprintf(stderr, "failed to initialize audio codecs!\n");
return -1;
}
if(!al_install_audio()){
fprintf(stderr, "failed to initialize audio!\n");
return -1;
}
if(!al_install_keyboard()){
fprintf(stderr, "failed to initialize keyboard!\n");
return -1;
}
if (!al_reserve_samples(10)){
fprintf(stderr, "failed to reserve samples!\n");
return -1;
}
al_init_font_addon();
if(!al_init_ttf_addon()){
fprintf(stderr, "failed to initialize fonts!\n");
return -1;
}
if (FULLSCREEN) al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST);
game.display = al_create_display(DISPLAY_WIDTH, DISPLAY_HEIGHT);
if(!game.display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_set_window_title(game.display, "Super Derpy: Muffin Attack");
if (FULLSCREEN) al_hide_mouse_cursor(game.display);
game.font = al_load_ttf_font("data/ShadowsIntoLight.ttf",al_get_display_height(game.display)*0.09,0 );
game.font_console = al_load_ttf_font("data/DejaVuSansMono.ttf",al_get_display_height(game.display)*0.018,0 );
game.event_queue = al_create_event_queue();
if(!game.event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
al_destroy_display(game.display);
return -1;
}
al_register_event_source(game.event_queue, al_get_display_event_source(game.display));
al_register_event_source(game.event_queue, al_get_timer_event_source(game.timer));
al_register_event_source(game.event_queue, al_get_keyboard_event_source());
game.showconsole = DEBUG_MODE;
game.console = al_create_bitmap(al_get_display_width(game.display), al_get_display_height(game.display)*0.12);
al_set_target_bitmap(game.console);
al_clear_to_color(al_map_rgba(0,0,0,80));
al_set_target_bitmap(al_get_backbuffer(game.display));
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_start_timer(game.timer);
game.loadstate = GAMESTATE_LOADING;
PreloadGameState(&game);
LoadGameState(&game);
game.loadstate = GAMESTATE_MENU;
while(1) {
ALLEGRO_EVENT ev;
al_wait_for_event(game.event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_TIMER) {
redraw = true;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
}
else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (ev.keyboard.keycode == ALLEGRO_KEY_TILDE)) {
game.showconsole = !game.showconsole;
}
KEYDOWN_STATE(GAMESTATE_MENU, Menu)
KEYDOWN_STATE(GAMESTATE_LOADING, Loading)
KEYDOWN_STATE(GAMESTATE_ABOUT, About)
KEYDOWN_STATE(GAMESTATE_INTRO, Intro)
KEYDOWN_STATE(GAMESTATE_MAP, Map)
KEYDOWN_STATE(GAMESTATE_LEVEL, Level)
else {
game.showconsole = true;
PrintConsole(&game, "ERROR: Keystroke in unknown (%d) gamestate! (5 sec sleep)", game.gamestate);
DrawConsole(&game);
al_flip_display();
al_rest(5.0);
PrintConsole(&game, "Returning to menu...");
game.gamestate = GAMESTATE_LOADING;
game.loadstate = GAMESTATE_MENU;
}
}
if(redraw && al_is_event_queue_empty(game.event_queue)) {
redraw = false;
switch (game.gamestate) {
DRAW_STATE(GAMESTATE_MENU, Menu)
DRAW_STATE(GAMESTATE_LOADING, Loading)
DRAW_STATE(GAMESTATE_ABOUT, About)
DRAW_STATE(GAMESTATE_INTRO, Intro)
DRAW_STATE(GAMESTATE_MAP, Map)
DRAW_STATE(GAMESTATE_LEVEL, Level)
default:
game.showconsole = true;
PrintConsole(&game, "ERROR: Unknown gamestate %d reached! (5 sec sleep)", game.gamestate);
DrawConsole(&game);
al_flip_display();
al_rest(5.0);
PrintConsole(&game, "Returning to menu...");
game.gamestate = GAMESTATE_LOADING;
game.loadstate = GAMESTATE_MENU;
break;
}
DrawConsole(&game);
al_flip_display();
al_rest(5.0);
PrintConsole(&game, "Returning to menu...");
game.gamestate = GAMESTATE_LOADING;
game.loadstate = GAMESTATE_MENU;
break;
}
}
UnloadGameState(&game);
if (game.gamestate != GAMESTATE_LOADING) {
game.gamestate = GAMESTATE_LOADING;
UnloadGameState(&game);
}
al_clear_to_color(al_map_rgb(0,0,0));
PrintConsole(&game, "Shutting down...");
DrawConsole(&game);
al_flip_display();
}
}
UnloadGameState(&game);
if (game.gamestate != GAMESTATE_LOADING) {
game.gamestate = GAMESTATE_LOADING;
UnloadGameState(&game);
}
al_clear_to_color(al_map_rgb(0,0,0));
PrintConsole(&game, "Shutting down...");
DrawConsole(&game);
al_flip_display();
al_rest(0.1);
al_destroy_timer(game.timer);
al_destroy_display(game.display);
al_destroy_bitmap(game.loading.loading_bitmap);
al_destroy_event_queue(game.event_queue);
al_destroy_font(game.font);
al_destroy_font(game.font_console);
al_uninstall_audio();
DeinitConfig();
return 0;
al_rest(0.1);
al_destroy_timer(game.timer);
al_destroy_display(game.display);
al_destroy_bitmap(game.loading.loading_bitmap);
al_destroy_event_queue(game.event_queue);
al_destroy_font(game.font);
al_destroy_font(game.font_console);
al_uninstall_audio();
DeinitConfig();
return 0;
}

View file

@ -48,6 +48,7 @@ void Map_Load(struct Game *game) {
}
Map_Draw(game);
}
int Map_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
if ((((game->map.selected<4) || (game->map.selected==6)) && (ev->keyboard.keycode==ALLEGRO_KEY_LEFT)) || ((game->map.selected>4) && (game->map.selected!=6) && (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT)) || ((game->map.selected==4) && (ev->keyboard.keycode==ALLEGRO_KEY_UP)) || ((game->map.selected==6) && (ev->keyboard.keycode==ALLEGRO_KEY_DOWN))) {
game->map.selected--;
@ -104,6 +105,7 @@ void Map_Preload(struct Game *game) {
al_draw_scaled_bitmap(game->map.highlight,0,0,al_get_bitmap_width(game->map.highlight),al_get_bitmap_height(game->map.highlight),0,0,al_get_display_width(game->display), al_get_display_height(game->display),0);
al_set_target_bitmap(al_get_backbuffer(game->display));
}
void Map_Unload(struct Game *game) {
game->level.current_level = game->map.selected;
ALLEGRO_EVENT ev;

View file

@ -216,4 +216,4 @@ int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
if (game->menu.selected==-1) game->menu.selected=3;
if (game->menu.selected==4) game->menu.selected=0;
return 0;
}
}