Update info about wav files on Maemo (README)
[tennix.git] / tennix.c
blob73962b923ebeb903e74de007f793d2c1e45dbbe6
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008 Thomas Perl <thp@perli.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 **/
24 #include <stdio.h>
25 #include <time.h>
26 #include <libgen.h>
28 #ifdef WIN32
29 #include <windows.h>
30 #endif
32 #include "tennix.h"
33 #include "game.h"
34 #include "graphics.h"
35 #include "sound.h"
36 #include "input.h"
38 SDL_Surface *screen;
40 #ifdef WIN32
42 /* IDs from the resource file */
43 #define START_BUTTON 1
44 #define CHECKBOX_FULLSCREEN 2
45 #define QUIT_BUTTON 3
47 BOOL CALLBACK ConfigDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
49 static int checkbox_is_checked;
51 switch (uMsg) {
52 case WM_CLOSE:
53 EndDialog(hwndDlg, IDCANCEL);
54 break;
55 case WM_COMMAND:
56 switch (wParam) {
57 case START_BUTTON:
58 EndDialog(hwndDlg, (checkbox_is_checked)?(IDYES):(IDNO));
59 break;
60 case QUIT_BUTTON:
61 EndDialog(hwndDlg, IDCANCEL);
62 break;
63 case CHECKBOX_FULLSCREEN:
64 checkbox_is_checked ^= 1;
65 break;
67 break;
68 default:
69 return FALSE;
71 return TRUE;
73 #endif
75 #ifdef WIN32
76 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
77 LPSTR lpCmdLine, int nCmdShow) {
78 #else
79 int main( int argc, char** argv) {
80 #endif
81 int i, slide, slide_direction = -1;
82 int ticks;
83 int mx, my;
84 Uint8 *keys;
85 Uint8 mb;
86 SDL_Event e;
87 int sdl_flags = SDL_SWSURFACE;
88 int copyright_line_width;
89 int btn_offset;
90 int btn_hovering = 0;
91 int slide_start;
93 #ifdef MAEMO
94 sdl_flags |= SDL_FULLSCREEN;
95 #endif
97 #ifdef WIN32
98 int mb_result;
99 mb_result = DialogBox(hInstance, "CONFIG", 0, (DLGPROC)ConfigDialogProc);
101 switch (mb_result) {
102 case IDYES:
103 sdl_flags |= SDL_FULLSCREEN;
104 break;
105 case IDCANCEL:
106 return 0;
107 break;
108 default:
109 break;
111 #else
112 fprintf( stderr, "Tennix %s\n%s\n%s\n\n", VERSION, COPYRIGHT, URL);
114 bool do_help = false;
115 i = 1;
116 while (i < argc) {
117 /* A poor/lazy man's getopt */
118 #define OPTION_SET(longopt,shortopt) \
119 (strcmp(argv[i], longopt)==0 || strcmp(argv[i], shortopt)==0)
120 #define OPTION_VALUE \
121 ((i+1 < argc)?(argv[i+1]):(NULL))
122 #define OPTION_VALUE_PROCESSED \
123 (i++)
124 if (OPTION_SET("--fullscreen", "-f")) {
125 sdl_flags |= SDL_FULLSCREEN;
127 else if (OPTION_SET("--help", "-h")) {
128 do_help = true;
130 else if (OPTION_SET("--list-joysticks", "-J")) {
131 SDL_Init(SDL_INIT_JOYSTICK);
132 joystick_list();
133 return 0;
135 else if (OPTION_SET("--joystick", "-j")) {
136 SDL_Init(SDL_INIT_JOYSTICK);
137 if (OPTION_VALUE == NULL) {
138 fprintf(stderr, "Error: You need to specify the name of the joystick as parameter.\n");
139 do_help = true;
140 break;
142 if (joystick_open(OPTION_VALUE)==0) {
143 fprintf(stderr, "Warning: Cannot find joystick \"%s\" - Ignored.\n", OPTION_VALUE);
144 break;
146 OPTION_VALUE_PROCESSED;
148 else {
149 fprintf(stderr, "Ignoring unknown option: %s\n", argv[i]);
151 i++;
154 if (do_help == true) {
155 fprintf(stderr, "Usage: %s [--fullscreen|-f] [--help|-h] [--list-joysticks|-J] [--joystick|-j] [joystick name]\n", argv[0]);
156 return 0;
158 #endif
160 srand( (unsigned)time( NULL));
162 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1) {
163 fprintf( stderr, "Can't init SDL: %s\n", SDL_GetError());
164 exit( 1);
167 SDL_VideoInfo* vi = (SDL_VideoInfo*)SDL_GetVideoInfo();
168 if( (screen = SDL_SetVideoMode( WIDTH, HEIGHT, vi->vfmt->BitsPerPixel, sdl_flags)) == NULL) {
169 fprintf( stderr, "Can't set video mode: %s\n", SDL_GetError());
170 exit( 1);
173 SDL_WM_SetCaption( "Tennix " VERSION, "Tennix");
174 SDL_ShowCursor( SDL_DISABLE);
175 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, 1);
177 init_sound();
178 init_graphics();
179 init_joystick();
181 play_sample_background(SOUND_BACKGROUND);
183 copyright_line_width = font_get_string_width( GR_DKC2_FONT, COPYRIGHT);
185 clear_screen();
186 start_fade();
188 i = 0;
189 ticks = SDL_GetTicks();
190 slide_start = get_image_width(GR_SIDEBAR);
191 slide = slide_start;
192 while( 1) {
193 if (SDL_GetTicks() > ticks + 20) {
194 if (slide > 0 && slide <= slide_start) {
195 slide += slide_direction*(1+slide/25);
196 } else if (slide >= slide_start) {
197 slide = slide_start;
198 slide_direction = 0;
199 } else {
200 slide = 0;
201 slide_direction = 1;
203 ticks = SDL_GetTicks();
205 clear_screen();
206 btn_offset = slide/7;
207 show_image(GR_SIDEBAR, WIDTH-get_image_width(GR_SIDEBAR)+slide, 0, 255);
208 show_image(GR_TENNIXLOGO, WIDTH-get_image_width(GR_SIDEBAR)-10+slide/2, 20, 255);
209 show_image(GR_BTN_PLAY, WIDTH-get_image_width(GR_BTN_PLAY)+slide+btn_offset+3-(3*(btn_hovering==1)), 150, 255);
210 show_image(GR_BTN_OPTIONS, WIDTH-get_image_width(GR_BTN_OPTIONS)+slide+btn_offset+3-(3*(btn_hovering==2)), 230, 255);
211 show_image(GR_BTN_QUIT, WIDTH-get_image_width(GR_BTN_QUIT)+slide+btn_offset+3-(3*(btn_hovering==3)), 350, 255);
213 SDL_PollEvent( &e);
214 if( e.type == SDL_QUIT) {
215 break;
217 keys = SDL_GetKeyState( NULL);
218 mb = SDL_GetMouseState( &mx, &my);
220 if( keys[SDLK_ESCAPE] || keys['q']) {
221 break;
224 if( keys['f']) {
225 SDL_WM_ToggleFullScreen( screen);
228 if( M_POS_START_GAME(mx,my)) {
229 if (btn_hovering != 1) play_sample(SOUND_MOUSEOVER);
230 btn_hovering = 1;
231 } else if( M_POS_START_MULTI(mx,my)) {
232 if (btn_hovering != 2) play_sample(SOUND_MOUSEOVER);
233 btn_hovering = 2;
234 } else if( M_POS_QUIT(mx,my)) {
235 if (btn_hovering != 3) play_sample(SOUND_MOUSEOVER);
236 btn_hovering = 3;
237 } else {
238 btn_hovering = 0;
241 font_draw_string( GR_SMALLISH_FONT, COPYRIGHT, (WIDTH-copyright_line_width)/2-80, HEIGHT-35, i*3, (((i*3)/150)%ANIMATION_COUNT));
242 #ifndef MAEMO /* No mouse cursor on Maemo (we have a touchscreen) */
243 show_sprite( GR_RACKET, ((mb&SDL_BUTTON( SDL_BUTTON_LEFT))>0)+(((mb&SDL_BUTTON( SDL_BUTTON_RIGHT))>0)*2), 4, mx, my, 255);
244 #endif
245 font_draw_string( GR_SMALLISH_FONT, "FYI: Currently, the options menu is the two-player mode :)", 10, 10, 0, 0);
247 #ifdef DEBUG
248 /* Draw the "real" mouse coordinates */
249 rectangle(mx-1, my-1, 2, 2, 255, 255, 255);
250 #endif
251 store_screen();
252 updatescr();
254 if( mb & SDL_BUTTON( SDL_BUTTON_LEFT)) {
255 play_sample(SOUND_MOUSECLICK);
256 if( M_POS_START_GAME(mx,my)) {
257 stop_sample(SOUND_BACKGROUND);
258 start_fade();
259 game( true);
260 SDL_Delay( 150);
261 start_fade();
262 while( SDL_PollEvent( &e));
263 play_sample_background(SOUND_BACKGROUND);
264 slide = slide_start;
265 slide_direction = -1;
267 if( M_POS_START_MULTI(mx,my)) {
268 stop_sample(SOUND_BACKGROUND);
269 start_fade();
270 game( false);
271 SDL_Delay( 150);
272 start_fade();
273 while( SDL_PollEvent( &e));
274 play_sample_background(SOUND_BACKGROUND);
276 if( M_POS_QUIT(mx,my)) {
277 stop_sample(SOUND_BACKGROUND);
278 break;
281 i++;
282 SDL_Delay(10);
285 start_fade();
286 store_screen();
287 while( is_fading()) {
288 clear_screen();
289 updatescr();
290 SDL_Delay( 10);
293 uninit_graphics();
294 uninit_joystick();
296 SDL_Quit();
297 return 0;