Fix the build.
[attac-man.git] / main.c
blob443f0d99f74411fc10942c6ce21bf227a2db5c2d
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>
34 #include "audio.h"
35 #include "object.h"
36 #include "game.h"
37 #include "map.h"
38 #include "player.h"
39 #include "render.h"
40 #include "m_math.h"
41 #include "menu.h"
42 #include "screen.h"
43 #include "net.h"
44 #include "oglconsole.h"
46 extern struct game* current_game;
49 * TODO: Rename function. Need to look around code where it calls
50 * SDL_Quit() and exit and see if we can call this instead in
51 * those places.
53 void die(int ret)
55 OGLCONSOLE_Quit();
56 SDL_Quit();
57 exit(ret);
61 int parse_resolution(char *res, int *w, int *h, int *bpp)
63 char res_copy[80];
64 char *p;
66 snprintf(res_copy, sizeof(res_copy)-1, "%s", res);
67 p = strtok(res_copy, "x");
68 if (!p)
69 goto out_err;
70 *w = atoi(p);
71 if (*w <= 0)
72 goto out_err;
74 p = strtok(NULL, "x");
75 if (!p)
76 goto out_err;
77 *h = atoi(p);
78 if (*h <= 0)
79 goto out_err;
81 p = strtok(NULL, "x");
82 if (!p)
83 goto out_ok;
84 *bpp = atoi(p);
85 if (*bpp <= 0)
86 goto out_err;
88 out_ok:
89 return 0;
90 out_err:
91 printf("Could not parse resolution '%s'\n", res);
92 return 1;
96 /* TODO: rewrite parsing routines here */
97 /* TODO: Add 'map' command to change maps */
98 static void console_input(OGLCONSOLE_Console console, int argc, char **argv)
100 char *cmd = argv[0];
101 int width, height, x;
103 if (argc < 1)
104 return;
106 if (!strcasecmp(cmd, "quit") ||
107 !strcasecmp(cmd, "exit")) {
108 die(0);
111 if (!strcasecmp(cmd, "res")) {
112 if (argc < 2) {
113 con_printf("usage: res <resolution> e.g. 640x480\n");
114 return;
116 if (parse_resolution(argv[1], &width, &height, &x) != 0) {
117 con_printf("usage: res <resolution> e.g. 640x480\n");
118 return;
120 screen_set_resolution(width, height);
121 screen_switch_resolution();
122 OGLCONSOLE_InitText(console, width, height);
123 return;
126 if (!strcasecmp(cmd, "score")) {
127 if (!current_game) {
128 con_printf("Start playing, first...\n");
129 return;
131 if (!current_game->players) {
132 con_printf("But, there are no players?\n");
133 return;
135 for (x = 0; x < current_game->n_players; x++) {
136 con_printf("Player %d: %d points, %d lives\n", x+1,
137 current_game->players[x].score,
138 current_game->players[x].lives);
139 return;
143 OGLCONSOLE_Output(console, "Invalid command: %s\n", cmd);
147 int main(int argc, char **argv)
149 int opt, ret = 0;
150 int width = 800, height = 600, bpp = 32, fullscreen = 0;
151 int enable_sound = 1;
153 while ((opt = getopt(argc, argv, "fwr:q")) != EOF) {
154 switch(opt) {
155 case 'f':
156 fullscreen = 1;
157 break;
158 case 'w':
159 fullscreen = 0;
160 break;
161 case 'q':
162 enable_sound = 0;
163 break;
164 case 'r':
165 if (parse_resolution(optarg, &width, &height,
166 &bpp) != 0)
167 return 1;
168 printf("Set resolution to %dx%dx%d\n",
169 width, height, bpp);
170 break;
171 default:
172 fprintf(stderr, "Invalid command line option: %c\n",
173 opt);
174 ret = 1;
175 case 'h':
176 /* fixme: help? */
177 return ret;
182 render_reshape_window(screen->w, screen->h);
185 srand(SDL_GetTicks());
187 screen_init(fullscreen, width, height, bpp);
189 audio_init(enable_sound);
191 /* preload */
193 object_read_file("gfx/pacman-moving.3d", &last_update);
194 object_read_file("gfx/pacman-dying.3d", &last_update);
195 object_read_file("gfx/pacman-stopped.3d", &last_update);
196 object_read_file("gfx/ghost-green-moving.3d", &last_update);
197 object_read_file("gfx/ghost-green-dying.3d", &last_update);
198 object_read_file("gfx/ghost-green-returning.3d", &last_update);
200 if(argc > 1 && strcmp(argv[1], "--server") == 0)
201 net_server_init();
203 if(argc > 1 && strcmp(argv[1], "--connect") == 0)
204 net_client_init(argv[2]);
207 OGLCONSOLE_Create();
208 OGLCONSOLE_EnterKey(console_input);
210 for(;;)
211 menu_run();
213 return 0;