PageRenderTime 13ms CodeModel.GetById 10ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/OJGL/GLShadersLoader.j

http://github.com/tartiflop/ojgl
Unknown | 79 lines | 60 code | 19 blank | 0 comment | 0 complexity | 28ae9565d802316f3a424bb28cde7376 MD5 | raw file
 1@import <Foundation/CPObject.j>
 2
 3@implementation GLShadersLoader : CPObject {
 4	id _target;
 5	SEL _onComplete;
 6	
 7	CPString _vertexShader;
 8	CPString _fragmentShader;
 9	
10	CPURLConnection _vertexShaderConnection;
11	CPURLConnection _fragmentShaderConnection;
12	
13}
14
15- (id)initWithShader:(CPString)vertexShaderURL fragmentShaderURL:(CPString)fragmentShaderURL target:(id)target onComplete:(SEL)onComplete {
16	self = [super init];
17	
18	if (self) {
19		_target = target;
20		_onComplete = onComplete;
21		
22		// Get vertex shader
23		var vertexShaderRequest = [CPURLRequest requestWithURL:vertexShaderURL];
24		_vertexShaderConnection = [CPURLConnection connectionWithRequest:vertexShaderRequest delegate:self];	
25		
26		// Get fragment shader
27		var fragmentShaderRequest = [CPURLRequest requestWithURL:fragmentShaderURL];
28		_fragmentShaderConnection = [CPURLConnection connectionWithRequest:fragmentShaderRequest delegate:self];	
29	}
30	
31	return self;
32}
33
34- (CPString)vertexShader {
35	return _vertexShader;
36}
37
38- (CPString)fragmentShader {
39	return _fragmentShader;
40}
41
42- (void)connection:(CPURLConnection)connection didReceiveData:(CPString)data {
43	if (connection == _vertexShaderConnection) {
44		// Verify that the data is not empty: could be that the file has not been found
45		if (data == "") {
46			CPLog.error("Vertex shader is empty");
47			return;
48		} 
49		
50		CPLog.info("got vertex shader");
51		_vertexShader = data;
52
53	} else if (connection == _fragmentShaderConnection) {
54		if (data == "") {
55			// Verify that the data is not empty: could be that the file has not been found
56			CPLog.error("Fragment shader is empty");
57			return;
58		} 
59		
60		CPLog.info("got fragment shader");
61		_fragmentShader = data;
62	}
63	
64	// If both vertex and fragment shaders have been successfully loaded then call selector
65	if (_vertexShader && _fragmentShader) {
66		objj_msgSend(_target, _onComplete);
67	}
68}
69
70- (void)connection:(CPURLConnection)connection didFailWithError:(CPString)error {
71	if (connection == _vertexShaderConnection) {
72		CPLog.error("Failed to get vertex shader");
73
74	} else if (connection == _fragmentShaderConnection) {
75		CPLog.error("Failed to get fragment shader");
76	} 
77}
78
79@end