static size_t show_memory(t_block *block, char *type) { size_t size; size = 0; ft_putstr(type); if (!block) { ft_putstr("is empty\n"); return (0); } ft_putstr("0x"); ft_putnbr_base((size_t)block->addr, 16); write(1, "\n", 1); while (block) { size += block->size; show_block(block); block = block->next; } return (size); }
bool loop(uint32_t& score) { score = 0; const uint8_t max_brightness = options::get_max_brightness(); const bool accelerometer_enabled = options::is_accelerometer_enabled(); block fig; uint8_t block_x = invalid_block_pos, block_y = invalid_block_pos; color::rgb block_color; uint8_t new_x; bool need_redraw = false; uint8_t game_counter = 0; uint8_t game_difficulty; uint8_t multiplier; uint8_t max_block_id; accelerometer::speed_state speed_state(5); accelerometer::direction last_direction = accelerometer::direction_none; init_difficulty(score, multiplier, game_difficulty, max_block_id); while(true) { timer::wait_for_interrupt(); if(block_x == invalid_block_pos) { fig = create_block(max_block_id); game::get_random_color(block_color, max_brightness); block_y = ws2812_matrix::height - 1; bool have_space = get_new_block_pos(fig, block_x); show_block(block_x, block_y, fig, block_color); ws2812_matrix::show(); if(!have_space) break; //No more space } if(++game_counter == game_difficulty) { game_counter = 0; hide_block(block_x, block_y, fig); if(block_y == fig.height - 1 || intersects(fig, block_x, block_y - 1)) { show_block(block_x, block_y, fig, block_color); block_y = invalid_block_pos; block_x = invalid_block_pos; //Force new block creation //Re-initializes difficulty in case of fast drop check_filled_rows(score, multiplier, game_difficulty, max_block_id, max_brightness); continue; } --block_y; need_redraw = true; } auto button_up_status = buttons::get_button_status(buttons::button_up); auto button_down_status = buttons::get_button_status(buttons::button_down); if(button_up_status != buttons::button_status_not_pressed && button_down_status != buttons::button_status_not_pressed) { button_up_status = buttons::button_status_not_pressed; button_down_status = buttons::button_status_not_pressed; hide_block(block_x, block_y, fig); if(!game::pause_with_screen_backup()) { number_display::output_number(score); return true; } show_block(block_x, block_y, fig, block_color); number_display::output_number(score); need_redraw = true; } new_x = block_x; if(button_up_status == buttons::button_status_pressed) { hide_block(block_x, block_y, fig); const block new_fig = rotate_block(fig); uint8_t move_left = 0; bool rotated = true; if(block_y < new_fig.height - 1) { rotated = false; } else { if(new_fig.width + block_x > ws2812_matrix::width) move_left = block_x + new_fig.width - ws2812_matrix::width; while(intersects(new_fig, block_x - move_left, block_y)) { ++move_left; if(move_left == new_fig.width || block_x < move_left) { rotated = false; break; } } } if(rotated) { block_x -= move_left; new_x = block_x; fig = new_fig; need_redraw = true; } else { show_block(block_x, block_y, fig, block_color); } } switch(move_helper::process_speed(&speed_state, nullptr, accelerometer_enabled)) { case move_direction_left: if(new_x < ws2812_matrix::width - fig.width) ++new_x; break; case move_direction_right: if(new_x) --new_x; break; default: break; } if(accelerometer_enabled) { if(game_difficulty != fast_fall_game_difficulty) { auto accel_direction = accelerometer::get_exclusive_direction(); if(accel_direction == accelerometer::direction_down && last_direction == accelerometer::direction_none) { game_difficulty = fast_fall_game_difficulty; game_counter = game_difficulty - 1; } last_direction = accel_direction; } } else { if(button_down_status == buttons::button_status_pressed && game_difficulty != fast_fall_game_difficulty) { game_difficulty = fast_fall_game_difficulty; game_counter = game_difficulty - 1; } } if(new_x != block_x) { hide_block(block_x, block_y, fig); if(!intersects(fig, new_x, block_y)) { block_x = new_x; need_redraw = true; } else { show_block(block_x, block_y, fig, block_color); } } if(need_redraw) { show_block(block_x, block_y, fig, block_color); ws2812_matrix::show(); need_redraw = false; } } return false; }
void main() { int driver=0,mode=VESA_1024x768x8bit,paused=0; char *p; block bl; int key,n=1; long t0; initgraph(&driver,&mode,""); randomize(); memset(map_check,0,sizeof(map_check)); draw_map(); t0=time(NULL); init_block(&bl); itoa(score,ss,10); outtextxy(X0+470,Y0+320,ss); outtextxy(X0+397,Y0+220,"Press spacebar to pause"); outtextxy(X0+397,Y0+270,"Press spacebar agian to continue"); while(!stop) { show_block(bl); if(check_xy(bl)==3) { bl.y--; init_block(&bl); check_gameover(); show_block(bl); } while(((long)time(NULL)-t0)<=n*dtime) { if(bioskey(1)!=0) { key=bioskey(0); clear_block(bl); switch(key) { case UP:bl.rotate++;break; case LEFT:bl.x--;break; case RIGHT:bl.x++;break; case DOWN:bl.y++;break; case ESC:stop=1;break; case SPACE:show_block(bl);paused=1;break; default:break; } while(paused){ if(bioskey(1)!=0) { if(bioskey(0)==SPACE) { clear_block(bl); paused=0; break; } } } if(bl.rotate>3 || bl.rotate<0) bl.rotate=0; if(check_xy(bl)==1) bl.x++; if(check_xy(bl)==2) bl.x--; if(check_xy(bl)==3) { bl.y--; check_gameover(); show_block(bl); while(cover()) { delete_last(cover()); score+=100; setfillstyle(SOLID_FILL,BLACK); bar(X0+460,Y0+300,X0+520,Y0+340); itoa(score,ss,10); setcolor(WHITE); outtextxy(X0+470,Y0+320,ss); } init_block(&bl); check_gameover(); } if(check_collision(bl)) { if(key==UP) bl.rotate--; if(key==LEFT) bl.x++; if(key==RIGHT) bl.x--; if(key==DOWN) { bl.y--; check_gameover(); show_block(bl); while(cover()) { delete_last(cover()); score+=100; setfillstyle(SOLID_FILL,BLACK); bar(X0+460,Y0+300,X0+520,Y0+340); itoa(score,ss,10); setcolor(WHITE); outtextxy(X0+470,Y0+320,ss); } init_block(&bl); check_gameover(); } } check_gameover(); show_block(bl); } } clear_block(bl); bl.y++; if(check_xy(bl)==3 || check_collision(bl)) { bl.y--; check_gameover(); show_block(bl); while(cover()) { delete_last(cover()); score+=100; setfillstyle(SOLID_FILL,BLACK); bar(X0+460,Y0+300,X0+520,Y0+340); itoa(score,ss,10); setcolor(WHITE); outtextxy(X0+470,Y0+320,ss); } init_block(&bl); check_gameover(); } n++; } getchar(); closegraph(); }