Import from neverball-1.4.0.tar.gz
[neverball-archive.git] / ball / main.c
bloba1dc220f1024f91227f643f57a325c28047b5ca5
1 /*
2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL 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.
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.
15 /*---------------------------------------------------------------------------*/
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
26 /*---------------------------------------------------------------------------*/
28 #include <SDL.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "glext.h"
33 #include "config.h"
34 #include "image.h"
35 #include "audio.h"
36 #include "demo.h"
37 #include "game.h"
38 #include "gui.h"
39 #include "set.h"
41 #include "st_conf.h"
42 #include "st_title.h"
44 #define TITLE "Neverball"
46 /*---------------------------------------------------------------------------*/
48 static void shot(void)
50 static char filename[MAXSTR];
51 static int num = 0;
53 sprintf(filename, "screen%02d.bmp", num++);
55 image_snap(filename);
58 /*---------------------------------------------------------------------------*/
60 static void toggle_wire(void)
62 static int wire = 0;
64 if (wire)
66 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
67 glEnable(GL_TEXTURE_2D);
68 glEnable(GL_LIGHTING);
69 wire = 0;
71 else
73 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
74 glDisable(GL_TEXTURE_2D);
75 glDisable(GL_LIGHTING);
76 wire = 1;
80 /*---------------------------------------------------------------------------*/
82 static int loop(void)
84 SDL_Event e;
85 int d = 1;
87 while (d && SDL_PollEvent(&e))
89 if (e.type == SDL_QUIT)
90 return 0;
92 if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_SPACE)
93 config_tgl_pause();
95 if (!config_get_pause())
96 switch (e.type)
98 case SDL_MOUSEMOTION:
99 st_point(+e.motion.x,
100 #ifdef __APPLE__
101 +e.motion.y,
102 #else
103 -e.motion.y + config_get_d(CONFIG_HEIGHT),
104 #endif
105 +e.motion.xrel,
106 config_get_d(CONFIG_MOUSE_INVERT)
107 ? +e.motion.yrel : -e.motion.yrel);
108 break;
110 case SDL_MOUSEBUTTONDOWN:
111 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
112 break;
114 case SDL_MOUSEBUTTONUP:
115 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
116 break;
118 case SDL_KEYDOWN:
120 switch (e.key.keysym.sym)
122 case SDLK_F10: shot(); break;
123 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
124 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
125 case SDLK_F7: toggle_wire(); break;
127 case SDLK_RETURN:
128 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
129 break;
130 case SDLK_LEFT:
131 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
132 break;
133 case SDLK_RIGHT:
134 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
135 break;
136 case SDLK_UP:
137 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
138 break;
139 case SDLK_DOWN:
140 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
141 break;
143 default:
144 d = st_keybd(e.key.keysym.sym, 1);
146 break;
148 case SDL_KEYUP:
150 switch (e.key.keysym.sym)
152 case SDLK_RETURN:
153 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
154 break;
155 case SDLK_LEFT:
156 case SDLK_RIGHT:
157 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
158 break;
159 case SDLK_DOWN:
160 case SDLK_UP:
161 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
162 break;
164 default:
165 d = st_keybd(e.key.keysym.sym, 0);
168 break;
170 case SDL_ACTIVEEVENT:
171 if (e.active.state == SDL_APPINPUTFOCUS)
172 if (e.active.gain == 0 && config_get_grab())
173 config_set_pause();
174 break;
176 case SDL_JOYAXISMOTION:
177 st_stick(e.jaxis.axis, e.jaxis.value);
178 break;
180 case SDL_JOYBUTTONDOWN:
181 d = st_buttn(e.jbutton.button, 1);
182 break;
184 case SDL_JOYBUTTONUP:
185 d = st_buttn(e.jbutton.button, 0);
186 break;
189 return d;
192 int main(int argc, char *argv[])
194 if (config_data_path((argc > 1 ? argv[1] : NULL), SET_FILE))
196 if (config_user_path(NULL))
198 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK))
200 SDL_Joystick *joy = NULL;
202 config_init();
203 config_load();
205 /* Initialize the joystick. */
207 if (SDL_NumJoysticks() > 0)
209 joy=SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
210 if (joy)
211 SDL_JoystickEventState(SDL_ENABLE);
214 /* Initialize the audio. */
216 audio_bind(AUD_MENU, 3, "snd/menu.wav");
217 audio_bind(AUD_START, 1, "snd/select.ogg");
218 audio_bind(AUD_READY, 1, "snd/ready.ogg");
219 audio_bind(AUD_SET, 1, "snd/set.ogg");
220 audio_bind(AUD_GO, 1, "snd/go.ogg");
221 audio_bind(AUD_BALL, 2, "snd/ball.ogg");
222 audio_bind(AUD_BUMP, 3, "snd/bump.ogg");
223 audio_bind(AUD_COIN, 2, "snd/coin.wav");
224 audio_bind(AUD_TICK, 4, "snd/tick.ogg");
225 audio_bind(AUD_TOCK, 4, "snd/tock.ogg");
226 audio_bind(AUD_SWITCH, 5, "snd/switch.wav");
227 audio_bind(AUD_JUMP, 5, "snd/jump.ogg");
228 audio_bind(AUD_GOAL, 5, "snd/goal.wav");
229 audio_bind(AUD_SCORE, 1, "snd/record.ogg");
230 audio_bind(AUD_FALL, 1, "snd/fall.ogg");
231 audio_bind(AUD_TIME, 1, "snd/time.ogg");
232 audio_bind(AUD_OVER, 1, "snd/over.ogg");
234 audio_init();
236 /* Require 16-bit double buffer with 16-bit depth buffer. */
238 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
239 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
240 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
241 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
242 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
244 /* Initialize the video. */
246 if (config_mode(config_get_d(CONFIG_FULLSCREEN),
247 config_get_d(CONFIG_WIDTH),
248 config_get_d(CONFIG_HEIGHT)))
250 int t1, t0 = SDL_GetTicks();
252 SDL_WM_SetCaption(TITLE, TITLE);
254 /* Initialize the run state and the title display. */
256 init_state(&st_null);
257 goto_state(&st_title);
259 /* Run the main game loop. */
261 while (loop())
262 if ((t1 = SDL_GetTicks()) > t0)
264 if (config_get_pause())
266 st_paint();
267 gui_blank();
269 else
271 st_timer((t1 - t0) / 1000.f);
272 st_paint();
274 SDL_GL_SwapBuffers();
276 t0 = t1;
278 if (config_get_d(CONFIG_NICE))
279 SDL_Delay(1);
282 else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
284 config_save();
286 if (SDL_JoystickOpened(0))
287 SDL_JoystickClose(joy);
289 SDL_Quit();
291 else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
293 else fprintf(stderr, "Failure to establish config directory\n");
295 else fprintf(stderr, "Failure to establish game data directory\n");
297 return 0;
300 /*---------------------------------------------------------------------------*/