add mupdf demo
This commit is contained in:
91
main.c
91
main.c
@@ -1,6 +1,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <mupdf/fitz.h>
|
||||
#define IMGL3W_IMPL
|
||||
#include "opengl3_loader.h"
|
||||
#define GLFW_INCLUDE_NONE
|
||||
@@ -23,6 +24,48 @@ extern "C" void userAppExit()
|
||||
}
|
||||
#endif
|
||||
|
||||
const char *vertexShaderSource = "#version 330 core\n"
|
||||
"layout (location = 0) in vec3 aPos;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
||||
"}\0";
|
||||
|
||||
static const GLfloat vertices[] = {
|
||||
0.f, 0.f,
|
||||
1.f, 0.f,
|
||||
1.f, 1.f,
|
||||
0.f, 1.f,
|
||||
};
|
||||
|
||||
struct texture
|
||||
{
|
||||
GLuint id;
|
||||
int x, y, w, h;
|
||||
};
|
||||
|
||||
void ui_texture_from_pixmap(struct texture *tex, fz_pixmap *pix)
|
||||
{
|
||||
if (!tex->id)
|
||||
glGenTextures(1, &tex->id);
|
||||
glBindTexture(GL_TEXTURE_2D, tex->id);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
|
||||
tex->x = pix->x;
|
||||
tex->y = pix->y;
|
||||
tex->w = pix->w;
|
||||
tex->h = pix->h;
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->w, tex->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix->samples);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void ui_draw_image(struct texture *tex, float x, float y)
|
||||
{
|
||||
}
|
||||
|
||||
static void die(const char *msg)
|
||||
{
|
||||
fprintf(stderr, "%s\n", msg);
|
||||
@@ -32,6 +75,13 @@ static void die(const char *msg)
|
||||
// Main code
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr, "usage: example input-file\n");
|
||||
return 0;
|
||||
}
|
||||
char *input = argv[1];
|
||||
|
||||
if (!glfwInit())
|
||||
die("init glfw failed");
|
||||
|
||||
@@ -39,8 +89,40 @@ int main(int argc, char *argv[])
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
|
||||
/* Create a context to hold the exception stack and various caches. */
|
||||
fz_document *doc;
|
||||
fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
|
||||
if (!ctx)
|
||||
die("cannot create mupdf context");
|
||||
|
||||
fz_try(ctx)
|
||||
fz_register_document_handlers(ctx);
|
||||
fz_catch(ctx)
|
||||
{
|
||||
fprintf(stderr, "cannot register document handlers\n");
|
||||
fz_drop_context(ctx);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
fz_try(ctx)
|
||||
doc = fz_open_document(ctx, input);
|
||||
fz_catch(ctx)
|
||||
{
|
||||
fprintf(stderr, "cannot open document\n");
|
||||
fz_drop_context(ctx);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
struct texture page_tex = {0};
|
||||
int page_count = fz_count_pages(ctx, doc);
|
||||
printf("page_count %d\n", page_count);
|
||||
|
||||
fz_matrix ctm = fz_scale(1.f, 1.f);
|
||||
fz_pixmap *pix = fz_new_pixmap_from_page_number(ctx, doc, 0, ctm, fz_device_rgb(ctx), 1);
|
||||
ui_texture_from_pixmap(&page_tex, pix);
|
||||
|
||||
// Create window with graphics context
|
||||
GLFWwindow *window = glfwCreateWindow(1280, 720, "GLTEXT", NULL, NULL);
|
||||
GLFWwindow *window = glfwCreateWindow(pix->w, pix->h, "mupdf", NULL, NULL);
|
||||
if (!window)
|
||||
{
|
||||
glfwTerminate();
|
||||
@@ -58,6 +140,8 @@ int main(int argc, char *argv[])
|
||||
glfwGetFramebufferSize(window, &fbw, &fbh);
|
||||
glViewport(0, 0, fbw, fbh);
|
||||
|
||||
ui_draw_image(&page_tex, pix->w, pix->h);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwWaitEvents();
|
||||
}
|
||||
@@ -65,5 +149,10 @@ int main(int argc, char *argv[])
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
|
||||
/* Clean up. */
|
||||
fz_drop_pixmap(ctx, pix);
|
||||
fz_drop_document(ctx, doc);
|
||||
fz_drop_context(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user