Re: [問題] 怎麼處理Ctrl+D ?

看板C_and_CPP作者 (艾斯寇德)時間15年前 (2009/04/28 14:24), 編輯推噓1(104)
留言5則, 2人參與, 最新討論串2/3 (看更多)
※ 引述《chenroseyaks (小盜)》之銘言: : 小的目前寫一個小遊戲,按Ctrl+D要執行一個新回合,已經困擾我好久 : while(fgets( ... ,stdin)) : { : ... : } : if(feof(stdin)) : { : ... : } : 按一次Ctrl+D後,接下來他就一直跑 feof(stdin) 裡面的東西,有沒有辦法 : 再讓他回到fgets那一行 ? : 謝謝各位 如果你希望整個程式流程可以即時反應按鍵,也許你要把遊戲拆成一小部分一小部分 的MSG,每個MSG包含一個procedure(function pointer) 以及一個enum 透過一個共同訊息以及工作佇列進行溝通,另外把按鍵偵測給另一個thread, 接收到reset訊息的時候就在目前執行MSG的下一個位置插入一個reinitialize訊息。 這做法還頗常用的,也許你可以考慮看看。 至於每個函式的MSG發送與狀態中斷,可以考慮用portable coroutine library 或者protothread 用message溝通的thread http://home.swbell.net/mck9/ct/ portable coroutine library http://www.xmailserver.org/libpcl.html 用fixed stack,改變ebp為heap,eip為function以及longjmp達成context switch 如果碰到不支援的平台,你可能要自己看看你的jmp_buf哪個offset是ebp,以及eip 先前我改了一下 這裡是set context改過的程式碼 #ifndef JumpBuf_Eip_IntOffset # define JumpBuf_Eip_IntOffset 5 #endif #ifndef JumpBuf_Esp_IntOffset # define JumpBuf_Esp_IntOffset 4 #endif static int co_set_context(co_ctx_t *ctx,void *func, char *stkbase,long stksiz) { char *stack; stack = stkbase + stksiz - sizeof(long); setjmp(ctx->cc); ctx->cc[JumpBuf_Eip_IntOffset] = (int)func; ctx->cc[JumpBuf_Esp_IntOffset] = (int)stack; return 0; } proto thread 用巨集,switch,preprocessor達成的thread,不會儲存狀態 http://www.sics.se/~adam/pt/ 如果整個程式都是跟按鍵綁在一起,那直接讓程式流程由kbhit以及getch決定吧。 int key_gettter(MSG* pmsg){ pmsg->kbhit_status = kbhit(); if( pmsg->kbhit_status ){ pmsg->key_input = getch(); } return pmsg->kbhit_status; } while( !program_end(&msg) ){ key_getter(&msg); msg_handler(&msg); /* 程式行為 */ } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.227.127.77 ※ 編輯: sunneo 來自: 61.227.127.77 (04/28 14:29)

04/28 14:29, , 1F
謝謝^ ^ 研究看看
04/28 14:29, 1F

04/28 14:36, , 2F
其實s大說的我都還沒學到 ... 大概要研究很久了XDD
04/28 14:36, 2F

04/28 14:38, , 3F
那我想你可以朝kbhit以及getch的實作做起 那可能較快
04/28 14:38, 3F

04/28 14:38, , 4F
只有基礎的while switch等看得懂
04/28 14:38, 4F

04/28 14:39, , 5F
嗯嗯 謝謝
04/28 14:39, 5F
※ 編輯: sunneo 來自: 61.227.127.77 (04/28 14:53)
文章代碼(AID): #19zg4kAb (C_and_CPP)
文章代碼(AID): #19zg4kAb (C_and_CPP)