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;
65 static void DrawLine( int x1, int y1, int x2, int y2, int penType )
67 DrawSegment( x1, y1, NULL, NULL, PEN_NONE );
68 DrawSegment( x2, y2, NULL, NULL, penType );
72 static void DrawLineEx( int x1, int y1, int x2, int y2, int penType )
75 DrawSegment( x1, y1, &savX, &savY, PEN_NONE );
76 DrawSegment( x2, y2, NULL, NULL, penType );
77 DrawSegment( savX, savY, NULL, NULL, PEN_NONE );
81 static int GetPvScore( int index )
83 int score = currPvInfo[ index ].score;
85 if( index & 1 ) score = -score; /* Flip score for black */
90 char* MakeEvalTitle( char * title)
93 static char buf[MSG_SIZ];
95 if( currCurrent <0 ) return title; // currCurrent = -1 crashed WB on start without ini file!
96 score = currPvInfo[ currCurrent ].score;
97 depth = currPvInfo[ currCurrent ].depth;
99 if( depth <=0 ) return title;
100 if( currCurrent & 1 ) score = -score; /* Flip score for black */
101 snprintf(buf, MSG_SIZ, "%s {%s%.2f/%-2d %d}", title, score>0 ? "+" : " ", score/100., depth, (currPvInfo[currCurrent].time+50)/100);
108 For a centipawn value, this function returns the height of the corresponding
109 histogram, centered on the reference axis.
111 Note: height can be negative!
113 static int GetValueY( int value )
115 if( value < -range*700 ) value = -range*700;
116 if( value > +range*700 ) value = +range*700;
117 if(value > 100*range) value += appData.zoom * 100 - 100*range; else
118 if(value < -100*range) value -= appData.zoom * 100 - 100*range; else
119 value *= appData.zoom;
120 return (nHeightPB / 2) - (int)(value * (nHeightPB - 2*MarginH) / ((1200. + 200.*appData.zoom)*range));
123 // the brush selection is made part of the DrawLine, by passing a style argument
124 // the wrapper for doing the text output makes this back-end
125 static void DrawAxisSegmentHoriz( int value, Boolean drawValue )
127 int y = GetValueY( range*value*100 );
130 char buf[MSG_SIZ], *b = buf;
132 if( value > 0 ) *b++ = '+';
133 sprintf(b, "%d", range*value);
135 DrawEvalText(buf, strlen(buf), y);
137 // [HGM] counts on DrawEvalText to have select transparent background for dotted line!
138 DrawLine( MarginX, y, MarginX + MarginW, y, PEN_BLACK ); // Y-axis tick marks
139 DrawLine( MarginX + MarginW, y, nWidthPB - MarginW, y, PEN_DOTTED ); // hor grid
142 // The DrawLines again must select their own brush.
143 // the initial brush selection is useless? BkMode needed for dotted line and text
144 static void DrawAxis()
146 int cy = nHeightPB / 2, space = nHeightPB/(6 + appData.zoom);
148 DrawAxisSegmentHoriz( +5, TRUE );
149 DrawAxisSegmentHoriz( +3, space >= 20 );
150 DrawAxisSegmentHoriz( +1, space >= 20 && space*appData.zoom >= 40 );
151 DrawAxisSegmentHoriz( 0, TRUE );
152 DrawAxisSegmentHoriz( -1, space >= 20 && space*appData.zoom >= 40 );
153 DrawAxisSegmentHoriz( -3, space >= 20 );
154 DrawAxisSegmentHoriz( -5, TRUE );
156 DrawLine( MarginX + MarginW, cy, nWidthPB - MarginW, cy, PEN_BLACK ); // x-axis
157 DrawLine( MarginX + MarginW, MarginH, MarginX + MarginW, nHeightPB - MarginH, PEN_BLACK ); // y-axis
161 static void DrawHistogram( int x, int y, int width, int value, int side )
163 int left, top, right, bottom;
165 if( value > -appData.evalThreshold*range && value < +appData.evalThreshold*range ) return;
168 right = left + width + 1;
171 top = GetValueY( value );
176 bottom = GetValueY( value ) + 1;
180 if( width == MIN_HIST_WIDTH ) {
182 DrawRectangle( left, top, right, bottom, side, FILLED );
185 DrawRectangle( left, top, right, bottom, side, OPEN );
190 static void DrawSeparator( int index, int x )
193 if( index == currCurrent ) {
194 DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_BLUEDOTTED );
196 else if( (index % 20) == 0 ) {
197 DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_DOTTED );
202 // made back-end by replacing MoveToEx and LineTo by DrawSegment
203 /* Actually draw histogram as a diagram, cause there's too much data */
204 static void DrawHistogramAsDiagram( int cy, int paint_width, int hist_count )
209 /* Rescale the graph every few moves (as opposed to every move) */
210 hist_count -= hist_count % 8;
214 step = (double) paint_width / (hist_count + 1);
216 for( i=0; i<2; i++ ) {
217 int index = currFirst;
218 int side = (currCurrent + i + 1) & 1; /* Draw current side last */
219 double x = MarginX + MarginW;
221 if( (index & 1) != side ) {
226 DrawSegment( (int) x, cy, NULL, NULL, PEN_NONE );
230 while( index < currLast ) {
233 DrawSeparator( index, (int) x );
235 /* Extend line up to current point */
236 if( currPvInfo[index].depth > 0 ) {
237 DrawSegment((int) x, GetValueY( GetPvScore(index) ), NULL, NULL, PEN_BOLD + side );
245 // back-end, delete pen selection
246 static void DrawHistogramFull( int cy, int hist_width, int hist_count )
250 // SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
252 for( i=0; i<hist_count; i++ ) {
253 int index = currFirst + i;
254 int x = MarginX + MarginW + index * hist_width;
256 /* Draw a separator every 10 moves */
257 DrawSeparator( index, x );
260 if( currPvInfo[i].depth > 0 ) {
261 DrawHistogram( x, cy, hist_width, GetPvScore(index), index & 1 );
274 static Boolean InitVisualization( VisualizationData * vd )
276 Boolean result = FALSE;
278 vd->cy = nHeightPB / 2;
279 vd->hist_width = MIN_HIST_WIDTH;
280 vd->hist_count = currLast - currFirst;
281 vd->paint_width = nWidthPB - MarginX - 2*MarginW;
283 if( vd->hist_count > 0 ) {
287 vd->hist_width = vd->paint_width / vd->hist_count;
289 if( vd->hist_width > MAX_HIST_WIDTH ) vd->hist_width = MAX_HIST_WIDTH;
291 vd->hist_width -= vd->hist_width % 2;
298 static void DrawHistograms()
300 VisualizationData vd;
302 if( InitVisualization( &vd ) ) {
303 if( vd.hist_width < MIN_HIST_WIDTH ) {
304 DrawHistogramAsDiagram( vd.cy, vd.paint_width, vd.hist_count );
307 DrawHistogramFull( vd.cy, vd.hist_width, vd.hist_count );
313 int GetMoveIndexFromPoint( int x, int y )
316 int start_x = MarginX + MarginW;
317 VisualizationData vd;
319 if( x >= start_x && InitVisualization( &vd ) ) {
320 /* Almost an hack here... we duplicate some of the paint logic */
321 if( vd.hist_width < MIN_HIST_WIDTH ) {
324 vd.hist_count -= vd.hist_count % 8;
328 step = (double) vd.paint_width / (vd.hist_count + 1);
331 result = (int) (0.5 + (double) (x - start_x) / step);
334 result = (x - start_x) / vd.hist_width;
338 if( result >= currLast ) {
345 // init and display part split of so they can be moved to front end
346 void PaintEvalGraph( void )
348 VariantClass v = gameInfo.variant;
349 range = (gameInfo.holdingsWidth && v != VariantSuper && v != VariantGreat && v != VariantSChess) ? 2 : 1; // [HGM] double range in drop games
351 DrawRectangle(0, 0, nWidthPB, nHeightPB, 2, FILLED);