Skip to content

Commit

Permalink
OpenGL context now supports high DPI displays. Added operator* to Rec…
Browse files Browse the repository at this point in the history
…tangle class.
  • Loading branch information
jules committed Nov 15, 2012
1 parent 667a187 commit f6ce67e
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 95 deletions.
23 changes: 12 additions & 11 deletions extras/JuceDemo/Source/demos/OpenGLDemo.cpp
Expand Up @@ -109,14 +109,8 @@ class DemoOpenGLCanvas : public Component,
{
OpenGLHelpers::clear (Colours::darkgrey.withAlpha (1.0f));

{
MessageManagerLock mm (Thread::getCurrentThread());
if (! mm.lockWasGained())
return;

updateTextureImage(); // this will update our dynamically-changing texture image.
drawBackground2DStuff(); // draws some 2D content to demonstrate the OpenGLGraphicsContext class
}
updateTextureImage(); // this will update our dynamically-changing texture image.
drawBackground2DStuff(); // draws some 2D content to demonstrate the OpenGLGraphicsContext class

// Having used the juce 2D renderer, it will have messed-up a whole load of GL state, so
// we'll put back any important settings before doing our normal GL 3D drawing..
Expand All @@ -127,8 +121,8 @@ class DemoOpenGLCanvas : public Component,
glEnable (GL_TEXTURE_2D);

#if JUCE_USE_OPENGL_FIXED_FUNCTION
OpenGLHelpers::prepareFor2D (getWidth(), getHeight());
OpenGLHelpers::setPerspective (45.0, getWidth() / (double) getHeight(), 0.1, 100.0);
OpenGLHelpers::prepareFor2D (getContextWidth(), getContextHeight());
OpenGLHelpers::setPerspective (45.0, getContextWidth() / (double) getContextHeight(), 0.1, 100.0);

glTranslatef (0.0f, 0.0f, -5.0f);
draggableOrientation.applyToOpenGLMatrix();
Expand Down Expand Up @@ -173,11 +167,14 @@ class DemoOpenGLCanvas : public Component,
void drawBackground2DStuff()
{
// Create an OpenGLGraphicsContext that will draw into this GL window..
ScopedPointer<LowLevelGraphicsContext> glRenderer (createOpenGLGraphicsContext (openGLContext));
ScopedPointer<LowLevelGraphicsContext> glRenderer (createOpenGLGraphicsContext (openGLContext,
getContextWidth(),
getContextHeight()));

if (glRenderer != nullptr)
{
Graphics g (glRenderer);
g.addTransform (AffineTransform::scale ((float) getScale()));

// This stuff just creates a spinning star shape and fills it..
Path p;
Expand All @@ -194,6 +191,10 @@ class DemoOpenGLCanvas : public Component,
}
}

double getScale() const { return Desktop::getInstance().getDisplays().getDisplayContaining (getScreenBounds().getCentre()).scale; }
int getContextWidth() const { return roundToInt (getScale() * getWidth()); }
int getContextHeight() const { return roundToInt (getScale() * getHeight()); }

void timerCallback()
{
rotation += (float) speedSlider.getValue();
Expand Down
8 changes: 6 additions & 2 deletions modules/juce_graphics/geometry/juce_AffineTransform.cpp
Expand Up @@ -149,8 +149,12 @@ AffineTransform AffineTransform::scaled (const float factorX, const float factor

AffineTransform AffineTransform::scale (const float factorX, const float factorY) noexcept
{
return AffineTransform (factorX, 0, 0,
0, factorY, 0);
return AffineTransform (factorX, 0, 0, 0, factorY, 0);
}

AffineTransform AffineTransform::scale (const float factor) noexcept
{
return AffineTransform (factor, 0, 0, 0, factor, 0);
}

AffineTransform AffineTransform::scaled (const float factorX, const float factorY,
Expand Down
3 changes: 3 additions & 0 deletions modules/juce_graphics/geometry/juce_AffineTransform.h
Expand Up @@ -173,6 +173,9 @@ class JUCE_API AffineTransform
static AffineTransform scale (float factorX,
float factorY) noexcept;

/** Returns a new transform which is a re-scale about the origin. */
static AffineTransform scale (float factor) noexcept;

/** Returns a new transform which is a re-scale centred around the point provided. */
static AffineTransform scale (float factorX, float factorY,
float pivotX, float pivotY) noexcept;
Expand Down
19 changes: 19 additions & 0 deletions modules/juce_graphics/geometry/juce_Rectangle.h
Expand Up @@ -292,6 +292,25 @@ class Rectangle
return *this;
}

/** Scales this rectangle by the given amount, centred around the origin. */
template <typename FloatType>
Rectangle operator* (FloatType scaleFactor) const noexcept
{
Rectangle r (*this);
r *= scaleFactor;
return r;
}

/** Scales this rectangle by the given amount, centred around the origin. */
template <typename FloatType>
Rectangle operator*= (FloatType scaleFactor) noexcept
{
pos *= scaleFactor;
w *= scaleFactor;
h *= scaleFactor;
return *this;
}

/** Expands the rectangle by a given amount.
Effectively, its new size is (x - deltaX, y - deltaY, w + deltaX * 2, h + deltaY * 2).
Expand Down
2 changes: 1 addition & 1 deletion modules/juce_graphics/native/juce_android_Fonts.cpp
Expand Up @@ -186,7 +186,7 @@ class AndroidTypeface : public Typeface
{
JNIEnv* env = getEnv();

jobject matrix = GraphicsHelpers::createMatrix (env, AffineTransform::scale (unitsToHeightScaleFactor, unitsToHeightScaleFactor).followedBy (t));
jobject matrix = GraphicsHelpers::createMatrix (env, AffineTransform::scale (unitsToHeightScaleFactor).followedBy (t));
jintArray maskData = (jintArray) android.activity.callObjectMethod (JuceAppActivity.renderGlyph, (jchar) glyphNumber, paint.get(), matrix, rect.get());

env->DeleteLocalRef (matrix);
Expand Down
Expand Up @@ -178,7 +178,7 @@ class WindowsDirectWriteTypeface : public Typeface
const float pathAscent = (1024.0f * dwFontMetrics.ascent) / designUnitsPerEm;
const float pathDescent = (1024.0f * dwFontMetrics.descent) / designUnitsPerEm;
const float pathScale = 1.0f / (std::abs (pathAscent) + std::abs (pathDescent));
pathTransform = AffineTransform::scale (pathScale, pathScale);
pathTransform = AffineTransform::scale (pathScale);
}

float getAscent() const { return ascent; }
Expand Down
4 changes: 2 additions & 2 deletions modules/juce_gui_basics/components/juce_Component.cpp
Expand Up @@ -1911,12 +1911,12 @@ void Component::paintEntireComponent (Graphics& g, const bool ignoreAlphaLevel)
(int) (scale * getWidth()), (int) (scale * getHeight()), ! flags.opaqueFlag);
{
Graphics g2 (effectImage);
g2.addTransform (AffineTransform::scale (scale, scale));
g2.addTransform (AffineTransform::scale (scale));
paintComponentAndChildren (g2);
}

g.saveState();
g.addTransform (AffineTransform::scale (1.0f / scale, 1.0f / scale));
g.addTransform (AffineTransform::scale (1.0f / scale));
effect->applyEffect (effectImage, g, scale, ignoreAlphaLevel ? 1.0f : getAlpha());
g.restoreState();
}
Expand Down
2 changes: 1 addition & 1 deletion modules/juce_gui_basics/drawables/juce_SVGParser.cpp
Expand Up @@ -1097,7 +1097,7 @@ class SVGState
else if (t.startsWithIgnoreCase ("scale"))
{
if (tokens.size() == 1)
trans = AffineTransform::scale (numbers[0], numbers[0]);
trans = AffineTransform::scale (numbers[0]);
else
trans = AffineTransform::scale (numbers[0], numbers[1]);
}
Expand Down
6 changes: 6 additions & 0 deletions modules/juce_opengl/native/juce_OpenGL_osx.h
Expand Up @@ -43,6 +43,12 @@ struct ThreadSafeNSOpenGLViewClass : public ObjCClass <NSOpenGLView>
static void init (id self)
{
object_setInstanceVariable (self, "lock", new CriticalSection());

#if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
if ([self respondsToSelector: @selector (setWantsBestResolutionOpenGLSurface:)])
[self setWantsBestResolutionOpenGLSurface: YES];
#endif

setNeedsUpdate (self, YES);
}

Expand Down

0 comments on commit f6ce67e

Please sign in to comment.