Widespread minor changes to eliminate the use of SDL_GetTicks to
[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     static int  num = 0;
46
47     sprintf(filename, "screen%02d.png", num++);
48
49     image_snap(filename);
50
51     return 1;
52 }
53
54 /*---------------------------------------------------------------------------*/
55
56 static void toggle_wire(void)
57 {
58     static int wire = 0;
59
60     if (wire)
61     {
62         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
63         glEnable(GL_TEXTURE_2D);
64         glEnable(GL_LIGHTING);
65         wire = 0;
66     }
67     else
68     {
69         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
70         glDisable(GL_TEXTURE_2D);
71         glDisable(GL_LIGHTING);
72         wire = 1;
73     }
74 }
75 /*---------------------------------------------------------------------------*/
76
77 static int loop(void)
78 {
79     SDL_Event e;
80     int d = 1;
81     int c;
82
83     while (d && SDL_PollEvent(&e))
84     {
85         if (e.type == SDL_QUIT)
86             return 0;
87
88         switch (e.type)
89         {
90         case SDL_MOUSEMOTION:
91             st_point(+e.motion.x,
92                      -e.motion.y + config_get_d(CONFIG_HEIGHT),
93                      +e.motion.xrel,
94                      -e.motion.yrel);
95             break;
96
97         case SDL_MOUSEBUTTONDOWN:
98             d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
99             break;
100
101         case SDL_MOUSEBUTTONUP:
102             d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
103             break;
104
105         case SDL_KEYDOWN:
106
107             c = e.key.keysym.sym;
108
109             if (config_tst_d(CONFIG_KEY_FORWARD, c))
110                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
111
112             else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
113                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
114
115             else if (config_tst_d(CONFIG_KEY_LEFT, c))
116                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
117
118             else if (config_tst_d(CONFIG_KEY_RIGHT, c))
119                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
120
121             else switch (c)
122             {
123             case SDLK_F10: d = shot();                break;
124             case SDLK_F9:  config_tgl_d(CONFIG_FPS);  break;
125             case SDLK_F8:  config_tgl_d(CONFIG_NICE); break;
126             case SDLK_F7:  toggle_wire();             break;
127
128             case SDLK_RETURN:
129                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
130                 break;
131             case SDLK_ESCAPE:
132                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
133                 break;
134
135             default:
136                 d = st_keybd(e.key.keysym.sym, 1);
137             }
138             break;
139
140         case SDL_KEYUP:
141
142             c = e.key.keysym.sym;
143
144             /* gui_stick needs a non-null value, so we use 1 instead of 0. */
145
146             if (config_tst_d(CONFIG_KEY_FORWARD, c))
147                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
148
149             else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
150                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
151
152             else if (config_tst_d(CONFIG_KEY_LEFT, c))
153                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
154
155             else if (config_tst_d(CONFIG_KEY_RIGHT, c))
156                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
157
158             else switch (c)
159             {
160             case SDLK_RETURN:
161                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
162                 break;
163             case SDLK_ESCAPE:
164                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
165                 break;
166
167             default:
168                 d = st_keybd(e.key.keysym.sym, 0);
169             }
170
171         case SDL_ACTIVEEVENT:
172             if (e.active.state == SDL_APPINPUTFOCUS)
173             {
174                 if (e.active.gain == 0)
175                     goto_pause(&st_over, 0);
176             }
177             break;
178
179         case SDL_JOYAXISMOTION:
180             st_stick(e.jaxis.axis, e.jaxis.value);
181             break;
182
183         case SDL_JOYBUTTONDOWN:
184             d = st_buttn(e.jbutton.button, 1);
185             break;
186
187         case SDL_JOYBUTTONUP:
188             d = st_buttn(e.jbutton.button, 0);
189             break;
190         }
191     }
192     return d;
193 }
194
195 int main(int argc, char *argv[])
196 {
197     int camera = 0;
198 #ifndef __APPLE__
199     SDL_Surface *icon;
200 #endif
201     SDL_Joystick *joy = NULL;
202
203     srand((int) time(NULL));
204
205     lang_init("neverball", CONFIG_LOCALE);
206
207     if (config_data_path((argc > 1 ? argv[1] : NULL), COURSE_FILE))
208     {
209         if (config_user_path(NULL))
210         {
211             if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == 0)
212             {
213                 config_init();
214                 config_load();
215
216                 /* Cache Neverball's camera setting. */
217
218                 camera = config_get_d(CONFIG_CAMERA);
219
220                 /* Initialize the joystick. */
221
222                 if (SDL_NumJoysticks() > 0)
223                 {
224                     joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
225                     if (joy)
226                     {
227                         SDL_JoystickEventState(SDL_ENABLE);
228                         set_joystick(joy);
229                     }
230                 }
231
232                 /* Initialize the audio. */
233
234                 audio_init();
235
236                 /* Require 16-bit double buffer with 16-bit depth buffer. */
237
238                 SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     5);
239                 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   5);
240                 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    5);
241                 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
242                 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
243
244 #ifndef __APPLE__
245                 if ((icon = load_surface("icon/neverputt.png")))
246                 {
247                     SDL_WM_SetIcon(icon, NULL);
248                     free(icon->pixels);
249                     SDL_FreeSurface(icon);
250                 }
251 #endif /* __APPLE__ */
252
253                 /* Initialize the video. */
254
255                 if (config_mode(config_get_d(CONFIG_FULLSCREEN),
256                                 config_get_d(CONFIG_WIDTH),
257                                 config_get_d(CONFIG_HEIGHT)))
258                 {
259                     int t1, t0 = SDL_GetTicks();
260
261                     SDL_WM_SetCaption(TITLE, TITLE);
262
263                     /* Run the main game loop. */
264
265                     init_state(&st_null);
266                     goto_state(&st_title);
267
268                     while (loop())
269                         if ((t1 = SDL_GetTicks()) > t0)
270                         {
271                             st_timer((t1 - t0) / 1000.f);
272                             st_paint(0.001f * t1);
273                             SDL_GL_SwapBuffers();
274
275                             t0 = t1;
276
277                             if (config_get_d(CONFIG_NICE))
278                                 SDL_Delay(1);
279                         }
280                 }
281                 else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
282
283                 /* Restore Neverball's camera setting. */
284
285                 config_set_d(CONFIG_CAMERA, camera);
286                 config_save();
287
288                 SDL_Quit();
289             }
290             else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
291         }
292         else fprintf(stderr, L_("Failure to establish config directory\n"));
293     }
294     else fprintf(stderr, L_("Failure to establish game data directory\n"));
295
296     return 0;
297 }
298
299 /*---------------------------------------------------------------------------*/
300