From 52965eb9d822dcd89dbde355f4ec3e76cc1e2591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=C2=ADn=20Lucas=20Golini?= Date: Sun, 24 Nov 2019 01:15:39 -0300 Subject: [PATCH] Crash fix on DisplayManager. UINodeDrawable clean ups. Added Node::removeAction(), Node:removeActionByTag(), Node::clearActions(). Store display DPI on scene node creation. --HG-- branch : dev --- bin/assets/layouts/test.css | 20 ++- bin/assets/layouts/test.xml | 2 + include/eepp/scene/node.hpp | 6 + include/eepp/scene/scenenode.hpp | 3 + include/eepp/ui/uinodedrawable.hpp | 13 +- src/eepp/scene/node.cpp | 12 ++ src/eepp/scene/scenenode.cpp | 9 + src/eepp/ui/uinode.cpp | 7 +- src/eepp/ui/uinodedrawable.cpp | 158 +++++++++--------- src/eepp/ui/uiwidget.cpp | 13 ++ .../backend/SDL2/displaymanagersdl2.cpp | 18 +- src/eepp/window/backend/SDL2/windowsdl2.cpp | 11 +- 12 files changed, 169 insertions(+), 103 deletions(-) diff --git a/bin/assets/layouts/test.css b/bin/assets/layouts/test.css index 707eb0006..62670d73e 100644 --- a/bin/assets/layouts/test.css +++ b/bin/assets/layouts/test.css @@ -290,6 +290,7 @@ TabWidget { background-size: auto; background-position: bottom right; padding: 24dp; + transition: all 1s; } #rrl:hover { @@ -297,10 +298,27 @@ TabWidget { background-position: top left; } +#rrl > .real_back { + background-color: #333; +} + +#rrl > .back { + width: 250dp; + height: 40dp; + layout-gravity: center; + background-color: #333; + transition: all 0.5s; +} + +#rrl:hover > .back { + width: 300dp; + height: 300dp; + background-color: black; +} + #rrl > #tmap { font-size: 24dp; padding: 12dp; - background-color: #333; } .eltv { diff --git a/bin/assets/layouts/test.xml b/bin/assets/layouts/test.xml index 9c13d1d87..e63eccf55 100644 --- a/bin/assets/layouts/test.xml +++ b/bin/assets/layouts/test.xml @@ -30,6 +30,8 @@ + + diff --git a/include/eepp/scene/node.hpp b/include/eepp/scene/node.hpp index 652591b8e..01bee6845 100644 --- a/include/eepp/scene/node.hpp +++ b/include/eepp/scene/node.hpp @@ -274,6 +274,12 @@ class EE_API Node : public Transformable { Node * runAction( Action * action ); + void removeAction( Action * action ); + + void removeActionByTag( const Uint32& tag ); + + void clearActions(); + Transform getLocalTransform() const; Transform getGlobalTransform() const; diff --git a/include/eepp/scene/scenenode.hpp b/include/eepp/scene/scenenode.hpp index 6b7415dbe..eb27eb72e 100644 --- a/include/eepp/scene/scenenode.hpp +++ b/include/eepp/scene/scenenode.hpp @@ -112,6 +112,8 @@ class EE_API SceneNode : public Node { const bool& getUpdateAllChilds() const; void setUpdateAllChilds( const bool& updateAllChilds ); + + const Float& getDPI() const; protected: friend class Node; typedef std::list CloseList; @@ -138,6 +140,7 @@ class EE_API SceneNode : public Node { std::list mScheduledUpdate; std::list mScheduledUpdateRemove; std::list mMouseOverNodes; + Float mDPI; virtual void onSizeChange(); diff --git a/include/eepp/ui/uinodedrawable.hpp b/include/eepp/ui/uinodedrawable.hpp index 2c1225695..dd0a96640 100644 --- a/include/eepp/ui/uinodedrawable.hpp +++ b/include/eepp/ui/uinodedrawable.hpp @@ -51,8 +51,12 @@ class EE_API UINodeDrawable : public Drawable { void setPositionEq( const std::string& offset ); + const std::string& getPositionEq() const { return mPositionEq; } + void setSizeEq( const std::string& size ); + const std::string& getSizeEq() const { return mDrawableSizeEq; } + const Repeat& getRepeat() const; void setRepeat( const Repeat& repeat ); @@ -62,6 +66,10 @@ class EE_API UINodeDrawable : public Drawable { const Sizef& getDrawableSize() const; void setDrawableSize( const Sizef& drawableSize ); + + Sizef calcDrawableSize( const std::string& drawableSizeEq ); + + Vector2f calcPosition( const std::string& positionEq ); protected: UINodeDrawable * mContainer; Sizef mSize; @@ -70,7 +78,6 @@ class EE_API UINodeDrawable : public Drawable { std::string mPositionEq; std::string mDrawableSizeEq; bool mNeedsUpdate; - bool mUpdatePosEq; bool mOwnsDrawable; Drawable * mDrawable; Uint32 mResourceChangeCbId; @@ -107,6 +114,8 @@ class EE_API UINodeDrawable : public Drawable { Uint32 getBorderRadius() const; + bool layerExists( int index ); + LayerDrawable * getLayer( int index ); void setDrawable( int index, Drawable * drawable, bool ownIt ); @@ -132,8 +141,6 @@ class EE_API UINodeDrawable : public Drawable { UINode * mOwner; RectangleDrawable mBackgroundColor; std::map mGroup; - std::map mPosEq; - std::map mSizeEq; Sizef mSize; bool mNeedsUpdate; bool mClipEnabled; diff --git a/src/eepp/scene/node.cpp b/src/eepp/scene/node.cpp index bae715291..43d1df6b3 100644 --- a/src/eepp/scene/node.cpp +++ b/src/eepp/scene/node.cpp @@ -1288,6 +1288,18 @@ Node * Node::runAction( Action * action ) { return this; } +void Node::removeAction( Action * action ) { + getActionManager()->removeAction( action ); +} + +void Node::removeActionByTag( const Uint32& tag ) { + getActionManager()->removeActionByTag( tag ); +} + +void Node::clearActions() { + getActionManager()->removeAllActionsFromTarget( this ); +} + void Node::runOnMainThread( Actions::Runnable::RunnableFunc runnable, const Time& delay ) { runAction( Actions::Runnable::New( runnable, delay ) ); } diff --git a/src/eepp/scene/scenenode.cpp b/src/eepp/scene/scenenode.cpp index 33d557a26..23c195ba8 100644 --- a/src/eepp/scene/scenenode.cpp +++ b/src/eepp/scene/scenenode.cpp @@ -46,6 +46,11 @@ SceneNode::SceneNode( EE::Window::Window * window ) : mResizeCb = mWindow->pushResizeCallback( cb::Make1( this, &SceneNode::resizeControl ) ); + DisplayManager * displayManager = Engine::instance()->getDisplayManager(); + int currentDisplayIndex = getWindow()->getCurrentDisplayIndex(); + Display * currentDisplay = displayManager->getDisplayIndex( currentDisplayIndex ); + mDPI = currentDisplay->getDPI(); + resizeControl( window ); } @@ -448,4 +453,8 @@ void SceneNode::setUpdateAllChilds( const bool& updateAllChilds ) { mUpdateAllChilds = updateAllChilds; } +const Float& SceneNode::getDPI() const { + return mDPI; +} + }} diff --git a/src/eepp/ui/uinode.cpp b/src/eepp/ui/uinode.cpp index 68b66513a..e4c58cda1 100644 --- a/src/eepp/ui/uinode.cpp +++ b/src/eepp/ui/uinode.cpp @@ -991,12 +991,7 @@ Float UINode::lengthAsPixels( const CSS::StyleSheetLength& length, const Sizef& percentAsWidth ? getParent()->getPixelsSize().getWidth() - drawableSize.getWidth() : getParent()->getPixelsSize().getHeight() - drawableSize.getHeight(), getSceneNode()->getPixelsSize(), - Engine::instance()->getDisplayManager()->getDisplayIndex( - getSceneNode()->getWindow()->getCurrentDisplayIndex() - )->getDPI(), - 12, - 12 - ); + getSceneNode()->getDPI(), 12, 12 ); } Float UINode::lengthAsDp( const CSS::StyleSheetLength& length, const Sizef& drawableSize, const bool& percentAsWidth ) { diff --git a/src/eepp/ui/uinodedrawable.cpp b/src/eepp/ui/uinodedrawable.cpp index 6e0d40238..cc263702a 100644 --- a/src/eepp/ui/uinodedrawable.cpp +++ b/src/eepp/ui/uinodedrawable.cpp @@ -48,6 +48,10 @@ Uint32 UINodeDrawable::getBorderRadius() const { return mBackgroundColor.getCorners(); } +bool UINodeDrawable::layerExists( int index ) { + return mGroup.find( index ) != mGroup.end(); +} + UINodeDrawable::LayerDrawable* UINodeDrawable::getLayer( int index ) { auto it = mGroup.find(index); @@ -60,15 +64,11 @@ UINodeDrawable::LayerDrawable* UINodeDrawable::getLayer( int index ) { void UINodeDrawable::setDrawable( int index, Drawable* drawable, bool ownIt ) { if ( drawable != getLayer( index )->getDrawable() ) { getLayer( index )->setDrawable( drawable, ownIt ); - mNeedsUpdate = true; } } void UINodeDrawable::setDrawablePosition( int index, const std::string& positionEq ) { - if ( mPosEq[index] != positionEq ) { - mPosEq[index] = positionEq; - mNeedsUpdate = true; - } + getLayer( index )->setPositionEq( positionEq ); } void UINodeDrawable::setDrawableRepeat( int index, const std::string& repeatRule ) { @@ -76,10 +76,7 @@ void UINodeDrawable::setDrawableRepeat( int index, const std::string& repeatRule } void UINodeDrawable::setDrawableSize( int index, const std::string& sizeEq ) { - if ( mSizeEq[index] != sizeEq ) { - mSizeEq[index] = sizeEq; - mNeedsUpdate = true; - } + getLayer( index )->setSizeEq( sizeEq ); } void UINodeDrawable::setBackgroundColor(const Color& color) { @@ -182,15 +179,6 @@ void UINodeDrawable::onSizeChange() { void UINodeDrawable::update() { mBackgroundColor.setPosition( mPosition ); mBackgroundColor.setSize( mSize ); - - for ( size_t i = 0; i < mGroup.size(); i++ ) { - UINodeDrawable::LayerDrawable * drawable = mGroup[i]; - drawable->setPosition( mPosition ); - drawable->setSizeEq( mSizeEq[i].empty() ? "auto" : mSizeEq[i] ); - drawable->setPositionEq( mPosEq[i] ); - drawable->setSize( mSize ); - } - mNeedsUpdate = false; } @@ -203,9 +191,9 @@ UINodeDrawable::LayerDrawable * UINodeDrawable::LayerDrawable::New( UINodeDrawab UINodeDrawable::LayerDrawable::LayerDrawable( UINodeDrawable * container ) : Drawable(UINODEDRAWABLE_LAYERDRAWABLE), mContainer(container), + mPositionEq("0px 0px"), mDrawableSizeEq("auto"), mNeedsUpdate(false), - mUpdatePosEq(false), mOwnsDrawable(false), mDrawable(NULL), mResourceChangeCbId(0) @@ -321,7 +309,6 @@ void UINodeDrawable::LayerDrawable::setRepeat( const UINodeDrawable::Repeat& rep void UINodeDrawable::LayerDrawable::invalidate() { mNeedsUpdate = true; - mUpdatePosEq = true; } const Sizef& UINodeDrawable::LayerDrawable::getDrawableSize() const { @@ -332,40 +319,35 @@ void UINodeDrawable::LayerDrawable::setDrawableSize( const Sizef& drawableSize ) mDrawableSize = drawableSize; } -void UINodeDrawable::LayerDrawable::onPositionChange() { - invalidate(); -} +Sizef UINodeDrawable::LayerDrawable::calcDrawableSize( const std::string& drawableSizeEq ) { + Sizef size; -void UINodeDrawable::LayerDrawable::update() { - if ( mDrawable == NULL ) - return; - - if ( mDrawableSizeEq == "auto" ) { + if ( drawableSizeEq == "auto" ) { if ( mDrawable->getDrawableType() == Drawable::RECTANGLE ) { - mDrawableSize = mSize; + size = mSize; } else { - mDrawableSize = mDrawable->getSize(); + size = mDrawable->getSize(); } - } else if ( mDrawableSizeEq == "expand" ) { - mDrawableSize = mSize; - } else if ( mDrawableSizeEq == "contain" ) { + } else if ( drawableSizeEq == "expand" ) { + size = mSize; + } else if ( drawableSizeEq == "contain" ) { Sizef drawableSize( mDrawable->getSize() ); Float Scale1 = mSize.getWidth() / drawableSize.getWidth(); Float Scale2 = mSize.getHeight() / drawableSize.getHeight(); if ( Scale1 < 1 || Scale2 < 1 ) { Scale1 = eemin( Scale1, Scale2 ); - mDrawableSize = Sizef( drawableSize.getWidth() * Scale1, drawableSize.getHeight() * Scale1 ); + size = Sizef( drawableSize.getWidth() * Scale1, drawableSize.getHeight() * Scale1 ); } else { - mDrawableSize = drawableSize; + size = drawableSize; } - } else if ( mDrawableSizeEq == "cover" ) { + } else if ( drawableSizeEq == "cover" ) { Sizef drawableSize( mDrawable->getSize() ); Float Scale1 = mSize.getWidth() / drawableSize.getWidth(); Float Scale2 = mSize.getHeight() / drawableSize.getHeight(); Scale1 = eemax( Scale1, Scale2 ); - mDrawableSize = Sizef( drawableSize.getWidth() * Scale1, drawableSize.getHeight() * Scale1 ); + size = Sizef( drawableSize.getWidth() * Scale1, drawableSize.getHeight() * Scale1 ); } else { - std::vector sizePart = String::split( mDrawableSizeEq, ' ' ); + std::vector sizePart = String::split( drawableSizeEq, ' ' ); if ( sizePart.size() == 1 ) { sizePart.push_back( "auto" ); @@ -374,75 +356,89 @@ void UINodeDrawable::LayerDrawable::update() { if ( sizePart.size() == 2 ) { if ( sizePart[0] == "auto" && sizePart[1] == "auto" ) { if ( mDrawable->getDrawableType() == Drawable::RECTANGLE ) { - mDrawableSize = mSize; + size = mSize; } else { - mDrawableSize = mDrawable->getSize(); + size = mDrawable->getSize(); } } else if ( sizePart[0] != "auto" ) { CSS::StyleSheetLength wl( CSS::StyleSheetLength::fromString( sizePart[0] ) ); - mDrawableSize.x = mContainer->getOwner()->lengthAsPixels( wl, Sizef::Zero, true ); + size.x = mContainer->getOwner()->lengthAsPixels( wl, Sizef::Zero, true ); if ( sizePart[1] == "auto" ) { Sizef drawableSize( mDrawable->getSize() ); - mDrawableSize.y = drawableSize.getHeight() * ( mDrawableSize.getWidth() / drawableSize.getWidth() ); + size.y = drawableSize.getHeight() * ( size.getWidth() / drawableSize.getWidth() ); } else { CSS::StyleSheetLength hl( CSS::StyleSheetLength::fromString( sizePart[1] ) ); - mDrawableSize.y = mContainer->getOwner()->lengthAsPixels( hl, Sizef::Zero, false ); + size.y = mContainer->getOwner()->lengthAsPixels( hl, Sizef::Zero, false ); } } else { CSS::StyleSheetLength hl( CSS::StyleSheetLength::fromString( sizePart[1] ) ); - mDrawableSize.y = mContainer->getOwner()->lengthAsPixels( hl, Sizef::Zero, false ); + size.y = mContainer->getOwner()->lengthAsPixels( hl, Sizef::Zero, false ); Sizef drawableSize( mDrawable->getSize() ); - mDrawableSize.x = drawableSize.getWidth() * ( mDrawableSize.getHeight() / drawableSize.getHeight() ); + size.x = drawableSize.getWidth() * ( size.getHeight() / drawableSize.getHeight() ); } } } - if ( mUpdatePosEq ) { - std::vector pos = String::split( mPositionEq, ' ' ); + return size; +} - if ( pos.size() == 1 ) { - pos.push_back( "center" ); +Vector2f UINodeDrawable::LayerDrawable::calcPosition( const std::string& positionEq ) { + Vector2f position( Vector2f::Zero ); + std::vector pos = String::split( positionEq, ' ' ); + + if ( pos.size() == 1 ) + pos.push_back( "center" ); + + if ( pos.size() == 2 ) { + CSS::StyleSheetLength xl( CSS::StyleSheetLength::fromString( pos[0] ) ); + CSS::StyleSheetLength yl( CSS::StyleSheetLength::fromString( pos[1] ) ); + position.x = mContainer->getOwner()->lengthAsPixels( xl, mDrawableSize, true ); + position.y = mContainer->getOwner()->lengthAsPixels( yl, mDrawableSize, false ); + } else if ( pos.size() > 2 ) { + if ( pos.size() == 3 ) { + pos.push_back( "0dp" ); } - if ( pos.size() == 2 ) { - CSS::StyleSheetLength xl( CSS::StyleSheetLength::fromString( pos[0] ) ); - CSS::StyleSheetLength yl( CSS::StyleSheetLength::fromString( pos[1] ) ); - mOffset.x = mContainer->getOwner()->lengthAsPixels( xl, mDrawableSize, true ); - mOffset.y = mContainer->getOwner()->lengthAsPixels( yl, mDrawableSize, false ); - } else if ( pos.size() > 2 ) { - if ( pos.size() == 3 ) { - pos.push_back( "0dp" ); - } + int xFloatIndex = 0; + int yFloatIndex = 2; - int xFloatIndex = 0; - int yFloatIndex = 2; - - if ( "bottom" == pos[0] || "top" == pos[0] ) { - xFloatIndex = 2; - yFloatIndex = 0; - } - - CSS::StyleSheetLength xl1( CSS::StyleSheetLength::fromString( pos[xFloatIndex] ) ); - CSS::StyleSheetLength xl2( CSS::StyleSheetLength::fromString( pos[xFloatIndex+1] ) ); - CSS::StyleSheetLength yl1( CSS::StyleSheetLength::fromString( pos[yFloatIndex] ) ); - CSS::StyleSheetLength yl2( CSS::StyleSheetLength::fromString( pos[yFloatIndex+1] ) ); - - mOffset.x = mContainer->getOwner()->lengthAsPixels( xl1, mDrawableSize, true ); - - Float xl2Val = mContainer->getOwner()->lengthAsPixels( xl2, mDrawableSize, true ); - mOffset.x += ( pos[xFloatIndex] == "right" ) ? -xl2Val : xl2Val; - - mOffset.y = mContainer->getOwner()->lengthAsPixels( yl1, mDrawableSize, false ); - - Float yl2Val = mContainer->getOwner()->lengthAsPixels( yl2, mDrawableSize, false ); - mOffset.y += ( pos[yFloatIndex] == "bottom" ) ? -yl2Val : yl2Val; + if ( "bottom" == pos[0] || "top" == pos[0] ) { + xFloatIndex = 2; + yFloatIndex = 0; } - mUpdatePosEq = false; + CSS::StyleSheetLength xl1( CSS::StyleSheetLength::fromString( pos[xFloatIndex] ) ); + CSS::StyleSheetLength xl2( CSS::StyleSheetLength::fromString( pos[xFloatIndex+1] ) ); + CSS::StyleSheetLength yl1( CSS::StyleSheetLength::fromString( pos[yFloatIndex] ) ); + CSS::StyleSheetLength yl2( CSS::StyleSheetLength::fromString( pos[yFloatIndex+1] ) ); + + position.x = mContainer->getOwner()->lengthAsPixels( xl1, mDrawableSize, true ); + + Float xl2Val = mContainer->getOwner()->lengthAsPixels( xl2, mDrawableSize, true ); + position.x += ( pos[xFloatIndex] == "right" ) ? -xl2Val : xl2Val; + + position.y = mContainer->getOwner()->lengthAsPixels( yl1, mDrawableSize, false ); + + Float yl2Val = mContainer->getOwner()->lengthAsPixels( yl2, mDrawableSize, false ); + position.y += ( pos[yFloatIndex] == "bottom" ) ? -yl2Val : yl2Val; } + return position; +} + +void UINodeDrawable::LayerDrawable::onPositionChange() { + invalidate(); +} + +void UINodeDrawable::LayerDrawable::update() { + if ( mDrawable == NULL ) + return; + + mDrawableSize = calcDrawableSize( mDrawableSizeEq ); + mOffset = calcPosition( mPositionEq ); + mNeedsUpdate = false; } diff --git a/src/eepp/ui/uiwidget.cpp b/src/eepp/ui/uiwidget.cpp index f06b8888e..e7126cbd1 100644 --- a/src/eepp/ui/uiwidget.cpp +++ b/src/eepp/ui/uiwidget.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -886,12 +887,15 @@ bool UIWidget::setAttribute( const NodeAttribute& attribute, const Uint32& state if ( !isSceneNodeLoading() && NULL != mStyle && mStyle->hasTransition( attribute.getName() ) ) { UIStyle::TransitionInfo transitionInfo( mStyle->getTransition( attribute.getName() ) ); + Uint32 tag = String::hash("width"); Action * action = Actions::ResizeWidth::New( getSize().getWidth(), newWidth, transitionInfo.duration, transitionInfo.timingFunction ); if ( Time::Zero != transitionInfo.delay ) action = Actions::Sequence::New( Actions::Delay::New( transitionInfo.delay ), action ); + action->setTag( tag ); + removeActionByTag( tag ); runAction( action ); } else { setInternalWidth( newWidth ); @@ -905,6 +909,7 @@ bool UIWidget::setAttribute( const NodeAttribute& attribute, const Uint32& state Float newHeight = attribute.asDpDimensionI(); if ( !isSceneNodeLoading() && NULL != mStyle && mStyle->hasTransition( attribute.getName() ) ) { + Uint32 tag = String::hash("height"); UIStyle::TransitionInfo transitionInfo( mStyle->getTransition( attribute.getName() ) ); Action * action = Actions::ResizeHeight::New( getSize().getHeight(), newHeight, transitionInfo.duration, transitionInfo.timingFunction ); @@ -912,6 +917,8 @@ bool UIWidget::setAttribute( const NodeAttribute& attribute, const Uint32& state if ( Time::Zero != transitionInfo.delay ) action = Actions::Sequence::New( Actions::Delay::New( transitionInfo.delay ), action ); + action->setTag( tag ); + removeActionByTag( tag ); runAction( action ); } else { setInternalHeight( newHeight ); @@ -946,10 +953,14 @@ bool UIWidget::setAttribute( const NodeAttribute& attribute, const Uint32& state setBackgroundDrawable( drawable, ownIt, index ); } ); } else if ( "background-position" == name || "backgroundposition" == name ) { + SAVE_NORMAL_STATE_ATTR( getBackground()->getLayer(0)->getPositionEq() ); + setBackgroundPosition( attribute.value(), 0 ); } else if ( "background-repeat" == name || "backgroundrepeat" == name ) { setBackgroundRepeat( attribute.value(), 0 ); } else if ( "background-size" == name || "backgroundsize" == name ) { + SAVE_NORMAL_STATE_ATTR( getBackground()->getLayer(0)->getSizeEq() ); + setBackgroundSize( attribute.value(), 0 ); } else if ( "foreground" == name ) { if ( Color::isColorString( attribute.getValue() ) ) { @@ -983,6 +994,8 @@ bool UIWidget::setAttribute( const NodeAttribute& attribute, const Uint32& state SAVE_NORMAL_STATE_ATTR( String::toStr( getForegroundRadius() ) ); setForegroundRadius( attribute.asUint() ); + } else if ( "foreground-position" == name || "foregroundposition" == name ) { + setForegroundPosition( attribute.value(), 0 ); } else if ( "foreground-size" == name || "foregroundsize" == name ) { setForegroundSize( attribute.value(), 0 ); } else if ( "border-color" == name || "bordercolor" == name ) { diff --git a/src/eepp/window/backend/SDL2/displaymanagersdl2.cpp b/src/eepp/window/backend/SDL2/displaymanagersdl2.cpp index 66f3514fc..1f2bd77b6 100644 --- a/src/eepp/window/backend/SDL2/displaymanagersdl2.cpp +++ b/src/eepp/window/backend/SDL2/displaymanagersdl2.cpp @@ -34,28 +34,28 @@ const int& DisplaySDL2::getIndex() const { const std::vector& DisplaySDL2::getModes() const { if ( displayModes.empty() ) { int count = SDL_GetNumDisplayModes( index ); - + if ( count > 0 ) { for ( int mode_index = 0; mode_index < count; mode_index++ ) { SDL_DisplayMode mode; - + if ( SDL_GetDisplayMode( index, mode_index, &mode ) == 0 ) { displayModes.push_back( DisplayMode( mode.w, mode.h, mode.refresh_rate, index ) ); } } } } - + return displayModes; } DisplayMode DisplaySDL2::getCurrentMode() { SDL_DisplayMode mode; - + if ( SDL_GetCurrentDisplayMode(index, &mode) == 0 ) { return DisplayMode( mode.w, mode.h, mode.refresh_rate, index ); } - + return DisplayMode(0,0,0,0); } @@ -67,11 +67,11 @@ DisplayMode DisplaySDL2::getClosestDisplayMode( DisplayMode wantedMode ) { target.format = 0; target.refresh_rate = wantedMode.RefreshRate; target.driverdata = 0; - + if ( SDL_GetClosestDisplayMode(0, &target, &mode) != NULL ) { return DisplayMode( mode.w, mode.h, mode.refresh_rate, index ); } - + return DisplayMode(0,0,0,0); } @@ -85,13 +85,11 @@ Rect DisplaySDL2::getUsableBounds() { } int DisplayManagerSDL2::getDisplayCount() { - SDL_Init(SDL_INIT_VIDEO); + if ( !SDL_WasInit(SDL_INIT_VIDEO) ) SDL_Init(SDL_INIT_VIDEO); return SDL_GetNumVideoDisplays(); } Display * DisplayManagerSDL2::getDisplayIndex( int index ) { - SDL_Init(SDL_INIT_VIDEO); - if ( displays.empty() ) { int count = getDisplayCount(); diff --git a/src/eepp/window/backend/SDL2/windowsdl2.cpp b/src/eepp/window/backend/SDL2/windowsdl2.cpp index 303907618..a61edb72f 100644 --- a/src/eepp/window/backend/SDL2/windowsdl2.cpp +++ b/src/eepp/window/backend/SDL2/windowsdl2.cpp @@ -253,7 +253,14 @@ void WindowSDL::unsetGLContextThread() { } int WindowSDL::getCurrentDisplayIndex() { - return SDL_GetWindowDisplayIndex( mSDLWindow ); + int index = SDL_GetWindowDisplayIndex( mSDLWindow ); + + if ( index < 0 ) { + eePRINTL( SDL_GetError() ); + return 0; + } + + return index; } std::string WindowSDL::getVersion() { @@ -407,7 +414,7 @@ void WindowSDL::setSize( Uint32 Width, Uint32 Height, bool Windowed ) { sendVideoResizeCb(); } -void WindowSDL::swapBuffers() { +void WindowSDL::swapBuffers() { SDL_GL_SwapWindow( mSDLWindow ); }