diff --git a/src/level.c b/src/level.c index 5b5993d..e239230 100644 --- a/src/level.c +++ b/src/level.c @@ -81,20 +81,9 @@ int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { return 0; } -void Level_Preload(struct Game *game) { - PrintConsole(game, "Initializing level %d...", game->level.current_level); +void Level_PreloadBitmaps(struct Game *game) { game->level.image =LoadScaledBitmap("table.png", al_get_display_width(game->display), al_get_display_height(game->display)); - game->level.sample = al_load_sample( "data/moonwalk.flac" ); game->level.derpy_walkcycle = LoadScaledBitmap("derpcycle.png", al_get_display_width(game->display)*0.1953125*6, al_get_display_height(game->display)*0.25*4); - - if (!game->level.sample){ - fprintf(stderr, "Audio clip sample not loaded!\n" ); - exit(-1); - } - - game->level.music = al_create_sample_instance(game->level.sample); - al_attach_sample_instance_to_mixer(game->level.music, game->audio.music); - al_set_sample_instance_playmode(game->level.music, ALLEGRO_PLAYMODE_LOOP); game->level.derpy = al_create_bitmap(al_get_display_width(game->display)*0.1953125, al_get_display_height(game->display)*0.25); @@ -104,9 +93,30 @@ 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.2, ALLEGRO_ALIGN_CENTRE, "Level %d: Not implemented yet!", game->level.current_level); al_draw_text(game->font, al_map_rgb(255,255,255), al_get_display_width(game->display)/2, al_get_display_height(game->display)/1.8, ALLEGRO_ALIGN_CENTRE, "Have some moonwalk instead."); al_set_target_bitmap(al_get_backbuffer(game->display)); +} + +void Level_Preload(struct Game *game) { + PrintConsole(game, "Initializing level %d...", game->level.current_level); + game->level.sample = al_load_sample( "data/moonwalk.flac" ); + game->level.music = al_create_sample_instance(game->level.sample); + al_attach_sample_instance_to_mixer(game->level.music, game->audio.music); + al_set_sample_instance_playmode(game->level.music, ALLEGRO_PLAYMODE_LOOP); + + if (!game->level.sample){ + fprintf(stderr, "Audio clip sample not loaded!\n" ); + exit(-1); + } + + Level_PreloadBitmaps(game); Pause_Preload(game); } +void Level_UnloadBitmaps(struct Game *game) { + al_destroy_bitmap(game->level.image); + al_destroy_bitmap(game->level.derpy); + al_destroy_bitmap(game->level.derpy_walkcycle); +} + void Level_Unload(struct Game *game) { Pause_Unload_Real(game); ALLEGRO_EVENT ev; @@ -124,9 +134,7 @@ void Level_Unload(struct Game *game) { DrawConsole(game); al_flip_display(); } - al_destroy_bitmap(game->level.image); - al_destroy_bitmap(game->level.derpy); - al_destroy_bitmap(game->level.derpy_walkcycle); + Level_UnloadBitmaps(game); al_destroy_bitmap(game->level.fade_bitmap); al_destroy_sample_instance(game->level.music); al_destroy_sample(game->level.sample); diff --git a/src/level.h b/src/level.h index 9853a93..350549c 100644 --- a/src/level.h +++ b/src/level.h @@ -25,3 +25,5 @@ void Level_Preload(struct Game *game); void Level_Unload(struct Game *game); void Level_Load(struct Game *game); int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev); +void Level_UnloadBitmaps(struct Game *game); +void Level_PreloadBitmaps(struct Game *game); \ No newline at end of file diff --git a/src/main.c b/src/main.c index d90a746..b60417c 100644 --- a/src/main.c +++ b/src/main.c @@ -240,6 +240,30 @@ float tps(struct Game *game, float t) { return t/game->fps; } +int Shared_Load(struct Game *game) { + game->font = al_load_ttf_font("data/ShadowsIntoLight.ttf",al_get_display_height(game->display)*0.09,0 ); + if(!game->font) { + fprintf(stderr, "failed to load game font!\n"); + return -1; + } + game->font_console = al_load_ttf_font("data/DejaVuSansMono.ttf",al_get_display_height(game->display)*0.018,0 ); + if(!game->font_console) { + fprintf(stderr, "failed to load console font!\n"); + return -1; + } + 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)); + return 0; +} + +void Shared_Unload(struct Game *game) { + al_destroy_font(game->font); + al_destroy_font(game->font_console); + al_destroy_bitmap(game->console); +} + int main(int argc, char **argv){ srand(time(NULL)); @@ -321,17 +345,9 @@ int main(int argc, char **argv){ al_set_new_bitmap_flags(ALLEGRO_MAG_LINEAR | ALLEGRO_MIN_LINEAR); //al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_32_WITH_ALPHA); - game.font = al_load_ttf_font("data/ShadowsIntoLight.ttf",al_get_display_height(game.display)*0.09,0 ); - if(!game.font) { - fprintf(stderr, "failed to load game font!\n"); - return -1; - } - game.font_console = al_load_ttf_font("data/DejaVuSansMono.ttf",al_get_display_height(game.display)*0.018,0 ); - if(!game.font_console) { - fprintf(stderr, "failed to load console font!\n"); - return -1; - } - + int ret = Shared_Load(&game); + if (ret!=0) return ret; + game.event_queue = al_create_event_queue(); if(!game.event_queue) { fprintf(stderr, "failed to create event_queue!\n"); @@ -354,10 +370,6 @@ int main(int argc, char **argv){ al_register_event_source(game.event_queue, al_get_keyboard_event_source()); game.showconsole = game.debug; - 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(); @@ -370,7 +382,7 @@ int main(int argc, char **argv){ game.loadstate = GAMESTATE_LOADING; PreloadGameState(&game); LoadGameState(&game); - game.loadstate = GAMESTATE_MENU; + game.loadstate = GAMESTATE_LEVEL; while(1) { ALLEGRO_EVENT ev; al_wait_for_event(game.event_queue, &ev); @@ -423,18 +435,19 @@ int main(int argc, char **argv){ al_flip_display(); al_rest(0.1); al_destroy_timer(game.timer); + Shared_Unload(&game); al_destroy_display(game.display); al_destroy_event_queue(game.event_queue); - al_destroy_font(game.font); - al_destroy_font(game.font_console); al_destroy_mixer(game.audio.fx); al_destroy_mixer(game.audio.music); al_destroy_mixer(game.audio.mixer); al_destroy_voice(game.audio.voice); al_uninstall_audio(); - al_shutdown_ttf_addon(); - al_shutdown_font_addon(); DeinitConfig(); - if (game.restart) return main(argc, argv); + if (game.restart) { + al_shutdown_ttf_addon(); + al_shutdown_font_addon(); + return main(argc, argv); + } return 0; } diff --git a/src/main.h b/src/main.h index 58d80a5..69e566a 100644 --- a/src/main.h +++ b/src/main.h @@ -223,4 +223,7 @@ float tps(struct Game *game, float t); void DrawGameState(struct Game *game); +int Shared_Load(struct Game *game); +void Shared_Unload(struct Game *game); + #endif diff --git a/src/menu.c b/src/menu.c index e0d30e2..1cc256a 100644 --- a/src/menu.c +++ b/src/menu.c @@ -223,11 +223,12 @@ void Menu_Stop(struct Game* game) { } al_stop_sample_instance(game->menu.music); al_stop_sample_instance(game->menu.rain_sound); + al_destroy_bitmap(game->menu.menu_fade_bitmap); } void Menu_Unload(struct Game *game) { + if (!game->menu.loaded) return; if (game->gamestate==GAMESTATE_MENU) Menu_Stop(game); - al_destroy_bitmap(game->menu.menu_fade_bitmap); al_destroy_bitmap(game->menu.pinkcloud); al_destroy_bitmap(game->menu.image); al_destroy_bitmap(game->menu.cloud); diff --git a/src/pause.c b/src/pause.c index 1818f61..df81226 100644 --- a/src/pause.c +++ b/src/pause.c @@ -22,6 +22,8 @@ #include "config.h" #include "pause.h" #include "menu.h" +#include "level.h" +#include "loading.h" int Pause_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { if ((game->menu.menustate==MENUSTATE_OPTIONS) && ((ev->keyboard.keycode==ALLEGRO_KEY_ESCAPE) || ((ev->keyboard.keycode==ALLEGRO_KEY_ENTER) && (game->menu.selected==3)))) { @@ -34,6 +36,25 @@ int Pause_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { game->menu.menustate=MENUSTATE_OPTIONS; game->menu.selected=0; PrintConsole(game, "menu state changed %d", game->menu.menustate); + if (game->menu.options.fullscreen!=game->fullscreen) { + al_toggle_display_flag(game->display, ALLEGRO_FULLSCREEN_WINDOW, game->menu.options.fullscreen); + al_clear_to_color(al_map_rgb(0,0,0)); + al_flip_display(); + game->fullscreen = game->menu.options.fullscreen; + if (game->fullscreen) al_hide_mouse_cursor(game->display); + else al_show_mouse_cursor(game->display); + Shared_Unload(game); + Shared_Load(game); + Loading_Unload(game); + Loading_Load(game); + Menu_Unload(game); + Menu_Preload(game); + Level_UnloadBitmaps(game); + Level_PreloadBitmaps(game); + Pause_Unload_Real(game); + Pause_Preload(game); + Pause_Load(game); + } } else return Menu_Keydown(game, ev); return 0; } @@ -42,7 +63,10 @@ void Pause_Preload(struct Game* game) { game->pause.bitmap = NULL; game->pause.derpy = LoadScaledBitmap("derpy_pause.png", al_get_display_width(game->display)*0.53, al_get_display_height(game->display)*0.604); PrintConsole(game,"Pause preloaded."); - if (!game->menu.loaded) Menu_Preload(game); + if (!game->menu.loaded) { + PrintConsole(game,"Pause: Preloading GAMESTATE_MENU..."); + Menu_Preload(game); + } } void Pause_Load(struct Game* game) {