add music to level 1

This commit is contained in:
Sebastian Krzyszkowiak 2012-04-09 15:25:56 +02:00
parent 754f88facf
commit 7ac12920bb
3 changed files with 32 additions and 20 deletions

View file

@ -82,6 +82,10 @@ bool Stop(struct Game *game, struct TM_Action *action, enum TM_ActionState state
}
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);
al_set_sample_instance_position(game->level.music, game->level.music_pos);
}
if (game->level.current_level!=1) Moonwalk_Draw(game);
else {
al_clear_to_color(al_map_rgb(0,0,0));
@ -152,6 +156,7 @@ bool FadeIn(struct Game *game, struct TM_Action *action, enum TM_ActionState sta
free(fadeloop);
TM_DestroyArgs(action->arguments);
action->arguments = NULL;
al_play_sample_instance(game->level.music);
}
return false;
}
@ -204,7 +209,7 @@ bool Welcome(struct Game *game, struct TM_Action *action, enum TM_ActionState st
//PrintConsole(game, "WELCOME RUNNING FADE=%f, IN=%d", *in);
int fade = *tmp;
if (fade>255) fade=255;
if (*tmp > 1500) { *tmp=255; *in=false; }
if (*tmp > 2048) { *tmp=255; *in=false; }
al_draw_tinted_bitmap(game->level.welcome, al_map_rgba_f(fade/255.0,fade/255.0,fade/255.0,fade/255.0), 0, 0, 0);
if (*in) {
*tmp+=tps(game, 600);
@ -251,6 +256,7 @@ void Level_Load(struct Game *game) {
TM_AddBackgroundAction(&FadeIn, NULL, 0);
TM_AddDelay(1000);
TM_AddQueuedBackgroundAction(&Welcome, NULL, 0);
TM_AddDelay(1000);
TM_AddAction(&Walk, NULL);
TM_AddAction(&Move, NULL);
TM_AddAction(&Stop, NULL);
@ -280,6 +286,10 @@ void Level_Load(struct Game *game) {
}
int Level_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
if (ev->keyboard.keycode==ALLEGRO_KEY_ESCAPE) {
game->level.music_pos = al_get_sample_instance_position(game->level.music);
al_set_sample_instance_playing(game->level.music, false);
}
if (game->level.current_level!=1) Moonwalk_Keydown(game, ev);
if (ev->keyboard.keycode==ALLEGRO_KEY_ESCAPE) {
game->gamestate = GAMESTATE_PAUSE;
@ -306,11 +316,24 @@ void Level_ProcessLogic(struct Game *game, ALLEGRO_EVENT *ev) {
void Level_Preload(struct Game *game) {
Pause_Preload(game);
if (game->level.current_level!=1) Moonwalk_Preload(game);
else {
game->level.sample = al_load_sample( "data/levels/1/music.flac" );
game->level.music = al_create_sample_instance(game->level.sample);
al_attach_sample_instance_to_mixer(game->level.music, game->audio.music);
al_set_sample_instance_playmode(game->level.music, ALLEGRO_PLAYMODE_LOOP);
if (!game->level.sample){
fprintf(stderr, "Audio clip sample not loaded!\n" );
exit(-1);
}
}
Level_PreloadBitmaps(game);
}
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();

View file

@ -45,9 +45,6 @@ enum gamestate_enum {
struct Moonwalk {
ALLEGRO_BITMAP *fade_bitmap; /*!< Bitmap used on fade-in and fade-out animations. */
ALLEGRO_BITMAP *image; /*!< Background texture. */
ALLEGRO_SAMPLE *sample; /*!< Sample with background music. */
ALLEGRO_SAMPLE_INSTANCE *music; /*!< Sample instance with background music. */
unsigned int music_pos; /*!< Position of sample instance. Used when pausing game. */
int current_level; /*!< Level number. */
int derpy_frame; /*!< Current frame of Derpy animation. */
int derpy_frame_tmp; /*!< Counter used to slow down Derpy animation. */
@ -63,6 +60,9 @@ struct Level {
bool handle_input;
int sheet_rows, sheet_cols, sheet_pos;
float sheet_tmp, sheet_speed;
ALLEGRO_SAMPLE *sample; /*!< Sample with background music. */
ALLEGRO_SAMPLE_INSTANCE *music; /*!< Sample instance with background music. */
unsigned int music_pos; /*!< Position of sample instance. Used when pausing game. */
ALLEGRO_BITMAP *background, *stage, *foreground, *clouds;
ALLEGRO_BITMAP *welcome;
ALLEGRO_BITMAP *derpy_walkcycle; /*!< Derpy walk cycle spritesheet. */

View file

@ -24,10 +24,6 @@
#include "moonwalk.h"
void Moonwalk_Draw(struct Game *game) {
if (!al_get_sample_instance_playing(game->level.moonwalk.music) && (game->loadstate==GAMESTATE_LEVEL)) {
al_set_sample_instance_playing(game->level.moonwalk.music, true);
al_set_sample_instance_position(game->level.moonwalk.music, game->level.moonwalk.music_pos);
}
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_walkcycle,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);
@ -66,7 +62,6 @@ void Moonwalk_Load(struct Game *game) {
game->level.moonwalk.derpy_frame = 0;
game->level.moonwalk.derpy_frame_tmp = 0;
game->level.moonwalk.derpy_pos = -0.2;
al_play_sample_instance(game->level.moonwalk.music);
ALLEGRO_EVENT ev;
int fadeloop;
for(fadeloop=0; fadeloop<256; fadeloop+=tps(game, 600)){
@ -80,10 +75,6 @@ void Moonwalk_Load(struct Game *game) {
}
int Moonwalk_Keydown(struct Game *game, ALLEGRO_EVENT *ev) {
if (ev->keyboard.keycode==ALLEGRO_KEY_ESCAPE) {
game->level.moonwalk.music_pos = al_get_sample_instance_position(game->level.moonwalk.music);
al_set_sample_instance_playing(game->level.moonwalk.music, false);
}
return 0;
}
@ -103,12 +94,12 @@ void Moonwalk_PreloadBitmaps(struct Game *game) {
void Moonwalk_Preload(struct Game *game) {
PrintConsole(game, "Initializing level %d...", game->level.current_level);
game->level.moonwalk.sample = al_load_sample( "data/moonwalk.flac" );
game->level.moonwalk.music = al_create_sample_instance(game->level.moonwalk.sample);
al_attach_sample_instance_to_mixer(game->level.moonwalk.music, game->audio.music);
al_set_sample_instance_playmode(game->level.moonwalk.music, ALLEGRO_PLAYMODE_LOOP);
game->level.sample = al_load_sample( "data/moonwalk.flac" );
game->level.music = al_create_sample_instance(game->level.sample);
al_attach_sample_instance_to_mixer(game->level.music, game->audio.music);
al_set_sample_instance_playmode(game->level.music, ALLEGRO_PLAYMODE_LOOP);
if (!game->level.moonwalk.sample){
if (!game->level.sample){
fprintf(stderr, "Audio clip sample not loaded!\n" );
exit(-1);
}
@ -135,6 +126,4 @@ void Moonwalk_Unload(struct Game *game) {
al_flip_display();
}
al_destroy_bitmap(game->level.moonwalk.fade_bitmap);
al_destroy_sample_instance(game->level.moonwalk.music);
al_destroy_sample(game->level.moonwalk.sample);
}