File: GLClient.cpp

package info (click to toggle)
arkrpg 0.1.4b-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,104 kB
  • ctags: 5,445
  • sloc: cpp: 28,145; sh: 9,006; ansic: 3,259; makefile: 344
file content (368 lines) | stat: -rw-r--r-- 9,885 bytes parent folder | download | duplicates (3)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* $Id: GLClient.cpp,v 1.52 2003/03/23 20:09:25 mrq Exp $
** 
** Ark - Libraries, Tools & Programs for MMORPG developpements.
** Copyright (C) 1999-2003 The Contributors of the Ark Project
** Please see the file "AUTHORS" for a list of contributors
**
** 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifdef WIN32
#include <windows.h>
#endif


#include <Ark/ArkTimer.h>
#include <Ark/ArkRenderer.h>
#include <Ark/ArkSystem.h>
#include <Ark/ArkFileSys.h>
#include <Client/GLClient.h>

#include <Client/TerrainView.h>
#include <Client/MenuView.h>
#include <Client/ScrollWidget.h>
#include <Client/SplashWidget.h>

#include <sstream>

#ifdef HAVE_MIXER
#include <SDL_mixer.h>
#endif

namespace Client
{
   GLClient::GLClient (int argc, char **argv) :
      Client (argc, argv),
      m_TerrainWidget (0),
      m_SplashWidget (0),
      m_MenuWidget (0)
   {
      Ark::Config *cfg = Ark::Sys()->Cfg();
      
      m_Width = Ark::Sys()->Cfg()->GetInt ("renderer::Width", 800);
      m_Height = Ark::Sys()->Cfg()->GetInt ("renderer::Height", 600);
      m_NearPlane = Ark::Sys()->Cfg()->GetScalar ("glrenderer::Near", 0.1f);
      m_FarPlane= Ark::Sys()->Cfg()->GetScalar ("glrenderer::Far", 1000.0f);
      
      //-------------------------------------------------------------
      m_Done = false;
  
      if (SDL_Init (SDL_INIT_VIDEO) < 0)
      {
	 Ark::Sys()->Fatal ("Couldn't initialize SDL: %s\n",
			    SDL_GetError ());
      }

      Ark::Sys()->AtExit ((Ark::ExitFunc) &SDL_Quit);
      const SDL_VideoInfo *vi = SDL_GetVideoInfo ();
      int bpp = Ark::Sys()->Cfg()->GetInt ("renderer::BPP",
					   vi->vfmt->BitsPerPixel);

      // Set the flags we want to use for setting the video mode
      int videoflags = SDL_OPENGL;//|SDL_FULLSCREEN;//|SDL_ANYFORMAT;

      // Check for Voodoo (Graphics or 2), which is always fullscreen
      if (getenv("MESA_GLX_FX") && getenv("MESA_GLX_FX")[0] == 'f')
	 videoflags |= SDL_FULLSCREEN;
      else
      {
	 // We should link to getopt/getlongopt, it would be cleaner...
	 // We have to get gettext/getopt for win32 anyway
	 for (int i = 1; argv [i]; ++i )
	 {
	    if (strcmp(argv[i], "--fullscreen") == 0)
	    {
	       videoflags |= SDL_FULLSCREEN;
	       break;
	    }

	    if (strcmp(argv[i], "--no-fullscreen") == 0)
	    {
	       videoflags &= ~SDL_FULLSCREEN;
	       break;
	    }
	 }
      }

      //--------------------------------------------------------------
      SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
      SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);

      if (SDL_SetVideoMode (m_Width, m_Height, bpp, videoflags) == NULL)
      {
	 Ark::Sys()->Fatal ("Couldn't set GL mode: %s", SDL_GetError());
      }

      // Set title
      Ark::String title = cfg->GetStr ("client::Title", "Ark Client");
      SDL_WM_SetCaption (title.c_str(), "arkclient");

      // Display some informations about OpenGL
      Ark::Sys()->Log ("Renderer informations: \n");
      Ark::Sys()->Log ("  Resolution: %dx%dx%d\n", m_Width, m_Height, bpp);
      Ark::Sys()->Log ("  GL_VENDOR: %s\n", glGetString(GL_VENDOR));
      Ark::Sys()->Log ("  GL_VERSION: %s\n", glGetString(GL_VERSION));
      Ark::Sys()->Log ("  GL_RENDERER: %s\n", glGetString(GL_RENDERER));
      Ark::Sys()->Log ("----------\n\n");

      m_Renderer = Ark::RendererFactory::CreateRenderer
	 ("ark::Renderer::OpenGL",0);
      m_UI = new UIRenderer (m_Renderer, 30, m_Width, m_Height);

      gamma_red = cfg->GetScalar ("client::Gamma",  1.0);
      gamma_green = cfg->GetScalar ("client::Gamma",  1.0);
      gamma_blue = cfg->GetScalar ("client::Gamma",  1.0);

      SDL_SetGamma(gamma_red,gamma_green,gamma_blue);
      Ark::Sys()->Log("gamma %f %f %f\n",gamma_red,gamma_green,gamma_blue );

      m_SplashWidget = WidgetPtr(new SplashWidget, 0);
      m_CurWidget = m_SplashWidget;
      m_Updater = NULL;
      Refresh();

#ifdef HAVE_MIXER
      Ark::Sys()->Log ("Audio informations: \n");
      Mix_OpenAudio (44100, AUDIO_S16, 1, 4096);
#endif
   }

   GLClient::~GLClient ()
   {
      if (m_UI)
	 delete m_UI;

#ifdef HAVE_MIXER
      Mix_CloseAudio();
#endif
   }

   bool
   GLClient::Refresh ()
   {
      glDepthRange (m_NearPlane, m_FarPlane);

      glClearDepth (m_FarPlane);
      glClearColor (0.0, 0.0, 0.0, 1.0);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

      m_Renderer->SetViewport (0, 0, m_Width, m_Height);

      m_CurWidget->Render (m_UI);
      SDL_GL_SwapBuffers();

      return true;
   }

   void
   GLClient::HandleMenuString(const Ark::String &str)
   {
      if (str == "playsolo")
      {
	 if (!m_TerrainWidget)
	 {
	    m_CurWidget = m_SplashWidget;

	    Refresh();

	    Login(Ark::Sys()->Cfg()->GetStr("client::Login","player1"), "n");

#ifdef HAVE_MIXER
            Ark::String mus = Ark::Sys()->FS()->GetFileName
	       ("{game}/data/musics/ambient1.mp3", false);
	    Mix_Music *music = Mix_LoadMUS (mus.c_str());
	    Mix_PlayMusic (music, -1);
#endif
	    m_TerrainWidget = WidgetPtr(new TerrainView(m_UI, this), 0);
	 }

	 m_CurWidget = m_TerrainWidget;
      }
      else if (str == "credits")
      {
	 m_CurWidget = WidgetPtr(new ScrollWidget
				 (m_UI, "{game}/data/misc/credits.cfg"), 0);
      }
      else if (str == "tellstory")
      {
	 m_CurWidget = WidgetPtr(new ScrollWidget
				 (m_UI, "{game}/data/misc/tellstory.cfg"), 0);
      }
      else if (str == "quit")
      {
	 m_Done = true;
      }
   }

   bool
   GLClient::HandleEvent (SDL_Event *event)
   {
      switch (event->type)
      {
	 case SDL_ACTIVEEVENT:
	    break;

	 case SDL_KEYDOWN:
	    if (event->key.keysym.sym == SDLK_ESCAPE)
	    {
	       if (m_CurWidget == m_MenuWidget && m_TerrainWidget)
		  m_CurWidget = m_TerrainWidget;
	       else
		  m_CurWidget = m_MenuWidget;
	       return true;
	    }
	    else if (event->key.keysym.sym == SDLK_F10)
	    {
	       gamma_red   *= 1.02f;
	       gamma_green *= 1.02f;
	       gamma_blue  *= 1.02f;
	       SDL_SetGamma(gamma_red,gamma_green,gamma_blue);
	       Ark::Sys()->Log("Gamma INCreased : r=%f g=%f b=%f\n",gamma_red,gamma_green,gamma_blue);
	       return true;
	    }
	    else if (event->key.keysym.sym == SDLK_F11)
	    {
	       gamma_red   /= 1.02f;
	       gamma_green /= 1.02f;
	       gamma_blue  /= 1.02f;
	       SDL_SetGamma(gamma_red,gamma_green,gamma_blue);
	       Ark::Sys()->Log("Gamma DECreased : r=%f g=%f b=%f\n",gamma_red,gamma_green,gamma_blue);
	       return true;
	    }
	    else if (event->key.keysym.sym == SDLK_F12)
	    {
	       Ark::Image img ("screen", m_Width, m_Height,
			       Ark::Image::RGB_888);

	       /* read our image data from the frame buffer */
	       glReadPixels (0, 0,
			     m_Width, m_Height,
			     GL_RGB, GL_UNSIGNED_BYTE, img.m_Data);

	       Ark::String name;
	       int i = 0;

	       do
	       {
		   std::ostringstream os;
		   os << "{home}/screen" << i << ".tga";
		   name = os.str();
		   ++i;
	       } while (Ark::Sys()->FS()->IsFile (name));

	       img.SaveTGA (name);
	    }
	    m_CurWidget->HandleKey (true,
				    event->key.keysym.mod,
				    event->key.keysym.sym);
	    break;

	 case SDL_KEYUP:
	    m_CurWidget->HandleKey (false,
				    event->key.keysym.mod,
				    event->key.keysym.sym);
	    break;

	 case SDL_QUIT:
	    m_Done = true;
	    break;

	 case SDL_MOUSEBUTTONDOWN:
	    m_CurWidget->HandleMButton (true, event->button.button,
                                        event->button.x,
                                        event->button.y);
	    break;

	 case SDL_MOUSEBUTTONUP:
	    m_CurWidget->HandleMButton (false, event->button.button,
					event->button.x,
					event->button.y);
	    break;
      }

      return true;
   }

   bool
   GLClient::ProcessEvents ()
   {
      SDL_Event event;

      int x,y,state = SDL_GetMouseState (&x, &y);
      HandleMotion (state, x, y);

      // Check if there's a pending event.
      while (SDL_PollEvent (&event))
	 HandleEvent(&event);

      return true;
   }

   int
   GLClient::Loop ()
   {
      GLenum gl_error;
      char* sdl_error;
      
      m_MenuWidget = WidgetPtr(new MenuView(m_UI), 0);
      m_CurWidget = m_MenuWidget;

      scalar period = 1.0f/m_MaxFPS;
      scalar delta = 0.0f;
  
      while (!m_Done)
      {
	 Ark::Timer timer;

	 //-----------------------------------------------------------
	 if (m_Updater)
	    m_Updater->Update(delta);
	 Refresh ();
          
	 //-----------------------------------------------------------
	 // Check for SDL/OpenGL error
	 gl_error = glGetError ();
	 if (gl_error != GL_NO_ERROR)
	 {
	    Ark::Sys()->Log ("OpenGL error: %s (%x)\n",
			     gluErrorString(gl_error), gl_error);
	 }
     
	 sdl_error = SDL_GetError ();
	 if (sdl_error[0] != '\0')
	 {
	    Ark::Sys()->Log ("SDL error: %s\n", sdl_error );
	    SDL_ClearError ();
	 }
     
	 //-----------------------------------------------------------
	 ProcessEvents ();
	 delta = timer.GetDelta();


	 //----------------------------------------------------------
	 // sleep until we reach the target FPS.
	 if (0 && delta < period)
	 {
	    printf ("Sleeping a bit: %d msec\n", int((period - delta) * 1000));
	    SDL_Delay ((uint) ((period - delta) * 1000.0));
	 }
      }

      return 0;
   }


}