Init SDL video subsystem separately, as part of video_init
[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 "video.h"
30 #include "course.h"
31 #include "hole.h"
32 #include "game.h"
33 #include "gui.h"
34
35 #include "st_conf.h"
36 #include "st_all.h"
37
38 #define TITLE "Neverputt " VERSION
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, 1);
96             break;
97
98         case SDL_MOUSEBUTTONUP:
99             d = st_click(e.button.button, 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                 if (e.active.gain == 0 && video_get_grab())
171                     goto_pause(&st_over, 0);
172             break;
173
174         case SDL_JOYAXISMOTION:
175             st_stick(e.jaxis.axis, e.jaxis.value);
176             break;
177
178         case SDL_JOYBUTTONDOWN:
179             d = st_buttn(e.jbutton.button, 1);
180             break;
181
182         case SDL_JOYBUTTONUP:
183             d = st_buttn(e.jbutton.button, 0);
184             break;
185         }
186     }
187     return d;
188 }
189
190 int main(int argc, char *argv[])
191 {
192     int camera = 0;
193     SDL_Joystick *joy = NULL;
194
195     config_exec_path = argv[0];
196
197     srand((int) time(NULL));
198
199     lang_init("neverball");
200
201     if (config_data_path((argc > 1 ? argv[1] : NULL), COURSE_FILE))
202     {
203         if (config_user_path(NULL))
204         {
205             if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == 0)
206             {
207                 config_init();
208                 config_load();
209
210                 /* Cache Neverball's camera setting. */
211
212                 camera = config_get_d(CONFIG_CAMERA);
213
214                 /* Initialize the joystick. */
215
216                 if (SDL_NumJoysticks() > 0)
217                 {
218                     joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
219                     if (joy)
220                     {
221                         SDL_JoystickEventState(SDL_ENABLE);
222                         set_joystick(joy);
223                     }
224                 }
225
226                 /* Initialize the audio. */
227
228                 audio_init();
229
230                 /* Initialize the video. */
231
232                 if (video_init(TITLE, "icon/neverputt.png"))
233                 {
234                     int t1, t0 = SDL_GetTicks();
235
236                     /* Run the main game loop. */
237
238                     init_state(&st_null);
239                     goto_state(&st_title);
240
241                     while (loop())
242                         if ((t1 = SDL_GetTicks()) > t0)
243                         {
244                             st_timer((t1 - t0) / 1000.f);
245                             st_paint(0.001f * t1);
246                             SDL_GL_SwapBuffers();
247
248                             t0 = t1;
249
250                             if (config_get_d(CONFIG_NICE))
251                                 SDL_Delay(1);
252                         }
253                 }
254
255                 /* Restore Neverball's camera setting. */
256
257                 config_set_d(CONFIG_CAMERA, camera);
258                 config_save();
259
260                 SDL_Quit();
261             }
262             else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
263         }
264         else fprintf(stderr, L_("Failure to establish config directory\n"));
265     }
266     else fprintf(stderr, L_("Failure to establish game data directory\n"));
267
268     return 0;
269 }
270
271 /*---------------------------------------------------------------------------*/
272