File: main.cpp

package info (click to toggle)
xevil 2.02r2-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,888 kB
  • sloc: cpp: 47,789; makefile: 189; csh: 4
file content (128 lines) | stat: -rw-r--r-- 3,275 bytes parent folder | download | duplicates (6)
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
/* 
 * XEvil(TM) Copyright (C) 1994,2000 Steve Hardt and Michael Judge
 * http://www.xevil.com
 * satan@xevil.com
 *
 * 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, the file "gpl.txt"; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA, or visit http://www.gnu.org.
 */

// "main.cpp"

// Include Files
#include "utils.h"

extern "C" {
#include <stdlib.h>
#include <X11/Xutil.h>

#ifdef OPENWOUND_XOS_STRLEN_HACK
#define _strings_h
#endif
#include <X11/Xos.h>
}

#include <iostream>

#include "utils.h"
#include "neth.h"
#include "game.h"

GameP g_game;

class TurnStarter: public ITurnStarter {
public:
  TurnStarter(struct timeval *timer) 
    : m_timer(timer) {
  }
  /* EFFECTS: Set timer to be the start time for the turn when 
     startTurn() is called. */
  
  virtual void start_turn() {
    if (gettimeofday(m_timer,NULL) != 0) {
      cerr << "Error with gettimeofday()." << endl;
    }
  }


private:
  struct timeval *m_timer;
};


// main's job is very simple.
// Create the Game, then run the outer loop, one iteration per turn.
// Empty the event queue and feed it to Game every turn.
int main(int argc, char **argv) {
  Utils::seed_random();

  GameP game = new Game(&argc,argv);
	g_game = game;

  //  assert (clock() != -1);

  long total = 0;
  long events = 0;

  // TurnStarter exists so Game::yield() can decide when to start timing 
  // the next turn.
  struct timeval startTime;
  TurnStarter turnStarter(&startTime);
  // Start the first turn.
  turnStarter.start_turn();

  while (True) {
    Quanta quanta = game->get_quanta(); 
    
    // Clock the game
    total++;
    
    game->pre_clock();

    if (game->has_ui()) {
      for (int dpyNum = 0; dpyNum < game->get_dpy_max(); dpyNum++) {
        int eventsNum;
        if (eventsNum = 
            XEventsQueued(game->get_dpy(dpyNum),QueuedAfterReading))
          for (int m = 0; m < eventsNum; m++) {
            XEvent event;
            XNextEvent(game->get_dpy(dpyNum),&event);
            game->process_event(dpyNum,&event);
            events++;
          }
      }
    }
    
    game->post_clock();
    if (game->show_stats() && !(total % Game::REPORT_TIME))
      cout << "total:" << total << " events:" << events << 
	  " percent:" << ((float)events / (float) total) << endl; 
    
    // This should be the only place exit() is called.
    // (But it isn't, ui.cpp calls if license agreement is rejected 
    //  and game.cpp calls if "xevil -info".)
    if (game->quit_game()) {
      delete game;
      exit(1);
    }
    
    // Let game decide how to throw time away.
    game->yield(startTime,quanta,&turnStarter);
  }

  // Never gets here.
  return 0;
}