File: P47Boot.d

package info (click to toggle)
parsec47 0.2.dfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,168 kB
  • ctags: 12
  • sloc: xml: 2,178; ansic: 47; makefile: 28
file content (158 lines) | stat: -rw-r--r-- 3,669 bytes parent folder | download
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
/*
 * $Id: P47Boot.d,v 1.6 2004/01/01 11:26:42 kenta Exp $
 *
 * Copyright 2003 Kenta Cho. All rights reserved.
 */
module abagames.p47.P47Boot;

private:
import std.string;
private import std.conv;
import std.c.stdlib;
import abagames.util.Logger;
import abagames.util.sdl.Pad;
import abagames.util.sdl.MainLoop;
import abagames.util.sdl.Screen3D;
import abagames.util.sdl.Sound;
import abagames.p47.P47Screen;
import abagames.p47.P47GameManager;
import abagames.p47.P47PrefManager;
import abagames.p47.Ship;

/**
 * Boot the game.
 */
private:
P47Screen screen;
Pad pad;
P47GameManager gameManager;
P47PrefManager prefManager;
MainLoop mainLoop;

private void usage(string args0) {
  Logger.error
    ("Usage: " ~ args0 ~ " [-brightness [0-100]] [-luminous [0-100]] [-nosound] [-window] [-fullscreen] [-reverse] [-lowres] [-slowship] [-nowait]");
}

private void parseArgs(string[] args) {
  for (int i = 1; i < args.length; i++) {
    switch (args[i]) {
    case "-brightness":
      if (i >= args.length - 1) {
	usage(args[0]);
	throw new Exception("Invalid options");
      }
      i++;
      float b = cast(float) to!int(args[i]) / 100;
      if (b < 0 || b > 1) {
	usage(args[0]);
	throw new Exception("Invalid options");
      }
      Screen3D.brightness = b;
      break;
    case "-luminous":
      if (i >= args.length - 1) {
	usage(args[0]);
	throw new Exception("Invalid options");
      }
      i++;
      float l = cast(float) to!int(args[i]) / 100;
      if (l < 0 || l > 1) {
	usage(args[0]);
	throw new Exception("Invalid options");
      }
      P47Screen.luminous = l;
      break;
    case "-nosound":
      Sound.noSound = true;
      break;
    case "-window":
      Screen3D.windowMode = true;
      break;
    case "-fullscreen":
      Screen3D.windowMode = false;
      break;
    case "-reverse":
      pad.buttonReversed = true;
      break;
    case "-lowres":
      Screen3D.lowres = true;
      break;
    case "-slowship":
      Ship.isSlow = true;
      break;
    case "-nowait":
      gameManager.nowait = true;
      break;
    case "-accframe":
      mainLoop.accframe = 1;
      break;
    default:
      usage(args[0]);
      throw new Exception("Invalid options");
    }
  }
}

public int boot(string[] args) {
  screen = new P47Screen;
  pad = new Pad;
  try {
    pad.openJoystick();
  } catch (Exception e) {}
  gameManager = new P47GameManager;
  prefManager = new P47PrefManager;
  mainLoop = new MainLoop(screen, pad, gameManager, prefManager);
  try {
    parseArgs(args);
  } catch (Exception e) {
    return EXIT_FAILURE;
  }
  mainLoop.loop();
  return EXIT_SUCCESS;
}

version (Win32_release) {

// Boot as the Windows executable.
import std.c.windows.windows;
import std.string;

extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();

extern (Windows)
public int WinMain(HINSTANCE hInstance,
	    HINSTANCE hPrevInstance,
	    LPSTR lpCmdLine,
	    int nCmdShow) {
  int result;
  
  gc_init();
  _minit();
  try {
    _moduleCtor();
    char exe[4096];
    GetModuleFileNameA(null, exe, 4096);
    string[1] prog;
    prog[0] = to!string(exe);
    result = boot(prog ~ std.string.split(to!string(lpCmdLine)));
  } catch (Throwable o) {
    //Logger.error("Exception: " ~ o.toString());
    Logger.info("Exception: " ~ o.toString());
    result = EXIT_FAILURE;
  }
  gc_term();
  return result;
}

} else {

// Boot as the general executable.
public int main(string[] args) {
  return boot(args);
}

}