bool nglTimer::Start(bool Immediate, bool Reset) { if (mRunning) { return false; } nglTime now; mLastTime = now; if (mCounter == 0) mCounter = mRoundsPerTick; mCounter = MIN(mRoundsPerTick, mCounter); if (Reset) { mCallCnt = 0; mCounter = mRoundsPerTick; } if (Immediate) TimerAction(); mRunning = true; return true; }
void nglTimer::CallOnDispatch() { if (!mRunning) { return; } mCounter--; if (!mCounter) { TimerAction(); mCounter = mRoundsPerTick; } }
bool nglTimer::Start(bool Immediate, bool Reset) { Stop(); nglTime now; if (Reset) mCallCnt = 0; mLastTime = now; if (!mpCFRunLoopTimer) { CFRunLoopTimerContext ctx; ctx.version = 0; ctx.info = (void*)this; ctx.retain = NULL; ctx.release = NULL; ctx.copyDescription = NULL; CFAbsoluteTime absTime = CFAbsoluteTimeGetCurrent() + (double)mPeriod; mpCFRunLoopTimer = CFRunLoopTimerCreate( kCFAllocatorDefault, absTime, mPeriod, 0,// option flags, not implemented so far 0,// timer order, not implemented so far &nglTimerAction, &ctx); } NGL_ASSERT(mpCFRunLoopTimer); if (!mpCFRunLoopTimer) return false; mpCFRunLoop = CFRunLoopGetCurrent(); NGL_ASSERT(mpCFRunLoop); mRunning = true; CFRunLoopAddTimer(mpCFRunLoop, mpCFRunLoopTimer, kCFRunLoopCommonModes); if (Immediate) TimerAction(); return true; }
static LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static HDC hDC; static PAINTSTRUCT ps; int x,y; static HWND hWndButton; switch(message) { case LMSG_CREATE: hWndButton = CreateWindow("button", "start", WS_CONTROL | BS_PUSHBUTTON | WS_BORDER | WS_VISIBLE, 106, 210, 40 , 20, hWnd, (HMENU)ID_BUTTON, NULL, NULL); break; case LMSG_COMMAND: switch(HIWORD(wParam)){ case BN_CLICKED://×ÀÃæÍŒ±êµÄµã»÷ÊÂŒþŽŠÀí switch(LOWORD(wParam)){ case ID_BUTTON: if(!is_run){ SetTimer(hWnd,1,10); } GameInit(); break; default: break; } break; default: break; } break; case LMSG_PENDOWN://×ÀÃæµÄmouseÊÂŒþ CaptureMouse(hWnd,BYCLIENT); x=(int)wParam; y=(int)lParam; if(x<board.x+BOARD_LENGTH/2) board.x-=10; else board.x+=10; break; case LMSG_TIMER: TimerAction(hWnd); break; case LMSG_PENMOVE: break; case LMSG_PENUP://×ÀÃæµÄmouseÊÂŒþ DisCaptureMouse(); break; case LMSG_PAINT: { HGDIOBJ brush; HGDIOBJ pen; ps.bPaintDirect=false; hDC=BeginPaint(hWnd, &ps); if(!hDC){ return true; } if(is_run){ brush = CreateSolidBrush(RGB(23,43,133)); pen = CreatePen(PS_SOLID,1,RGB(230,0,255)); SelectObject(hDC,brush); SelectObject(hDC,pen); DeleteObject(brush); DeleteObject(pen); for(x=0;x<BRICK_NUM_IN_Y;x++) for(y=0;y<BRICK_NUM_IN_X;y++) { if(brick[x][y]==1) Rectangle(hDC,y*BRICK_LENGTH,x*BRICK_WIDTH,y*BRICK_LENGTH+BRICK_LENGTH,x*BRICK_WIDTH+BRICK_WIDTH); } Rectangle(hDC,board.x,board.y,board.x+BOARD_LENGTH,board.y+BOARD_WIDTH); Circle(hDC,ball.x,ball.y,BALL_RADIUS); } EndPaint(hWnd, &ps); break; } case LMSG_CLOSE: is_run=false; KillTimer(hWnd,1); PostQuitMessage(hWnd); break; case LMSG_DESTROY: is_run=false; KillTimer(hWnd,1); PostQuitMessage(hWnd);//ÓÃÀŽÍ˳öÏûϢѻ· break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return true; }