Changeset a35b458 in mainline for uspace/dist/src/c/demos/tetris
- Timestamp:
- 2018-03-02T20:10:49Z (8 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- Location:
- uspace/dist/src/c/demos/tetris
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/dist/src/c/demos/tetris/scores.c
r3061bc1 ra35b458 99 99 { 100 100 int i; 101 101 102 102 clear_screen(); 103 103 moveto(10, 0); 104 104 printf("\tRank \tLevel \tName\t points\n"); 105 105 printf("\t========================================================\n"); 106 106 107 107 for (i = 0; i < NUMSPOTS - 1; i++) 108 108 printf("\t%6d %6d %-16s %20d\n", 109 109 i + 1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score); 110 110 111 111 if (!firstgame) { 112 112 printf("\t========================================================\n"); … … 114 114 scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score); 115 115 } 116 116 117 117 printf("\n\n\n\n\tPress any key to return to main menu."); 118 118 getchar(); … … 126 126 cons_event_t ev; 127 127 kbd_event_t *kev; 128 128 129 129 clear_screen(); 130 130 moveto(10, 10); … … 134 134 i = 6; 135 135 off = 6; 136 136 137 137 moveto(10 , 28); 138 138 printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME-i, 139 139 "........................................"); 140 140 141 141 while (1) { 142 142 console_flush(console); 143 143 if (!console_get_event(console, &ev)) 144 144 exit(1); 145 145 146 146 if (ev.type != CEV_KEY || ev.ev.key.type == KEY_RELEASE) 147 147 continue; 148 148 149 149 kev = &ev.ev.key; 150 150 151 151 if (kev->key == KC_ENTER || kev->key == KC_NENTER) 152 152 break; 153 153 154 154 if (kev->key == KC_BACKSPACE) { 155 155 if (i > 0) { 156 156 wchar_t uc; 157 157 158 158 --i; 159 159 while (off > 0) { … … 165 165 break; 166 166 } 167 167 168 168 scores[NUMSPOTS - 1].hs_name[off] = '\0'; 169 169 } … … 177 177 } 178 178 } 179 179 180 180 moveto(10, 28); 181 181 printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i, 182 182 "........................................"); 183 183 } 184 184 185 185 scores[NUMSPOTS - 1].hs_score = score; 186 186 scores[NUMSPOTS - 1].hs_level = level; 187 187 188 188 i = NUMSPOTS - 1; 189 189 while ((i > 0) && (scores[i - 1].hs_score < score)) 190 190 i--; 191 191 192 192 for (j = NUMSPOTS - 2; j > i; j--) 193 193 copyhiscore(j, j-1); 194 194 195 195 copyhiscore(i, NUMSPOTS - 1); 196 196 } … … 236 236 return; 237 237 } 238 238 239 239 cnt = fwrite(scores, sizeof(struct highscore), NUMSPOTS, f); 240 240 rc = fclose(f); -
uspace/dist/src/c/demos/tetris/screen.c
r3061bc1 ra35b458 148 148 sysarg_t ccap; 149 149 errno_t rc = console_get_color_cap(console, &ccap); 150 150 151 151 if (rc != EOK) 152 152 return false; 153 153 154 154 return ((ccap & CONSOLE_CAP_RGB) == CONSOLE_CAP_RGB); 155 155 } … … 161 161 { 162 162 winsize_t ws; 163 163 164 164 Rows = 0; 165 165 Cols = 0; 166 166 167 167 if (get_display_size(&ws) == 0) { 168 168 Rows = ws.ws_row; … … 171 171 172 172 use_color = get_display_color_sup(); 173 173 174 174 if ((Rows < MINROWS) || (Cols < MINCOLS)) { 175 175 char smallscr[55]; 176 176 177 177 snprintf(smallscr, sizeof(smallscr), 178 178 "the screen is too small (must be at least %dx%d)", … … 181 181 } 182 182 isset = 1; 183 183 184 184 scr_clear(); 185 185 } … … 197 197 if (isset) 198 198 scr_end(); 199 199 200 200 errx(1, "aborting: %s", why); 201 201 } … … 213 213 int j; 214 214 int ccol; 215 215 216 216 /* Always leave cursor after last displayed point */ 217 217 curscreen[D_LAST * B_COLS - 1] = -1; 218 218 219 219 if (score != curscore) { 220 220 moveto(0, 0); … … 222 222 curscore = score; 223 223 } 224 224 225 225 /* Draw preview of next pattern */ 226 226 if ((showpreview) && (nextshape != lastshape)) { … … 228 228 static int r = 5, c = 2; 229 229 int tr, tc, t; 230 230 231 231 lastshape = nextshape; 232 232 233 233 /* Clean */ 234 234 resume_normal(); … … 241 241 moveto(r + 2, c - 1); 242 242 putstr(" "); 243 243 244 244 moveto(r - 3, c - 2); 245 245 putstr("Next shape:"); 246 246 247 247 /* Draw */ 248 248 start_standout(nextshape->color); … … 252 252 t = c + r * B_COLS; 253 253 t += nextshape->off[i]; 254 254 255 255 tr = t / B_COLS; 256 256 tc = t % B_COLS; 257 257 258 258 moveto(tr, 2*tc); 259 259 putstr(" "); … … 261 261 resume_normal(); 262 262 } 263 263 264 264 bp = &board[D_FIRST * B_COLS]; 265 265 sp = &curscreen[D_FIRST * B_COLS]; … … 269 269 if (*sp == (so = *bp)) 270 270 continue; 271 271 272 272 *sp = so; 273 273 if (i != ccol) { … … 278 278 moveto(RTOD(j), CTOD(i)); 279 279 } 280 280 281 281 if (so != cur_so) { 282 282 if (so) … … 287 287 } 288 288 putstr(" "); 289 289 290 290 ccol = i + 1; 291 291 /* … … 297 297 * the next cell is a different color. 298 298 */ 299 299 300 300 if ((i > STOP) || (sp[1] != bp[1]) || (so != bp[1])) 301 301 continue; 302 302 303 303 if (sp[2] != bp[2]) 304 304 sp[1] = -1; … … 309 309 } 310 310 } 311 311 312 312 if (cur_so) 313 313 resume_normal(); 314 314 315 315 console_flush(console); 316 316 } … … 323 323 { 324 324 int l = str_size(s); 325 325 326 326 moveto(Rows - 2, ((Cols - l) >> 1) - 1); 327 327 328 328 if (set) 329 329 putstr(s); … … 341 341 { 342 342 suseconds_t timeout = fallrate; 343 343 344 344 while (timeout > 0) { 345 345 cons_event_t event; 346 346 347 347 if (!console_get_event_timeout(console, &event, &timeout)) 348 348 break; … … 359 359 * and increase speed. 360 360 */ 361 361 362 362 if (timeleft <= 0) { 363 363 faster(); 364 364 timeleft = fallrate; 365 365 } 366 366 367 367 /* 368 368 * Wait to see if there is any input. If so, take it and … … 371 371 * make timeleft zero and return -1. 372 372 */ 373 373 374 374 wchar_t c = 0; 375 375 376 376 while (c == 0) { 377 377 cons_event_t event; 378 378 379 379 if (!console_get_event_timeout(console, &event, &timeleft)) { 380 380 timeleft = 0; 381 381 return -1; 382 382 } 383 383 384 384 if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) 385 385 c = event.ev.key.c; 386 386 } 387 387 388 388 return (int) c; 389 389 } … … 395 395 { 396 396 wchar_t c = 0; 397 397 398 398 while (c == 0) { 399 399 cons_event_t event; 400 400 401 401 if (!console_get_event(console, &event)) 402 402 return -1; 403 403 404 404 if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) 405 405 c = event.ev.key.c; 406 406 } 407 407 408 408 return (int) c; 409 409 } -
uspace/dist/src/c/demos/tetris/shapes.c
r3061bc1 ra35b458 96 96 { 97 97 const int *o = shape->off; 98 98 99 99 if ((board[pos]) || (board[pos + *o++]) || (board[pos + *o++]) || 100 100 (board[pos + *o])) 101 101 return 0; 102 102 103 103 return 1; 104 104 } … … 111 111 { 112 112 const int *o = shape->off; 113 113 114 114 board[pos] = onoff ? shape->color : 0x000000; 115 115 board[pos + *o++] = onoff ? shape->color : 0x000000; -
uspace/dist/src/c/demos/tetris/tetris.c
r3061bc1 ra35b458 98 98 int i; 99 99 cell *p = board; 100 100 101 101 for (i = B_SIZE; i; i--) 102 102 *p++ = (i <= (2 * B_COLS) || (i % B_COLS) < 2) ? 0x0000ff : 0x000000; … … 113 113 int base; 114 114 cell *p; 115 115 116 116 for (i = A_FIRST; i < A_LAST; i++) { 117 117 base = i * B_COLS + 1; … … 122 122 rows++; 123 123 memset(&board[base], 0, sizeof(cell) * (B_COLS - 2)); 124 124 125 125 scr_update(); 126 126 tsleep(); 127 127 128 128 while (--base != 0) 129 129 board[base + B_COLS] = board[base]; 130 130 131 131 scr_update(); 132 132 tsleep(); 133 133 134 134 break; 135 135 } 136 136 } 137 137 } 138 138 139 139 switch (rows) { 140 140 case 1: … … 160 160 int i; 161 161 int j = rand() % 4; 162 162 163 163 for (i = 0; i < j; i++) 164 164 tmp = &shapes[classic ? tmp->rotc : tmp->rot]; 165 165 166 166 return (tmp); 167 167 } … … 170 170 { 171 171 struct timeval tv; 172 172 173 173 gettimeofday(&tv, NULL); 174 174 srand(tv.tv_sec + tv.tv_usec / 100000); … … 180 180 moveto(5, 10); 181 181 puts("Tetris\n\n"); 182 182 183 183 moveto(8, 10); 184 184 printf("Level = %d (press keys 1 - 9 to change)", level); … … 202 202 while (1) { 203 203 int i = getchar(); 204 204 205 205 switch(i) { 206 206 case 'p': … … 249 249 int j; 250 250 int ch; 251 251 252 252 console = console_init(stdin, stdout); 253 253 254 254 keys = "jkl pq"; 255 255 256 256 classic = 0; 257 257 showpreview = 1; 258 258 259 259 while ((ch = getopt(argc, argv, "ck:ps")) != -1) 260 260 switch(ch) { … … 280 280 usage(); 281 281 } 282 282 283 283 argc -= optind; 284 284 argv += optind; 285 285 286 286 if (argc) 287 287 usage(); 288 288 289 289 for (i = 0; i <= 5; i++) { 290 290 for (j = i + 1; j <= 5; j++) { … … 292 292 errx(1, "%s", "duplicate command keys specified."); 293 293 } 294 294 295 295 if (keys[i] == ' ') 296 296 str_cpy(key_write[i], sizeof(key_write[i]), "<space>"); … … 300 300 } 301 301 } 302 302 303 303 snprintf(key_msg, sizeof(key_msg), 304 304 "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit", 305 305 key_write[0], key_write[1], key_write[2], key_write[3], 306 306 key_write[4], key_write[5]); 307 307 308 308 scr_init(); 309 309 if (loadscores() != EOK) … … 312 312 while (tetris_menu(&level)) { 313 313 fallrate = 1000000 / level; 314 314 315 315 scr_clear(); 316 316 setup_board(); 317 317 318 318 srandomdev(); 319 319 scr_set(); 320 320 321 321 pos = A_FIRST * B_COLS + (B_COLS / 2) - 1; 322 322 nextshape = randshape(); 323 323 curshape = randshape(); 324 324 325 325 scr_msg(key_msg, 1); 326 326 327 327 while (1) { 328 328 place(curshape, pos, 1); … … 338 338 continue; 339 339 } 340 340 341 341 /* 342 342 * Put up the current shape `permanently', … … 346 346 score++; 347 347 elide(); 348 348 349 349 /* 350 350 * Choose a new shape. If it does not fit, … … 354 354 nextshape = randshape(); 355 355 pos = A_FIRST * B_COLS + (B_COLS / 2) - 1; 356 356 357 357 if (!fits_in(curshape, pos)) 358 358 break; 359 360 continue; 361 } 362 359 360 continue; 361 } 362 363 363 /* 364 364 * Handle command keys. … … 368 368 break; 369 369 } 370 370 371 371 if (c == keys[4]) { 372 372 static char msg[] = 373 373 "paused - press RETURN to continue"; 374 374 375 375 place(curshape, pos, 1); 376 376 do { … … 380 380 console_flush(console); 381 381 } while (!twait()); 382 382 383 383 scr_msg(msg, 0); 384 384 scr_msg(key_msg, 1); … … 386 386 continue; 387 387 } 388 388 389 389 if (c == keys[0]) { 390 390 /* move left */ … … 393 393 continue; 394 394 } 395 395 396 396 if (c == keys[1]) { 397 397 /* turn */ 398 398 const struct shape *new = 399 399 &shapes[classic ? curshape->rotc : curshape->rot]; 400 400 401 401 if (fits_in(new, pos)) 402 402 curshape = new; 403 403 continue; 404 404 } 405 405 406 406 if (c == keys[2]) { 407 407 /* move right */ … … 410 410 continue; 411 411 } 412 412 413 413 if (c == keys[3]) { 414 414 /* move to bottom */ … … 419 419 continue; 420 420 } 421 421 422 422 if (c == '\f') { 423 423 scr_clear(); … … 425 425 } 426 426 } 427 427 428 428 scr_clear(); 429 429 loadscores(); … … 432 432 score = 0; 433 433 } 434 434 435 435 scr_clear(); 436 436 printf("\nGame over.\n"); 437 437 scr_end(); 438 438 439 439 return 0; 440 440 }
Note:
See TracChangeset
for help on using the changeset viewer.
