aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/evas/src/modules/engines/gl_cocoa/evas_gl_cocoa_main.m
blob: aa6b8950d481f263fbd2585fa9cbd13323350fbb (plain)
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

#include <Cocoa/Cocoa.h>

#include "evas_engine.h"

static Evas_GL_Cocoa_Window *_evas_gl_cocoa_window = NULL;

@interface EvasGLView : NSOpenGLView
{
}

+ (NSOpenGLPixelFormat*) basicPixelFormat;
- (id) initWithFrame: (NSRect) frameRect;

@end


@implementation EvasGLView

- (id) init
{
   self = [super init];
   return self;
}

+ (NSOpenGLPixelFormat*) basicPixelFormat
{
   NSOpenGLPixelFormatAttribute attributes [] = {
            NSOpenGLPFAWindow,
            NSOpenGLPFAAccelerated,
            NSOpenGLPFADoubleBuffer,
            /*NSOpenGLPFAColorSize, 24,
            NSOpenGLPFAAlphaSize, 8,
            NSOpenGLPFADepthSize, 24,*/
            0
   };
   return [[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes] autorelease];
}

// ---------------------------------

-(id) initWithFrame: (NSRect) frameRect
{
   NSOpenGLPixelFormat * pf = [EvasGLView basicPixelFormat];
   self = [super initWithFrame: frameRect pixelFormat: pf];
   return self;
}

@end


Evas_GL_Cocoa_Window *
eng_window_new(void *window,
	       int      w,
	       int      h)
{
   Evas_GL_Cocoa_Window *gw;
   int context_attrs[3];
   int config_attrs[20];
   int major_version, minor_version;
   int num_config;

   gw = calloc(1, sizeof(Evas_GL_Cocoa_Window));
   if (!gw) return NULL;
 
   _evas_gl_cocoa_window = gw;
   gw->window = window;
   gw->view = [[EvasGLView alloc] initWithFrame:NSMakeRect(0,0,w,h)];
   NSOpenGLContext *ctx = [(NSOpenGLView*)gw->view openGLContext];
   [ctx makeCurrentContext];
   gw->gl_context = evas_gl_common_context_new();

   if (!gw->gl_context)
     {
	free(gw);
	return NULL;
     }
   evas_gl_common_context_use(gw->gl_context);
   evas_gl_common_context_resize(gw->gl_context, w, h, 0);

   return gw;
}

void
eng_window_free(Evas_GL_Cocoa_Window *gw)
{
   if (gw == _evas_gl_cocoa_window) _evas_gl_cocoa_window = NULL;
   evas_gl_common_context_free(gw->gl_context);
   free(gw);
}

void
eng_window_use(Evas_GL_Cocoa_Window *gw)
{
   if (_evas_gl_cocoa_window != gw)
     {
	[[(NSOpenGLView*)gw->view openGLContext] makeCurrentContext];
        if (_evas_gl_cocoa_window)
          evas_gl_common_context_flush(_evas_gl_cocoa_window->gl_context);
	_evas_gl_cocoa_window = gw;

     }
   evas_gl_common_context_use(gw->gl_context);
}

void
eng_window_swap_buffers(Evas_GL_Cocoa_Window *gw)
{
   [[(NSOpenGLView*)gw->view openGLContext] flushBuffer];
}

void
eng_window_vsync_set(int on)
{

}


void
eng_window_resize(Evas_GL_Cocoa_Window *gw, int width, int height)
{
  NSRect view_frame;

  INF("Resize %d %d\n", width, height);

  view_frame = [(EvasGLView*)gw->view frame];
  printf("view_frame : %3.3f %3.3f\n", view_frame.size.height, view_frame.size.width);
  view_frame.size.height = height;
  view_frame.size.width = width;
  printf("view_frame : %3.3f %3.3f\n", view_frame.size.height, view_frame.size.width);
  [(EvasGLView*)gw->view setFrame:view_frame];
  [[(NSOpenGLView*)gw->view openGLContext] flushBuffer];
}