Skip to content

Fix paused #606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Core/Contents/Include/PolyCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ namespace Polycode {
String defaultWorkingDirectory;

void *userPointer;


//used for pausing workaround..
int frameRate;
long refreshInterval;
unsigned int timeSleptMs;

Expand Down
6 changes: 6 additions & 0 deletions Core/Contents/Include/PolyWinCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ class Gamepad_devicePrivate {
void initTouch();

void handleViewResize(int width, int height);

/**
* Handles focus changes
* @param newFocus bool value: true if gained focus, false if lost focus
*/
void handleFocusChange(bool newFocus);

String executeExternalCommand(String command, String args, String inDirectory);
std::vector<String> openFilePicker(std::vector<CoreFileExtension> extensions, bool allowMultiple);
Expand Down
6 changes: 6 additions & 0 deletions Core/Contents/PolycodeView/MSVC/PolycodeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_KILLFOCUS:
core->handleFocusChange(false);
break;
case WM_SETFOCUS:
core->handleFocusChange(true);
break;
default:
useDefault = true;
break;
Expand Down
32 changes: 17 additions & 15 deletions Core/Contents/Source/PolyCocoaCore.mm
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,17 @@ long getThreadID() {
}

void CocoaCore::warpCursor(int x, int y) {

CGSetLocalEventsSuppressionInterval(0);
NSArray *theScreens = [NSScreen screens];
for (NSScreen *theScreen in theScreens) {
CGPoint CenterOfWindow = CGPointMake([glView window].frame.origin.x+x, (-1)*([glView window].frame.origin.y-theScreen.frame.size.height)-yRes+y);
CGDisplayMoveCursorToPoint (kCGDirectMainDisplay, CenterOfWindow);
break;
if(!paused){
CGSetLocalEventsSuppressionInterval(0);
NSArray *theScreens = [NSScreen screens];
for (NSScreen *theScreen in theScreens) {
CGPoint CenterOfWindow = CGPointMake([glView window].frame.origin.x+x, (-1)*([glView window].frame.origin.y-theScreen.frame.size.height)-yRes+y);
CGDisplayMoveCursorToPoint (kCGDirectMainDisplay, CenterOfWindow);
break;
}
lastMouseX = x;
lastMouseY = y;
}
lastMouseX = x;
lastMouseY = y;
}


Expand Down Expand Up @@ -659,13 +660,14 @@ long getThreadID() {
if(!running)
return false;
doSleep();

if(modeChangeInfo.needResolutionChange) {
_setVideoMode(modeChangeInfo.xRes, modeChangeInfo.yRes, modeChangeInfo.fullScreen, modeChangeInfo.vSync, modeChangeInfo.aaLevel, modeChangeInfo.anisotropyLevel);
modeChangeInfo.needResolutionChange = false;
}
if(!paused){
if(modeChangeInfo.needResolutionChange) {
_setVideoMode(modeChangeInfo.xRes, modeChangeInfo.yRes, modeChangeInfo.fullScreen, modeChangeInfo.vSync, modeChangeInfo.aaLevel, modeChangeInfo.anisotropyLevel);
modeChangeInfo.needResolutionChange = false;
}

updateCore();
updateCore();
}
checkEvents();
return running;
}
Expand Down
12 changes: 10 additions & 2 deletions Core/Contents/Source/PolyCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace Polycode {
frameRate = 60;

setFramerate(frameRate);
refreshInterval = 1000 / frameRate;
threadedEventMutex = NULL;
}

Expand All @@ -98,6 +99,7 @@ namespace Polycode {
}

void Core::setFramerate(int frameRate, int maxFixedCycles) {
this->frameRate = frameRate;
refreshInterval = 1000 / frameRate;
fixedTimestep = 1.0 / ((double) frameRate);
maxFixedElapsed = fixedTimestep * maxFixedCycles;
Expand Down Expand Up @@ -168,6 +170,7 @@ namespace Polycode {
void Core::loseFocus() {
if(pauseOnLoseFocus) {
paused = true;
refreshInterval = 1000 / 2;
}
input->clearInput();
dispatchEvent(new Event(), EVENT_LOST_FOCUS);
Expand All @@ -177,7 +180,8 @@ namespace Polycode {
if(pauseOnLoseFocus) {
paused = false;
}
input->clearInput();
input->clearInput();
refreshInterval = 1000 / frameRate;
dispatchEvent(new Event(), EVENT_GAINED_FOCUS);
}

Expand All @@ -197,7 +201,11 @@ namespace Polycode {

bool Core::updateAndRender() {
bool ret = Update();
Render();

if (!paused){
Render();
}

return ret;
}

Expand Down
9 changes: 6 additions & 3 deletions Core/Contents/Source/PolySDLCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,10 @@ bool SDLCore::systemUpdate() {
return false;
doSleep();

updateCore();

if(!paused) {
updateCore();
}

SDL_Event event;
while ( SDL_PollEvent(&event) ) {
switch (event.type) {
Expand Down Expand Up @@ -417,7 +419,8 @@ void SDLCore::setCursor(int cursorType) {
}

void SDLCore::warpCursor(int x, int y) {
SDL_WarpMouse(x, y);
if(!paused)
SDL_WarpMouse(x, y);
}

void SDLCore::lockMutex(CoreMutex *mutex) {
Expand Down
62 changes: 44 additions & 18 deletions Core/Contents/Source/PolyWinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,15 @@ void Win32Core::captureMouse(bool newval) {
}

void Win32Core::warpCursor(int x, int y) {
POINT point;
point.x = x;
point.y = y;
ClientToScreen(hWnd, &point);
SetCursorPos(point.x,point.y);
lastMouseX = x;
lastMouseY = y;
if(!paused){
POINT point;
point.x = x;
point.y = y;
ClientToScreen(hWnd, &point);
SetCursorPos(point.x,point.y);
lastMouseX = x;
lastMouseY = y;
}
}

unsigned int Win32Core::getTicks() {
Expand All @@ -213,11 +215,13 @@ void Win32Core::Render() {
bool Win32Core::systemUpdate() {
if(!running)
return false;
captureMouse(Core::mouseCaptured);
doSleep();
checkEvents();
Gamepad_processEvents();
updateCore();
doSleep();
if(!paused){
checkEvents();
Gamepad_processEvents();
updateCore();
captureMouse(Core::mouseCaptured);
}
return running;
}

Expand Down Expand Up @@ -731,6 +735,25 @@ bool Win32Core::checkSpecialKeyEvents(PolyKEY key) {

void Win32Core::checkEvents() {
lockMutex(eventMutex);

if ((GetKeyState(VK_LBUTTON) & 0x80) != 0 && !input->getMouseButtonState(CoreInput::MOUSE_BUTTON1)){
input->setMouseButtonState(CoreInput::MOUSE_BUTTON1, true, getTicks());
} else if ((GetKeyState(VK_LBUTTON) & 0x80) == 0 && input->getMouseButtonState(CoreInput::MOUSE_BUTTON1)) {
input->setMouseButtonState(CoreInput::MOUSE_BUTTON1, false, getTicks());
}

if ((GetKeyState(VK_RBUTTON) & 0x80) != 0 && !input->getMouseButtonState(CoreInput::MOUSE_BUTTON2)){
input->setMouseButtonState(CoreInput::MOUSE_BUTTON2, true, getTicks());
} else if ((GetKeyState(VK_RBUTTON) & 0x80) == 0 && input->getMouseButtonState(CoreInput::MOUSE_BUTTON2)){
input->setMouseButtonState(CoreInput::MOUSE_BUTTON2, false, getTicks());
}

if ((GetKeyState(VK_MBUTTON) & 0x80) != 0 && !input->getMouseButtonState(CoreInput::MOUSE_BUTTON3)){
input->setMouseButtonState(CoreInput::MOUSE_BUTTON3, true, getTicks());
} else if ((GetKeyState(VK_MBUTTON) & 0x80) == 0 && input->getMouseButtonState(CoreInput::MOUSE_BUTTON3)) {
input->setMouseButtonState(CoreInput::MOUSE_BUTTON3, false, getTicks());
}

Win32Event event;
for(int i=0; i < win32Events.size(); i++) {
event = win32Events[i];
Expand All @@ -752,12 +775,6 @@ void Win32Core::checkEvents() {
lastMouseY = event.mouseY;
input->setMousePosition(event.mouseX, event.mouseY, getTicks());
break;
case InputEvent::EVENT_MOUSEDOWN:
input->setMouseButtonState(event.mouseButton, true, getTicks());
break;
case InputEvent::EVENT_MOUSEUP:
input->setMouseButtonState(event.mouseButton, false, getTicks());
break;
case InputEvent::EVENT_MOUSEWHEEL_UP:
input->mouseWheelUp(getTicks());
break;
Expand Down Expand Up @@ -1411,3 +1428,12 @@ String Win32Core::getClipboardString() {
GlobalUnlock(clip0);
return retString;
}

void Win32Core::handleFocusChange(bool newFocus){
if (newFocus){
gainFocus();
}
else {
loseFocus();
}
}
6 changes: 6 additions & 0 deletions IDE/Build/WindowsShared/PolycodeWinIDEView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_KILLFOCUS:
core->handleFocusChange(false);
break;
case WM_SETFOCUS:
core->handleFocusChange(true);
break;
default:
useDefault = true;
break;
Expand Down
82 changes: 38 additions & 44 deletions IDE/Contents/Source/PolycodeIDEApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ PolycodeIDEApp::PolycodeIDEApp(PolycodeView *view) : EventDispatcher() {
core = new POLYCODE_CORE((PolycodeView*)view, 1100, 700,false,false, 0, 0,60, -1, true);
#endif

// core->pauseOnLoseFocus = true;
core->pauseOnLoseFocus = true;

printf("DIR: %s\n", core->getDefaultWorkingDirectory().c_str());

Expand All @@ -55,8 +55,6 @@ core = new POLYCODE_CORE((PolycodeView*)view, 1100, 700,false,false, 0, 0,60, -1
runNextFrame = false;

core->addEventListener(this, Core::EVENT_CORE_RESIZE);
core->addEventListener(this, Core::EVENT_LOST_FOCUS);
core->addEventListener(this, Core::EVENT_GAINED_FOCUS);

globalClipboard = new PolycodeClipboard();

Expand Down Expand Up @@ -483,6 +481,8 @@ void PolycodeIDEApp::doRunProject() {

frame->showConsole();

CoreServices::getInstance()->getCore()->pauseOnLoseFocus = false;

String outPath = PolycodeToolLauncher::generateTempPath(projectManager->getActiveProject()) + ".polyapp";
PolycodeToolLauncher::buildProject(projectManager->getActiveProject(), outPath, false);
PolycodeToolLauncher::runPolyapp(outPath);
Expand Down Expand Up @@ -828,12 +828,6 @@ void PolycodeIDEApp::handleEvent(Event *event) {

if(event->getDispatcher() == core) {
switch(event->getEventCode()) {
case Core::EVENT_LOST_FOCUS:
core->setFramerate(3);
break;
case Core::EVENT_GAINED_FOCUS:
core->setFramerate(60);
break;
case Core::EVENT_CORE_RESIZE:
if(menuBar) {
frame->Resize(core->getXRes(), core->getYRes()-25);
Expand Down Expand Up @@ -1368,51 +1362,51 @@ PolycodeIDEApp::~PolycodeIDEApp() {

bool PolycodeIDEApp::Update() {

if(!core->paused){
if(willRunProject) {
willRunProject = false;
runProject();
}

if(willRunProject) {
willRunProject = false;
runProject();
}

if(runNextFrame) {
runNextFrame = false;
doRunProject();
}
if(runNextFrame) {
runNextFrame = false;
doRunProject();
}

if(lastConnected != debugger->isConnected()) {
needsRedraw = true;
lastConnected = debugger->isConnected();
}
if(lastConnected != debugger->isConnected()) {
needsRedraw = true;
lastConnected = debugger->isConnected();
}

if(debugger->isConnected()) {
frame->stopButton->visible = true;
frame->stopButton->enabled = true;
if(debugger->isConnected()) {
frame->stopButton->visible = true;
frame->stopButton->enabled = true;

frame->playButton->visible = false;
frame->playButton->enabled = false;
frame->playButton->visible = false;
frame->playButton->enabled = false;

} else {
frame->stopButton->visible = false;
frame->stopButton->enabled = false;
} else {
frame->stopButton->visible = false;
frame->stopButton->enabled = false;

frame->playButton->visible = true;
frame->playButton->enabled = true;
}
frame->playButton->visible = true;
frame->playButton->enabled = true;
}


if(projectManager->getProjectCount() == 1) {
projectManager->setActiveProject(projectManager->getProjectByIndex(0));
}
if(projectManager->getProjectCount() == 1) {
projectManager->setActiveProject(projectManager->getProjectByIndex(0));
}

if(projectManager->getProjectCount() > 0) {
frame->welcomeEntity->enabled = false;
if(projectManager->getProjectCount() > 0) {
frame->welcomeEntity->enabled = false;

frame->getConsoleSizer()->enabled = true;
} else {
frame->welcomeEntity->enabled = true;
frame->getConsoleSizer()->enabled = false;
}

frame->getConsoleSizer()->enabled = true;
} else {
frame->welcomeEntity->enabled = true;
frame->getConsoleSizer()->enabled = false;
}
}

return core->updateAndRender();
}
Expand Down
Loading