Skip to content

Instantly share code, notes, and snippets.

@starwing
Last active August 29, 2015 14:02
Show Gist options
  • Save starwing/b3774df16628a77abfb2 to your computer and use it in GitHub Desktop.
Save starwing/b3774df16628a77abfb2 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "../src/nanovg.h"
#define NANOVG_GL2_IMPLEMENTATION
#include "../src/nanovg_gl.h"
/*#include "../example/perf.h"*/
struct Lyrics {
int time;
const char *str;
} lyrics[] = {
{ 0, "时光是琥珀 泪一滴滴被反锁" },
{ 658, "情书再不朽 也磨成沙漏" },
{ 1346, "青春的上游 白云飞走苍狗与海鸥" },
{ 2065, "闪过的念头 潺潺的溜走" },
{ 2652, "" },
{ 2721, "命运好幽默 让爱的人都沉默" },
{ 3413, "一整个宇宙 换一颗红豆" },
{ 4092, "回忆如困兽 寂寞太久而渐渐温柔" },
{ 4810, "放开了拳头 反而更自由" },
{ 5377, "" },
{ 5465, "慢动作 缱绻胶卷 " },
{ 5776, "重播默片 定格一瞬间" },
{ 6202, "我们在 告别的演唱会 " },
{ 6617, "说好不再见" },
{ 6860, "" },
{ 6902, "你写给我 我的第一首歌" },
{ 7476, "你和我 十指紧扣 默写前奏" },
{ 7980, "可是那然后呢" },
{ 8258, "还好我有 我这一首情歌" },
{ 8843, "轻轻的 轻轻哼着 哭着笑着" },
{ 9328, "我的 天长地久" },
{ 9929, "" },
{ 11469, "命运好幽默 让爱的人都沉默" },
{ 12157, "一整个宇宙 换一颗红豆" },
{ 12842, "回忆如困兽 寂寞太久而渐渐温柔" },
{ 13553, "放开了拳头 反而更自由" },
{ 14173, "" },
{ 14213, "长镜头 越拉越远 " },
{ 14533, "越来越远 事隔好几年" },
{ 14944, "我们在 怀念的演唱会 " },
{ 15366, "礼貌的吻别" },
{ 15789, "" },
{ 15819, "你写给我 我的第一首歌" },
{ 16397, "你和我 十指紧扣 默写前奏" },
{ 16899, "可是那然后呢" },
{ 17177, "还好我有 我这一首情歌" },
{ 17766, "轻轻的 轻轻哼着 哭着笑着" },
{ 18246, "我的 天长地久" },
{ 18854, "" },
{ 18896, "陪我唱歌 清唱你的情歌" },
{ 19487, "舍不得 短短副歌 心还热着" },
{ 19993, "也该告一段落" },
{ 20259, "还好我有 我下一首情歌" },
{ 20856, "生命宛如 静静的 相拥的河" },
{ 21333, "永远 天长地久" },
{ 22094, "" },
{ 22300, NULL },
};
static void errorcb(int error, const char* desc) {
printf("GLFW error %d: %s\n", error, desc);
}
int idx = 0;
static void renderText(struct NVGcontext* ctx, int mx, int my, int w, int h, float t) {
int time = t*100;
int curr = lyrics[idx].time/3;
int next = lyrics[idx+1].time/3;
printf("\r[%d] time = %d, curr = %d, next = %d", idx, time, curr, next);
nvgFontFace(ctx, "sans");
nvgFontSize(ctx, 40);
nvgGlobalAlpha(ctx, 1.0f);
nvgFontBlur(ctx, 0);
nvgTextAlign(ctx, NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
if (time >= curr && time <= curr+30) {
nvgGlobalAlpha(ctx, (0-curr+time)/30.0f);
nvgFontBlur(ctx, 30+curr-time);
}
else if (time >= next-30) {
nvgGlobalAlpha(ctx, (next-time)/30.0f);
nvgFontBlur(ctx, 30-next+time);
}
nvgText(ctx, w/2, h/2, lyrics[idx].str, NULL);
if (time >= next) {
++idx;
if (lyrics[idx].str == NULL) {
idx = 0;
glfwSetTime(0);
}
}
}
int main() {
GLFWwindow* window;
struct NVGcontext* vg = NULL;
/*struct PerfGraph fps;*/
int prevt;
if (!glfwInit()) {
printf("Failed to init GLFW.");
return -1;
}
/*initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");*/
glfwSetErrorCallback(errorcb);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
#ifdef DEMO_MSAA
glfwWindowHint(GLFW_SAMPLES, 4);
#endif
window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
// window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
printf("Could not init glew.\n");
return -1;
}
#ifdef DEMO_MSAA
vg = nvgCreateGL2(NVG_STENCIL_STROKES);
#else
vg = nvgCreateGL2(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
#endif
if (vg == NULL) {
printf("Could not init nanovg.\n");
return -1;
}
if (nvgCreateFont(vg, "sans", "./DroidSansFallback.ttf") == -1) {
printf("Could not add font sans\n");
return -1;
}
glfwSwapInterval(0);
glfwSetTime(0);
prevt = glfwGetTime();
while (!glfwWindowShouldClose(window)) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE))
break;
double mx, my, t, dt;
int winWidth, winHeight;
int fbWidth, fbHeight;
float pxRatio;
t = glfwGetTime();
dt = t - prevt;
prevt = t;
(void)dt;
/*updateGraph(&fps, dt);*/
glfwGetCursorPos(window, &mx, &my);
glfwGetWindowSize(window, &winWidth, &winHeight);
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
// Calculate pixel ration for hi-dpi devices.
pxRatio = (float)fbWidth / (float)winWidth;
// Update and render
glViewport(0, 0, fbWidth, fbHeight);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
renderText(vg, mx, my, winWidth, winHeight, t);
/*renderGraph(vg, 5, 5, &fps);*/
nvgEndFrame(vg);
glfwSwapBuffers(window);
glfwPollEvents();
}
nvgDeleteGL2(vg);
glfwTerminate();
return 0;
}
/* cc: flags+='-ggdb'
* cc: input+='../src/nanovg.c ../src/stb_image.c'
* cc: libs+='-lglew -lglfw3 -lopengl32 -lgdi32' */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment