bool GameApp::UpdateAndRender() { float ticks = (float)SDL_GetTicks() / 1000.0f; float elapsed = ticks - lastFrameTicks; lastFrameTicks = ticks; float fixedElapsed = elapsed + timeLeftOver; if (fixedElapsed > FIXED_TIMESTEP * MAX_TIMESTEPS) fixedElapsed = FIXED_TIMESTEP * MAX_TIMESTEPS; while (fixedElapsed >= FIXED_TIMESTEP) { fixedElapsed -= FIXED_TIMESTEP; FixedUpdate(); Collision(); } timeLeftOver = fixedElapsed; ProcessEvents(); if (player.collidedBottom && keys[SDL_SCANCODE_SPACE]) { player.velocityY = 1.0f; Mix_PlayChannel(-1, jumpSound, 0); } Render(); return done; }
bool PlatKillas::UpdateAndRender() { float ticks = (float)SDL_GetTicks() / 1000.0f; float elapsed = ticks - lastFrameTicks; lastFrameTicks = ticks; SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) { done = true; } else if (event.type == SDL_KEYDOWN) { if (event.key.keysym.scancode == SDL_SCANCODE_SPACE) { if (shootTimer > 0.1f) shootBullet(); } } } const Uint8 *keys = SDL_GetKeyboardState(NULL); if (keys[SDL_SCANCODE_W]) { if (!player->isJumping) { player->jump(); } } if (keys[SDL_SCANCODE_D]) { SheetSprite playerSprite = SheetSprite(characterSpriteSheetTexture, 12, 8, 27); player->sprite = playerSprite; player->setWalkRight(); } else if (keys[SDL_SCANCODE_A]) { SheetSprite playerSprite = SheetSprite(characterSpriteSheetTexture, 12, 8, 15); player->sprite = playerSprite; player->setWalkLeft(); } else { player->setIdle(); } if (keys[SDL_SCANCODE_X]) { if (shootTimer > 0.1f) { shootTimer = 0.0f; shootBullet(); } } float fixedElapsed = elapsed + timeLeftOver; if (fixedElapsed > FIXED_TIMESTEP* MAX_TIMESTEPS) { fixedElapsed = FIXED_TIMESTEP* MAX_TIMESTEPS; } while (fixedElapsed >= FIXED_TIMESTEP) { fixedElapsed -= FIXED_TIMESTEP; FixedUpdate(); } timeLeftOver = fixedElapsed; Update(elapsed); Render(); return done; }
bool Asteroids::UpdateAndRender() { float ticks = (float)SDL_GetTicks() / 1000.0f; float elapsed = ticks - lastFrameTicks; lastFrameTicks = ticks; SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) { done = true; } else if (event.type == SDL_KEYDOWN) { if (event.key.keysym.scancode == SDL_SCANCODE_SPACE) { if (shootTimer > 0.1f) shootBullet(); } } } const Uint8 *keys = SDL_GetKeyboardState(NULL); if (keys[SDL_SCANCODE_W]) { player->velocity_x = 1.0f; player->velocity_y = 1.0f; } else if (keys[SDL_SCANCODE_S]) { player->velocity_x = 0.0f; player->velocity_y = 0.0f; } if (keys[SDL_SCANCODE_D]) { player->rotation -= 5.0f * elapsed; } else if (keys[SDL_SCANCODE_A]) { player->rotation += 5.0f * elapsed; } if (keys[SDL_SCANCODE_X]) { if (shootTimer > 0.1f) { shootTimer = 0.0f; shootBullet(); } } float fixedElapsed = elapsed + timeLeftOver; if (fixedElapsed > FIXED_TIMESTEP* MAX_TIMESTEPS) { fixedElapsed = FIXED_TIMESTEP* MAX_TIMESTEPS; } while (fixedElapsed >= FIXED_TIMESTEP) { fixedElapsed -= FIXED_TIMESTEP; FixedUpdate(fixedElapsed); } timeLeftOver = fixedElapsed; Update(elapsed); Render(); return done; }
int main(int argc, char *argv[]) { Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096); Mix_Chunk *hit; hit = Mix_LoadWAV("Blip_Select2.wav"); Mix_Chunk *lose; lose = Mix_LoadWAV("Powerup.wav"); Mix_Music *song; song = Mix_LoadMUS("testsong.mp3"); Mix_PlayMusic(song, -1); //I would put these in Setup(), but then I have to return them bool done = false; float lastFrameTicks = 0.0f; float ballAngle = 0.0f; Setup(); while (!done) { //Couldn't think of a better place for this either float ticks = (float)SDL_GetTicks() / 1000.0f; float elapsed = ticks - lastFrameTicks; lastFrameTicks = ticks; ProcessEvents(done, elapsed); float fixedElapsed = elapsed + timeLeftOver; if (fixedElapsed > FIXED_TIMESTEP * MAX_TIMESTEPS) { fixedElapsed = FIXED_TIMESTEP * MAX_TIMESTEPS; } while (fixedElapsed >= FIXED_TIMESTEP) { fixedElapsed -= FIXED_TIMESTEP; FixedUpdate(hit); } timeLeftOver = fixedElapsed; Update(done, elapsed, lose); Render(); } Mix_FreeMusic(song); SDL_Quit(); return 0; }
bool GameName::UpdateAndRender() { // Updating the timing of the game float ticks = (float)SDL_GetTicks() / 1000.0f; float elapsed = ticks - lastFrameTicks; lastFrameTicks = ticks; SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE || event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { done = true; } else if (event.type == SDL_MOUSEMOTION) { mousePosition.x = (((float)event.motion.x / ScreenResolution.x) * 2 * aspectRatio) - aspectRatio; mousePosition.y = (((float)(ScreenResolution.y - event.motion.y) / ScreenResolution.y) * 2.0f) - 1.0f; } } // Keyboard Input const Uint8 *keys = SDL_GetKeyboardState(NULL); if (keys[SDL_SCANCODE_LEFT]) { } if (keys[SDL_SCANCODE_RIGHT]) { } if (keys[SDL_SCANCODE_UP]) { } if (keys[SDL_SCANCODE_DOWN]) { } float fixedElapsed = elapsed + timeLeftOver; if (fixedElapsed > FIXED_TIMESTEP * MAX_TIMESTEPS) { fixedElapsed = FIXED_TIMESTEP * MAX_TIMESTEPS; } while (fixedElapsed >= FIXED_TIMESTEP) { fixedElapsed -= FIXED_TIMESTEP; FixedUpdate(); } timeLeftOver = fixedElapsed; Update(elapsed); Render(); return done; }
bool Bot::Update(float dt) { if (unit->GetHP() <= 0) return false; bool ret = true; if (fixedUpdateSeconds <= updateTimer.ReadSec()) { FixedUpdate(); updateTimer.Start(); } return ret; }
bool PlatformerGame::UpdateandRender() { SDL_Event event; keys = SDL_GetKeyboardState(NULL); float lastFrameTicks = 0.0f; float ticks = (float)SDL_GetTicks() / 1000.0f; float elapsed = ticks - lastFrameTicks; lastFrameTicks = ticks; float fixedElapsed = elapsed + timeLeftOver; if (fixedElapsed > FIXED_TIMESTEP * MAX_TIMESTEPS) { fixedElapsed = FIXED_TIMESTEP * MAX_TIMESTEPS; } while (fixedElapsed >= FIXED_TIMESTEP) { fixedElapsed -= FIXED_TIMESTEP; dynamicEntities[0].FixedUpdate(); FixedUpdate(); } timeLeftOver = fixedElapsed; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) { done = true; } } if (keys[SDL_SCANCODE_LEFT]) { dynamicEntities[0].setAccelX(-0.1); } else if (keys[SDL_SCANCODE_RIGHT]) { dynamicEntities[0].setAccelX(0.1); } else { dynamicEntities[0].setAccelX(0.0); } if (event.key.keysym.scancode == SDL_SCANCODE_SPACE) { if (dynamicEntities[0].getCollidedBottom()) { dynamicEntities[0].setVelocityY(2.0f); dynamicEntities[0].setCollidedBottom(false); } } Render(); return done; }
bool SideScroller::UpdateAndRender() { float ticks = (float)SDL_GetTicks() / 1000.0f; float elapsed = ticks - lastFrameTicks; lastFrameTicks = ticks; float fixedElapsed = elapsed + timeLeftOver; if (fixedElapsed > FIXED_TIMESTEP* MAX_TIMESTEPS) { fixedElapsed = FIXED_TIMESTEP* MAX_TIMESTEPS; } while (fixedElapsed >= FIXED_TIMESTEP) { fixedElapsed -= FIXED_TIMESTEP; FixedUpdate(); } timeLeftOver = fixedElapsed; Update(elapsed); Render(); return done; }
void ExComplete::HandleEvent(TEvent &Event) { unsigned long kb = kbCode(Event.Key.Code); int DoQuit = 0; int i = 0; if (WordsLast < 2) { if ((WordsLast == 1) && (kb != kbEsc)) { DoQuit = 1; } else { EndExec(0); Event.What = evNone; } } else if (Event.What == evKeyDown) { switch (kb) { case kbPgUp: case kbLeft: // if there would not be locale sort, we could check only // the next string, but with `locale sort` this is impossible!! // this loop is little inefficient but it's quite short & nice for (i = WordPos; i-- > 0;) if (strncmp(Words[WordPos], Words[i], WordFixed) == 0) { WordPos = i; break; } Event.What = evNone; break; case kbPgDn: case kbRight: for (i = WordPos; i++ < WordsLast - 1;) if (strncmp(Words[WordPos], Words[i], WordFixed) == 0) { WordPos = i; break; } Event.What = evNone; break; case kbHome: for (i = 0; i < WordPos; i++) if (strncmp(Words[WordPos], Words[i], WordFixed) == 0) WordPos = i; Event.What = evNone; break; case kbEnd: for (i = WordsLast - 1; i > WordPos; i--) if (strncmp(Words[WordPos], Words[i], WordFixed) == 0) WordPos = i; Event.What = evNone; break; case kbTab: while (WordPos < WordsLast - 1) { WordPos++; if (strncmp(Words[WordPos], Words[WordPos - 1], WordFixed + 1)) break; } Event.What = evNone; break; case kbTab | kfShift: while (WordPos > 0) { WordPos--; if (strncmp(Words[WordPos], Words[WordPos + 1], WordFixed + 1)) break; } Event.What = evNone; break; case kbIns: case kbUp: FixedUpdate(1); Event.What = evNone; break; case kbBackSp: case kbDel: case kbDown: FixedUpdate(-1); Event.What = evNone; break; case kbEsc: EndExec(0); Event.What = evNone; break; case kbEnter: case kbSpace: case kbTab | kfCtrl: DoQuit = 1; break; default: if (CheckASCII(Event.Key.Code&~kfShift)) { char *s = new char[WordFixed + 2]; if (s != NULL) { if (WordFixed > 0) strncpy(s, Words[WordPos], WordFixed); s[WordFixed] = (unsigned char)(Event.Key.Code & 0xFF); s[WordFixed + 1] = 0; for (int i = 0; i < WordsLast; i++) if (strncmp(s, Words[i], WordFixed + 1) == 0) { WordPos = i; if (WordFixedCount == 1) DoQuit = 1; else FixedUpdate(1); break; } delete[] s; } Event.What = evNone; } break; } } if (DoQuit) { /* int rc = 0; int l = strlen(Words[WordPos]); if (Buffer->InsText(Buffer->VToR(Orig.Row), Orig.Col, l, Words[WordPos], 1) && Buffer->SetPos(Orig.Col + l, Orig.Row)) { Buffer->Draw(Buffer->VToR(Orig.Row), Buffer->VToR(Orig.Row)); rc = 1; }*/ int rc = DoCompleteWord(); EndExec(rc); Event.What = evNone; } }
int ExComplete::RefreshComplete() { if ((Buffer->CP.Col == 0) || (Buffer->SetPos(Buffer->CP.Col, Buffer->CP.Row) == 0)) return 0; PELine L = Buffer->VLine(Buffer->CP.Row); int C = Buffer->CP.Col; int P = Buffer->CharOffset(L, C); if (!P || P > L->Count) return 0; int P1 = P; while ((P > 0) && CheckASCII(L->Chars[P - 1])) P--; int wlen = P1 - P; if (!wlen) return 0; WordBegin = new char[wlen + 1]; if (WordBegin == NULL) return 0; strncpy(WordBegin, L->Chars + P, wlen); WordBegin[wlen] = 0; // fprintf(stderr, "Calling %d %s\n", wlen, WordBegin); // Search words in TAGS TagComplete(Words, &WordsLast, MAXCOMPLETEWORDS, WordBegin); // fprintf(stderr, "Located %d words\n", WordsLast); // these words are already sorted // Search words in current TEXT Buffer->Match.Col = Buffer->Match.Row = 0; // this might look strange but it is necessary to catch // the first word at position 0,0 for match :-) unsigned long mask = SEARCH_NOPOS | SEARCH_WORDBEG; while (Buffer->FindStr(L->Chars + P, wlen, mask) == 1) { mask |= SEARCH_NEXT; PELine M = Buffer->RLine(Buffer->Match.Row); int X = Buffer->CharOffset(M, Buffer->Match.Col); if ((L->Chars == M->Chars) && (P == X)) continue; int XL = X; while ((XL < M->Count) && CheckASCII(M->Chars[XL])) XL++; int len = XL - X - wlen; if (len == 0) continue; char *s = new char[len + 1]; if (s != NULL) { strncpy(s, M->Chars + X + wlen, len); s[len] = 0; int c = 1, H = 0, L = 0, R = WordsLast; // using sort to insert only unique words while (L < R) { H = (L + R) / 2; c = strcmp(s, Words[H]); if (c < 0) R = H; else if (c > 0) L = H + 1; else break; } if (c != 0) { // Loop exited without finding the word. Instead, // it found the spot where the new should be inserted. WordsLast++; int i = WordsLast; while (i > L) { Words[i] = Words[i-1]; i--; } Words[i] = s; if (WordsLast >= MAXCOMPLETEWORDS) break; } else { // word was already listed, free duplicate. delete[] s; } } } Buffer->Match.Row = Buffer->Match.Col = -1; Buffer->MatchLen = Buffer->MatchCount = 0; // sort by current locales qsort(Words, WordsLast, sizeof(Words[0]), CmpStr); FixedUpdate(0); //for(int i = 0; i < WordsLast; i++) //if (Words[i]) //fprintf(stderr, "%3d:\t%10p\t%s\n", i, Words[i], Words[i]); //fprintf(stderr, "Words %3d\n", WordsLast); return WordsLast; }