rename evas_glew_main.c to evas_glew__win32_main.c
[framework/uifw/evas.git] / src / modules / engines / gl_glew / evas_glew_win32_main.c
1 #include "evas_engine.h"
2
3 #ifdef HAVE_GL_GLEW_H
4 # include <GL/wglew.h>
5 #endif
6
7 static Evas_GL_Glew_Window *_evas_gl_glew_window = NULL;
8
9 Evas_GL_Glew_Window *
10 eng_window_new(HWND  window,
11                int   depth,
12                int   width,
13                int   height)
14 {
15    PIXELFORMATDESCRIPTOR pfd;
16    Evas_GL_Glew_Window  *gw;
17    int                   format;
18
19    gw = calloc(1, sizeof(Evas_GL_Glew_Window));
20    if (!gw) return NULL;
21
22    gw->window = window;
23    gw->depth = depth;
24
25    gw->dc = GetDC(window);
26    if (!gw->dc)
27     goto free_window;
28
29    ZeroMemory(&pfd, sizeof (pfd));
30    pfd.nSize = sizeof (pfd);
31    pfd.nVersion = 1;
32    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
33    pfd.iPixelType = PFD_TYPE_RGBA;
34    pfd.cColorBits = 24;
35    pfd.cDepthBits = 32;
36    pfd.iLayerType = PFD_MAIN_PLANE;
37
38    format = ChoosePixelFormat(gw->dc, &pfd);
39    if (!format)
40      goto release_dc;
41
42    SetPixelFormat(gw->dc, format, &pfd);
43
44    if (pfd.iPixelType != PFD_TYPE_RGBA)
45      goto release_dc;
46
47    gw->context = wglCreateContext(gw->dc);
48    if (!gw->context)
49      goto release_dc;
50
51    wglMakeCurrent(gw->dc, gw->context);
52
53    if (glewInit() != GLEW_OK)
54      goto delete_context;
55
56    if (!GLEW_VERSION_2_0)
57      {
58         fprintf(stderr, "\nERROR: OpenGL 2.0 not supported. Exiting...\n\n");
59         goto delete_context;
60      }
61
62    _evas_gl_glew_window = gw;
63
64    gw->gl_context = evas_gl_common_context_new();
65    if (!gw->gl_context)
66      goto delete_context;
67    evas_gl_common_context_resize(gw->gl_context, width, height);
68
69    return gw;
70
71  delete_context:
72    wglMakeCurrent(NULL, NULL);
73    wglDeleteContext(gw->context);
74  release_dc:
75    ReleaseDC(window, gw->dc);
76  free_window:
77    free(gw);
78
79    return NULL;
80 }
81
82 void
83 eng_window_free(Evas_GL_Glew_Window *gw)
84 {
85    if (!gw)
86      return;
87    if (gw == _evas_gl_glew_window) _evas_gl_glew_window = NULL;
88    evas_gl_common_context_free(gw->gl_context);
89    wglMakeCurrent(NULL, NULL);
90    wglDeleteContext(gw->context);
91    ReleaseDC(gw->window, gw->dc);
92    free(gw);
93 }
94
95 void
96 eng_window_use(Evas_GL_Glew_Window *gw)
97 {
98    if (_evas_gl_glew_window != gw)
99      {
100         if (_evas_gl_glew_window)
101           evas_gl_common_context_flush(_evas_gl_glew_window->gl_context);
102         _evas_gl_glew_window = gw;
103         wglMakeCurrent(gw->dc, gw->context);
104      }
105    evas_gl_common_context_use(gw->gl_context);
106 }
107
108 void
109 eng_window_swap_buffers(Evas_GL_Glew_Window *gw)
110 {
111    SwapBuffers(gw->dc);
112 }
113
114 void
115 eng_window_vsync_set(int on)
116 {
117 #if 1 /* Using Glew */
118    wglSwapIntervalEXT(on);
119 #else /* Using plain OpenGL */
120    const char *extensions = glGetString(GL_EXTENSIONS);
121
122    /* check if WGL_EXT_swap_control extension is supported */
123    if (strstr(extensions, "WGL_EXT_swap_control") == 0)
124      return;
125    else
126      {
127         wglSwapIntervalEXT = (PFNWGLSWAPINTERVALFARPROC)wglGetProcAddress("wglSwapIntervalEXT");
128
129         if (wglSwapIntervalEXT)
130           wglSwapIntervalEXT(on);
131      }
132 #endif
133 }