Debian Codegraph @

src/menu/NodeDrawer.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "NodeDrawer.h"
10
11 #include "ResImagePack.h"
12 #include "Font.h"
13
14 #include "Log.h"
15 #include "V2.h"
16 #include "Path.h"
17 #include "LevelNode.h"
18 #include "TimerAgent.h"
19 #include "minmax.h"
20
21 #include "SDL_gfxPrimitives.h"
22
23 //-----------------------------------------------------------------
24 NodeDrawer::NodeDrawer()
25 {
26 m_font = new Font(Path::dataReadPath("font/font_menu.ttf"), 22);
27
28 m_imagePack = new ResImagePack();
29 m_imagePack->addImage("solved",
30 Path::dataReadPath("images/menu/n0.png"));
31
32 m_imagePack->addImage("open",
33 Path::dataReadPath("images/menu/n1.png"));
34 m_imagePack->addImage("open",
35 Path::dataReadPath("images/menu/n2.png"));
36 m_imagePack->addImage("open",
37 Path::dataReadPath("images/menu/n3.png"));
38 m_imagePack->addImage("open",
39 Path::dataReadPath("images/menu/n4.png"));
40
41 m_imagePack->addImage("far",
42 Path::dataReadPath("images/menu/n_far.png"));
43 }
44 //-----------------------------------------------------------------
45 NodeDrawer::~NodeDrawer()
46 {
47 m_imagePack->removeAll();
48 delete m_imagePack;
49 delete m_font;
50 }
51 //-----------------------------------------------------------------
52 /**
53 * Draw blinking dot centred on node position.
54 */
55 void
56 NodeDrawer::drawNode(const LevelNode *node) const
57 {
58 V2 loc = node->getLoc();
59 drawDot(m_imagePack->getRes("far"), loc);
60
61 SDL_Surface *dot = NULL;
62 switch (node->getState()) {
63 case LevelNode::STATE_FAR:
64 return;
65 case LevelNode::STATE_OPEN:
66 {
67 int phase = TimerAgent::agent()->getCycles() % 10;
68 if (phase > 4) {
69 phase--;
70 }
71 if (phase > 7) {
72 phase--;
73 }
74 if (phase >= 4) {
75 phase = 7 - phase;
76 }
77 dot = m_imagePack->getRes("open", phase);
78 }
79 break;
80 case LevelNode::STATE_SOLVED:
81 dot = m_imagePack->getRes("solved");
82 break;
83 default:
84 LOG_WARNING(ExInfo("don't know how to draw node")
85 .addInfo("state", node->getState()));
86 return;
87 }
88 drawDot(dot, loc);
89 }
90 //-----------------------------------------------------------------
91 /**
92 * Draw centred.
93 * @param x x cord. or centre
94 * @param x y cord. or centre
95 */
96 void
97 NodeDrawer::drawDot(SDL_Surface *dot, const V2 &loc) const
98 {
99 SDL_Rect rect;
100 rect.x = loc.getX() - dot->w / 2;
101 rect.y = loc.getY() - dot->h / 2;
102 SDL_BlitSurface(dot, NULL, m_screen, &rect);
103 }
104 //-----------------------------------------------------------------
105 /**
106 * Highlightes selected node.
107 */
108 void
109 NodeDrawer::drawSelect(const V2 &loc) const
110 {
111
112 const SDL_Surface *dot = m_imagePack->getRes("solved");
113 int radius = max(dot->w, dot->h) / 2 + 1;
114 Uint32 colorRGBA = 0xffc61880;
115
116 filledCircleColor(m_screen, loc.getX(), loc.getY(), radius, colorRGBA);
117 }
118 //-----------------------------------------------------------------
119 /**
120 * Draws name of selected level.
121 */
122 void
123 NodeDrawer::drawSelected(const std::string &levelname) const
124 {
125 //TODO: draw deflected text
126 int text_width = m_font->calcTextWidth(levelname);
127
128 SDL_Rect rect;
129 rect.x = (m_screen->w - text_width) / 2;
130 rect.y = m_screen->h - 50;
131
132 SDL_Color color = {255, 255, 0, 255};
133 SDL_Surface *surface = m_font->renderTextOutlined(levelname, color);
134 SDL_BlitSurface(surface, NULL, m_screen, &rect);
135 SDL_FreeSurface(surface);
136 }
137 //-----------------------------------------------------------------
138 void
139 NodeDrawer::drawEdge(const LevelNode *start, const LevelNode *end) const
140 {
141 //TODO: nice curves
142 Sint16 x1 = start->getLoc().getX();
143 Sint16 y1 = start->getLoc().getY();
144 Sint16 x2 = end->getLoc().getX();
145 Sint16 y2 = end->getLoc().getY();
146
147 Uint32 colorRGBA = 0xffff00ff;
148 aalineColor(m_screen, x1, y1, x2, y2, colorRGBA);
149 aalineColor(m_screen, x1 - 1, y1 - 1 , x2 - 1, y2 - 1, colorRGBA);
150 aalineColor(m_screen, x1 + 1, y1 + 1 , x2 + 1, y2 + 1, colorRGBA);
151 aalineColor(m_screen, x1 - 1, y1 + 1 , x2 - 1, y2 + 1, colorRGBA);
152 aalineColor(m_screen, x1 + 1, y1 - 1 , x2 + 1, y2 - 1, colorRGBA);
153 }
154