Import from neverball-1.3.7.tar.gz
[neverball-archive.git] / putt / main.c
blob475694667e3a625b7e7aaf80354a84fe90865a92
1 /*
2 * Copyright (C) 2003 Robert Kooima
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.
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 <SDL_ttf.h>
30 #include <SDL_mixer.h>
31 #include <time.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include "glext.h"
37 #include "audio.h"
38 #include "image.h"
39 #include "state.h"
40 #include "config.h"
41 #include "hole.h"
43 #define TITLE "Neverputt"
45 /*---------------------------------------------------------------------------*/
47 static int shot(void)
49 static char filename[MAXSTR];
50 static int num = 0;
52 sprintf(filename, "screen%02d.bmp", num++);
54 image_snap(filename);
56 return 1;
59 /*---------------------------------------------------------------------------*/
61 static int loop(void)
63 SDL_Event e;
64 int d = 1;
66 while (d && SDL_PollEvent(&e))
68 if (e.type == SDL_QUIT)
69 return 0;
71 if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_SPACE)
72 config_tgl_pause();
74 if (!config_get_pause())
75 switch (e.type)
77 case SDL_MOUSEMOTION:
78 d = st_point(+e.motion.x,
79 -e.motion.y + config_get_d(CONFIG_HEIGHT),
80 +e.motion.xrel,
81 -e.motion.yrel);
82 break;
84 case SDL_MOUSEBUTTONDOWN:
85 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
86 break;
88 case SDL_MOUSEBUTTONUP:
89 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
90 break;
92 case SDL_KEYDOWN:
93 switch (e.key.keysym.sym)
95 case SDLK_F10: d = shot(); break;
96 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
97 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
99 default:
100 d = st_keybd(e.key.keysym.sym);
102 break;
104 case SDL_ACTIVEEVENT:
105 if (e.active.state == SDL_APPINPUTFOCUS)
107 if (e.active.gain == 0)
108 config_set_pause();
110 break;
113 return d;
116 int main(int argc, char *argv[])
118 int camera = 0;
120 if (config_data_path((argc > 1 ? argv[1] : NULL), HOLE_FILE))
122 if (config_user_path(NULL))
124 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == 0)
126 config_init();
127 config_load();
129 /* Cache Neverball's camera setting. */
131 camera = config_get_d(CONFIG_CAMERA);
133 /* Initialize the audio. */
135 audio_init();
137 /* Require 16-bit double buffer with 16-bit depth buffer. */
139 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
140 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
141 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
142 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
143 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
145 /* Initialize the video. */
147 if (config_mode(config_get_d(CONFIG_FULLSCREEN),
148 config_get_d(CONFIG_WIDTH),
149 config_get_d(CONFIG_HEIGHT)))
151 int t1, t0 = SDL_GetTicks();
153 SDL_WM_SetCaption(TITLE, TITLE);
155 /* Run the main game loop. */
157 goto_state(&st_title);
159 while (loop())
160 if ((t1 = SDL_GetTicks()) > t0)
162 if (!config_get_pause())
163 st_timer((t1 - t0) / 1000.f);
165 st_paint();
166 SDL_GL_SwapBuffers();
168 t0 = t1;
170 if (config_get_d(CONFIG_NICE))
171 SDL_Delay(1);
174 else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
176 /* Restore Neverball's camera setting. */
178 config_set_d(CONFIG_CAMERA, camera);
179 config_save();
181 SDL_Quit();
183 else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
185 else fprintf(stderr, "Failure to establish config directory\n");
187 else fprintf(stderr, "Failure to establish game data directory\n");
189 return 0;
192 /*---------------------------------------------------------------------------*/