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