Skip to content

Commit

Permalink
macOS: Support retina display (#96)
Browse files Browse the repository at this point in the history
Apply angband/angband#416 changes to support retina on macOS.
  • Loading branch information
motemen authored Oct 11, 2022
1 parent 09a53ab commit 092e187
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main-cocoa.m
Original file line number Diff line number Diff line change
Expand Up @@ -1587,19 +1587,33 @@ - (void)updateImage

CGLayerRelease(self.angbandLayer);

/* Use the highest monitor scale factor on the system to work out what
* scale to draw at - not the recommended method, but works where we
* can't easily get the monitor the current draw is occurring on. */
float angbandLayerScale = 1.0;
if ([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)]) {
for (NSScreen *screen in [NSScreen screens]) {
angbandLayerScale = fmax(angbandLayerScale, [screen backingScaleFactor]);
}
}

/* Make a bitmap context as an example for our layer */
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGContextRef exampleCtx = CGBitmapContextCreate(NULL, 1, 1, 8 /* bits per component */, 48 /* bytesPerRow */, cs, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host);
CGColorSpaceRelease(cs);

/* Create the layer at the appropriate size */
size.width = fmax(1, ceil(size.width));
size.height = fmax(1, ceil(size.height));
size.width = fmax(1, ceil(size.width * angbandLayerScale));
size.height = fmax(1, ceil(size.height * angbandLayerScale));
self.angbandLayer =
CGLayerCreateWithContext(exampleCtx, *(CGSize *)&size, NULL);

CFRelease(exampleCtx);

/* Set the new context of the layer to draw at the correct scale */
CGContextRef ctx = CGLayerGetContext(self.angbandLayer);
CGContextScaleCTM(ctx, angbandLayerScale, angbandLayerScale);

[self lockFocus];
[[NSColor blackColor] set];
NSRectFill((NSRect){NSZeroPoint, [self baseSize]});
Expand Down

0 comments on commit 092e187

Please sign in to comment.