Import from neverball-1.3.1.tar.gz
[neverball-archive.git] / putt / main.c
blob2ac53d9fda0f983a85de79edcd6cdc135860113d
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, "glu32.lib")
24 #pragma comment(lib, "opengl32.lib")
25 #endif
27 /*---------------------------------------------------------------------------*/
29 #include <SDL.h>
30 #include <SDL_ttf.h>
31 #include <SDL_mixer.h>
32 #include <time.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #include "glext.h"
38 #include "audio.h"
39 #include "image.h"
40 #include "state.h"
41 #include "config.h"
42 #include "hole.h"
44 #define TITLE "Neverputt"
46 /*---------------------------------------------------------------------------*/
48 static int shot(void)
50 static char filename[MAXSTR];
51 static int num = 0;
53 sprintf(filename, "screen%02d.bmp", num++);
55 image_snap(filename);
57 return 1;
60 /*---------------------------------------------------------------------------*/
62 static int loop(void)
64 SDL_Event e;
65 int d = 1;
67 while (d && SDL_PollEvent(&e))
69 if (e.type == SDL_QUIT)
70 return 0;
72 if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_SPACE)
73 config_tgl_pause();
75 if (!config_get_pause())
76 switch (e.type)
78 case SDL_MOUSEMOTION:
79 d = st_point(+e.motion.x,
80 -e.motion.y + config_get_d(CONFIG_HEIGHT),
81 +e.motion.xrel,
82 -e.motion.yrel);
83 break;
85 case SDL_MOUSEBUTTONDOWN:
86 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
87 break;
89 case SDL_MOUSEBUTTONUP:
90 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
91 break;
93 case SDL_KEYDOWN:
94 switch (e.key.keysym.sym)
96 case SDLK_F10: d = shot(); break;
97 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
98 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
100 default:
101 d = st_keybd(e.key.keysym.sym);
103 break;
105 case SDL_ACTIVEEVENT:
106 if (e.active.state == SDL_APPINPUTFOCUS)
108 if (e.active.gain == 0)
109 config_set_pause();
111 break;
114 return d;
117 int main(int argc, char *argv[])
119 int camera = 0;
121 if (config_data_path((argc > 1 ? argv[1] : NULL), HOLE_FILE))
123 if (config_user_path(NULL))
125 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == 0)
127 config_init();
128 config_load();
130 /* Cache Neverball's camera setting. */
132 camera = config_get_d(CONFIG_CAMERA);
134 /* Initialize the audio. */
136 audio_init();
138 /* Require 16-bit double buffer with 16-bit depth buffer. */
140 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
141 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
142 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
143 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
144 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
146 /* Initialize the video. */
148 if (config_mode(config_get_d(CONFIG_FULLSCREEN),
149 config_get_d(CONFIG_WIDTH),
150 config_get_d(CONFIG_HEIGHT)))
152 int t1, t0 = SDL_GetTicks();
154 SDL_WM_SetCaption(TITLE, TITLE);
156 /* Run the main game loop. */
158 goto_state(&st_title);
160 while (loop())
161 if ((t1 = SDL_GetTicks()) > t0)
163 if (!config_get_pause())
164 st_timer((t1 - t0) / 1000.f);
166 st_paint();
167 SDL_GL_SwapBuffers();
169 t0 = t1;
171 if (config_get_d(CONFIG_NICE))
172 SDL_Delay(1);
175 else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
177 /* Restore Neverball's camera setting. */
179 config_set_d(CONFIG_CAMERA, camera);
180 config_save();
182 SDL_Quit();
184 else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
186 else fprintf(stderr, "Failure to establish config directory\n");
188 else fprintf(stderr, "Failure to establish game data directory\n");
190 return 0;
193 /*---------------------------------------------------------------------------*/