void init () { int c, x, y, i; srand (time(NULL)); initscr (); raw (); nodelay(stdscr,1); noecho(); curs_set(0); nonl(); keypad(stdscr,1); resize_term(25,80); #ifdef PDCURSES PDC_set_title("Cymon's Games - Alleytris"); #endif start_color(); for (c = 0; c < COLORS; c++) if (c == COLOR_WHITE) init_pair(c, COLOR_BLACK, c); else init_pair(c, COLOR_WHITE, c); /* Allocate the memory required for the board buffer here */ buf = (int **)malloc(sizeof(int *) * (HEIGHT + 1)); for (i = 0; i < HEIGHT + 1; i++) { buf[i] = (int*)malloc(sizeof(int) * (col_width + 2)); } x = (COLS - col_width - 15) / 2; y = (LINES - HEIGHT) / 2; clear (); refresh (); pwin = newwin(HEIGHT + 1, col_width * 2 + 2, y, x); nwin = newwin(6, 10, y + 1, x + col_width * 2 + 5); iwin = newwin(15, 15, y + 8, x + col_width * 2 + 3); wattrset (iwin, COLOR_PAIR (COLORS / 2)); wfillrect (iwin, 1, 1, 14, 13); box (iwin, 0,0); drawmsg (iwin, 4, 1, 10, instruct); wrefresh (iwin); }
int again (int hs) { int ch; WINDOW *againwin; againwin = newwin(11, 13, (LINES / 2) - 6, (COLS / 2) - 4); wattrset (againwin, COLOR_PAIR (COLORS / 2 - 1)); wfillrect (againwin, 0, 0, 11, 13); mvwprintw (againwin, 1, 1, "High Score:"); mvwprintw (againwin, 3, 3, " %d lines", hs); drawmsg (againwin, 6, 1, 4, againmsg); wrefresh (againwin); wclear (againwin); do {ch = getch();} while ((tolower(ch) != 'q')&&(tolower(ch) != 'n')&&(tolower(ch) != 'y')); wrefresh (againwin); wattrset (iwin, COLOR_PAIR (COLORS / 2)); wfillrect (iwin, 1, 1, 14, 13); box (iwin, 0,0); drawmsg (iwin, 4, 1, 10, instruct); wrefresh (iwin); delwin (againwin); if (tolower(ch) == 'y') return 1; else return 0; }
int play () { BRICKTYPE brick, shadow; int level, next, lines, in, c, d, shadcol, nextop, shadop; double delay; clock_t start, check; level = 1; nextop = 1; shadop = 0; lines = 0; for (c = 0; c < HEIGHT + 1; c++) for (d = 0; d < col_width + 2; d++) buf[c][d] = (c < HEIGHT && d > 0 && d < col_width + 1) ? 0 : 1; brick.piece = rand () % PIECES; brick.x = col_width / 2; brick.y = 0; do { /* Next piece */ delay = 1.0 - level * .05; if (delay < .10) delay = 0.10 - (level - 20) * .01; if (delay < .06) delay = 0.6; if (shadop) brick.y = -2; next = rand () % PIECES; if (brick.piece % (COLORS / 2) == level % (COLORS / 2)) shadcol = (brick.piece + 1) % (COLORS / 2); else shadcol = brick.piece % (COLORS / 2); drawplay (level); mvwprintw (iwin, 1, 2, "Level : %d", level); mvwprintw (iwin, 2, 2, "Lines : %d", lines); wrefresh (iwin); start = clock (); shadow.piece = brick.piece; do { shadow.x = brick.x; shadow.y = brick.y; shadow.rot = brick.rot; while (!clip(&shadow)) shadow.y++; shadow.y--; if (nextop) drawpiece (nwin, 1, 2, brick.rot, next, BCOL(next)); if (shadop) drawpiece (pwin, shadow.y, shadow.x, shadow.rot, shadow.piece, shadcol); drawpiece (pwin, brick.y, brick.x, brick.rot, brick.piece, BCOL(brick.piece)); wrefresh (pwin); wrefresh (nwin); do { in = getch (); check = clock (); } while ((in == ERR)&&((double)(check - start) / CLOCKS_PER_SEC < delay)); if (nextop) drawpiece (nwin, 1, 2, brick.rot, next, (level + 1) % (COLORS / 2)); if (shadop) drawpiece (pwin, shadow.y, shadow.x, shadow.rot, shadow.piece, level % (COLORS / 2)); drawpiece (pwin, brick.y, brick.x, brick.rot, brick.piece, level % (COLORS / 2)); if ((double)(check - start) / CLOCKS_PER_SEC > delay) { brick.y++; start = clock (); } else switch (in) { case KEY_RIGHT : brick.x++; if (clip(&brick)) brick.x--; else start = clock (); break; case KEY_LEFT : brick.x--; if (clip(&brick)) brick.x++; else start = clock (); break; case KEY_UP : brick.rot++; brick.rot %= 4; if (clip(&brick)) { brick.x--; if (clip (&brick)) { brick.x += 2; if (clip (&brick)) { brick.x--; brick.rot += 3; brick.rot %= 4; } else start = clock (); } else start = clock (); } else start = clock (); break; case KEY_DOWN : brick.y++; start = clock (); break; case ' ' : while (!clip(&brick)) brick.y++; brick.y--; start = clock (); break; case 'n' : case 'N' : nextop = !nextop; break; case 's' : case 'S' : shadop = !shadop; break; case 'p' : case 'P' : wattrset (pwin, COLOR_PAIR(level % (COLORS / 2))); wfillrect (pwin, 0, 1, HEIGHT, col_width * 2); drawmsg (pwin, HEIGHT / 2, 2, 4, pausemsg); wrefresh (pwin); while (getch() == ERR) {} drawplay (level); wrefresh (pwin); break; case 'q' : case 'Q' : return (lines); } } while (!clip (&brick)); /* end of brickfall */ brick.y--; if (brick.y < 0) return (lines); for (d = 0; d < 4; d++) for (c = 0; c < 4; c++) /* commit piece to buffer */ if (blocks[rx(brick.rot % rots[brick.piece], d, c)][brick.piece][ry(brick.rot % rots[brick.piece], d, c)] != '.') buf[d + brick.y][c + brick.x - 1] = BCOL(brick.piece); drawplay (level); lines += clearedlines (level); level = lines / 10 + 1; brick.piece = next; brick.x = col_width / 2; brick.y = 0; next = rand () % PIECES; } while (!clip (&brick)); /* end of game */ drawpiece (pwin, brick.y, brick.x, brick.rot, brick.piece, BCOL(brick.piece)); wrefresh (pwin); return (lines); }
void notify(DBusMessage *dmsg) { DBusMessage *reply; DBusMessageIter args; DBusMessageIter hints; DBusMessageIter hint; char *hint_name; int i; int id = 23; const char *appname = NULL; const char *summary = NULL; const char *body = NULL; const char *icon = NULL; const char *fgcolor = NULL; const char *bgcolor = NULL; int urgency = 1; msg_queue_t *msg = malloc(sizeof(msg_queue_t)); dbus_uint32_t nid=0; dbus_int32_t expires=-1; dbus_serial++; dunst_printf(DEBUG, "new dbus message\n"); dbus_message_iter_init(dmsg, &args); dunst_printf(DEBUG, "extracting appname\n"); _extract_basic(DBUS_TYPE_STRING, &args, &appname); dbus_message_iter_next( &args ); dunst_printf(DEBUG, "extracting nid\n"); _extract_basic(DBUS_TYPE_UINT32, &args, &nid); dbus_message_iter_next( &args ); dunst_printf(DEBUG, "extracting icon\n"); _extract_basic(DBUS_TYPE_STRING, &args, &icon); dbus_message_iter_next( &args ); dunst_printf(DEBUG, "extracting summary\n"); _extract_basic(DBUS_TYPE_STRING, &args, &summary); dbus_message_iter_next( &args ); dunst_printf(DEBUG, "extracting body\n"); _extract_basic(DBUS_TYPE_STRING, &args, &body); dbus_message_iter_next( &args ); dbus_message_iter_next( &args ); dunst_printf(DEBUG, "extracting hints\n"); dbus_message_iter_recurse(&args, &hints); dbus_message_iter_next( &args ); dunst_printf(DEBUG, "extracting expires\n"); _extract_basic(DBUS_TYPE_INT32, &args, &expires); dunst_printf(DEBUG, "extracting hints\n"); while (dbus_message_iter_get_arg_type(&hints) != DBUS_TYPE_INVALID) { dbus_message_iter_recurse(&hints, &hint); while (dbus_message_iter_get_arg_type(&hint) != DBUS_TYPE_INVALID) { if(dbus_message_iter_get_arg_type(&hint) != DBUS_TYPE_STRING) { dbus_message_iter_next(&hint); continue; } dbus_message_iter_get_basic(&hint, &hint_name); _extract_hint("urgency", hint_name, &hint, &urgency); _extract_hint("fgcolor", hint_name, &hint, &fgcolor); _extract_hint("bgcolor", hint_name, &hint, &bgcolor); dbus_message_iter_next(&hint); } dbus_message_iter_next(&hints); } if(expires > 0) { /* do some rounding */ expires = (expires+500)/1000; if(expires < 1) { expires = 1; } } msg->appname = strdup(appname); msg->summary = strdup(summary); msg->body = strdup(body); msg->icon = strdup(icon); msg->timeout = expires; msg->urgency = urgency; for(i = 0; i < ColLast; i++) { msg->color_strings[i] = NULL; } msg->color_strings[ColFG] = fgcolor == NULL ? NULL : strdup(fgcolor); msg->color_strings[ColBG] = bgcolor == NULL ? NULL : strdup(bgcolor); initmsg(msg); msgqueue = add(msgqueue, msg); drawmsg(); reply = dbus_message_new_method_return(dmsg); dbus_message_iter_init_append(reply, &args); dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &id); dbus_connection_send(dbus_conn, reply, &dbus_serial); dbus_message_unref(reply); }