1 /* $Id: HitMark.cpp,v 1.11 2002/01/19 13:17:18 nan Exp $ */
2 
3 // Copyright (C) 2000, 2002  $B?@Fn(B $B5H9((B(Kanna Yoshihiro)
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (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 #include "ttinc.h"
20 #include "HitMark.h"
21 #include "LoadImage.h"
22 #include "RCFile.h"
23 
24 extern RCFile *theRC;
25 
26 GLuint HitMark::m_textures[2] = {0, 0};
27 
HitMark()28 HitMark::HitMark() {
29   m_x = m_y = m_z = m_vx = m_vy = m_vz = 0.0;
30   m_swingError = SWING_PERFECT;
31 }
32 
~HitMark()33 HitMark::~HitMark() {
34 }
35 
36 bool
Init()37 HitMark::Init() {
38   ImageData errorImage[2];
39   int i, j;
40 
41   if ( m_textures[0] == 0 ) {
42     errorImage[0].LoadPPM( "images/Nice.ppm" );
43     errorImage[1].LoadPPM( "images/Bad.ppm" );
44 
45     for ( i = 0 ; i < errorImage[0].GetWidth() ; i++ ) {
46       for ( j = 0 ; j < errorImage[0].GetHeight() ; j++ ) {
47 	errorImage[0].SetPixel( i, j, 1, 0 );
48 	errorImage[0].SetPixel( i, j, 2, 0 );
49 	if ( errorImage[0].GetPixel( i, j, 0 ) >= 5 )
50 	  errorImage[0].SetPixel( i, j, 3, 255 );
51 	else
52 	  errorImage[0].SetPixel( i, j, 3, 0 );
53 	errorImage[1].SetPixel( i, j, 0, 0 );
54 	errorImage[1].SetPixel( i, j, 1, 0 );
55 	if ( errorImage[1].GetPixel( i, j, 2 ) >= 5 )
56 	  errorImage[1].SetPixel( i, j, 3, 255 );
57 	else
58 	  errorImage[1].SetPixel( i, j, 3, 0 );
59       }
60     }
61 
62     glGenTextures( 2, m_textures );
63     for ( i = 0 ; i < 2 ; i++ ) {
64       glBindTexture(GL_TEXTURE_2D, m_textures[i] );
65       glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
66       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
67       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
68       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
69       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
70       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
71 
72       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
73 		   errorImage[i].GetWidth(), errorImage[i].GetHeight(),
74 		   0, GL_RGBA, GL_UNSIGNED_BYTE, errorImage[i].GetImage() );
75     }
76   }
77 
78   return true;
79 }
80 
81 bool
Hit(double x,double y,double z,double vx,double vy,double vz,long swingError)82 HitMark::Hit( double x, double y, double z, double vx, double vy, double vz,
83 	       long swingError ) {
84   m_x = x;
85   m_y = y;
86   m_z = z;
87   m_vx = vx;
88   m_vy = vy;
89   m_vz = vz;
90   m_swingError = swingError;
91 
92 #ifndef WIN32
93   struct timeval tv;
94   struct timezone tz;
95 #endif
96 
97 #ifdef WIN32
98   ftime( &startTime );
99 #else
100   gettimeofday( &tv, &tz );
101   startTime.time = tv.tv_sec;
102   startTime.millitm = tv.tv_usec/1000;
103 #endif
104 
105   return true;
106 }
107 
108 bool
Redraw()109 HitMark::Redraw() {
110   struct timeb now;
111 
112 #ifndef WIN32
113   struct timeval tv;
114   struct timezone tz;
115 #endif
116 
117 #ifdef WIN32
118   ftime( &now );
119 #else
120   gettimeofday( &tv, &tz );
121   now.time = tv.tv_sec;
122   now.millitm = tv.tv_usec/1000;
123 #endif
124 
125   m_time = (now.time-startTime.time)*100+(now.millitm-startTime.millitm)/10;
126 
127   if ( m_time > 50 )
128     return false;
129 
130   if ( m_time < 20 )
131     m_time += m_time/2;
132   else if ( m_time < 30 )
133     m_time = 20+(30-m_time);
134   else
135     m_time = 20;
136 
137   return true;
138 }
139 
140 bool
RedrawAlpha()141 HitMark::RedrawAlpha() {
142   if ( !theRC->isTexture )
143     return true;
144 
145   glDisable(GL_DEPTH_TEST);
146   glDepthMask(0);
147 
148   glPushMatrix();
149   glTranslatef( (float)m_x, (float)m_y, TABLEHEIGHT+0.5F );
150 
151   switch ( m_swingError ) {
152   case SWING_PERFECT:
153     glEnable(GL_TEXTURE_2D);
154     glColor3f( 0.0F, 0.0F, 0.0F );
155     glBindTexture(GL_TEXTURE_2D, m_textures[0] );
156     glBegin(GL_QUADS);
157     glTexCoord2f(0.0F, 1.0F); glVertex3f( -0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
158     glTexCoord2f(0.0F, 0.0F); glVertex3f( -0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
159     glTexCoord2f(1.0F, 0.0F); glVertex3f(  0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
160     glTexCoord2f(1.0F, 1.0F); glVertex3f(  0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
161     glEnd();
162     glDisable(GL_TEXTURE_2D);
163     break;
164   case SWING_GREAT:
165     glEnable(GL_TEXTURE_2D);
166     glColor3f( 0.0F, 0.0F, 0.0F );
167     glBindTexture(GL_TEXTURE_2D, m_textures[0] );
168     glBegin(GL_QUADS);
169     glTexCoord2f(0.0F, 1.0F); glVertex3f( -0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
170     glTexCoord2f(0.0F, 0.0F); glVertex3f( -0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
171     glTexCoord2f(1.0F, 0.0F); glVertex3f(  0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
172     glTexCoord2f(1.0F, 1.0F); glVertex3f(  0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
173     glEnd();
174     glDisable(GL_TEXTURE_2D);
175     break;
176   case SWING_GOOD:
177     glEnable(GL_TEXTURE_2D);
178     glColor3f( 0.0, 0.0, 0.0 );
179     glBindTexture(GL_TEXTURE_2D, m_textures[1] );
180     glBegin(GL_QUADS);
181     glTexCoord2f(0.0F, 1.0F); glVertex3f( -0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
182     glTexCoord2f(0.0F, 0.0F); glVertex3f( -0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
183     glTexCoord2f(1.0F, 0.0F); glVertex3f(  0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
184     glTexCoord2f(1.0F, 1.0F); glVertex3f(  0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
185     glEnd();
186     glDisable(GL_TEXTURE_2D);
187     break;
188   case SWING_BOO:
189     glEnable(GL_TEXTURE_2D);
190     glColor3f( 0.0F, 0.0F, 0.0F );
191     glBindTexture(GL_TEXTURE_2D, m_textures[1] );
192     glBegin(GL_QUADS);
193     glTexCoord2f(0.0F, 1.0F); glVertex3f( -0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
194     glTexCoord2f(0.0F, 0.0F); glVertex3f( -0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
195     glTexCoord2f(1.0F, 0.0F); glVertex3f(  0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
196     glTexCoord2f(1.0F, 1.0F); glVertex3f(  0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
197     glEnd();
198     glDisable(GL_TEXTURE_2D);
199     break;
200   case SWING_MISS:
201 /*
202     glEnable(GL_TEXTURE_2D);
203     glColor3f( 0.0F, 0.0F, 0.0F );
204     glBindTexture(GL_TEXTURE_2D, m_textures[0] );
205     glBegin(GL_QUADS);
206     glTexCoord2f(0.0F, 1.0F); glVertex3f( -0.3F*time/50, 0.0F, -0.42F*time/50);
207     glTexCoord2f(0.0F, 0.0F); glVertex3f( -0.3F*time/50, 0.0F,  0.42F*time/50);
208     glTexCoord2f(1.0F, 0.0F); glVertex3f(  0.3F*time/50, 0.0F,  0.42F*time/50);
209     glTexCoord2f(1.0F, 1.0F); glVertex3f(  0.3F*time/50, 0.0F, -0.42F*time/50);
210     glEnd();
211     glDisable(GL_TEXTURE_2D);
212 */
213     break;
214   }
215 
216   glPopMatrix();
217 
218   glDepthMask(1);
219   if ( theRC->gmode != GMODE_SIMPLE )
220     glEnable(GL_DEPTH_TEST);
221 
222   return true;
223 }
224