2 This file is part of cave9.
4 cave9 is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 cave9 is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with cave9. If not, see <http://www.gnu.org/licenses/>.
25 typedef struct Input_struct
27 bool pressed
[SDLK_LAST
];
28 enum {WELCOME
, PLAY
, PAUSE
, GAMEOVER
, QUIT
} state
;
31 void game_init(Display
* display
, Cave
* cave
, Ship
* digger
, Ship
* player
)
33 ship_init(player
, SHIP_RADIUS
);
34 ship_init(digger
, MAX_CAVE_RADIUS
);
35 cave_init(cave
,digger
);
36 display_message(display
, cave
, player
, "");
39 void control(Display
* display
, Cave
* cave
, Ship
* digger
, Ship
* player
, Input
* input
)
43 while(SDL_PollEvent(&event
)) {
46 switch(event
.key
.keysym
.sym
) {
49 input
->state
= Input::QUIT
;
52 if(input
->state
== Input::PLAY
) {
53 input
->state
= Input::PAUSE
;
54 display_message(display
, cave
, player
, "paused");
56 SDL_WM_ToggleFullScreen(display
->screen
);
61 if(input
->state
== Input::WELCOME
62 || input
->state
== Input::PAUSE
63 || input
->state
== Input::GAMEOVER
) {
64 if(input
->state
== Input::GAMEOVER
)
65 game_init(display
, cave
, digger
, player
);
67 display_message(display
, cave
, player
, "");
68 input
->state
= Input::PLAY
;
70 else if(input
->state
== Input::PLAY
) {
71 input
->state
= Input::PAUSE
;
72 display_message(display
, cave
, player
, "paused");
76 if(SDL_GetModState() & KMOD_ALT
)
77 SDL_WM_ToggleFullScreen(display
->screen
);
83 input
->pressed
[event
.key
.keysym
.sym
] = (event
.type
== SDL_KEYDOWN
);
86 input
->state
= Input::QUIT
;
91 SDL_GL_GetAttribute( SDL_GL_MULTISAMPLESAMPLES
, &aa
);
92 viewport(display
, event
.resize
.w
, event
.resize
.h
, 0,
93 display
->screen
->flags
& SDL_FULLSCREEN
, aa
);
97 display_frame(display
, cave
, player
);
105 void player_control(Ship
* player
, Input
* input
)
108 input
->pressed
[SDLK_LEFT
] ||
109 input
->pressed
[SDLK_LSHIFT
] ||
110 input
->pressed
[SDLK_LCTRL
];
112 input
->pressed
[SDLK_RIGHT
] ||
113 input
->pressed
[SDLK_RSHIFT
] ||
114 input
->pressed
[SDLK_RCTRL
];
117 void args_init(Args
* args
, int argc
, char* argv
[])
122 args
->fullscreen
= 0;
124 args
->antialiasing
= 0;
136 { 0, &help_called
, "-h", "--help" },
137 { 1, &args
->width
, "-W", "--width" },
138 { 1, &args
->height
, "-H", "--height" },
139 { 1, &args
->bpp
, "-B", "--bpp" },
140 { 0, &args
->fullscreen
, "-F", "--fullscreen" },
141 { 0, &args
->highres
, "-R", "--highres" },
142 { 1, &args
->antialiasing
, "-A", "--antialiasing" },
143 { 0, &args
->monoliths
, "-M", "--monoliths" },
144 { 1, &args
->start
, "-S", "--start" },
145 { 0, &args
->cockpit
, "-C", "--cockpit" },
146 { 0, NULL
, NULL
, NULL
}
149 for(int i
= 1; i
< argc
; ++i
) {
150 for(int opt
= 0; ; ++opt
) {
151 if(options
[opt
].val
== NULL
) {
152 fprintf(stderr
, "invalid argument %s\n", argv
[i
]);
156 if(!strcmp(argv
[i
], options
[opt
].short_name
) || !strcmp(argv
[i
], options
[opt
].long_name
)) {
158 if(options
[opt
].has_arg
) {
160 fprintf(stderr
, "argument required for %s\n", argv
[i
-1]);
163 value
= atoi(argv
[i
]);
165 *(options
[opt
].val
) = value
;
172 printf("command-line options:\n");
173 for(int opt
= 0; options
[opt
].val
; ++opt
) {
174 printf("%s or %s", options
[opt
].short_name
, options
[opt
].long_name
);
175 if(options
[opt
].has_arg
)
183 int main(int argc
, char* argv
[])
188 memset( input
.pressed
, 0, sizeof(input
.pressed
) );
196 args_init(&args
, argc
, argv
);
197 display_init(&display
, &args
);
199 player
.start
= digger
.start
= (float)args
.start
;
200 game_init(&display
, &cave
, &digger
, &player
);
201 input
.state
= Input::WELCOME
;
202 display_message(&display
, &cave
, &player
, "welcome! left+right for control. [press space]");
205 while(input
.state
!= Input::QUIT
) {
206 int t0
= SDL_GetTicks();
208 control(&display
, &cave
, &digger
, &player
, &input
);
210 switch(input
.state
) {
212 player_control(&player
, &input
);
213 ship_move(&player
, dt
);
214 digger_control(&digger
);
215 ship_move(&digger
, dt
);
216 if(collision(&cave
, &player
) <= 0) {
217 display_message(&display
, &cave
, &player
, "gameover. [press space]");
218 input
.state
= Input::GAMEOVER
;
220 cave_gen(&cave
, &digger
);
222 display_frame(&display
, &cave
, &player
);
226 case Input::GAMEOVER
:
231 int t1
= SDL_GetTicks();
232 SDL_Delay( MAX( 1, 1000/FPS
-(t1
-t0
) ) );
234 dt
= (SDL_GetTicks()-t0
)/1000.;
238 display_net_finish(&display
);
239 display_message(&display
, &cave
, &player
, "bye.");
244 // vim600:fdm=syntax:fdn=1: