libsuperderpy/main.c

260 lines
9.6 KiB
C
Raw Normal View History

2012-02-15 23:57:06 +01:00
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <math.h>
float FPS = 40;
int DISPLAY_WIDTH = 800;
int DISPLAY_HEIGHT = 500;
2012-02-16 12:48:48 +01:00
bool FULLSCREEN = true;
2012-02-15 23:57:06 +01:00
enum gamestate_enum {
2012-02-16 12:48:48 +01:00
GAMESTATE_LOADING,
2012-02-15 23:57:06 +01:00
GAMESTATE_MENU
};
2012-02-16 12:48:48 +01:00
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;
};
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
struct Loading {
ALLEGRO_BITMAP *loading_bitmap, *image;
};
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
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" );
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
game->menu.sample = al_load_sample( "menu.wav" );
game->menu.cloud = al_load_bitmap( "cloud.png" );
game->menu.pinkcloud = al_load_bitmap( "pinkcloud.png" );
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
if (!game->menu.sample){
fprintf(stderr, "Audio clip sample not loaded!\n" );
2012-02-15 23:57:06 +01:00
exit(-1);
}
2012-02-16 12:48:48 +01:00
2012-02-15 23:57:06 +01:00
// Scale menu bitmap
2012-02-16 12:48:48 +01:00
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);
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
al_set_target_bitmap(game->menu.menu_fade_bitmap);
2012-02-15 23:57:06 +01:00
al_clear_to_color(al_map_rgb(183,234,193));
2012-02-16 12:48:48 +01:00
al_draw_bitmap(game->menu.menu_bitmap,0, 0, 0);
2012-02-15 23:57:06 +01:00
// Cloud menu bitmap
2012-02-16 12:48:48 +01:00
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);
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
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);
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
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");
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
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(game->menu.menu_fade_bitmap);
}
}
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);
2012-02-15 23:57:06 +01:00
for(int fadeloop=0; fadeloop<256; fadeloop+=10){
2012-02-16 12:48:48 +01:00
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);
2012-02-15 23:57:06 +01:00
al_flip_display();
}
}
2012-02-16 12:48:48 +01:00
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;
2012-02-15 23:57:06 +01:00
}
int main(int argc, char **argv){
ALLEGRO_DISPLAY_MODE disp_data;
bool redraw = true;
2012-02-16 12:48:48 +01:00
struct Game game;
2012-02-15 23:57:06 +01:00
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
2012-02-16 12:48:48 +01:00
al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR);
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
game.timer = al_create_timer(1.0 / FPS);
if(!game.timer) {
2012-02-15 23:57:06 +01:00
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_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_init_acodec_addon()){
fprintf(stderr, "failed to initialize audio codecs!\n");
return -1;
}
if (!al_reserve_samples(1)){
fprintf(stderr, "failed to reserve samples!\n");
return -1;
}
2012-02-16 12:48:48 +01:00
al_init_font_addon();
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
if(!al_init_ttf_addon()){
fprintf(stderr, "failed to initialize fonts!\n");
2012-02-15 23:57:06 +01:00
return -1;
}
2012-02-16 12:48:48 +01:00
if (FULLSCREEN) al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
game.display = al_create_display(DISPLAY_WIDTH, DISPLAY_HEIGHT);
if(!game.display) {
2012-02-15 23:57:06 +01:00
fprintf(stderr, "failed to create display!\n");
return -1;
}
2012-02-16 12:48:48 +01:00
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 );
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
game.event_queue = al_create_event_queue();
if(!game.event_queue) {
2012-02-15 23:57:06 +01:00
fprintf(stderr, "failed to create event_queue!\n");
2012-02-16 12:48:48 +01:00
al_destroy_display(game.display);
2012-02-15 23:57:06 +01:00
return -1;
}
2012-02-16 12:48:48 +01:00
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());
2012-02-15 23:57:06 +01:00
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
2012-02-16 12:48:48 +01:00
al_start_timer(game.timer);
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
game.loadstate = GAMESTATE_LOADING;
PreloadGameState(&game);
LoadGameState(&game);
game.loadstate = GAMESTATE_MENU;
2012-02-15 23:57:06 +01:00
while(1)
{
ALLEGRO_EVENT ev;
2012-02-16 12:48:48 +01:00
al_wait_for_event(game.event_queue, &ev);
2012-02-15 23:57:06 +01:00
if(ev.type == ALLEGRO_EVENT_TIMER) {
redraw = true;
}
else if((ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) || (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)) {
break;
}
2012-02-16 12:48:48 +01:00
if(redraw && al_is_event_queue_empty(game.event_queue)) {
2012-02-15 23:57:06 +01:00
redraw = false;
2012-02-16 12:48:48 +01:00
if (game.gamestate==GAMESTATE_LOADING) {
2012-02-15 23:57:06 +01:00
for(int fadeloop=0; fadeloop<256; fadeloop+=10){
2012-02-16 12:48:48 +01:00
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);
2012-02-15 23:57:06 +01:00
al_flip_display();
}
2012-02-16 12:48:48 +01:00
al_draw_bitmap(game.loading.loading_bitmap,0,0,0);
2012-02-15 23:57:06 +01:00
al_flip_display();
2012-02-16 12:48:48 +01:00
PreloadGameState(&game);
2012-02-15 23:57:06 +01:00
for(int fadeloop=255; fadeloop>0; fadeloop-=10){
2012-02-16 12:48:48 +01:00
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);
2012-02-15 23:57:06 +01:00
al_flip_display();
}
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
2012-02-16 12:48:48 +01:00
//al_rest(0.2);
LoadGameState(&game);
2012-02-15 23:57:06 +01:00
}
2012-02-16 12:48:48 +01:00
else if (game.gamestate==GAMESTATE_MENU) {
2012-02-15 23:57:06 +01:00
al_clear_to_color(al_map_rgb(183,234,193));
2012-02-16 12:48:48 +01:00
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;
2012-02-15 23:57:06 +01:00
al_flip_display();
}
}
}
2012-02-16 12:48:48 +01:00
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);
2012-02-15 23:57:06 +01:00
return 0;
}