作者sunneo (艾斯寇德)
看板C_and_CPP
标题Re: [问题] 怎麽处理Ctrl+D ?
时间Tue Apr 28 14:24:44 2009
※ 引述《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)
1F:推 chenroseyaks:谢谢^ ^ 研究看看 04/28 14:29
2F:→ chenroseyaks:其实s大说的我都还没学到 ... 大概要研究很久了XDD 04/28 14:36
3F:→ sunneo:那我想你可以朝kbhit以及getch的实作做起 那可能较快 04/28 14:38
4F:→ chenroseyaks:只有基础的while switch等看得懂 04/28 14:38
5F:→ chenroseyaks:嗯嗯 谢谢 04/28 14:39
※ 编辑: sunneo 来自: 61.227.127.77 (04/28 14:53)