void Game_Update() { // calculate a spinning world matrix MFMatrix world; world.SetTranslation(MakeVector(0, -5, 50)); static float rotation = 0.0f; rotation += MFSystem_TimeDelta(); world.RotateY(rotation); // set world matrix to the model MFModel_SetWorldMatrix(pModel, world); // advance the animation MFAnimation *pAnim = MFModel_GetAnimation(pModel); if(pAnim) { float start, end; MFAnimation_GetFrameRange(pAnim, &start, &end); static float time = 0.f; time += MFSystem_TimeDelta();// * 500; while(time >= end) time -= end; MFAnimation_SetFrame(pAnim, time); } }
void MFParticleSystem_DrawRotating(MFParticleSystem *pParticleSystem, const MFMatrix <v) { int numParticles = pParticleSystem->particles.GetLength(); if(!numParticles) return; float fadeStart = pParticleSystem->params.life - pParticleSystem->params.fadeDelay; MFMaterial_SetMaterial(pParticleSystem->pMaterial); MFPrimitive(PT_TriList, 0); MFBegin(numParticles * 6); MFParticle **ppI = pParticleSystem->particles.Begin(); while(*ppI) { MFParticle *pParticle = *ppI; float dt = MFSystem_TimeDelta(); pParticle->rot += pParticleSystem->params.rotationRate * dt; pParticle->size += pParticleSystem->params.scaleRate * dt; pParticle->velocity += pParticleSystem->params.force * dt; pParticle->pos += pParticle->velocity * dt; float t = pParticle->size * 0.5f; float rad = MFSqrt(t*t*2); float xoff = MFCos(-pParticle->rot + 0.7853981f)*rad; float yoff = MFSin(-pParticle->rot + 0.7853981f)*rad; float alpha = MFMin(pParticle->life / fadeStart, 1.0f); MFVector pos = ApplyMatrixH(pParticle->pos, ltv); MFSetColourV(MakeVector(pParticle->colour, pParticle->colour.w * alpha)); MFSetTexCoord1(1, 0); MFSetPosition(pos.x + xoff, pos.y + yoff, pos.z); MFSetTexCoord1(0, 1); MFSetPosition(pos.x - xoff, pos.y - yoff, pos.z); MFSetTexCoord1(0, 0); MFSetPosition(pos.x - yoff, pos.y + xoff, pos.z); MFSetTexCoord1(1, 0); MFSetPosition(pos.x + xoff, pos.y + yoff, pos.z); MFSetTexCoord1(1, 1); MFSetPosition(pos.x + yoff, pos.y - xoff, pos.z); MFSetTexCoord1(0, 1); MFSetPosition(pos.x - xoff, pos.y - yoff, pos.z); pParticle->life -= dt; if(pParticle->life < 0.0f) pParticleSystem->particles.Destroy(ppI); ppI++; } MFEnd(); }
MF_API void MFParticleSystem_UpdateEmitter(MFParticleEmitter *pEmitter) { // emit new particles pEmitter->emitTimeout += MFSystem_TimeDelta(); while(pEmitter->emitTimeout > pEmitter->emitPeriod) { MFParticleSystem_AddParticle(pEmitter); pEmitter->emitTimeout -= pEmitter->emitPeriod; } }
void Game_Draw() { MFCALLSTACK; MFRenderer_SetClearColour(0.f, 0.f, 0.2f, 1.f); MFRenderer_ClearScreen(); // Set identity camera (no camera) MFView_Push(); MFView_SetAspectRatio(MFDisplay_GetNativeAspectRatio()); MFView_SetProjection(); MFMaterial_SetMaterial(MFMaterial_GetStockMaterial(MFMat_White)); // set the world matrix to identity MFMatrix world = MFMatrix::identity; // move the box into the scene (along the z axis) world.Translate(MakeVector(0, 0, 5)); // increment rotation static float rotation = 0.0f; rotation += MFSystem_TimeDelta(); // rotate the box world.RotateYPR(rotation, rotation * 2.0f, rotation * 0.5f); // begin rendering the box MFPrimitive(PT_TriList); MFSetMatrix(world); // begin rendering 12 triangles (12 * 3 vertices) MFBegin(3 * 12); // draw a bunch of triangles MFSetColour(1,0,0,1); MFSetPosition(-1,-1, -1); MFSetPosition(-1, 1, -1); MFSetPosition( 1, 1, -1); MFSetPosition(-1,-1, -1); MFSetPosition( 1, 1, -1); MFSetPosition( 1,-1, -1); MFSetColour(0,1,0,1); MFSetPosition(-1,-1,1); MFSetPosition( 1,-1,1); MFSetPosition( 1, 1,1); MFSetPosition(-1,-1,1); MFSetPosition( 1, 1,1); MFSetPosition(-1, 1,1); MFSetColour(0,0,1,1); MFSetPosition( 1,-1,1); MFSetPosition( 1,-1,-1); MFSetPosition( 1, 1,-1); MFSetPosition( 1,-1,1); MFSetPosition( 1, 1,-1); MFSetPosition( 1, 1,1); MFSetColour(1,0,1,1); MFSetPosition(-1,-1,1); MFSetPosition(-1, 1,1); MFSetPosition(-1, 1,-1); MFSetPosition(-1,-1,1); MFSetPosition(-1, 1,-1); MFSetPosition(-1,-1,-1); MFSetColour(1,1,0,1); MFSetPosition(-1, 1,1); MFSetPosition( 1, 1,1); MFSetPosition( 1, 1,-1); MFSetPosition(-1, 1,1); MFSetPosition( 1, 1,-1); MFSetPosition(-1, 1,-1); MFSetColour(0,1,1,1); MFSetPosition(-1,-1,1); MFSetPosition(-1,-1,-1); MFSetPosition( 1,-1,-1); MFSetPosition(-1,-1,1); MFSetPosition( 1,-1,-1); MFSetPosition( 1,-1,1); MFEnd(); MFRect disp; MFDisplay_GetDisplayRect(&disp); MFView_SetOrtho(&disp); pUI->Draw(); MFView_Pop(); }
void Game_Update() { if(!bShowModel) { if(MFInput_WasPressed(Key_Up, IDD_Keyboard) && menuIndex > 0) --menuIndex; else if(MFInput_WasPressed(Key_Down, IDD_Keyboard) && menuIndex < (int)models.size()-1) ++menuIndex; else if(MFInput_WasPressed(Key_Return, IDD_Keyboard) && models.size() > 0) { bShowModel = true; // load model pModel = MFModel_CreateWithAnimation(models[menuIndex].CStr()); } } else { if(MFInput_WasPressed(Key_Escape, IDD_Keyboard)) { if(pModel) { MFModel_Destroy(pModel); pModel = NULL; } models.clear(); Scan("data:"); if(models.size() <= (size_t)menuIndex) menuIndex = models.size() ? (int)models.size() - 1 : 0; bShowModel = false; return; } if(pModel) { if(MFInput_Read(Mouse_LeftButton, IDD_Mouse) > 0.f) { yaw += -MFInput_Read(Mouse_XDelta, IDD_Mouse) * 0.02f; pitch += -MFInput_Read(Mouse_YDelta, IDD_Mouse) * 0.015f; } if(MFInput_Read(Mouse_MiddleButton, IDD_Mouse) > 0.f) { zoom *= 1.f + -MFInput_Read(Mouse_YDelta, IDD_Mouse) * 0.02f; } // calculate a spinning world matrix MFMatrix world; world.SetTranslation(MakeVector(0, -0.25f, 1) * zoom); world.RotateY(yaw); world.RotateX(pitch); // set world matrix to the model MFModel_SetWorldMatrix(pModel, world); // advance the animation MFAnimation *pAnim = MFModel_GetAnimation(pModel); if(pAnim) { float start, end; MFAnimation_GetFrameRange(pAnim, &start, &end); static float time = 0.f; time += MFSystem_TimeDelta();// * 500; while(time >= end) time -= end; MFAnimation_SetFrame(pAnim, time); } } } }
void HKStringEntryLogic::Update() { bool shiftL = !!MFInput_Read(Key_LShift, IDD_Keyboard); bool shiftR = !!MFInput_Read(Key_RShift, IDD_Keyboard); bool ctrlL = !!MFInput_Read(Key_LControl, IDD_Keyboard); bool ctrlR = !!MFInput_Read(Key_RControl, IDD_Keyboard); int keyPressed = 0; bool shift = shiftL || shiftR; bool ctrl = ctrlL || ctrlR; #if defined(MF_WINDOWS) if(ctrl && MFInput_WasPressed(Key_C, IDD_Keyboard) && selectionStart != selectionEnd) { BOOL opened = OpenClipboard(apphWnd); if(opened) { int selMin = MFMin(selectionStart, selectionEnd); int selMax = MFMax(selectionStart, selectionEnd); int numChars = selMax-selMin; HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, numChars + 1); char *pString = (char*)GlobalLock(hData); MFString_Copy(pString, GetRenderString().SubStr(selMin, numChars).CStr()); GlobalUnlock(hData); EmptyClipboard(); SetClipboardData(CF_TEXT, hData); CloseClipboard(); } } else if(ctrl && MFInput_WasPressed(Key_X, IDD_Keyboard) && selectionStart != selectionEnd) { BOOL opened = OpenClipboard(apphWnd); if(opened) { int selMin = MFMin(selectionStart, selectionEnd); int selMax = MFMax(selectionStart, selectionEnd); int numChars = selMax-selMin; HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, numChars + 1); char *pString = (char*)GlobalLock(hData); MFString_Copy(pString, GetRenderString().SubStr(selMin, numChars).CStr()); GlobalUnlock(hData); EmptyClipboard(); SetClipboardData(CF_TEXT, hData); CloseClipboard(); ClearSelection(); } } else if(ctrl && MFInput_WasPressed(Key_V, IDD_Keyboard)) { BOOL opened = OpenClipboard(apphWnd); if(opened) { int selMin = MFMin(selectionStart, selectionEnd); int selMax = MFMax(selectionStart, selectionEnd); int numChars = selMax-selMin; HANDLE hData = GetClipboardData(CF_TEXT); MFString paste((const char*)GlobalLock(hData), true); buffer.Replace(selMin, numChars, paste); GlobalUnlock(hData); cursorPos = selMin + paste.NumBytes(); selectionStart = selectionEnd = cursorPos; GlobalUnlock(hData); CloseClipboard(); if((numChars || cursorPos != selMin) && changeCallback) changeCallback(buffer.CStr()); } } else #endif { // check for new keypresses for(int a=0; a<255; a++) { if(MFInput_WasPressed(a, IDD_Keyboard)) { keyPressed = a; holdKey = a; repeatDelay = gRepeatDelay; break; } } // handle repeat keys if(holdKey && MFInput_Read(holdKey, IDD_Keyboard)) { repeatDelay -= MFSystem_TimeDelta(); if(repeatDelay <= 0.f) { keyPressed = holdKey; repeatDelay += gRepeatRate; } } else holdKey = 0; // if there was a new key press if(keyPressed) { switch(keyPressed) { case Key_Backspace: case Key_Delete: { if(selectionStart != selectionEnd) { ClearSelection(); } else { if(keyPressed == Key_Backspace && cursorPos > 0) { buffer.ClearRange(--cursorPos, 1); selectionStart = selectionEnd = cursorPos; if(changeCallback) changeCallback(buffer.CStr()); } else if(keyPressed == Key_Delete && cursorPos < buffer.NumBytes()) { buffer.ClearRange(cursorPos, 1); selectionStart = selectionEnd = cursorPos; if(changeCallback) changeCallback(buffer.CStr()); } } break; } case Key_Left: case Key_Right: case Key_Home: case Key_End: { if(ctrl) { if(keyPressed == Key_Left) { while(cursorPos && MFIsWhite(buffer[cursorPos-1])) --cursorPos; if(MFIsAlphaNumeric(buffer[cursorPos-1])) { while(cursorPos && MFIsAlphaNumeric(buffer[cursorPos-1])) --cursorPos; } else if(cursorPos) { --cursorPos; while(cursorPos && buffer[cursorPos-1] == buffer[cursorPos]) --cursorPos; } } else if(keyPressed == Key_Right) { while(cursorPos < buffer.NumBytes() && MFIsWhite(buffer[cursorPos])) ++cursorPos; if(MFIsAlphaNumeric(buffer[cursorPos])) { while(cursorPos < buffer.NumBytes() && MFIsAlphaNumeric(buffer[cursorPos])) ++cursorPos; } else if(cursorPos < buffer.NumBytes()) { ++cursorPos; while(cursorPos < buffer.NumBytes() && buffer[cursorPos] == buffer[cursorPos-1]) ++cursorPos; } } else if(keyPressed == Key_Home) cursorPos = 0; else if(keyPressed == Key_End) cursorPos = buffer.NumBytes(); } else { if(keyPressed == Key_Left) cursorPos = (!shift && selectionStart != selectionEnd ? MFMin(selectionStart, selectionEnd) : MFMax(cursorPos-1, 0)); else if(keyPressed == Key_Right) cursorPos = (!shift && selectionStart != selectionEnd ? MFMax(selectionStart, selectionEnd) : MFMin(cursorPos+1, buffer.NumBytes())); else if(keyPressed == Key_Home) cursorPos = 0; // TODO: if multiline, go to start of line.. else if(keyPressed == Key_End) cursorPos = buffer.NumBytes(); // TODO: if multiline, go to end of line... } if(shift) selectionEnd = cursorPos; else selectionStart = selectionEnd = cursorPos; break; } default: { bool caps = MFInput_GetKeyboardStatusState(KSS_CapsLock); int ascii = MFInput_KeyToAscii(keyPressed, shift, caps); if(ascii && (!maxLen || buffer.NumBytes() < maxLen-1)) { // check character exclusions if(MFIsNewline(ascii) && type != ST_MultiLine) break; if(ascii == '\t' && type != ST_MultiLine) break; if(type == ST_Numeric && !MFIsNumeric(ascii)) break; if(include) { if(include.FindChar(ascii) < 0) break; } if(exclude) { if(exclude.FindChar(ascii) >= 0) break; } int selMin = MFMin(selectionStart, selectionEnd); int selMax = MFMax(selectionStart, selectionEnd); int selRange = selMax - selMin; const uint16 wstr[] = { (uint16)ascii, 0 }; char insert[5]; MFString_CopyUTF16ToUTF8(insert, wstr); buffer.Replace(selMin, selRange, insert); cursorPos = selMin + 1; selectionStart = selectionEnd = cursorPos; if(changeCallback) changeCallback(buffer.CStr()); } break; } } } } }
MF_API void MFParticleSystem_Draw(MFParticleSystem *pParticleSystem) { int numParticles = pParticleSystem->particles.GetLength(); if(numParticles == 0) return; // render particles MFView_Push(); MFMatrix ltv = MFView_GetWorldToViewMatrix(); MFView_SetCameraMatrix(MFMatrix::identity); // update and draw each particle if(pParticleSystem->params.rotationRate != 0.0f) { MFParticleSystem_DrawRotating(pParticleSystem, ltv); MFView_Pop(); return; } float fadeStart = pParticleSystem->params.life - pParticleSystem->params.fadeDelay; MFMaterial_SetMaterial(pParticleSystem->pMaterial); MFPrimitive(PT_QuadList, 0); MFBegin(numParticles * 2); MFParticle **ppI = pParticleSystem->particles.Begin(); while(*ppI) { MFParticle *pParticle = *ppI; float dt = MFSystem_TimeDelta(); pParticle->size += pParticleSystem->params.scaleRate * dt; pParticle->velocity += pParticleSystem->params.force * dt; pParticle->pos += pParticle->velocity * dt; float halfSize = pParticle->size * 0.5f; float alpha = MFMin(pParticle->life / fadeStart, 1.0f); MFVector pos = ApplyMatrixH(pParticle->pos, ltv); MFSetColourV(MakeVector(pParticle->colour, pParticle->colour.w * alpha)); MFSetTexCoord1(0, 0); MFSetPosition(pos.x - halfSize, pos.y + halfSize, pos.z); MFSetTexCoord1(1, 1); MFSetPosition(pos.x + halfSize, pos.y - halfSize, pos.z); pParticle->life -= dt; if(pParticle->life < 0.0f) pParticleSystem->particles.Destroy(ppI); ppI++; } MFEnd(); MFView_Pop(); }