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