1/*
2    Copyright (c) 2005-2021 Intel Corporation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15*/
16
17#import <Foundation/Foundation.h>
18#import "OpenGLView.h"
19
20// defined in macvideo.cpp
21extern char* window_title;
22extern int cocoa_update;
23extern int g_sizex, g_sizey;
24extern unsigned int *g_pImg;
25void on_mouse_func(int x, int y, int k);
26void on_key_func(int x);
27
28bool initialized = false;
29
30#if TARGET_OS_IPHONE
31
32#import "OpenGLES/ES2/gl.h"
33
34@implementation OpenGLView
35
36@synthesize timer;
37@synthesize imageRect;
38
39- (void)drawRect:(CGRect)start
40{
41    if (initialized == false) {
42        NSLog(@"INITIALIZE");
43        timer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(update_window) userInfo:nil repeats:YES];
44        imageRect = [[UIScreen mainScreen] bounds];
45        CGFloat full_height = imageRect.size.height;
46        const float ratio=(float)g_sizex/g_sizey;
47        imageRect.size.height=imageRect.size.width/ratio;
48        imageRect.origin.y=(full_height-imageRect.size.height)/2;
49        initialized = true;
50    }
51
52    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
53    CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, g_pImg, 4*g_sizex*g_sizey, NULL);
54
55    CGImageRef inputImage = CGImageCreate(g_sizex, g_sizey, 8, 32, g_sizex * 4, colourSpace,(CGBitmapInfo)kCGImageAlphaNoneSkipLast, dataProvider, NULL, NO, kCGRenderingIntentDefault);
56    UIImage *image = [UIImage imageWithCGImage:inputImage];
57
58    CGDataProviderRelease(dataProvider);
59    CGColorSpaceRelease(colourSpace);
60    CGImageRelease(inputImage);
61
62    [image drawInRect:imageRect];
63
64}
65
66- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
67{
68    CGPoint point = [[touches anyObject] locationInView:self];
69    const int x = point.x;
70    const int y = point.y;
71    if ( (y-imageRect.origin.y) > 0 && y < (imageRect.origin.y + imageRect.size.height ))
72    on_mouse_func( x*g_sizex/(imageRect.size.width), (y-imageRect.origin.y)*g_sizey/imageRect.size.height,1);
73    [self setNeedsDisplay];
74}
75
76-(void) update_window{
77    if( cocoa_update ) [self setNeedsDisplay];
78}
79
80@end
81
82#elif TARGET_OS_MAC
83
84#import <OpenGL/gl.h>
85
86@implementation OpenGLView
87
88@synthesize timer;
89
90- (void) drawRect:(NSRect)start
91{
92    if (initialized == false) {
93        NSLog(@"INITIALIZE");
94        timer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(update_window) userInfo:nil repeats:YES];
95        initialized = true;
96    }
97    glWindowPos2i(0, (int)self.visibleRect.size.height);
98    glPixelZoom( (float)self.visibleRect.size.width /(float)g_sizex,
99                -(float)self.visibleRect.size.height/(float)g_sizey);
100    glDrawPixels(g_sizex, g_sizey, GL_BGRA_EXT, GL_UNSIGNED_INT_8_8_8_8_REV, g_pImg);
101    glFlush();
102}
103
104-(void) update_window{
105    if( cocoa_update ) [self setNeedsDisplay:YES];
106    if( window_title ) [self.window setTitle:[NSString stringWithFormat:@"%s", window_title]];
107}
108
109-(void) keyDown:(NSEvent *)theEvent{
110    on_key_func([theEvent.characters characterAtIndex:0]);
111}
112
113-(void) mouseDown:(NSEvent *)theEvent{
114    // mouse event for seismic and fractal
115    NSPoint point= theEvent.locationInWindow;
116    const int x = (int)point.x;
117    const int y = (int)point.y;
118    NSRect rect = self.visibleRect;
119    on_mouse_func(x*g_sizex/(int)rect.size.width,((int)rect.size.height-y)*g_sizey/(int)rect.size.height,1);
120    [self setNeedsDisplay:YES];
121}
122
123- (BOOL) acceptsFirstResponder
124{
125    return YES;
126}
127
128- (void) rightMouseDown:(NSEvent *)theEvent
129{
130    return;
131}
132
133-(void) viewDidEndLiveResize
134{
135    NSRect rect = self.visibleRect;
136    const int x=(int)rect.size.width;
137    const int y=(int)rect.size.height;
138    [self.window setTitle:[NSString stringWithFormat:@"X=%d Y=%d", x,y]];
139}
140
141@end
142
143#endif
144