Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5016693
Showing
9 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
quadsim | ||
*.dSYM | ||
*.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#import <Cocoa/Cocoa.h> | ||
#include "sim.h" | ||
|
||
@interface AppDelegate : NSObject <NSApplicationDelegate> { | ||
NSWindow *window; | ||
sim_ctx *simContext; | ||
} | ||
@end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#import "AppDelegate.h" | ||
#import "OpenGLView.h" | ||
|
||
@implementation AppDelegate | ||
|
||
void draw(double t, void *ctx) { | ||
AppDelegate *del = (AppDelegate *)ctx; | ||
|
||
sim_draw(del->simContext, t); | ||
} | ||
|
||
- (id)init { | ||
if (self = [super init]) { | ||
NSRect bounds = NSMakeRect(0,0,400,400); | ||
window = [[NSWindow alloc] initWithContentRect:bounds styleMask:NSTitledWindowMask | NSClosableWindowMask backing:NSBackingStoreBuffered defer:YES]; | ||
|
||
simContext = sim_create(); | ||
|
||
[window setContentView:[[[OpenGLView alloc] initWithFrame:bounds draw:draw context:self] autorelease]]; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)dealloc { | ||
sim_destroy(simContext); | ||
[window release]; | ||
[super dealloc]; | ||
} | ||
|
||
- (void)applicationWillFinishLaunching:(NSNotification *)notification { | ||
[window makeKeyAndOrderFront:self]; | ||
} | ||
|
||
@end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
build: | ||
gcc -O0 -g -framework AppKit -framework OpenGL -framework QuartzCore main.m AppDelegate.m OpenGLView.m sim.c -o quadsim | ||
|
||
clean: | ||
rm -f ./*.o quadsim |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#import <Cocoa/Cocoa.h> | ||
#import <OpenGL/gl.h> | ||
#import <QuartzCore/QuartzCore.h> | ||
|
||
@interface OpenGLView : NSOpenGLView { | ||
CVDisplayLinkRef displayLink; | ||
double _timeFreq, _prevTime; | ||
void (*drawFunction)(double, void*); | ||
void *ctx; | ||
} | ||
|
||
- (id)initWithFrame:(NSRect)rect draw:(void(*)(double, void*))d context:(void*)ctx; | ||
|
||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#import "OpenGLView.h" | ||
|
||
@implementation OpenGLView | ||
|
||
- (id)initWithFrame:(NSRect)frame draw:(void(*)(double, void*))d context:(void *)leCtx { | ||
if (self = [super initWithFrame:frame pixelFormat:[NSOpenGLView defaultPixelFormat]]) { | ||
_timeFreq = CVGetHostClockFrequency(); | ||
drawFunction = d; | ||
ctx = leCtx; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)dealloc { | ||
CVDisplayLinkRelease(displayLink); | ||
[super dealloc]; | ||
} | ||
|
||
- (BOOL)getFrameForTime:(const CVTimeStamp *)time { | ||
BOOL result = NO; | ||
|
||
@synchronized (self) { | ||
|
||
NSOpenGLContext *currentContext = [self openGLContext]; | ||
[currentContext makeCurrentContext]; | ||
|
||
CGLContextObj context = [currentContext CGLContextObj]; | ||
|
||
// CGLLockContext(context); | ||
|
||
double hostTime = (double) time->hostTime; | ||
double now = hostTime / _timeFreq; | ||
|
||
// this will not update unless 1/30th of a second has passed since the last update | ||
if ( now < _prevTime + (1.0 / 30.0) ) | ||
{ | ||
// returning NO will cause the layer to NOT be redrawn | ||
result = NO; | ||
} | ||
else | ||
{ | ||
// change whatever you want to change here, as a function of time elapsed | ||
|
||
drawFunction(now - _prevTime, ctx); | ||
|
||
_prevTime = now; | ||
// return YES to have your layer redrawn | ||
result = YES; | ||
} | ||
|
||
[currentContext flushBuffer]; | ||
// CGLUnlockContext(context); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, | ||
CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext) | ||
{ | ||
CVReturn result = [(OpenGLView*)displayLinkContext getFrameForTime:outputTime]; | ||
return result; | ||
} | ||
|
||
- (void)prepareOpenGL { | ||
// Synchronize buffer swaps with vertical refresh rate | ||
GLint swapInt = 1; | ||
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; | ||
|
||
// Create a display link capable of being used with all active displays | ||
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink); | ||
|
||
// Set the renderer output callback function | ||
CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, self); | ||
|
||
|
||
// Set the display link for the current renderer | ||
CGLContextObj cglContext = [[self openGLContext] CGLContextObj]; | ||
CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj]; | ||
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat); | ||
|
||
// Activate the display link | ||
CVDisplayLinkStart(displayLink); | ||
} | ||
|
||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#import <Cocoa/cocoa.h> | ||
#import "AppDelegate.h" | ||
|
||
int main(int argc, char *argv[]) { | ||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | ||
NSApplication *app = [NSApplication sharedApplication]; | ||
|
||
AppDelegate *del = [[[AppDelegate alloc] init] autorelease]; | ||
[app setDelegate:del]; | ||
[app run]; | ||
[pool drain]; | ||
|
||
return 0; | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "sim.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <OpenGL/gl.h> | ||
|
||
|
||
sim_ctx *sim_create() { | ||
return (sim_ctx *)malloc(sizeof(sim_ctx)); | ||
} | ||
|
||
void sim_destroy(sim_ctx *c) { | ||
free(c); | ||
} | ||
|
||
void sim_update() { | ||
} | ||
|
||
void sim_draw(sim_ctx *ctx, double t) { | ||
ctx->t += t * .000001; | ||
|
||
glRotatef(ctx->t, 0,0,1); | ||
|
||
glClearColor(0,0,0,0); | ||
glClear(GL_COLOR_BUFFER_BIT); | ||
|
||
glColor3f(1.0f, 0.85f, 0.35f); | ||
glBegin(GL_TRIANGLES); | ||
{ | ||
glVertex3f( 0.0, 0.6, 0.0); | ||
glVertex3f( -0.2, -0.3, 0.0); | ||
glVertex3f( 0.2, -0.3 ,0.0); | ||
} | ||
glEnd(); | ||
|
||
glFlush(); | ||
} | ||
|
||
|
||
// const int TICKS_PER_SECOND = 25; | ||
// const int SKIP_TICKS = 1000 / TICKS_PER_SECOND; | ||
// const int MAX_FRAMESKIP = 5; | ||
|
||
// DWORD next_game_tick = GetTickCount(); | ||
// int loops; | ||
// float interpolation; | ||
|
||
// bool game_is_running = true; | ||
// while( game_is_running ) { | ||
|
||
// loops = 0; | ||
// while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) { | ||
// update_game(); | ||
|
||
// next_game_tick += SKIP_TICKS; | ||
// loops++; | ||
// } | ||
|
||
// interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick ) | ||
// / float( SKIP_TICKS ); | ||
// display_game( interpolation ); | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
|
||
struct sim_ctx_t | ||
{ | ||
double t; | ||
}; | ||
typedef struct sim_ctx_t sim_ctx; | ||
|
||
sim_ctx *sim_create(); | ||
void sim_destroy(sim_ctx *c); | ||
|
||
void sim_update(); | ||
void sim_draw(sim_ctx *ctx, double t); |