File: BaseView2D.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 (325 lines) | stat: -rw-r--r-- 8,036 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* $Id: BaseView2D.cpp,v 1.15 2002/11/04 14:02:52 nan Exp $ */

// Copyright (C) 2001, 2002  $B?@Fn(B $B5H9((B(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 "BaseView.h"
#include "BaseView2D.h"
#include "Player.h"
#include "Control.h"
#include "Ball.h"
#include "LoadImage.h"
#include "PlayGame.h"
#include "RCFile.h"

extern long mode;

extern Ball theBall;

extern RCFile *theRC;

// x --- x axis is the bottom line of the net. The plane x=0 represents
//       the vertical plain which includes center line. 
// y --- y axis is the center line. The plane y=0 includes the net. 
// z --- z axis is the vertical line which includes the center point of
//       the table. The plane z=0 is the floor. 

BaseView2D::BaseView2D() {
}

BaseView2D::~BaseView2D() {
}

bool
BaseView2D::Init() {
// Create and initialize Window

  if ( theRC->fullScreen )
    m_baseSurface = SDL_SetVideoMode( m_winWidth, m_winHeight, 0,
				      SDL_HWSURFACE|SDL_FULLSCREEN );
  else
    m_baseSurface = SDL_SetVideoMode( m_winWidth, m_winHeight, 0,
				      SDL_HWSURFACE );

  SDL_WM_SetCaption( "CannonSmash", NULL );

  m_fieldView = (FieldView *)View::CreateView( VIEW_FIELD );
  m_fieldView->Init();

  m_updateX1 = 0;
  m_updateY1 = 0;
  m_updateX2 = BaseView::GetWinWidth()-1;
  m_updateY2 = BaseView::GetWinHeight()-1;

  m_fieldView->Redraw();
  m_fieldView->RedrawAlpha();
  SDL_UpdateRect(m_baseSurface, 0, 0, 0, 0);

  return true;
}

void
BaseView2D::DisplayFunc() {
  BaseView::TheView()->RedrawAll();
}

bool
BaseView2D::RedrawAll() {
  SDL_Rect rect;

  SetViewPosition();

  m_fieldView->GetDamageRect();
  if ( Control::TheControl()->GetThePlayer() )
    Control::TheControl()->GetThePlayer()->GetView()->GetDamageRect();
  if ( Control::TheControl()->GetComPlayer() )
    Control::TheControl()->GetComPlayer()->GetView()->GetDamageRect();
  theBall.GetView()->GetDamageRect();
  if ( Control::TheControl()->GetView() )
    Control::TheControl()->GetView()->GetDamageRect();

  rect.x = (short)m_updateX1;
  rect.y = (short)m_updateY1;
  rect.w = (short)(m_updateX2-m_updateX1+1);
  rect.h = (short)(m_updateY2-m_updateY1+1);

  SDL_SetClipRect( m_baseSurface, &rect );

  m_fieldView->Redraw();

  if ( Control::TheControl()->GetComPlayer() )
    Control::TheControl()->GetComPlayer()->GetView()->Redraw();

  SDL_SetClipRect( m_baseSurface, &rect );
  m_fieldView->RedrawAlpha();

  SDL_SetClipRect( m_baseSurface, NULL );

  if ( Control::TheControl()->GetThePlayer() )
    Control::TheControl()->GetThePlayer()->GetView()->RedrawAlpha();
  theBall.GetView()->Redraw();

  if ( Control::TheControl()->GetView() ) {
    Control::TheControl()->GetView()->Redraw();
    Control::TheControl()->GetView()->RedrawAlpha();
  }

  // $B:FIA2hNN0h$N$_IA2h$9$k(B
  SDL_UpdateRects(m_baseSurface, 1, &rect);
  m_updateX1 =m_updateY1 = m_updateX2 =m_updateY2 = 0;

  return true;
}

bool
BaseView2D::SetViewPosition() {
  SetLookAt();

  return true;
}

// Move the viewpoint as the player moves
void
BaseView2D::SetLookAt() {
  // later we have to do something
}
      
void
BaseView2D::EndGame() {
  // $B8e$G<BAu(B
#if 0
  static char file[][30] = {"images/win.ppm", "images/lose.ppm"};
  GLubyte bmp[400*70/8];
#ifndef HAVE_LIBZ
  FILE *fp;
#else
  gzFile fp;
#endif
  int i, j;

  glColor4f(1.0F, 1.0F, 1.0F, 0.0F);

  glPushMatrix();
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  gluOrtho2D( 0.0F, (GLfloat)m_winWidth, 0.0F, (GLfloat)m_winHeight );
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  printf( "EndGame %d : %d\n",
	  ((PlayGame *)Control::TheControl())->GetScore(Control::TheControl()->GetThePlayer()), 
	  ((PlayGame *)Control::TheControl())->GetScore(Control::TheControl()->GetComPlayer()) );

  if ( Control::TheControl()->IsPlaying() && 
       ((PlayGame *)Control::TheControl())->GetScore(Control::TheControl()->GetThePlayer()) >
       ((PlayGame *)Control::TheControl())->GetScore(Control::TheControl()->GetComPlayer()) ) {
#ifndef HAVE_LIBZ
    if ( (fp = fopen(&file[0][0], "r")) == NULL )
      return;
#else
    if ( (fp = gzopenx(&file[0][0], "rs")) == NULL )
      return;
#endif
  }  else {
#ifndef HAVE_LIBZ
    if ( (fp = fopen(&file[1][0], "r")) == NULL )
      return;
#else
    if ( (fp = gzopenx(&file[1][0], "rs")) == NULL )
      return;
#endif
  }

  for ( i = 69 ; i >= 0 ; i-- ) {
    for ( j = 0 ; j < 400/8 ; j++ ) {
      bmp[i*50+j] = (unsigned char)strtol( getWord(fp), NULL, 16 );
    }
  }

#ifndef HAVE_LIBZ
  fclose(fp);
#else
  gzclose(fp);
#endif

  glRasterPos2i( 220, 330 );
  glBitmap( 400, 70, 0.0F, 0.0F, 0.0F, 0, bmp );

  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();

  SDL_GL_SwapBuffers();

#ifdef WIN32
  Sleep(3000);
#else
  sleep(3);
#endif
#endif
}

void
BaseView2D::QuitGame() {
  SDL_FreeSurface( m_baseSurface );
  delete m_fieldView;
}

bool
BaseView2D::AddUpdateRect( SDL_Rect *r ) {
  if ( !r ) {
    m_updateX1 = m_updateY1 = 0;
    m_updateX2 = BaseView::GetWinWidth()-1;
    m_updateY2 = BaseView::GetWinHeight()-1;

    return true;
  }

  if ( m_updateX2 == 0 && m_updateY2 == 0 ) {
    m_updateX1 = r->x;
    m_updateY1 = r->y;
    m_updateX2 = r->x + r->w - 1;
    m_updateY2 = r->y + r->h - 1;
  } else {
    if ( r->x < m_updateX1 )
      m_updateX1 = r->x;
    if ( r->y < m_updateY1 )
      m_updateY1 = r->y;
    if ( r->x+r->w-1 > m_updateX2 )
      m_updateX2 = r->x+r->w-1;
    if ( r->y+r->h-1 > m_updateY2 )
      m_updateY2 = r->y+r->h-1;
  }

  if ( m_updateX1 < 0 )
    m_updateX1 = 0;
  if ( m_updateX2 < 0 )
    m_updateX2 = 0;
  if ( m_updateX1 > BaseView::GetWinWidth() )
    m_updateX1 = BaseView::GetWinWidth();
  if ( m_updateX2 > BaseView::GetWinWidth() )
    m_updateX2 = BaseView::GetWinWidth();
  if ( m_updateY1 < 0 )
    m_updateY1 = 0;
  if ( m_updateY2 < 0 )
    m_updateY2 = 0;
  if ( m_updateY1 > BaseView::GetWinHeight() )
    m_updateY1 = BaseView::GetWinHeight();
  if ( m_updateY2 > BaseView::GetWinHeight() )
    m_updateY2 = BaseView::GetWinHeight();

  return true;
}

// For rendering
bool
RenderRect( double x1, double y1, double z1, 
	    double x2, double y2, double z2, 
	    SDL_Rect *rect ) {
  int _x1, _y1, _x2, _y2;

  RenderPoint( x1, y1, z1, &_x1, &_y1 );
  RenderPoint( x2, y2, z2, &_x2, &_y2 );

  if ( _x1 < _x2 ) {
    rect->x = _x1;
    rect->w = _x2-_x1;
  } else {
    rect->x = _x2;
    rect->w = _x1-_x2;
  }

  if ( _y1 < _y2 ) {
    rect->y = _y1;
    rect->h = _y2-_y1;
  } else {
    rect->y = _y2;
    rect->h = _y1-_y2;
  }

  if ( rect->x > BaseView::GetWinWidth() )
    rect->x = (short)BaseView::GetWinWidth();
  if ( rect->x+rect->w > BaseView::GetWinWidth() )
    rect->w = BaseView::GetWinWidth()-rect->x;
  if ( rect->y > BaseView::GetWinHeight() )
    rect->y = (short)BaseView::GetWinHeight();
  if ( rect->y+rect->h > BaseView::GetWinHeight() )
    rect->h = BaseView::GetWinHeight()-rect->y;

  return true;
}

bool
RenderPoint( double x, double y, double z, 
	     int *_x, int *_y ) {
  double __y, __z;

  y += 4.0; z -= 2.0;

  __y = y*1.7320508/2 - z*0.5;
  __z = y*0.5 + z*1.7320508/2;

  y = __y; z = __z;

  *_x = (int)(x/y*0.87*BaseView::GetWinWidth()+BaseView::GetWinWidth()/2);
  *_y = (int)(-z/y*0.87*BaseView::GetWinHeight()+BaseView::GetWinHeight()/2);

  return true;
}