Split config into two files, 'config' and 'base_config'. Therefore #38 is fixed....
[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 #ifdef WIN32
18 #pragma comment(lib, "SDL_ttf.lib")
19 #pragma comment(lib, "SDL_mixer.lib")
20 #pragma comment(lib, "SDL_image.lib")
21 #pragma comment(lib, "SDL.lib")
22 #pragma comment(lib, "SDLmain.lib")
23 #pragma comment(lib, "opengl32.lib")
24 #endif
25
26 /*---------------------------------------------------------------------------*/
27
28 #include <SDL.h>
29 #include <SDL_ttf.h>
30 #include <SDL_mixer.h>
31 #include <SDL_image.h>
32 #include <time.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <locale.h>
37
38 #include "glext.h"
39 #include "audio.h"
40 #include "image.h"
41 #include "state.h"
42 #include "config.h"
43 #include "course.h"
44 #include "hole.h"
45 #include "game.h"
46 #include "gui.h"
47
48 #include "st_conf.h"
49 #include "st_all.h"
50
51 #define TITLE _("Neverputt")
52
53 /*---------------------------------------------------------------------------*/
54
55 static int shot(void)
56 {
57     static char filename[MAXSTR];
58     static int  num = 0;
59
60     sprintf(filename, "screen%02d.bmp", num++);
61
62     image_snap(filename, config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT));
63
64     return 1;
65 }
66
67 /*---------------------------------------------------------------------------*/
68
69 static void toggle_wire(void)
70 {
71     static int wire = 0;
72
73     if (wire)
74     {
75         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
76         glEnable(GL_TEXTURE_2D);
77         glEnable(GL_LIGHTING);
78         wire = 0;
79     }
80     else
81     {
82         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
83         glDisable(GL_TEXTURE_2D);
84         glDisable(GL_LIGHTING);
85         wire = 1;
86     }
87 }
88 /*---------------------------------------------------------------------------*/
89
90 static int loop(void)
91 {
92     SDL_Event e;
93     int d = 1;
94
95     while (d && SDL_PollEvent(&e))
96     {
97         if (e.type == SDL_QUIT)
98             return 0;
99
100         if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_SPACE)
101             config_tgl_pause();
102
103         if (!config_get_pause())
104             switch (e.type)
105             {
106             case SDL_MOUSEMOTION:
107                 st_point(+e.motion.x,
108                          -e.motion.y + config_get_d(CONFIG_HEIGHT),
109                          +e.motion.xrel,
110                          -e.motion.yrel);
111                 break;
112
113             case SDL_MOUSEBUTTONDOWN:
114                 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
115                 break;
116
117             case SDL_MOUSEBUTTONUP:
118                 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
119                 break;
120
121             case SDL_KEYDOWN:
122                 switch (e.key.keysym.sym)
123                 {
124                 case SDLK_F10: d = shot();                break;
125                 case SDLK_F9:  config_tgl_d(CONFIG_FPS);  break;
126                 case SDLK_F8:  config_tgl_d(CONFIG_NICE); break;
127                 case SDLK_F7:  toggle_wire();             break;
128                 
129                 default:
130                     d = st_keybd(e.key.keysym.sym, 1);
131                 }
132                 break;
133
134             case SDL_ACTIVEEVENT:
135                 if (e.active.state == SDL_APPINPUTFOCUS)
136                 {
137                     if (e.active.gain == 0)
138                         config_set_pause();
139                 }
140                 break;
141             }
142     }
143     return d;
144 }
145
146 int main(int argc, char *argv[])
147 {
148     int camera = 0;
149  
150     srand((int) time(NULL));
151
152     language_init("neverball", CONFIG_LOCALE);
153
154     if (config_data_path((argc > 1 ? argv[1] : NULL), COURSE_FILE))
155     {
156         if (config_user_path(NULL))
157         {
158             if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == 0)
159             {
160                 config_init();
161                 config_load();
162
163                 /* Initialize the language. */
164                 
165                 language_set(language_from_code(config_simple_get_s(CONFIG_LANG)));
166
167                 /* Cache Neverball's camera setting. */
168
169                 camera = config_get_d(CONFIG_CAMERA);
170
171                 /* Initialize the audio. */
172
173                 audio_bind(AUD_BIRDIE,  1, "snd/birdie.ogg");
174                 audio_bind(AUD_BOGEY,   1, "snd/bogey.ogg");
175                 audio_bind(AUD_BUMP,    1, "snd/bink.wav");
176                 audio_bind(AUD_DOUBLE,  1, "snd/double.ogg");
177                 audio_bind(AUD_EAGLE,   1, "snd/eagle.ogg");
178                 audio_bind(AUD_JUMP,    2, "snd/jump.ogg");
179                 audio_bind(AUD_MENU,    2, "snd/menu.wav");
180                 audio_bind(AUD_ONE,     1, "snd/one.ogg");
181                 audio_bind(AUD_PAR,     1, "snd/par.ogg");
182                 audio_bind(AUD_PENALTY, 1, "snd/penalty.ogg");
183                 audio_bind(AUD_PLAYER1, 1, "snd/player1.ogg");
184                 audio_bind(AUD_PLAYER2, 1, "snd/player2.ogg");
185                 audio_bind(AUD_PLAYER3, 1, "snd/player3.ogg");
186                 audio_bind(AUD_PLAYER4, 1, "snd/player4.ogg");
187                 audio_bind(AUD_SWITCH,  2, "snd/switch.wav");
188                 audio_bind(AUD_SUCCESS, 1, "snd/success.ogg");
189
190                 audio_init();
191
192                 /* Require 16-bit double buffer with 16-bit depth buffer. */
193
194                 SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     5);
195                 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   5);
196                 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    5);
197                 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
198                 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
199
200                 /* Initialize the video. */
201
202                 if (config_mode(config_get_d(CONFIG_FULLSCREEN),
203                                 config_get_d(CONFIG_WIDTH),
204                                 config_get_d(CONFIG_HEIGHT)))
205                 {
206                     int t1, t0 = SDL_GetTicks();
207
208                     SDL_Surface *icon = IMG_Load(config_data("icon/neverputt.png"));
209                     SDL_WM_SetIcon(icon, NULL);
210                     SDL_WM_SetCaption(TITLE, TITLE); 
211
212                     /* Run the main game loop. */
213
214                     init_state(&st_null);
215                     goto_state(&st_title);
216
217                     while (loop())
218                         if ((t1 = SDL_GetTicks()) > t0)
219                         {
220                            if (config_get_pause())
221                             {
222                                 st_paint();
223                                 gui_blank();
224                             }
225                             else
226                             {
227                                 st_timer((t1 - t0) / 1000.f);
228                                 st_paint();
229                             }
230                             SDL_GL_SwapBuffers();
231
232                             t0 = t1;
233
234                             if (config_get_d(CONFIG_NICE))
235                                 SDL_Delay(1);
236                         }
237                 }
238                 else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
239
240                 /* Restore Neverball's camera setting. */
241
242                 config_set_d(CONFIG_CAMERA, camera);
243                 config_save();
244
245                 SDL_Quit();
246             }
247             else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
248         }
249         else fprintf(stderr, _("Failure to establish config directory\n"));
250     }
251     else fprintf(stderr, _("Failure to establish game data directory\n"));
252
253     return 0;
254 }
255
256 /*---------------------------------------------------------------------------*/
257