2 * evalgraph.c - Evaluation graph back-end part
4 * Author: Alessandro Scotti (Dec 2005)
6 * Copyright 2005 Alessandro Scotti
8 * Enhancments Copyright 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
10 * ------------------------------------------------------------------------
12 * GNU XBoard is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or (at
15 * your option) any later version.
17 * GNU XBoard is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see http://www.gnu.org/licenses/. *
25 *------------------------------------------------------------------------
26 ** See the file ChangeLog for a revision history. */
28 // code refactored by HGM to obtain front-end / back-end separation
37 #else /* not STDC_HEADERS */
40 # else /* not HAVE_STRING_H */
42 # endif /* not HAVE_STRING_H */
43 #endif /* not STDC_HEADERS */
48 #include "evalgraph.h"
51 ChessProgramStats_Move * currPvInfo;
66 DrawLine (int x1, int y1, int x2, int y2, int penType)
68 DrawSegment( x1, y1, NULL, NULL, PEN_NONE );
69 DrawSegment( x2, y2, NULL, NULL, penType );
74 DrawLineEx (int x1, int y1, int x2, int y2, int penType)
77 DrawSegment( x1, y1, &savX, &savY, PEN_NONE );
78 DrawSegment( x2, y2, NULL, NULL, penType );
79 DrawSegment( savX, savY, NULL, NULL, PEN_NONE );
84 GetPvScore (int index)
86 int score = currPvInfo[ index ].score;
88 if( index & 1 ) score = -score; /* Flip score for black */
94 MakeEvalTitle (char *title)
97 static char buf[MSG_SIZ];
99 if( currCurrent <0 ) return title; // currCurrent = -1 crashed WB on start without ini file!
100 score = currPvInfo[ currCurrent ].score;
101 depth = currPvInfo[ currCurrent ].depth;
103 if( depth <=0 ) return title;
104 if( currCurrent & 1 ) score = -score; /* Flip score for black */
105 snprintf(buf, MSG_SIZ, "%s {%s%.2f/%-2d %d}", title, score>0 ? "+" : " ", score/100., depth, (currPvInfo[currCurrent].time+50)/100);
112 For a centipawn value, this function returns the height of the corresponding
113 histogram, centered on the reference axis.
115 Note: height can be negative!
118 GetValueY (int value)
120 if( value < -range*700 ) value = -range*700;
121 if( value > +range*700 ) value = +range*700;
122 if(value > 100*range) value += appData.zoom * 100 - 100*range; else
123 if(value < -100*range) value -= appData.zoom * 100 - 100*range; else
124 value *= appData.zoom;
125 return (nHeightPB / 2) - (int)(value * (nHeightPB - 2*MarginH) / ((1200. + 200.*appData.zoom)*range));
128 // the brush selection is made part of the DrawLine, by passing a style argument
129 // the wrapper for doing the text output makes this back-end
131 DrawAxisSegmentHoriz (int value, Boolean drawValue)
133 int y = GetValueY( range*value*100 );
136 char buf[MSG_SIZ], *b = buf;
138 if( value > 0 ) *b++ = '+';
139 sprintf(b, "%d", range*value);
141 DrawEvalText(buf, strlen(buf), y);
143 // [HGM] counts on DrawEvalText to have select transparent background for dotted line!
144 DrawLine( MarginX, y, MarginX + MarginW, y, PEN_BLACK ); // Y-axis tick marks
145 DrawLine( MarginX + MarginW, y, nWidthPB - MarginW, y, PEN_DOTTED ); // hor grid
148 // The DrawLines again must select their own brush.
149 // the initial brush selection is useless? BkMode needed for dotted line and text
153 int cy = nHeightPB / 2, space = nHeightPB/(6 + appData.zoom);
155 DrawAxisSegmentHoriz( +5, TRUE );
156 DrawAxisSegmentHoriz( +3, space >= 20 );
157 DrawAxisSegmentHoriz( +1, space >= 20 && space*appData.zoom >= 40 );
158 DrawAxisSegmentHoriz( 0, TRUE );
159 DrawAxisSegmentHoriz( -1, space >= 20 && space*appData.zoom >= 40 );
160 DrawAxisSegmentHoriz( -3, space >= 20 );
161 DrawAxisSegmentHoriz( -5, TRUE );
163 DrawLine( MarginX + MarginW, cy, nWidthPB - MarginW, cy, PEN_BLACK ); // x-axis
164 DrawLine( MarginX + MarginW, MarginH, MarginX + MarginW, nHeightPB - MarginH, PEN_BLACK ); // y-axis
169 DrawHistogram (int x, int y, int width, int value, int side)
171 int left, top, right, bottom;
173 if( value > -appData.evalThreshold*range && value < +appData.evalThreshold*range ) return;
176 right = left + width + 1;
179 top = GetValueY( value );
184 bottom = GetValueY( value ) + 1;
188 if( width == MIN_HIST_WIDTH ) {
190 DrawRectangle( left, top, right, bottom, side, FILLED );
193 DrawRectangle( left, top, right, bottom, side, OPEN );
199 DrawSeparator (int index, int x)
202 if( index == currCurrent ) {
203 DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_BLUEDOTTED );
205 else if( (index % 20) == 0 ) {
206 DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_DOTTED );
211 // made back-end by replacing MoveToEx and LineTo by DrawSegment
212 /* Actually draw histogram as a diagram, cause there's too much data */
214 DrawHistogramAsDiagram (int cy, int paint_width, int hist_count)
219 /* Rescale the graph every few moves (as opposed to every move) */
220 hist_count -= hist_count % 8;
224 step = (double) paint_width / (hist_count + 1);
226 for( i=0; i<2; i++ ) {
227 int index = currFirst;
228 int side = (currCurrent + i + 1) & 1; /* Draw current side last */
229 double x = MarginX + MarginW;
231 if( (index & 1) != side ) {
236 DrawSegment( (int) x, cy, NULL, NULL, PEN_NONE );
240 while( index < currLast ) {
243 DrawSeparator( index, (int) x );
245 /* Extend line up to current point */
246 if( currPvInfo[index].depth > 0 ) {
247 DrawSegment((int) x, GetValueY( GetPvScore(index) ), NULL, NULL, PEN_BOLD + side );
255 // back-end, delete pen selection
257 DrawHistogramFull (int cy, int hist_width, int hist_count)
261 // SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
263 for( i=0; i<hist_count; i++ ) {
264 int index = currFirst + i;
265 int x = MarginX + MarginW + index * hist_width;
267 /* Draw a separator every 10 moves */
268 DrawSeparator( index, x );
271 if( currPvInfo[i].depth > 0 ) {
272 DrawHistogram( x, cy, hist_width, GetPvScore(index), index & 1 );
286 InitVisualization (VisualizationData *vd)
288 Boolean result = FALSE;
290 vd->cy = nHeightPB / 2;
291 vd->hist_width = MIN_HIST_WIDTH;
292 vd->hist_count = currLast - currFirst;
293 vd->paint_width = nWidthPB - MarginX - 2*MarginW;
295 if( vd->hist_count > 0 ) {
299 vd->hist_width = vd->paint_width / vd->hist_count;
301 if( vd->hist_width > MAX_HIST_WIDTH ) vd->hist_width = MAX_HIST_WIDTH;
303 vd->hist_width -= vd->hist_width % 2;
313 VisualizationData vd;
315 if( InitVisualization( &vd ) ) {
316 if( vd.hist_width < MIN_HIST_WIDTH ) {
317 DrawHistogramAsDiagram( vd.cy, vd.paint_width, vd.hist_count );
320 DrawHistogramFull( vd.cy, vd.hist_width, vd.hist_count );
327 GetMoveIndexFromPoint (int x, int y)
330 int start_x = MarginX + MarginW;
331 VisualizationData vd;
333 if( x >= start_x && InitVisualization( &vd ) ) {
334 /* Almost an hack here... we duplicate some of the paint logic */
335 if( vd.hist_width < MIN_HIST_WIDTH ) {
338 vd.hist_count -= vd.hist_count % 8;
342 step = (double) vd.paint_width / (vd.hist_count + 1);
345 result = (int) (0.5 + (double) (x - start_x) / step);
348 result = (x - start_x) / vd.hist_width;
352 if( result >= currLast ) {
359 // init and display part split of so they can be moved to front end
361 PaintEvalGraph (void)
363 VariantClass v = gameInfo.variant;
364 range = (gameInfo.holdingsWidth && v != VariantSuper && v != VariantGreat && v != VariantSChess) ? 2 : 1; // [HGM] double range in drop games
366 DrawRectangle(0, 0, nWidthPB, nHeightPB, 2, FILLED);