1
0
Files
gltext/main.c
2025-05-28 19:47:16 +08:00

158 lines
3.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <mupdf/fitz.h>
#define IMGL3W_IMPL
#include "opengl3_loader.h"
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#if defined(__SWITCH__)
#include <switch.h>
extern "C" void userAppInit()
{
socketInitializeDefault();
nxlinkStdio();
appletSetWirelessPriorityMode(AppletWirelessPriorityMode_OptimizedForWlan);
plInitialize(PlServiceType_User);
}
extern "C" void userAppExit()
{
plExit();
socketExit();
}
#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);
exit(1);
}
// 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");
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
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(pix->w, pix->h, "mupdf", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
imgl3wInit2(glfwGetProcAddress);
glfwSwapInterval(1);
glfwSetTime(0);
int fbw = 0, fbh = 0;
// Main loop
while (!glfwWindowShouldClose(window))
{
glfwGetFramebufferSize(window, &fbw, &fbh);
glViewport(0, 0, fbw, fbh);
ui_draw_image(&page_tex, pix->w, pix->h);
glfwSwapBuffers(window);
glfwWaitEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
/* Clean up. */
fz_drop_pixmap(ctx, pix);
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
return 0;
}