toggle framerate

This commit is contained in:
Frank DeMarco 2018-12-03 04:46:10 -05:00
parent 7768702de4
commit 209ca9c2c0
1 changed files with 28 additions and 17 deletions

View File

@ -155,14 +155,15 @@ GLuint get_gl_texture_from_surface(SDL_Surface *surface, GLint mipmap_filter)
SDL_Surface* get_framerate_indicator_surface(int frame_count)
{
TTF_Font *font = TTF_OpenFont("SourceCodePro-Regular.otf", 14);
SDL_Surface *message = zoomSurface(
TTF_RenderText_Blended(font, std::to_string(frame_count).c_str(), {255, 0, 0}), 1, -1,
SMOOTHING_OFF);
if (!message)
SDL_Surface *shaded = TTF_RenderText_Shaded(
font, std::to_string(frame_count).c_str(), {0, 0, 0}, {255, 255, 255});
SDL_Surface *converted = SDL_ConvertSurfaceFormat(shaded, SDL_PIXELFORMAT_ARGB8888, 0);
SDL_Surface *flipped = zoomSurface(converted, 1, -1, SMOOTHING_OFF);
if (!flipped)
{
fprintf(stderr, "Could not create text! SDL_Error: %s\n", SDL_GetError());
}
return message;
return flipped;
}
void set_framerate_indicator(int frame_count, GLuint id)
@ -309,8 +310,8 @@ int main(int argc, char *argv[])
std::array<glm::vec3, 6> framerate_indicator_vertices = {
{
{.5, 1, 0}, {1, 1, 0}, {.5, .5, 0},
{1, 1, 0}, {1, .5, 0}, {.5, .5, 0}
{.9, 1, 0}, {1, 1, 0}, {.9, .9, 0},
{1, 1, 0}, {1, .9, 0}, {.9, .9, 0}
}};
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -450,7 +451,7 @@ int main(int argc, char *argv[])
float frame_length = 1000.0 / framerate;
int last_frame_timestamp, frame_count_timestamp, last_capture_timestamp;
last_frame_timestamp = frame_count_timestamp = last_capture_timestamp = SDL_GetTicks();
bool is_recording = false;
bool is_recording = false, show_framerate = false;
std::list<SDL_Surface*> frames;
int recording_capture_framerate = 100, frame_time_overflow = 0, capture_time_overflow = 0,
frame_count = 0, ticks;
@ -506,6 +507,10 @@ int main(int argc, char *argv[])
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
}
}
else if (event.key.keysym.sym == SDLK_f && SDL_GetModState() & KMOD_CTRL)
{
show_framerate = !show_framerate;
}
}
}
if (is_recording && ticks - last_capture_timestamp + capture_time_overflow >
@ -534,11 +539,14 @@ int main(int argc, char *argv[])
glDisableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glDrawArrays(GL_TRIANGLES, 36, 6);
glBindTexture(GL_TEXTURE_2D, framerate_texture_id);
glDisableVertexAttribArray(2);
glEnableVertexAttribArray(1);
glVertexAttrib3f(2, 1, 1, 1);
glDrawArrays(GL_TRIANGLES, 42, 6);
if (show_framerate)
{
glBindTexture(GL_TEXTURE_2D, framerate_texture_id);
glDisableVertexAttribArray(2);
glEnableVertexAttribArray(1);
glVertexAttrib3f(2, 1, 1, 1);
glDrawArrays(GL_TRIANGLES, 42, 6);
}
// printf("%s\n", glm::to_string(model).c_str());
model = glm::rotate(model, .0005f * frame_length, glm::vec3(0.0f, 1.0f, 0.0f));
mvp = projection * view * model;
@ -546,8 +554,9 @@ int main(int argc, char *argv[])
glUseProgram(world_program);
glUniformMatrix4fv(m_id, 1, GL_FALSE, &mvp[0][0]);
glBindTexture(GL_TEXTURE_2D, t_id);
// glEnableVertexAttribArray(1);
// glDisableVertexAttribArray(2);
glEnableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glVertexAttrib3f(2, 1, 1, 1);
glDrawArrays(GL_TRIANGLES, 0, 36);
//glFlush();
//SDL_Rect rect = {x++, 0, 240, 160};
@ -558,8 +567,10 @@ int main(int argc, char *argv[])
if (ticks - frame_count_timestamp >= 1000)
{
frame_count_timestamp = ticks;
printf("%i\n", frame_count);
set_framerate_indicator(frame_count, framerate_texture_id);
if (show_framerate)
{
set_framerate_indicator(frame_count, framerate_texture_id);
}
frame_count = 0;
}
SDL_Delay(15);