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, 2013, 2014 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 {%d: %s%.2f/%-2d %d}", title, currCurrent/2+1,
106 score>0 ? "+" : " ", score/100., depth, (currPvInfo[currCurrent].time+50)/100);
113 For a centipawn value, this function returns the height of the corresponding
114 histogram, centered on the reference axis.
116 Note: height can be negative!
119 GetValueY (int value)
121 if( value < -range*700 ) value = -range*700;
122 if( value > +range*700 ) value = +range*700;
123 if(value > 100*range) value += (appData.zoom - 1)*100*range; else
124 if(value < -100*range) value -= (appData.zoom - 1)*100*range; else
125 value *= appData.zoom;
126 return (nHeightPB / 2) - (int)(value * (nHeightPB - 2*MarginH) / ((1200. + 200.*appData.zoom)*range));
129 // the brush selection is made part of the DrawLine, by passing a style argument
130 // the wrapper for doing the text output makes this back-end
132 DrawAxisSegmentHoriz (int value, Boolean drawValue)
134 int y = GetValueY( range*value*100 );
137 char buf[MSG_SIZ], *b = buf;
139 if( value > 0 ) *b++ = '+';
140 sprintf(b, "%d", range*value);
142 DrawEvalText(buf, strlen(buf), y);
144 // [HGM] counts on DrawEvalText to have select transparent background for dotted line!
145 DrawLine( MarginX, y, MarginX + MarginW, y, PEN_BLACK ); // Y-axis tick marks
146 DrawLine( MarginX + MarginW, y, nWidthPB - MarginW, y, PEN_DOTTED ); // hor grid
149 // The DrawLines again must select their own brush.
150 // the initial brush selection is useless? BkMode needed for dotted line and text
154 int cy = nHeightPB / 2, space = nHeightPB/(6 + appData.zoom);
156 DrawAxisSegmentHoriz( +5, TRUE );
157 DrawAxisSegmentHoriz( +3, space >= 20 );
158 DrawAxisSegmentHoriz( +1, space >= 20 && space*appData.zoom >= 40 );
159 DrawAxisSegmentHoriz( 0, TRUE );
160 DrawAxisSegmentHoriz( -1, space >= 20 && space*appData.zoom >= 40 );
161 DrawAxisSegmentHoriz( -3, space >= 20 );
162 DrawAxisSegmentHoriz( -5, TRUE );
164 DrawLine( MarginX + MarginW, cy, nWidthPB - MarginW, cy, PEN_BLACK ); // x-axis
165 DrawLine( MarginX + MarginW, MarginH, MarginX + MarginW, nHeightPB - MarginH, PEN_BLACK ); // y-axis
170 DrawHistogram (int x, int y, int width, int value, int side)
172 int left, top, right, bottom;
174 if( value > -appData.evalThreshold*range && value < +appData.evalThreshold*range ) return;
177 right = left + width + 1;
180 top = GetValueY( value );
185 bottom = GetValueY( value ) + 1;
189 if( width == MIN_HIST_WIDTH ) {
191 DrawRectangle( left, top, right, bottom, side, FILLED );
194 DrawRectangle( left, top, right, bottom, side, OPEN );
200 DrawSeparator (int index, int x)
203 if( index == currCurrent ) {
204 DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_BLUEDOTTED );
206 else if( (index % 20) == 0 ) {
207 DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_DOTTED );
212 // made back-end by replacing MoveToEx and LineTo by DrawSegment
213 /* Actually draw histogram as a diagram, cause there's too much data */
215 DrawHistogramAsDiagram (int cy, int paint_width, int hist_count)
220 /* Rescale the graph every few moves (as opposed to every move) */
221 hist_count -= hist_count % 8;
225 step = (double) paint_width / (hist_count + 1);
227 for( i=0; i<2; i++ ) {
228 int index = currFirst;
229 int side = (currCurrent + i + 1) & 1; /* Draw current side last */
230 double x = MarginX + MarginW;
232 if( (index & 1) != side ) {
237 DrawSegment( (int) x, cy, NULL, NULL, PEN_NONE );
241 while( index < currLast ) {
244 DrawSeparator( index, (int) x );
246 /* Extend line up to current point */
247 if( currPvInfo[index].depth > 0 ) {
248 DrawSegment((int) x, GetValueY( GetPvScore(index) ), NULL, NULL, (side==0 ? PEN_BOLDWHITE: PEN_BOLDBLACK) );
256 // back-end, delete pen selection
258 DrawHistogramFull (int cy, int hist_width, int hist_count)
262 // SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
264 for( i=0; i<hist_count; i++ ) {
265 int index = currFirst + i;
266 int x = MarginX + MarginW + index * hist_width;
268 /* Draw a separator every 10 moves */
269 DrawSeparator( index, x );
272 if( currPvInfo[i].depth > 0 ) {
273 DrawHistogram( x, cy, hist_width, GetPvScore(index), index & 1 );
287 InitVisualization (VisualizationData *vd)
289 Boolean result = FALSE;
291 vd->cy = nHeightPB / 2;
292 vd->hist_width = MIN_HIST_WIDTH;
293 vd->hist_count = currLast - currFirst;
294 vd->paint_width = nWidthPB - MarginX - 2*MarginW;
296 if( vd->hist_count > 0 ) {
300 vd->hist_width = vd->paint_width / vd->hist_count;
302 if( vd->hist_width > MAX_HIST_WIDTH ) vd->hist_width = MAX_HIST_WIDTH;
304 vd->hist_width -= vd->hist_width % 2;
314 VisualizationData vd;
316 if( InitVisualization( &vd ) ) {
317 if( vd.hist_width < MIN_HIST_WIDTH ) {
318 DrawHistogramAsDiagram( vd.cy, vd.paint_width, vd.hist_count );
321 DrawHistogramFull( vd.cy, vd.hist_width, vd.hist_count );
328 GetMoveIndexFromPoint (int x, int y)
331 int start_x = MarginX + MarginW;
332 VisualizationData vd;
334 if( x >= start_x && InitVisualization( &vd ) ) {
335 /* Almost an hack here... we duplicate some of the paint logic */
336 if( vd.hist_width < MIN_HIST_WIDTH ) {
339 vd.hist_count -= vd.hist_count % 8;
343 step = (double) vd.paint_width / (vd.hist_count + 1);
346 result = (int) (0.5 + (double) (x - start_x) / step);
349 result = (x - start_x) / vd.hist_width;
353 if( result >= currLast ) {
360 // init and display part split of so they can be moved to front end
362 PaintEvalGraph (void)
364 VariantClass v = gameInfo.variant;
365 range = (gameInfo.holdingsWidth && v != VariantSuper && v != VariantGreat && v != VariantSChess) ? 2 : 1; // [HGM] double range in drop games
367 DrawRectangle(0, 0, nWidthPB, nHeightPB, 2, FILLED);