quephird / happiNESs

An attempt at an NES emulator written in Swift
5 stars 0 forks source link

Improve image fidelity #51

Closed quephird closed 1 month ago

quephird commented 1 month ago

This PR accomplishes exactly what it says in the title, namely that the pixels in the screen are sharp now instead of blurred. Previously, this had to do with how Canvas was scaling up the image computed inside the closure passed in. Now we use a SwiftUI Image component instead, and call the .interpolation() modifier passing in .none to suppress blurring. It relies on a new computed property, image, which derives a CGImage from the screen buffer.

It should be noted that the method of creating a new CGImage varies slightly from the one used in Canvas before, in that we create a CFData and create a CGDataProvider instance using it. Previously, it was ok to use unsafe code and rely on bufferPointer inside the closure of Canvas because we both created the image and immediately drew it to the graphics context. However, now we the creation of the image and the rendering of it are in two places, and so if we still used that unsafe code in the computed property, that pointer might have become invalid by the time we actually rendered it in the SwiftUI component. Instead, the computed property creates a CFData instance which will have a brand new copy of the image data instead of indirectly relying on such a pointer.