refactored every gamestate for logic spearation except first level

This commit is contained in:
Sebastian Krzyszkowiak 2012-09-26 03:08:58 +02:00
parent a6a02ba0c2
commit e6fa44514e
16 changed files with 231 additions and 299 deletions

View file

@ -21,17 +21,26 @@
#include <stdio.h>
#include "about.h"
void About_Logic(struct Game *game) {
if (al_get_sample_instance_position(game->about.music)<700000) { return; }
if (game->about.fadeloop>=0) {
if (game->about.fadeloop==0) PrintConsole(game, "Fade in");
game->about.fadeloop+=5;
if (game->about.fadeloop>=256) {
al_destroy_bitmap(game->about.fade_bitmap);
game->about.fadeloop=-1;
}
return;
}
game->about.x+=0.00025;
}
void About_Draw(struct Game *game) {
/*PrintConsole(game, "%d", al_get_sample_instance_position(game->about.music));*/
if (al_get_sample_instance_position(game->about.music)<700000) { al_clear_to_color(al_map_rgba(0,0,0,0)); return; }
if (game->about.fadeloop>=0) {
if (game->about.fadeloop==0) PrintConsole(game, "Fade in");
al_draw_tinted_bitmap(game->about.fade_bitmap,al_map_rgba_f(game->about.fadeloop/255.0,game->about.fadeloop/255.0,game->about.fadeloop/255.0,1),0,0,0);
game->about.fadeloop+=tps(game, 300);
if (game->about.fadeloop>=256) {
al_destroy_bitmap(game->about.fade_bitmap);
game->about.fadeloop=-1;
}
return;
}
@ -43,7 +52,6 @@ void About_Draw(struct Game *game) {
subbitmap = al_create_sub_bitmap(game->about.text_bitmap, 0, x*al_get_bitmap_height(game->about.text_bitmap), al_get_bitmap_width(game->about.text_bitmap), game->viewportHeight);
al_draw_rotated_bitmap(subbitmap, al_get_bitmap_width(subbitmap)/2.0, al_get_bitmap_height(subbitmap)/2.0, game->viewportWidth*0.5+al_get_bitmap_width(subbitmap)/2.0, game->viewportHeight*0.1+al_get_bitmap_height(subbitmap)/2.0, -0.11, 0);
al_destroy_bitmap(subbitmap);
game->about.x+=tps(game, 60*0.00025);
if (game->about.x>1) {
UnloadGameState(game);
game->loadstate = GAMESTATE_MENU;
@ -198,29 +206,12 @@ void About_Preload(struct Game *game, void (*progress)(struct Game*, float)) {
}
void About_Unload(struct Game *game) {
if (game->about.x<0) game->about.x=0;
ALLEGRO_EVENT ev;
game->about.fade_bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight);
al_set_target_bitmap(game->about.fade_bitmap);
al_draw_bitmap(game->about.image,0,0,0);
al_draw_bitmap(game->about.letter, game->viewportWidth*0.3, -game->viewportHeight*0.1, 0);
ALLEGRO_BITMAP* subbitmap;
subbitmap = al_create_sub_bitmap(game->about.text_bitmap, 0, game->about.x*al_get_bitmap_height(game->about.text_bitmap), al_get_bitmap_width(game->about.text_bitmap), game->viewportHeight);
al_draw_rotated_bitmap(subbitmap, al_get_bitmap_width(subbitmap)/2.0, al_get_bitmap_height(subbitmap)/2.0, game->viewportWidth*0.5+al_get_bitmap_width(subbitmap)/2.0, game->viewportHeight*0.1+al_get_bitmap_height(subbitmap)/2.0, -0.11, 0);
al_destroy_bitmap(subbitmap);
al_set_target_bitmap(al_get_backbuffer(game->display));
float fadeloop;
if (game->about.fadeloop!=0)
for(fadeloop=255; fadeloop>=0; fadeloop-=tps(game, 600)){
al_wait_for_event(game->event_queue, &ev);
al_draw_tinted_bitmap(game->about.fade_bitmap, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0);
DrawConsole(game);
al_flip_display();
if (game->about.fadeloop!=0) {
FadeGameState(game, false);
}
al_destroy_bitmap(game->about.image);
al_destroy_bitmap(game->about.letter);
al_destroy_bitmap(game->about.fade_bitmap);
if (game->about.fadeloop>=0) al_destroy_bitmap(game->about.fade_bitmap);
al_destroy_bitmap(game->about.text_bitmap);
al_destroy_sample_instance(game->about.music);
al_destroy_sample(game->about.sample);

View file

@ -21,6 +21,7 @@
#include "main.h"
void About_Draw(struct Game *game);
void About_Logic(struct Game *game);
void About_Preload(struct Game *game, void (*progress)(struct Game*, float));
void About_Unload(struct Game *game);
void About_Load(struct Game *game);

View file

@ -33,16 +33,7 @@ void Disclaimer_Draw(struct Game *game) {
}
void Disclaimer_Load(struct Game *game) {
ALLEGRO_EVENT ev;
float fadeloop;
for (fadeloop=255; fadeloop>0; fadeloop-=tps(game, 600)) {
al_wait_for_event(game->event_queue, &ev);
Disclaimer_Draw(game);
al_draw_filled_rectangle(0,0,game->viewportWidth,game->viewportHeight,al_map_rgba(0,0,0,fadeloop));
DrawConsole(game);
al_flip_display();
}
Disclaimer_Draw(game);
FadeGameState(game, true);
}
int Disclaimer_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
@ -62,18 +53,9 @@ void Disclaimer_Preload(struct Game *game, void (*progress)(struct Game*, float)
}
void Disclaimer_Unload(struct Game *game) {
ALLEGRO_EVENT ev;
float fadeloop;
for (fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)) {
al_wait_for_event(game->event_queue, &ev);
Disclaimer_Draw(game);
al_draw_filled_rectangle(0,0,game->viewportWidth,game->viewportHeight,al_map_rgba(0,0,0,fadeloop));
DrawConsole(game);
al_flip_display();
}
FadeGameState(game, false);
if (!game->menu.loaded) {
al_destroy_font(game->menu.font);
al_destroy_font(game->menu.font_selected);
}
al_clear_to_color(al_map_rgb(0,0,0));
}

View file

@ -43,8 +43,6 @@ void AnimPage(struct Game *game, int page, ALLEGRO_COLOR tint) {
if (page==4) { amount1=5; amount2=3; anim*=2; }
if (page<5) al_draw_tinted_bitmap_region(game->intro.animsprites[page],tint,game->viewportWidth*0.3125*(int)fmod(anim,amount1),game->viewportHeight*0.63*(((int)(anim/amount1))%amount2),game->viewportWidth*0.3125, game->viewportHeight*0.63,offset+game->viewportWidth*1.08, game->viewportHeight*0.18,0);
game->intro.anim += tps(game, 2);
}
void FillPage(struct Game *game, int page) {
@ -142,6 +140,20 @@ void FillPage(struct Game *game, int page) {
al_destroy_bitmap(second);
}
void Intro_Logic(struct Game *game) {
game->intro.anim += 1.0/20.0;
if (game->intro.in_animation) {
int old = game->intro.position%game->viewportWidth;
game->intro.position -= 10;
if (game->intro.position%game->viewportWidth>old) {
game->intro.in_animation = false;
FillPage(game, game->intro.page+1);
PrintConsole(game, "Animation finished.");
al_set_audio_stream_playing(game->intro.audiostream, true);
}
}
}
void Intro_Draw(struct Game *game) {
al_clear_to_color(al_map_rgb(0,0,0));
if (game->intro.in_animation) {
@ -155,44 +167,21 @@ void Intro_Draw(struct Game *game) {
al_draw_text_with_shadow(game->intro.font, al_map_rgb(255,255,255), game->viewportWidth/2, game->viewportHeight*0.90, ALLEGRO_ALIGN_CENTRE, "Press any key to continue or escape to skip...");
al_draw_bitmap(game->intro.frame, 0, 0, 0);
/*PrintConsole(game, "drawing");*/
if (game->intro.in_animation) {
/*PrintConsole(game, "animating");*/
int old = game->intro.position%game->viewportWidth;
if (tps(game, 600)) game->intro.position -= tps(game, 600);
else game->intro.position -= 1;
/*PrintConsole(game, "%d", game->intro.position%game->viewportWidth);*/
if (game->intro.position%game->viewportWidth>old) {
/*DrawConsole(game);
al_flip_display();*/
game->intro.in_animation = false;
FillPage(game, game->intro.page+1);
PrintConsole(game, "Animation finished.");
al_set_audio_stream_playing(game->intro.audiostream, true);
}
else if (game->intro.position<=-4*game->viewportWidth) {
if (game->intro.in_animation && game->intro.position<=-4*game->viewportWidth) {
PrintConsole(game, "This was the last page.");
game->intro.in_animation = false;
game->intro.page--;
UnloadGameState(game);
game->loadstate = GAMESTATE_MAP;
PrintConsole(game, "Chainloading GAMESTATE_MAP...");
LoadGameState(game);
}
}
}
void Intro_Load(struct Game *game) {
al_play_sample_instance(game->intro.music);
ALLEGRO_EVENT ev;
float fadeloop;
for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){
al_wait_for_event(game->event_queue, &ev);
al_draw_tinted_bitmap(game->intro.table,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0);
AnimPage(game, game->intro.page+1, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1));
al_draw_tinted_bitmap(game->intro.frame,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0);
DrawConsole(game);
al_flip_display();
}
FadeGameState(game, true);
al_set_audio_stream_playing(game->intro.audiostream, true);
Intro_Draw(game);
}
int Intro_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
@ -269,26 +258,11 @@ void Intro_Preload(struct Game *game, void (*progress)(struct Game*, float)) {
}
void Intro_Unload(struct Game *game) {
FadeGameState(game, false);
if (game->intro.audiostream) {
al_set_audio_stream_playing(game->intro.audiostream, false);
al_destroy_audio_stream(game->intro.audiostream);
}
ALLEGRO_EVENT ev;
float fadeloop;
for(fadeloop=255; fadeloop>=0; fadeloop-=tps(game, 600)){
al_wait_for_event(game->event_queue, &ev);
if (game->intro.in_animation) {
al_draw_tinted_bitmap(game->intro.table, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), -1*game->viewportWidth + (cos(((-1*((game->intro.position)%game->viewportWidth))/(float)game->viewportWidth)*(ALLEGRO_PI))/2.0)*game->viewportWidth + game->viewportWidth/2.0, 0, 0);
AnimPage(game, game->intro.page, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1));
}
else {
al_draw_tinted_bitmap(game->intro.table, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0);
AnimPage(game, game->intro.page+1, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1));
}
al_draw_tinted_bitmap(game->intro.frame,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0);
DrawConsole(game);
al_flip_display();
}
al_destroy_bitmap(game->intro.frame);
al_destroy_bitmap(game->intro.table);
int i;

View file

@ -21,6 +21,7 @@
#include "main.h"
void Intro_Draw(struct Game *game);
void Intro_Logic(struct Game *game);
void Intro_Preload(struct Game *game, void (*progress)(struct Game*, float));
void Intro_Unload(struct Game *game);
void Intro_Load(struct Game *game);

View file

@ -99,6 +99,10 @@ void Level_Passed(struct Game *game) {
}
}
void Level_Logic(struct Game *game) {
if (game->level.current_level!=1) Moonwalk_Logic(game);
}
void Level_Draw(struct Game *game) {
if (!al_get_sample_instance_playing(game->level.music) && (game->loadstate==GAMESTATE_LEVEL)) {
al_set_sample_instance_playing(game->level.music, true);
@ -380,7 +384,7 @@ int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
return 0;
}
void Level_ProcessLogic(struct Game *game, ALLEGRO_EVENT *ev) {
void Level_ProcessEvent(struct Game *game, ALLEGRO_EVENT *ev) {
if (game->level.handle_input) {
if ((ev->type==ALLEGRO_EVENT_KEY_UP) && (ev->keyboard.keycode==ALLEGRO_KEY_LEFT)) {
game->level.speed_modifier = 1;
@ -427,12 +431,12 @@ void Level_Preload(struct Game *game, void (*progress)(struct Game*, float)) {
void Level_Unload(struct Game *game) {
Pause_Unload_Real(game);
al_destroy_sample_instance(game->level.music);
al_destroy_sample(game->level.sample);
if (game->level.current_level!=1) Moonwalk_Unload(game);
else {
TM_Destroy();
}
al_destroy_sample_instance(game->level.music);
al_destroy_sample(game->level.sample);
struct Obstacle *t = game->level.obstacles;
if (t) {
while (t->next) {

View file

@ -23,10 +23,11 @@
void SelectDerpySpritesheet(struct Game *game, char* name);
void Level_Passed(struct Game *game);
void Level_Draw(struct Game *game);
void Level_Logic(struct Game *game);
void Level_Preload(struct Game *game, void (*progress)(struct Game*, float));
void Level_Unload(struct Game *game);
void Level_Load(struct Game *game);
void Level_ProcessLogic(struct Game *game, ALLEGRO_EVENT *ev);
void Level_ProcessEvent(struct Game *game, ALLEGRO_EVENT *ev);
int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev);
void Level_UnloadBitmaps(struct Game *game);
void Level_PreloadBitmaps(struct Game *game, void (*progress)(struct Game*, float));

View file

@ -31,14 +31,19 @@ void Progress(struct Game *game, float p) {
}
void Loading_Draw(struct Game *game) {
float fadeloop=0;
while (fadeloop<256) {
ALLEGRO_EVENT ev;
float fadeloop;
for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){
al_wait_for_event(game->event_queue, &ev);
if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->timer)) {
fadeloop+=10;
}
if (al_is_event_queue_empty(game->event_queue)) {
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);
DrawConsole(game);
al_flip_display();
}
}
al_draw_bitmap(game->loading.loading_bitmap,0,0,0);
@ -49,13 +54,22 @@ void Loading_Draw(struct Game *game) {
al_wait_for_vsync();
al_start_timer(game->timer);
for(fadeloop=255; fadeloop>0; fadeloop-=tps(game, 600)){
fadeloop=0;
while (fadeloop<256) {
ALLEGRO_EVENT ev;
al_wait_for_event(game->event_queue, &ev);
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_draw_filled_rectangle(0, game->viewportHeight*0.985, game->viewportWidth, game->viewportHeight, al_map_rgba(fadeloop,fadeloop,fadeloop,255));
if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->timer)) {
fadeloop+=10;
}
if (al_is_event_queue_empty(game->event_queue)) {
al_draw_bitmap(game->loading.loading_bitmap,0,0,0);
al_draw_filled_rectangle(0, game->viewportHeight*0.985, game->viewportWidth, game->viewportHeight, al_map_rgba(255,255,255,255));
al_draw_filled_rectangle(0, 0, game->viewportWidth, game->viewportHeight, al_map_rgba_f(0,0,0,fadeloop/255.0));
DrawConsole(game);
al_flip_display();
}
}
al_clear_to_color(al_map_rgb(0,0,0));
DrawConsole(game);
al_flip_display();

View file

@ -59,6 +59,9 @@
/*! \brief Macro for drawing active gamestate. */
#define DRAW_STATE(state, name) case state:\
name ## _Draw(game); break;
/*! \brief Macro for invocing logic function of active gamestate. */
#define LOGIC_STATE(state, name) case state:\
name ## _Logic(game); break;
double old_time = 0, fps;
int frames_done = 0;
@ -186,7 +189,9 @@ void UnloadGameState(struct Game *game) {
}
void LoadGameState(struct Game *game) {
switch (game->loadstate) {
game->gamestate = game->loadstate;
game->loadstate = -1;
switch (game->gamestate) {
LOAD_STATE(GAMESTATE_MENU, Menu)
LOAD_STATE(GAMESTATE_LOADING, Loading)
LOAD_STATE(GAMESTATE_ABOUT, About)
@ -198,8 +203,6 @@ void LoadGameState(struct Game *game) {
PrintConsole(game, "ERROR: Attempted to load unknown gamestate %d!", game->loadstate);
}
PrintConsole(game, "finished");
game->gamestate = game->loadstate;
game->loadstate = -1;
}
void DrawGameState(struct Game *game) {
@ -226,6 +229,52 @@ void DrawGameState(struct Game *game) {
}
}
void LogicGameState(struct Game *game) {
switch (game->gamestate) {
LOGIC_STATE(GAMESTATE_ABOUT, About)
LOGIC_STATE(GAMESTATE_MENU, Menu)
LOGIC_STATE(GAMESTATE_MAP, Map)
LOGIC_STATE(GAMESTATE_INTRO, Intro)
LOGIC_STATE(GAMESTATE_LEVEL, Level)
default:
// not every gamestate needs to have logic function
break;
}
}
void FadeGameState(struct Game *game, bool in) {
ALLEGRO_BITMAP* bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight);
al_set_target_bitmap(bitmap);
al_clear_to_color(al_map_rgb(0,0,0));
al_set_target_bitmap(al_get_backbuffer(game->display));
float fadeloop;
if (in) {
fadeloop = 255;
} else {
fadeloop = 0;
}
while ((in && fadeloop>=0) || (!in && fadeloop<255)) {
ALLEGRO_EVENT ev;
al_wait_for_event(game->event_queue, &ev);
if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game->timer)) {
LogicGameState(game);
if (in) {
fadeloop-=10;
} else {
fadeloop+=10;
}
}
if (al_is_event_queue_empty(game->event_queue)) {
DrawGameState(game);
al_draw_tinted_bitmap(bitmap,al_map_rgba_f(1,1,1,fadeloop/255.0),0,0,0);
DrawConsole(game);
al_flip_display();
}
}
al_destroy_bitmap(bitmap);
DrawGameState(game);
}
/*! \brief Scales bitmap using software linear filtering method to current target. */
void ScaleBitmap(ALLEGRO_BITMAP* source, int width, int height) {
if ((al_get_bitmap_width(source)==width) && (al_get_bitmap_height(source)==height)) {
@ -299,8 +348,7 @@ ALLEGRO_BITMAP* LoadScaledBitmap(char* filename, int width, int height) {
}
float tps(struct Game *game, float t) {
if (game->fps>0) return t/game->fps;
else return t/fps;
return t/60;
}
void SetupViewport(struct Game *game) {
@ -373,15 +421,12 @@ int main(int argc, char **argv){
InitConfig();
bool redraw = true;
struct Game game;
game.fullscreen = atoi(GetConfigOptionDefault("SuperDerpy", "fullscreen", "1"));
game.music = atoi(GetConfigOptionDefault("SuperDerpy", "music", "7"));
game.voice = atoi(GetConfigOptionDefault("SuperDerpy", "voice", "10"));
game.fx = atoi(GetConfigOptionDefault("SuperDerpy", "fx", "10"));
game.fps = atoi(GetConfigOptionDefault("SuperDerpy", "fps", "0"));
game.debug = atoi(GetConfigOptionDefault("SuperDerpy", "debug", "0"));
game.width = atoi(GetConfigOptionDefault("SuperDerpy", "width", "800"));
if (game.width<320) game.width=320;
@ -481,28 +526,14 @@ int main(int argc, char **argv){
game.showconsole = game.debug;
ALLEGRO_DISPLAY_MODE mode;
al_get_display_mode(0, &mode);
if (mode.refresh_rate < game.fps) {
if (atoi(GetConfigOptionDefault("SuperDerpy", "lower_fps_to_refresh_rate", "0"))) {
PrintConsole(&game, "Refresh rate %d lower than FPS %d, lowering", mode.refresh_rate, game.fps);
game.fps = mode.refresh_rate;
} else {
PrintConsole(&game, "Refresh rate %d lower than FPS %d, NOT lowering due to config", mode.refresh_rate, game.fps);
}
} else if (game.fps == 0) game.fps = mode.refresh_rate;
if (game.fps>600) game.fps = 600;
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
if (game.fps>0) game.timer = al_create_timer(ALLEGRO_BPS_TO_SECS(game.fps));
else game.timer = al_create_timer(ALLEGRO_BPS_TO_SECS(600));
game.timer = al_create_timer(ALLEGRO_BPS_TO_SECS(60)); // logic timer
if(!game.timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}
al_register_event_source(game.event_queue, al_get_timer_event_source(game.timer));
al_wait_for_vsync();
al_start_timer(game.timer);
setlocale(LC_NUMERIC, "C"); /* FIXME? */
@ -529,17 +560,9 @@ int main(int argc, char **argv){
while(1) {
ALLEGRO_EVENT ev;
bool event = false;
if (game.fps<0) {
redraw = true;
event=al_get_next_event(game.event_queue, &ev);
} else {
al_wait_for_event(game.event_queue, &ev);
event=true;
}
if (!event) {}
else if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game.timer)) {
redraw = true;
if ((ev.type == ALLEGRO_EVENT_TIMER) && (ev.timer.source == game.timer)) {
LogicGameState(&game);
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
@ -587,14 +610,11 @@ int main(int argc, char **argv){
game.gamestate = GAMESTATE_LOADING;
game.loadstate = GAMESTATE_MENU;
}
} else {
if (game.gamestate == GAMESTATE_LEVEL) {
Level_ProcessLogic(&game, &ev);
}
} else if (game.gamestate == GAMESTATE_LEVEL) {
Level_ProcessEvent(&game, &ev);
}
if(redraw && al_is_event_queue_empty(game.event_queue)) {
redraw = false;
if (al_is_event_queue_empty(game.event_queue)) {
DrawGameState(&game);
DrawConsole(&game);
al_flip_display();

View file

@ -95,7 +95,6 @@ struct Spritesheet {
/*! \brief Resources used by moonwalk level placeholder. */
struct Moonwalk {
ALLEGRO_BITMAP *fade_bitmap; /*!< Bitmap used on fade-in and fade-out animations. */
ALLEGRO_BITMAP *image; /*!< Background texture. */
int derpy_frame; /*!< Current frame of Derpy animation. */
int derpy_frame_tmp; /*!< Counter used to slow down Derpy animation. */
@ -173,7 +172,6 @@ enum menustate_enum {
/*! \brief Resources used by Menu state. */
struct Menu {
ALLEGRO_BITMAP *menu_fade_bitmap; /*!< Screenshot bitmap used for fades. */
ALLEGRO_BITMAP *image; /*!< Bitmap with lower portion of menu landscape. */
ALLEGRO_BITMAP *cloud; /*!< Bitmap with bigger cloud. */
ALLEGRO_BITMAP *cloud2; /*!< Bitmap with small cloud. */
@ -340,6 +338,12 @@ float tps(struct Game *game, float t);
/*! \brief Draws frame from current gamestate. */
void DrawGameState(struct Game *game);
/*! \brief Processes logic of current gamestate. */
void LogicGameState(struct Game *game);
/*! \brief Displays fade in or fade out animation on current gamestate. */
void FadeGameState(struct Game *game, bool in);
/*! \brief Load shared resources. */
int Shared_Load(struct Game *game);

View file

@ -53,20 +53,15 @@ void Map_Draw(struct Game *game) {
break;
}
al_draw_scaled_bitmap(game->map.arrow, 0, 0, al_get_bitmap_width(game->map.arrow), al_get_bitmap_height(game->map.arrow), game->viewportWidth*x, game->viewportHeight*y + ((sin(game->map.arrowpos)+0.5)/20.0)*game->viewportHeight, game->viewportWidth*0.1, game->viewportHeight*0.16, 0);
game->map.arrowpos += tps(game, 0.1*60);
}
void Map_Logic(struct Game *game) {
game->map.arrowpos += 0.1;
}
void Map_Load(struct Game *game) {
al_play_sample_instance(game->map.music);
ALLEGRO_EVENT ev;
float fadeloop;
for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){
al_wait_for_event(game->event_queue, &ev);
al_draw_tinted_bitmap(game->map.map,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0);
DrawConsole(game);
al_flip_display();
}
Map_Draw(game);
FadeGameState(game, true);
}
int Map_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
@ -148,14 +143,7 @@ void Map_Preload(struct Game *game, void (*progress)(struct Game*, float)) {
}
void Map_Unload(struct Game *game) {
ALLEGRO_EVENT ev;
float fadeloop;
for(fadeloop=255; fadeloop>=0; fadeloop-=tps(game, 600)){
al_wait_for_event(game->event_queue, &ev);
al_draw_tinted_bitmap(game->map.map, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0);
DrawConsole(game);
al_flip_display();
}
FadeGameState(game, false);
al_destroy_bitmap(game->map.map);
al_destroy_bitmap(game->map.map_bg);
al_destroy_bitmap(game->map.highlight);

View file

@ -21,6 +21,7 @@
#include "main.h"
void Map_Draw(struct Game *game);
void Map_Logic(struct Game *game);
void Map_Preload(struct Game *game, void (*progress)(struct Game*, float));
void Map_Unload(struct Game *game);
void Map_Load(struct Game *game);

View file

@ -170,9 +170,11 @@ void Menu_Draw(struct Game *game) {
al_draw_text_with_shadow(game->menu.font_subtitle, al_map_rgb(255,255,255), game->viewportWidth*0.5, game->viewportHeight*0.275, ALLEGRO_ALIGN_CENTRE, "Muffin Attack");
DrawMenuState(game);
}
game->menu.cloud_position-=tps(game, 0.1*60);
game->menu.cloud2_position-=tps(game, 0.025*60);
void Menu_Logic(struct Game *game) {
game->menu.cloud_position-=0.1;
game->menu.cloud2_position-=0.025;
if (game->menu.cloud_position<-80) { game->menu.cloud_position=100; PrintConsole(game, "cloud_position"); }
if (game->menu.cloud2_position<0) { game->menu.cloud2_position=100; PrintConsole(game, "cloud2_position"); }
}
@ -285,22 +287,9 @@ void Menu_Preload(struct Game *game, void (*progress)(struct Game*, float)) {
}
void Menu_Stop(struct Game* game) {
game->menu.menu_fade_bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight);
al_set_target_bitmap(game->menu.menu_fade_bitmap);
al_clear_to_color(al_map_rgb(0,0,0));
al_set_target_bitmap(al_get_backbuffer(game->display));
ALLEGRO_EVENT ev;
float fadeloop;
for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){
al_wait_for_event(game->event_queue, &ev);
Menu_Draw(game);
al_draw_tinted_bitmap(game->menu.menu_fade_bitmap,al_map_rgba_f(1,1,1,fadeloop/255.0),0,0,0);
DrawConsole(game);
al_flip_display();
}
FadeGameState(game, false);
al_stop_sample_instance(game->menu.music);
al_stop_sample_instance(game->menu.rain_sound);
al_destroy_bitmap(game->menu.menu_fade_bitmap);
}
void ChangeMenuState(struct Game *game, enum menustate_enum state) {
@ -347,21 +336,7 @@ void Menu_Load(struct Game *game) {
al_play_sample_instance(game->menu.music);
al_play_sample_instance(game->menu.rain_sound);
game->menu.menu_fade_bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight);
al_set_target_bitmap(game->menu.menu_fade_bitmap);
al_clear_to_color(al_map_rgb(0,0,0));
al_set_target_bitmap(al_get_backbuffer(game->display));
ALLEGRO_EVENT ev;
float fadeloop;
for(fadeloop=255; fadeloop>=0; fadeloop-=tps(game, 600)){
al_wait_for_event(game->event_queue, &ev);
Menu_Draw(game);
al_draw_tinted_bitmap(game->menu.menu_fade_bitmap,al_map_rgba_f(1,1,1,fadeloop/255.0),0,0,0);
DrawConsole(game);
al_flip_display();
}
al_destroy_bitmap(game->menu.menu_fade_bitmap);
Menu_Draw(game);
FadeGameState(game, true);
}
int Menu_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {

View file

@ -22,6 +22,7 @@
void DrawMenuState(struct Game *game);
void Menu_Draw(struct Game *game);
void Menu_Logic(struct Game *game);
void Menu_Preload(struct Game *game, void (*progress)(struct Game*, float));
void Menu_Stop(struct Game *game);
void Menu_Unload(struct Game *game);

View file

@ -23,14 +23,26 @@
#include "level.h"
#include "moonwalk.h"
void Moonwalk_Logic(struct Game *game) {
game->level.moonwalk.derpy_pos=game->level.moonwalk.derpy_pos+0.00092;
game->level.moonwalk.derpy_frame_tmp++;
if (game->level.moonwalk.derpy_frame_tmp%3==0) {
if (game->level.moonwalk.derpy_frame_tmp%5==0) game->level.moonwalk.derpy_frame++;
if (game->level.moonwalk.derpy_frame_tmp%22==21) game->level.moonwalk.derpy_frame--;
game->level.moonwalk.derpy_frame++;
if (game->level.moonwalk.derpy_frame>=24) game->level.moonwalk.derpy_frame=0;
}
}
void Moonwalk_Draw(struct Game *game) {
al_set_target_bitmap(game->level.derpy);
al_clear_to_color(al_map_rgba(0,0,0,0));
al_draw_bitmap_region(*(game->level.derpy_sheet),al_get_bitmap_width(game->level.derpy)*(game->level.moonwalk.derpy_frame%6),al_get_bitmap_height(game->level.derpy)*(game->level.moonwalk.derpy_frame/6),al_get_bitmap_width(game->level.derpy), al_get_bitmap_height(game->level.derpy),0,0,0);
al_set_target_bitmap(al_get_backbuffer(game->display));
game->level.moonwalk.derpy_pos=game->level.moonwalk.derpy_pos+tps(game, 60*0.00092);
if (game->level.moonwalk.derpy_pos>1) { UnloadGameState(game);
if (game->level.moonwalk.derpy_pos>1) {
game->level.moonwalk.derpy_pos=-1;
UnloadGameState(game);
Level_Passed(game);
if (game->level.current_level<6) {
game->gamestate = GAMESTATE_LOADING;
@ -39,16 +51,7 @@ void Moonwalk_Draw(struct Game *game) {
game->gamestate = GAMESTATE_LOADING;
game->loadstate = GAMESTATE_ABOUT;
}
return; }
int i;
for (i = 0; i < tps(game, 60); i++ ) {
game->level.moonwalk.derpy_frame_tmp++;
if (game->level.moonwalk.derpy_frame_tmp%3==0) {
if (game->level.moonwalk.derpy_frame_tmp%5==0) game->level.moonwalk.derpy_frame++;
if (game->level.moonwalk.derpy_frame_tmp%22==21) game->level.moonwalk.derpy_frame--;
game->level.moonwalk.derpy_frame++;
if (game->level.moonwalk.derpy_frame>=24) game->level.moonwalk.derpy_frame=0;
}
return;
}
al_draw_scaled_bitmap(game->level.moonwalk.image,0,0,al_get_bitmap_width(game->level.moonwalk.image),al_get_bitmap_height(game->level.moonwalk.image),0,0,game->viewportWidth, game->viewportHeight,0);
al_draw_bitmap(game->level.derpy, game->level.moonwalk.derpy_pos*game->viewportWidth, game->viewportHeight*0.95-al_get_bitmap_height(game->level.derpy), ALLEGRO_FLIP_HORIZONTAL);
@ -62,16 +65,7 @@ void Moonwalk_Load(struct Game *game) {
game->level.moonwalk.derpy_frame_tmp = 0;
game->level.moonwalk.derpy_pos = -0.2;
al_play_sample_instance(game->level.music);
ALLEGRO_EVENT ev;
float fadeloop;
for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){
al_wait_for_event(game->event_queue, &ev);
al_draw_tinted_bitmap(game->level.moonwalk.fade_bitmap,al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1),0,0,0);
DrawConsole(game);
al_flip_display();
}
al_destroy_bitmap(game->level.moonwalk.fade_bitmap);
Moonwalk_Draw(game);
FadeGameState(game, true);
}
int Moonwalk_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
@ -84,11 +78,6 @@ void Moonwalk_PreloadBitmaps(struct Game *game) {
game->level.derpy = al_create_bitmap(game->viewportWidth*0.1953125, game->viewportHeight*0.25);
game->level.moonwalk.fade_bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight);
al_set_target_bitmap(game->level.moonwalk.fade_bitmap);
al_draw_bitmap(game->level.moonwalk.image,0,0,0);
al_draw_textf(game->font, al_map_rgb(255,255,255), game->viewportWidth/2, game->viewportHeight/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), game->viewportWidth/2, game->viewportHeight/1.8, ALLEGRO_ALIGN_CENTRE, "Have some moonwalk instead.");
al_set_target_bitmap(al_get_backbuffer(game->display));
}
@ -110,20 +99,5 @@ void Moonwalk_UnloadBitmaps(struct Game *game) {
}
void Moonwalk_Unload(struct Game *game) {
ALLEGRO_EVENT ev;
game->level.moonwalk.fade_bitmap = al_create_bitmap(game->viewportWidth, game->viewportHeight);
al_set_target_bitmap(game->level.moonwalk.fade_bitmap);
al_draw_bitmap(game->level.moonwalk.image,0,0,0);
al_draw_bitmap(game->level.derpy, game->level.moonwalk.derpy_pos*game->viewportWidth, game->viewportHeight*0.95-al_get_bitmap_height(game->level.derpy), ALLEGRO_FLIP_HORIZONTAL);
al_draw_textf(game->font, al_map_rgb(255,255,255), game->viewportWidth/2, game->viewportHeight/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), game->viewportWidth/2, game->viewportHeight/1.8, ALLEGRO_ALIGN_CENTRE, "Have some moonwalk instead.");
al_set_target_bitmap(al_get_backbuffer(game->display));
float fadeloop;
for(fadeloop=255; fadeloop>=0; fadeloop-=tps(game, 600)){
al_wait_for_event(game->event_queue, &ev);
al_draw_tinted_bitmap(game->level.moonwalk.fade_bitmap, al_map_rgba_f(fadeloop/255.0,fadeloop/255.0,fadeloop/255.0,1), 0, 0, 0);
DrawConsole(game);
al_flip_display();
}
al_destroy_bitmap(game->level.moonwalk.fade_bitmap);
FadeGameState(game, false);
}

View file

@ -21,6 +21,7 @@
#include "main.h"
void Moonwalk_Draw(struct Game *game);
void Moonwalk_Logic(struct Game *game);
void Moonwalk_Preload(struct Game *game);
void Moonwalk_Unload(struct Game *game);
void Moonwalk_Load(struct Game *game);