Browse Source

add renDoRenderLoop interface

testing
cinnaboot 6 years ago
parent
commit
e598a58e2e
  1. 38
      examples/assimp_loading/main.cpp
  2. 8
      include/renderer.h
  3. 29
      src/renderer.cpp

38
examples/assimp_loading/main.cpp

@ -8,47 +8,11 @@
#include "entity.h"
typedef void (*frame_callback_fn) (render_state*);
void
doFrameCallback(render_state* rs)
{
}
void
doRenderLoop(render_state* rs, frame_callback_fn callback_fn)
{
// TODO: move frame delay to renderFrame()
const uint TARGET_FPS = 60;
const uint FRAME_DELAY = 1000 / TARGET_FPS;
uint frameStart, frameTime;
bool running = true;
SDL_Event e;
while (running) {
frameStart = SDL_GetTicks();
// TODO: add a better structure for input, and provide a callback to use
// here
while (SDL_PollEvent(&e)) {
if ((e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE)
|| e.type == SDL_QUIT) {
running = false;
}
}
callback_fn(rs);
renRenderFrame(rs);
SDL_GL_SwapWindow(rs->handles.window);
frameTime = SDL_GetTicks() - frameStart;
if (FRAME_DELAY > frameTime)
SDL_Delay(FRAME_DELAY - frameTime);
}
}
// TODO: remove/refactor this when we get animation working
#include "animation_testing.cpp"
@ -91,7 +55,7 @@ main()
// TODO: implement setting rotation from entity
//spaceship.rotation = glm::vec4(0, 0, 0, 0);
doRenderLoop(rs, doFrameCallback);
renDoRenderLoop(rs, 60, doFrameCallback);
} else {
LOG(Error) << "Error initializing entity, exiting\n";
}

8
include/renderer.h

@ -57,6 +57,14 @@ void renShutdown(render_state* rs);
render_group*
renAllocateGroup(uint entity_count, shader_program shader);
//
typedef void (*frame_callback_fn) (render_state*);
void renDoRenderLoop(render_state* rs,
uint framerate = 60,
frame_callback_fn cb_func = nullptr);
//
void renRenderFrame(render_state* rs);
bool

29
src/renderer.cpp

@ -105,6 +105,35 @@ renAllocateGroup(uint entity_count, shader_program shader)
return rg;
}
// TODO: add a better structure for input
void
renDoRenderLoop(render_state* rs, uint framerate, frame_callback_fn cb_func)
{
uint delay = (framerate > 0) ? framerate : 0;
uint frameStart, frameTime;
bool running = true;
SDL_Event e;
while (running) {
frameStart = SDL_GetTicks();
while (SDL_PollEvent(&e)) {
if ((e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE)
|| e.type == SDL_QUIT) {
running = false;
}
}
if (cb_func != nullptr) cb_func(rs);
renRenderFrame(rs);
SDL_GL_SwapWindow(rs->handles.window);
frameTime = SDL_GetTicks() - frameStart;
if (delay > frameTime)
SDL_Delay(delay - frameTime);
}
}
void
renRenderFrame(render_state* rs)
{

Loading…
Cancel
Save