fixed incorrect checking of the existence of GLX_EXT_swap_control and friends
[demo_prior] / src / demo.c
index 97da536..a27d86c 100644 (file)
@@ -5,6 +5,9 @@
 #include "post.h"
 #include "sdr.h"
 #include "opt.h"
+#include "drawtext.h"
+#include "imtk.h"
+#include "osd.h"
 
 void reg_whitted(void);
 
@@ -12,8 +15,12 @@ int win_width, win_height;
 float win_aspect;
 long time_msec;
 
+struct dtx_font *fnt_ui;
+int fnt_ui_size;
+
+int dbgui;
+
 static int reshape_pending;
-static unsigned int fbtex;
 static unsigned int sdr_gamma;
 
 int demo_init(void)
@@ -26,18 +33,29 @@ int demo_init(void)
 
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
-       glEnable(GL_FRAMEBUFFER_SRGB);
-       glEnable(GL_MULTISAMPLE);
+       if(opt.srgb) {
+               glEnable(GL_FRAMEBUFFER_SRGB);
+       }
+       if(opt.msaa) {
+               glEnable(GL_MULTISAMPLE);
+       }
+
+       if(opt.vsync) {
+               gl_swap_interval(1);
+       } else {
+               gl_swap_interval(0);
+       }
 
-       glGenTextures(1, &fbtex);
-       glBindTexture(GL_TEXTURE_2D, fbtex);
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-       if(win_width) {
-               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, win_width, win_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+       if(!(fnt_ui = dtx_open_font_glyphmap("data/ui.glyphmap"))) {
+               fprintf(stderr, "failed to open ui font\n");
+               return -1;
        }
+       fnt_ui_size = dtx_get_glyphmap_ptsize(dtx_get_glyphmap(fnt_ui, 0));
+       dtx_prepare_range(fnt_ui, fnt_ui_size, 32, 127);
 
-       if(!(sdr_gamma = create_program_load("sdr/gamma.v.glsl", "sdr/gamma.p.glsl"))) {
+       post_init();
+
+       if(!(sdr_gamma = create_program_load("sdr/post.v.glsl", "sdr/gamma.p.glsl"))) {
                fprintf(stderr, "Warning: failed to load the gamma correction shader\n");
        }
 
@@ -61,13 +79,13 @@ void demo_cleanup(void)
        if(sdr_gamma) {
                glDeleteProgram(sdr_gamma);
        }
-       if(fbtex) {
-               glDeleteTextures(1, &fbtex);
-       }
+       post_cleanup();
 
        for(i=0; i<num_parts; i++) {
                parts[i]->destroy();
        }
+
+       dtx_close_font(fnt_ui);
 }
 
 void demo_display(void)
@@ -75,10 +93,8 @@ void demo_display(void)
        long part_time;
 
        if(reshape_pending) {
-               /* reshape fbtex */
-               glBindTexture(GL_TEXTURE_2D, fbtex);
-               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, win_width, win_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
-               reshape_pending = 1;
+               post_reshape(win_width, win_height);
+               reshape_pending = 0;
        }
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -110,12 +126,30 @@ void demo_display(void)
 
        /* no-srgb gamma correction fallback */
        if(!opt.srgb) {
-               glBindTexture(GL_TEXTURE_2D, fbtex);
+               glBindTexture(GL_TEXTURE_2D, post_fbtex[0].id);
                glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, win_width, win_height);
 
                glUseProgram(sdr_gamma);
-               overlay(fbtex, win_aspect, 1.0);
+               overlay_tex(post_fbtex, 1.0);
+       }
+
+       {
+               static long frames, prev_upd, fps;
+               long dt;
+
+               frames++;
+
+               dt = time_msec - prev_upd;
+               if(dt >= 750) {
+                       fps = ((frames * 1000 << 8) + 128) / dt;
+                       frames = 0;
+                       prev_upd = time_msec;
+               }
+
+               print_text(win_width - 80, 20, 1.0, 0.8, 0.1, "fps: %ld.%ld", fps >> 8, ((fps & 0xff) * 10) >> 8);
        }
+
+       draw_osd();
 }
 
 void demo_reshape(int x, int y)
@@ -131,13 +165,22 @@ void demo_reshape(int x, int y)
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(50.0, win_aspect, 0.5, 500.0);
+
+       imtk_set_viewport(x, y);
 }
 
 void demo_keyboard(int key, int st)
 {
-       if(st && key == 27) {
-               demo_quit();
-               return;
+       if(st) {
+               switch(key) {
+               case 27:
+                       demo_quit();
+                       return;
+
+               case '`':
+                       dbgui ^= 1;
+                       break;
+               }
        }
 
        if(cur_part && cur_part->keyboard) {