summaryrefslogtreecommitdiff
path: root/macosx/plugins/DFXVideo/macsrc/PluginGLView.m
blob: a3dda640aba7a43439cabe308c71bb6f353877dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/***************************************************************************
    PluginGLView.m
    PeopsSoftGPU
  
    Created by Gil Pedersen on Sun April 18 2004.
    Copyright (c) 2004 Gil Pedersen.
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version. See also the license.txt file for *
 *   additional informations.                                              *
 *                                                                         *
 ***************************************************************************/

#import "PluginGLView.h"
#import "SGPUPreferences.h"
#include "externals.h"
#undef BOOL
#include "gpu.h"
#include "swap.h"

#include <time.h>
extern time_t tStart;

@implementation PluginGLView
@synthesize glLock;

//- (id)initWithFrame:(NSRect)frameRect
- (id) initWithCoder: (NSCoder *) coder
{
	if ((self = [super initWithCoder:coder]) == nil)
		return nil;
	
	glLock = [[NSLock alloc] init];
	if (nil == glLock) {
		return nil;
	}
	
	if ([self setupOpenGL3]) {
		oglProfile = NSOpenGLProfileVersion3_2Core;
	} else if ([self setupOpenGL2]) {
		oglProfile = NSOpenGLProfileVersionLegacy;
	} else
		return nil;
	
	return self;
}

- (void)dealloc
{
	if (oglProfile == NSOpenGLProfileVersionLegacy) {
		[self cleanupGL2];
	} else if (oglProfile == NSOpenGLProfileVersion3_2Core) {
		[self cleanupGL3];
	}
	
	if (image_base) {
		free(image_base);
	}
}

- (BOOL)isOpaque
{
	return YES;
}

- (BOOL)acceptsFirstResponder
{
	return NO;
}

- (void)drawRect:(NSRect)aRect
{
	// Check if an update has occured to the buffer
	if ([self lockFocusIfCanDraw]) {
		
		// Make this context current
		if (drawBG) {
			[[NSColor blackColor] setFill];
			[NSBezierPath fillRect:[self visibleRect]];
		}
		
		//glFinish() ;
		// Swap buffer to screen
		//[[self openGLContext] flushBuffer];
		
		[self unlockFocus];
	}
}

#if 0
- (void)update  // moved or resized
{
	NSRect rect;

	[super update];

	[[self openGLContext] makeCurrentContext];
	[[self openGLContext] update];

	rect = [self bounds];
	
	glViewport(0, 0, (int) rect.size.width, (int) rect.size.height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity(); 

	//[self setNeedsDisplay:true];
}
#endif

- (void)reshape	// scrolled, moved or resized
{
	[glLock lock];
	
	[super reshape];
	
	if (oglProfile == NSOpenGLProfileVersionLegacy) {
		[self reshapeGL2];
		[self renderScreenGL2];
	} else if (oglProfile == NSOpenGLProfileVersion3_2Core) {
		[self reshapeGL3];
		[self renderScreenGL3];
	}
	
	//[self setNeedsDisplay:true];
	
	[glLock unlock];
}

- (void)renderScreen
{
	if (oglProfile == NSOpenGLProfileVersionLegacy) {
		[self renderScreenGL2];
	} else if (oglProfile == NSOpenGLProfileVersion3_2Core) {
		[self renderScreenGL3];
	}
}

- (void)loadTextures:(GLboolean)first
{
	if (oglProfile == NSOpenGLProfileVersionLegacy) {
		[self loadTexturesGL2:first];
	} else if (oglProfile == NSOpenGLProfileVersion3_2Core) {
		[self loadTexturesGL3:first];
	}
}

- (void)swapBuffer
{
	if (oglProfile == NSOpenGLProfileVersionLegacy) {
		RunOnMainThreadSync(^{
			[self swapBufferGL2];
		});
	} else if (oglProfile == NSOpenGLProfileVersion3_2Core) {
		RunOnMainThreadSync(^{
			[self swapBufferGL3];
		});
	}
}

- (void)clearBuffer:(BOOL)display
{
	if (display == NO) {
		//[[self openGLContext] makeCurrentContext];
		//glClear(GL_COLOR_BUFFER_BIT);
		//[self loadTextures:NO];
	} else {
		noDisplay = YES;
		//[self setNeedsDisplay:true];
	}
}

+ (char*)loadSource:(NSURL *)filename
{
	//Since we're passing Cocoa NSURLs, let's use Cocoa's methods
	if (filename == nil) {
		return NULL;
	}
	
	NSUInteger len;
	NSMutableData *shaderData = [[NSMutableData alloc] initWithContentsOfURL:filename];
	[shaderData appendBytes:"\0" length:1];
	len = [shaderData length];
	char *shaderText = malloc(len);
	[shaderData getBytes:shaderText length:len];
	return shaderText;
}

@end