Implement new shot name generation using a persistent index.
[neverball] / putt / main.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERPUTT is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 /*---------------------------------------------------------------------------*/
16
17 #include <SDL.h>
18 #include <time.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <locale.h>
23
24 #include "glext.h"
25 #include "audio.h"
26 #include "image.h"
27 #include "state.h"
28 #include "config.h"
29 #include "course.h"
30 #include "hole.h"
31 #include "game.h"
32 #include "gui.h"
33 #include "text.h"
34
35 #include "st_conf.h"
36 #include "st_all.h"
37
38 #define TITLE "Neverputt"
39
40 /*---------------------------------------------------------------------------*/
41
42 static int shot(void)
43 {
44     static char filename[MAXSTR];
45
46     sprintf(filename, "screen%05d.png", config_screenshot());
47     image_snap(config_user(filename));
48
49     return 1;
50 }
51 /*---------------------------------------------------------------------------*/
52
53 static void toggle_wire(void)
54 {
55     static int wire = 0;
56
57     if (wire)
58     {
59         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
60         glEnable(GL_TEXTURE_2D);
61         glEnable(GL_LIGHTING);
62         wire = 0;
63     }
64     else
65     {
66         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
67         glDisable(GL_TEXTURE_2D);
68         glDisable(GL_LIGHTING);
69         wire = 1;
70     }
71 }
72 /*---------------------------------------------------------------------------*/
73
74 static int loop(void)
75 {
76     SDL_Event e;
77     int d = 1;
78     int c;
79
80     while (d && SDL_PollEvent(&e))
81     {
82         if (e.type == SDL_QUIT)
83             return 0;
84
85         switch (e.type)
86         {
87         case SDL_MOUSEMOTION:
88             st_point(+e.motion.x,
89                      -e.motion.y + config_get_d(CONFIG_HEIGHT),
90                      +e.motion.xrel,
91                      -e.motion.yrel);
92             break;
93
94         case SDL_MOUSEBUTTONDOWN:
95             d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
96             break;
97
98         case SDL_MOUSEBUTTONUP:
99             d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
100             break;
101
102         case SDL_KEYDOWN:
103
104             c = e.key.keysym.sym;
105
106             if (config_tst_d(CONFIG_KEY_FORWARD, c))
107                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
108
109             else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
110                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
111
112             else if (config_tst_d(CONFIG_KEY_LEFT, c))
113                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
114
115             else if (config_tst_d(CONFIG_KEY_RIGHT, c))
116                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
117
118             else switch (c)
119             {
120             case SDLK_F10: d = shot();                break;
121             case SDLK_F9:  config_tgl_d(CONFIG_FPS);  break;
122             case SDLK_F8:  config_tgl_d(CONFIG_NICE); break;
123             case SDLK_F7:  toggle_wire();             break;
124
125             case SDLK_RETURN:
126                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
127                 break;
128             case SDLK_ESCAPE:
129                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
130                 break;
131
132             default:
133                 d = st_keybd(e.key.keysym.sym, 1);
134             }
135             break;
136
137         case SDL_KEYUP:
138
139             c = e.key.keysym.sym;
140
141             /* gui_stick needs a non-null value, so we use 1 instead of 0. */
142
143             if (config_tst_d(CONFIG_KEY_FORWARD, c))
144                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
145
146             else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
147                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
148
149             else if (config_tst_d(CONFIG_KEY_LEFT, c))
150                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
151
152             else if (config_tst_d(CONFIG_KEY_RIGHT, c))
153                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
154
155             else switch (c)
156             {
157             case SDLK_RETURN:
158                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
159                 break;
160             case SDLK_ESCAPE:
161                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
162                 break;
163
164             default:
165                 d = st_keybd(e.key.keysym.sym, 0);
166             }
167
168         case SDL_ACTIVEEVENT:
169             if (e.active.state == SDL_APPINPUTFOCUS)
170             {
171                 if (e.active.gain == 0)
172                     goto_pause(&st_over, 0);
173             }
174             break;
175
176         case SDL_JOYAXISMOTION:
177             st_stick(e.jaxis.axis, e.jaxis.value);
178             break;
179
180         case SDL_JOYBUTTONDOWN:
181             d = st_buttn(e.jbutton.button, 1);
182             break;
183
184         case SDL_JOYBUTTONUP:
185             d = st_buttn(e.jbutton.button, 0);
186             break;
187         }
188     }
189     return d;
190 }
191
192 int main(int argc, char *argv[])
193 {
194     int camera = 0;
195 #ifndef __APPLE__
196     SDL_Surface *icon;
197 #endif
198     SDL_Joystick *joy = NULL;
199
200     srand((int) time(NULL));
201
202     lang_init("neverball", CONFIG_LOCALE);
203
204     if (config_data_path((argc > 1 ? argv[1] : NULL), COURSE_FILE))
205     {
206         if (config_user_path(NULL))
207         {
208             if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == 0)
209             {
210                 config_init();
211                 config_load();
212
213                 /* Cache Neverball's camera setting. */
214
215                 camera = config_get_d(CONFIG_CAMERA);
216
217                 /* Initialize the joystick. */
218
219                 if (SDL_NumJoysticks() > 0)
220                 {
221                     joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
222                     if (joy)
223                     {
224                         SDL_JoystickEventState(SDL_ENABLE);
225                         set_joystick(joy);
226                     }
227                 }
228
229                 /* Initialize the audio. */
230
231                 audio_init();
232
233                 /* Require 16-bit double buffer with 16-bit depth buffer. */
234
235                 SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     5);
236                 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   5);
237                 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    5);
238                 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
239                 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
240
241 #ifndef __APPLE__
242                 if ((icon = load_surface("icon/neverputt.png")))
243                 {
244                     SDL_WM_SetIcon(icon, NULL);
245                     free(icon->pixels);
246                     SDL_FreeSurface(icon);
247                 }
248 #endif /* __APPLE__ */
249
250                 /* Initialize the video. */
251
252                 if (config_mode(config_get_d(CONFIG_FULLSCREEN),
253                                 config_get_d(CONFIG_WIDTH),
254                                 config_get_d(CONFIG_HEIGHT)))
255                 {
256                     int t1, t0 = SDL_GetTicks();
257
258                     SDL_WM_SetCaption(TITLE, TITLE);
259
260                     /* Run the main game loop. */
261
262                     init_state(&st_null);
263                     goto_state(&st_title);
264
265                     while (loop())
266                         if ((t1 = SDL_GetTicks()) > t0)
267                         {
268                             st_timer((t1 - t0) / 1000.f);
269                             st_paint(0.001f * t1);
270                             SDL_GL_SwapBuffers();
271
272                             t0 = t1;
273
274                             if (config_get_d(CONFIG_NICE))
275                                 SDL_Delay(1);
276                         }
277                 }
278                 else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
279
280                 /* Restore Neverball's camera setting. */
281
282                 config_set_d(CONFIG_CAMERA, camera);
283                 config_save();
284
285                 SDL_Quit();
286             }
287             else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
288         }
289         else fprintf(stderr, L_("Failure to establish config directory\n"));
290     }
291     else fprintf(stderr, L_("Failure to establish game data directory\n"));
292
293     return 0;
294 }
295
296 /*---------------------------------------------------------------------------*/
297