Skip to content

Commit

Permalink
Merge pull request #259 from baconpaul/retina-bitmaps-226
Browse files Browse the repository at this point in the history
Retina Bitmaps
  • Loading branch information
baconpaul authored Jan 12, 2019
2 parents ffe56be + 5c77a49 commit 26adc86
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 4 deletions.
4 changes: 3 additions & 1 deletion scripts/macOS/package-au.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ cp -R "$BUNDLE_RES_SRC_LOCATION" "$BUNDLE_DIR/Contents/Resources"
if [[ -z "$SURGE_USE_VECTOR_SKIN" ]]; then
cp $BITMAP_SRC_LOCATION/* "$BUNDLE_DIR/Contents/Resources/"
else
rm "$BUNDLE_DIR/Contents/Resources/bmp?????.png"
rm "$BUNDLE_DIR/Contents/Resources/bmp*.png"
cp $VECTOR_BITMAP_SRC_LOCATION/bmp?????.png "$BUNDLE_DIR/Contents/Resources/"
mkdir "$BUNDLE_DIR/Contents/Resources/scalable"
cp $VECTOR_BITMAP_SRC_LOCATION/*png "$BUNDLE_DIR/Contents/Resources/scalable"
fi

5 changes: 4 additions & 1 deletion scripts/macOS/package-vst.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ cp -R "$BUNDLE_RES_SRC_LOCATION" "$BUNDLE_DIR/Contents/Resources"
if [[ -z "$SURGE_USE_VECTOR_SKIN" ]]; then
cp $BITMAP_SRC_LOCATION/* "$BUNDLE_DIR/Contents/Resources/"
else
rm "$BUNDLE_DIR/Contents/Resources/bmp?????.png"
rm "$BUNDLE_DIR/Contents/Resources/bmp*.png"
cp $VECTOR_BITMAP_SRC_LOCATION/bmp?????.png "$BUNDLE_DIR/Contents/Resources/"
mkdir "$BUNDLE_DIR/Contents/Resources/scalable"
cp $VECTOR_BITMAP_SRC_LOCATION/*png "$BUNDLE_DIR/Contents/Resources/scalable"
fi


4 changes: 3 additions & 1 deletion scripts/macOS/package-vst3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ cp -R "$BUNDLE_RES_SRC_LOCATION" "$BUNDLE_DIR/Contents/Resources"
if [[ -z "$SURGE_USE_VECTOR_SKIN" ]]; then
cp $BITMAP_SRC_LOCATION/* "$BUNDLE_DIR/Contents/Resources/"
else
rm "$BUNDLE_DIR/Contents/Resources/bmp?????.png"
rm "$BUNDLE_DIR/Contents/Resources/bmp*.png"
cp $VECTOR_BITMAP_SRC_LOCATION/bmp?????.png "$BUNDLE_DIR/Contents/Resources/"
mkdir "$BUNDLE_DIR/Contents/Resources/scalable"
cp $VECTOR_BITMAP_SRC_LOCATION/*png "$BUNDLE_DIR/Contents/Resources/scalable"
fi

70 changes: 70 additions & 0 deletions src/common/gui/CScalableBitmap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "CScalableBitmap.h"
#if MAC
#include <CoreFoundation/CoreFoundation.h>
#endif

CScalableBitmap::CScalableBitmap(CResourceDescription desc) : CBitmap(desc)
{
int id = 0;
if(desc.type == CResourceDescription::kIntegerType)
id = (int32_t)desc.u.id;

/*
** At this stage this scales array may seem like overkill since it only has two
** values. But to impelemnt scalability we will end up with multiple sizes, including
** 1.5x maybe 1.25x and so on. So build the architecture now to support that even
** though we only populate with two types. And since our sizes will include a 1.25x
** hash on integer percentages (so 100 -> 1x), not on floats.
*/

scales = {{ 100, 200 }};
scaleFilePostfixes[ 100 ] = "";
scaleFilePostfixes[ 200 ] = "@2x";

for(auto sc : scales)
{
auto postfix = scaleFilePostfixes[sc];

char filename [PATH_MAX];
sprintf (filename, "scalable/bmp%05d%s.png", id, postfix.c_str());

CBitmap *tmp = new CBitmap(CResourceDescription( filename ));

if(tmp->getWidth() > 0)
{
scaledBitmaps[sc] = tmp;
}
else
{
scaledBitmaps[sc] = NULL;
}

}
}

void CScalableBitmap::draw (CDrawContext* context, const CRect& rect, const CPoint& offset, float alpha )
{
/*
** For now this is a fixed 'retina' style draw of using 2x bitmaps at 0.5 scale if they exist
*/
if (scaledBitmaps[ 200 ] != NULL)
{
CGraphicsTransform tf = CGraphicsTransform().scale( 0.5, 0.5 );
CGraphicsTransform invtf = tf.inverse();

CDrawContext::Transform tr(*context, tf);

// Have to de-const these to do the transform alas
CRect ncrect = rect;
CRect nr = invtf.transform(ncrect);

CPoint ncoff = offset;
CPoint no = invtf.transform(ncoff);

scaledBitmaps[ 200 ]->draw(context, nr, no, alpha);
}
else
{
CBitmap::draw(context, rect, offset, alpha);
}
}
21 changes: 21 additions & 0 deletions src/common/gui/CScalableBitmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
** CScalableBitmap is an implmentation of VSGTUI::CBitmap which can
** load bitmaps at multiple resolutions and draw them scaled accordingly
*/

#include <vstgui/vstgui.h>

#include <vector>
#include <map>

class CScalableBitmap : public VSTGUI::CBitmap
{
public:
CScalableBitmap( CResourceDescription d );

std::vector< int > scales; // 100, 150, 200, 300 etc... - int percentages
std::map< int, VSTGUI::CBitmap * > scaledBitmaps;
std::map< int, std::string > scaleFilePostfixes;

virtual void draw (CDrawContext* context, const CRect& rect, const CPoint& offset, float alpha);
};
14 changes: 13 additions & 1 deletion src/common/gui/SurgeBitmaps.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include "SurgeBitmaps.h"
#include <map>

#if MAC
#define USE_SCALABLE_BITMAPS 1
#endif

#ifdef USE_SCALABLE_BITMAPS
#include "CScalableBitmap.h"
#endif

std::map<int, VSTGUI::CBitmap*> bitmap_registry;

static std::atomic_int refCount(0);
Expand All @@ -13,7 +21,7 @@ SurgeBitmaps::SurgeBitmaps()
addEntry(IDB_BUTTON_ABOUT);
addEntry(IDB_ABOUT);
addEntry(IDB_FILTERBUTTONS);
addEntry(IDB_OSCSWITCH);
addEntry(IDB_OSCSWITCH);
addEntry(IDB_FILTERSUBTYPE);
addEntry(IDB_RELATIVE_TOGGLE);
addEntry(IDB_OSCSELECT);
Expand Down Expand Up @@ -72,7 +80,11 @@ void SurgeBitmaps::addEntry(int id)
{
assert(bitmap_registry.find(id) == bitmap_registry.end());

#ifdef USE_SCALABLE_BITMAPS
VSTGUI::CBitmap *bitmap = new CScalableBitmap(CResourceDescription(id));
#else
VSTGUI::CBitmap* bitmap = new VSTGUI::CBitmap(CResourceDescription(id));
#endif
bitmap_registry[id] = bitmap;
}

Expand Down

0 comments on commit 26adc86

Please sign in to comment.