void MusicPlayGame( SoundDevice *device, const char *missionPath, const char *music) { // Play a tune // Start by trying to play a mission specific song, // otherwise pick one from the general collection... MusicStop(device); bool played = false; if (music != NULL && strlen(music) != 0) { char buf[CDOGS_PATH_MAX]; // First, try to play music from the same directory // This may be a new-style directory campaign GetDataFilePath(buf, missionPath); strcat(buf, "/"); strcat(buf, music); played = MusicPlay(device, buf); if (!played) { char buf2[CDOGS_PATH_MAX]; GetDataFilePath(buf2, missionPath); PathGetDirname(buf, buf2); strcat(buf, music); played = MusicPlay(device, buf); } } if (!played && gGameSongs != NULL) { MusicPlay(device, gGameSongs->path); ShiftSongs(&gGameSongs); } }
/***************************************************** * @func : MusicPause() * @summary: 暂停音乐文件播放 * @in : void * @out : none * @return : TRUE : ALL OK * : FALSE : 音乐播放失败 * @author : DaPPer * @time : 2013-3-24 16:06:55 * @history: ******************************************************/ int CMusicProcessor::MusicPause() { if (NULL == player) { return FALSE; } int iRet = FALSE; if (true == PlayStatus.bPlay) { iRet = player->Pause(); if (TRUE == iRet) { PlayStatus.bPlay = false; } } else { iRet = MusicPlay(); } return iRet; }
void MusicPlayMenu(SoundDevice *device) { MusicStop(device); if (gMenuSongs) { MusicPlay(device, gMenuSongs->path); ShiftSongs(&gMenuSongs); } }
/***************************************************** * @func : NewMusicPlay * @summary: 播放一首新的歌曲所需要的处理 * @in : const TCHAR* szFileName : 要播放的歌曲的名字 * @out : none * @return : TRUE : ALL OK * : FALSE : 音乐播放失败 * @author : DaPPer * @time : 2013-3-31 20:17:40 * @history: ******************************************************/ int CMusicProcessor::NewMusicPlay(const TCHAR* szFileName) { int iRet = FALSE; if (MusicClose() && OpenFile(szFileName) && MusicPlay()) { iRet = TRUE; } return iRet; }
int PlayLaserGame(int diff_level) { int i, img, done, quit, frame, lowest, lowest_y, tux_img, old_tux_img, tux_pressing, tux_anim, tux_anim_frame, tux_same_counter, level_start_wait, num_cities_alive, num_comets_alive, paused, picked_comet, gameover; Uint16 key_unicode; SDL_Event event; Uint32 last_time = 0; Uint32 now_time = 0; SDLKey key; SDL_Rect src, dest; /* str[] is a buffer to draw the scores, waves, etc. (don't need wchar_t) */ char str[64]; LOG( "starting Comet Zap game\n" ); DOUT( diff_level ); SDL_ShowCursor(0); laser_load_data(); /* Clear window: */ SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); SDL_Flip(screen); /* --- MAIN GAME LOOP: --- */ done = 0; quit = 0; src.w = src.h = 0; /* Prepare to start the game: */ wave = 1; score = 0; gameover = 0; level_start_wait = LEVEL_START_WAIT_START; /* (Create and position cities) */ for (i = 0; i < NUM_CITIES; i++) { cities[i].alive = 1; cities[i].expl = 0; cities[i].shields = 1; } /* figure out x placement: */ calc_city_pos(); num_cities_alive = NUM_CITIES; num_comets_alive = 0; /* (Clear laser) */ laser.alive = 0; /* Reset remaining stuff: */ laser_reset_level(diff_level); /* --- MAIN GAME LOOP!!! --- */ frame = 0; paused = 0; picked_comet = -1; tux_img = IMG_TUX_RELAX1; tux_anim = -1; tux_anim_frame = 0; tux_same_counter = 0; ans_num = 0; MusicPlay(musics[MUS_GAME + (rand() % NUM_MUSICS)], 0); do { frame++; last_time = SDL_GetTicks(); old_tux_img = tux_img; tux_pressing = 0; /* Handle any incoming events: */ while (SDL_PollEvent(&event) > 0) { if (event.type == SDL_QUIT) { /* Window close event - quit! */ exit(0); } else if (event.type == SDL_KEYDOWN) { key = event.key.keysym.sym; if (key == SDLK_F10) { SwitchScreenMode(); calc_city_pos(); recalc_comet_pos(); } if (key == SDLK_F11) SDL_SaveBMP( screen, "laser.bmp"); if (key == SDLK_ESCAPE) paused = 1; /* --- eat other keys until level wait has passed --- */ if (level_start_wait > 0) key = SDLK_UNKNOWN; key_unicode = event.key.keysym.unicode; //key_unicode = event.key.keysym.unicode & 0xff; DEBUGCODE { fprintf(stderr, "key_unicode = %d\n", key_unicode); } /* For now, tuxtype is case-insensitive for input, */ /* with only uppercase for answers: */ if (key_unicode >= 97 && key_unicode <= 122) key_unicode -= 32; //convert lowercase to uppercase if (key_unicode >= 224 && key_unicode <= 255) key_unicode -= 32; //same for non-US chars LOG ("After checking for lower case:\n"); DEBUGCODE { fprintf(stderr, "key_unicode = %d\n", key_unicode); } /* Now update with case-folded value: */ ans[ans_num++] = key_unicode; } } /* Handle answer: */ for (;ans_num>0;ans_num--) { /* Pick the lowest shootable comet which has the right answer: */ lowest_y = 0; lowest = -1; for (i = 0; i < MAX_COMETS; i++) if (comets[i].alive && comets[i].shootable && comets[i].expl == 0 && comets[i].ch == ans[ans_num -1 ] && comets[i].y > lowest_y) { lowest = i; lowest_y = comets[i].y; } /* If there was an comet with this answer, destroy it! */ if (lowest != -1) { /* Destroy comet: */ comets[lowest].expl = COMET_EXPL_START; /* Make next letter in word shootable: */ comets[lowest].shootable = 0; if (comets[lowest].next) comets[lowest].next->shootable = 1; /* Fire laser: */ laser.alive = LASER_START; /* this is a hack so drawing to the center of the screen works */ if (abs(comets[lowest].x - screen->w/2) < 10) { laser.x1 = screen->w / 2; laser.y1 = screen->h; laser.x2 = laser.x1; laser.y2 = comets[lowest].y; } else { laser.x1 = screen->w / 2; laser.y1 = screen->h; laser.x2 = comets[lowest].x; laser.y2 = comets[lowest].y; } PlaySound(sounds[SND_LASER]); /* 50% of the time.. */ if (0 == (rand() % 2)) { /* ... pick an animation to play: */ if (0 == (rand() % 2)) tux_anim = IMG_TUX_YES1; else tux_anim = IMG_TUX_YAY1; tux_anim_frame = ANIM_FRAME_START; } /* Increment score: */ laser_add_score( (diff_level+1) * 5 * ((screen->h - comets[lowest].y)/20 )); } else { /* Didn't hit anything! */ PlaySound(sounds[SND_BUZZ]); if (0 == (rand() % 2)) tux_img = IMG_TUX_DRAT; else tux_img = IMG_TUX_YIPE; laser_add_score( -25 * wave); } } /* Handle start-wait countdown: */ if (level_start_wait > 0) { level_start_wait--; if (level_start_wait > LEVEL_START_WAIT_START / 4) tux_img = IMG_TUX_RELAX1; else if (level_start_wait > 0) tux_img = IMG_TUX_RELAX2; else tux_img = IMG_TUX_SIT; if (level_start_wait == LEVEL_START_WAIT_START / 4) PlaySound(sounds[SND_ALARM]); } /* If Tux pressed a button, pick a new (different!) stance: */ if (tux_pressing) { while (tux_img == old_tux_img) tux_img = IMG_TUX_CONSOLE1 + (rand() % 3); PlaySound(sounds[SND_TOCK]); } /* If Tux is being animated, show the animation: */ if (tux_anim != -1) { tux_anim_frame--; if (tux_anim_frame < 0) tux_anim = -1; else tux_img = tux_anim + 1 - (tux_anim_frame / (ANIM_FRAME_START / 2)); } /* Reset Tux to sitting if he's been doing nothing for a while: */ if (old_tux_img == tux_img) { tux_same_counter++; if (tux_same_counter >= 20) old_tux_img = tux_img = IMG_TUX_SIT; if (tux_same_counter >= 60) old_tux_img = tux_img = IMG_TUX_RELAX1; } else tux_same_counter = 0; /* Handle comets: */ num_comets_alive = 0; distanceMoved += speed; for (i = 0; i < MAX_COMETS; i++) { if (comets[i].alive) { num_comets_alive++; comets[i].x = comets[i].x + 0; comets[i].y = comets[i].y + speed; if (comets[i].y >= (screen->h - images[IMG_CITY_BLUE]->h) && comets[i].expl == 0) { /* Disable shields or destroy city: */ if (cities[comets[i].city].shields) { cities[comets[i].city].shields = 0; PlaySound(sounds[SND_SHIELDSDOWN]); laser_add_score(-50 * (diff_level+1)); } else { cities[comets[i].city].expl = CITY_EXPL_START; PlaySound(sounds[SND_EXPLOSION]); laser_add_score(-100 * (diff_level+1)); } tux_anim = IMG_TUX_FIST1; tux_anim_frame = ANIM_FRAME_START; /* Destroy comet: */ comets[i].expl = COMET_EXPL_START; } /* Handle comet explosion animation: */ if (comets[i].expl != 0) { comets[i].expl--; if (comets[i].expl == 0) comets[i].alive = 0; } } } /* Handle laser: */ if (laser.alive > 0) laser.alive--; /* Comet time! */ if (level_start_wait == 0 && (frame % 5) == 0 && gameover == 0) { if (num_attackers > 0) { /* More comets to add during this wave! */ if ((num_comets_alive < 2 || ((rand() % 4) == 0)) && distanceMoved > 40) { distanceMoved = 0; laser_add_comet(diff_level); num_attackers--; } } else { if (num_comets_alive == 0) { /* Time for the next wave! */ /* FIXME: End of level stuff goes here */ if (num_cities_alive > 0) { /* Go on to the next wave: */ wave++; laser_reset_level(diff_level); } else { /* No more cities! Game over! */ gameover = GAMEOVER_COUNTER_START; } } } } /* Handle cities: */ num_cities_alive = 0; for (i = 0; i < NUM_CITIES; i++) if (cities[i].alive) { num_cities_alive++; /* Handle animated explosion: */ if (cities[i].expl) { cities[i].expl--; if (cities[i].expl == 0) cities[i].alive = 0; } } /* Handle game-over: */ if (gameover > 0) { gameover--; if (gameover == 0) done = 1; } if ((num_cities_alive==0) && (gameover == 0)) gameover = GAMEOVER_COUNTER_START; /* Draw background: */ SDL_BlitSurface(CurrentBkgd(), NULL, screen, NULL); /* Draw wave: */ dest.x = 0; dest.y = 0; dest.w = images[IMG_WAVE]->w; dest.h = images[IMG_WAVE]->h; SDL_BlitSurface(images[IMG_WAVE], NULL, screen, &dest); sprintf(str, "%d", wave); laser_draw_numbers(str, images[IMG_WAVE]->w + (images[IMG_NUMBERS]->w / 10)); /* Draw score: */ dest.x = (screen->w - ((images[IMG_NUMBERS]->w / 10) * 7) - images[IMG_SCORE]->w); dest.y = 0; dest.w = images[IMG_SCORE]->w; dest.h = images[IMG_SCORE]->h; SDL_BlitSurface(images[IMG_SCORE], NULL, screen, &dest); sprintf(str, "%.6d", score); laser_draw_numbers(str, screen->w - ((images[IMG_NUMBERS]->w / 10) * 6)); /* Draw comets: */ for (i = 0; i < MAX_COMETS; i++) if (comets[i].alive) { /* Decide which image to display: */ if (comets[i].expl == 0) img = IMG_COMET1 + ((frame + i) % 3); else img = (IMG_COMETEX2 - (comets[i].expl / (COMET_EXPL_START / 2))); /* Draw it! */ dest.x = comets[i].x - (images[img]->w / 2); dest.y = comets[i].y - images[img]->h; dest.w = images[img]->w; dest.h = images[img]->h; SDL_BlitSurface(images[img], NULL, screen, &dest); } /* Draw letters: */ for (i = 0; i < MAX_COMETS; i++) if (comets[i].alive && comets[i].expl == 0) laser_draw_let(comets[i].ch, comets[i].x, comets[i].y); /* Draw cities: */ if (frame%2 == 0) NEXT_FRAME(shield); for (i = 0; i < NUM_CITIES; i++) { /* Decide which image to display: */ if (cities[i].alive) { if (cities[i].expl == 0) img = IMG_CITY_BLUE; else img = (IMG_CITY_BLUE_EXPL5 - (cities[i].expl / (CITY_EXPL_START / 5))); } else img = IMG_CITY_BLUE_DEAD; /* Change image to appropriate color: */ img += ((wave % MAX_CITY_COLORS) * (IMG_CITY_GREEN - IMG_CITY_BLUE)); /* Draw it! */ dest.x = cities[i].x - (images[img]->w / 2); dest.y = (screen->h) - (images[img]->h); dest.w = (images[img]->w); dest.h = (images[img]->h); SDL_BlitSurface(images[img], NULL, screen, &dest); /* Draw sheilds: */ if (cities[i].shields) { dest.x = cities[i].x - (shield->frame[shield->cur]->w / 2); dest.h = (screen->h) - (shield->frame[shield->cur]->h); dest.w = src.w; dest.h = src.h; SDL_BlitSurface( shield->frame[shield->cur], NULL, screen, &dest); } } /* Draw laser: */ if (laser.alive) laser_draw_line(laser.x1, laser.y1, laser.x2, laser.y2, 255 / (LASER_START - laser.alive), 192 / (LASER_START - laser.alive), 64); laser_draw_console_image(IMG_CONSOLE); if (gameover > 0) tux_img = IMG_TUX_FIST1 + ((frame / 2) % 2); laser_draw_console_image(tux_img); /* Draw "Game Over" */ if (gameover > 0) { dest.x = (screen->w - images[IMG_GAMEOVER]->w) / 2; dest.y = (screen->h - images[IMG_GAMEOVER]->h) / 2; dest.w = images[IMG_GAMEOVER]->w; dest.h = images[IMG_GAMEOVER]->h; SDL_BlitSurface(images[IMG_GAMEOVER], NULL, screen, &dest); } /* Swap buffers: */ SDL_Flip(screen); /* If we're in "PAUSE" mode, pause! */ if (paused) { quit = Pause(); paused = 0; } /* Keep playing music: */ if (settings.sys_sound && !Mix_PlayingMusic()) MusicPlay(musics[MUS_GAME + (rand() % NUM_MUSICS)], 0); /* Pause (keep frame-rate event) */ DEBUGCODE { fprintf(stderr, "now_time = %d\tlast_time = %d, elapsed time = %d\n", now_time, last_time, now_time - last_time); } now_time = SDL_GetTicks(); if (now_time < last_time + FPS) SDL_Delay(last_time + FPS - now_time); }