1 /*
2  * Tux Racer
3  * Copyright (C) 1999-2001 Jasmin F. Patry
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19 
20 #include "tcl_util.h"
21 
22 /* Parse an n-tuple of doubles specified as a tcl-list.
23  * Used for grabbing point or vector coordinates, colours, and other things.
24  * Puts results into an array of doubles.
25  */
get_tcl_tuple(Tcl_Interp * ip,char * inList,scalar_t * p,int n)26 int get_tcl_tuple ( Tcl_Interp *ip, char *inList, scalar_t *p, int n )
27 {
28     char **indices;
29     double tmp;
30     int num_doubles;
31     int rtn;
32     char s[100];
33     int i;
34 
35     rtn = Tcl_SplitList(ip, inList, &num_doubles, &indices);
36 
37     if ((TCL_OK != rtn) || (n != num_doubles)) {
38 	sprintf(s,"%d",n);
39 	Tcl_AppendResult(ip,
40 			 "Expected a tuple of ", s, " doubles.\n",
41 			 (char *) 0
42 	    );
43 	Tcl_Free((char *)indices);
44 	return TCL_ERROR;
45     }
46 
47     for (i = 0; i < n; i++) {
48 	if (TCL_OK != Tcl_GetDouble(ip, indices[i], &tmp)) {
49 	    Tcl_Free((char *)indices);
50 	    sprintf(s,"%d",n);
51 	    Tcl_AppendResult(ip,
52 			     "Expected a tuple of ", s, " doubles.\n",
53 			     (char *) 0
54 		);
55 	    return TCL_ERROR;
56 	}
57 	p[i] = tmp;
58     }
59     Tcl_Free((char *)indices);
60     return TCL_OK;
61 
62 }
63 
64 /* Parse an n-tuple of ints specified as a tcl-list. */
get_tcl_int_tuple(Tcl_Interp * ip,char * inList,int * p,int n)65 int get_tcl_int_tuple( Tcl_Interp *ip, char *inList, int *p, int n )
66 {
67     char **indices;
68     int tmp;
69     int num_ints;
70     int rtn;
71     char s[100];
72     int i;
73 
74     rtn = Tcl_SplitList(ip, inList, &num_ints, &indices);
75 
76     if ((TCL_OK != rtn) || (n != num_ints)) {
77 	sprintf(s,"%d",n);
78 	Tcl_AppendResult(ip,
79 			 "Expected a tuple of ", s, " integers.\n",
80 			 (char *) 0
81 	    );
82 	Tcl_Free((char *)indices);
83 	return TCL_ERROR;
84     }
85 
86     for (i = 0; i < n; i++) {
87 	if (TCL_OK != Tcl_GetInt(ip, indices[i], &tmp)) {
88 	    Tcl_Free((char *)indices);
89 	    sprintf(s,"%d",n);
90 	    Tcl_AppendResult(ip,
91 			     "Expected a tuple of ", s, " integers.\n",
92 			     (char *) 0
93 		);
94 	    return TCL_ERROR;
95 	}
96 	p[i] = tmp;
97     }
98     Tcl_Free((char *)indices);
99     return TCL_OK;
100 }
101 
make_point2d_from_array(scalar_t * p)102 point2d_t make_point2d_from_array ( scalar_t *p )
103 {
104     return make_point2d(p[0],p[1]);
105 }
106 
make_point_from_array(scalar_t * p)107 point_t make_point_from_array ( scalar_t *p )
108 {
109     return make_point(p[0],p[1],p[2]);
110 }
111 
make_vector_from_array(scalar_t * v)112 vector_t make_vector_from_array ( scalar_t *v )
113 {
114     return make_vector(v[0],v[1],v[2]);
115 }
116 
make_colour_from_array(scalar_t * c)117 colour_t make_colour_from_array ( scalar_t *c )
118 {
119     return make_colour(c[0],c[1],c[2],1.0);
120 }
121 
make_colour(scalar_t r,scalar_t g,scalar_t b,scalar_t a)122 colour_t make_colour( scalar_t r, scalar_t g, scalar_t b, scalar_t a)
123 {
124     colour_t result;
125     result.r = r;
126     result.g = g;
127     result.b = b;
128     result.a = a;
129     return result;
130 }
131 
132 
133 /*---------------------------------------------------------------------------*/
134 /*!
135   Sets the Tcl stderr and stdout channels to be the same as the C stderr and
136   stdout streams.
137 
138   \author  jfpatry
139 */
setup_tcl_std_channels()140 void setup_tcl_std_channels()
141 {
142     /* Only need to do this under Win32 */
143 #if defined( NATIVE_WIN32_COMPILER )
144     Tcl_Channel stdout_chnl, stderr_chnl;
145 
146     /* I'm not sure why the _dup is necessary under Windows.
147 
148        See the Tcl_SetStdChannel manpage for more info.
149     */
150 
151     /* Create new stdout channel */
152     Tcl_SetStdChannel( NULL, TCL_STDOUT );
153 
154     stdout_chnl = Tcl_MakeFileChannel(
155 	(ClientData) _get_osfhandle( _dup( _fileno(stdout) ) ),
156 	TCL_WRITABLE );
157 
158     check_assertion( stdout_chnl, "Couldn't create new stdout channel" );
159 
160 
161     /* Create a new stderr channel */
162     Tcl_SetStdChannel( NULL, TCL_STDERR );
163 
164     stderr_chnl = Tcl_MakeFileChannel(
165 	(ClientData) _get_osfhandle( _dup( _fileno(stderr) ) ),
166 	TCL_WRITABLE );
167 
168     check_assertion( stderr_chnl, "Couldn't create new stderr channel" );
169 #endif
170 }
171