vrshoot

view src/scr_debug.cc @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
1 #include <assert.h>
2 #include "scr_debug.h"
3 #include "opengl.h"
4 #include "unistate.h"
5 #include "sdrman.h"
6 #include "game.h"
7 #include "drawtext/drawtext.h"
8 #include "datapath.h"
9 #include "logger.h"
10 #include "timer.h"
11 #include "scr_game.h"
12 #include "gfxutil.h"
14 static ShaderProg *sdr, *color_sdr;
15 static struct dtx_font *font;
17 const char *DebugScreen::get_name() const
18 {
19 return "DebugScreen";
20 }
22 #define FONT_FILENAME "consolas.ttf"
23 #define FONT_SIZE 20
25 bool DebugScreen::init()
26 {
27 if(!(sdr = get_sdrprog("font.v.glsl", "font.p.glsl"))) {
28 return false;
29 }
30 int vattr = sdr->get_attrib_location("attr_vertex");
31 int tcattr = sdr->get_attrib_location("attr_texcoord");
32 assert(vattr != -1 && tcattr != -1);
34 dtx_vertex_attribs(vattr, tcattr);
36 if(!(font = dtx_open_font(datafile_path(FONT_FILENAME).c_str(), FONT_SIZE))) {
37 return false;
38 }
40 if(!(color_sdr = get_sdrprog("color.v.glsl", "color.p.glsl"))) {
41 return false;
42 }
43 return true;
44 }
46 void DebugScreen::cleanup()
47 {
48 delete sdr;
49 dtx_close_font(font);
50 }
52 void DebugScreen::display() const
53 {
54 OverlayScreen::display(); // this will draw the previous screen
56 static unsigned long frame;
57 static unsigned long prev_tm;
58 unsigned long tm = get_time_msec();
59 static char fps_text[64];
61 frame++;
62 unsigned long interval = tm - prev_tm;
63 if(interval >= 1000) {
64 float fps = (float)frame / ((float)interval / 1000.0f);
65 sprintf(fps_text, "fps: %.1f", fps);
67 frame = 0;
68 prev_tm = tm;
69 }
71 Matrix4x4 mat;
72 mat.set_translation(Vector3(5, get_screen_height() - 20, 0));
73 set_world_matrix(mat);
75 set_unistate("st_color", Vector4(1, 1, 0, 1));
77 sdr->bind();
79 dtx_use_font(font, FONT_SIZE);
80 dtx_string(fps_text);
81 dtx_flush();
82 }