version 2.8
gwin_gl3d.c
Go to the documentation of this file.
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.org/license.html
6  */
7 
8 /**
9  * @file src/gwin/gwin_gl3d.c
10  * @brief GWIN sub-system button code
11  */
12 
13 #include "../../gfx.h"
14 
15 #if GFX_USE_GWIN && GWIN_NEED_GL3D
16 
17 #if GDISP_PIXELFORMAT != GDISP_PIXELFORMAT_RGB565
18  #error "GWIN: GL3D only support GDISP_PIXELFORMAT_RGB565 color format (TinyGL limitation)"
19 #endif
20 
21 #include "gwin_class.h"
22 
23 #include "../../3rdparty/tinygl-0.4-ugfx/src/zgl.h"
24 
25 // Forward definitions
26 static void gl3dDestroy(GWindowObject *gh);
27 static void gl3dRedraw(GWindowObject *gh);
28 static int gl3dResizeGLViewport(GLContext *c, int *xsize_ptr, int *ysize_ptr);
29 
30 static const gwinVMT gl3dVMT = {
31  "GL3D", // The classname
32  sizeof(GGL3DObject), // The object size
33  gl3dDestroy, // The destroy routine
34  gl3dRedraw, // The redraw routine
35  0, // The after-clear routine
36 };
37 
38 static bool_t haveGLwindow = FALSE;
39 
40 GHandle gwinGGL3DCreate(GDisplay *g, GGL3DObject *gl, const GWindowInit *pInit) {
41  ZBuffer * zb;
42  GLContext * glcxt;
43 
44  // Only one GL3D window allowed at a time (TinyGL limitation)
45  if (haveGLwindow)
46  return 0;
47 
48  if (!(gl = (GGL3DObject *)_gwindowCreate(g, &gl->g, pInit, &gl3dVMT, 0)))
49  return 0;
50 
51  // Must be a multiple of 4 bytes
52  gl->g.width &= ~3;
53  gl->g.height &= ~3;
54 
55  zb = ZB_open(gl->g.width, gl->g.height, ZB_MODE_5R6G5B, 0, NULL, NULL, NULL);
56  if (!zb) {
57  if ((gl->g.flags & GWIN_FLG_DYNAMIC))
58  gfxFree(gl);
59  return 0;
60  }
61 
62  /* initialisation of the TinyGL interpreter */
63  glInit(zb);
64  gl->glcxt = glcxt = gl_get_context();
65  glcxt->opaque = gl;
66  glcxt->gl_resize_viewport = gl3dResizeGLViewport;
67 
68  /* set the viewport : we force a call to the viewport resize routine */
69  glcxt->viewport.xsize=-1;
70  glcxt->viewport.ysize=-1;
71 
72  glViewport(0, 0, gl->g.width, gl->g.height);
73 
74  haveGLwindow = TRUE;
75  gwinSetVisible((GHandle)gl, pInit->show);
76  return (GHandle)gl;
77 }
78 
79 static void gl3dDestroy(GWindowObject *gh) {
80  (void) gh;
81  glClose();
82  haveGLwindow = FALSE;
83 }
84 
85 static void gl3dRedraw(GWindowObject *gh) {
86  ZBuffer * zb;
87 
88  zb = ((GGL3DObject *)gh)->glcxt->zb;
89  gdispGBlitArea(gh->display, gh->x, gh->y, zb->xsize, zb->ysize, 0, 0, zb->linesize/sizeof(color_t), (const pixel_t *)zb->pbuf);
90 }
91 
92 static int gl3dResizeGLViewport(GLContext *c, int *xsize_ptr, int *ysize_ptr) {
93  int cx, cy;
94 
95  cx = *xsize_ptr;
96  cy = *ysize_ptr;
97 
98  // We ensure that cx and cy are multiples of 4 for the zbuffer. TODO: find a better solution
99  cx &= ~3;
100  cy &= ~3;
101 
102  if (cx <= 0 || cy <= 0)
103  return -1;
104 
105  *xsize_ptr = cx;
106  *ysize_ptr = cy;
107 
108  // Resize the GWIN???
109 
110  // Resize the Z buffer
111  ZB_resize(c->zb, NULL, cx, cy);
112  return 0;
113 }
114 
115 /**
116  * TinyGL support routines
117  */
118 
119 #include <string.h>
120 
121 #define NO_CLIBRARY
122 
123 void tgl_warning(const char *format, ...) { (void)format; }
124 void tgl_trace(const char *format, ...) { (void)format; }
125 void tgl_fixme(const char *format, ...) { (void)format; }
126 void gl_fatal_error(char *format, ...) { gfxHalt(format); }
127 void gl_assert(int test) { if (!test) gfxHalt("TinyGL Assert"); }
128 
129 void gl_free(void *p) { gfxFree(p); }
130 void *gl_malloc(int size) { return gfxAlloc(size); }
131 
132 void *gl_zalloc(int size) {
133  void *p;
134 
135  p = gfxAlloc(size);
136  if (p)
137  memset(p, 0, size);
138  return p;
139 }
140 
141 
142 /**
143  * Pre-load TinyGL headers
144  */
145 
146 /**
147  * TinyGL wrapper code
148  */
149 
150 #include "../../3rdparty/tinygl-0.4-ugfx/src/api.c"
151 #include "../../3rdparty/tinygl-0.4-ugfx/src/list.c"
152 #include "../../3rdparty/tinygl-0.4-ugfx/src/vertex.c"
153 #include "../../3rdparty/tinygl-0.4-ugfx/src/init.c"
154 #include "../../3rdparty/tinygl-0.4-ugfx/src/matrix.c"
155 #include "../../3rdparty/tinygl-0.4-ugfx/src/texture.c"
156 #include "../../3rdparty/tinygl-0.4-ugfx/src/misc.c"
157 #include "../../3rdparty/tinygl-0.4-ugfx/src/clear.c"
158 #include "../../3rdparty/tinygl-0.4-ugfx/src/light.c"
159 #include "../../3rdparty/tinygl-0.4-ugfx/src/clip.c"
160 #include "../../3rdparty/tinygl-0.4-ugfx/src/select.c"
161 #include "../../3rdparty/tinygl-0.4-ugfx/src/get.c"
162 #include "../../3rdparty/tinygl-0.4-ugfx/src/zbuffer.c"
163 #include "../../3rdparty/tinygl-0.4-ugfx/src/zline.c"
164 #include "../../3rdparty/tinygl-0.4-ugfx/src/zdither.c"
165 #include "../../3rdparty/tinygl-0.4-ugfx/src/ztriangle.c"
166 #include "../../3rdparty/tinygl-0.4-ugfx/src/zmath.c"
167 #include "../../3rdparty/tinygl-0.4-ugfx/src/image_util.c"
168 #include "../../3rdparty/tinygl-0.4-ugfx/src/arrays.c"
169 #include "../../3rdparty/tinygl-0.4-ugfx/src/specbuf.c"
170 
171 #endif /* GFX_USE_GWIN && GWIN_NEED_GL3D */
The structure to initialise a GWIN.
Definition: gwin.h:75
coord_t y
Definition: gwin.h:48
coord_t x
Definition: gwin.h:47
#define FALSE
Generic &#39;false&#39; boolean constant.
Definition: gfx.h:31
GHandle gwinGGL3DCreate(GDisplay *g, GGL3DObject *gg, const GWindowInit *pInit)
Create a gl3d window.
void * gfxAlloc(size_t sz)
Allocate memory.
void gwinSetVisible(GHandle gh, bool_t visible)
Sets whether a window is visible or not.
bool_t show
Definition: gwin.h:80
GDisplay * display
Definition: gwin.h:46
void gfxFree(void *ptr)
Free memory.
void gfxHalt(const char *msg)
Halt the GFX application due to an error.
The Virtual Method Table for a GWIN window.
Definition: gwin_class.h:55
A window object structure.
Definition: gwin.h:40
COLOR_TYPE color_t
The color type definition.
Definition: gdisp_colors.h:412
void gdispGBlitArea(GDisplay *g, coord_t x, coord_t y, coord_t cx, coord_t cy, coord_t srcx, coord_t srcy, coord_t srccx, const pixel_t *buffer)
Fill an area using the supplied bitmap.
#define TRUE
Generic &#39;true&#39; boolean constant.
Definition: gfx.h:38
color_t pixel_t
The pixel format.
Definition: gdisp.h:226