Skip to content

Commit

Permalink
fps refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yamader committed Dec 25, 2023
1 parent 69c73e2 commit 6701dc4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 40 deletions.
16 changes: 14 additions & 2 deletions src/engine/Context.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ import std.string;
import sdl;
import engine;

enum initFps = 60.;

struct Context {
bool running = true;
MonoTime updated;
Duration elapsed;

package {
// [ms]
ulong dur = cast(ulong)(1000 / initFps);
ulong updated;
ulong elapsed;
}
real fps() => 1000. / dur;
real fps(real v) {
dur = cast(ulong)(1000 / v);
return fps;
}

GameObject root;
InputManager im;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/GameObject.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GameObject: Loggable {

package Context* ctx;

auto dur() const => real(ctx.elapsed.total!"usecs") / 4096;
real dur() const => ctx.elapsed / 10.;
auto im() const => ctx.im;
auto everyone() => ctx.root.descendant;

Expand Down
80 changes: 43 additions & 37 deletions src/engine/System.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ class System: Loggable {
}

void run() {
ctx.updated = SDL_GetTicks64;
ctx.root.realSetup(&ctx);
ctx.updated = MonoTime.currTime;

loop; // 初回レンダリング
while(ctx.running) {
auto cur = MonoTime.currTime;
auto cur = SDL_GetTicks64;
auto elapsed = cur - ctx.updated;
if(elapsed < ctx.dur) {
import core.thread.osthread;
Thread.sleep(800.usecs);
continue;
}
ctx.updated = cur;
ctx.elapsed = elapsed;

Expand All @@ -44,44 +49,45 @@ class System: Loggable {
}

void loop() {
SDL_Event e;
SDL_PollEvent(&e);

auto keyUpdate = false;
switch(e.type) {
case SDL_KEYDOWN:
auto key = cast(char)e.key.keysym.sym;
auto state = ctx.im.state;
auto once = ctx.im.once;

if(!state[key]) once[key] = true;
else once[key] = false;

state[key] = true;
keyUpdate = true;
break;

case SDL_KEYUP:
ctx.im.state[cast(char)e.key.keysym.sym] = false;
break;
auto keyUpdated = false;

case SDL_MOUSEMOTION:
auto motion = e.motion;
break;

case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
auto btn = e.button;
break;

case SDL_QUIT:
ctx.running = false;
break;

default:
SDL_Event e;
while(SDL_PollEvent(&e)) {
switch(e.type) {
case SDL_KEYDOWN:
auto key = cast(char)e.key.keysym.sym;
auto state = ctx.im.state;
auto once = ctx.im.once;

if(!state[key]) once[key] = true;
else once[key] = false;

state[key] = true;
keyUpdated = true;
break;

case SDL_KEYUP:
ctx.im.state[cast(char)e.key.keysym.sym] = false;
break;

case SDL_MOUSEMOTION:
auto motion = e.motion;
break;

case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
auto btn = e.button;
break;

case SDL_QUIT:
ctx.running = false;
break;

default:
}
}

if(!keyUpdate)ctx.im.once ^= ctx.im.once;
if(!keyUpdated)ctx.im.once ^= ctx.im.once;

// Collider
auto gos = ctx.root.everyone.filter!(e => e.has!BoxCollider && e.has!Transform).array;
Expand Down

0 comments on commit 6701dc4

Please sign in to comment.