Added yielding, input refreshes as fast as possible.

This commit is contained in:
Harrison Deng 2020-07-04 00:39:23 -05:00
parent 3ecd32520c
commit fbda9b912f

View File

@ -81,7 +81,7 @@ namespace SlatedGameToolkit.Framework {
try { try {
IState initialState = (IState) o; IState initialState = (IState) o;
manager.Initialize(initialState); manager.Initialize(initialState);
DateTime currentTime = DateTime.Now; DateTime previousTime = DateTime.Now;
TimeSpan timePassedFromLastUpdate = TimeSpan.Zero; TimeSpan timePassedFromLastUpdate = TimeSpan.Zero;
TimeSpan timePassedFromLastRender = TimeSpan.Zero; TimeSpan timePassedFromLastRender = TimeSpan.Zero;
TimeSpan updateDeltaTime = GameEngine.updateDeltaTime; TimeSpan updateDeltaTime = GameEngine.updateDeltaTime;
@ -99,68 +99,70 @@ namespace SlatedGameToolkit.Framework {
} }
deltaChanged = false; deltaChanged = false;
} }
#region EventHandling
//Events
SDL.SDL_Event SDL_Event;
while (SDL.SDL_PollEvent(out SDL_Event) != 0) {
switch (SDL_Event.type) {
case SDL.SDL_EventType.SDL_MOUSEMOTION:
Mouse.OnMouseMoved(SDL_Event.motion.x, SDL_Event.motion.y);
break;
case SDL.SDL_EventType.SDL_MOUSEWHEEL:
Mouse.OnScroll(SDL_Event.wheel.x, SDL_Event.wheel.y);
break;
case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN:
if (SDL.SDL_BUTTON_LEFT == SDL_Event.button.button) {
Mouse.OnLeftChange(true);
} else if (SDL.SDL_BUTTON_RIGHT == SDL_Event.button.button) {
Mouse.OnRightChange(true);
} else if (SDL.SDL_BUTTON_MIDDLE == SDL_Event.button.button) {
Mouse.OnMiddleChange(true);
}
break;
case SDL.SDL_EventType.SDL_MOUSEBUTTONUP:
if (SDL.SDL_BUTTON_LEFT == SDL_Event.button.button) {
Mouse.OnLeftChange(false);
} else if (SDL.SDL_BUTTON_RIGHT == SDL_Event.button.button) {
Mouse.OnRightChange(false);
} else if (SDL.SDL_BUTTON_MIDDLE == SDL_Event.button.button) {
Mouse.OnMiddleChange(false);
}
break;
case SDL.SDL_EventType.SDL_KEYDOWN:
Keyboard.OnKeyPressed(SDL_Event.key.keysym.sym);
break;
case SDL.SDL_EventType.SDL_KEYUP:
Keyboard.OnKeyReleased(SDL_Event.key.keysym.sym);
break;
case SDL.SDL_EventType.SDL_WINDOWEVENT:
WindowContext handle = WindowContextsManager.ContextFromWindowID(SDL_Event.window.windowID);
switch (SDL_Event.window.windowEvent)
{
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED:
handle.OnResize(SDL_Event.window.data1, SDL_Event.window.data2);
break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST:
handle.OnFocusLost();
break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED:
handle.OnFocusGained();
break;
}
break;
case SDL.SDL_EventType.SDL_QUIT:
Stop();
break;
}
}
#endregion
DateTime frameStart = DateTime.Now; DateTime frameStart = DateTime.Now;
TimeSpan difference = frameStart - currentTime; TimeSpan difference = frameStart - previousTime;
currentTime = frameStart; previousTime = frameStart;
timePassedFromLastUpdate += difference; timePassedFromLastUpdate += difference;
while (timePassedFromLastUpdate > updateDeltaTime) { while (timePassedFromLastUpdate > updateDeltaTime) {
//Updates. //Updates.
//Events
SDL.SDL_Event SDL_Event;
while (SDL.SDL_PollEvent(out SDL_Event) != 0) {
switch (SDL_Event.type) {
case SDL.SDL_EventType.SDL_MOUSEMOTION:
Mouse.OnMouseMoved(SDL_Event.motion.x, SDL_Event.motion.y);
break;
case SDL.SDL_EventType.SDL_MOUSEWHEEL:
Mouse.OnScroll(SDL_Event.wheel.x, SDL_Event.wheel.y);
break;
case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN:
if (SDL.SDL_BUTTON_LEFT == SDL_Event.button.button) {
Mouse.OnLeftChange(true);
} else if (SDL.SDL_BUTTON_RIGHT == SDL_Event.button.button) {
Mouse.OnRightChange(true);
} else if (SDL.SDL_BUTTON_MIDDLE == SDL_Event.button.button) {
Mouse.OnMiddleChange(true);
}
break;
case SDL.SDL_EventType.SDL_MOUSEBUTTONUP:
if (SDL.SDL_BUTTON_LEFT == SDL_Event.button.button) {
Mouse.OnLeftChange(false);
} else if (SDL.SDL_BUTTON_RIGHT == SDL_Event.button.button) {
Mouse.OnRightChange(false);
} else if (SDL.SDL_BUTTON_MIDDLE == SDL_Event.button.button) {
Mouse.OnMiddleChange(false);
}
break;
case SDL.SDL_EventType.SDL_KEYDOWN:
Keyboard.OnKeyPressed(SDL_Event.key.keysym.sym);
break;
case SDL.SDL_EventType.SDL_KEYUP:
Keyboard.OnKeyReleased(SDL_Event.key.keysym.sym);
break;
case SDL.SDL_EventType.SDL_WINDOWEVENT:
WindowContext handle = WindowContextsManager.ContextFromWindowID(SDL_Event.window.windowID);
switch (SDL_Event.window.windowEvent)
{
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED:
handle.OnResize(SDL_Event.window.data1, SDL_Event.window.data2);
break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST:
handle.OnFocusLost();
break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED:
handle.OnFocusGained();
break;
}
break;
case SDL.SDL_EventType.SDL_QUIT:
Stop();
break;
}
}
manager.update(updateDeltaTime.TotalSeconds <= 0 ? timePassedFromLastUpdate.TotalSeconds : updateDeltaTime.TotalSeconds); manager.update(updateDeltaTime.TotalSeconds <= 0 ? timePassedFromLastUpdate.TotalSeconds : updateDeltaTime.TotalSeconds);
timePassedFromLastUpdate -= updateDeltaTime; timePassedFromLastUpdate -= updateDeltaTime;
if (updateDeltaTime.TotalSeconds <= 0) { if (updateDeltaTime.TotalSeconds <= 0) {
@ -174,6 +176,8 @@ namespace SlatedGameToolkit.Framework {
//Draw calls. //Draw calls.
manager.render(updateDeltaTime.TotalSeconds <= 0 ? updateDeltaTime.TotalSeconds : (timePassedFromLastUpdate / updateDeltaTime)); manager.render(updateDeltaTime.TotalSeconds <= 0 ? updateDeltaTime.TotalSeconds : (timePassedFromLastUpdate / updateDeltaTime));
timePassedFromLastRender = TimeSpan.Zero; timePassedFromLastRender = TimeSpan.Zero;
} else {
Thread.Yield();
} }
} }
} catch (Exception e) { } catch (Exception e) {