commit 8830a7671f08db7247fbac950483ff40207fc3fb
parent 6ef510548444e7cc943769f2d1992ccaeaf8a334
Author: Brian Swetland <swetland@frotz.net>
Date: Tue, 29 Jan 2013 17:54:57 -0800
further c++ification of all the things
Diffstat:
M | Makefile | | | 8 | ++++++-- |
A | app.h | | | 53 | +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | debugtext.cc | | | 2 | +- |
D | glue.h | | | 84 | ------------------------------------------------------------------------------- |
A | opengl.h | | | 71 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | program.cc | | | 56 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
M | sdlglue.cc | | | 140 | ++++++++++++++++++++++++++----------------------------------------------------- |
M | test1.cc | | | 38 | +++++++++++++++++++++++++------------- |
M | test2.cc | | | 46 | +++++++++++++++++++++++++++++----------------- |
D | test2d.cc | | | 180 | ------------------------------------------------------------------------------- |
M | test3.cc | | | 44 | ++++++++++++++++++++++++++++---------------- |
M | test4.cc | | | 43 | ++++++++++++++++++++++++++----------------- |
M | test5.cc | | | 78 | ++++++++++++++++++++++++++++++++++++++++++++++-------------------------------- |
13 files changed, 385 insertions(+), 458 deletions(-)
diff --git a/Makefile b/Makefile
@@ -7,8 +7,12 @@ SDLLIBS := $(shell $(SDLCFG) --libs)
CFLAGS := $(SDLFLAGS) -DWITH_SDL2=0 -Wall -g -O2
CFLAGS += -ffunction-sections -fdata-sections
-LFLAGS := -Wl,-gc-sections -static-libstdc++
+
+LFLAGS := -static-libstdc++
+LFLAGS += -Wl,-gc-sections
+
CXXFLAGS := $(CFLAGS)
+
LIBS := stuff.a $(SDLLIBS) -lGL -lm -lpng
all:: everything
@@ -24,7 +28,7 @@ stuff.a: $(LIBOBJS)
rm -f stuff.a
ar cr stuff.a $(LIBOBJS)
-APPS := test1 test2 test3 test4 test5 test2d
+APPS := test1 test2 test3 test4 test5
define build-test
$1: $1.o stuff.a
diff --git a/app.h b/app.h
@@ -0,0 +1,53 @@
+/* Copyright 2013 Brian Swetland <swetland@frotz.net>
+ *
+ * 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.
+ */
+
+#ifndef _APP_H_
+#define _APP_H_
+
+#include "opengl.h"
+
+class App {
+public:
+ App();
+ virtual ~App();
+
+ // configuration options
+ void setSize(int w, int h);
+
+ // parse commandline for configuration overrides
+ void setOptions(int argc, char **argv);
+
+ // state query
+ int fps(void) { return _fps; };
+ int width(void) { return _width; };
+ int height(void) { return _height; };
+ float aspect(void) { return ((float) _width) / ((float) _height); };
+ int keydown(int code) { return _keystate[code]; }
+
+ int run(void);
+
+ virtual int init(void) = 0;
+ virtual int render(void) = 0;
+
+private:
+ int _width, _height;
+ int _vsync;
+ int _fps;
+
+ char _keystate[512];
+ void handleEvents(void);
+};
+
+#endif
diff --git a/debugtext.cc b/debugtext.cc
@@ -21,8 +21,8 @@
#include <unistd.h>
#endif
+#include "opengl.h"
#include "util.h"
-#include "glue.h"
#include "matrix.h"
#include "debugtext.h"
diff --git a/glue.h b/glue.h
@@ -1,84 +0,0 @@
-/* Copyright 2013 Brian Swetland <swetland@frotz.net>
- *
- * 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.
- */
-
-#ifndef _SDL_GLUE_H_
-#define _SDL_GLUE_H_
-
-#ifndef _WIN32
-#define GL_GLEXT_PROTOTYPES 1
-#endif
-
-#include <SDL_opengl.h>
-
-struct ctxt {
- unsigned width;
- unsigned height;
- void *data;
-};
-
-int scene_init(struct ctxt *c);
-int scene_draw(struct ctxt *c);
-
-int shader_compile(const char *vshader, const char *fshader,
- GLuint *pgm, GLuint *vshd, GLuint *fshd);
-
-#ifdef _WIN32
-#ifndef GLUE_DEFINE_EXTENSIONS
-#define GLXTN extern
-#else
-#define GLXTN
-#endif
-
-GLXTN PFNGLACTIVETEXTUREPROC glActiveTexture;
-GLXTN PFNGLATTACHSHADERPROC glAttachShader;
-GLXTN PFNGLBINDBUFFERPROC glBindBuffer;
-GLXTN PFNGLBUFFERDATAPROC glBufferData;
-GLXTN PFNGLCOMPILESHADERPROC glCompileShader;
-GLXTN PFNGLCREATEPROGRAMPROC glCreateProgram;
-GLXTN PFNGLCREATESHADERPROC glCreateShader;
-GLXTN PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
-GLXTN PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
-GLXTN PFNGLGENBUFFERSPROC glGenBuffers;
-GLXTN PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation;
-GLXTN PFNGLGETPROGRAMIVPROC glGetProgramiv;
-GLXTN PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
-GLXTN PFNGLGETSHADERIVPROC glGetShaderiv;
-GLXTN PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
-GLXTN PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
-GLXTN PFNGLLINKPROGRAMPROC glLinkProgram;
-GLXTN PFNGLSHADERSOURCEPROC glShaderSource;
-GLXTN PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv;
-GLXTN PFNGLUNIFORM1IPROC glUniform1i;
-GLXTN PFNGLUNIFORM4FVPROC glUniform4fv;
-GLXTN PFNGLUSEPROGRAMPROC glUseProgram;
-GLXTN PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
-
-#ifdef GLUE_DEFINE_EXTENSIONS
-#define EFUNC(n) { (void**) &n, #n }
-struct {
- void **func;
- const char *name;
-} fntb[] = {
- EFUNC(glActiveTexture), EFUNC(glAttachShader), EFUNC(glBindBuffer), EFUNC(glBufferData),
- EFUNC(glCompileShader), EFUNC(glCreateProgram), EFUNC(glCreateShader), EFUNC(glDisableVertexAttribArray),
- EFUNC(glEnableVertexAttribArray), EFUNC(glGenBuffers), EFUNC(glGetAttribLocation), EFUNC(glGetProgramiv),
- EFUNC(glGetProgramInfoLog), EFUNC(glGetShaderiv), EFUNC(glGetShaderInfoLog),
- EFUNC(glGetUniformLocation), EFUNC(glLinkProgram), EFUNC(glShaderSource),
- EFUNC(glUniformMatrix4fv), EFUNC(glUseProgram), EFUNC(glUniform1i), EFUNC(glUniform4fv), EFUNC(glVertexAttribPointer),
-};
-#endif
-
-#endif
-#endif
diff --git a/opengl.h b/opengl.h
@@ -0,0 +1,71 @@
+/* Copyright 2013 Brian Swetland <swetland@frotz.net>
+ *
+ * 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.
+ */
+
+#ifndef _OPEN_GL_H_
+#define _OPEN_GL_H_
+
+#ifndef _WIN32
+#define GL_GLEXT_PROTOTYPES 1
+#endif
+
+#include <SDL_opengl.h>
+
+#ifdef _WIN32
+#ifndef GLUE_DEFINE_EXTENSIONS
+#define GLXTN extern
+#else
+#define GLXTN
+#endif
+
+GLXTN PFNGLACTIVETEXTUREPROC glActiveTexture;
+GLXTN PFNGLATTACHSHADERPROC glAttachShader;
+GLXTN PFNGLBINDBUFFERPROC glBindBuffer;
+GLXTN PFNGLBUFFERDATAPROC glBufferData;
+GLXTN PFNGLCOMPILESHADERPROC glCompileShader;
+GLXTN PFNGLCREATEPROGRAMPROC glCreateProgram;
+GLXTN PFNGLCREATESHADERPROC glCreateShader;
+GLXTN PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
+GLXTN PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
+GLXTN PFNGLGENBUFFERSPROC glGenBuffers;
+GLXTN PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation;
+GLXTN PFNGLGETPROGRAMIVPROC glGetProgramiv;
+GLXTN PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
+GLXTN PFNGLGETSHADERIVPROC glGetShaderiv;
+GLXTN PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
+GLXTN PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
+GLXTN PFNGLLINKPROGRAMPROC glLinkProgram;
+GLXTN PFNGLSHADERSOURCEPROC glShaderSource;
+GLXTN PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv;
+GLXTN PFNGLUNIFORM1IPROC glUniform1i;
+GLXTN PFNGLUNIFORM4FVPROC glUniform4fv;
+GLXTN PFNGLUSEPROGRAMPROC glUseProgram;
+GLXTN PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
+
+#ifdef GLUE_DEFINE_EXTENSIONS
+#define EFUNC(n) { (void**) &n, #n }
+struct {
+ void **func;
+ const char *name;
+} fntb[] = {
+ EFUNC(glActiveTexture), EFUNC(glAttachShader), EFUNC(glBindBuffer), EFUNC(glBufferData),
+ EFUNC(glCompileShader), EFUNC(glCreateProgram), EFUNC(glCreateShader), EFUNC(glDisableVertexAttribArray),
+ EFUNC(glEnableVertexAttribArray), EFUNC(glGenBuffers), EFUNC(glGetAttribLocation), EFUNC(glGetProgramiv),
+ EFUNC(glGetProgramInfoLog), EFUNC(glGetShaderiv), EFUNC(glGetShaderInfoLog),
+ EFUNC(glGetUniformLocation), EFUNC(glLinkProgram), EFUNC(glShaderSource),
+ EFUNC(glUniformMatrix4fv), EFUNC(glUseProgram), EFUNC(glUniform1i), EFUNC(glUniform4fv), EFUNC(glVertexAttribPointer),
+};
+#endif
+#endif
+#endif
diff --git a/program.cc b/program.cc
@@ -15,11 +15,65 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
-#include "glue.h"
+#include "app.h"
#include "util.h"
#include "program.h"
+static int check_compile_error(GLuint obj, const char *fn) {
+ GLint r, len;
+ char *buf;
+
+ if (fn) glGetShaderiv(obj, GL_COMPILE_STATUS, &r);
+ else glGetProgramiv(obj, GL_LINK_STATUS, &r);
+
+ if (r) return 0;
+
+ if (fn) glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &len);
+ else glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &len);
+
+ buf = (char*) malloc(len + 1);
+ memset(buf, 0, len);
+ if (buf != 0) {
+ if (fn) glGetShaderInfoLog(obj, len, &len, buf);
+ else glGetProgramInfoLog(obj, len, &len, buf);
+ buf[len] = 0;
+ }
+ fprintf(stderr,"Shader %s Error:\n%s\n",
+ fn ? "Compile" : "Link", buf ? buf : "???");
+
+ free(buf);
+ return -1;
+}
+
+static int shader_compile(const char *vshader, const char *fshader,
+ GLuint *_pgm, GLuint *_vshd, GLuint *_fshd) {
+ GLuint pgm, vshd, fshd;
+
+ pgm = glCreateProgram();
+ vshd = glCreateShader(GL_VERTEX_SHADER);
+ fshd = glCreateShader(GL_FRAGMENT_SHADER);
+ glShaderSource(vshd, 1, &vshader, NULL);
+ glShaderSource(fshd, 1, &fshader, NULL);
+ glCompileShader(vshd);
+ check_compile_error(vshd, vshader);
+
+ glCompileShader(fshd);
+ check_compile_error(fshd, fshader);
+
+ glAttachShader(pgm, vshd);
+ glAttachShader(pgm, fshd);
+ glLinkProgram(pgm);
+ if (check_compile_error(pgm, 0))
+ return -1;
+
+ *_pgm = pgm;
+ *_vshd = vshd;
+ *_fshd = fshd;
+ return 0;
+}
+
Program::Program() : vsrc(NULL), fsrc(NULL), status(-1) {
}
diff --git a/sdlglue.cc b/sdlglue.cc
@@ -13,32 +13,32 @@
* limitations under the License.
*/
-#include <SDL.h>
-
#include <stdio.h>
#include <stdlib.h>
+#include <time.h>
+
#ifndef _WIN32
#include <unistd.h>
#endif
-#include <time.h>
-
-#include "util.h"
+#include <SDL.h>
#ifdef _WIN32
#define GLUE_DEFINE_EXTENSIONS
#endif
-#include "glue.h"
+#include "opengl.h"
+#include "util.h"
+#include "app.h"
-void die(const char *fmt, ...) {
+static void die(const char *fmt, ...) {
fprintf(stderr,"ERROR: %s\n", fmt);
exit(-1);
}
#ifdef _WIN32
#if !WITH_SDL2
-int SDL_GL_ExtensionSupported(const char *name) {
+static int SDL_GL_ExtensionSupported(const char *name) {
if (strstr((char*)glGetString(GL_EXTENSIONS), name))
return 1;
else
@@ -46,7 +46,7 @@ int SDL_GL_ExtensionSupported(const char *name) {
}
#endif
-void glsl_init(void) {
+static void glsl_init(void) {
int n;
if (!SDL_GL_ExtensionSupported("GL_ARB_shader_objects") ||
!SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") ||
@@ -63,78 +63,35 @@ void glsl_init(void) {
void glsl_init(void) {}
#endif
-int check_compile_error(GLuint obj, const char *fn) {
- GLint r, len;
- char *buf;
-
- if (fn) glGetShaderiv(obj, GL_COMPILE_STATUS, &r);
- else glGetProgramiv(obj, GL_LINK_STATUS, &r);
-
- if (r) return 0;
-
- if (fn) glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &len);
- else glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &len);
-
- buf = (char*) malloc(len + 1);
- memset(buf, 0, len);
- if (buf != 0) {
- if (fn) glGetShaderInfoLog(obj, len, &len, buf);
- else glGetProgramInfoLog(obj, len, &len, buf);
- buf[len] = 0;
- }
- fprintf(stderr,"Shader %s Error:\n%s\n",
- fn ? "Compile" : "Link", buf ? buf : "???");
-
- free(buf);
- return -1;
+static void quit(void) {
+ SDL_Quit();
+ exit(0);
}
-int shader_compile(const char *vshader, const char *fshader,
- GLuint *_pgm, GLuint *_vshd, GLuint *_fshd) {
- GLuint pgm, vshd, fshd;
-
- pgm = glCreateProgram();
- vshd = glCreateShader(GL_VERTEX_SHADER);
- fshd = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(vshd, 1, &vshader, NULL);
- glShaderSource(fshd, 1, &fshader, NULL);
- glCompileShader(vshd);
- check_compile_error(vshd, vshader);
-
- glCompileShader(fshd);
- check_compile_error(fshd, fshader);
-
- glAttachShader(pgm, vshd);
- glAttachShader(pgm, fshd);
- glLinkProgram(pgm);
- if (check_compile_error(pgm, 0))
- return -1;
-
- *_pgm = pgm;
- *_vshd = vshd;
- *_fshd = fshd;
- return 0;
+App::App() : _width(640), _height(480), _vsync(1), _fps(0) {
+ memset(_keystate, 0, sizeof(_keystate));
}
-void quit(void) {
- SDL_Quit();
- exit(0);
+App::~App() {
}
-unsigned char keystate[SDLK_LAST + 1] = { 0, };
+void App::setSize(int w, int h) {
+ _width = w;
+ _height = h;
+}
-void handle_events(void) {
+void App::handleEvents(void) {
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_KEYDOWN:
- keystate[ev.key.keysym.sym] = 1;
+ _keystate[ev.key.keysym.sym] = 1;
if (ev.key.keysym.sym == SDLK_ESCAPE)
quit();
break;
case SDL_KEYUP:
- keystate[ev.key.keysym.sym] = 0;
+ _keystate[ev.key.keysym.sym] = 0;
break;
case SDL_QUIT:
quit();
@@ -142,36 +99,30 @@ void handle_events(void) {
}
}
-int fps = 0;
-
-int main(int argc, char **argv) {
- int vsync = 1;
- struct ctxt c;
+void App::setOptions(int argc, char **argv) {
char *x;
- time_t t0, t1;
- int count;
-#if WITH_SDL2
- SDL_Window *w;
- SDL_GLContext gc;
-#endif
-
- c.width = 640;
- c.height = 480;
- c.data = 0;
-
argc--;
argv++;
while (argc--) {
if (!strcmp("-nosync",argv[0])) {
- vsync = 0;
+ _vsync = 0;
} else if (isdigit(argv[0][0]) && (x = strchr(argv[0],'x'))) {
- c.width = atoi(argv[0]);
- c.height = atoi(x + 1);
+ _width = atoi(argv[0]);
+ _height = atoi(x + 1);
} else {
fprintf(stderr,"unknown argument '%s'\n",argv[0]);
}
argv++;
}
+}
+
+int App::run(void) {
+ time_t t0, t1;
+ int count;
+#if WITH_SDL2
+ SDL_Window *w;
+ SDL_GLContext gc;
+#endif
if (SDL_Init(SDL_INIT_VIDEO))
die("sdl video init failed");
@@ -195,36 +146,36 @@ int main(int argc, char **argv) {
#endif
/* enable vsync */
- SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,vsync);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,vsync);
+ SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, _vsync);
+ SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, _vsync);
#if WITH_SDL2
- if (!(w = SDL_CreateWindow("Test", 0, 0, c.width, c.height,
+ if (!(w = SDL_CreateWindow("Test", 0, 0, _width, _height,
SDL_WINDOW_OPENGL)))
die("sdl cannot create window");
if (!(gc = SDL_GL_CreateContext(w)))
die("sdl cannot create gl context");
#else
- if (SDL_SetVideoMode(c.width, c.height, 32,
+ if (SDL_SetVideoMode(_width, _height, 32,
SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL) == NULL)
die("sdl cannot set mode");
#endif
glsl_init();
- if (scene_init(&c))
+ if (init())
return -1;
t0 = time(0);
count = 0;
for (;;) {
- handle_events();
+ handleEvents();
- if (scene_draw(&c))
+ if (render())
return -1;
- if (vsync) {
+ if (_vsync) {
#if WITH_SDL2
SDL_GL_SwapWindow(w);
#else
@@ -237,13 +188,12 @@ int main(int argc, char **argv) {
t1 = time(0);
count++;
if (t0 != t1) {
- fps = count;
+ _fps = count;
printf("%d fps\n", count);
count = 0;
t0 = t1;
}
}
-
- return 0;
+ return -1;
}
diff --git a/test1.cc b/test1.cc
@@ -17,42 +17,49 @@
#include <stdlib.h>
#include <unistd.h>
+#include "app.h"
#include "util.h"
-#include "glue.h"
#include "matrix.h"
#include "program.h"
-void *texdata;
-unsigned texw, texh;
+class TestApp : public App {
+public:
+ int init(void);
+ int render(void);
-GLuint tex0;
-GLuint aVertex, aTexCoord;
-GLuint uMVP, uTexture;
+private:
+ void *texdata;
+ unsigned texw, texh;
+
+ GLuint tex0;
+ GLuint aVertex, aTexCoord;
+ GLuint uMVP, uTexture;
-mat4 MVP;
-Program pgm;
+ mat4 MVP;
+ Program pgm;
+};
-GLfloat verts[] = {
+static GLfloat verts[] = {
-0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
};
-GLfloat texcoords[] = {
+static GLfloat texcoords[] = {
0.0, 0.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0,
};
-int scene_init(struct ctxt *c) {
+int TestApp::init(void) {
if (!(texdata = load_png_rgba("texture1.png", &texw, &texh, 1)))
return -1;
MVP.setOrtho(-2.66, 2.66, -2, 2, 1, -1);
- glViewport(0, 0, c->width, c->height);
+ glViewport(0, 0, width(), height());
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
@@ -78,7 +85,7 @@ int scene_init(struct ctxt *c) {
return 0;
}
-int scene_draw(struct ctxt *c) {
+int TestApp::render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
pgm.use();
@@ -100,3 +107,8 @@ int scene_draw(struct ctxt *c) {
return 0;
}
+int main(int argc, char **argv) {
+ TestApp app;
+ app.setOptions(argc, argv);
+ return app.run();
+}
diff --git a/test2.cc b/test2.cc
@@ -18,27 +18,33 @@
#include <unistd.h>
#include <string.h>
+#include "app.h"
#include "util.h"
-#include "glue.h"
#include "program.h"
#include "matrix.h"
#include <math.h>
-void *texdata;
-unsigned texw, texh;
+class TestApp : public App {
+public:
+ int init(void);
+ int render(void);
+private:
+ void *texdata;
+ unsigned texw, texh;
-GLuint tex0;
-GLuint aVertex, aTexCoord;
-GLuint uMVP, uTexture;
+ GLuint tex0;
+ GLuint aVertex, aTexCoord;
+ GLuint uMVP, uTexture;
-Program pgm;
-mat4 perspective;
-mat4 MVP;
+ Program pgm;
+ mat4 perspective;
+ mat4 MVP;
-float a = 0.0;
+ float a;
+};
-GLfloat data[] = {
+static GLfloat data[] = {
-0.5f, 0.5f, 0.5f, 0.25, 0.75,
-0.5f, -0.5f, 0.5f, 0.25, 0.50,
0.5f, -0.5f, 0.5f, 0.50, 0.50,
@@ -70,7 +76,7 @@ GLfloat data[] = {
0.5f, -0.5f, 0.5f, 0.50, 0.50,
};
-GLubyte indices[] = {
+static GLubyte indices[] = {
0, 1, 2, 3, 0, 2,
4, 5, 6, 7, 4, 6,
8, 9, 10, 11, 8, 10,
@@ -79,15 +85,15 @@ GLubyte indices[] = {
20, 21, 22, 23, 20, 22,
};
-int scene_init(struct ctxt *c) {
- float aspect = ((float) c->width) / ((float) c->height);
+int TestApp::init(void) {
+ a = 0.0;
if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh, 1)))
return -1;
- perspective.setPerspective(D2R(60.0), aspect, 1.0, 10.0);
+ perspective.setPerspective(D2R(60.0), aspect(), 1.0, 10.0);
- glViewport(0, 0, c->width, c->height);
+ glViewport(0, 0, width(), height());
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
@@ -115,7 +121,7 @@ int scene_init(struct ctxt *c) {
return 0;
}
-int scene_draw(struct ctxt *c) {
+int TestApp::render(void) {
mat4 camera;
camera.identity();
@@ -148,3 +154,9 @@ int scene_draw(struct ctxt *c) {
return 0;
}
+int main(int argc, char **argv) {
+ TestApp app;
+ app.setOptions(argc, argv);
+ return app.run();
+}
+
diff --git a/test2d.cc b/test2d.cc
@@ -1,180 +0,0 @@
-/* Copyright 2013 Brian Swetland <swetland@frotz.net>
- *
- * 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.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-
-#include "util.h"
-#include "glue.h"
-#include "matrix.h"
-
-#define USE_VBO 1
-#define CDATA_RGBA 0
-
-void *texdata;
-unsigned texw, texh;
-
-const char *vert_src, *frag_src;
-
-GLuint pgm, vshd, fshd, tex0, tex1;
-GLuint aVertex, aTexCoord;
-GLuint uMVP, uTexture0, uTexture1;
-
-#if CDATA_RGBA
-unsigned cbdata[32*32];
-#else
-unsigned char cbdata[32*32];
-#endif
-
-unsigned cbw = 32;
-unsigned cbh = 32;
-
-struct acell {
- unsigned char x;
- unsigned char y;
- unsigned char z;
- unsigned char w;
- unsigned char u;
- unsigned char v;
- unsigned char i;
- unsigned char j;
-};
-
-struct acell adata[32 * 32 * 6];
-
-void adata_init(struct acell *data, int w, int h) {
- int x, y;
- struct acell *p = data;
- memset(p, 0, sizeof(struct acell) * w * h * 6);
- y = 1;
- for (y = h; y > 0; y--) {
- for (x = 0; x < w; x++) {
- p->x = x+0; p->y = y+0;
- p->u = 0; p->v = 0; p->i = x; p->j = h-y; p++;
- p->x = x+0; p->y = y-1;
- p->u = 0; p->v = 1; p->i = x; p->j = h-y; p++;
- p->x = x+1; p->y = y-1;
- p->u = 1; p->v = 1; p->i = x; p->j = h-y; p++;
- p->x = x+1; p->y = y-1;
- p->u = 1; p->v = 1; p->i = x; p->j = h-y; p++;
- p->x = x+1; p->y = y+0;
- p->u = 1; p->v = 0; p->i = x; p->j = h-y; p++;
- p->x = x+0; p->y = y+0;
- p->u = 0; p->v = 0; p->i = x; p->j = h-y; p++;
- }
- }
-}
-
-int scene_init(struct ctxt *c) {
-
- adata_init(adata, 32, 32);
-
- if (!(texdata = load_png_rgba("font-vincent-8x8.png", &texw, &texh, 1)))
- return -1;
- if (!(vert_src = (const char*) load_file("test2d.vs", 0)))
- return -1;
- if (!(frag_src = (const char*) load_file("test2d.fs", 0)))
- return -1;
-
- glViewport(0, 0, c->width, c->height);
- glClearColor(0, 0, 180, 255);
- glClearDepth(1.0f);
-
- if (shader_compile(vert_src, frag_src, &pgm, &vshd, &fshd))
- return -1;
-
- aVertex = glGetAttribLocation(pgm, "aVertex");
- aTexCoord = glGetAttribLocation(pgm, "aTexCoord");
- uMVP = glGetUniformLocation(pgm, "uMVP");
- uTexture0 = glGetUniformLocation(pgm, "uTexture0");
- uTexture1 = glGetUniformLocation(pgm, "uTexture1");
-
- glGenTextures(1, &tex0);
- glGenTextures(1, &tex1);
-
- glBindTexture(GL_TEXTURE_2D, tex0);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texw, texh, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, texdata);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-
- memset(cbdata, 0, sizeof(cbdata));
- for (int z = 0; z < 256; z++) cbdata[z] = z;
- //sprintf((char*) cbdata, "0123456789 Hello, GL World!");
- //sprintf(((char*) cbdata) + 32, "Line Number Two");
- //sprintf(((char*) cbdata) + 64, "<<-- 33333");
-
- glBindTexture(GL_TEXTURE_2D, tex1);
-#if CDATA_RGBA
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cbw, cbh, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, cbdata);
-#else
- glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, cbw, cbh, 0, GL_ALPHA,
- GL_UNSIGNED_BYTE, cbdata);
-#endif
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-
-#if USE_VBO
- GLuint buf;
- glGenBuffers(1,&buf);
- glBindBuffer(GL_ARRAY_BUFFER, buf);
- glBufferData(GL_ARRAY_BUFFER, sizeof(adata), adata, GL_STATIC_DRAW);
-#endif
- return 0;
-
-}
-
-int scene_draw(struct ctxt *c) {
- mat4 MVP;
-
- MVP.setOrtho(0.0, 32.0, 0.0, 32.0, 1.0, -1.0);
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glUseProgram(pgm);
-
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, tex0);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, tex1);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-
- glUniformMatrix4fv(uMVP, 1, GL_FALSE, MVP);
- glUniform1i(uTexture0, 0);
- glUniform1i(uTexture1, 1);
-
- glEnable(GL_BLEND);
- glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
-#if USE_VBO
- glVertexAttribPointer(aVertex, 4, GL_UNSIGNED_BYTE, GL_FALSE, 8, (void*) 0);
- glVertexAttribPointer(aTexCoord, 4, GL_UNSIGNED_BYTE, GL_FALSE, 8, (void*) 4);
-#else
- glVertexAttribPointer(aVertex, 4, GL_UNSIGNED_BYTE, GL_FALSE, 8, adata);
- glVertexAttribPointer(aTexCoord, 4, GL_UNSIGNED_BYTE, GL_FALSE, 8, ((char*)adata) + 4);
-#endif
- glEnableVertexAttribArray(aVertex);
- glEnableVertexAttribArray(aTexCoord);
-
- glDrawArrays(GL_TRIANGLES, 0, 32 * 32 * 6);
-
- return 0;
-}
-
diff --git a/test3.cc b/test3.cc
@@ -18,30 +18,36 @@
#include <unistd.h>
#include <string.h>
+#include "app.h"
#include "util.h"
-#include "glue.h"
#include "matrix.h"
#include "program.h"
#include <math.h>
-void *texdata;
-unsigned texw, texh;
+class TestApp : public App {
+public:
+ int init(void);
+ int render(void);
+private:
+ void *texdata;
+ unsigned texw, texh;
-GLuint tex0;
-GLuint aVertex, aTexCoord;
-GLuint uMVP, uTexture;
+ GLuint tex0;
+ GLuint aVertex, aTexCoord;
+ GLuint uMVP, uTexture;
-Program pgm;
-mat4 perspective;
-mat4 MVP;
+ Program pgm;
+ mat4 perspective;
+ mat4 MVP;
-float a = 0.0;
+ float a;
-struct model *m;
+ struct model *m;
+};
-int scene_init(struct ctxt *c) {
- float aspect = ((float) c->width) / ((float) c->height);
+int TestApp::init(void) {
+ a = 0.0;
if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh, 1)))
return -1;
@@ -49,9 +55,9 @@ int scene_init(struct ctxt *c) {
if (!(m = load_wavefront_obj("cube.obj")))
return -1;
- perspective.setPerspective(D2R(60.0), aspect, 1.0, 10.0);
+ perspective.setPerspective(D2R(60.0), aspect(), 1.0, 10.0);
- glViewport(0, 0, c->width, c->height);
+ glViewport(0, 0, width(), height());
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
@@ -79,7 +85,7 @@ int scene_init(struct ctxt *c) {
return 0;
}
-int scene_draw(struct ctxt *c) {
+int TestApp::render(void) {
mat4 camera;
camera.identity();
@@ -112,3 +118,9 @@ int scene_draw(struct ctxt *c) {
return 0;
}
+int main(int argc, char **argv) {
+ TestApp app;
+ app.setOptions(argc, argv);
+ return app.run();
+}
+
diff --git a/test4.cc b/test4.cc
@@ -17,22 +17,28 @@
#include <stdlib.h>
#include <unistd.h>
+#include "app.h"
#include "util.h"
-#include "glue.h"
#include "matrix.h"
#include "program.h"
-void *texdata;
-unsigned texw, texh;
+class TestApp : public App {
+public:
+ int init(void);
+ int render(void);
+private:
+ void *texdata;
+ unsigned texw, texh;
-GLuint tex0;
-GLuint aVertex, aTexCoord;
-GLuint uMVP, uTexture;
+ GLuint tex0;
+ GLuint aVertex, aTexCoord;
+ GLuint uMVP, uTexture;
-Program pgm;
-mat4 MVP;
+ Program pgm;
+ mat4 MVP;
+};
-GLfloat verts[] = {
+static GLfloat verts[] = {
-1, -1, 0,
-1, 1, 0,
1, -1, 0,
@@ -43,16 +49,14 @@ GLfloat verts[] = {
0.5f, 0.5f, 0.0f,
};
-GLfloat texcoords[] = {
+static GLfloat texcoords[] = {
0.0, 0.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0,
};
-int scene_init(struct ctxt *c) {
- float aspect;
-
+int TestApp::init(void) {
if (!(texdata = load_png_gray("texture.sdf.png", &texw, &texh, 1)))
return -1;
@@ -64,10 +68,9 @@ int scene_init(struct ctxt *c) {
uMVP = pgm.getUniformID("uMVP");
uTexture = pgm.getUniformID("uTexture");
- aspect = ((float)c->width) / ((float)c->height);
- MVP.setOrtho(-aspect, aspect, -1, 1, 1, -1);
+ MVP.setOrtho(-aspect(), aspect(), -1, 1, 1, -1);
- glViewport(0, 0, c->width, c->height);
+ glViewport(0, 0, width(), height());
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
@@ -83,7 +86,7 @@ int scene_init(struct ctxt *c) {
return 0;
}
-int scene_draw(struct ctxt *c) {
+int TestApp::render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
pgm.use();
@@ -105,3 +108,9 @@ int scene_draw(struct ctxt *c) {
return 0;
}
+int main(int argc, char **argv) {
+ TestApp app;
+ app.setOptions(argc, argv);
+ return app.run();
+}
+
diff --git a/test5.cc b/test5.cc
@@ -20,48 +20,56 @@
#include <unistd.h>
#endif
+#include "app.h"
#include "util.h"
#include "matrix.h"
-#include "glue.h"
#include "program.h"
#include "debugtext.h"
#include <math.h>
-void *texdata;
-unsigned texw, texh;
-
-GLuint tex0;
-GLuint aVertex, aNormal, aTexCoord;
-GLuint uMV, uMVP, uLight, uTexture;
+#include <SDL_keysym.h>
-Program pgm;
-DebugText debugtext;
+class TestApp : public App {
+public:
+ TestApp();
+ int init(void);
+ int render(void);
+private:
+ void *texdata;
+ unsigned texw, texh;
-float camx = 0, camy = 0, camz = -5;
-float camrx = 0, camry = 0, camrz = 0;
+ GLuint tex0;
+ GLuint aVertex, aNormal, aTexCoord;
+ GLuint uMV, uMVP, uLight, uTexture;
-extern unsigned char keystate[];
-#include <SDL_keysym.h>
+ Program pgm;
+ DebugText debugtext;
-mat4 Projection;
+ float camx, camy, camz;
+ float camrx, camry, camrz;
-float a = 0.0;
+ mat4 Projection;
-struct model *m;
+ float a;
-extern int fps;
+ struct model *m;
+};
-int scene_init(struct ctxt *c) {
- float aspect = ((float) c->width) / ((float) c->height);
+TestApp::TestApp() : App(),
+ camx(0), camy(0), camz(-5),
+ camrx(0), camry(0), camrz(0),
+ a(0) {
+}
+int TestApp::init(void) {
if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh, 1)))
return -1;
if (!(m = load_wavefront_obj("cube.obj")))
return -1;
- glViewport(0, 0, c->width, c->height);
+ glViewport(0, 0, width(), height());
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
@@ -84,13 +92,13 @@ int scene_init(struct ctxt *c) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- Projection.setPerspective(D2R(60.0), aspect, 1.0, 100.0);
+ Projection.setPerspective(D2R(60.0), aspect(), 1.0, 100.0);
debugtext.init(32,32);
return 0;
}
-int scene_draw(struct ctxt *c) {
+int TestApp::render(void) {
mat4 MVP;
mat4 MV;
mat4 Model;
@@ -102,18 +110,18 @@ int scene_draw(struct ctxt *c) {
float vz2 = cosf(D2R(camry + 90.0));
float vx2 = -sinf(D2R(camry + 90.0));
- if (keystate[SDLK_w]) { camx += vx * 0.1; camz += vz * 0.1; }
- if (keystate[SDLK_s]) { camx -= vx * 0.1; camz -= vz * 0.1; }
- if (keystate[SDLK_a]) { camx += vx2 * 0.1; camz += vz2 * 0.1; }
- if (keystate[SDLK_d]) { camx -= vx2 * 0.1; camz -= vz2 * 0.1; }
+ if (keydown(SDLK_w)) { camx += vx * 0.1; camz += vz * 0.1; }
+ if (keydown(SDLK_s)) { camx -= vx * 0.1; camz -= vz * 0.1; }
+ if (keydown(SDLK_a)) { camx += vx2 * 0.1; camz += vz2 * 0.1; }
+ if (keydown(SDLK_d)) { camx -= vx2 * 0.1; camz -= vz2 * 0.1; }
- if (keystate[SDLK_q]) camry += 3.0;
- if (keystate[SDLK_e]) camry -= 3.0;
+ if (keydown(SDLK_q)) camry += 3.0;
+ if (keydown(SDLK_e)) camry -= 3.0;
- if (keystate[SDLK_r]) camrx -= 1.0;
- if (keystate[SDLK_f]) camrx += 1.0;
+ if (keydown(SDLK_r)) camrx -= 1.0;
+ if (keydown(SDLK_f)) camrx += 1.0;
- if (keystate[SDLK_x]) { camrx = 0; camrz = 0; }
+ if (keydown(SDLK_x)) { camrx = 0; camrz = 0; }
if (camrx < -45.0) camrx = -45.0;
if (camrx > 45.0) camrx = 45.0;
@@ -182,9 +190,15 @@ int scene_draw(struct ctxt *c) {
debugtext.clear();
debugtext.printf("Hello, Test #5\n");
debugtext.printf("Cam @ %6.3f %6.3f\n", camx, camz);
- debugtext.printf("\n%d fps\n", fps);
+ debugtext.printf("\n%d fps\n", fps());
debugtext.render();
return 0;
}
+int main(int argc, char **argv) {
+ TestApp app;
+ app.setOptions(argc, argv);
+ return app.run();
+}
+