Skip to content

Commit

Permalink
Started with C99 wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasBanana committed Aug 2, 2023
1 parent 38cb8b8 commit c14d473
Show file tree
Hide file tree
Showing 76 changed files with 6,774 additions and 5 deletions.
40 changes: 35 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ set(EXECUTABLE_OUTPUT_PATH ${OUTPUT_DIR} CACHE PATH "Build directory" FORCE)
set(LIBRARY_OUTPUT_PATH ${OUTPUT_DIR} CACHE PATH "Build directory" FORCE)
set(PROJECT_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include")
set(EXAMPLE_PROJECTS_DIR "${PROJECT_SOURCE_DIR}/examples/Cpp")
set(EXAMPLE_C99_PROJECTS_DIR "${PROJECT_SOURCE_DIR}/examples/C99")


# === Preprocessor definitions ===
Expand Down Expand Up @@ -98,6 +99,16 @@ macro(ADD_EXAMPLE_PROJECT TEST_NAME TEST_FILES LIB_FILES)
set_target_properties(${TEST_NAME} PROPERTIES LINKER_LANGUAGE CXX DEBUG_POSTFIX "D")
endmacro()

macro(ADD_C99_EXAMPLE_PROJECT TEST_NAME TEST_FILES LIB_FILES)
if(APPLE)
add_executable(${TEST_NAME} MACOSX_BUNDLE ${TEST_FILES})
else()
add_executable(${TEST_NAME} ${TEST_FILES})
endif()
target_link_libraries(${TEST_NAME} ${LIB_FILES})
set_target_properties(${TEST_NAME} PROPERTIES LINKER_LANGUAGE C DEBUG_POSTFIX "D")
endmacro()

macro(ADD_MOBILE_EXAMPLE_PROJECT TEST_NAME TEST_FILES LIB_FILES)
if(LLGL_IOS_PLATFORM)
add_executable(${TEST_NAME} MACOSX_BUNDLE ${TEST_FILES})
Expand Down Expand Up @@ -195,6 +206,8 @@ if(WIN32)
option(LLGL_BUILD_WRAPPER_CSHARP "Include wrapper for C#" OFF)
endif()

option(LLGL_BUILD_WRAPPER_C99 "Include wrapper for C99" OFF)

if(LLGL_ENABLE_CHECKED_CAST)
ADD_DEBUG_DEFINE(LLGL_ENABLE_CHECKED_CAST)
endif()
Expand Down Expand Up @@ -731,6 +744,15 @@ set(
${FilesRendererDXCommon}
)

# Wrapper: C99
if(LLGL_BUILD_WRAPPER_C99)
file(GLOB FilesWrapperC99 ${PROJECT_SOURCE_DIR}/wrapper/C99/*.*)
file(GLOB FilesIncludeC99 ${PROJECT_INCLUDE_DIR}/LLGL-C/*.*)
source_group("Wrapper\\C99\\Include" FILES ${FilesIncludeC99})
source_group("Wrapper\\C99\\Sources" FILES ${FilesWrapperC99})
set(FilesLLGL ${FilesLLGL} ${FilesIncludeC99} ${FilesWrapperC99})
endif()

# Base project
if(LLGL_BUILD_STATIC_LIB)
set(SUMMARY_LIBRARY_TYPE "Static")
Expand Down Expand Up @@ -978,14 +1000,14 @@ if(GaussLib_INCLUDE_DIR)
add_library(android_native_app_glue STATIC ${FilesAndroidNativeAppGlue})
target_link_libraries(ExampleBase android_native_app_glue)
include_directories(${ANDROID_APP_GLUE_DIR})
endif()
endif(LLGL_ANDROID_PLATFORM)

set(EXAMPLE_PROJECT_LIBS ${LLGL_DEPENDENCIES} ExampleBase)

if(LLGL_ANDROID_PLATFORM)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
ADD_MOBILE_EXAMPLE_PROJECT(Example_HelloAndroid "${FilesExample_HelloAndroid}" "${EXAMPLE_PROJECT_LIBS}")
else()
else(LLGL_ANDROID_PLATFORM)
ADD_EXAMPLE_PROJECT(Example_HelloTriangle "${FilesExample_HelloTriangle}" "${EXAMPLE_PROJECT_LIBS}")
ADD_EXAMPLE_PROJECT(Example_Tessellation "${FilesExample_Tessellation}" "${EXAMPLE_PROJECT_LIBS}")
ADD_EXAMPLE_PROJECT(Example_Texturing "${FilesExample_Texturing}" "${EXAMPLE_PROJECT_LIBS}")
Expand All @@ -1009,8 +1031,12 @@ if(GaussLib_INCLUDE_DIR)
ADD_EXAMPLE_PROJECT(Example_VolumeRendering "${FilesExample_VolumeRendering}" "${EXAMPLE_PROJECT_LIBS}")
ADD_EXAMPLE_PROJECT(Example_PrimitiveRestart "${FilesExample_PrimitiveRestart}" "${EXAMPLE_PROJECT_LIBS}")
ADD_EXAMPLE_PROJECT(Example_ResourceBinding "${FilesExample_ResourceBinding}" "${EXAMPLE_PROJECT_LIBS}")
endif()
endif()

if (LLGL_BUILD_WRAPPER_C99)
ADD_C99_EXAMPLE_PROJECT(Example_C99_HelloTriangle "${EXAMPLE_C99_PROJECTS_DIR}/HelloTriangle/HelloTriangle.c" "${LLGL_DEPENDENCIES}")
endif(LLGL_BUILD_WRAPPER_C99)
endif(LLGL_ANDROID_PLATFORM)
endif(LLGL_BUILD_EXAMPLES)
else()
# if(LLGL_BUILD_TESTS)
# message(SEND_ERROR "LLGL_BUILD_TESTS is enabled but 'GaussLib_INCLUDE_DIR' path is missing")
Expand All @@ -1022,7 +1048,7 @@ endif()

# Wrapper: C#
if(WIN32 AND LLGL_BUILD_WRAPPER_CSHARP)
add_subdirectory(Wrapper/CSharp)
add_subdirectory(wrapper/CSharp)
endif()

# Install targets, headers, and CMake config files
Expand Down Expand Up @@ -1080,6 +1106,10 @@ if(LLGL_BUILD_RENDERER_DIRECT3D12)
message("Build Renderer: Direct3D 12.0")
endif()

if(LLGL_BUILD_WRAPPER_C99)
message("Build Wrapper: C99")
endif()

if(WIN32 AND LLGL_BUILD_WRAPPER_CSHARP)
message("Build Wrapper: C#")
endif()
Expand Down
190 changes: 190 additions & 0 deletions examples/C99/HelloTriangle/HelloTriangle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* HelloTriangle.c
*
* Copyright (c) 2015 Lukas Hermanns. All rights reserved.
* Licensed under the terms of the BSD 3-Clause license (see LICENSE.txt).
*/

#include <LLGL-C/LLGL.h>
#include <stdio.h>


#define ENABLE_MULTISAMPLING 1

int main(int argc, char* argv[])
{
// Load render system module
const char* rendererModule = "OpenGL";
if (llglLoadRenderSystem(rendererModule) == 0)
{
fprintf(stderr, "Failed to load render system: %s\n", rendererModule);
return 1;
}

// Create swap-chain
LLGLSwapChainDescriptor swapChainDesc =
{
.resolution = { 800, 600 },
.colorBits = 32,
.depthBits = 0, // We don't need a depth buffer for this example
.stencilBits = 0, // We don't need a stencil buffer for this example
#if ENABLE_MULTISAMPLING
.samples = 8, // check if LLGL adapts sample count that is too high
#endif
};
LLGLSwapChain swapChain = llglCreateSwapChain(&swapChainDesc);

// Enable V-sync
llglSetVsyncInterval(swapChain, 1);

// Set window title and show window
LLGLSurface surface = llglGetSurface(swapChain);
LLGLWindow window = LLGL_GET_AS(LLGLWindow, surface);

llglSetWindowTitle(window, L"LLGL C99 Example: Hello Triangle");
llglShowWindow(window, true);

// Vertex data structure
typedef struct Vertex
{
float position[2];
uint8_t color[4];
}
Vertex;

// Vertex data (3 vertices for our triangle)
const float s = 0.5f;

Vertex vertices[] =
{
{ { 0, s }, { 255, 0, 0, 255 } }, // 1st vertex: center-top, red
{ { s, -s }, { 0, 255, 0, 255 } }, // 2nd vertex: right-bottom, green
{ { -s, -s }, { 0, 0, 255, 255 } }, // 3rd vertex: left-bottom, blue
};

// Vertex format with 2D floating-point vector for position and 4D byte vector for color
LLGLVertexAttribute vertexAttributes[2] =
{
{ .name = "position", .format = LLGLFormatRG32Float, .location = 0, .offset = 0, .stride = sizeof(Vertex) },
{ .name = "color", .format = LLGLFormatRGBA8UNorm, .location = 1, .offset = offsetof(Vertex, color), .stride = sizeof(Vertex) },
};

// Create vertex buffer
LLGLBufferDescriptor vertexBufferDesc =
{
.size = sizeof(vertices), // Size (in bytes) of the vertex buffer
.bindFlags = LLGLBindVertexBuffer, // Enables the buffer to be bound to a vertex buffer slot
.numVertexAttribs = 2,
.vertexAttribs = vertexAttributes, // Vertex format layout
};
LLGLBuffer vertexBuffer = llglCreateBuffer(&vertexBufferDesc, vertices);

// Create shaders
LLGLShaderDescriptor vertShaderDesc = { .type = LLGLShaderTypeVertex, .source = "HelloTriangle.vert", .sourceType = LLGLShaderSourceTypeCodeFile, };
LLGLShaderDescriptor fragShaderDesc = { .type = LLGLShaderTypeFragment, .source = "HelloTriangle.frag", .sourceType = LLGLShaderSourceTypeCodeFile, };

// Specify vertex attributes for vertex shader
vertShaderDesc.vertex.numInputAttribs = 2;
vertShaderDesc.vertex.inputAttribs = vertexAttributes;

LLGLShader shaders[2] =
{
llglCreateShader(&vertShaderDesc),
llglCreateShader(&fragShaderDesc),
};

for (int i = 0; i < 2; ++i)
{
LLGLReport shaderReport = llglGetShaderReport(shaders[i]);
if (llglHasReportErrors(shaderReport))
{
fprintf(stderr, "%s\n", llglGetReportText(shaderReport));
return 1;
}
}

// Create graphics pipeline
LLGLGraphicsPipelineDescriptor pipelineDesc =
{
.vertexShader = shaders[0],
.fragmentShader = shaders[1],
.renderPass = llglGetRenderTargetRenderPass(LLGL_GET_AS(LLGLRenderTarget, swapChain)),
.primitiveTopology = LLGLPrimitiveTopologyTriangleList,
#if ENABLE_MULTISAMPLING
.rasterizer = { .multiSampleEnabled = (swapChainDesc.samples > 1) },
#endif
.blend.targets[0].colorMask = LLGLColorMaskAll,
};

// Create graphics PSO
LLGLPipelineState pipeline = llglCreateGraphicsPipelineState(&pipelineDesc);

// Link shader program and check for errors
LLGLReport pipelineReport = llglGetPipelineStateReport(pipeline);
if (llglHasReportErrors(pipelineReport))
{
fprintf(stderr, "%s\n", llglGetReportText(pipelineReport));
return 1;
}

// Create command buffer to submit subsequent graphics commands to the GPU
LLGLCommandBufferDescriptor cmdBufferDesc =
{
.flags = LLGLCommandBufferImmediateSubmit,
.numNativeBuffers = 2,
};
LLGLCommandBuffer cmdBuffer = llglCreateCommandBuffer(&cmdBufferDesc);

// Initialize frame constants
const LLGLViewport viewport =
{
.x = 0.0f,
.y = 0.0f,
.width = (float)swapChainDesc.resolution.width,
.height = (float)swapChainDesc.resolution.height,
.minDepth = 0.0f,
.maxDepth = 1.0f
};

const LLGLClearValue clearColor =
{
.color = { 0.1f, 0.1f, 0.2f, 1.0f },
};

// Enter main loop
while (llglProcessSurfaceEvents(surface))
{
// Begin recording commands
llglBegin(cmdBuffer);
{
// Set viewport and scissor rectangle
llglSetViewport(&viewport);

// Set vertex buffer
llglSetVertexBuffer(vertexBuffer);

// Set the swap-chain as the initial render target
llglBeginRenderPass(LLGL_GET_AS(LLGLRenderTarget, swapChain));
{
// Clear color buffer
llglClear(LLGLClearColor, &clearColor);

// Set graphics pipeline
llglSetPipelineState(pipeline);

// Draw triangle with 3 vertices
llglDraw(3, 0);
}
llglEndRenderPass();
}
llglEnd();

// Present the result on the screen
llglPresent(swapChain);
}

// Clean up
llglUnloadRenderSystem();

return 0;
}
11 changes: 11 additions & 0 deletions examples/C99/HelloTriangle/HelloTriangle.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// GLSL shader version 1.30 (for OpenGL 3.1)
#version 130

in vec4 vColor;

out vec4 outColor;

void main()
{
outColor = vColor;
}
Binary file added examples/C99/HelloTriangle/HelloTriangle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions examples/C99/HelloTriangle/HelloTriangle.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// GLSL shader version 1.30 (for OpenGL 3.1)
#version 130

in vec2 position;
in vec4 color;

out vec4 vColor;

void main()
{
gl_Position = vec4(position, 0, 1);
vColor = color;
}
14 changes: 14 additions & 0 deletions examples/C99/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# C99 Examples

This folder contains all LLGL examples written in the C programming language (ISO-C99).
Note that you need to set the **working directory** to `<Your-LLGL-Repository>/examples/C99/<Example>` in order to get your examples running.
For the first example, this could be `/Users/JohnDoe/LLGL/examples/C99/HelloTriangle` or `C:\Users\JohnDoe\LLGL\examples\C99\HelloTriangle` for instance.

## Examples

### [Hello Triangle](HelloTriangle)

This example illustrates how to interact with LLGL in the C programing language.

<p align="center"><img src="HelloTriangle/HelloTriangle.png" width="400" height="300"/></p>

25 changes: 25 additions & 0 deletions include/LLGL-C/Buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Buffer.h
*
* Copyright (c) 2015 Lukas Hermanns. All rights reserved.
* Licensed under the terms of the BSD 3-Clause license (see LICENSE.txt).
*/

#ifndef LLGL_C99_BUFFER_H
#define LLGL_C99_BUFFER_H


#include <LLGL-C/Export.h>
#include <LLGL-C/Types.h>
#include <LLGL-C/BufferFlags.h>


LLGL_C_EXPORT long llglGetBufferBindFlags(LLGLBuffer buffer);
LLGL_C_EXPORT void llglGetBufferDesc(LLGLBuffer buffer, LLGLBufferDescriptor* outDesc);


#endif



// ================================================================================
Loading

0 comments on commit c14d473

Please sign in to comment.