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

NodeDrawer.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 "NodeDrawer.h"
00010 
00011 #include "ResImagePack.h"
00012 #include "Font.h"
00013 
00014 #include "Log.h"
00015 #include "V2.h"
00016 #include "Path.h"
00017 #include "LevelNode.h"
00018 #include "TimerAgent.h"
00019 
00020 #include "SDL_gfxPrimitives.h"
00021 
00022 //-----------------------------------------------------------------
00023 NodeDrawer::NodeDrawer()
00024 {
00025     //TODO: allow to set font and color
00026     m_font = new Font(Path::dataReadPath("font/font_menu.ttf"), 22);
00027 
00028     m_imagePack = new ResImagePack();
00029     m_imagePack->addImage("solved",
00030             Path::dataReadPath("images/menu/n0.png"));
00031 
00032     m_imagePack->addImage("open",
00033             Path::dataReadPath("images/menu/n1.png"));
00034     m_imagePack->addImage("open",
00035             Path::dataReadPath("images/menu/n2.png"));
00036     m_imagePack->addImage("open",
00037             Path::dataReadPath("images/menu/n3.png"));
00038     m_imagePack->addImage("open",
00039             Path::dataReadPath("images/menu/n4.png"));
00040 
00041     m_imagePack->addImage("far",
00042             Path::dataReadPath("images/menu/n_far.png"));
00043 }
00044 //-----------------------------------------------------------------
00045 NodeDrawer::~NodeDrawer()
00046 {
00047     m_imagePack->removeAll();
00048     delete m_imagePack;
00049     delete m_font;
00050 }
00051 //-----------------------------------------------------------------
00052 /**
00053  * Draw blinking dot centred on node position.
00054  */
00055 void
00056 NodeDrawer::drawNode(const LevelNode *node) const
00057 {
00058     V2 loc = node->getLoc();
00059     drawDot(m_imagePack->getRes("far"), loc);
00060 
00061     SDL_Surface *dot = NULL;
00062     switch (node->getState()) {
00063         case LevelNode::STATE_FAR:
00064             return;
00065         case LevelNode::STATE_OPEN:
00066             {
00067                 int phase = TimerAgent::agent()->getCycles() % 10;
00068                 if (phase > 4) {
00069                     phase--;
00070                 }
00071                 if (phase > 7) {
00072                     phase--;
00073                 }
00074                 if (phase >= 4) {
00075                     phase = 7 - phase;
00076                 }
00077                 dot = m_imagePack->getRes("open", phase);
00078             }
00079             break;
00080         case LevelNode::STATE_SOLVED:
00081             dot = m_imagePack->getRes("solved");
00082             break;
00083         default:
00084             LOG_WARNING(ExInfo("don't know how to draw node")
00085                     .addInfo("state", node->getState()));
00086             return;
00087     }
00088     drawDot(dot, loc);
00089 }
00090 //-----------------------------------------------------------------
00091 /**
00092  * Draw centred.
00093  * @param x x cord. or centre
00094  * @param x y cord. or centre
00095  */
00096 void
00097 NodeDrawer::drawDot(SDL_Surface *dot, const V2 &loc) const
00098 {
00099     SDL_Rect rect;
00100     rect.x = loc.getX() - dot->w / 2;
00101     rect.y = loc.getY() - dot->h / 2;
00102     SDL_BlitSurface(dot, NULL, m_screen, &rect);
00103 }
00104 //-----------------------------------------------------------------
00105 void
00106 NodeDrawer::drawSelected(const std::string &levelname) const
00107 {
00108     //TODO: draw deflected text
00109     int text_width = m_font->calcTextWidth(levelname);
00110 
00111     SDL_Rect rect;
00112     rect.x = (m_screen->w - text_width) / 2;
00113     rect.y = m_screen->h - 50;
00114 
00115     SDL_Color color = {255, 255, 0, 255};
00116     SDL_Surface *surface = m_font->renderTextOutlined(levelname, color);
00117     SDL_BlitSurface(surface, NULL, m_screen, &rect);
00118     SDL_FreeSurface(surface);
00119 }
00120 //-----------------------------------------------------------------
00121 void
00122 NodeDrawer::drawEdge(const LevelNode *start, const LevelNode *end) const
00123 {
00124     //TODO: nice curves
00125     Sint16 x1 = start->getLoc().getX();
00126     Sint16 y1 = start->getLoc().getY();
00127     Sint16 x2 = end->getLoc().getX();
00128     Sint16 y2 = end->getLoc().getY();
00129 
00130     Uint32 colorRGBA = 0xffff00ff;
00131     aalineColor(m_screen, x1, y1, x2, y2, colorRGBA);
00132     aalineColor(m_screen, x1 - 1, y1 - 1 , x2 - 1, y2 - 1, colorRGBA);
00133     aalineColor(m_screen, x1 + 1, y1 + 1 , x2 + 1, y2 + 1, colorRGBA);
00134     aalineColor(m_screen, x1 - 1, y1 + 1 , x2 - 1, y2 + 1, colorRGBA);
00135     aalineColor(m_screen, x1 + 1, y1 - 1 , x2 + 1, y2 - 1, colorRGBA);
00136 }
00137 

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