// $Id: starparticlesystem.cc,v 1.4 2006/08/05 09:08:59 matthew Exp $ // Fish Supper // Copyright (C) 2006 Matthew Clarke // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. #include "starparticlesystem.h" // ******************* // *** CONSTRUCTOR *** // ******************* FS::StarParticleSystem::StarParticleSystem() { active = false; for (int i = 0; i < MAX_STARS; ++i) { my_stars[i].isr.y = 0; my_stars[i].isr.w = STAR_WIDTH; my_stars[i].isr.h = STAR_HEIGHT; } // for erase_rect.w = STAR_WIDTH; erase_rect.h = STAR_HEIGHT; } // FS::StarParticleSystem::StarParticleSystem() // ****************** // *** DESTRUCTOR *** // ****************** FS::StarParticleSystem::~StarParticleSystem() { // NYI } // FS::StarParticleSystem::~StarParticleSystem() // ************************ // *** MEMBER FUNCTIONS *** // ************************ // ************************************************** void FS::StarParticleSystem::update(int time_passed, SDL_Rect box, PlayDisplay * pd) { // Check if we need to launch a new star particle. if ( (!stopped) && (num_stars < MAX_STARS) && ((time_passed - start_time) > LAUNCH_INTERVAL) ) { launch_new_star(time_passed, box); start_time = time_passed + ((time_passed - start_time) % LAUNCH_INTERVAL); } // if // Update existing star particles. bool still_alive = false; min_x = SCREEN_WIDTH; max_x = -1; min_y = SCREEN_HEIGHT; max_y = -1; for (int i = 0; i < MAX_STARS; ++i) { if ( my_stars[i].active ) { if ( (time_passed - my_stars[i].start_time) > DURATION ) { ++my_stars[i].current_frame; if ( my_stars[i].current_frame >= NUM_FRAMES ) { my_stars[i].active = false; continue; } // if my_stars[i].start_time = time_passed + ((time_passed - my_stars[i].start_time) % DURATION); } // if still_alive = true; // update min/max values min_x = min(min_x, my_stars[i].x); max_x = max(max_x, my_stars[i].x); min_y = min(min_y, my_stars[i].y); max_y = max(max_y, my_stars[i].y); } // if } // for if ( !still_alive ) { active = false; } else { draw_stars(pd); } // if ... else } // FS::StarParticleSystem::update() // ************************************************** void FS::StarParticleSystem::activate(int t, SDL_Rect box) { active = true; stopped = false; start_time = t; num_stars = 0; for (int i = 0; i < MAX_STARS; ++i) { my_stars[i].active = false; } // for // launch a new star straight away launch_new_star(t, box); } // FS::StarParticleSystem::activate() // ************************************************** void FS::StarParticleSystem::stop(int t) { stopped = true; } // FS::StarParticleSystem::stop() // ************************************************** void FS::StarParticleSystem::launch_new_star(int t, SDL_Rect box) { box.x -= STAR_WIDTH; box.w += STAR_WIDTH; box.y -= STAR_HEIGHT; box.h += STAR_HEIGHT; my_stars[num_stars].active = true; my_stars[num_stars].current_frame = 0; my_stars[num_stars].x = box.x + (rand() % box.w); my_stars[num_stars].y = box.y + (rand() % box.h); my_stars[num_stars].start_time = t; ++num_stars; } // FS::StarParticleSystem::launch_new_star() // ************************************************** void FS::StarParticleSystem::draw_stars(PlayDisplay * pd) { for (int i = 0; i < MAX_STARS; ++i) { if (my_stars[i].active) { erase_rect.x = my_stars[i].x; erase_rect.y = my_stars[i].y; pd->request_erase(&erase_rect); my_stars[i].isr.x = my_stars[i].current_frame * STAR_WIDTH; dest_rect.x = my_stars[i].x; dest_rect.y = my_stars[i].y; pd->request_blit( STARS, &my_stars[i].isr, &dest_rect ); } // if } // for update_rect.x = max(0, min_x); update_rect.y = max(0, min_y); update_rect.w = min(SCREEN_WIDTH - 1, max_x + STAR_WIDTH) - update_rect.x; update_rect.h = min(SCREEN_HEIGHT - 1, max_y + STAR_HEIGHT) - update_rect.y; pd->request_update(update_rect); } // FS::StarParticleSystem::draw_stars() // ************************************************** // ************************************************** // ************************************************** // **************************************************