framerate indicator
This commit is contained in:
parent
b42a20e490
commit
7768702de4
2
Makefile
2
Makefile
|
@ -18,7 +18,7 @@ INC = -Iglm -Isdl2-gfx
|
|||
|
||||
linux :
|
||||
g++ -c $(CFLAGS) $(INC) -D__LINUX__ main.cpp sdl2-gfx/SDL2_rotozoom.c
|
||||
g++ $(LFLAGS) SDL2_rotozoom.o main.o -lSDL2_image -lGL -o main
|
||||
g++ $(LFLAGS) SDL2_rotozoom.o main.o -lSDL2_image -lSDL2_ttf -lGL -o main
|
||||
|
||||
android :
|
||||
if [ ! -d $(BUILDDIR) ]; then mkdir $(BUILDDIR); fi;
|
||||
|
|
Binary file not shown.
142
main.cpp
142
main.cpp
|
@ -4,6 +4,8 @@
|
|||
#include <array>
|
||||
#include <list>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#include <SDL_ttf.h>
|
||||
|
@ -132,6 +134,47 @@ void end_recording(std::list<SDL_Surface*> frames, bool *is_recording)
|
|||
}
|
||||
}
|
||||
|
||||
GLuint get_gl_texture_from_surface(SDL_Surface *surface, GLint mipmap_filter)
|
||||
{
|
||||
GLuint id;
|
||||
glCreateTextures(GL_TEXTURE_2D, 1, &id);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
GLenum format;
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
format = GL_RGBA;
|
||||
#else
|
||||
format = GL_BGRA;
|
||||
#endif
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, format,
|
||||
GL_UNSIGNED_BYTE, surface->pixels);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipmap_filter);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmap_filter);
|
||||
return id;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
fprintf(stderr, "Could not create text! SDL_Error: %s\n", SDL_GetError());
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
void set_framerate_indicator(int frame_count, GLuint id)
|
||||
{
|
||||
SDL_Surface *message = get_framerate_indicator_surface(frame_count);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, message->w, message->h, 0, GL_BGRA, GL_UNSIGNED_BYTE,
|
||||
message->pixels);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
setenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN", "0", true);
|
||||
|
@ -141,11 +184,11 @@ int main(int argc, char *argv[])
|
|||
fprintf(stderr, "stderr test message\n");
|
||||
SDL_SetMainReady();
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
{
|
||||
fprintf(stderr, "SDL could not initalize! SDL_Error: %s\n", SDL_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
fprintf(stderr, "SDL could not initalize! SDL_Error: %s\n", SDL_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
int sw = 640, sh = 480;
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
|
@ -179,6 +222,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
else
|
||||
{
|
||||
TTF_Init();
|
||||
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
|
||||
printf("OpenGL %s\n", glGetString(GL_VERSION));
|
||||
SDL_Event event;
|
||||
|
@ -212,7 +256,7 @@ int main(int argc, char *argv[])
|
|||
v4-v7-v6 (back)
|
||||
v6-v5-v4
|
||||
*/
|
||||
std::array<std::array<GLfloat, 3>, 36> cube = {{
|
||||
std::array<glm::vec3, 36> cube = {{
|
||||
{1, 1, 1}, {-1, 1, 1}, {-1,-1, 1},
|
||||
{-1,-1, 1}, {1,-1, 1}, {1, 1, 1},
|
||||
|
||||
|
@ -251,7 +295,7 @@ int main(int argc, char *argv[])
|
|||
// {1, 0, 1}, {1, 0, 1}, {1, 0, 1}
|
||||
// };
|
||||
|
||||
std::array<std::array<GLfloat, 3>, 36> background_vertices = {
|
||||
std::array<glm::vec3, 6> background_vertices = {
|
||||
{
|
||||
{-1, 1, 0}, {1, 1, 0}, {-1, -1, 0},
|
||||
{1, 1, 0}, {1, -1, 0}, {-1, -1, 0}
|
||||
|
@ -263,10 +307,20 @@ int main(int argc, char *argv[])
|
|||
GLuint background_colors_buffer;
|
||||
glGenBuffers(1, &background_colors_buffer);
|
||||
|
||||
std::vector<std::array<GLfloat, 3>> vertices;
|
||||
vertices.reserve(cube.size() + background_vertices.size());
|
||||
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}
|
||||
}};
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
std::vector<glm::vec3> vertices;
|
||||
vertices.reserve(cube.size() + background_vertices.size() + framerate_indicator_vertices.size());
|
||||
vertices.insert(vertices.begin(), cube.begin(), cube.end());
|
||||
vertices.insert(vertices.end(), background_vertices.begin(), background_vertices.end());
|
||||
vertices.insert(vertices.end(), framerate_indicator_vertices.begin(),
|
||||
framerate_indicator_vertices.end());
|
||||
|
||||
glm::mat4 projection = glm::perspective(glm::radians(45.0f),
|
||||
(float) sw / (float) sh, 0.1f, 100.0f);
|
||||
|
@ -280,26 +334,36 @@ int main(int argc, char *argv[])
|
|||
SDL_Surface *surface = rotateSurface90Degrees(
|
||||
IMG_Load("tile.png"), 2);
|
||||
printf("bytes per pixel %i\n", surface->format->BytesPerPixel);
|
||||
GLuint t_id;
|
||||
glCreateTextures(GL_TEXTURE_2D, 1, &t_id);
|
||||
glBindTexture(GL_TEXTURE_2D, t_id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_BGRA,
|
||||
GL_UNSIGNED_BYTE, surface->pixels);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
GLfloat cube_uv[72] = {
|
||||
1, 1, 0, 1, 0, 0,
|
||||
0, 0, 1, 0, 1, 1,
|
||||
0, 1, 0, 0, 1, 0,
|
||||
1, 0, 1, 1, 0, 1,
|
||||
0, 1, 0, 0, 1, 0,
|
||||
1, 0, 1, 1, 0, 1,
|
||||
1, 1, 0, 1, 0, 0,
|
||||
0, 0, 1, 0, 1, 1,
|
||||
0, 0, 1, 0, 1, 1,
|
||||
1, 1, 0, 1, 0, 0,
|
||||
0, 0, 1, 0, 1, 1,
|
||||
1, 1, 0, 1, 0, 0};
|
||||
GLuint t_id = get_gl_texture_from_surface(surface, GL_LINEAR);
|
||||
|
||||
std::array<glm::vec2, 6> framerate_indicator_uv = {
|
||||
{
|
||||
{0, 1}, {1, 1}, {0, 0},
|
||||
{1, 1}, {1, 0}, {0, 0}
|
||||
}};
|
||||
std::array<glm::vec2, 36> cube_uv = {
|
||||
{
|
||||
{1, 1}, {0, 1}, {0, 0},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{0, 1}, {0, 0}, {1, 0},
|
||||
{1, 0}, {1, 1}, {0, 1},
|
||||
{0, 1}, {0, 0}, {1, 0},
|
||||
{1, 0}, {1, 1}, {0, 1},
|
||||
{1, 1}, {0, 1}, {0, 0},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{1, 1}, {0, 1}, {0, 0},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{1, 1}, {0, 1}, {0, 0}
|
||||
}};
|
||||
std::vector<glm::vec2> uv;
|
||||
uv.reserve(cube_uv.size() + background_vertices.size() + framerate_indicator_uv.size());
|
||||
std::copy(cube_uv.begin(), cube_uv.end(), uv.begin());
|
||||
std::copy(framerate_indicator_uv.begin(), framerate_indicator_uv.end(),
|
||||
uv.begin() + cube_uv.size() + background_vertices.size());
|
||||
// uv.insert(uv.end(), cube_uv.begin(), cube_uv.end());
|
||||
// uv.insert(uv.end(), framerate_indicator_uv.begin(),
|
||||
// framerate_indicator_uv.end());
|
||||
GLuint uvbuffer;
|
||||
glGenBuffers(1, &uvbuffer);
|
||||
|
||||
|
@ -337,7 +401,7 @@ int main(int argc, char *argv[])
|
|||
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
|
||||
|
||||
// Copy the data from cube_uv to our buffer
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_uv), cube_uv, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, uv.capacity() * sizeof(GLfloat) * 2, &uv.front(), GL_STATIC_DRAW);
|
||||
|
||||
// Specify that our texture coordinate data is going into attribute index 1
|
||||
// and contains two floats per vertex
|
||||
|
@ -350,7 +414,7 @@ int main(int argc, char *argv[])
|
|||
glBindBuffer(GL_ARRAY_BUFFER, background_colors_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, 0, GL_STATIC_DRAW);
|
||||
glBufferSubData(GL_ARRAY_BUFFER,
|
||||
(vertices.size() - background_vertices.size()) * sizeof(GLfloat) * 3,
|
||||
(cube.size()) * sizeof(GLfloat) * 3,
|
||||
background_vertices.size() * sizeof(GLfloat) * 3, background_colors);
|
||||
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
|
@ -391,6 +455,9 @@ int main(int argc, char *argv[])
|
|||
int recording_capture_framerate = 100, frame_time_overflow = 0, capture_time_overflow = 0,
|
||||
frame_count = 0, ticks;
|
||||
|
||||
GLuint framerate_texture_id = get_gl_texture_from_surface(
|
||||
get_framerate_indicator_surface(frame_count), GL_LINEAR);
|
||||
|
||||
while (!done)
|
||||
{
|
||||
ticks = SDL_GetTicks();
|
||||
|
@ -467,6 +534,11 @@ 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);
|
||||
// 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;
|
||||
|
@ -474,9 +546,8 @@ 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);
|
||||
glVertexAttrib3f(2, 1, 1, 1);
|
||||
// glEnableVertexAttribArray(1);
|
||||
// glDisableVertexAttribArray(2);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
//glFlush();
|
||||
//SDL_Rect rect = {x++, 0, 240, 160};
|
||||
|
@ -488,9 +559,10 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
frame_count_timestamp = ticks;
|
||||
printf("%i\n", frame_count);
|
||||
set_framerate_indicator(frame_count, framerate_texture_id);
|
||||
frame_count = 0;
|
||||
}
|
||||
// SDL_Delay(15);
|
||||
SDL_Delay(15);
|
||||
}
|
||||
SDL_GL_DeleteContext(glcontext);
|
||||
SDL_DestroyTexture(texture);
|
||||
|
|
Loading…
Reference in New Issue