Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

View.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "View.h"
00010 
00011 #include "PhaseLocker.h"
00012 #include "ModelList.h"
00013 #include "Decor.h"
00014 
00015 #include "Cube.h"
00016 #include "Anim.h"
00017 #include "Dir.h"
00018 #include "minmax.h"
00019 
00020 //-----------------------------------------------------------------
00021 /**
00022  * Create new view.
00023  * @param models wrapper arount models
00024  */
00025 View::View(const ModelList &models)
00026      : m_models(models), m_screenShift(0, 0)
00027 {
00028     m_animShift = 0;
00029     m_shiftSize = SCALE;
00030     m_screen = NULL;
00031 }
00032 //-----------------------------------------------------------------
00033 View::~View()
00034 {
00035     removeDecors();
00036 }
00037 //-----------------------------------------------------------------
00038 void
00039 View::removeDecors()
00040 {
00041     t_decors::iterator end = m_decors.end();
00042     for (t_decors::iterator i = m_decors.begin(); i != end; ++i) {
00043         delete *i;
00044     }
00045     m_decors.clear();
00046 }
00047 //-----------------------------------------------------------------
00048 void
00049 View::drawDecors()
00050 {
00051     t_decors::iterator end = m_decors.end();
00052     for (t_decors::iterator i = m_decors.begin(); i != end; ++i) {
00053         (*i)->drawOnScreen(this, m_screen);
00054     }
00055 }
00056 
00057 //-----------------------------------------------------------------
00058 /**
00059  * Prepare new anim.
00060  */
00061     void
00062 View::noteNewRound(int phases)
00063 {
00064     m_animShift = 0;
00065     computeShiftSize(phases);
00066 }
00067 //-----------------------------------------------------------------
00068 void
00069 View::drawOn(SDL_Surface *screen)
00070 {
00071     m_screen = screen;
00072     m_animShift = min(SCALE, m_animShift + m_shiftSize);
00073     m_models.drawOn(this);
00074     drawDecors();
00075 }
00076 //-----------------------------------------------------------------
00077 /**
00078  * Draw model.
00079  * Care about model shift during move.
00080  */
00081     void
00082 View::drawModel(Cube *model)
00083 {
00084     if (!model->isLost()) {
00085         V2 screenPos = getScreenPos(model);
00086 
00087         Anim::eSide side = Anim::SIDE_LEFT;
00088         if (!model->isLeft()) {
00089             side = Anim::SIDE_RIGHT;
00090         }
00091         model->anim()->drawAt(m_screen,
00092                 screenPos.getX(), screenPos.getY(), side);
00093     }
00094 }
00095 //-----------------------------------------------------------------
00096 /**
00097  * Split move in a few phases.
00098  */
00099     void
00100 View::computeShiftSize(int phases)
00101 {
00102     if (phases > 0) {
00103         m_shiftSize = SCALE / phases;
00104     }
00105     else {
00106         m_shiftSize = SCALE;
00107     }
00108 }
00109 //-----------------------------------------------------------------
00110 /**
00111  * Returns position on screen when model will be drawn.
00112  */
00113 V2
00114 View::getScreenPos(const Cube *model) const
00115 {
00116     V2 shift(0, 0);
00117     Dir::eDir dir = model->getLastMoveDir();
00118     if (dir != Dir::DIR_NO) {
00119         shift = Dir::dir2xy(dir);
00120         shift = shift.scale(m_animShift);
00121     }
00122     shift = shift.plus(m_screenShift);
00123 
00124     V2 anim_shift = model->const_anim()->getViewShift();
00125     return model->getLocation().plus(anim_shift).scale(SCALE).plus(shift);
00126 }
00127 //-----------------------------------------------------------------
00128 /**
00129  * Returns position of tile under cursor.
00130  */
00131 V2
00132 View::getFieldPos(const V2 &cursor) const
00133 {
00134     return cursor.minus(m_screenShift).shrink(SCALE);
00135 }
00136 

Generated on Wed Jun 1 09:54:32 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2