gpt4 book ai didi

c - OpenGL 在使用使用它制作的 dll 时给出异常

转载 作者:行者123 更新时间:2023-11-30 16:36:36 25 4
gpt4 key购买 nike

我正在使用 OpenGL 用 C 语言创建一个库。它只是创建一个窗口,但当我在示例程序上使用它时,它总是崩溃并给出异常:--

“抛出异常:写入访问冲突”

'“win”为 nullptr'

但是当我将它用作 header 并将其直接链接到示例程序而无需任何外部链接时,它会异常地工作。我是否错过了 OpenGL 不允许我用它制作任何库和 dll 的事情?我正在尝试用它制作自己的框架。

我也在使用 GLFW 和 GLEW。

编辑:

窗口.c:

#include "window.h"
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>

/* Create the structures... */

struct LIB_Vector2i
{
int x;
int y;
};

struct LIB_Vector2f
{
float x;
float y;
};

struct LIB_Window
{
LIB_String title;
int x;
int y;
int width;
int height;
LIB_Bool fullscreen;
GLFWwindow* window;
};

/* Start the functions here... */
LIB_Bool LIB_Initialize()
{
return (LIB_Bool)glfwInit();
}

void LIB_SetEvents()
{
glfwPollEvents();
}

void LIB_ClearToColor(LIB_Color color)
{
glClearColor(color.r, color.g, color.b, color.a);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}

void LIB_SetFrameColor(LIB_Color color)
{
glClearColor(color.r, color.g, color.b, color.a);
}

LIB_Window* LIB_CreateWindow(const LIB_String title, int x, int y, int width, int height, const LIB_Bool resizable, const LIB_Bool fullscreen)
{
LIB_Window wind;
wind.title = title;
wind.x = x;
wind.y = y;
wind.width = width;
wind.height = height;
wind.fullscreen = fullscreen;

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_RESIZABLE, resizable);

GLFWwindow* window = NULL;
if (window == NULL)
{
if (fullscreen == 1)
{
window = glfwCreateWindow(width, height, title, glfwGetPrimaryMonitor(), NULL);
}
else if (fullscreen == 0)
{
window = glfwCreateWindow(width, height, title, NULL, NULL);
}
}

int screen_width, screen_height;
glfwGetFramebufferSize(window, &screen_width, &screen_height);

if (window == NULL)
{
glfwTerminate();
return;
}

glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;

if (glewInit() != GLEW_OK)
{
return;
}

glViewport(0, 0, screen_width, screen_height);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return &wind;
}

void LIB_GetCenterPosition(LIB_Window * window, int *x, int *y)
{
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
*x = (mode->width - window->width) / 2;
*x = (mode->height - window->height) / 2;
}

void LIB_GetWindowFrameSize(LIB_Window * window, int *width, int *height)
{
int screenwidth, screenheight;
glfwGetFramebufferSize(window->window, &screenwidth, &screenheight);
*width = screenwidth;
*height = screenheight;
}

void LIB_GetWindowCursorPosition(LIB_Window * window, float *x, float *y)
{
double cx, cy;
glfwGetCursorPos(window->window, &cx, &cy);
*x = (float)cx;
*y = (float)cy;
}

void LIB_GetDisplaySize(int *width, int *height)
{
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
*width = mode->width;
*height = mode->height;
}

void LIB_GetWindowPosition(LIB_Window * window, int *x, int *y)
{
*x = window->x;
*y= window->y;
}

void LIB_GetWindowSize(LIB_Window * window, int *width, int *height)
{
*width = window->width;
*height = window->height;
}

LIB_String LIB_GetWindowTitle(LIB_Window * window)
{
return window->title;
}

LIB_Bool LIB_IsWindowFullScreen(LIB_Window * window)
{
return window->fullscreen;
}

LIB_Bool LIB_IsWindowOpened(LIB_Window * window)
{
return !glfwWindowShouldClose(window->window);
}

void LIB_SwapWindowBuffers(LIB_Window * window)
{
glfwSwapBuffers(window->window);
}

void LIB_SetWindowPosition(LIB_Window * window, int x, int y)
{
glfwSetWindowPos(window->window, x,y);
window->x = x;
window->y = y;
}

void LIB_SetWindowSize(LIB_Window * window, int width, int height)
{
glfwSetWindowSize(window->window, width, height);
window->width = width;
window->height = height;
}

void LIB_SetWindowTitle(LIB_Window * window, const LIB_String title)
{
glfwSetWindowTitle(window->window, title);
window->title = title;
}

void LIB_SetFullScreenState(LIB_Window * window, const LIB_Bool fullscreen)
{
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
if (fullscreen == LIB_FALSE)
{
glfwSetWindowMonitor(window->window, NULL, window->x, window->y, window->width, window->height, 60);
}
else if (fullscreen == LIB_TRUE)
{
glfwSetWindowMonitor(window->window, glfwGetPrimaryMonitor(), 0, 0, mode->width, mode->height, 60);
}
window->fullscreen = fullscreen;
}

void LIB_DestroyWindow(LIB_Window * window)
{
window->title = NULL;
window->x = 0;
window->y = 0;
window->width = 0;
window->height = 0;
free(window);
}

void LIB_Terminate()
{
printf("LIB terminated by the user!!\n");
glfwTerminate();
}

窗口.h:

#ifndef LIB_GRAPHICS
#define LIB_GRAPHICS

#define LIB_FALSE 0
#define LIB_TRUE 1

#define LIB_BeginRender LIB_SetEvents(); LIB_ClearToColor
#define LIB_EndRender LIB_SwapWindowBuffers

/* Define other things... */

typedef const char* LIB_String;
typedef unsigned LIB_Integer;
typedef char LIB_Char;
typedef int LIB_Bool;

/* Define the structures... */

typedef struct LIB_Window LIB_Window;

typedef struct LIB_Color
{
int r;
int g;
int b;
int a;
} LIB_Color;

#ifndef LIB_EXPORTS
#define LIB_EXPORTS _declspec(dllexport)
#else
#define LIB_EXPORTS _declspec(dllimport)
#endif

/* Constructors, destructors and other functions... */

LIB_EXPORTS LIB_Bool LIB_Initialize();
LIB_EXPORTS void LIB_SetEvents();
LIB_EXPORTS void LIB_ClearToColor(LIB_Color color);
LIB_EXPORTS void LIB_SetFrameColor(LIB_Color color);


LIB_EXPORTS LIB_Window* LIB_CreateWindow(LIB_String title, int x, int y, int width, int height, LIB_Bool resizable, LIB_Bool fullscreen);
LIB_EXPORTS void LIB_GetDisplaySize(int *width, int *height);

LIB_EXPORTS void LIB_GetCenterPosition(LIB_Window* window, int *x, int *y);
LIB_EXPORTS void LIB_GetWindowFrameSize(LIB_Window* window, int *width, int *height);
LIB_EXPORTS void LIB_GetWindowCursorPosition(LIB_Window* window, float *x, float *y);

LIB_EXPORTS void LIB_GetWindowPosition(LIB_Window* window, int *x, int *y);
LIB_EXPORTS void LIB_GetWindowSize(LIB_Window* window, int *width, int *height);
LIB_EXPORTS LIB_String LIB_GetWindowTitle(LIB_Window* window);
LIB_EXPORTS LIB_Bool LIB_IsWindowFullScreen(LIB_Window* window);
LIB_EXPORTS LIB_Bool LIB_IsWindowOpened(LIB_Window* window);
LIB_EXPORTS void LIB_SwapWindowBuffers(LIB_Window* window);

LIB_EXPORTS void LIB_SetWindowPosition(LIB_Window* window, int x, int y);
LIB_EXPORTS void LIB_SetWindowSize(LIB_Window* window, int width, int height);
LIB_EXPORTS void LIB_SetWindowTitle(LIB_Window* window, LIB_String title);
LIB_EXPORTS void LIB_SetFullScreenState(LIB_Window* window, LIB_Bool fullscreen);

LIB_EXPORTS void LIB_DestroyWindow(LIB_Window* window);


LIB_EXPORTS void LIB_Terminate();

#endif /* LIB_GRAPHICS */

最佳答案

你的代码没有任何意义。您使用 GLFWwindow* 成员声明一个 struct LIB_Window,并在 LIB_CreateWindow() 中创建一个本地 struct LIB_Window 变量您永远不会将实际窗口分配给结构成员,然后,您将地址返回到堆栈上的变量,该地址在函数离开时变得无效(并且某些代码路径仅使用return; 根本没有值。)。

此代码在 C 端完全损坏,您的问题与 DLL 或 OpenGL 无关。

关于c - OpenGL 在使用使用它制作的 dll 时给出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48369146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com