console: Format tabs semi-intelligently
[attac-man.git] / main.c
blob2692edff6a885b348636e6196a00f99a33c191ac
1 /*
2 Pacman Arena
3 Copyright (C) 2003 Nuno Subtil
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 static const char cvsid[] =
21 "$Id: main.c,v 1.60 2003/11/27 22:11:57 nsubtil Exp $";
23 #ifdef _WIN32
24 #include <windows.h>
25 #endif
27 #include <GL/gl.h>
28 #include <SDL.h>
29 #include <SDL_net.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <libgen.h> /* basename() */
35 #include "audio.h"
36 #include "object.h"
37 #include "game.h"
38 #include "map.h"
39 #include "player.h"
40 #include "render.h"
41 #include "m_math.h"
42 #include "menu.h"
43 #include "screen.h"
44 #include "net.h"
45 #include "oglconsole.h"
46 #include "score.h"
48 extern struct game* current_game;
51 * TODO: Rename function. Need to look around code where it calls
52 * SDL_Quit() and exit and see if we can call this instead in
53 * those places.
55 void die(int ret)
57 OGLCONSOLE_Quit();
58 SDL_Quit();
59 exit(ret);
63 int parse_resolution(char *res, int *w, int *h, int *bpp)
65 char res_copy[80];
66 char *p;
68 snprintf(res_copy, sizeof(res_copy)-1, "%s", res);
69 p = strtok(res_copy, "x");
70 if (!p)
71 goto out_err;
72 *w = atoi(p);
73 if (*w <= 0)
74 goto out_err;
76 p = strtok(NULL, "x");
77 if (!p)
78 goto out_err;
79 *h = atoi(p);
80 if (*h <= 0)
81 goto out_err;
83 p = strtok(NULL, "x");
84 if (!p)
85 goto out_ok;
86 *bpp = atoi(p);
87 if (*bpp <= 0)
88 goto out_err;
90 out_ok:
91 return 0;
92 out_err:
93 printf("Could not parse resolution '%s'\n", res);
94 return 1;
98 /* TODO: rewrite parsing routines here */
99 /* TODO: Add 'map' command to change maps */
100 static void console_input(OGLCONSOLE_Console console, int argc, char **argv)
102 char *cmd = argv[0];
103 int width, height, x;
105 if (argc < 1)
106 return;
108 if (!strcasecmp(cmd, "quit") ||
109 !strcasecmp(cmd, "exit")) {
110 die(0);
113 if (!strcasecmp(cmd, "res")) {
114 if (argc < 2) {
115 con_printf("usage: res <resolution> e.g. 640x480\n");
116 return;
118 if (parse_resolution(argv[1], &width, &height, &x) != 0) {
119 con_printf("usage: res <resolution> e.g. 640x480\n");
120 return;
122 screen_set_resolution(width, height);
123 screen_switch_resolution();
124 OGLCONSOLE_InitText(console, width, height);
125 return;
128 if (!strcasecmp(cmd, "high-scores")) {
129 debug_print_high_scores();
130 return;
133 if (!strncasecmp(cmd, "score", 5)) {
134 if (!current_game) {
135 con_printf("Start playing, first...\n");
136 return;
138 if (!current_game->players) {
139 con_printf("But, there are no players?\n");
140 return;
142 for (x = 0; x < current_game->n_players; x++) {
143 con_printf("Player %d: %d points, %d lives\n", x+1,
144 current_game->players[x].score,
145 current_game->players[x].lives);
146 return;
150 OGLCONSOLE_Output(console, "Invalid command: %s\n", cmd);
153 void usage(char *prog)
155 printf("Usage: %s [options]\n"
156 "\t-f\tFullscreen\n"
157 "\t-w\tWindowed\n"
158 "\t-q\tQuiet (disable sound)\n"
159 "\t-r\tResolution <w>x<h> e.g. 640x480\n"
160 "\t-h\tThis help text\n", basename(prog));
163 int main(int argc, char **argv)
165 int opt, ret = 0;
166 int width = 800, height = 600, bpp = 32, fullscreen = 0;
167 int enable_sound = 1;
169 while ((opt = getopt(argc, argv, "fwr:q")) != EOF) {
170 switch(opt) {
171 case 'f':
172 fullscreen = 1;
173 break;
174 case 'w':
175 fullscreen = 0;
176 break;
177 case 'q':
178 enable_sound = 0;
179 break;
180 case 'r':
181 if (parse_resolution(optarg, &width, &height,
182 &bpp) != 0)
183 return 1;
184 printf("Set resolution to %dx%dx%d\n",
185 width, height, bpp);
186 break;
187 default:
188 fprintf(stderr, "Invalid command line option: %c\n",
189 opt);
190 ret = 1;
191 case 'h':
192 usage(argv[0]);
193 return 0;
198 render_reshape_window(screen->w, screen->h);
201 srand(SDL_GetTicks());
203 screen_init(fullscreen, width, height, bpp);
205 audio_init(enable_sound);
207 /* preload */
209 object_read_file("gfx/pacman-moving.3d", &last_update);
210 object_read_file("gfx/pacman-dying.3d", &last_update);
211 object_read_file("gfx/pacman-stopped.3d", &last_update);
212 object_read_file("gfx/ghost-green-moving.3d", &last_update);
213 object_read_file("gfx/ghost-green-dying.3d", &last_update);
214 object_read_file("gfx/ghost-green-returning.3d", &last_update);
216 if(argc > 1 && strcmp(argv[1], "--server") == 0)
217 net_server_init();
219 if(argc > 1 && strcmp(argv[1], "--connect") == 0)
220 net_client_init(argv[2]);
223 OGLCONSOLE_Create();
224 OGLCONSOLE_EnterKey(console_input);
226 high_scores_init();
228 for(;;)
229 menu_run();
231 return 0;