Files
stgsharp/实验/contexttest/StgSharpC/StgSharpC.c
T

126 lines
3.0 KiB
C

// StgShqrpGraphic.cpp : 定义 DLL 的导出函数。
//
#include "./include/ssgc_framework.h"
#include "./include/glad_gl.h"
#include "./include/glfw3.h"
#include "./include/StgSharpC.h"
#include <stdio.h>
GladGLContext* currentContext;
extern char* infolog[512] = { 0 };
void FrameBufferSizeCallback(int x, int y, int height, int width);
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(
800, 600, "Test Context", NULL, NULL
);
glfwMakeContextCurrent(window);
currentContext = malloc(sizeof(GladGLContext));
gladLoadGLContext(currentContext, glfwGetProcAddress);
glfwSetFramebufferSizeCallback(window, FrameBufferSizeCallback);
while (glfwWindowShouldClose(window))
{
currentContext->Clear(GL_COLOR_BUFFER_BIT | GLFW_DEPTH_BITS);
currentContext->ClearColor(0.5f, 0.3f, 0.6f, 1.0f);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
void FrameBufferSizeCallback(int x, int y, int height, int width)
{
currentContext->Viewport(0, 0, height, width);
}
void printShaderCode(char* const* code)
{
printf(*code);
}
SSCAPI void _cdecl initGL(int majorVersion, int minorVersion)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, majorVersion);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minorVersion);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
}
SSCAPI unsigned int loadShaderCode(GladGLContext* context, char* const shaderCode, GLint shadertype)
{
if (currentContext != NULL)
{
unsigned int shader = context->CreateShader(shadertype);
context->ShaderSource(shader, 1, &shaderCode, NULL);
context->CompileShader(shader);
int success;
context->GetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
context->GetShaderInfoLog(shader, 512, NULL, infolog);
infolog[511] = '\0';
return 0;
}
else
{
return shader;
}
}
else
{
return 0;
}
}
SSCAPI unsigned int linkShaderProgram(GladGLContext* context, GLuint shaderProgram)
{
context->LinkProgram(shaderProgram);
int success;
context->GetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success)
{
context->GetProgramInfoLog(shaderProgram, 512, NULL, infolog);
return 0;
}
else
{
return shaderProgram;
}
}
SSCAPI void SSCDECL loadImageData(char* location, Image* out, imageLoader loader)
{
if (loader == NULL)
{
loader = stbi_load;
}
out->pixelPtr = loader(location, &(out->width), &(out->height), &(out->channel), 0);
}
SSCAPI void SSCDECL unloadImageData(Image* out)
{
free(out->pixelPtr);
}
char* _cdecl readLog()
{
infolog[511] = '\0';
return &infolog;
}