File: HitMark.cpp

package info (click to toggle)
csmash 0.6.6-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,548 kB
  • ctags: 1,687
  • sloc: cpp: 19,531; sh: 3,515; makefile: 439; ansic: 120; sed: 16
file content (223 lines) | stat: -rw-r--r-- 6,600 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* $Id: HitMark.cpp,v 1.11 2002/01/19 13:17:18 nan Exp $ */

// Copyright (C) 2000, 2002  神南 吉宏(Kanna Yoshihiro)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include "ttinc.h"
#include "HitMark.h"
#include "LoadImage.h"
#include "RCFile.h"

extern RCFile *theRC;

GLuint HitMark::m_textures[2] = {0, 0};

HitMark::HitMark() {
  m_x = m_y = m_z = m_vx = m_vy = m_vz = 0.0;
  m_swingError = SWING_PERFECT;
}

HitMark::~HitMark() {
}

bool
HitMark::Init() {
  ImageData errorImage[2];
  int i, j;

  if ( m_textures[0] == 0 ) {
    errorImage[0].LoadPPM( "images/Nice.ppm" );
    errorImage[1].LoadPPM( "images/Bad.ppm" );

    for ( i = 0 ; i < errorImage[0].GetWidth() ; i++ ) {
      for ( j = 0 ; j < errorImage[0].GetHeight() ; j++ ) {
	errorImage[0].SetPixel( i, j, 1, 0 );
	errorImage[0].SetPixel( i, j, 2, 0 );
	if ( errorImage[0].GetPixel( i, j, 0 ) >= 5 )
	  errorImage[0].SetPixel( i, j, 3, 255 );
	else
	  errorImage[0].SetPixel( i, j, 3, 0 );
	errorImage[1].SetPixel( i, j, 0, 0 );
	errorImage[1].SetPixel( i, j, 1, 0 );
	if ( errorImage[1].GetPixel( i, j, 2 ) >= 5 )
	  errorImage[1].SetPixel( i, j, 3, 255 );
	else
	  errorImage[1].SetPixel( i, j, 3, 0 );
      }
    }

    glGenTextures( 2, m_textures );
    for ( i = 0 ; i < 2 ; i++ ) {
      glBindTexture(GL_TEXTURE_2D, m_textures[i] );
      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
		   errorImage[i].GetWidth(), errorImage[i].GetHeight(),
		   0, GL_RGBA, GL_UNSIGNED_BYTE, errorImage[i].GetImage() );
    }
  }

  return true;
}

bool
HitMark::Hit( double x, double y, double z, double vx, double vy, double vz,
	       long swingError ) {
  m_x = x;
  m_y = y;
  m_z = z;
  m_vx = vx;
  m_vy = vy;
  m_vz = vz;
  m_swingError = swingError;

#ifndef WIN32
  struct timeval tv;
  struct timezone tz;
#endif

#ifdef WIN32
  ftime( &startTime );
#else
  gettimeofday( &tv, &tz );
  startTime.time = tv.tv_sec;
  startTime.millitm = tv.tv_usec/1000;
#endif

  return true;
}

bool
HitMark::Redraw() {
  struct timeb now;

#ifndef WIN32
  struct timeval tv;
  struct timezone tz;
#endif

#ifdef WIN32
  ftime( &now );
#else
  gettimeofday( &tv, &tz );
  now.time = tv.tv_sec;
  now.millitm = tv.tv_usec/1000;
#endif

  m_time = (now.time-startTime.time)*100+(now.millitm-startTime.millitm)/10;

  if ( m_time > 50 )
    return false;

  if ( m_time < 20 )
    m_time += m_time/2;
  else if ( m_time < 30 )
    m_time = 20+(30-m_time);
  else
    m_time = 20;

  return true;
}

bool
HitMark::RedrawAlpha() {
  if ( !theRC->isTexture )
    return true;

  glDisable(GL_DEPTH_TEST);
  glDepthMask(0);
  
  glPushMatrix();
  glTranslatef( (float)m_x, (float)m_y, TABLEHEIGHT+0.5F );

  switch ( m_swingError ) {
  case SWING_PERFECT:
    glEnable(GL_TEXTURE_2D);
    glColor3f( 0.0F, 0.0F, 0.0F );
    glBindTexture(GL_TEXTURE_2D, m_textures[0] );
    glBegin(GL_QUADS);
    glTexCoord2f(0.0F, 1.0F); glVertex3f( -0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
    glTexCoord2f(0.0F, 0.0F); glVertex3f( -0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
    glTexCoord2f(1.0F, 0.0F); glVertex3f(  0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
    glTexCoord2f(1.0F, 1.0F); glVertex3f(  0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    break;
  case SWING_GREAT:
    glEnable(GL_TEXTURE_2D);
    glColor3f( 0.0F, 0.0F, 0.0F );
    glBindTexture(GL_TEXTURE_2D, m_textures[0] );
    glBegin(GL_QUADS);
    glTexCoord2f(0.0F, 1.0F); glVertex3f( -0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
    glTexCoord2f(0.0F, 0.0F); glVertex3f( -0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
    glTexCoord2f(1.0F, 0.0F); glVertex3f(  0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
    glTexCoord2f(1.0F, 1.0F); glVertex3f(  0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    break;
  case SWING_GOOD:
    glEnable(GL_TEXTURE_2D);
    glColor3f( 0.0, 0.0, 0.0 );
    glBindTexture(GL_TEXTURE_2D, m_textures[1] );
    glBegin(GL_QUADS);
    glTexCoord2f(0.0F, 1.0F); glVertex3f( -0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
    glTexCoord2f(0.0F, 0.0F); glVertex3f( -0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
    glTexCoord2f(1.0F, 0.0F); glVertex3f(  0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
    glTexCoord2f(1.0F, 1.0F); glVertex3f(  0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    break;
  case SWING_BOO:
    glEnable(GL_TEXTURE_2D);
    glColor3f( 0.0F, 0.0F, 0.0F );
    glBindTexture(GL_TEXTURE_2D, m_textures[1] );
    glBegin(GL_QUADS);
    glTexCoord2f(0.0F, 1.0F); glVertex3f( -0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
    glTexCoord2f(0.0F, 0.0F); glVertex3f( -0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
    glTexCoord2f(1.0F, 0.0F); glVertex3f(  0.3F*m_time/50, 0.0F,  0.42F*m_time/50);
    glTexCoord2f(1.0F, 1.0F); glVertex3f(  0.3F*m_time/50, 0.0F, -0.42F*m_time/50);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    break;
  case SWING_MISS:
/*
    glEnable(GL_TEXTURE_2D);
    glColor3f( 0.0F, 0.0F, 0.0F );
    glBindTexture(GL_TEXTURE_2D, m_textures[0] );
    glBegin(GL_QUADS);
    glTexCoord2f(0.0F, 1.0F); glVertex3f( -0.3F*time/50, 0.0F, -0.42F*time/50);
    glTexCoord2f(0.0F, 0.0F); glVertex3f( -0.3F*time/50, 0.0F,  0.42F*time/50);
    glTexCoord2f(1.0F, 0.0F); glVertex3f(  0.3F*time/50, 0.0F,  0.42F*time/50);
    glTexCoord2f(1.0F, 1.0F); glVertex3f(  0.3F*time/50, 0.0F, -0.42F*time/50);
    glEnd();
    glDisable(GL_TEXTURE_2D);
*/
    break;
  }

  glPopMatrix();

  glDepthMask(1);
  if ( theRC->gmode != GMODE_SIMPLE )
    glEnable(GL_DEPTH_TEST);

  return true;
}