void TCOD_sys_flush(bool render) { static uint32 old_time,new_time=0, elapsed=0; int32 frame_time,time_to_wait; if ( render ) { TCOD_sys_console_to_bitmap(renderWindow,TCOD_console_get_width(NULL),TCOD_console_get_height(NULL),consoleBuffer,prevConsoleBuffer); sfRenderWindow_Display(renderWindow); } old_time=new_time; new_time=TCOD_sys_elapsed_milli(); if ( new_time / 1000 != elapsed ) { /* update fps every second */ fps=cur_fps; cur_fps=0; elapsed=new_time/1000; printf ("nbBlit : %d\n",(int)(nbBlit/fps));nbBlit=0; } /* if too fast, wait */ frame_time=(new_time - old_time); last_frame_length = frame_time * 0.001f; cur_fps++; time_to_wait=min_frame_length-frame_time; if (old_time > 0 && time_to_wait > 0) { TCOD_sys_sleep_milli(time_to_wait); new_time = TCOD_sys_elapsed_milli(); frame_time=(new_time - old_time); } last_frame_length = frame_time * 0.001f; }
void TCOD_sys_flush(bool render) { static uint32 old_time,new_time=0, elapsed=0; int32 frame_time,time_to_wait; if ( render ) { TCOD_sys_console_to_bitmap(screen,TCOD_console_get_width(NULL),TCOD_console_get_height(NULL),consoleBuffer, prevConsoleBuffer); if ( TCOD_sdl_renderer ) { TCOD_sdl_renderer((void *)screen); } SDL_Flip(screen); } old_time=new_time; new_time=TCOD_sys_elapsed_milli(); if ( new_time / 1000 != elapsed ) { /* update fps every second */ fps=cur_fps; cur_fps=0; elapsed=new_time/1000; } /* if too fast, wait */ frame_time=(new_time - old_time); last_frame_length = frame_time * 0.001f; cur_fps++; time_to_wait=min_frame_length-frame_time; if (old_time > 0 && time_to_wait > 0) { TCOD_sys_sleep_milli(time_to_wait); new_time = TCOD_sys_elapsed_milli(); frame_time=(new_time - old_time); } last_frame_length = frame_time * 0.001f; }
uint32 TCODSystem::getElapsedMilli() { return TCOD_sys_elapsed_milli(); }
/* renders the textfield */ void TCOD_text_render (TCOD_text_t txt, TCOD_console_t con) { text_t * data = (text_t*)txt; uint32 time; bool cursor_on; char back=0; int curx,cury,cursorx,cursory, curpos; char *ptr; TCOD_IFNOT(data && data->con ) return; time = TCOD_sys_elapsed_milli(); cursor_on = (int)( time % data->interval ) > data->halfinterval; TCOD_console_set_default_background(data->con, data->back); TCOD_console_set_default_foreground(data->con, data->fore); TCOD_console_clear(data->con); /* compute cursor position */ get_cursor_coords(data,&cursorx,&cursory); if ( cursor_on && data->ascii_cursor) { /* save the character under cursor position */ back = data->text[data->cursor_pos]; data->text[data->cursor_pos] = data->ascii_cursor; } /* render prompt */ if (data->prompt) TCOD_console_print_rect_ex(data->con,0,0,data->w,data->h,TCOD_BKGND_SET,TCOD_LEFT,"%s",data->prompt); /* render text */ curx=data->textx; cury=data->texty; ptr=data->text; curpos=0; while (*ptr) { if ( *ptr == '\n') { if ( (curx == 0 || curpos == 0 ) && curpos >= data->sel_start && curpos < data->sel_end ) { /* inverted colors for selected empty lines */ TCOD_console_set_char_background(data->con, curx, cury, data->fore, TCOD_BKGND_SET); TCOD_console_set_char_foreground(data->con, curx, cury, data->back); } curx=0; cury++; } else { if ( curpos >= data->sel_start && curpos < data->sel_end ) { /* inverted colors for selection */ TCOD_console_set_char_background(data->con, curx, cury, data->fore, TCOD_BKGND_SET); TCOD_console_set_char_foreground(data->con, curx, cury, data->back); } TCOD_console_set_char(data->con,curx,cury,*ptr); curx++; if ( curx == data->w ) { curx=0; cury++; } } ptr++; curpos++; } if ( cursor_on ) { if ( data->ascii_cursor) { /* restore the character under cursor */ data->text[data->cursor_pos] = back; } else { /* invert colors at cursor position */ TCOD_console_set_char_background(data->con,cursorx,cursory,data->fore,TCOD_BKGND_SET); TCOD_console_set_char_foreground(data->con,cursorx,cursory,data->back); } } else if (! cursor_on && ! data->ascii_cursor && data->multiline ) { /* normal colors for cursor ( might be inside selection ) */ TCOD_console_set_char_background(data->con,cursorx,cursory,data->back,TCOD_BKGND_SET); TCOD_console_set_char_foreground(data->con,cursorx,cursory,data->fore); } TCOD_console_blit(data->con,0,0,data->w,data->h,con,data->x,data->y,1.0f,data->transparency); }
static void tcod_nextKeyOrMouseEvent(rogueEvent *returnEvent, boolean textInput, boolean colorsDance) { boolean tryAgain; TCOD_key_t key; TCOD_mouse_t mouse; uint32 theTime, waitTime; short x, y; TCOD_console_flush(); key.vk = TCODK_NONE; if (noMenu && rogue.nextGame == NG_NOTHING) rogue.nextGame = NG_NEW_GAME; for (;;) { theTime = TCOD_sys_elapsed_milli(); if (TCOD_console_is_window_closed()) { rogue.gameHasEnded = true; // causes the game loop to terminate quickly rogue.nextGame = NG_QUIT; // causes the menu to drop out immediately returnEvent->eventType = KEYSTROKE; returnEvent->param1 = ESCAPE_KEY; return; } tryAgain = false; if (bufferedKey.vk != TCODK_NONE) { rewriteKey(&bufferedKey, textInput); if (processKeystroke(bufferedKey, returnEvent, textInput)) { bufferedKey.vk = TCODK_NONE; return; } else { bufferedKey.vk = TCODK_NONE; } } if (missedMouse.lmb) { returnEvent->eventType = missedMouse.lmb; returnEvent->param1 = missedMouse.x; returnEvent->param2 = missedMouse.y; if (TCOD_console_is_key_pressed(TCODK_CONTROL)) { returnEvent->controlKey = true; } if (TCOD_console_is_key_pressed(TCODK_SHIFT)) { returnEvent->shiftKey = true; } missedMouse.lmb = missedMouse.lmb == MOUSE_DOWN ? MOUSE_UP : 0; return; } if (missedMouse.rmb) { returnEvent->eventType = missedMouse.rmb == MOUSE_DOWN ? RIGHT_MOUSE_DOWN : RIGHT_MOUSE_UP; returnEvent->param1 = missedMouse.x; returnEvent->param2 = missedMouse.y; if (TCOD_console_is_key_pressed(TCODK_CONTROL)) { returnEvent->controlKey = true; } if (TCOD_console_is_key_pressed(TCODK_SHIFT)) { returnEvent->shiftKey = true; } missedMouse.rmb = missedMouse.rmb == MOUSE_DOWN ? MOUSE_UP : 0; return; } if (!(serverMode || (SDL_GetAppState() & SDL_APPACTIVE))) { TCOD_sys_sleep_milli(100); } else { if (colorsDance) { shuffleTerrainColors(3, true); commitDraws(); } TCOD_console_flush(); } #ifdef USE_NEW_TCOD_API TCOD_sys_check_for_event(TCOD_EVENT_KEY_PRESS | TCOD_EVENT_MOUSE, &key, &mouse); #else key = TCOD_console_check_for_keypress(TCOD_KEY_PRESSED); #endif rewriteKey(&key, textInput); if (processKeystroke(key, returnEvent, textInput)) { return; } mouse = TCOD_mouse_get_status(); if (serverMode || (SDL_GetAppState() & SDL_APPACTIVE)) { x = mouse.cx; y = mouse.cy; } else { x = 0; y = 0; } if ( mouse.lbutton_pressed || mouse.rbutton_pressed || mouse.lbutton != brogueMouse.lmb || mouse.rbutton != brogueMouse.rmb || brogueMouse.x !=x || brogueMouse.y != y) { returnEvent->param1 = x; returnEvent->param2 = y; getModifiers(returnEvent); if (mouse.lbutton_pressed) { if (!brogueMouse.lmb) { // we missed a mouseDown event -- better make up for it! missedMouse.x = x; missedMouse.y = y; missedMouse.lmb = MOUSE_UP; returnEvent->eventType = MOUSE_DOWN; } else { returnEvent->eventType = MOUSE_UP; } } else if (mouse.lbutton && !brogueMouse.lmb) { returnEvent->eventType = MOUSE_DOWN; } else { returnEvent->eventType = MOUSE_ENTERED_CELL; } if (mouse.rbutton_pressed) { if (!brogueMouse.rmb) { // we missed a mouseDown event -- better make up for it! missedMouse.x = x; missedMouse.y = y; missedMouse.rmb = MOUSE_UP; returnEvent->eventType = RIGHT_MOUSE_DOWN; } else { returnEvent->eventType = RIGHT_MOUSE_UP; } } else if (mouse.rbutton && !brogueMouse.rmb) { returnEvent->eventType = RIGHT_MOUSE_DOWN; } brogueMouse.x = x; brogueMouse.y = y; brogueMouse.lmb = mouse.lbutton; brogueMouse.rmb = mouse.rbutton; if (returnEvent->eventType == MOUSE_ENTERED_CELL && !hasMouseMoved) { hasMouseMoved = true; } else { return; } } waitTime = PAUSE_BETWEEN_EVENT_POLLING + theTime - TCOD_sys_elapsed_milli(); if (waitTime > 0 && waitTime <= PAUSE_BETWEEN_EVENT_POLLING) { TCOD_sys_sleep_milli(waitTime); } } }
int main( int argc, char *argv[] ) { char *font="tilesense/libtcod/fonts/courier12x12_aa_tc.png"; int nb_char_horiz=0,nb_char_vertic=0; int font_flags=TCOD_FONT_TYPE_GREYSCALE|TCOD_FONT_LAYOUT_TCOD; TCOD_console_set_custom_font(font,font_flags,nb_char_horiz,nb_char_vertic); TCOD_console_init_root(80,40,"salamandeRL",false); Map room = initRoom(); Object player = initPlayer(room); object_sense(player); TCOD_sys_set_fps(30); float elapsed = 0; char finished = 0; TCOD_key_t key = {TCODK_NONE,0}; TCOD_console_set_foreground_color(NULL,TCOD_white); do { key = TCOD_console_check_for_keypress(TCOD_KEY_PRESSED); if(key.vk != TCODK_NONE) { TCOD_console_clear(NULL); } TCOD_console_print_right(NULL,79,26,TCOD_BKGND_NONE,"last frame : %3d ms (%3d fps)", (int)(TCOD_sys_get_last_frame_length()*1000), TCOD_sys_get_fps()); TCOD_console_print_right(NULL,79,27,TCOD_BKGND_NONE,"elapsed : %8dms %4.2fs", TCOD_sys_elapsed_milli(),TCOD_sys_elapsed_seconds()); TCOD_console_print_left(NULL,0,27,TCOD_BKGND_NONE,"other stat stuff can go here"); //map drawmap(room, player); TCOD_console_print_left(NULL, object_position(player).x*2, object_position(player).y, TCOD_BKGND_NONE,"@"); //divider TCOD_console_print_left(NULL,0,28,TCOD_BKGND_NONE, "--------------------------------------------------------------------------------" ); //text display TCOD_console_print_left(NULL,2,29,TCOD_BKGND_NONE,"we'll probably put text down here."); /* update the game screen */ TCOD_console_flush(); if(key.vk == TCODK_RIGHT) { map_turn_object(room, "@", 1); } else if(key.vk == TCODK_LEFT) { map_turn_object(room, "@", -1); } else if(key.vk == TCODK_CHAR) { switch(key.c) { case 'w': map_move_object(room, "@", (mapVec){0, -1, 0}); break; case 'a': map_move_object(room, "@", (mapVec){-1, 0, 0}); break; case 's': map_move_object(room, "@", (mapVec){0, 1, 0}); break; case 'd': map_move_object(room, "@", (mapVec){1, 0, 0}); break; case 'q': finished = 1; break; //next, handle chomping default: break; } } } while (!finished && !TCOD_console_is_window_closed()); return 0; }