This commit is contained in:
ZtRXR 2024-06-28 21:17:11 +08:00
parent 82ed26c165
commit 0d25c81cb3
4 changed files with 55 additions and 39 deletions

View File

@ -10,4 +10,5 @@ pybind11_add_module(${PROJECT_NAME} ${SRC})
add_subdirectory(glfw) add_subdirectory(glfw)
target_link_libraries(${PROJECT_NAME} PRIVATE glfw) target_link_libraries(${PROJECT_NAME} PRIVATE glfw)
find_package(OpenGl REQUIRED) find_package(OpenGl REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(${PROJECT_NAME} PRIVATE OpenGL::GL) target_link_libraries(${PROJECT_NAME} PRIVATE OpenGL::GL)

5
include/glpy.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
namespace tests{
int glfw_window(unsigned width,unsigned height, std::string title);
}

View File

@ -1,46 +1,17 @@
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include <pybind11/stl.h> #include <pybind11/stl.h>
#include <GLFW/glfw3.h> #include <glpy.h>
int pytest_glfw_window()
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
namespace py = pybind11;
PYBIND11_MODULE(glpy, m) { PYBIND11_MODULE(glpy, m) {
m.doc() = "glpy a python opengl project"; // optional module docstring m.doc() = "glpy a python opengl project"; // optional module docstring
m.def("pytest_glfw_window",pytest_glfw_window); auto tests = m.def_submodule("tests","the tests module");
tests.def("glfw_window",&tests::glfw_window,
py::arg("width"),
py::arg("height"),
py::arg("title")
);
} }

39
src/test_window.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <pybind11/stl.h>
#include <GLFW/glfw3.h>
#include <glpy.h>
int tests::glfw_window(unsigned width,unsigned height, std::string title)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}