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