int GameControls::get_current_direction8() { uint16_t direction_mask = 0x0000; if(is_key_pressed(RIGHT)) direction_mask |= direction_masks[0]; if(is_key_pressed(UP)) direction_mask |= direction_masks[1]; if(is_key_pressed(LEFT)) direction_mask |= direction_masks[2]; if(is_key_pressed(DOWN)) direction_mask |= direction_masks[3]; return masks_to_directions8[direction_mask]; }
Input InputManager::get_input(){ static bool exit = false; frame_text.resize(0); SDL_Event evnt; SDL_StartTextInput(); //Will keep looping until there are no more events to process while (SDL_PollEvent(&evnt)) { switch (evnt.type) { case SDL_QUIT: return Input::EXIT_REQUEST; break; case SDL_MOUSEMOTION: set_mouse_coords((F32)evnt.motion.x, (F32)evnt.motion.y); break; case SDL_TEXTINPUT: frame_text += evnt.text.text; break; case SDL_KEYDOWN: key_press(evnt.key.keysym.sym , KEYBOARD); break; case SDL_KEYUP: key_release(evnt.key.keysym.sym , KEYBOARD); break; case SDL_MOUSEBUTTONDOWN: key_press(evnt.button.button , MOUSE); break; case SDL_MOUSEBUTTONUP: key_release(evnt.button.button , MOUSE); break; case SDL_JOYBUTTONDOWN: /* Handle Joystick Button Presses */ key_press(evnt.jbutton.button , CONTROLLER); break; case SDL_JOYBUTTONUP: /* Handle Joystick Button Presses */ key_release(evnt.jbutton.button , CONTROLLER); break; case SDL_JOYAXISMOTION: switch (evnt.jaxis.axis){ case 0: set_controller_axis_coord(&m_controller_LX_coords.x, evnt.jaxis.value); break; case 1: set_controller_axis_coord(&m_controller_LX_coords.y, evnt.jaxis.value); break; case 2: set_controller_axis_coord(&m_controller_RX_coords.x, evnt.jaxis.value); break; case 3: set_controller_axis_coord(&m_controller_RX_coords.y, evnt.jaxis.value); break; } } } SDL_StopTextInput(); if (is_key_pressed(SDLK_ESCAPE,KEYBOARD))exit = !exit; if(!exit)SDL_WarpMouseInWindow(m_window, WINDOW_WIDTH / 2.0f, WINDOW_HEIGHT / 2.0f); return Input::OK; }
static bool handle_custom_key_press (const SDL_KeyboardEvent *ev) { if (ev->type != SDL_KEYDOWN) return false; if ((is_key_pressed (SDLK_LCTRL) || is_key_pressed (SDLK_RCTRL))) { if (ev->keysym.sym == SDLK_q) { game_quit (); return true; } } return false; }
long modem_connect_callback(void) { if (is_key_pressed(KC_ESCAPE, KMod_DONTCARE)) { clear_key_pressed(KC_ESCAPE); return -7; } if (LbScreenLock() == Lb_SUCCESS) { draw_text_box(get_string(GUIStr_NetConnectnModem)); LbScreenUnlock(); } LbScreenRender(); return 0; }
static void controller_keys_update(int all_keys) { int i; for (i=0; i<QT60168_TOUCH_NUMBER_OF_SENSORS; i++ ) { // if (qt60168_is_key_pressed(i)) if (is_key_pressed(all_keys, i)) touch_states[i] = TOUCH_PRESS; else if (touch_states[i] == TOUCH_PRESS) touch_states[i] = TOUCH_RELEASE; else touch_states[i] = TOUCH_IDLE; } update_controller_state(); }
static int get_player_control_flags(int player) { GameManager & m = manager; if (m.ignore_controls) return 0; int flags = 0; if (m.control_type == CONTROL_KEYBOARD) { if (is_key_pressed(m.up)) flags |= CONTROL_UP; if (is_key_pressed(m.down)) flags |= CONTROL_DOWN; if (is_key_pressed(m.left)) flags |= CONTROL_LEFT; if (is_key_pressed(m.right)) flags |= CONTROL_RIGHT; if (is_key_pressed(m.button1)) flags |= CONTROL_BUTTON1; if (is_key_pressed(m.button2)) flags |= CONTROL_BUTTON2; if (is_key_pressed(m.button3)) flags |= CONTROL_BUTTON3; if (is_key_pressed(m.button4)) flags |= CONTROL_BUTTON4; } else { flags |= get_joystick_direction_flags(player); if (is_joystick_pressed(player, JOYSTICK_BUTTON1)) flags |= CONTROL_BUTTON1; if (is_joystick_pressed(player, JOYSTICK_BUTTON2)) flags |= CONTROL_BUTTON2; if (is_joystick_pressed(player, JOYSTICK_BUTTON3)) flags |= CONTROL_BUTTON3; if (is_joystick_pressed(player, JOYSTICK_BUTTON4)) flags |= CONTROL_BUTTON4; } return flags; }
static uint8_t get_key_flags (void) { uint8_t flags = 0; if (is_key_pressed (SDLK_LSHIFT) || is_key_pressed (SDLK_RSHIFT)) flags |= 0x10; if (is_key_pressed (SDLK_LCTRL) || is_key_pressed (SDLK_RCTRL)) flags |= 0x20; if (is_key_pressed (SDLK_LALT) || is_key_pressed (SDLK_RALT)) flags |= 0x40; return flags; }
void handle_event(XEvent *event) { /* see /usr/include/X11/X.h for definitions of X Windows event types */ // you won't want to leave these printf's in here for your finished program, // but they should help you get started and debug. static int xprev, yprev, xstart, ystart; switch (event->type) { case ButtonPress: /* button press */ printf(" ButtonPress. button %d at (%d, %d)\n", event->xbutton.button, event->xbutton.x, event->xbutton.y); switch (event->xbutton.button) { case 1: world.curp = world.pick_particle(event->xbutton.x, event->xbutton.y, view); if (!world.curp) break; if (is_key_pressed(pane.display, XK_Shift_L) || is_key_pressed(pane.display, XK_Shift_R)) { world.delete_particle(world.curp); world.curp = NULL; } else fl_set_slider_value(ui->mass_slider, world.curp->mass); // set slider to show the mass of this particle break; case 2: { Vec2 p = view.screen_to_world(event->xbutton.x, event->xbutton.y); world.curp = world.insert_particle(p[0], p[1], 0, 0, fl_get_slider_value(ui->mass_slider)); } break; case 3: // save these for later use when MotionNotify event comes xstart = xprev = event->xbutton.x; ystart = yprev = event->xbutton.y; break; } world.draw(); break; case ButtonRelease: /* button release */ printf(" ButtonRelease. button %d\n", event->xbutton.button); break; case MotionNotify: /* mouse pointer moved */ printf(" MotionNotify at (%d, %d) state=%d=(%s %s %s)\n", event->xmotion.x, event->xmotion.y, event->xmotion.state, event->xmotion.state&Button1Mask ? "button1" : ".", event->xmotion.state&Button2Mask ? "button2" : ".", event->xmotion.state&Button3Mask ? "button3" : "."); if (world.curp && ( event->xmotion.state & (Button1Mask|Button2Mask))) { /* printf("moving particle %d\n", world.curp-world.pt); */ world.curp->pos = view.screen_to_world(event->xmotion.x, event->xmotion.y); } else if (event->xmotion.state & Button3Mask) { if (is_key_pressed(pane.display, XK_Shift_L) || is_key_pressed(pane.display, XK_Shift_R)) { float s = 1. + (event->xmotion.x - xprev)/100.; /* printf("s=%g\n", s); */ Vec2 pos = view.screen_to_world(xstart, ystart); // scale view around the point where user moused down glTranslatef(pos[0], pos[1], 0.); glScalef(s, s, 1.); glTranslatef(-pos[0], -pos[1], 0.); } else { float t = view.viewport.width*view.relscale(); glTranslatef((event->xmotion.x - xprev)/t, -(event->xmotion.y - yprev)/t, 0.); } view.update(); xprev = event->xmotion.x; yprev = event->xmotion.y; } world.draw(); break; case EnterNotify: /* mouse pointer entered window */ printf(" EnterNotify.\n"); break; case LeaveNotify: /* mouse pointer left window */ printf(" LeaveNotify.\n"); break; case KeyPress: /* key pressed */ case KeyRelease: /* key released */ { char buf[10]; int n; KeySym keysym; printf(event->type==KeyPress ? " KeyPress." : " KeyRelease."); n = XLookupString(&event->xkey, buf, sizeof buf - 1, &keysym, 0); buf[n] = 0; /* print key as ascii and in hexadecimal */ /* see /usr/include/X11/keysymdef.h for key codes */ printf(" key=(%c)=0x%x\n", keysym, keysym); } break; case Expose: /* window just uncovered */ printf(" Expose.\n"); world.draw(); break; case VisibilityNotify: /* window occluded or exposed */ printf(" VisibilityNotify.\n"); break; default: printf(" unknown event type.\n"); break; } }
void GameUpdate() { game* g = &g_game; if (is_key_pressed(KEY_RESET)) { memset(g->_map, 0, sizeof(g->_map)); g->_entities.free(); g->_player = 0; g->_cam_pos = vec2(MAP_WIDTH * 0.5f, 8.5f); g->_target_cam_y = 8.5f; g->_diff = 1; g->_diff_dmg = 1; g->_spawn_time = 800; g->_spawn_count = 6; g->_wave_incoming = true; g->_plr_dmg = 1; GameInit(); g_title = true; } if (g_title) { set_camera(vec2(), 10.0f); float ratio = g_WinSize.y / (float)g_WinSize.x; vec2 orig(-10.0f, -10.0f * ratio); draw_string(vec2(0.0f, -4.75), 0.15f, TEXT_CENTRE, colour(0.5f, 0.5f, 1.0f, 1.0f), "Tunnel Defense"); float y = -3.25f; draw_string(vec2(0.0f, y), 0.05f, TEXT_CENTRE, colour(0.3f, 0.3f, 0.6f, 1.0f), "Originally made for LD29 - post competition version"); y += 0.5f; y += 0.5f; draw_string(vec2(0.0f, y), 0.05f, TEXT_CENTRE, colour(0.6f, 0.3f, 0.3f, 1.0f), "Dig for your life, the creeps are coming and the"); y += 0.5f; draw_string(vec2(0.0f, y), 0.05f, TEXT_CENTRE, colour(0.6f, 0.3f, 0.3f, 1.0f), "only safe place is underground! Collect resources"); y += 0.5f; draw_string(vec2(0.0f, y), 0.05f, TEXT_CENTRE, colour(0.6f, 0.3f, 0.3f, 1.0f), "and build turrets to protect yourself."); y += 0.5f; y += 0.25f; float r = 5.0f; draw_string(vec2(-r, y), 0.05f, TEXT_LEFT, colour(0.6f, 0.6f, 0.6f, 1.0f), "\001\002\003\004"); draw_string(vec2(r, y), 0.05f, TEXT_RIGHT, colour(0.3f, 0.6f, 0.3f, 1.0f), "Move + Aim"); y += 0.5f; draw_string(vec2(-r, y), 0.05f, TEXT_LEFT, colour(0.6f, 0.6f, 0.6f, 1.0f), "%c", g_LocKeyZ); draw_string(vec2(r, y), 0.05f, TEXT_RIGHT, colour(0.3f, 0.6f, 0.3f, 1.0f), "Jump"); y += 0.5f; draw_string(vec2(-r, y), 0.05f, TEXT_LEFT, colour(0.6f, 0.6f, 0.6f, 1.0f), "%c", g_LocKeyX); draw_string(vec2(r, y), 0.05f, TEXT_RIGHT, colour(0.3f, 0.6f, 0.3f, 1.0f), "Dig block / shoot"); y += 0.5f; draw_string(vec2(-r, y), 0.05f, TEXT_LEFT, colour(0.6f, 0.6f, 0.6f, 1.0f), "%c", g_LocKeyC); draw_string(vec2(r, y), 0.05f, TEXT_RIGHT, colour(0.3f, 0.6f, 0.3f, 1.0f), "Build / upgrade turret"); y += 0.5f; y += 0.25f; draw_string(vec2(0.0f, y), 0.05f, TEXT_CENTRE, colour(0.15f, 0.3f, 0.15f, 1.0f), "Alternate controls - %c%c%c%c, %c, %c, %c", g_LocKeyW, g_LocKeyA, g_LocKeyS, g_LocKeyD, g_LocKeyI, g_LocKeyO, g_LocKeyP); y += 0.5f; y += 0.25f; draw_string(vec2(0.0f, y), 0.05f, TEXT_CENTRE, colour(0.3f, 0.15f, 0.3f, 1.0f), "Building / upgrading a turret costs 3 metal."); y += 0.5f; draw_string(vec2(0.0f, y), 0.05f, TEXT_CENTRE, colour(0.3f, 0.15f, 0.3f, 1.0f), "Building a turret increases your shot power."); y += 0.5f; y += 0.25f; draw_string(vec2(0.0f, y), 0.05f, TEXT_CENTRE, colour(0.5f, 0.5f, 0.5f, 1.0f), "press SPACE to START"); y+= 0.5f; y += 0.5f; draw_string(vec2(0.0f, y), 0.035f, TEXT_CENTRE, colour(0.15f, 0.15f, 0.15f, 1.0f), "by Stephen Cakebread @quantumrain"); if (is_key_pressed(KEY_FIRE) || is_key_pressed(KEY_ALT_FIRE)) g_title = false; return; } if (player* p = g->_player) { vec2 target_cam_pos = vec2(MAP_WIDTH * 0.5f, p->centre().y); float dy = target_cam_pos.y - g->_target_cam_y; float hyst = 1.25f; if (fabsf(dy) > hyst) { if (dy > 0.0f) g->_target_cam_y += dy - hyst; else g->_target_cam_y += dy + hyst; } target_cam_pos.y = g->_target_cam_y; update_search(g, to_ivec2(p->centre())); g->_cam_pos = lerp(g->_cam_pos, target_cam_pos, 0.2f); g->_plr_dmg = 1; for(uint32_t i = 0; i < g->_entities.size(); i++) { entity* e = g->_entities[i]; if (!(e->_flags & entity::FLAG_DESTROYED)) { if (e->_type == ET_TURRET) g->_plr_dmg++; } } } set_camera(g->_cam_pos, 15.0f); if (g->_player) { if (--g->_spawn_time <= 0) { if (g->_wave_incoming) { g->_spawn_count = 4 + 4 * (int)g->_diff; g->_diff_dmg = (int)(2.5f * (1 + g->_diff)) - 1; } g->_wave_incoming = false; vec2 pos(g_game_rand.frand(1.0f, MAP_WIDTH - 1.0f), 2.0f); if (bug* e = spawn_entity(&g_game, new bug(), pos)) { e->_max_damage = g->_diff_dmg; if (g_game_rand.rand(0, 5) == 0) e->_max_damage += g_game_rand.rand(0, g->_diff_dmg); if (g_game_rand.rand(0, 15) == 0) e->_max_damage += g_game_rand.rand(0, g->_diff_dmg); if (g_game_rand.rand(0, 30) == 0) e->_max_damage += g_game_rand.rand(0, g->_diff_dmg); } g->_spawn_time = clamp(60 - ((g->_diff * 5) / 4), 10, 60); if (--g->_spawn_count <= 0){ g->_diff += 1 + (g->_diff / 5); g->_spawn_time = 600; g->_wave_incoming = true; } } } tick_entities(g); purge_entities(g); update_particles(g); colour sky0(0.4f, 0.7f, 1.0f, 1.0f); colour sky1(0.45f, 0.75f, 1.0f, 1.0f); colour sky2(0.3f, 0.6f, 0.9f, 1.0f); colour sky3(0.35f, 0.65f, 0.9f, 1.0f); sky0 *= colour(0.65f, 0.0f); sky1 *= colour(0.65f, 0.0f); sky2 *= colour(0.35f, 1.0f); sky3 *= colour(0.35f, 1.0f); float sky_space = 4.5f; float sky_top = 4.75f; float sky_y = 10.0f; float sky_yf = 10.5f; float sky_yb = 11.0f; for(int j = 0; j < MAP_HEIGHT; j++) { colour bk_colour(get_master_colour(j) * colour(0.2f, 1.0f)); for(int i = 0; i < MAP_WIDTH; i++) { int t = g->get_tile(i, j); if (t == TT_EMPTY || t == TT_TURRET) { int hash = ((i * 7) ^ (j * 3)) + (i + j); hash ^= ((hash >> 3) * 9); int tile_num = 132 + (hash % 4); draw_tile(vec2((float)i, (float)j), vec2(i + 1.0f, j + 1.0f), bk_colour, tile_num, 0); } } }
void GameProcess() { if (game.status == STATUS_PLAYING) { int c_shoot_restore_delay = (6 + 2*game.stage_level); int c_shoot_restore_period = (game.stage_level < 2) ? (3 + game.stage_level) : 5; // shoot if ( ( (game.shoot_keypressed) || (is_key_pressed(RS_ATTACK_KEY_MASK)) ) && (game.ammo>0) ) { game.shoot_delay ++; if (game.shoot_delay > GAME_SHOOT_PERIOD) { // if (game.ammo > 0) { game.shoot_restore_delay = 0; game.ammo--; soundbuf_play(&game.sound_shoot, 0); game_obj_add( game_obj( OBJ_BULLET, 0, 0, 0, game.player_x+5, game.player_y, 0, 0.0) ); // }; game.shoot_delay = 1; // -= GAME_SHOOT_PERIOD; game.shoot_keypressed = 0; }; } else { if (game.ammo < GAME_AMMO_MAX) { game.shoot_restore_delay++; if (game.shoot_restore_delay > c_shoot_restore_delay) { game.shoot_delay++; if (game.shoot_delay > c_shoot_restore_period) { game.ammo++; game.shoot_delay -= c_shoot_restore_period; }; }; }; }; int speed = 4; int bullet_speed = 11; int red_bullet_speed = 8; int rock_speed = 6; game.player_x += speed * ( is_key_pressed(RS_ARROW_RIGHT_MASK) - is_key_pressed(RS_ARROW_LEFT_MASK) ); game.player_y += speed * ( is_key_pressed(RS_ARROW_DOWN_MASK) - is_key_pressed(RS_ARROW_UP_MASK) ); game.player_x = rs_clamp_i(game.player_x, 15, GAME_WIDTH - 45); game.player_y = rs_clamp_i(game.player_y, 15, GAME_HEIGHT - 45); game.tz += 1; int c_rocktimer_const = (game.stage_level < 4) ? (9 - 2*game.stage_level) : 4; int c_rocktimer_var = (game.stage_level < 6) ? (16 - 2*game.stage_level) : 6; game.stage_timer++; if (game.stage == 0) { // level start next_stage_after_sec(3); } else if (game.stage == 1) { // game.stage = 4; // rocks next_rock_timer--; if (next_rock_timer < 1) { next_rock_timer = c_rocktimer_const + rs_rand()%c_rocktimer_var; //game_obj_add( game_obj( ((rs_rand() % 512) > 256) ? OBJ_ROCK : OBJ_MINIROCK, 0, rs_rand() % ROCKS_COUNT , 32, GAME_WIDTH + 100, 30 + rs_rand()%(GAME_HEIGHT-60), 0, 0.0 ) ); int flagsin = 0; if ( game.stage_level > 4 ) { if ( rs_rand()%1024 < (16*game.stage_level) ) { flagsin = OBJ_FLAG_SIN; }; }; game_obj_add( game_obj( OBJ_ROCK, OBJ_FLAG_ENEMY | flagsin, rs_rand() % ROCKS_COUNT , game.tex_rocks[0].w/2, GAME_WIDTH + 50, 30 + rs_rand()%(GAME_HEIGHT-90), 0, 0.0 ) ); }; next_stage_after_sec(12); } if (game.stage == 2) { BIT_SET (game.flags, GAME_FLAG_INSTRUCTIONS_PASSED); next_stage_after_sec(4); } else if (game.stage == 3) { // rocks next_rock_timer--; if (next_rock_timer < 1) { next_rock_timer = c_rocktimer_const + 1 + rs_rand()%c_rocktimer_var; //game_obj_add( game_obj( ((rs_rand() % 512) > 256) ? OBJ_ROCK : OBJ_MINIROCK, 0, rs_rand() % ROCKS_COUNT , 32, GAME_WIDTH + 100, 30 + rs_rand()%(GAME_HEIGHT-60), 0, 0.0 ) ); int flagsin = 0; if ( game.stage_level > 2 ) { if ( rs_rand()%1024 < (16*game.stage_level) ) { flagsin = OBJ_FLAG_SIN; }; }; game_obj_add( game_obj( OBJ_MINIROCK, OBJ_FLAG_ENEMY|flagsin, rs_rand() % ROCKS_COUNT , game.tex_minirocks[0].w/2, GAME_WIDTH + 50, 30 + rs_rand()%(GAME_HEIGHT-90), 0, 0.0 ) ); }; next_stage_after_sec(16); } else if (game.stage == 4) { next_stage_after_sec(4); } else if (game.stage == 5) { // rocks next_rock_timer--; if (next_rock_timer < 1) { next_rock_timer = 5; //game_obj_add( game_obj( ((rs_rand() % 512) > 256) ? OBJ_ROCK : OBJ_MINIROCK, 0, rs_rand() % ROCKS_COUNT , 32, GAME_WIDTH + 100, 30 + rs_rand()%(GAME_HEIGHT-60), 0, 0.0 ) ); game_obj_add( game_obj( OBJ_MINIROCK, OBJ_FLAG_ENEMY | OBJ_FLAG_SIN, rs_rand() % ROCKS_COUNT , game.tex_minirocks[0].w/2, GAME_WIDTH + 50, GAME_HEIGHT/8, 0, 0.0 ) ); }; next_stage_after_sec(6); } else if (game.stage == 6) { // mix rocks next_rock_timer--; if (next_rock_timer < 1) { next_rock_timer = c_rocktimer_const + rs_rand()%(c_rocktimer_var-3); int flagsin = 0; if ( game.stage_level > 3 ) { if ( rs_rand()%1024 < (16*game.stage_level) ) { flagsin = OBJ_FLAG_SIN; }; }; //game_obj_add( game_obj( ((rs_rand() % 512) > 256) ? OBJ_ROCK : OBJ_MINIROCK, 0, rs_rand() % ROCKS_COUNT , 32, GAME_WIDTH + 100, 30 + rs_rand()%(GAME_HEIGHT-60), 0, 0.0 ) ); game_obj_add( game_obj( rs_rand()%1024 < 768 ? OBJ_MINIROCK : OBJ_ROCK, OBJ_FLAG_ENEMY | flagsin, rs_rand() % ROCKS_COUNT , rs_rand()%1024 < 768 ? game.tex_minirocks[0].w/2 : game.tex_rocks[0].w/2, GAME_WIDTH + 100, 30 + rs_rand()%(GAME_HEIGHT-90), 0, 0.0 ) ); }; next_stage_after_sec(10); } else if (game.stage == 7) { if (game.stage_timer > 3*25) { next_stage_now(); BIT_CLEAR(game.flags, GAME_FLAG_BOSS_DESTROYED); game_obj_add( game_obj( OBJ_TURRET, OBJ_FLAG_ENEMY | OBJ_FLAG_BOSS, 60, game.tex_rocks[0].w/2, GAME_WIDTH+60, GAME_HEIGHT/2, 0, 0.0 ) ); }; } else if (game.stage == 8) { if ( IS_BIT_SET(game.flags, GAME_FLAG_BOSS_DESTROYED) ) { next_stage_now(); }; } else if (game.stage == 9) { next_stage_after_sec(2); } else if (game.stage == 10) { /* game.status = STATUS_MENU; menu_open( MENU_LEVEL_PASSED ); level_passed_score_str[1] = '0' + (game.score / 100) % 10; level_passed_score_str[2] = '0' + (game.score / 10) % 10; level_passed_score_str[3] = '0' + (game.score / 1) % 10; */ game.stage_level++; game.stage = 0; game.stage_timer = 0; }; int i, j; game_obj_t *obj; for (i = 0; i < game.objs_count; i++) { obj = &(game.objs[i]); if (obj->obj_type == OBJ_BULLET) { obj->x += bullet_speed; if (obj->x > GAME_WIDTH) { // destroy object game_obj_remove(i); i--; continue; }; for (j = 0; j < game.objs_count; j++) { if (IS_BIT_SET(game.objs[j].flags, OBJ_FLAG_ENEMY)) { if (check_collision(i, j)) { if (IS_BIT_SET( game.objs[j].flags, OBJ_FLAG_BOSS)) { game.objs[j].tag--; if (game.objs[j].tag < 1) { BIT_SET( game.objs[j].flags, OBJ_FLAG_DESTROYED ); BIT_SET( game.flags, GAME_FLAG_BOSS_DESTROYED ); game.score += 50; }; } else { BIT_SET( game.objs[j].flags, OBJ_FLAG_DESTROYED ); game.score += game.objs[j].obj_type == OBJ_ROCK ? 2 : 3; }; game_obj_remove(i); i--; break; // continue parent loop }; }; }; } else if (obj->obj_type == OBJ_RED_BULLET) { obj->x -= red_bullet_speed; if (obj->x < 4) { // destroy object game_obj_remove(i); i--; continue; }; if (check_collision_with_player(i)) { player_hit(); game_obj_remove(i); i--; continue; }; } else if (obj->obj_type == OBJ_EXPLOSION) { obj->t++; if (obj->t >= EXPLOSIONS_COUNT) { game_obj_remove(i); i--; continue; }; } else if (obj->obj_type == OBJ_HUGE_EXPLOSION) { obj->t++; if (obj->t >= HUGE_EXPLOSIONS_COUNT) { game_obj_remove(i); i--; continue; }; } else if ( (obj->obj_type == OBJ_ROCK) || (obj->obj_type == OBJ_MINIROCK) ) { obj->x -= rock_speed; if (obj->x < - obj->radius * 2) { game_obj_remove(i); i--; continue; }; if ( IS_BIT_SET(obj->flags, OBJ_FLAG_SIN) ) { obj->f += 0.2; obj->y += 7.0 * sin(obj->f); }; if ( check_collision_with_player(i) ) { player_hit(); game_obj_remove(i); i--; continue; }; } else if ( obj->obj_type == OBJ_TURRET ) { if (obj->x > GAME_WIDTH*3/4) { obj->x -= 2; } else { obj->f += 0.03; obj->y = GAME_HEIGHT * ( 0.5 + 0.3*sin(obj->f) ); }; if ( obj->x < GAME_WIDTH*4/5 ) { // turret shoot obj->t--; if (obj->t < 1) { soundbuf_play(&game.sound_turret_shoot, 0); game_obj_add( game_obj( OBJ_RED_BULLET, 0, 0, 3, obj->x - 5, obj->y, 0, 0) ); int c_const = (game.stage_level < 4) ? (10 - 2*game.stage_level) : 3; int c_var = (game.stage_level < 6) ? (20 - 3*game.stage_level) : 3; obj->t = c_const + rs_rand() % c_var; if ( (rs_rand()%1024) < 80 ) { obj->t += 18; }; }; }; }; }; for (i = 0; i < game.objs_count; i++) { if ( IS_BIT_SET( game.objs[i].flags, OBJ_FLAG_DESTROYED ) ) { if (game.objs[i].obj_type == OBJ_TURRET) { soundbuf_play( &game.sound_huge_explosion, 0 ); game_obj_add( game_obj( OBJ_HUGE_EXPLOSION, 0, 0, HUGE_EXPLOSION_RADIUS, game.objs[i].x, game.objs[i].y, 0, 0.0 ) ); } else { soundbuf_play( &game.sound_explosions[ rs_rand() % SOUND_EXPLOSIONS_COUNT ], 0 ); game_obj_add( game_obj( OBJ_EXPLOSION, 0, 0, EXPLOSION_RADIUS, game.objs[i].x, game.objs[i].y, 0, 0.0 ) ); }; game_obj_remove(i); i--; }; }; }; game_draw(); if ( (game.status == STATUS_MENU) && (game.menu_index != MENU_PAUSE) ) { soundbuf_loop_check( &game.sound_music ); }; }
/*! * @brief Watchdog main routine * Run a simple application which enables watchdog, then * continuously refreshes the watchdog to prevent CPU reset * Upon SW1 button push, the watchdog will expire after * approximately 2 seconds and issue reset */ void main(void) { // Configure watchdog. const wdog_user_config_t wdogConfig = { .timeoutValue = 2048U,// Watchdog overflow time is about 2s .windowValue = 0, // Watchdog window value, 0-disable window function .clockPrescalerValue = kWdogClockPrescalerValueDevide1, // Watchdog clock prescaler .updateRegisterEnable = true, // Update register enabled .clockSource = kClockWdogSrcLpoClk, // Watchdog clock source is LPO 1KHz .workInWaitModeEnable = true, // Enable watchdog in wait mode .workInStopModeEnable = true, // Enable watchdog in stop mode .workInDebugModeEnable = false,// Disable watchdog in debug mode }; // Init hardware. hardware_init(); // Init OSA layer. OSA_Init(); // Call this function to initialize the console UART. This function // enables the use of STDIO functions (printf, scanf, etc.) dbg_uart_init(); // Init pinsfor switch and led. GPIO_DRV_Init(switchPins, ledPins); // Initialize wdog before the WDOG timer has a chance //to reset the device // Turn LED1 on; LED1_ON; WDOG_DRV_Init(&wdogConfig); // If not wdog reset, clear reset count if (!(RCM->SRS0 & RCM_SRS0_WDOG_MASK)) { WDOG_DRV_ClearResetCount(); printf("\r\n WDOG example \r\n"); } // Check if WDOG reset occurred , disable WDOG and turn off LED1. if (WDOG_DRV_GetResetCount()) { printf("\r\n WDOG reset count %ld",WDOG_DRV_GetResetCount()); } printf("\r\n Press SW1 to expire watchdog "); // Continue to run in loop to refresh watchdog until SW1 is pushed while (1) { // Check for SW1 button push.Pin is grounded when button is pushed. if (0 != is_key_pressed()) { while (1) { // Button has been pushed,blink LED // showing that the watchdog is about to expire. LED1_TOGGLE; OSA_TimeDelay(BLINK_TIME); } } // Restart the watchdog so it doesn't reset. WDOG_DRV_Refresh(); OSA_TimeDelay(100u); } }