general code redesign

This commit is contained in:
Sebastian Krzyszkowiak 2012-02-16 12:48:48 +01:00
parent 490ea0aa4b
commit d59141bf05

244
main.c
View file

@ -10,91 +10,131 @@
float FPS = 40;
int DISPLAY_WIDTH = 800;
int DISPLAY_HEIGHT = 500;
bool FULLSCREEN = true;
enum gamestate_enum {
GAMESTATE_INTRO,
GAMESTATE_LOADING,
GAMESTATE_MENU
};
enum gamestate_enum gamestate = GAMESTATE_INTRO;
enum gamestate_enum loadstate = GAMESTATE_MENU;
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_SAMPLE *sample = NULL;
ALLEGRO_BITMAP *loading_bitmap = NULL, *menu_bitmap = NULL, *cloud_bitmap = NULL, *pinkcloud_bitmap = NULL;
ALLEGRO_FONT *font;
float cloud_position = 100;
struct Menu {
ALLEGRO_BITMAP *menu_bitmap, *menu_fade_bitmap, *image;
ALLEGRO_BITMAP *cloud_bitmap, *cloud;
ALLEGRO_BITMAP *pinkcloud_bitmap, *pinkcloud;
float cloud_position;
ALLEGRO_SAMPLE *sample;
};
void PreloadGameState(int state) {
al_rest(1.0);
}
struct Loading {
ALLEGRO_BITMAP *loading_bitmap, *image;
};
void LoadGameState(int state) {
if (state==GAMESTATE_MENU) {
struct Game {
ALLEGRO_DISPLAY *display;
ALLEGRO_FONT *font;
enum gamestate_enum gamestate;
enum gamestate_enum loadstate;
ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_TIMER *timer;
struct Menu menu;
struct Loading loading;
};
void PreloadGameState(struct Game *game) {
if (game->loadstate==GAMESTATE_MENU) {
game->menu.cloud_position = 100;
game->menu.image = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display)); //FIXME: WHYYYYYYY?
game->menu.image = al_load_bitmap( "menu.png" );
ALLEGRO_BITMAP *image = al_load_bitmap( "menu.png" );
sample = al_load_sample( "menu.wav" );
ALLEGRO_BITMAP *cloud = al_load_bitmap( "cloud.png" );
ALLEGRO_BITMAP *pinkcloud = al_load_bitmap( "pinkcloud.png" );
game->menu.sample = al_load_sample( "menu.wav" );
game->menu.cloud = al_load_bitmap( "cloud.png" );
game->menu.pinkcloud = al_load_bitmap( "pinkcloud.png" );
if (!sample){
printf( "Audio clip sample not loaded!\n" );
if (!game->menu.sample){
fprintf(stderr, "Audio clip sample not loaded!\n" );
exit(-1);
}
// Scale menu bitmap
menu_bitmap = al_create_bitmap(al_get_display_width(display), al_get_display_height(display));
ALLEGRO_BITMAP *menu_fade_bitmap = al_create_bitmap(al_get_display_width(display), al_get_display_height(display));
al_set_target_bitmap(menu_bitmap);
al_draw_scaled_bitmap(image,0, 0, al_get_bitmap_width(image), al_get_bitmap_height(image), 0, 0, al_get_display_width(display), al_get_display_height(display),0);
al_destroy_bitmap(image);
al_set_target_bitmap(menu_fade_bitmap);
// Scale menu bitmap
game->menu.menu_bitmap = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display));
game->menu.menu_fade_bitmap = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display));
al_set_target_bitmap(game->menu.menu_bitmap);
al_draw_scaled_bitmap(game->menu.image,0, 0, al_get_bitmap_width(game->menu.image), al_get_bitmap_height(game->menu.image), 0, 0, al_get_display_width(game->display), al_get_display_height(game->display),0);
al_destroy_bitmap(game->menu.image);
al_set_target_bitmap(game->menu.menu_fade_bitmap);
al_clear_to_color(al_map_rgb(183,234,193));
al_draw_bitmap(menu_bitmap,0, 0, 0);
al_draw_bitmap(game->menu.menu_bitmap,0, 0, 0);
// Cloud menu bitmap
cloud_bitmap = al_create_bitmap(al_get_display_width(display)*0.5, al_get_display_height(display)*0.25);
al_set_target_bitmap(cloud_bitmap);
al_draw_scaled_bitmap(cloud,0, 0, al_get_bitmap_width(cloud), al_get_bitmap_height(cloud), 0, 0, al_get_bitmap_width(cloud_bitmap), al_get_bitmap_height(cloud_bitmap),0);
al_destroy_bitmap(cloud);
game->menu.cloud_bitmap = al_create_bitmap(al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.25);
al_set_target_bitmap(game->menu.cloud_bitmap);
al_draw_scaled_bitmap(game->menu.cloud,0, 0, al_get_bitmap_width(game->menu.cloud), al_get_bitmap_height(game->menu.cloud), 0, 0, al_get_bitmap_width(game->menu.cloud_bitmap), al_get_bitmap_height(game->menu.cloud_bitmap),0);
al_destroy_bitmap(game->menu.cloud);
pinkcloud_bitmap = al_create_bitmap(al_get_display_width(display)*0.33125, al_get_display_height(display)*0.8122);
al_set_target_bitmap(pinkcloud_bitmap);
al_draw_scaled_bitmap(pinkcloud,0, 0, al_get_bitmap_width(pinkcloud), al_get_bitmap_height(pinkcloud), 0, 0, al_get_bitmap_width(pinkcloud_bitmap), al_get_bitmap_height(pinkcloud_bitmap),0);
al_destroy_bitmap(pinkcloud);
game->menu.pinkcloud_bitmap = al_create_bitmap(al_get_display_width(game->display)*0.33125, al_get_display_height(game->display)*0.8122);
al_set_target_bitmap(game->menu.pinkcloud_bitmap);
al_draw_scaled_bitmap(game->menu.pinkcloud,0, 0, al_get_bitmap_width(game->menu.pinkcloud), al_get_bitmap_height(game->menu.pinkcloud), 0, 0, al_get_bitmap_width(game->menu.pinkcloud_bitmap), al_get_bitmap_height(game->menu.pinkcloud_bitmap),0);
al_destroy_bitmap(game->menu.pinkcloud);
al_set_target_bitmap(menu_fade_bitmap);
al_draw_bitmap(pinkcloud_bitmap,al_get_display_width(display)*0.132, 0,0);
al_draw_text(font, al_map_rgb(255,255,255), al_get_display_width(display)*0.5, al_get_display_height(display)*0.2, ALLEGRO_ALIGN_CENTRE, "Super Derpy");
al_set_target_bitmap(game->menu.menu_fade_bitmap);
al_draw_bitmap(game->menu.pinkcloud_bitmap,al_get_display_width(game->display)*0.132, 0,0);
al_draw_text(game->font, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.2, ALLEGRO_ALIGN_CENTRE, "Super Derpy");
al_set_target_bitmap(al_get_backbuffer(display));
al_play_sample(sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
for(int fadeloop=0; fadeloop<256; fadeloop+=10){
al_draw_tinted_bitmap(menu_fade_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0);
al_set_target_bitmap(al_get_backbuffer(game->display));
}
}
void UnloadGameState(struct Game *game) {
if (game->gamestate==GAMESTATE_MENU) {
al_destroy_sample(game->menu.sample);
for(int fadeloop=255; fadeloop>=0; fadeloop-=10){
al_draw_tinted_bitmap(game->menu.menu_fade_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0);
al_flip_display();
}
al_destroy_bitmap(menu_fade_bitmap);
al_destroy_bitmap(game->menu.menu_fade_bitmap);
}
gamestate = state;
}
void LoadGameState(struct Game *game) {
if (game->loadstate==GAMESTATE_MENU) {
al_play_sample(game->menu.sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
for(int fadeloop=0; fadeloop<256; fadeloop+=10){
al_draw_tinted_bitmap(game->menu.menu_fade_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0);
al_flip_display();
}
}
else if (game->loadstate==GAMESTATE_LOADING) {
game->loading.image = al_load_bitmap( "loading.png" );
// Scale "Loading" bitmap
game->loading.loading_bitmap = al_create_bitmap(al_get_display_width(game->display), al_get_display_height(game->display));
al_set_target_bitmap(game->loading.loading_bitmap);
al_draw_scaled_bitmap(game->loading.image,0, 0, al_get_bitmap_width(game->loading.image), al_get_bitmap_height(game->loading.image), 0, 0, al_get_display_width(game->display), al_get_display_height(game->display),0);
al_draw_text(game->font, al_map_rgb(255,255,255), 30, al_get_display_height(game->display)-120, ALLEGRO_ALIGN_LEFT, "Loading...");
al_set_target_bitmap(al_get_backbuffer(game->display));
al_destroy_bitmap(game->loading.image);
}
game->gamestate = game->loadstate;
game->loadstate = -1;
}
int main(int argc, char **argv){
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *image = NULL;
ALLEGRO_DISPLAY_MODE disp_data;
bool redraw = true;
struct Game game;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
//al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR);
al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR);
timer = al_create_timer(1.0 / FPS);
if(!timer) {
game.timer = al_create_timer(1.0 / FPS);
if(!game.timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}
@ -126,60 +166,47 @@ int main(int argc, char **argv){
return -1;
}
al_init_font_addon(); // initialize the font addon
al_init_ttf_addon();// initialize the ttf (True Type Font) addon
al_init_font_addon();
image = al_load_bitmap( "loading.png" );
sample = al_load_sample( "menu.wav" );
if (!sample){
printf( "Audio clip sample not loaded!\n" );
if(!al_init_ttf_addon()){
fprintf(stderr, "failed to initialize fonts!\n");
return -1;
}
// printf("%d\n", al_get_num_display_modes() );
//al_get_display_mode(0, &disp_data);
al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
display = al_create_display(800,500); //disp_data.width, disp_data.height); //DISPLAY_WIDTH, DISPLAY_HEIGHT);
al_set_window_title(display, "Super Derpy: Muffins Attack");
if(!display) {
if (FULLSCREEN) al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
game.display = al_create_display(DISPLAY_WIDTH, DISPLAY_HEIGHT);
if(!game.display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_hide_mouse_cursor(display);
font = al_load_ttf_font("ShadowsIntoLight.ttf",72,0 );
al_set_window_title(game.display, "Super Derpy: Muffin Attack");
al_hide_mouse_cursor(game.display);
game.font = al_load_ttf_font("ShadowsIntoLight.ttf",72,0 );
// Scale "Loading" bitmap
loading_bitmap = al_create_bitmap(al_get_display_width(display), al_get_display_height(display));
al_set_target_bitmap(loading_bitmap);
al_draw_scaled_bitmap(image,0, 0, al_get_bitmap_width(image), al_get_bitmap_height(image), 0, 0, al_get_display_width(display), al_get_display_height(display),0);
al_draw_text(font, al_map_rgb(255,255,255), 30, al_get_display_height(display)-120, ALLEGRO_ALIGN_LEFT, "Loading...");
al_set_target_bitmap(al_get_backbuffer(display));
al_destroy_bitmap(image);
event_queue = al_create_event_queue();
if(!event_queue) {
game.event_queue = al_create_event_queue();
if(!game.event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
al_destroy_display(display);
al_destroy_display(game.display);
return -1;
}
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
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());
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_start_timer(timer);
//al_rest(2.0);
al_start_timer(game.timer);
LoadGameState(GAMESTATE_INTRO);
game.loadstate = GAMESTATE_LOADING;
PreloadGameState(&game);
LoadGameState(&game);
game.loadstate = GAMESTATE_MENU;
while(1)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
al_wait_for_event(game.event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_TIMER) {
redraw = true;
@ -188,46 +215,45 @@ int main(int argc, char **argv){
break;
}
if(redraw && al_is_event_queue_empty(event_queue)) {
if(redraw && al_is_event_queue_empty(game.event_queue)) {
redraw = false;
if (gamestate==GAMESTATE_INTRO) {
if (game.gamestate==GAMESTATE_LOADING) {
for(int fadeloop=0; fadeloop<256; fadeloop+=10){
al_draw_tinted_bitmap(loading_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0);
al_draw_tinted_bitmap(game.loading.loading_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0);
al_flip_display();
}
al_draw_bitmap(loading_bitmap,0,0,0);
al_draw_bitmap(game.loading.loading_bitmap,0,0,0);
al_flip_display();
PreloadGameState(loadstate);
PreloadGameState(&game);
for(int fadeloop=255; fadeloop>0; fadeloop-=10){
al_draw_tinted_bitmap(loading_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0);
al_draw_tinted_bitmap(game.loading.loading_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0);
al_flip_display();
}
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(0.2);
LoadGameState(loadstate);
//al_rest(0.2);
LoadGameState(&game);
}
else if (gamestate==GAMESTATE_MENU) {
else if (game.gamestate==GAMESTATE_MENU) {
al_clear_to_color(al_map_rgb(183,234,193));
al_draw_scaled_bitmap(cloud_bitmap,0,0,al_get_bitmap_width(cloud_bitmap), al_get_bitmap_height(cloud_bitmap), al_get_display_width(display)*(cloud_position-20)/(-75), al_get_display_height(display)*0.15, al_get_bitmap_width(cloud_bitmap)/2, al_get_bitmap_height(cloud_bitmap)/2,0);
al_draw_bitmap(menu_bitmap,0, 0,0);
al_draw_bitmap(pinkcloud_bitmap,(al_get_display_width(display)*0.132) + (cos((cloud_position/50+80)*1.74444))*20, 0,0);
al_draw_bitmap(cloud_bitmap,al_get_display_width(display)*cloud_position/100, 30,0);
al_draw_text(font, al_map_rgb(255,255,255), al_get_display_width(display)*0.5, al_get_display_height(display)*0.2, ALLEGRO_ALIGN_CENTRE, "Super Derpy");
cloud_position-=0.2;
if (cloud_position<-80) cloud_position=100;
al_draw_scaled_bitmap(game.menu.cloud_bitmap,0,0,al_get_bitmap_width(game.menu.cloud_bitmap), al_get_bitmap_height(game.menu.cloud_bitmap), al_get_display_width(game.display)*(game.menu.cloud_position-20)/(-75), al_get_display_height(game.display)*0.15, al_get_bitmap_width(game.menu.cloud_bitmap)/2, al_get_bitmap_height(game.menu.cloud_bitmap)/2,0);
al_draw_bitmap(game.menu.menu_bitmap,0, 0,0);
al_draw_bitmap(game.menu.pinkcloud_bitmap,(al_get_display_width(game.display)*0.132) + (cos((game.menu.cloud_position/50+80)*1.74444))*20, 0,0);
al_draw_bitmap(game.menu.cloud_bitmap,al_get_display_width(game.display)*game.menu.cloud_position/100, 30,0);
al_draw_text(game.font, al_map_rgb(255,255,255), al_get_display_width(game.display)*0.5, al_get_display_height(game.display)*0.2, ALLEGRO_ALIGN_CENTRE, "Super Derpy");
game.menu.cloud_position-=0.2;
if (game.menu.cloud_position<-80) game.menu.cloud_position=100;
al_flip_display();
}
}
}
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_bitmap(loading_bitmap);
al_destroy_event_queue(event_queue);
al_destroy_sample(sample);
UnloadGameState(&game);
al_destroy_timer(game.timer);
al_destroy_display(game.display);
al_destroy_bitmap(game.loading.loading_bitmap);
al_destroy_event_queue(game.event_queue);
return 0;
}