使用dll时,OpenGL会出现异常

时间:2018-01-21 16:41:03

标签: c exception opengl dll

我正在使用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 */

1 个答案:

答案 0 :(得分:0)

你的代码没有丝毫意义。您使用struct LIB_Window成员声明了GLFWwindow*,并在LIB_CreateWindow()中创建了一个本地struct LIB_Window变量,您从未将实际窗口分配给struct成员,然后返回堆栈上的变量的地址,当函数离开时变为无效(并且某些代码路径只使用return;而没有值。)。

这个代码在C方面完全破解,你的问题与DLL或OpenGL无关。