Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (C) 2010 LunarG Inc.
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice shall be included
  12.  * in all copies or substantial portions of the Software.
  13.  *
  14.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  17.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20.  * DEALINGS IN THE SOFTWARE.
  21.  *
  22.  * Authors:
  23.  *    Chia-I Wu <olv@lunarg.com>
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <stdarg.h>
  30. #include <sys/time.h>
  31.  
  32. #include "EGL/egl.h"
  33. #include "EGL/eglext.h"
  34.  
  35. #include "render.h"
  36. #include "eglutint.h"
  37. #include <kos32sys.h>
  38.  
  39. static struct eglut_state _eglut_state = {
  40.    .api_mask = EGLUT_OPENGL_BIT,
  41.    .window_width = 300,
  42.    .window_height = 300,
  43.    .verbose = 0,
  44.    .num_windows = 0,
  45. };
  46.  
  47. struct eglut_state *_eglut = &_eglut_state;
  48.  
  49. void
  50. _eglutFatal(char *format, ...)
  51. {
  52.   va_list args;
  53.  
  54.   va_start(args, format);
  55.  
  56.   fprintf(stderr, "EGLUT: ");
  57.   vfprintf(stderr, format, args);
  58.   va_end(args);
  59.   putc('\n', stderr);
  60.  
  61.   exit(1);
  62. }
  63.  
  64. /* return current time (in milliseconds) */
  65. int
  66. _eglutNow(void)
  67. {
  68.    struct timeval tv;
  69. #ifdef __VMS
  70.    (void) gettimeofday(&tv, NULL );
  71. #else
  72.    struct timezone tz;
  73.    (void) gettimeofday(&tv, &tz);
  74. #endif
  75.    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  76. }
  77.  
  78. static void
  79. _eglutDestroyWindow(struct eglut_window *win)
  80. {
  81.    eglDestroySurface(_eglut->dpy, win->surface);
  82.    _eglutNativeFiniWindow(win);
  83.    eglDestroyContext(_eglut->dpy, win->context);
  84. }
  85.  
  86. static EGLConfig
  87. _eglutChooseConfig(void)
  88. {
  89.    EGLConfig config;
  90.    EGLint config_attribs[32];
  91.    EGLint num_configs, i;
  92.  
  93.    i = 0;
  94.    config_attribs[i++] = EGL_RED_SIZE;
  95.    config_attribs[i++] = 1;
  96.    config_attribs[i++] = EGL_GREEN_SIZE;
  97.    config_attribs[i++] = 1;
  98.    config_attribs[i++] = EGL_BLUE_SIZE;
  99.    config_attribs[i++] = 1;
  100.    config_attribs[i++] = EGL_DEPTH_SIZE;
  101.    config_attribs[i++] = 1;
  102.  
  103.    config_attribs[i++] = EGL_SURFACE_TYPE;
  104.    config_attribs[i++] = EGL_WINDOW_BIT;
  105.  
  106.    config_attribs[i++] = EGL_RENDERABLE_TYPE;
  107.    config_attribs[i++] = EGL_OPENGL_BIT;
  108.    config_attribs[i] = EGL_NONE;
  109.  
  110. //   renderable_type = 0x0;
  111. //   if (_eglut->api_mask & EGLUT_OPENGL_BIT)
  112. //      renderable_type |= EGL_OPENGL_BIT;
  113. //   if (_eglut->api_mask & EGLUT_OPENGL_ES1_BIT)
  114. //      renderable_type |= EGL_OPENGL_ES_BIT;
  115. //   if (_eglut->api_mask & EGLUT_OPENGL_ES2_BIT)
  116. //      renderable_type |= EGL_OPENGL_ES2_BIT;
  117. //   if (_eglut->api_mask & EGLUT_OPENVG_BIT)
  118. //      renderable_type |= EGL_OPENVG_BIT;
  119.  
  120.    if (!eglChooseConfig(_eglut->dpy,
  121.             config_attribs, &config, 1, &num_configs) || !num_configs)
  122.       _eglutFatal("failed to choose a config");
  123.  
  124.    return config;
  125. }
  126.  
  127. static struct eglut_window *
  128. _eglutCreateWindow(const char *title, int x, int y, int w, int h)
  129. {
  130.     struct eglut_window *win;
  131.  
  132.     win = calloc(1, sizeof(*win));
  133.     if (!win)
  134.         _eglutFatal("failed to allocate window");
  135.  
  136.     win->config = _eglutChooseConfig();
  137.  
  138.     eglBindAPI(EGL_OPENGL_API);
  139.     win->context = eglCreateContext(_eglut->dpy, win->config, EGL_NO_CONTEXT, NULL);
  140.     if (!win->context)
  141.         _eglutFatal("failed to create context");
  142.  
  143.     _eglutNativeInitWindow(win, title, x, y, w, h);
  144.     switch (_eglut->surface_type) {
  145.         case EGL_WINDOW_BIT:
  146.             win->surface = eglCreateWindowSurface(_eglut->dpy,
  147.                            win->config, win->native.u.window, NULL);
  148.         break;
  149.         default:
  150.             break;
  151.     }
  152.     if (win->surface == EGL_NO_SURFACE)
  153.         _eglutFatal("failed to create surface");
  154.  
  155.     return win;
  156. }
  157.  
  158. void
  159. eglutInitAPIMask(int mask)
  160. {
  161.    _eglut->api_mask = mask;
  162. }
  163.  
  164. void
  165. eglutInitWindowSize(int width, int height)
  166. {
  167.    _eglut->window_width = width;
  168.    _eglut->window_height = height;
  169. }
  170.  
  171. void
  172. eglutInit(int argc, char **argv)
  173. {
  174.    int i;
  175.  
  176.    for (i = 1; i < argc; i++) {
  177.       if (strcmp(argv[i], "-display") == 0)
  178.          _eglut->display_name = argv[++i];
  179.       else if (strcmp(argv[i], "-info") == 0) {
  180.          _eglut->verbose = 1;
  181.       }
  182.    }
  183.  
  184.    _eglutNativeInitDisplay();
  185.    _eglut->dpy = eglGetDisplay(_eglut->native_dpy);
  186.  
  187.    if (!eglInitialize(_eglut->dpy, &_eglut->major, &_eglut->minor))
  188.       _eglutFatal("failed to initialize EGL display");
  189.  
  190.    _eglut->init_time = _eglutNow();
  191.  
  192.    printf("EGL_VERSION = %s\n", eglQueryString(_eglut->dpy, EGL_VERSION));
  193.    if (_eglut->verbose) {
  194.       printf("EGL_VENDOR = %s\n", eglQueryString(_eglut->dpy, EGL_VENDOR));
  195.       printf("EGL_EXTENSIONS = %s\n",
  196.             eglQueryString(_eglut->dpy, EGL_EXTENSIONS));
  197.       printf("EGL_CLIENT_APIS = %s\n",
  198.             eglQueryString(_eglut->dpy, EGL_CLIENT_APIS));
  199.    }
  200. }
  201.  
  202. int
  203. eglutGet(int state)
  204. {
  205.    int val;
  206.  
  207.    switch (state) {
  208.    case EGLUT_ELAPSED_TIME:
  209.       val = _eglutNow() - _eglut->init_time;
  210.       break;
  211.    default:
  212.       val = -1;
  213.       break;
  214.    }
  215.  
  216.    return val;
  217. }
  218.  
  219. void
  220. eglutIdleFunc(EGLUTidleCB func)
  221. {
  222.    _eglut->idle_cb = func;
  223. }
  224.  
  225. void
  226. eglutPostRedisplay(void)
  227. {
  228.    _eglut->redisplay = 1;
  229. }
  230.  
  231. void
  232. eglutMainLoop(void)
  233. {
  234.    struct eglut_window *win = _eglut->current;
  235.  
  236.    if (!win)
  237.       _eglutFatal("no window is created\n");
  238.  
  239.    if (win->reshape_cb)
  240.       win->reshape_cb(win->native.width, win->native.height);
  241.  
  242.    _eglutNativeEventLoop();
  243. }
  244.  
  245. void
  246. eglutFini(void)
  247. {
  248.    eglTerminate(_eglut->dpy);
  249.    _eglutNativeFiniDisplay();
  250. }
  251.  
  252. void
  253. eglutDestroyWindow(int win)
  254. {
  255.    eglMakeCurrent(_eglut->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  256.    _eglutDestroyWindow(_eglut->current);
  257. }
  258.  
  259. static void
  260. _eglutDefaultKeyboard(unsigned char key)
  261. {
  262. }
  263.  
  264. int
  265. eglutCreateWindow(const char *title)
  266. {
  267.     struct eglut_window *win;
  268.     int skinh;
  269.  
  270.     win = _eglutCreateWindow(title, 20, 20,
  271.           _eglut->window_width, _eglut->window_height);
  272.  
  273.     win->index = _eglut->num_windows++;
  274.     win->reshape_cb = NULL;
  275.     win->display_cb = NULL;
  276.     win->keyboard_cb = _eglutDefaultKeyboard;
  277.     win->special_cb = NULL;
  278.  
  279.     if (!eglMakeCurrent(_eglut->dpy, win->surface, win->surface, win->context))
  280.         _eglutFatal("failed to make window current");
  281.     _eglut->current = win;
  282.  
  283.     skinh = get_skin_height();
  284.  
  285.     _eglut->render = create_render(_eglut->dpy, win->surface, TYPE_3_BORDER_WIDTH, skinh);
  286.     return win->index;
  287. }
  288.  
  289. int
  290. eglutGetWindowWidth(void)
  291. {
  292.    struct eglut_window *win = _eglut->current;
  293.    return win->native.width;
  294. }
  295.  
  296. int
  297. eglutGetWindowHeight(void)
  298. {
  299.    struct eglut_window *win = _eglut->current;
  300.    return win->native.height;
  301. }
  302.  
  303. void
  304. eglutDisplayFunc(EGLUTdisplayCB func)
  305. {
  306.    struct eglut_window *win = _eglut->current;
  307.    win->display_cb = func;
  308.  
  309. }
  310.  
  311. void
  312. eglutReshapeFunc(EGLUTreshapeCB func)
  313. {
  314.    struct eglut_window *win = _eglut->current;
  315.    win->reshape_cb = func;
  316. }
  317.  
  318. void
  319. eglutKeyboardFunc(EGLUTkeyboardCB func)
  320. {
  321.    struct eglut_window *win = _eglut->current;
  322.    win->keyboard_cb = func;
  323. }
  324.  
  325. void
  326. eglutSpecialFunc(EGLUTspecialCB func)
  327. {
  328.    struct eglut_window *win = _eglut->current;
  329.    win->special_cb = func;
  330. }
  331.  
  332. int atexit(void (*func)(void))
  333. {
  334.     return 0;
  335. };
  336.