File: main.cpp

package info (click to toggle)
qonk 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,076 kB
  • ctags: 2,026
  • sloc: cpp: 14,978; sh: 3,464; makefile: 200
file content (286 lines) | stat: -rw-r--r-- 5,495 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
// Copyright 2005 by Anthony Liekens anthony@liekens.net

#ifdef HAVE_CONFIG_H
# include "config.h"
#else
#define VERSION no-version
#endif

#include <sstream>
#include <string>

#include "sdl_driver.h"
#include "settings.h"

#include "canvas.h"

#include "game.h"
#include "main.h"

#include "menumanager.h"
#include "videooptions.h"
#include "gameoptions.h"

using namespace std;

Main::Main()
  : game(0), running(true), lastTicks(0), fpsCounter(0), fps(0)
{
  driver = new SDLDriver(*this);
  menuManager = new MenuManager(*this);
}

Main::~Main()
{
  delete menuManager;
  delete driver;
}

void
Main::startSinglePlayerGame(int planets, int players)
{
  GameOptions &gameOptions = menuManager->getGameOptions();

  if( planets <= 1 )
  {
    cerr << "Resetting to least of 2 planets" << endl;
    planets = 2;
  }

  if( players > planets - 1 )
  {
    cerr << "Resetting computer players to maximum of " << planets - 1 << endl;
    players = planets - 1;
  }

  if( players < 1 )
  {
    cerr << "Resetting computer players to at least 1" << endl;
    players = 1;
  }

  gameOptions.setPlanets(planets);
  gameOptions.setPlayers(players);

  startSinglePlayerGame(gameOptions);
}

void
Main::startSinglePlayerGame(GameOptions &gameOptions)
{
  if (game)
  {
    delete game;
    game = 0;
  }

  menuManager->hide();

  game = new Game(gameOptions);
  game->setPaused(false);
}

void
Main::quit()
{
  running = false;
}

void
Main::showMenu()
{
  if (menuManager->isVisible())
    return;

  menuManager->show();

  // Just in case there is a game, pause it.
  if (game)
    game->setPaused(true);

}

void
Main::applyVideoOptions(VideoOptions &videoOptions)
{
      // Causes the game to pause which is nice when the CRT/TFT is resyncing.
      showMenu();

      Settings::setFullscreen(videoOptions.getFullscreen());
      Settings::setScreenSize(videoOptions.getScreenWidth(), videoOptions.getScreenHeight());

      Canvas::initScreen();

      menuManager->resize();
}

void
Main::handle(GameAction gameAction, int value)
{
  switch (gameAction)
  {
    case GA_SENSE_CANCEL:
      if (!value)
        menuManager->senseFinished(false);
      break;
    case GA_SENSE_COMPLETE:
      if (!value)
        menuManager->senseFinished(true);
      break;
    case GA_TOGGLE_FULLSCREEN:
      if (value) return;
      {
        VideoOptions &vo = menuManager->getVideoOptions();
        vo.toggleFullscreen();
        applyVideoOptions(vo);
      }
      break;
    case GA_LEAVE:
      // It is important to act on keyrelease since this is the same
      // for what guichan reacts. By doing so it is predictable that
      // after handling GA_LEAVE (which may make the menusystem visible)
      // guichan's logic is called.
      if (value)
        return;

      // Main does not care about making the menu system invisible. This is done
      // from inside the menu system.
      if (!menuManager->isVisible())
        menuManager->show();

      break;
    case GA_SCREENSHOT:
      if (value)
        return;

      SDL_SaveBMP( SDL_GetVideoSurface(), "screenshot.bmp" );
      break;
    default:
      // Let the game instance handle the rest
      if (game)
        game->handle(gameAction, value);
      break;
  }
}

void
Main::renderFPS()
{
	stringstream s;
  
	fpsCounter++;

	s << fps;
	Canvas::drawText( Settings::getScreenWidth() - 32, 5, s.str().c_str(), 0x80, 0x80, 0x80 );

	if( SDL_GetTicks() - lastTicks >= 1000 ) {
		lastTicks = SDL_GetTicks();
    fps = fpsCounter;
		fpsCounter = 0;
	}
  
}

void
Main::run()
{
  bool b;

  // Main loop: loop forever.
	while (running) {

    Canvas::drawBox(0, 0, Settings::getScreenWidth(), Settings::getScreenHeight(), 0, 0, 0);

    if (game)
      game->render();
    menuManager->render();
    renderFPS();
    Canvas::updateScreen();
    
    b = menuManager->update();

    if (game)
    {
      if (!game->run(b))
      {
        delete game;
        game = 0;
        menuManager->showSinglePlayerMenu();
      }
    }

		// Poll for events, and handle the ones we care about.
    driver->loop();
	}

}

int main(int argc, char** argv) {
	int numberOfPlanets = -1;
	int numberOfComputerPlayers = -1;

  cout << "This is " << PACKAGE_STRING << endl;
  cout << "(c) 2003 Anthony Liekens" << endl;
  cout << "(c) 2007 Qonk development team" << endl;
  cout << "This game is free software released under the GNU General Public License, " << endl;
  cout << "either version 2 of the license or, at your option, any later version. " << endl;

	Settings::init();

	switch (argc) {
		case 3:
			numberOfPlanets = atoi( argv[ 1 ] );
			numberOfComputerPlayers = atoi( argv[ 2 ] );
			break;
    case 1:
      // Do nothing.
      break;
		default:
			cerr << "Usage: " << argv[ 0 ] << " numberOfPlanets numberOfComputerPlayers" << endl;
			exit(1);
			break;
	}

  Main *main = new Main();

  if (numberOfPlanets == -1
      && numberOfComputerPlayers == -1)
    main->showMenu();
  else
    main->startSinglePlayerGame(numberOfPlanets, numberOfComputerPlayers);

  main->run();
  
  delete main;

  Settings::store();

  return 0;
}

void
Main::startSensing()
{
  driver->setSenseMode(true);
}

void
Main::finishSensing(GameAction ga, bool completed)
{
  if (completed)
  {
    Settings::set(ga, driver->getSensedInput());
    driver->initActionMap();
  }

  driver->setSenseMode(false);
}

void
Main::resetGameAction(GameAction ga)
{
   Settings::unset(ga);
   driver->setSenseMode(true);
   driver->setSenseMode(false);
}