Created
March 23, 2018 22:50
-
-
Save ZenToad/cddf7d811e680318aa38299445611352 to your computer and use it in GitHub Desktop.
Mortis game loop template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define STB_TRUETYPE_IMPLEMENTATION | |
#define STB_IMAGE_IMPLEMENTATION | |
#include "glm/glm/glm.hpp" | |
#include "glm/glm/ext.hpp" | |
#include "stb/stb_truetype.h" | |
#include "stb/stb_image.h" | |
#include "zen/sdlu.h" | |
#include "zen/glut.h" | |
#include "zen/zen.h" | |
const int WINDOW_WIDTH = 1024; | |
const int WINDOW_HEIGHT = 768; | |
const float fixedTimestep = 0.01f; | |
float accumulator = 0.0f; | |
float previousState; | |
float currentState; | |
uint32_t fontID; | |
void setup() { | |
previousState = 0.0f; | |
currentState = 0.0f; | |
SDL::createWindow("Lua Test", WINDOW_WIDTH, WINDOW_HEIGHT); | |
glut::init(); | |
// loading a font | |
uint8_t *file = SDL::loadFile("../fonts/FreePixel.ttf"); | |
fontID = glut::loadFont(file, 16); | |
SDL::freeFile(file); | |
glut::setupMultisampler(WINDOW_WIDTH, WINDOW_HEIGHT, 8); | |
SDL::showWindow(); | |
} | |
void shutdown() { | |
SDL::destroyWindow(); | |
} | |
void fixedUpdate() { | |
previousState = currentState; | |
currentState += fixedTimestep * 10; | |
if (currentState > WINDOW_WIDTH) { | |
currentState = 0; | |
} | |
} | |
void update() { | |
} | |
void input() { | |
SDL::window.shouldClose = SDL::keyDown(ESCAPE); | |
} | |
void render(float alpha) { | |
float xpos = (1.0f - alpha) * previousState + alpha * currentState; | |
float ypos = 200.0f; | |
float radius = 42.0f; | |
glm::vec2 xy0(xpos - radius, ypos - radius); | |
glm::vec2 xy1(xpos + radius, ypos + radius); | |
float z = -0.5; | |
glm::vec4 color(0.0f, 1.0f, 0.0f, 0.5f); | |
glm::mat4 mat = glm::ortho(0.0f, (float)WINDOW_WIDTH, (float)WINDOW_HEIGHT, 0.0f, 0.1f, 1.0f); | |
glut::drawRect(&xy0[0], &xy1[0], z, &color[0], &mat[0][0]); | |
} | |
int main(int argc, char *argv[]) { | |
setup(); | |
SDL::createWindow("Lua Test", WINDOW_WIDTH, WINDOW_HEIGHT); | |
glut::init(); | |
// loading a font | |
uint8_t *file = SDL::loadFile("../fonts/FreePixel.ttf"); | |
fontID = glut::loadFont(file, 16); | |
SDL::freeFile(file); | |
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); | |
glut::setupMultisampler(WINDOW_WIDTH, WINDOW_HEIGHT, 8); | |
SDL::showWindow(); | |
while (SDL::isRunning()) { | |
glut::initFrame(); | |
SDL::beginFrame(); | |
float frameTime = SDL::frame.deltaTime; | |
if (frameTime < 0.2f) { | |
frameTime = 0.2f; | |
} | |
accumulator += frameTime; | |
while (accumulator >= fixedTimestep) { | |
fixedUpdate(); | |
accumulator -= fixedTimestep; | |
} | |
input(); | |
update(); | |
render(accumulator / fixedTimestep); | |
glEnable(GL_BLEND); | |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
glm::vec3 todoPos(10.0f, 20.0f, -0.5f); | |
glm::vec4 color(0.0f, 1.0f, 0.0f, 0.5f); | |
glm::mat4 mat = glm::ortho(0.0f, (float)WINDOW_WIDTH, (float)WINDOW_HEIGHT, 0.0f, 0.1f, 1.0f); | |
std::string str = zen::format("%d frames", SDL::frame.totalFrames); | |
glut::drawFont(fontID, str.c_str(), &todoPos[0], &color[0], &mat[0][0]); | |
todoPos.y += 20; | |
int32_t ms = (int32_t)(1.0e3 * SDL::frame.totalTime / SDL::frame.totalFrames); | |
str = zen::format("%d (ms)", ms); | |
glut::drawFont(fontID, str.c_str(), &todoPos[0], &color[0], &mat[0][0]); | |
glut::renderFrame(); | |
SDL::endFrame(); | |
} | |
glut::quit(); | |
SDL::destroyWindow(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment