TestGlfmWindow.cpp
Go to the documentation of this file.
1 #include "TestGlfmWindow.hpp"
2 #include <ivorium_config.hpp>
3 
4 #if IV_GLPLATFORM_GLFM
5 
6 #include <glfm.h>
7 
8 namespace iv
9 {
10 
11 static GLint program = 0;
12 static GLuint vertexBuffer = 0;
13 
14 static void onFrame(GLFMDisplay *display, const double frameTime);
15 static void onSurfaceCreated(GLFMDisplay *display, const int width, const int height);
16 static void onSurfaceDestroyed(GLFMDisplay *display);
17 
18 
19 TestGlfmWindow::TestGlfmWindow( GLFMDisplay * display )
20 {
21  glfmSetDisplayConfig(display,
22  GLFMRenderingAPIOpenGLES2,
23  GLFMColorFormatRGBA8888,
24  GLFMDepthFormatNone,
25  GLFMStencilFormatNone,
26  GLFMMultisampleNone);
27  glfmSetSurfaceCreatedFunc(display, onSurfaceCreated);
28  glfmSetSurfaceResizedFunc(display, onSurfaceCreated);
29  glfmSetSurfaceDestroyedFunc(display, onSurfaceDestroyed);
30  glfmSetMainLoopFunc(display, onFrame);
31 }
32 
34 {
35 }
36 
37 
38 static void onSurfaceCreated(GLFMDisplay *display, const int width, const int height) {
39  glViewport(0, 0, width, height);
40 }
41 
42 static void onSurfaceDestroyed(GLFMDisplay *display) {
43  // When the surface is destroyed, all existing GL resources are no longer valid.
44  program = 0;
45  vertexBuffer = 0;
46 }
47 
48 static GLuint compileShader(const GLenum type, const GLchar *shaderString) {
49  const GLint shaderLength = (GLint)strlen(shaderString);
50  GLuint shader = glCreateShader(type);
51  glShaderSource(shader, 1, &shaderString, &shaderLength);
52  glCompileShader(shader);
53  return shader;
54 }
55 
56 static void onFrame(GLFMDisplay *display, const double frameTime) {
57  if (program == 0) {
58  const GLchar *vertexShader =
59  "attribute highp vec4 position;\n"
60  "void main() {\n"
61  " gl_Position = position;\n"
62  "}";
63 
64  const GLchar *fragmentShader =
65  "void main() {\n"
66  " gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
67  "}";
68 
69  program = glCreateProgram();
70  GLuint vertShader = compileShader(GL_VERTEX_SHADER, vertexShader);
71  GLuint fragShader = compileShader(GL_FRAGMENT_SHADER, fragmentShader);
72 
73  glAttachShader(program, vertShader);
74  glAttachShader(program, fragShader);
75 
76  glLinkProgram(program);
77 
78  glDeleteShader(vertShader);
79  glDeleteShader(fragShader);
80  }
81  if (vertexBuffer == 0) {
82  const GLfloat vertices[] = {
83  0.0, 0.5, 0.0,
84  -0.5, -0.5, 0.0,
85  0.5, -0.5, 0.0,
86  };
87  glGenBuffers(1, &vertexBuffer);
88  glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
89  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
90  }
91 
92  glClearColor(0.4f, 0.0f, 0.6f, 1.0f);
93  glClear(GL_COLOR_BUFFER_BIT);
94 
95  glUseProgram(program);
96  glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
97 
98  glEnableVertexAttribArray(0);
99  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
100  glDrawArrays(GL_TRIANGLES, 0, 3);
101 }
102 
103 }
104 
105 #endif