vrshoot

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/scr_debug.cc	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,82 @@
     1.4 +#include <assert.h>
     1.5 +#include "scr_debug.h"
     1.6 +#include "opengl.h"
     1.7 +#include "unistate.h"
     1.8 +#include "sdrman.h"
     1.9 +#include "game.h"
    1.10 +#include "drawtext/drawtext.h"
    1.11 +#include "datapath.h"
    1.12 +#include "logger.h"
    1.13 +#include "timer.h"
    1.14 +#include "scr_game.h"
    1.15 +#include "gfxutil.h"
    1.16 +
    1.17 +static ShaderProg *sdr, *color_sdr;
    1.18 +static struct dtx_font *font;
    1.19 +
    1.20 +const char *DebugScreen::get_name() const
    1.21 +{
    1.22 +	return "DebugScreen";
    1.23 +}
    1.24 +
    1.25 +#define FONT_FILENAME	"consolas.ttf"
    1.26 +#define FONT_SIZE		20
    1.27 +
    1.28 +bool DebugScreen::init()
    1.29 +{
    1.30 +	if(!(sdr = get_sdrprog("font.v.glsl", "font.p.glsl"))) {
    1.31 +		return false;
    1.32 +	}
    1.33 +	int vattr = sdr->get_attrib_location("attr_vertex");
    1.34 +	int tcattr = sdr->get_attrib_location("attr_texcoord");
    1.35 +	assert(vattr != -1 && tcattr != -1);
    1.36 +
    1.37 +	dtx_vertex_attribs(vattr, tcattr);
    1.38 +
    1.39 +	if(!(font = dtx_open_font(datafile_path(FONT_FILENAME).c_str(), FONT_SIZE))) {
    1.40 +		return false;
    1.41 +	}
    1.42 +
    1.43 +	if(!(color_sdr = get_sdrprog("color.v.glsl", "color.p.glsl"))) {
    1.44 +		return false;
    1.45 +	}
    1.46 +	return true;
    1.47 +}
    1.48 +
    1.49 +void DebugScreen::cleanup()
    1.50 +{
    1.51 +	delete sdr;
    1.52 +	dtx_close_font(font);
    1.53 +}
    1.54 +
    1.55 +void DebugScreen::display() const
    1.56 +{
    1.57 +	OverlayScreen::display();	// this will draw the previous screen
    1.58 +
    1.59 +	static unsigned long frame;
    1.60 +	static unsigned long prev_tm;
    1.61 +	unsigned long tm = get_time_msec();
    1.62 +	static char fps_text[64];
    1.63 +
    1.64 +	frame++;
    1.65 +	unsigned long interval = tm - prev_tm;
    1.66 +	if(interval >= 1000) {
    1.67 +		float fps = (float)frame / ((float)interval / 1000.0f);
    1.68 +		sprintf(fps_text, "fps: %.1f", fps);
    1.69 +
    1.70 +		frame = 0;
    1.71 +		prev_tm = tm;
    1.72 +	}
    1.73 +
    1.74 +	Matrix4x4 mat;
    1.75 +	mat.set_translation(Vector3(5, get_screen_height() - 20, 0));
    1.76 +	set_world_matrix(mat);
    1.77 +
    1.78 +	set_unistate("st_color", Vector4(1, 1, 0, 1));
    1.79 +
    1.80 +	sdr->bind();
    1.81 +
    1.82 +	dtx_use_font(font, FONT_SIZE);
    1.83 +	dtx_string(fps_text);
    1.84 +	dtx_flush();
    1.85 +}