More menu clean-ups and simplifications
[tennix.git] / tennix.c
blobda2aabe0746e6348369d431bab21506a6fa41b02
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 btn_offset;
89 int btn_hovering = 0, btn_hovering_old = 0;
90 int slide_start;
91 bool is_clicked = false;
92 bool quit = false;
94 #ifdef MAEMO
95 sdl_flags |= SDL_FULLSCREEN;
96 #endif
98 #ifdef WIN32
99 int mb_result;
100 mb_result = DialogBox(hInstance, "CONFIG", 0, (DLGPROC)ConfigDialogProc);
102 switch (mb_result) {
103 case IDYES:
104 sdl_flags |= SDL_FULLSCREEN;
105 break;
106 case IDCANCEL:
107 return 0;
108 break;
109 default:
110 break;
112 #else
113 fprintf( stderr, "Tennix %s\n%s\n%s\n\n", VERSION, COPYRIGHT, URL);
115 bool do_help = false;
116 i = 1;
117 while (i < argc) {
118 /* A poor/lazy man's getopt */
119 #define OPTION_SET(longopt,shortopt) \
120 (strcmp(argv[i], longopt)==0 || strcmp(argv[i], shortopt)==0)
121 #define OPTION_VALUE \
122 ((i+1 < argc)?(argv[i+1]):(NULL))
123 #define OPTION_VALUE_PROCESSED \
124 (i++)
125 if (OPTION_SET("--fullscreen", "-f")) {
126 sdl_flags |= SDL_FULLSCREEN;
128 else if (OPTION_SET("--help", "-h")) {
129 do_help = true;
131 else if (OPTION_SET("--list-joysticks", "-J")) {
132 SDL_Init(SDL_INIT_JOYSTICK);
133 joystick_list();
134 return 0;
136 else if (OPTION_SET("--joystick", "-j")) {
137 SDL_Init(SDL_INIT_JOYSTICK);
138 if (OPTION_VALUE == NULL) {
139 fprintf(stderr, "Error: You need to specify the name of the joystick as parameter.\n");
140 do_help = true;
141 break;
143 if (joystick_open(OPTION_VALUE)==0) {
144 fprintf(stderr, "Warning: Cannot find joystick \"%s\" - Ignored.\n", OPTION_VALUE);
145 break;
147 OPTION_VALUE_PROCESSED;
149 else {
150 fprintf(stderr, "Ignoring unknown option: %s\n", argv[i]);
152 i++;
155 if (do_help == true) {
156 fprintf(stderr, "Usage: %s [--fullscreen|-f] [--help|-h] [--list-joysticks|-J] [--joystick|-j] [joystick name]\n", argv[0]);
157 return 0;
159 #endif
161 srand( (unsigned)time( NULL));
163 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1) {
164 fprintf( stderr, "Can't init SDL: %s\n", SDL_GetError());
165 exit( 1);
168 SDL_VideoInfo* vi = (SDL_VideoInfo*)SDL_GetVideoInfo();
169 if( (screen = SDL_SetVideoMode( WIDTH, HEIGHT, vi->vfmt->BitsPerPixel, sdl_flags)) == NULL) {
170 fprintf( stderr, "Can't set video mode: %s\n", SDL_GetError());
171 exit( 1);
174 SDL_WM_SetCaption( "Tennix " VERSION, "Tennix");
175 SDL_ShowCursor( SDL_DISABLE);
176 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, 1);
178 init_sound();
179 init_graphics();
180 init_joystick();
182 /*play_sample_background(SOUND_BACKGROUND);*/
184 i = 0;
185 ticks = SDL_GetTicks();
186 slide_start = get_image_width(GR_SIDEBAR);
187 slide = slide_start;
188 while(!quit) {
189 if (SDL_GetTicks() > ticks + 20) {
190 if (slide > 0 && slide <= slide_start) {
191 slide += slide_direction+(slide_direction*slide/(sqrt(2*slide)));
192 } else if (slide >= slide_start) {
193 slide = slide_start;
194 slide_direction = 0;
195 } else {
196 slide = 0;
197 slide_direction = 1;
199 ticks = SDL_GetTicks();
201 btn_offset = slide/7;
202 show_image(GR_SIDEBAR, WIDTH-get_image_width(GR_SIDEBAR)+slide, 0, 255);
203 show_image(GR_TENNIXLOGO, WIDTH-get_image_width(GR_SIDEBAR)-10, 20-slide, 255);
204 show_image(GR_BTN_PLAY, WIDTH-get_image_width(GR_BTN_PLAY)+slide+btn_offset+3-(3*(btn_hovering==MENU_START)), 150, 255);
205 show_image(GR_BTN_OPTIONS, WIDTH-get_image_width(GR_BTN_OPTIONS)+slide+btn_offset+3-(3*(btn_hovering==MENU_OPTIONS)), 230, 255);
206 show_image(GR_BTN_QUIT, WIDTH-get_image_width(GR_BTN_QUIT)+slide+btn_offset+3-(3*(btn_hovering==MENU_QUIT)), 350, 255);
208 SDL_PollEvent( &e);
209 if( e.type == SDL_QUIT) {
210 break;
212 keys = SDL_GetKeyState( NULL);
213 mb = SDL_GetMouseState( &mx, &my);
215 btn_hovering_old = btn_hovering;
216 btn_hovering = M_POS_DECODE(mx, my);
217 if (btn_hovering_old != btn_hovering && btn_hovering != 0) {
218 play_sample(SOUND_MOUSEOVER);
221 if( keys[SDLK_ESCAPE] || keys['q']) {
222 break;
225 if( keys['f']) {
226 SDL_WM_ToggleFullScreen( screen);
229 #ifndef MAEMO /* No mouse cursor on Maemo (we have a touchscreen) */
230 show_sprite( GR_RACKET, ((mb&SDL_BUTTON( SDL_BUTTON_LEFT))>0)+(((mb&SDL_BUTTON( SDL_BUTTON_RIGHT))>0)*2), 4, mx, my, 255);
231 #endif
233 #ifdef DEBUG
234 /* Draw the "real" mouse coordinates */
235 rectangle(mx-1, my-1, 2, 2, 255, 255, 255);
236 #endif
237 if (!(mb & SDL_BUTTON(SDL_BUTTON_LEFT)) && btn_hovering != MENU_NONE && is_clicked == true) store_screen();
238 updatescr();
240 if( mb & SDL_BUTTON( SDL_BUTTON_LEFT)) {
241 is_clicked = true;
242 } else if (is_clicked == true) {
243 play_sample(SOUND_MOUSECLICK);
244 switch (btn_hovering) {
245 case MENU_START: case MENU_OPTIONS:
246 stop_sample(SOUND_BACKGROUND);
247 start_fade();
248 game(btn_hovering == MENU_START);
249 SDL_Delay(150);
250 start_fade();
251 while( SDL_PollEvent( &e));
252 /*play_sample_background(SOUND_BACKGROUND);*/
253 slide = slide_start;
254 slide_direction = -1;
255 break;
256 case MENU_QUIT:
257 stop_sample(SOUND_BACKGROUND);
258 quit = true;
259 break;
261 is_clicked = false;
263 i++;
264 SDL_Delay(10);
267 start_fade();
268 store_screen();
269 while( is_fading()) {
270 clear_screen();
271 updatescr();
272 SDL_Delay( 10);
275 uninit_graphics();
276 uninit_joystick();
278 SDL_Quit();
279 return 0;