1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/*
* xtrojka (c) 1994,1995,1996 Maarten Los
*
* #include "COPYRIGHT"
*
* created: 26.xi.1995
* modified: 7.ii.1996 Added update statistics
*
* This is the game module, doing everything that's not done
* in the trojka core.
*/
#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/keysym.h>
#include "game.h"
#include "sh_main.h"
#include "sh_stat.h"
#include "scores.h"
#include "menu.h"
#include "debug.h"
#include "tr_core.h"
#include "xtrojka.h"
#include "screen.h"
extern XtAppContext app_context;
extern Widget main_screen; /* the mother of all widgets */
extern Widget screen;
extern Widget form;
extern flag is_wizard;
extern GAME_STATE game_state;
extern int starting_speed;
extern int speed;
static XtIntervalId clocker;
flag ignore_clock;
flag remove_clock;
tt_command CMD_DOWN = { tc_c_blockdown, 0 , 0 };
tt_command CMD_LEFT = { tc_c_blockleft, 0 , 0 };
tt_command CMD_RIGHT = { tc_c_blockright, 0 , 0 };
tt_command CMD_DROP = { tc_c_dropblock, 0 , 0 };
tt_command CMD_SPEEDUP = { tc_c_speedup, 0 , 0 };
/*
* source
*/
void mainloop(void)
{
/*
* this is the main loop where the game is played
*/
XEvent event;
DEBUG("game.c", "mainloop")
set_state(st_idle);
for(;;) {
XtAppNextEvent(app_context, &event);
XtDispatchEvent(&event);
}
}
void resetgame(void)
{
tt_command CMD_INIT;
DEBUG("game.c", "resetgame")
ignore_clock = False;
CMD_INIT.command = tc_c_init;
CMD_INIT.param1 = starting_speed;
CMD_INIT.param2 = is_wizard;
trojka_api(&CMD_INIT);
show_score();
set_speed_item(starting_speed);
draw_field();
}
void set_state(gs)
GAME_STATE gs;
{
/*
* disables/enables menu's event- and update handlers
* depending on the game state.
*/
DEBUG("game.c", "set_state")
game_state = gs;
set_menus(gs);
if(gs == st_playing) {
remove_clock = False;
resetgame();
/*
* start the clocker
*/
clocker = XtAppAddTimeOut(app_context, tv_ticks,
(XtTimerCallbackProc)block_down_intr,
0);
}
else if(gs == st_idle) {
remove_clock = True;
draw_title();
}
}
void init_event_handlers()
{
DEBUG("game.c", "init_event_handlers")
/*
* catch moving main screen
*/
XtAddEventHandler(main_screen, StructureNotifyMask, False,
(XtEventHandler)move_mainwindow_hlr, (Opaque)NULL);
/*
* catch enter/leave/focus events
*/
XtAddEventHandler(screen,
LeaveWindowMask | EnterWindowMask,
False,
(XtEventHandler)std_window_hlr, (Opaque)NULL);
/*
* catch keyboard events
*/
XtAddEventHandler(form, KeyPressMask, False,
(XtEventHandler)key_pressed_hlr, (Opaque)NULL);
}
void block_down_intr(w, id)
Widget w;
XtIntervalId *id;
{
/*
* this function is called every n timer ticks depending
* on the game speed.
*/
int res;
DEBUG("game.c", "block_down_intr")
if(!remove_clock)
clocker=XtAppAddTimeOut(app_context, tv_ticks,
(XtTimerCallbackProc)block_down_intr, 0);
else {
remove_clock = False;
return;
}
if(ignore_clock)
return;
if((res = trojka_api(&CMD_DOWN)) == tc_res_gameover) {
gameover();
}
else if(res == tc_res_touchdown) {
show_score();
update_stat();
}
}
void key_pressed_hlr(w, unused, ke, continue_to_dispatch)
Widget w;
XtPointer unused;
XKeyPressedEvent *ke;
Boolean *continue_to_dispatch;
{
extern tt_int tv_shape;
/*
* This function handles on pressing a key, and acts
* depending on the state the game is in.
*/
Modifiers dum;
KeySym sym;
DEBUG("game.c", "game_key_pressed_hlr")
*continue_to_dispatch = TRUE;
if(ke->type != KeyPress)
return;
XtTranslateKeycode(ke->display, ke->keycode, 0, &dum, &sym);
/*
* act on 'st_playing'
*/
if(game_state == st_playing) {
switch(sym) {
/*
* Dirty debug. Get any block you want
* case XK_b:
*
* tv_shape = (tv_shape + 1) % tc_blocks;
* break;
*/
case XK_h:
case XK_Left:
if(ignore_clock)
return;
trojka_api(&CMD_LEFT);
break;
case XK_l:
case XK_Right:
if(ignore_clock)
return;
trojka_api(&CMD_RIGHT);
break;
case XK_space:
case XK_j:
case XK_Insert:
case XK_Down:
if(ignore_clock)
return;
trojka_api(&CMD_DROP);
break;
case XK_Up:
case XK_k:
trojka_api(&CMD_SPEEDUP);
speed = tv_speed;
set_speed_item(speed);
break;
default:
break;
}
}
if(game_state == st_idle) {
/* NOT NECCESARY YET */
/* MOST ARE COVERED BY ACTIONS */
}
}
void move_mainwindow_hlr(w, unused, ce, continue_to_dispatch)
Widget w;
XtPointer unused;
XConfigureEvent *ce;
Boolean *continue_to_dispatch;
{
*continue_to_dispatch = TRUE;
DEBUG("game.c", "move_mainwindow_hlr");
}
void gameover_action(w, unused, event, continue_to_dispatch)
Widget w;
XtPointer unused;
XEvent *event;
Boolean *continue_to_dispatch;
{
DEBUG("game.c", "gameover_action");
gameover();
}
void gameover(void)
{
DEBUG("game.c", "gameover")
remove_clock = True;
do_hiscores();
speed = starting_speed;
set_state(st_idle);
}
void new_game(void)
{
DEBUG("game.c", "new_game");
if(game_state != st_playing)
set_state(st_playing);
}
void std_window_hlr(w, unused, event, continue_to_dispatch)
Widget w;
XtPointer unused;
XAnyEvent *event;
Boolean *continue_to_dispatch;
{
/*
* this function is a callback function. It is called each time
* the mouse pointer enters the window
*/
DEBUG("game.c", "std_window_hlr")
*continue_to_dispatch = TRUE;
if(game_state != st_playing)
return;
switch(event->type) {
case EnterNotify:
DEBUG("\tevent_type","EnterNotify");
ignore_clock = False;
break;
case LeaveNotify:
DEBUG("\tevent_type","LeaveNotify");
ignore_clock = True;
break;
}
}
|