libsuperderpy/src/main.c

383 lines
12 KiB
C
Raw Normal View History

2012-02-28 13:09:12 +01:00
/*! \file main.c
* \brief Main file of SuperDerpy engine.
*
* Contains basic functions shared by all views.
*/
2012-02-15 23:57:06 +01:00
#include <stdio.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-22 12:43:14 +01:00
#include "level.h"
2012-03-02 16:52:13 +01:00
#include "pause.h"
2012-02-24 13:03:30 +01:00
#include "config.h"
2012-02-15 23:57:06 +01:00
2012-02-28 13:09:12 +01:00
/*! \brief Macro for preloading gamestate.
*
* Preloading of state happens when loading screen is displayed.
*/
#define PRELOAD_STATE(state, name) case state:\
PrintConsole(game, "Preload %s...", #state); DrawConsole(game); al_flip_display(); name ## _Preload(game); break;
2012-02-28 13:09:12 +01:00
/*! \brief Macro for unloading gamestate.
*
* Unloading of state happens after it's fadeout.
*/
#define UNLOAD_STATE(state, name) case state:\
PrintConsole(game, "Unload %s...", #state); name ## _Unload(game); break;
2012-02-28 13:09:12 +01:00
/*! \brief Macro for loading gamestate.
*
* Loading of state means setting it as active and running it.
*/
#define LOAD_STATE(state, name) case state:\
PrintConsole(game, "Load %s...", #state); name ## _Load(game); break;
2012-02-28 13:09:12 +01:00
/*! \brief Macro for sending keydown events to gamestate. */
2012-02-25 22:19:12 +01:00
#define KEYDOWN_STATE(state, name) else if (game.gamestate==state) { if (name ## _Keydown(&game, &ev)) break; }
2012-02-28 13:09:12 +01:00
/*! \brief Macro for drawing active gamestate. */
2012-02-25 22:19:12 +01:00
#define DRAW_STATE(state, name) case state:\
2012-03-02 16:52:13 +01:00
name ## _Draw(game); break;
void al_draw_text_with_shadow(ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x, float y, int flags, char const *text) {
al_draw_text(font, al_map_rgba(0,0,0,128), x+1, y+1, flags, text);
al_draw_text(font, color, x, y, flags, text);
}
void PrintConsole(struct Game *game, char* format, ...) {
va_list vl;
va_start(vl, format);
char text[255] = {};
vsprintf(text, format, vl);
va_end(vl);
if (game->debug) 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) && (game->menu.loaded)) {
PrintConsole(game, "GAMESTATE_MENU already loaded, skipping...");
return;
}
switch (game->loadstate) {
PRELOAD_STATE(GAMESTATE_MENU, Menu)
PRELOAD_STATE(GAMESTATE_LOADING, Loading)
PRELOAD_STATE(GAMESTATE_ABOUT, About)
PRELOAD_STATE(GAMESTATE_INTRO, Intro)
PRELOAD_STATE(GAMESTATE_MAP, Map)
PRELOAD_STATE(GAMESTATE_LEVEL, Level)
default:
PrintConsole(game, "ERROR: Attempted to preload unknown gamestate %d!", game->loadstate);
break;
2012-02-17 20:32:58 +01:00
}
PrintConsole(game, "finished");
2012-02-16 12:48:48 +01:00
}
void UnloadGameState(struct Game *game) {
switch (game->gamestate) {
case GAMESTATE_MENU:
if (game->shuttingdown) {
PrintConsole(game, "Unload GAMESTATE_MENU..."); Menu_Unload(game);
} else {
PrintConsole(game, "Just stopping GAMESTATE_MENU..."); Menu_Stop(game);
}
break;
2012-03-02 16:52:13 +01:00
UNLOAD_STATE(GAMESTATE_PAUSE, Pause)
UNLOAD_STATE(GAMESTATE_LOADING, Loading)
UNLOAD_STATE(GAMESTATE_ABOUT, About)
UNLOAD_STATE(GAMESTATE_INTRO, Intro)
UNLOAD_STATE(GAMESTATE_MAP, Map)
UNLOAD_STATE(GAMESTATE_LEVEL, Level)
default:
PrintConsole(game, "ERROR: Attempted to unload unknown gamestate %d!", game->gamestate);
break;
2012-02-17 20:32:58 +01:00
}
PrintConsole(game, "finished");
2012-02-16 12:48:48 +01:00
}
void LoadGameState(struct Game *game) {
switch (game->loadstate) {
LOAD_STATE(GAMESTATE_MENU, Menu)
LOAD_STATE(GAMESTATE_LOADING, Loading)
LOAD_STATE(GAMESTATE_ABOUT, About)
LOAD_STATE(GAMESTATE_INTRO, Intro)
LOAD_STATE(GAMESTATE_MAP, Map)
LOAD_STATE(GAMESTATE_LEVEL, Level)
default:
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
}
2012-03-02 16:52:13 +01:00
void DrawGameState(struct Game *game) {
switch (game->gamestate) {
DRAW_STATE(GAMESTATE_MENU, Menu)
DRAW_STATE(GAMESTATE_PAUSE, Pause)
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;
}
}
2012-03-01 01:14:24 +01:00
void ScaleBitmap(ALLEGRO_BITMAP* source, int width, int height, float val) {
if ((al_get_bitmap_width(source)==width) && (al_get_bitmap_height(source)==height)) {
al_draw_bitmap(source, 0, 0, 0);
return;
}
2012-03-01 00:37:16 +01:00
int x, y;
2012-03-01 22:34:19 +01:00
al_lock_bitmap(al_get_target_bitmap(), ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_WRITEONLY);
al_lock_bitmap(source, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY);
for (y = 0; y < height; y++) {
2012-03-01 00:37:16 +01:00
float pixy = ((float)y / height) * al_get_bitmap_height(source);
2012-03-01 22:34:19 +01:00
for (x = 0; x < width; x++) {
2012-03-01 00:37:16 +01:00
float pixx = ((float)x / width) * al_get_bitmap_width(source);
2012-03-01 01:14:24 +01:00
ALLEGRO_COLOR a = al_get_pixel(source, pixx-val, pixy-val);
ALLEGRO_COLOR b = al_get_pixel(source, pixx+val, pixy-val);
ALLEGRO_COLOR c = al_get_pixel(source, pixx-val, pixy+val);
ALLEGRO_COLOR d = al_get_pixel(source, pixx+val, pixy+val);
2012-03-01 00:37:16 +01:00
ALLEGRO_COLOR result = al_map_rgba_f(
(a.r+b.r+c.r+d.r) / 4,
(a.g+b.b+c.g+d.g) / 4,
(a.b+b.g+c.b+d.b) / 4,
(a.a+b.a+c.a+d.a) / 4
);
2012-03-01 22:34:19 +01:00
al_put_pixel(x, y, result);
2012-03-01 00:37:16 +01:00
}
}
2012-03-01 22:34:19 +01:00
al_unlock_bitmap(al_get_target_bitmap());
al_unlock_bitmap(source);
2012-03-01 00:37:16 +01:00
}
ALLEGRO_BITMAP* LoadFromCache(struct Game *game, char* filename, int width, int height) {
ALLEGRO_BITMAP *source, *target = al_create_bitmap(width, height);
al_set_target_bitmap(target);
al_clear_to_color(al_map_rgba(0,0,0,0));
char origfn[255] = "data/";
char cachefn[255] = "data/cache/";
strcat(origfn, filename);
strcat(cachefn, filename);
void GenerateBitmap() {
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
source = al_load_bitmap( origfn );
al_set_new_bitmap_flags(ALLEGRO_MAG_LINEAR | ALLEGRO_MIN_LINEAR);
2012-03-01 01:14:24 +01:00
ScaleBitmap(source, width, height, 0.35);
2012-03-01 00:37:16 +01:00
al_save_bitmap(cachefn, target);
PrintConsole(game, "Cache bitmap %s generated.", filename);
}
source = al_load_bitmap( cachefn );
if (source) {
if ((al_get_bitmap_width(source)!=width) || (al_get_bitmap_height(source)!=height)) {
al_destroy_bitmap(source);
GenerateBitmap();
return target;
}
return source;
} else GenerateBitmap();
return target;
}
2012-03-01 10:49:20 +01:00
float tps(struct Game *game, float t) {
return t/game->fps;
}
2012-02-15 23:57:06 +01:00
int main(int argc, char **argv){
2012-02-25 22:26:31 +01:00
srand(time(NULL));
2012-02-26 00:47:41 +01:00
2012-02-25 22:26:31 +01:00
InitConfig();
2012-02-24 13:03:30 +01:00
2012-02-25 22:26:31 +01:00
bool redraw = true;
2012-02-15 23:57:06 +01:00
2012-02-25 22:26:31 +01:00
struct Game game;
2012-02-16 12:48:48 +01:00
game.fullscreen = atoi(GetConfigOptionDefault("[SuperDerpy]", "fullscreen", "1"));
game.music = atoi(GetConfigOptionDefault("[SuperDerpy]", "music", "1"));
game.fx = atoi(GetConfigOptionDefault("[SuperDerpy]", "fx", "1"));
game.fps = atoi(GetConfigOptionDefault("[SuperDerpy]", "fps", "60"));
if (game.fps<1) game.fps=60;
game.debug = atoi(GetConfigOptionDefault("[SuperDerpy]", "debug", "0"));
game.width = atoi(GetConfigOptionDefault("[SuperDerpy]", "width", "800"));
if (game.width<320) game.width=320;
game.height = atoi(GetConfigOptionDefault("[SuperDerpy]", "height", "500"));
if (game.height<200) game.height=200;
2012-02-25 22:26:31 +01:00
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
2012-02-15 23:57:06 +01:00
game.timer = al_create_timer(ALLEGRO_BPS_TO_SECS(game.fps));
2012-02-25 22:26:31 +01:00
if(!game.timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}
2012-02-15 23:57:06 +01:00
2012-02-25 22:26:31 +01:00
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-15 23:57:06 +01:00
2012-02-25 22:26:31 +01:00
if(!al_init_acodec_addon()){
fprintf(stderr, "failed to initialize audio codecs!\n");
return -1;
}
2012-02-19 19:17:04 +01:00
2012-02-25 22:26:31 +01:00
if(!al_install_audio()){
fprintf(stderr, "failed to initialize audio!\n");
return -1;
}
2012-02-15 23:57:06 +01:00
2012-02-25 22:26:31 +01:00
if(!al_install_keyboard()){
fprintf(stderr, "failed to initialize keyboard!\n");
return -1;
}
2012-02-15 23:57:06 +01:00
2012-02-25 22:26:31 +01:00
if (!al_reserve_samples(10)){
fprintf(stderr, "failed to reserve samples!\n");
return -1;
}
2012-02-15 23:57:06 +01:00
2012-02-25 22:26:31 +01:00
al_init_font_addon();
2012-02-15 23:57:06 +01:00
2012-02-25 22:26:31 +01:00
if(!al_init_ttf_addon()){
fprintf(stderr, "failed to initialize fonts!\n");
return -1;
}
2012-02-16 12:48:48 +01:00
if (game.fullscreen) al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
2012-02-25 22:26:31 +01:00
al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST);
2012-03-01 13:18:56 +01:00
//al_set_new_display_option(ALLEGRO_OPENGL, 1, ALLEGRO_SUGGEST);
game.display = al_create_display(game.width, game.height);
2012-02-25 22:26:31 +01:00
if(!game.display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_set_window_title(game.display, "Super Derpy: Muffin Attack");
if (game.fullscreen) al_hide_mouse_cursor(game.display);
al_set_new_bitmap_flags(ALLEGRO_MAG_LINEAR | ALLEGRO_MIN_LINEAR);
2012-03-01 13:32:44 +01:00
//al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_32_WITH_ALPHA);
2012-02-25 22:26:31 +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-25 22:26:31 +01:00
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;
}
2012-02-15 23:57:06 +01:00
2012-02-25 22:26:31 +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
game.showconsole = game.debug;
2012-02-25 22:26:31 +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-17 13:25:06 +01:00
2012-02-25 22:26:31 +01:00
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
2012-02-15 23:57:06 +01:00
2012-02-25 22:26:31 +01:00
al_start_timer(game.timer);
game.shuttingdown = false;
game.menu.loaded = false;
2012-02-25 22:26:31 +01:00
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);
2012-02-15 23:57:06 +01:00
2012-02-25 22:26:31 +01:00
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;
}
2012-03-02 16:52:13 +01:00
KEYDOWN_STATE(GAMESTATE_PAUSE, Pause)
2012-02-25 22:26:31 +01:00
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;
2012-03-02 16:52:13 +01:00
DrawGameState(&game);
2012-02-25 22:19:12 +01:00
DrawConsole(&game);
al_flip_display();
2012-02-25 22:26:31 +01:00
}
}
game.shuttingdown = true;
2012-02-25 22:26:31 +01:00
UnloadGameState(&game);
if (game.gamestate != GAMESTATE_LOADING) {
game.gamestate = GAMESTATE_LOADING;
UnloadGameState(&game);
}
2012-02-25 22:26:31 +01:00
al_clear_to_color(al_map_rgb(0,0,0));
PrintConsole(&game, "Shutting down...");
2012-02-17 13:25:06 +01:00
DrawConsole(&game);
al_flip_display();
2012-02-25 22:26:31 +01:00
al_rest(0.1);
al_destroy_timer(game.timer);
al_destroy_display(game.display);
al_destroy_event_queue(game.event_queue);
al_destroy_font(game.font);
al_destroy_font(game.font_console);
al_uninstall_audio();
DeinitConfig();
return 0;
2012-02-15 23:57:06 +01:00
}