// $Id: settingsdisplay.cc,v 1.6 2006/08/26 19:40:49 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 "settingsdisplay.h" // ******************* // *** CONSTRUCTOR *** // ******************* FS::SettingsDisplay::SettingsDisplay(SDL_Surface * s, Settings * _my_settings) { screen = s; my_settings = _my_settings; background = SDL_DisplayFormat( IMG_Load( PKG_DATA_DIR "/images/settings/background.png" )); // load images std::string filename; char digits[3]; for (int i = 0; i < NUM_IMAGES; ++i) { sprintf(digits, "%02d", i); filename = (PKG_DATA_DIR "/images/settings/image_"); (filename += digits) += ".png"; my_images[i] = SDL_DisplayFormatAlpha(IMG_Load(filename.c_str())); SDL_SetAlpha(my_images[i], SDL_SRCALPHA|SDL_RLEACCEL, 0); } // for init_rects(); } // FS::SettingsDisplay::SettingsDisplay() // ****************** // *** DESTRUCTOR *** // ****************** FS::SettingsDisplay::~SettingsDisplay() { for (int i = 0; i < NUM_IMAGES; ++i) { SDL_FreeSurface(my_images[i]); } // for SDL_FreeSurface(background); } // FS::SettingsDisplay::~SettingsDisplay() // ************************ // *** MEMBER FUNCTIONS *** // ************************ // ************************************************** void FS::SettingsDisplay::start_draw() { current_option = last_option = CONTROL; init_scratch(); SDL_BlitSurface(background, NULL, screen, NULL); for (int i = 0; i < NUM_OPTIONS; ++i) { if ( i == current_option ) { SDL_BlitSurface(my_images[i], NULL, screen, &option_rects[i]); draw_tick(i, true); } else { SDL_BlitSurface(my_images[i+FAINT_OFFSET], NULL, screen, &option_rects[i]); draw_tick(i, false); } // if ... else } // for SDL_UpdateRect(screen, 0, 0, 0, 0); } // FS::SettingsDisplay::start_draw() // ************************************************** bool FS::SettingsDisplay::redraw(KeyboardEvent * events, int n) { bool changed = false; bool quit_display = false; // return this value int i = 0; last_option = current_option; while (i < n) { if ( events[i] == ESCAPE_PRESSED ) { quit_display = true; break; // 'short-circuit' } else if ( events[i] == UP_KEY_PRESSED ) { --current_option; if (current_option < 0) { current_option = NUM_OPTIONS - 1; } // if changed = true; } else if ( events[i] == DOWN_KEY_PRESSED) { current_option = (current_option + 1) % NUM_OPTIONS; changed = true; } else if ( events[i] == JUMP_KEY_PRESSED) { if (current_option == OK) { write_scratch(); quit_display = true; break; } else if (current_option == CANCEL) { quit_display = true; break; } else { swap_scratch(current_option); changed = true; } // if ... else } // if ... else ++i; } // while if (changed) { do_redraw(); } // if return quit_display; } // FS::SettingsDisplay::redraw() // ************************************************** void FS::SettingsDisplay::init_rects() { // FIXME: could use constants here! //int xs[] = { 230, 230, 230, 383, 365 }; //int ys[] = { 150, 270, 390, 500, 540 }; //int ws[] = { 340, 340, 340, 33, 69 }; //int hs[] = { 86, 86, 86, 17, 18 }; int xs[] = { 80, 80, 80, 233, 215 }; int ys[] = { 120, 240, 360, 470, 510 }; int ws[] = { 340, 340, 340, 33, 69 }; int hs[] = { 86, 86, 86, 17, 18 }; for (int i = 0; i < NUM_OPTIONS; ++i) { option_rects[i].x = xs[i]; option_rects[i].y = ys[i]; option_rects[i].w = ws[i]; option_rects[i].h = hs[i]; } // for } // FS::SettingsDisplay::init_rects() // ************************************************** void FS::SettingsDisplay::do_redraw() { // erase last option and redraw faint SDL_BlitSurface(background, &option_rects[last_option], screen, &option_rects[last_option]); SDL_BlitSurface(my_images[last_option+FAINT_OFFSET], NULL, screen, &option_rects[last_option]); draw_tick(last_option, false); // redraw current option normal (i.e. highlighted) // erase first in case user has pressed 'jump' (and we're swapping ticks) SDL_BlitSurface(background, &option_rects[current_option], screen, &option_rects[current_option]); SDL_BlitSurface(my_images[current_option], NULL, screen, &option_rects[current_option]); draw_tick(current_option, true); dirty_rects[0] = option_rects[last_option]; dirty_rects[1] = option_rects[current_option]; SDL_UpdateRects(screen, 2, dirty_rects); } // FS::SettingsDisplay::do_redraw() // ************************************************** void FS::SettingsDisplay::init_scratch() { scratch_joystick_selected = my_settings->is_joystick_selected(); scratch_sound = my_settings->is_sound(); scratch_fullscreen = my_settings->is_fullscreen(); } // FS::SettingsDisplay::init_scratch() // ************************************************** void FS::SettingsDisplay::draw_tick(int option, bool highlighted) { if (option > SIZE) { return; } // if SDL_Rect tick_rect; tick_rect.y = option_rects[option].y + TICK_Y_OFFSET; tick_rect.w = 50; tick_rect.h = 50; if (option == CONTROL) { if (scratch_joystick_selected) { tick_rect.x = option_rects[option].x + TICK1_X_OFFSET; } else { tick_rect.x = option_rects[option].x + TICK0_X_OFFSET; } // if ... else } else if (option == SOUND) { if (scratch_sound) { tick_rect.x = option_rects[option].x + TICK0_X_OFFSET; } else { tick_rect.x = option_rects[option].x + TICK1_X_OFFSET; } // if ... else } else if (option == SIZE) { if (scratch_fullscreen) { tick_rect.x = option_rects[option].x + TICK1_X_OFFSET; } else { tick_rect.x = option_rects[option].x + TICK0_X_OFFSET; } // if ... else } // if ... else SDL_BlitSurface( my_images[IMAGE_TICK + ((highlighted) ? 0 : FAINT_OFFSET)], NULL, screen, &tick_rect); } // FS::SettingsDisplay::draw_tick() // ************************************************** void FS::SettingsDisplay::swap_scratch(int option) { switch (option) { case CONTROL: scratch_joystick_selected = !scratch_joystick_selected; break; case SOUND: scratch_sound = !scratch_sound; break; case SIZE: scratch_fullscreen = !scratch_fullscreen; break; case OK: case CANCEL: break; } // switch } // FS::SettingsDisplay::swap_scratch() // ************************************************** void FS::SettingsDisplay::write_scratch() { //my_settings->write_config_file(scratch_joystick_selected, scratch_sound, // scratch_fullscreen, my_settings->get_furthest_level()); my_settings->set_joystick_selected(scratch_joystick_selected); my_settings->set_sound(scratch_sound); my_settings->set_fullscreen(scratch_fullscreen); } // FS::SettingsDisplay::write_scratch() // **************************************************