【发布时间】:2018-01-21 16:41:03
【问题描述】:
我正在使用 OpenGL 用 C 语言制作一个库。它除了创建一个窗口之外什么都不做,但是当我在示例程序上使用它时,它总是崩溃并给出异常:--
'抛出异常:写访问冲突'
'"win" 为 nullptr'
但是当我将它用作标题并将其直接链接到示例程序而没有任何外部链接时,它可以正常工作。是不是我错过了 OpenGL 不允许我用它制作任何库和 dll 的东西。我正在尝试用它制作自己的框架。
我也在使用 GLFW 和 GLEW。
编辑:
window.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();
}
window.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 */
【问题讨论】:
-
这听起来与部署有关。您是否检查以确保应用程序在运行时正确链接(静态库包含在您的 exe 中并且动态库可访问)?您肯定需要特定于平台的 dll 来运行应用程序。
-
我的另一个想法是,您需要确保您没有尝试从多个线程使用 Opengl 上下文而不使其成为当前的。也许上传一些相关的代码和部署细节。很难猜出问题出在哪里。