SameBoy | Accurate GB/GBC emulator |
| download: https://git.y1.nz/archives/sameboy.tar.gz | |
| README | Files | Log | Refs | LICENSE |
Cocoa/GBVisualizerView.m
1 #import "GBVisualizerView.h"
2 #import "GBPaletteEditorController.h"
3 #import <Core/gb.h>
4
5 #define SAMPLE_COUNT 1024
6
7 static NSColor *color_to_effect_color(typeof(GB_PALETTE_DMG.colors[0]) color)
8 {
9 if (@available(macOS 10.10, *)) {
10 double tint = MAX(color.r, MAX(color.g, color.b)) + 64;
11
12 return [NSColor colorWithRed:color.r / tint
13 green:color.g / tint
14 blue:color.b / tint
15 alpha:tint/(255 + 64)];
16
17 }
18 return [NSColor colorWithRed:color.r / 255.0
19 green:color.g / 255.0
20 blue:color.b / 255.0
21 alpha:1.0];
22 }
23
24 @implementation GBVisualizerView
25 {
26 GB_sample_t _samples[SAMPLE_COUNT];
27 size_t _position;
28 }
29
30 - (void)drawRect:(NSRect)dirtyRect
31 {
32 const GB_palette_t *palette = [GBPaletteEditorController userPalette];
33 NSSize size = self.bounds.size;
34
35 [color_to_effect_color(palette->colors[0]) setFill];
36 NSRectFill(self.bounds);
37
38 NSBezierPath *line = [NSBezierPath bezierPath];
39 [line moveToPoint:NSMakePoint(0, size.height / 2)];
40
41 for (unsigned i = 0; i < SAMPLE_COUNT; i++) {
42 GB_sample_t *sample = _samples + ((i + _position) % SAMPLE_COUNT);
43 double volume = ((signed)sample->left + (signed)sample->right) / 32768.0;
44 [line lineToPoint:NSMakePoint(size.width * (i + 0.5) / SAMPLE_COUNT,
45 (volume + 1) * size.height / 2)];
46 }
47
48 [line lineToPoint:NSMakePoint(size.width, size.height / 2)];
49 [line setLineWidth:1.0];
50
51 [color_to_effect_color(palette->colors[2]) setFill];
52 [line fill];
53
54 [color_to_effect_color(palette->colors[1]) setFill];
55 NSRectFill(NSMakeRect(0, size.height / 2 - 0.5, size.width, 1));
56
57 [color_to_effect_color(palette->colors[3]) setStroke];
58 [line stroke];
59
60 [super drawRect:dirtyRect];
61 }
62
63 - (void)addSample:(GB_sample_t *)sample
64 {
65 _samples[_position++] = *sample;
66 if (_position == SAMPLE_COUNT) {
67 _position = 0;
68 }
69 }
70
71 @end
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.