summaryrefslogtreecommitdiff
path: root/vcl/inc/glyphy/demo/demo-common.h
blob: 1c070f23d945ffa285395083e4565b0eb1a0ab22 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright 2012 Google, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Google Author(s): Behdad Esfahbod, Maysum Panju
 */

#ifndef DEMO_COMMON_H
#define DEMO_COMMON_H

#include <glyphy.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>

#include <algorithm>
#include <vector>

/* Tailor config for various platforms. */

#ifdef EMSCRIPTEN
/* https://github.com/kripken/emscripten/issues/340 */
#  undef HAVE_GLEW
   /* WebGL shaders are ES2 */
#  define GL_ES_VERSION_2_0 1
#endif

#if defined(__ANDROID__)
#  define HAVE_GLES2 1
#  define HAVE_GLUT 1
#endif

#ifdef _WIN32
#  define HAVE_GL 1
#  define HAVE_GLEW 1
#endif

/* Get Glew out of the way. */
#ifdef HAVE_GLEW
#  include <GL/glew.h>
#else
#  define GLEW_OK 0
   static inline int glewInit (void) { return GLEW_OK; }
   static inline int glewIsSupported (const char *s)
   { return 0 == strcmp ("GL_VERSION_2_0", s); }
#endif /* HAVE_GLEW */

/* WTF this block?! */
#if defined(HAVE_GLES2)
#  include <GLES2/gl2.h>
#elif defined(HAVE_GL)
#  ifndef HAVE_GLEW
#    define GL_GLEXT_PROTOTYPES 1
#    if defined(__APPLE__)
#      include <OpenGL/gl.h>
#    else
#      include <GL/gl.h>
#    endif
#  endif
#  if defined(__APPLE__)
#    include <OpenGL/OpenGL.h>
#  else
#    ifdef HAVE_GLEW
#      ifdef _WIN32
#	 include <GL/wglew.h>
#      else
#	include <GL/glxew.h>
#      endif
#    endif
#  endif
#endif /* HAVE_GL */

/* Finally, Glut. */
#ifdef HAVE_GLUT
#  if defined(__APPLE__)
#    include <GLUT/glut.h>
#  else
#    include <GL/glut.h>
#  endif
#endif




/* Logging. */
#ifdef __ANDROID__
#  include <android/log.h>
#  define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "glyphy-demo", __VA_ARGS__))
#  define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "glyphy-demo", __VA_ARGS__))
#  define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "glyphy-demo", __VA_ARGS__))
#else /* !__ANDROID__ */
#if 0
#  define LOGI(...) ((void) fprintf (stderr, __VA_ARGS__))
#  define LOGW(...) ((void) fprintf (stderr, __VA_ARGS__))
#  define LOGE(...) ((void) fprintf (stderr, __VA_ARGS__), abort ())
#else
#  define LOGI(...) do { } while(false)
#  define LOGW(...) do { } while(false)
#  define LOGE(...) do { } while(false)
#endif
#endif



#define STRINGIZE1(Src) #Src
#define STRINGIZE(Src) STRINGIZE1(Src)

#define ARRAY_LEN(Array) (sizeof (Array) / sizeof (*Array))


#define MIN_FONT_SIZE 10
#define TOLERANCE (1./2048)


#define gl(name) \
	for (GLint ee_, ii_ = 0; \
	     ii_ < 1; \
	     (ii_++, \
	      (ee_ = glGetError()) && \
	      (reportGLerror (ee_, #name, __LINE__, __FILE__), 0))) \
	  gl##name


static inline void
reportGLerror(GLint e, const char *api, int line, const char *file)
{
  fflush(stdout);
  fprintf(stderr, "\nwglGetCurrentDC=%p wglGetCurrentContext=%p\n", wglGetCurrentDC(), wglGetCurrentContext());
  fprintf (stderr, "gl%s failed with error %04X on %s:%d\n", api, e, file, line);
  fflush (stderr);
  exit (1);
}

static inline void
die (const char *msg)
{
  fprintf (stderr, "%s\n", msg);
  exit (1);
}

template <typename T>
T clamp (T v, T m, T M)
{
  return v < m ? m : v > M ? M : v;
}


#if defined(_MSC_VER)
#define DEMO_FUNC __FUNCSIG__
#else
#define DEMO_FUNC __func__
#endif

struct auto_trace_t
{
  auto_trace_t (const char *func_) : func (func_)
  { printf ("Enter: %s\n", func); }

  ~auto_trace_t (void)
  { printf ("Leave: %s\n", func); }

  private:
  const char * const func;
};

#if 0
#define TRACE() auto_trace_t trace(DEMO_FUNC)
#else
#define TRACE() do { } while(false)
#endif

#endif /* DEMO_COMMON_H */