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