initialize

This commit is contained in:
Frank DeMarco 2018-12-13 19:02:44 -05:00
parent 82c475f92e
commit 59dbec98a0
1 changed files with 332 additions and 386 deletions

718
main.cpp
View File

@ -1,3 +1,4 @@
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <vector>
@ -181,9 +182,23 @@ void set_framerate_indicator(int frame_count, GLuint id)
struct Game
{
SDL_Window *window;
int sw, sh;
SDL_Renderer *renderer;
SDL_GLContext glcontext;
int sw = 640, sh = 480, framerate = 60, recording_capture_framerate = 100,
frame_time_overflow = 0, capture_time_overflow = 0, frame_count = 0,
last_frame_timestamp, frame_count_timestamp, last_capture_timestamp, ticks;
bool done = false, is_recording = false, show_framerate = false;
SDL_Event event;
float r = 0.0;
GLuint vbo, space_texture_id, mvp_id, framerate_texture_id, flat_program, world_program,
fake_texture_id;
float frame_length = 1000.0 / framerate;
std::list<SDL_Surface*> frames;
glm::mat4 projection, view, model, mvp;
Game() { }
Game()
bool initialize()
{
setenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN", "0", true);
SDL_version version;
@ -193,410 +208,341 @@ struct Game
SDL_SetMainReady();
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
fprintf(stderr, "SDL could not initalize! SDL_Error: %s\n", SDL_GetError());
print_sdl_error("SDL could not initalize!");
return false;
}
else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
window = SDL_CreateWindow("TARE control", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
sw, sh, SDL_WINDOW_OPENGL);
if (window == NULL)
{
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);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
window = SDL_CreateWindow(
"TARE control",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
sw,
sh,
SDL_WINDOW_OPENGL);
if (window == NULL)
print_sdl_error("Could not create window!");
return false;
}
SDL_ShowCursor(0);
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer == NULL)
{
print_sdl_error("Could not create renderer!");
return false;
}
SDL_Texture *texture = IMG_LoadTexture(renderer, "background.png");
if (!texture)
{
print_sdl_error("Could not load image!");
return false;
}
if (TTF_Init() < 0)
{
print_sdl_error("Could not initialize SDL ttf!");
return false;
}
if ((glcontext = SDL_GL_CreateContext(window)) == NULL)
{
print_sdl_error("Could not get GL context!");
return false;
}
printf("OpenGL %s, renderer %s, shading language %s\n", glGetString(GL_VERSION),
glGetString(GL_RENDERER), glGetString(GL_SHADING_LANGUAGE_VERSION));
/*
v0-v1-v2 (front)
v2-v3-v0
v0-v3-v4 (right)
v4-v5-v0
v0-v5-v6 (top)
v6-v1-v0
v1-v6-v7 (left)
v7-v2-v1
v7-v4-v3 (bottom)
v3-v2-v7
v4-v7-v6 (back)
v6-v5-v4
*/
std::array<glm::vec3, 36> cube = {
{
fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
{1, 1, 1}, {-1, 1, 1}, {-1,-1, 1},
{-1,-1, 1}, {1,-1, 1}, {1, 1, 1},
{1, 1, 1}, {1,-1, 1}, {1,-1,-1},
{1,-1,-1}, {1, 1,-1}, {1, 1, 1},
{1, 1, 1}, {1, 1,-1}, {-1, 1,-1},
{-1, 1,-1}, {-1, 1, 1}, {1, 1, 1},
{-1, 1, 1}, {-1, 1,-1}, {-1,-1,-1},
{-1,-1,-1}, {-1,-1, 1}, {-1, 1, 1},
{-1,-1,-1}, {1,-1,-1}, {1,-1, 1},
{1,-1, 1}, {-1,-1, 1}, {-1,-1,-1},
{1,-1,-1}, {-1,-1,-1}, {-1, 1,-1},
{-1, 1,-1}, {1, 1,-1}, {1,-1,-1}
}};
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}
}};
GLfloat background_colors[40][3] = {
{.2, .6, .8}, {.2, .6, .8}, {1, 1, 0},
{.2, .6, .8}, {1, 1, 0}, {1, 1, 0}
};
GLuint background_colors_buffer;
glGenBuffers(1, &background_colors_buffer);
std::array<glm::vec3, 6> framerate_indicator_vertices = {
{
{.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);
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());
projection = glm::perspective(glm::radians(45.0f),
(float) sw / (float) sh, 0.1f, 100.0f);
view = glm::lookAt(
glm::vec3(4, 3, 3), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
model = glm::mat4(1.0f);
SDL_Surface *surface = rotateSurface90Degrees(IMG_Load("tile.png"), 2);
printf("tile.png bytes per pixel %i\n", surface->format->BytesPerPixel);
space_texture_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());
GLuint uvbuffer;
glGenBuffers(1, &uvbuffer);
unsigned char fake_texture_color[4] = {255, 255, 255, 255};
glCreateTextures(GL_TEXTURE_2D, 1, &fake_texture_id);
glBindTexture(GL_TEXTURE_2D, fake_texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
fake_texture_color);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, &vertices.front(),
GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, uv.capacity() * sizeof(GLfloat) * 2, &uv.front(), GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, background_colors_buffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,
(cube.size()) * sizeof(GLfloat) * 3,
background_vertices.size() * sizeof(GLfloat) * 3, background_colors);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(2);
GLuint vertex_shader = load_shader("triangle.vert", GL_VERTEX_SHADER);
GLuint fragment_shader = load_shader("triangle.frag", GL_FRAGMENT_SHADER);
GLuint flat_shader = load_shader("flat.vert", GL_VERTEX_SHADER);
world_program = glCreateProgram();
glAttachShader(world_program, vertex_shader);
glAttachShader(world_program, fragment_shader);
glBindAttribLocation(world_program, 0, "in_Position");
glBindAttribLocation(world_program, 1, "vertexUV");
glBindAttribLocation(world_program, 2, "in_Color");
link_shader(world_program);
flat_program = glCreateProgram();
glAttachShader(flat_program, flat_shader);
glAttachShader(flat_program, fragment_shader);
glBindAttribLocation(flat_program, 0, "in_Position");
glBindAttribLocation(flat_program, 1, "vertexUV");
glBindAttribLocation(flat_program, 2, "in_Color");
link_shader(flat_program);
mvp_id = glGetUniformLocation(world_program, "MVP");
GLuint sampler_uniform_id = glGetUniformLocation(world_program, "myTextureSampler");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, space_texture_id);
glUniform1i(sampler_uniform_id, 0);
glDepthFunc(GL_LESS);
last_frame_timestamp = frame_count_timestamp = last_capture_timestamp = SDL_GetTicks();
framerate_texture_id = get_gl_texture_from_surface(
get_framerate_indicator_surface(frame_count), GL_LINEAR);
return true;
}
void print_sdl_error(std::string message)
{
std::cerr << message << SDL_GetError() << std::endl;
}
void run()
{
while (!done)
{
ticks = SDL_GetTicks();
if (ticks - last_frame_timestamp + frame_time_overflow >= frame_length)
{
frame_count++;
frame_time_overflow = ticks - last_frame_timestamp +
frame_time_overflow - frame_length;
last_frame_timestamp = ticks;
update();
}
else
SDL_Delay(15);
if (ticks - frame_count_timestamp >= 1000)
{
SDL_ShowCursor(0);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer == NULL)
frame_count_timestamp = ticks;
if (show_framerate)
{
fprintf(stderr, "Could not create renderer! SDL_Error: %s\n", SDL_GetError());
set_framerate_indicator(frame_count, framerate_texture_id);
}
else
frame_count = 0;
}
}
}
void update()
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
done = true;
}
else if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_F9)
{
SDL_Texture *texture = IMG_LoadTexture(renderer, "background.png");
if (!texture)
capture_screen(window);
}
else if (event.key.keysym.sym == SDLK_F10)
{
if (!is_recording)
{
fprintf(stderr, "Could not load image! SDL_Error: %s\n", SDL_GetError());
start_recording(&is_recording);
}
else
{
TTF_Init();
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
printf("OpenGL %s, renderer %s, shading language %s\n",
glGetString(GL_VERSION), glGetString(GL_RENDERER),
glGetString(GL_SHADING_LANGUAGE_VERSION));
SDL_Event event;
int done = 0;
float r = 0.0;
GLuint vao, vbo;
// GLfloat diamond[4][2] = {
// { 0.0, 1.0 },
// { 1.0, 0.0 },
// { 0.0, -1.0 },
// { -1.0, 0.0 } };
// const GLfloat diamond_colors[4][3] = {
// { 1.0, 0.0, 0.0 },
// { 0.0, 1.0, 0.0 },
// { 0.0, 0.0, 1.0 },
// { 1.0, 1.0, 1.0 } };
/*
v0-v1-v2 (front)
v2-v3-v0
v0-v3-v4 (right)
v4-v5-v0
v0-v5-v6 (top)
v6-v1-v0
v1-v6-v7 (left)
v7-v2-v1
v7-v4-v3 (bottom)
v3-v2-v7
v4-v7-v6 (back)
v6-v5-v4
*/
std::array<glm::vec3, 36> cube = {{
{1, 1, 1}, {-1, 1, 1}, {-1,-1, 1},
{-1,-1, 1}, {1,-1, 1}, {1, 1, 1},
{1, 1, 1}, {1,-1, 1}, {1,-1,-1},
{1,-1,-1}, {1, 1,-1}, {1, 1, 1},
{1, 1, 1}, {1, 1,-1}, {-1, 1,-1},
{-1, 1,-1}, {-1, 1, 1}, {1, 1, 1},
{-1, 1, 1}, {-1, 1,-1}, {-1,-1,-1},
{-1,-1,-1}, {-1,-1, 1}, {-1, 1, 1},
{-1,-1,-1}, {1,-1,-1}, {1,-1, 1},
{1,-1, 1}, {-1,-1, 1}, {-1,-1,-1},
{1,-1,-1}, {-1,-1,-1}, {-1, 1,-1},
{-1, 1,-1}, {1, 1,-1}, {1,-1,-1}
}};
// GLfloat cube_colors[36][3] = {
// {1, 0, 0}, {1, 0, 0}, {1, 0, 0},
// {1, 0, 0}, {1, 0, 0}, {1, 0, 0},
// {0, 1, 0}, {0, 1, 0}, {0, 1, 0},
// {0, 1, 0}, {0, 1, 0}, {0, 1, 0},
// {0, 0, 1}, {0, 0, 1}, {0, 0, 1},
// {0, 0, 1}, {0, 0, 1}, {0, 0, 1},
// {1, 1, 0}, {1, 1, 0}, {1, 1, 0},
// {1, 1, 0}, {1, 1, 0}, {1, 1, 0},
// {0, 1, 1}, {0, 1, 1}, {0, 1, 1},
// {0, 1, 1}, {0, 1, 1}, {0, 1, 1},
// {1, 0, 1}, {1, 0, 1}, {1, 0, 1},
// {1, 0, 1}, {1, 0, 1}, {1, 0, 1}
// };
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}
}};
GLfloat background_colors[40][3] = {
{.2, .6, .8}, {.2, .6, .8}, {1, 1, 0},
{.2, .6, .8}, {1, 1, 0}, {1, 1, 0}
};
GLuint background_colors_buffer;
glGenBuffers(1, &background_colors_buffer);
std::array<glm::vec3, 6> framerate_indicator_vertices = {
{
{.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);
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);
glm::mat4 view = glm::lookAt(
glm::vec3(4, 3, 3),
glm::vec3(0, 0, 0),
glm::vec3(0, 1, 0));
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 mvp;
SDL_Surface *surface = rotateSurface90Degrees(
IMG_Load("tile.png"), 2);
printf("bytes per pixel %i\n", surface->format->BytesPerPixel);
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);
GLuint fake_texture_id;
unsigned char fake_texture_color[4] = {255, 255, 255, 255};
glCreateTextures(GL_TEXTURE_2D, 1, &fake_texture_id);
glBindTexture(GL_TEXTURE_2D, fake_texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
fake_texture_color);
// Allocate and assign a Vertex Array Object to our handle
glGenVertexArrays(1, &vao);
// Bind our Vertex Array Object as the current used object
glBindVertexArray(vao);
// Allocate and assign one Vertex Buffer Objects to our handle
glGenBuffers(1, &vbo);
// Bind vbo as being the active buffer and storing vertex coordinates
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Copy vertex data into buffer
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, &vertices.front(),
GL_STATIC_DRAW);
// Specify that our coordinate data is going into attribute
// index 0, and contains three floats per vertex
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Enable attribute index 0 as being used
glEnableVertexAttribArray(0);
// Bind our second VBO as being the active buffer and storing texture coordinates
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
// Copy the data from cube_uv to our buffer
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
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
// Enable attribute index 1
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, background_colors_buffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,
(cube.size()) * sizeof(GLfloat) * 3,
background_vertices.size() * sizeof(GLfloat) * 3, background_colors);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
GLuint vertex_shader = load_shader("triangle.vert", GL_VERTEX_SHADER);
GLuint fragment_shader = load_shader("triangle.frag", GL_FRAGMENT_SHADER);
GLuint flat_shader = load_shader("flat.vert", GL_VERTEX_SHADER);
GLuint world_program = glCreateProgram();
glAttachShader(world_program, vertex_shader);
glAttachShader(world_program, fragment_shader);
glBindAttribLocation(world_program, 0, "in_Position");
glBindAttribLocation(world_program, 1, "vertexUV");
glBindAttribLocation(world_program, 2, "in_Color");
link_shader(world_program);
GLuint flat_program = glCreateProgram();
glAttachShader(flat_program, flat_shader);
glAttachShader(flat_program, fragment_shader);
glBindAttribLocation(flat_program, 0, "in_Position");
glBindAttribLocation(flat_program, 1, "vertexUV");
glBindAttribLocation(flat_program, 2, "in_Color");
link_shader(flat_program);
GLuint m_id = glGetUniformLocation(world_program, "MVP");
GLuint t_uniform_id = glGetUniformLocation(world_program, "myTextureSampler");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, t_id);
glUniform1i(t_uniform_id, 0);
glDepthFunc(GL_LESS);
int framerate = 60;
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, 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;
GLuint framerate_texture_id = get_gl_texture_from_surface(
get_framerate_indicator_surface(frame_count), GL_LINEAR);
while (!done)
{
ticks = SDL_GetTicks();
if (ticks - last_frame_timestamp + frame_time_overflow >= frame_length)
{
frame_count++;
frame_time_overflow = ticks - last_frame_timestamp +
frame_time_overflow - frame_length;
last_frame_timestamp = ticks;
for (int ii = 1; frame_time_overflow > frame_length;
ii++, frame_time_overflow -= frame_length)
{
fprintf(stderr, "lost %i frame(s)\n", ii);
}
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
done = 1;
}
else if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_F9)
{
capture_screen(window);
}
else if (event.key.keysym.sym == SDLK_F10)
{
if (!is_recording)
{
start_recording(&is_recording);
}
else
{
end_recording(frames, &is_recording);
}
}
else if (event.key.keysym.sym == SDLK_F11)
{
if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)
{
SDL_SetWindowFullscreen(window, 0);
}
else
{
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 >
recording_capture_framerate)
{
frames.push_back(get_screen_surface(window));
printf("added frame at %i\n", ticks);
capture_time_overflow = ticks - last_capture_timestamp + capture_time_overflow -
recording_capture_framerate;
last_capture_timestamp = ticks;
for (int ii = 1; capture_time_overflow > recording_capture_framerate;
ii++, capture_time_overflow -= recording_capture_framerate)
{
fprintf(stderr, "lost %i frame(s) during capture\n", ii);
}
}
// glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
// glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), diamond, GL_STATIC_DRAW);
// glClearColor((sin(r) + 1) / 2, (sin(r) + 1) / 2, .5, 1);
glClearColor(.7, .7, .5, 1);
r += .025;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glUseProgram(flat_program);
glBindTexture(GL_TEXTURE_2D, fake_texture_id);
glDisableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glDrawArrays(GL_TRIANGLES, 36, 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;
glEnable(GL_DEPTH_TEST);
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);
glDrawArrays(GL_TRIANGLES, 0, 36);
//glFlush();
//SDL_Rect rect = {x++, 0, 240, 160};
//SDL_RenderCopy(renderer, texture, NULL, &rect);
//SDL_RenderPresent(renderer);
SDL_GL_SwapWindow(window);
}
if (ticks - frame_count_timestamp >= 1000)
{
frame_count_timestamp = ticks;
if (show_framerate)
{
set_framerate_indicator(frame_count, framerate_texture_id);
}
frame_count = 0;
}
SDL_Delay(15);
}
SDL_GL_DeleteContext(glcontext);
SDL_DestroyTexture(texture);
end_recording(frames, &is_recording);
}
SDL_DestroyRenderer(renderer);
}
SDL_DestroyWindow(window);
else if (event.key.keysym.sym == SDLK_F11)
{
if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)
{
SDL_SetWindowFullscreen(window, 0);
}
else
{
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 >
recording_capture_framerate)
{
frames.push_back(get_screen_surface(window));
printf("added frame at %i\n", ticks);
capture_time_overflow = ticks - last_capture_timestamp + capture_time_overflow -
recording_capture_framerate;
last_capture_timestamp = ticks;
for (int ii = 1; capture_time_overflow > recording_capture_framerate;
ii++, capture_time_overflow -= recording_capture_framerate)
{
fprintf(stderr, "lost %i frame(s) during capture\n", ii);
}
}
// glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
// glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), diamond, GL_STATIC_DRAW);
// glClearColor((sin(r) + 1) / 2, (sin(r) + 1) / 2, .5, 1);
glClearColor(.7, .7, .5, 1);
r += .025;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glUseProgram(flat_program);
glBindTexture(GL_TEXTURE_2D, fake_texture_id);
glDisableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glDrawArrays(GL_TRIANGLES, 36, 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;
glEnable(GL_DEPTH_TEST);
glUseProgram(world_program);
glUniformMatrix4fv(mvp_id, 1, GL_FALSE, &mvp[0][0]);
glBindTexture(GL_TEXTURE_2D, space_texture_id);
glEnableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glVertexAttrib3f(2, 1, 1, 1);
glDrawArrays(GL_TRIANGLES, 0, 36);
//glFlush();
//SDL_Rect rect = {x++, 0, 240, 160};
//SDL_RenderCopy(renderer, texture, NULL, &rect);
//SDL_RenderPresent(renderer);
SDL_GL_SwapWindow(window);
}
void quit()
{
if (glcontext != NULL)
{
SDL_GL_DeleteContext(glcontext);
}
if (renderer != NULL)
{
SDL_DestroyRenderer(renderer);
}
if (window != NULL)
{
SDL_DestroyWindow(window);
}
if (TTF_WasInit())
{
TTF_Quit();
}
SDL_Quit();
}
};
int main(int argc, char *argv[])
{
new Game();
Game g = Game();
if (g.initialize())
{
g.run();
}
g.quit();
return 0;
}