2015-07-17 00:40:46 +00:00
|
|
|
//
|
|
|
|
// Atari2600.m
|
|
|
|
// ElectrEm
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/07/2015.
|
|
|
|
// Copyright © 2015 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "Atari2600.h"
|
|
|
|
#import "Atari2600.hpp"
|
2015-07-25 00:37:08 +00:00
|
|
|
#import <OpenGL/gl3.h>
|
|
|
|
|
|
|
|
const char *vertexShader =
|
|
|
|
"#version 150\n"
|
|
|
|
"\n"
|
|
|
|
"in vec2 position;\n"
|
|
|
|
"\n"
|
|
|
|
"out vec4 colour;\n"
|
|
|
|
"\n"
|
|
|
|
"void main (void)\n"
|
|
|
|
"{\n"
|
2015-07-25 03:29:45 +00:00
|
|
|
"colour = vec4(1.0, 1.0, 1.0, 1.0);\n"
|
|
|
|
"gl_Position = vec4(position.x * 2.0 - 1.0, 1.0 - position.y * 2.0, 0.0, 1.0);\n"
|
2015-07-25 00:37:08 +00:00
|
|
|
"}\n";
|
|
|
|
|
|
|
|
const char *fragmentShader =
|
|
|
|
"#version 150\n"
|
|
|
|
"\n"
|
|
|
|
"in vec4 colour;\n"
|
|
|
|
"out vec4 fragColour;\n"
|
|
|
|
"\n"
|
|
|
|
"void main(void)\n"
|
|
|
|
"{\n"
|
|
|
|
"fragColour = colour;\n"
|
|
|
|
"}\n";
|
2015-07-17 00:40:46 +00:00
|
|
|
|
2015-07-24 00:07:35 +00:00
|
|
|
@interface CSAtari2600 (Callbacks)
|
2015-07-25 03:29:45 +00:00
|
|
|
- (void)crtDidEndFrame:(CRTFrame *)frame;
|
2015-07-24 00:07:35 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
struct Atari2600CRTDelegate: public Outputs::CRT::CRTDelegate {
|
|
|
|
CSAtari2600 *atari;
|
2015-07-25 03:29:45 +00:00
|
|
|
void crt_did_end_frame(Outputs::CRT *crt, CRTFrame *frame) { [atari crtDidEndFrame:frame]; }
|
2015-07-24 00:07:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
@implementation CSAtari2600 {
|
|
|
|
Atari2600::Machine _atari2600;
|
|
|
|
Atari2600CRTDelegate _crtDelegate;
|
|
|
|
|
|
|
|
dispatch_queue_t _serialDispatchQueue;
|
2015-07-25 03:29:45 +00:00
|
|
|
CRTFrame *_queuedFrame;
|
2015-07-25 00:37:08 +00:00
|
|
|
|
|
|
|
GLuint _vertexShader, _fragmentShader;
|
|
|
|
GLuint _shaderProgram;
|
|
|
|
|
|
|
|
GLuint _arrayBuffer, _vertexArray;
|
2015-07-24 00:07:35 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 03:29:45 +00:00
|
|
|
- (void)crtDidEndFrame:(CRTFrame *)frame {
|
2015-07-23 00:45:10 +00:00
|
|
|
|
2015-07-24 00:16:48 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-07-24 00:45:07 +00:00
|
|
|
if(_queuedFrame) {
|
|
|
|
dispatch_async(_serialDispatchQueue, ^{
|
|
|
|
_atari2600.get_crt()->return_frame();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_queuedFrame = frame;
|
|
|
|
|
|
|
|
[self.delegate atari2600NeedsRedraw:self];
|
|
|
|
});
|
|
|
|
}
|
2015-07-22 22:15:18 +00:00
|
|
|
|
2015-07-25 00:37:08 +00:00
|
|
|
- (GLuint)compileShader:(const char *)source type:(GLenum)type
|
|
|
|
{
|
|
|
|
GLuint shader = glCreateShader(type);
|
|
|
|
glShaderSource(shader, 1, &source, NULL);
|
|
|
|
glCompileShader(shader);
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:45:07 +00:00
|
|
|
- (void)draw {
|
|
|
|
|
2015-07-25 00:37:08 +00:00
|
|
|
if(!_shaderProgram)
|
|
|
|
{
|
|
|
|
_shaderProgram = glCreateProgram();
|
|
|
|
_vertexShader = [self compileShader:vertexShader type:GL_VERTEX_SHADER];
|
|
|
|
_fragmentShader = [self compileShader:fragmentShader type:GL_FRAGMENT_SHADER];
|
|
|
|
|
|
|
|
glAttachShader(_shaderProgram, _vertexShader);
|
|
|
|
glAttachShader(_shaderProgram, _fragmentShader);
|
|
|
|
glLinkProgram(_shaderProgram);
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &_vertexArray);
|
|
|
|
glBindVertexArray(_vertexArray);
|
|
|
|
glGenBuffers(1, &_arrayBuffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _arrayBuffer);
|
|
|
|
|
2015-07-25 03:29:45 +00:00
|
|
|
GLushort vertices[] = {
|
|
|
|
0, 0,
|
|
|
|
32767, 32767,
|
2015-07-25 00:37:08 +00:00
|
|
|
};
|
|
|
|
|
2015-07-25 03:29:45 +00:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
|
2015-07-25 00:37:08 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 00:45:07 +00:00
|
|
|
if(_queuedFrame)
|
|
|
|
{
|
|
|
|
glClearColor(1.0, 0.0, 0.0, 1.0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2015-07-25 00:37:08 +00:00
|
|
|
|
|
|
|
glUseProgram(_shaderProgram);
|
|
|
|
|
|
|
|
GLint position = glGetAttribLocation(_shaderProgram, "position");
|
|
|
|
|
2015-07-25 03:29:45 +00:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, _queuedFrame->number_of_runs * sizeof(GLushort) * 8, _queuedFrame->runs, GL_DYNAMIC_DRAW);
|
|
|
|
|
2015-07-25 00:37:08 +00:00
|
|
|
glEnableVertexAttribArray(position);
|
2015-07-25 03:29:45 +00:00
|
|
|
glVertexAttribPointer(position, 2, GL_UNSIGNED_SHORT, GL_TRUE, 4 * sizeof(GLushort), (void *)0);
|
2015-07-22 22:15:18 +00:00
|
|
|
|
2015-07-25 03:29:45 +00:00
|
|
|
glDrawArrays(GL_LINES, 0, _queuedFrame->number_of_runs*2);
|
2015-07-24 00:45:07 +00:00
|
|
|
}
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)runForNumberOfCycles:(int)cycles {
|
2015-07-24 00:07:35 +00:00
|
|
|
dispatch_async(_serialDispatchQueue, ^{
|
|
|
|
_atari2600.run_for_cycles(cycles);
|
|
|
|
});
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setROM:(NSData *)rom {
|
2015-07-24 00:07:35 +00:00
|
|
|
dispatch_async(_serialDispatchQueue, ^{
|
|
|
|
_atari2600.set_rom(rom.length, (const uint8_t *)rom.bytes);
|
|
|
|
});
|
2015-07-17 00:40:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 22:15:18 +00:00
|
|
|
- (instancetype)init {
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self) {
|
2015-07-24 00:07:35 +00:00
|
|
|
_crtDelegate.atari = self;
|
2015-07-22 22:15:18 +00:00
|
|
|
_atari2600.get_crt()->set_delegate(&_crtDelegate);
|
2015-07-24 00:16:48 +00:00
|
|
|
_serialDispatchQueue = dispatch_queue_create("Atari 2600 queue", DISPATCH_QUEUE_SERIAL);
|
2015-07-22 22:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-07-17 00:40:46 +00:00
|
|
|
@end
|