citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 4629f770f576bd8f39ff8980808b14f4d55d8eb6
parent 8a59b6d7001d229e7e037085211d6cf6492ed082
Author: Kyle Milz <milz@black.my.domain>
Date:   Mon, 15 Apr 2019 16:55:54 -0700

bin: merge gltest.cc with gl.cc

Diffstat:
Mbin/Jamfile | 14+++++---------
Mbin/gl.cc | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Dbin/gltest.cc | 118-------------------------------------------------------------------------------
3 files changed, 118 insertions(+), 129 deletions(-)

diff --git a/bin/Jamfile b/bin/Jamfile @@ -52,9 +52,10 @@ LINKLIBS on citrun_inst += Main citrun_inst : $(INST_SRCS) ; # -# citrun_gl, citrun_gltest +# citrun_gl # GL_SRCS = + gl.cc gl_atlas.cc gl_buffer.cc gl_font.cc @@ -65,21 +66,16 @@ GL_SRCS = gl_view.cc matrix4x4.c ; -Library gl_common : $(GL_SRCS) ; - Stringize gl_atlas_glsl.h : gl_atlas.glsl ; Stringize gl_vshader_glsl.h : gl_vshader.glsl ; Stringize gl_fshader_glsl.h : gl_fshader.glsl ; -ObjectC++Flags gl.cc gltest.cc $(GL_SRCS) : `pkg-config --cflags $(gl_pkgs)` ; +ObjectC++Flags $(GL_SRCS) : `pkg-config --cflags $(gl_pkgs)` ; +LINKFLAGS on citrun_gl = $(LINKFLAGS) ; LINKLIBS on citrun_gl += -lm $(GL_EXTRALIB) `pkg-config --libs $(gl_pkgs)` ; -LINKLIBS on citrun_gltest += -lm `pkg-config --libs $(gl_pkgs)` ; - -LinkLibraries citrun_gl citrun_gltest : gl_common ; -Main citrun_gl : gl.cc ; -Main citrun_gltest : gltest.cc ; +Main citrun_gl : $(GL_SRCS) ; # # install diff --git a/bin/gl.cc b/bin/gl.cc @@ -1,8 +1,14 @@ -#include <GL/glew.h> +#include <GL/glew.h> // glewInit, glewIsSupported #include <GLFW/glfw3.h> +#define GLAPI extern +#include <GL/osmesa.h> // OSMesa{Context,CreateContext,MakeCurrent} + #include <err.h> +#include <stdio.h> // fclose, fopen, fputc +#include <stdlib.h> +#include <string.h> -#include "gl_main.h" +#include "gl_main.h" // citrun::gl_main void @@ -17,9 +23,114 @@ error_callback(int error, const char *desc) fprintf(stderr, "Error: %s\n", desc); } +// +// osdemo.c originally from Brian Paul, public domain. +// +static void +write_targa(const char *filename, const GLubyte *buffer, int width, int height) +{ + FILE *f = fopen( filename, "w" ); + if (f) { + int i, x, y; + const GLubyte *ptr = buffer; + printf ("osdemo, writing tga file \n"); + fputc (0x00, f); /* ID Length, 0 => No ID */ + fputc (0x00, f); /* Color Map Type, 0 => No color map included */ + fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */ + fputc (0x00, f); /* Next five bytes are about the color map entries */ + fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */ + fputc (0x00, f); + fputc (0x00, f); + fputc (0x00, f); + fputc (0x00, f); /* X-origin of Image */ + fputc (0x00, f); + fputc (0x00, f); /* Y-origin of Image */ + fputc (0x00, f); + fputc (width & 0xff, f); /* Image Width */ + fputc ((width>>8) & 0xff, f); + fputc (height & 0xff, f); /* Image Height */ + fputc ((height>>8) & 0xff, f); + fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */ + fputc (0x20, f); /* Image Descriptor */ + fclose(f); + f = fopen( filename, "ab" ); /* reopen in binary append mode */ + for (y=height-1; y>=0; y--) { + for (x=0; x<width; x++) { + i = (y*width + x) * 4; + fputc(ptr[i+2], f); /* write blue */ + fputc(ptr[i+1], f); /* write green */ + fputc(ptr[i], f); /* write red */ + } + } + } +} + +int +altmain(int argc, char *argv[]) +{ + OSMesaContext ctx; + void *buffer; + char *filename = NULL; + int width = 400; + int height = 400; + + filename = argv[1]; + if (filename == NULL) + errx(0,"Specify a filename if you want to make an image file"); + + if (argc == 4) { + width = atoi(argv[2]); + height = atoi(argv[3]); + } + + // Create an RGBA-mode context +#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 + // specify Z, stencil, accum sizes + ctx = OSMesaCreateContextExt(OSMESA_RGBA, 16, 0, 0, NULL); +#else + ctx = OSMesaCreateContext(OSMESA_RGBA, NULL); +#endif + if (!ctx) + errx(1, "OSMesaCreateContext failed!"); + + // Allocate the image buffer + if ((buffer = malloc(width * height * 4 * sizeof(GLubyte))) == NULL) + errx(1, "Alloc image buffer failed!"); + + // Bind the buffer to the context and make it current + if (!OSMesaMakeCurrent(ctx, buffer, GL_UNSIGNED_BYTE, width, height)) + errx(1, "OSMesaMakeCurrent failed!"); + + int z, s, a; + glGetIntegerv(GL_DEPTH_BITS, &z); + glGetIntegerv(GL_STENCIL_BITS, &s); + glGetIntegerv(GL_ACCUM_RED_BITS, &a); + printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a); + + GLenum glew_status = glewInit(); + if (GLEW_OK != glew_status) + errx(1, "glewInit %s", glewGetErrorString(glew_status)); + if (!glewIsSupported("GL_VERSION_2_0")) + errx(1, "No support for OpenGL 2.0 found"); + + citrun::gl_main main; + main.tick(); + + write_targa(filename, static_cast<const GLubyte *>(buffer), width, height); + printf("all done\n"); + + free(buffer); + OSMesaDestroyContext( ctx ); + + return 0; +} + int main(int argc, char *argv[]) { + if (argc > 1) + return altmain(argc, argv); + GLFWwindow *window; glfwSetErrorCallback(error_callback); diff --git a/bin/gltest.cc b/bin/gltest.cc @@ -1,118 +0,0 @@ -// -// osdemo.c originally from Brian Paul, public domain. -// -#include <GL/glew.h> // glewInit, glewIsSupported -#define GLAPI extern -#include <GL/osmesa.h> // OSMesa{Context,CreateContext,MakeCurrent} -#include <err.h> -#include <stdio.h> // fclose, fopen, fputc -#include <stdlib.h> -#include <string.h> - -#include "gl_main.h" // citrun::gl_main - - -static void -write_targa(const char *filename, const GLubyte *buffer, int width, int height) -{ - FILE *f = fopen( filename, "w" ); - if (f) { - int i, x, y; - const GLubyte *ptr = buffer; - printf ("osdemo, writing tga file \n"); - fputc (0x00, f); /* ID Length, 0 => No ID */ - fputc (0x00, f); /* Color Map Type, 0 => No color map included */ - fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */ - fputc (0x00, f); /* Next five bytes are about the color map entries */ - fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */ - fputc (0x00, f); - fputc (0x00, f); - fputc (0x00, f); - fputc (0x00, f); /* X-origin of Image */ - fputc (0x00, f); - fputc (0x00, f); /* Y-origin of Image */ - fputc (0x00, f); - fputc (width & 0xff, f); /* Image Width */ - fputc ((width>>8) & 0xff, f); - fputc (height & 0xff, f); /* Image Height */ - fputc ((height>>8) & 0xff, f); - fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */ - fputc (0x20, f); /* Image Descriptor */ - fclose(f); - f = fopen( filename, "ab" ); /* reopen in binary append mode */ - for (y=height-1; y>=0; y--) { - for (x=0; x<width; x++) { - i = (y*width + x) * 4; - fputc(ptr[i+2], f); /* write blue */ - fputc(ptr[i+1], f); /* write green */ - fputc(ptr[i], f); /* write red */ - } - } - } -} - -int -main(int argc, char *argv[]) -{ - OSMesaContext ctx; - void *buffer; - char *filename = NULL; - int width = 400; - int height = 400; - - if (argc < 2) { - fprintf(stderr, "Usage:\n"); - fprintf(stderr, " %s filename [width height]\n", argv[0]); - return 0; - } - - filename = argv[1]; - if (filename == NULL) - errx(0,"Specify a filename if you want to make an image file"); - - if (argc == 4) { - width = atoi(argv[2]); - height = atoi(argv[3]); - } - - // Create an RGBA-mode context -#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 - // specify Z, stencil, accum sizes - ctx = OSMesaCreateContextExt(OSMESA_RGBA, 16, 0, 0, NULL); -#else - ctx = OSMesaCreateContext(OSMESA_RGBA, NULL); -#endif - if (!ctx) - errx(1, "OSMesaCreateContext failed!"); - - // Allocate the image buffer - if ((buffer = malloc(width * height * 4 * sizeof(GLubyte))) == NULL) - errx(1, "Alloc image buffer failed!"); - - // Bind the buffer to the context and make it current - if (!OSMesaMakeCurrent(ctx, buffer, GL_UNSIGNED_BYTE, width, height)) - errx(1, "OSMesaMakeCurrent failed!"); - - int z, s, a; - glGetIntegerv(GL_DEPTH_BITS, &z); - glGetIntegerv(GL_STENCIL_BITS, &s); - glGetIntegerv(GL_ACCUM_RED_BITS, &a); - printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a); - - GLenum glew_status = glewInit(); - if (GLEW_OK != glew_status) - errx(1, "glewInit %s", glewGetErrorString(glew_status)); - if (!glewIsSupported("GL_VERSION_2_0")) - errx(1, "No support for OpenGL 2.0 found"); - - citrun::gl_main main; - main.tick(); - - write_targa(filename, static_cast<const GLubyte *>(buffer), width, height); - printf("all done\n"); - - free(buffer); - OSMesaDestroyContext( ctx ); - - return 0; -}