libsuperderpy/src/main.c

305 lines
9.1 KiB
C
Raw Normal View History

2012-02-15 23:57:06 +01:00
#include <stdio.h>
#include <stdarg.h>
2012-02-16 15:40:58 +01:00
#include "menu.h"
#include "loading.h"
#include "about.h"
2012-02-17 20:32:58 +01:00
#include "intro.h"
#include "map.h"
2012-02-15 23:57:06 +01:00
2012-02-16 17:51:52 +01:00
float FPS = 60;
2012-02-15 23:57:06 +01:00
int DISPLAY_WIDTH = 800;
int DISPLAY_HEIGHT = 500;
2012-02-16 12:48:48 +01:00
bool FULLSCREEN = true;
2012-02-19 19:17:04 +01:00
bool DEBUG_MODE = true;
2012-02-17 13:25:06 +01:00
void PrintConsole(struct Game *game, char* format, ...) {
va_list vl;
va_start(vl, format);
char text[255] = {};
vsprintf(text, format, vl);
va_end(vl);
2012-02-19 19:17:04 +01:00
if (DEBUG_MODE) printf("%s\n", text);
2012-02-21 00:00:41 +01:00
ALLEGRO_BITMAP *con = al_create_bitmap(al_get_bitmap_width(game->console), al_get_bitmap_height(game->console));
2012-02-17 13:25:06 +01:00
al_set_target_bitmap(con);
al_clear_to_color(al_map_rgba(0,0,0,80));
al_draw_bitmap_region(game->console, 0, al_get_bitmap_height(game->console)*0.2, al_get_bitmap_width(game->console), al_get_bitmap_height(game->console)*0.8, 0, 0, 0);
2012-02-21 00:00:41 +01:00
al_draw_text(game->font_console, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.005, al_get_bitmap_height(game->console)*0.81, ALLEGRO_ALIGN_LEFT, text);
2012-02-17 13:25:06 +01:00
al_set_target_bitmap(game->console);
al_clear_to_color(al_map_rgba(0,0,0,0));
//al_draw_bitmap_region(game->console, 0, al_get_bitmap_height(game->console)*0.2, al_get_bitmap_width(game->console), al_get_bitmap_height(game->console)*0.8, 0, 0, 0);
al_draw_bitmap(con, 0, 0, 0);
al_set_target_bitmap(al_get_backbuffer(game->display));
2012-02-18 04:14:35 +01:00
al_destroy_bitmap(con);
2012-02-17 13:25:06 +01:00
}
void DrawConsole(struct Game *game) {
if (game->showconsole) al_draw_bitmap(game->console, 0, 0, 0);
2012-02-17 13:25:06 +01:00
}
2012-02-15 23:57:06 +01:00
2012-02-16 12:48:48 +01:00
void PreloadGameState(struct Game *game) {
if (game->loadstate==GAMESTATE_MENU) {
2012-02-17 13:25:06 +01:00
PrintConsole(game, "Preload GAMESTATE_MENU...");
2012-02-19 22:20:05 +01:00
DrawConsole(game);
al_flip_display();
2012-02-16 15:40:58 +01:00
Menu_Preload(game);
2012-02-16 12:48:48 +01:00
}
else if (game->loadstate==GAMESTATE_LOADING) {
2012-02-17 13:25:06 +01:00
PrintConsole(game, "Preload GAMESTATE_LOADING...");
2012-02-19 22:20:05 +01:00
DrawConsole(game);
al_flip_display();
Loading_Preload(game);
}
else if (game->loadstate==GAMESTATE_ABOUT) {
2012-02-17 13:25:06 +01:00
PrintConsole(game, "Preload GAMESTATE_ABOUT...");
2012-02-19 22:20:05 +01:00
DrawConsole(game);
al_flip_display();
About_Preload(game);
}
2012-02-17 20:32:58 +01:00
else if (game->loadstate==GAMESTATE_INTRO) {
PrintConsole(game, "Preload GAMESTATE_INTRO...");
2012-02-19 22:20:05 +01:00
DrawConsole(game);
al_flip_display();
2012-02-17 20:32:58 +01:00
Intro_Preload(game);
}
else if (game->loadstate==GAMESTATE_MAP) {
PrintConsole(game, "Preload GAMESTATE_MAP...");
2012-02-19 22:20:05 +01:00
DrawConsole(game);
al_flip_display();
Map_Preload(game);
2012-02-17 20:32:58 +01:00
} else {
2012-02-21 23:33:02 +01:00
PrintConsole(game, "ERROR: Attempted to preload unknown gamestate %d!", game->loadstate);
2012-02-17 20:32:58 +01:00
}
PrintConsole(game, "finished");
2012-02-16 12:48:48 +01:00
}
void UnloadGameState(struct Game *game) {
if (game->gamestate==GAMESTATE_MENU) {
2012-02-17 13:25:06 +01:00
PrintConsole(game, "Unload GAMESTATE_MENU...");
2012-02-16 15:40:58 +01:00
Menu_Unload(game);
2012-02-16 12:48:48 +01:00
}
else if (game->gamestate==GAMESTATE_LOADING) {
2012-02-17 13:25:06 +01:00
PrintConsole(game, "Unload GAMESTATE_LOADING...");
Loading_Unload(game);
}
else if (game->gamestate==GAMESTATE_ABOUT) {
2012-02-17 13:25:06 +01:00
PrintConsole(game, "Unload GAMESTATE_ABOUT...");
About_Unload(game);
}
2012-02-17 20:32:58 +01:00
else if (game->gamestate==GAMESTATE_INTRO) {
PrintConsole(game, "Unload GAMESTATE_INTRO...");
Intro_Unload(game);
}
else if (game->gamestate==GAMESTATE_MAP) {
PrintConsole(game, "Unload GAMESTATE_MAP...");
Map_Unload(game);
2012-02-17 20:32:58 +01:00
} else {
PrintConsole(game, "ERROR: Attempted to unload unknown gamestate %d!", game->gamestate);
2012-02-17 20:32:58 +01:00
}
PrintConsole(game, "finished");
2012-02-16 12:48:48 +01:00
}
void LoadGameState(struct Game *game) {
if (game->loadstate==GAMESTATE_MENU) {
2012-02-17 13:25:06 +01:00
PrintConsole(game, "Load GAMESTATE_MENU...");
2012-02-16 15:40:58 +01:00
Menu_Load(game);
2012-02-15 23:57:06 +01:00
}
2012-02-16 12:48:48 +01:00
else if (game->loadstate==GAMESTATE_LOADING) {
2012-02-17 13:25:06 +01:00
PrintConsole(game, "Load GAMESTATE_LOADING...");
2012-02-16 15:40:58 +01:00
Loading_Load(game);
2012-02-16 12:48:48 +01:00
}
else if (game->loadstate==GAMESTATE_ABOUT) {
2012-02-17 13:25:06 +01:00
PrintConsole(game, "Load GAMESTATE_ABOUT...");
About_Load(game);
}
2012-02-17 20:32:58 +01:00
else if (game->loadstate==GAMESTATE_INTRO) {
PrintConsole(game, "Load GAMESTATE_INTRO...");
Intro_Load(game);
}
else if (game->loadstate==GAMESTATE_MAP) {
PrintConsole(game, "Load GAMESTATE_MAP...");
Map_Load(game);
2012-02-17 20:32:58 +01:00
} else {
2012-02-21 23:33:02 +01:00
PrintConsole(game, "ERROR: Attempted to load unknown gamestate %d!", game->loadstate);
2012-02-17 20:32:58 +01:00
}
PrintConsole(game, "finished");
2012-02-16 12:48:48 +01:00
game->gamestate = game->loadstate;
game->loadstate = -1;
2012-02-15 23:57:06 +01:00
}
int main(int argc, char **argv){
2012-02-16 17:05:16 +01:00
srand(time(NULL));
2012-02-19 20:18:28 +01:00
//ALLEGRO_DISPLAY_MODE disp_data;
2012-02-15 23:57:06 +01:00
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;
}
al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR^ALLEGRO_MAG_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;
}
2012-02-19 19:17:04 +01:00
if(!al_init_acodec_addon()){
fprintf(stderr, "failed to initialize audio codecs!\n");
return -1;
}
2012-02-15 23:57:06 +01:00
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;
}
2012-02-19 20:18:28 +01:00
if (!al_reserve_samples(10)){
2012-02-15 23:57:06 +01:00
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");
if (FULLSCREEN) al_hide_mouse_cursor(game.display);
2012-02-19 18:10:08 +01:00
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 );
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
2012-02-19 19:17:04 +01:00
game.showconsole = DEBUG_MODE;
2012-02-17 13:25:06 +01:00
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));
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) {
2012-02-15 23:57:06 +01:00
break;
}
else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
2012-02-17 13:25:06 +01:00
if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (ev.keyboard.keycode == ALLEGRO_KEY_TILDE)) {
game.showconsole = !game.showconsole;
2012-02-17 13:25:06 +01:00
}
else if (game.gamestate==GAMESTATE_LOADING) {
if (Loading_Keydown(&game, &ev)) break;
}
else if (game.gamestate==GAMESTATE_MENU) {
if (Menu_Keydown(&game, &ev)) break;
}
else if (game.gamestate==GAMESTATE_ABOUT) {
if (About_Keydown(&game, &ev)) break;
}
2012-02-18 04:14:35 +01:00
else if (game.gamestate==GAMESTATE_INTRO) {
if (Intro_Keydown(&game, &ev)) break;
}
else if (game.gamestate==GAMESTATE_MAP) {
if (Map_Keydown(&game, &ev)) break;
}
2012-02-18 04:14:35 +01:00
else {
game.showconsole = true;
PrintConsole(&game, "ERROR: Keystroke in unknown (%d) gamestate! (5 sec sleep)", game.gamestate);
2012-02-18 04:14:35 +01:00
DrawConsole(&game);
al_flip_display();
al_rest(5.0);
PrintConsole(&game, "Returning to menu...");
game.gamestate = GAMESTATE_LOADING;
game.loadstate = GAMESTATE_MENU;
}
}
2012-02-15 23:57:06 +01:00
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-16 15:40:58 +01:00
Loading_Draw(&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-16 15:40:58 +01:00
Menu_Draw(&game);
2012-02-15 23:57:06 +01:00
}
else if (game.gamestate==GAMESTATE_ABOUT) {
About_Draw(&game);
2012-02-17 20:32:58 +01:00
}
else if (game.gamestate==GAMESTATE_INTRO) {
Intro_Draw(&game);
}
else if (game.gamestate==GAMESTATE_MAP) {
Map_Draw(&game);
2012-02-17 20:32:58 +01:00
}
else {
game.showconsole = true;
PrintConsole(&game, "ERROR: Unknown gamestate %d reached! (5 sec sleep)", game.gamestate);
2012-02-17 20:32:58 +01:00
DrawConsole(&game);
al_flip_display();
al_rest(5.0);
PrintConsole(&game, "Returning to menu...");
game.gamestate = GAMESTATE_LOADING;
game.loadstate = GAMESTATE_MENU;
}
2012-02-17 13:25:06 +01:00
DrawConsole(&game);
al_flip_display();
2012-02-15 23:57:06 +01:00
}
}
2012-02-16 12:48:48 +01:00
UnloadGameState(&game);
2012-02-21 00:00:41 +01:00
PrintConsole(&game, "Shutting down...");
2012-02-17 13:25:06 +01:00
al_rest(0.1);
2012-02-16 12:48:48 +01:00
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-16 22:18:20 +01:00
al_destroy_font(game.font);
2012-02-17 13:25:06 +01:00
al_destroy_font(game.font_console);
2012-02-15 23:57:06 +01:00
return 0;
}