citrun

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

commit 5183ac010ea199601baffdb97240074432ab98b0
parent f96e49e2130be581eb96881f971f797f81943309
Author: Kyle Milz <kyle@0x30.net>
Date:   Sun, 11 Dec 2016 12:33:51 -0700

src: initial glut to glfw conversion

Diffstat:
MJamrules | 13++++++-------
Msrc/Jamfile | 22++++++++++++----------
Msrc/demo-common.h | 2--
Msrc/gl_main.cc | 104+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Msrc/gl_view.cc | 27+++++++++++++++++++--------
5 files changed, 125 insertions(+), 43 deletions(-)

diff --git a/Jamrules b/Jamrules @@ -40,21 +40,20 @@ CLANG_LIBS = if $(OS) = OPENBSD { C++ = eg++ ; - PKG_CONFIG_LIBS = gl glew freetype2 ; - LINKLIBS on citrun-gl = -lm -lglut ; + PKG_CONFIG_LIBS = gl glfw3 glew freetype2 ; LINKLIBS on citrun-inst += -Wl,--start-group $(CLANG_LIBS) -Wl,--end-group ; } if $(OS) = MACOSX { - PKG_CONFIG_LIBS = glew freetype2 ; - LINKLIBS on citrun-gl = -framework OpenGL -framework GLUT -lm ; + PKG_CONFIG_LIBS = glfw3 glew freetype2 ; + LINKLIBS on citrun-gl = -framework OpenGL ; LINKLIBS on citrun-inst = $(CLANG_LIBS) ; } if $(OS) = LINUX { # llvm version is 3.5 on Debian, which is too old. - PKG_CONFIG_LIBS = gl glew freetype2 ; - LINKLIBS on citrun-gl = -lbsd -lm -lglut ; + PKG_CONFIG_LIBS = gl glfw3 glew freetype2 ; + LINKLIBS on citrun-gl = -lbsd ; LINKLIBS on citrun-inst = -lbsd ; LINKLIBS on citrun-inst += -Wl,--start-group $(CLANG_LIBS) -Wl,--end-group ; } @@ -92,7 +91,7 @@ if $(CITRUN_SA) { # Link with the c++ compiler so that the matching c++ runtime library gets added # automatically. -LINK on citrun-inst citrun-term citrun-gl = $(C++) ; +LINK on citrun-inst citrun-term citrun-gl citrun-gltest = $(C++) ; rule Stringize { diff --git a/src/Jamfile b/src/Jamfile @@ -5,9 +5,7 @@ SubDir TOP src ; # ObjectCcFlags rt.c : -fPIC -ansi ; Library libcitrun : rt.c ; - Library utils : process_file.cc process_dir.cc ; -LinkLibraries citrun-term citrun-gl : utils ; # # citrun-wrap, citrun-check @@ -23,13 +21,13 @@ ReplaceTokens citrun-wrap ; # LINKLIBS on citrun-term += -lcurses ; # XXX: Not the main focus right now. +# LinkLibraries citrun-term : utils ; # Main citrun-term : term_main.cc ; # -# citrun-gl +# citrun-gl & citrun-gltest # GL_SRCS = - gl_main.cc gl_view.cc demo-atlas.cc gl_buffer.cc @@ -39,18 +37,22 @@ GL_SRCS = matrix4x4.c trackball.c ; +Library gl_common : $(GL_SRCS) ; + Stringize demo_atlas_glsl.h : demo_atlas.glsl ; Stringize demo_vshader_glsl.h : demo_vshader.glsl ; Stringize demo_fshader_glsl.h : demo_fshader.glsl ; -ObjectC++Flags $(GL_SRCS) : `pkg-config $(PKG_CONFIG_LIBS) --cflags` ; -# Bug in openbsd pkg-config, fixed in -current. -ObjectC++Flags $(GL_SRCS) : -I/usr/local/include ; +ObjectC++Flags gl_main.cc gl_testmain.cc $(GL_SRCS) : + `pkg-config $(PKG_CONFIG_LIBS) --cflags` ; + +LINKLIBS on citrun-gl citrun-gltest += -lm `pkg-config $(PKG_CONFIG_LIBS) --libs` ; +LinkLibraries citrun-gl citrun-gltest : gl_common libglyphy utils ; -LINKLIBS on citrun-gl += `pkg-config $(PKG_CONFIG_LIBS) --libs` ; -LinkLibraries citrun-gl : libglyphy ; +LINKLIBS on citrun-gltest += -lOSMesa ; -Main citrun-gl : $(GL_SRCS) ; +Main citrun-gl : gl_main.cc ; +Main citrun-gltest : gl_testmain.cc ; # # citrun-inst diff --git a/src/demo-common.h b/src/demo-common.h @@ -33,10 +33,8 @@ #if defined(__APPLE__) # include <OpenGL/OpenGL.h> -# include <GLUT/glut.h> #else # include <GLES2/gl2.h> -# include <GL/glut.h> #endif diff --git a/src/gl_main.cc b/src/gl_main.cc @@ -10,6 +10,8 @@ #include "process_dir.h" #include "process_file.h" +#include <GLFW/glfw3.h> + #if defined(__OpenBSD__) #define FONT_PATH "/usr/X11R6/lib/X11/fonts/TTF/DejaVuSansMono.ttf" #elif defined(__APPLE__) @@ -71,7 +73,8 @@ display(void) static long current_time(void) { - return glutGet(GLUT_ELAPSED_TIME); + //return glutGet(GLUT_ELAPSED_TIME); + return glfwGetTime(); } void @@ -128,18 +131,7 @@ next_frame(View *vu) std::cout << "tick" << std::endl; } - glutPostRedisplay (); -} - -void -idle_step(void) -{ - View *vu = static_vu; - if (vu->animate) { - next_frame (vu); - } - else - glutIdleFunc(NULL); + // glutPostRedisplay (); } void @@ -147,7 +139,7 @@ print_fps(int ms) { View *vu = static_vu; if (vu->animate) { - glutTimerFunc (ms, print_fps, ms); + // glutTimerFunc (ms, print_fps, ms); long t = current_time (); LOGI ("%gfps\n", vu->num_frames * 1000. / (t - vu->fps_start_time)); vu->num_frames = 0; @@ -162,13 +154,14 @@ start_animation() View *vu = static_vu; vu->num_frames = 0; vu->last_frame_time = vu->fps_start_time = current_time(); - glutIdleFunc(idle_step); + // glutIdleFunc(idle_step); if (!vu->has_fps_timer) { vu->has_fps_timer = true; - glutTimerFunc (5000, print_fps, 5000); + // glutTimerFunc (5000, print_fps, 5000); } } +#if 0 int main(int argc, char *argv[]) { @@ -213,3 +206,82 @@ main(int argc, char *argv[]) return 0; } +#endif + +static void +error_callback(int error, const char *desc) +{ + fprintf(stderr, "Error: %s\n", desc); +} + +int +main(int argc, char *argv[]) +{ + GLFWwindow *window; + + glfwSetErrorCallback(error_callback); + + if (!glfwInit()) + return 1; + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + + window = glfwCreateWindow(1600, 1200, "C It Run", NULL, NULL); + if (window == NULL) { + glfwTerminate(); + return 1; + } + + // glfwSetKeyCallback(window, key_callback); + + // glutReshapeFunc(reshape_func); + // glutDisplayFunc(display); + // glutKeyboardFunc(keyboard_func); + // glutSpecialFunc(special_func); + // glutMouseFunc(mouse_func); + // glutMotionFunc(motion_func); + + glfwMakeContextCurrent(window); + glfwSwapInterval(1); + + GLenum glew_status = glewInit(); + if (GLEW_OK != glew_status) + errx(1, "%s", glewGetErrorString(glew_status)); + if (!glewIsSupported("GL_VERSION_2_0")) + errx(1, "No support for OpenGL 2.0 found"); + + st = demo_glstate_create(); + buffer = demo_buffer_create(); + + static_vu = new View(st, buffer); + + FT_Init_FreeType(&ft_library); + + ft_face = NULL; + FT_New_Face(ft_library, FONT_PATH, /* face_index */ 0, &ft_face); + + font = demo_font_create(ft_face, demo_glstate_get_atlas(st)); + + static_vu->setup(); + + static_vu->toggle_animation(); + + glyphy_point_t top_left = { 0, 0 }; + demo_buffer_move_to(buffer, &top_left); + demo_buffer_add_text(buffer, "waiting...", font, 1); + + while (!glfwWindowShouldClose(window)) { + + next_frame(static_vu); + static_vu->display(); + + glfwSwapBuffers(window); + glfwPollEvents(); + } + + glfwDestroyWindow(window); + glfwTerminate(); + + return 0; +} diff --git a/src/gl_view.cc b/src/gl_view.cc @@ -17,6 +17,7 @@ */ #include "gl_view.h" +#include <GLFW/glfw3.h> extern "C" { #include "trackball.h" @@ -143,15 +144,17 @@ View::apply_transform(float *mat) static long current_time (void) { - return glutGet(GLUT_ELAPSED_TIME); + return glfwGetTime(); } void View::toggle_animation() { +#if 0 animate = !animate; if (animate) start_animation(); +#endif } void @@ -197,6 +200,7 @@ View::toggle_srgb() void View::toggle_fullscreen() { +#if 0 fullscreen = !fullscreen; if (fullscreen) { x = glutGet(GLUT_WINDOW_X); @@ -208,6 +212,7 @@ View::toggle_fullscreen() glutReshapeWindow(width, height); glutPositionWindow(x, y); } +#endif } void @@ -221,7 +226,7 @@ void View::reshape_func(int width, int height) { glViewport (0, 0, width, height); - glutPostRedisplay (); + // glutPostRedisplay (); } #define STEP 1.05 @@ -311,12 +316,13 @@ View::keyboard_func(unsigned char key, int x, int y) default: return; } - glutPostRedisplay (); + // glutPostRedisplay (); } void View::special_func(int key, int x, int y) { +#if 0 switch (key) { case GLUT_KEY_UP: @@ -335,18 +341,20 @@ View::special_func(int key, int x, int y) default: return; } - glutPostRedisplay (); +#endif + // glutPostRedisplay (); } void View::mouse_func(int button, int state, int x, int y) { +#if 0 if (state == GLUT_DOWN) { buttons |= (1 << button); click_handled = false; } else buttons &= !(1 << button); - modifiers = glutGetModifiers (); + // modifiers = glutGetModifiers (); switch (button) { case GLUT_RIGHT_BUTTON: @@ -385,12 +393,13 @@ View::mouse_func(int button, int state, int x, int y) scale(1. / STEP); break; } +#endif beginx = lastx = x; beginy = lasty = y; dragged = false; - glutPostRedisplay (); + // glutPostRedisplay (); } void @@ -403,6 +412,7 @@ View::motion_func(int x, int y) GLuint width = viewport[2]; GLuint height = viewport[3]; +#if 0 if (buttons & (1 << GLUT_LEFT_BUTTON)) { if (modifiers & GLUT_ACTIVE_SHIFT) { @@ -455,12 +465,13 @@ View::motion_func(int x, int y) +(2. * beginx / width - 1) * (1 - factor), -(2. * beginy / height - 1) * (1 - factor)); } +#endif lastx = x; lasty = y; lastt = current_time (); - glutPostRedisplay (); + // glutPostRedisplay (); } void @@ -511,7 +522,7 @@ View::display() demo_buffer_draw (buffer); - glutSwapBuffers (); + // glutSwapBuffers (); } void