Skip to content

Commit

Permalink
Mac Platform Integration changes for Schnitzel Motor Engine:
Browse files Browse the repository at this point in the history
- Extend build.sh to include Homebrew checks and GLFW, Freetype library paths on Mac.
- Remove old object files and build files during the build process.
- Add new quad shaders for OpenGL 4.1.
- Introduce separate README for Mac-specific instructions.
- Remove Linux-specific platform code in favor of a more unified approach.
- Refactor Mac platform integration, remove Objective-C dependency.
- Update README and .clang-format for better documentation and code style.
- Add .gitattributes for consistent Git configuration.
- Explicitly set C++ version requirement to c++14

renderer.cpp changes:
- Preprocessor Directives: Added #define USE_OPENGL410 to toggle between OpenGL 4.10 and earlier versions.
- Includes: Reordered includes for better organization.
- OpenGL Structs: Added #ifdef USE_OPENGL410 to include new IDs (vaoID, vboID) for OpenGL 4.10.
- Font Loading: Simplified assignments for glyph structures.
- Debug Callback: Wrapped the existing debug callback function with #ifndef __APPLE__.
- Shader Creation: Conditional compilation for shader source based on USE_OPENGL410.
- Link Errors: Added a function check_link_errors to check for shader program linking errors.
- Vertex Buffer Allocation: Added a new function create_vertex_array_buffer to create vertex array buffers.
- Initialize Vertex Array Buffers: Added a new function init_vertex_array_buffers to initialize vertex array and storage buffers conditionally based on USE_OPENGL410.
- OpenGL Initialization: Modified to use new functions for error checking and buffer initialization.
- Rendering: Modified to use VAO and VBO when USE_OPENGL410 is defined.
  • Loading branch information
Cakez77 authored and runstop committed Oct 23, 2023
1 parent e9d07c0 commit c424906
Show file tree
Hide file tree
Showing 17 changed files with 900 additions and 293 deletions.
33 changes: 33 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
ColumnLimit: 120
BasedOnStyle: LLVM
IndentWidth: 2
UseTab: false

IndentCaseLabels: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
AllowShortBlocksOnASingleLine: Never
MaxEmptyLinesToKeep: 1

PointerAlignment: Left
AllowShortFunctionsOnASingleLine: None
BinPackParameters: false
BinPackArguments: false
AlignAfterOpenBracket: Align
BreakConstructorInitializers: BeforeColon

---
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
18 changes: 18 additions & 0 deletions .github/workflows/mac.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: mac
on: [push, pull_request]

jobs:
build:
runs-on: macos-latest
env:
CC: clang
CXX: clang++
steps:
- name: install dependencies
run: |
brew update
brew install glfw3 freetype
- name: checkout
uses: actions/checkout@v3
- name: build
run: ./build.sh
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ build
.DS_Store
*.swp
*.dSYM/
*.dylib
*.so
*.xcuserstate
*.xcworkspace
Expand Down
62 changes: 62 additions & 0 deletions MAC_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Schnitzel Motor Engine: Mac Build Notes

The Schnitzel Motor Engine Platform Layer is in development to ensure smooth integration across various operating systems, with a focus on supporting OpenGL functionalities on macOS. This document outlines the technical considerations and rationale behind our implementation choices on the Mac platform.

## OpenGL Support on macOS

The Schnitzel Motor Engine utilizes OpenGL 4.1, the last version released by Apple until macOS 10.11 Mojave. Several factors influenced this choice:

- **Ease of Adoption**: OpenGL 4.1 is a core part of macOS from version 10.11 Mojave onwards, removing the need for additional software installations. The straightforward setup comes from the ease of including necessary headers and libraries.
- **Portability**: Transitioning the OpenGL Schnitzel Motor Engine to macOS requires minimal code alterations, making the process efficient.
- **Beginner-Friendly**: OpenGL is a relatively intuitive graphics API, making it a suitable choice for those new to game development.

Nonetheless, this decision comes with limitations:
- **Deprecation**: OpenGL 4.1 is deprecated, with the potential for removal in future macOS versions.
- **Apple's Metal Recommendation**: Apple recommends Metal for all graphics rendering on macOS and iOS, hinting at a possible end to OpenGL support in future macOS versions.

## OpenGL Implementation Challenges on Mac

The main challenge is the lack of support for OpenGL 4.3 and above on macOS, necessitating a reliance on OpenGL 4.1. Our engine's alignment with OpenGL 4.1 allows for progression with minimal codebase adjustments. However, several features available in OpenGL 4.3 are absent in 4.1, such as:

- **Shader Storage Buffer Objects (SSBOs)**: Allow shaders to read from and write to buffer objects.
- **Debug Output**: Provides debugging and performance information from the OpenGL driver.
- **Texture Views**: Allows creating multiple views of a texture.
- **Vertex Attribute Binding**: Manages binding between vertex attributes and buffer objects.

These missing features necessitate alternative approaches or extensions to ensure the engine's robust operation on OpenGL 4.1.

## Shader Adaptation from OpenGL 4.3 to 4.1
Transitioning the OpenGL 4.3 shader code to comply with OpenGL 4.1 standards posed challenges due to the absence of certain features in OpenGL 4.1. Various strategies were employed to ensure a successful transition, with the resulting shader code performing very closely to the original version in terms of performance.

### 1. **Buffer Management**:
- The `USE_OPENGL410` directive segregates the buffer data handling for OpenGL 4.1 and 4.3 in the provided code.
- For OpenGL 4.1:
- A Vertex Buffer Object (VBO) stores vertex data, with `glBufferData` allocating buffer space, and `glVertexAttribPointer` setting up attribute pointers for interleaved data.
- For OpenGL 4.3:
- A Shader Storage Buffer Object (SSBO) manages transform data, a feature not available in OpenGL 4.1, necessitating a different buffer management strategy.

### **Pros**:
- **Performance**: The strategies employed ensured performance remained very close to the original version despite OpenGL 4.1 limitations.
- **Learning Opportunity**: Porting shader code between different OpenGL versions offers insight into the OpenGL API's evolution and different versions' capabilities.

### **Cons**:
- **Unsupported Shader Development**: As modern OpenGL shader API calls are implemented, finding workarounds for missing shader functionality becomes challenging, possibly leading to certain OpenGL features being unsupported or only partially supported.

### **Key Differences**:
- **Buffer Management**: Use of VBOs in OpenGL 4.1 versus SSBOs in OpenGL 4.3 for handling buffer data.
- **Vertex Processing**: Explicit vertex data processing in OpenGL 4.1 compared to the more streamlined instanced rendering approach in OpenGL 4.3.
- **Shader Compilation**: Consistent shader compilation and error handling across both versions aid in debugging and ensure shader correctness.

## Rationale for Using GLFW

Minimizing reliance on third-party libraries is a key design principle of the Schnitzel Motor Engine, allowing those new to game development to grasp all game development process aspects without unnecessary obfuscation. Libraries like GLEW, GLFW, and SDL, while simplifying certain aspects, could potentially obscure crucial game development concepts.

## A Mac Future without OpenGL

If Apple phases out OpenGL from upcoming macOS releases, Vulkan and Metal become the two viable alternatives for the C++ graphics developer on macOS:

- **Vulkan**: Offers cross-platform support but demands a deeper understanding of the graphics pipeline, and lacks official backing from Apple. [More about Vulkan on macOS](#).
- **Metal**: Apple's native graphics API for macOS and iOS, highly recommended for graphics rendering on these platforms, though with a steeper learning curve compared to OpenGL. [More information on Metal](#).

OpenGL's beginner-friendly nature stands out, and the hope is for Apple to continue including OpenGL 4.1 in future macOS releases. However, as OpenGL 4.1 ages, a transition to Vulkan or Metal, or exploring other emerging options like MGL may become necessary.

52 changes: 47 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
# SchnitzelMotor
A crispy cross platform C/C++ engine. Supports Linux and Windows currently.
A crispy cross platform C/C++ engine. Supports Linux, Windows, and macOS.

# Building the engine (Windows)
1. Download and install clang from here https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.6/LLVM-16.0.6-win64.exe
# Building the Engine

## Windows Instructions:
1. Download and install clang from [here](https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.6/LLVM-16.0.6-win64.exe)
2. ![image](https://github.com/Cakez77/SchnitzelMotor/assets/45374095/1ad4bdf5-f43c-4774-9f34-5fcdd2ed7f2c)
3. Download and install git from here
3. Download and install git from [here](https://git-scm.com/download/win)
4. Add `sh.exe` to the path, you can find it here : `C:\Program Files\Git\bin`
5.

## Mac Instructions:
1. Ensure you have [Homebrew](https://brew.sh/) installed on your machine.
```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
2. Install dependencies using Homebrew:
```bash
brew update
brew install llvm glfw freetype
```
3. Clone the repository:
```bash
git clone https://github.com/Cakez77/SchnitzelMotor.git
cd SchnitzelMotor
```
4. Run the build script:
```bash
./build.sh
```

## GitHub Actions (macOS)
For automated builds on macOS using GitHub Actions, the provided `mac.yml` workflow file can be utilized. This workflow installs necessary dependencies, checks out the code, and runs the build script:
```yaml
name: mac
on: [push, pull_request]

jobs:
build:
runs-on: macos-latest
steps:
- name: install dependencies
run: |
brew update
brew install glfw3 freetype
- name: checkout
uses: actions/checkout@v3
- name: build
run: ./build.sh
```
For more information on GitHub Actions, refer to the [official documentation](https://docs.github.com/en/actions).
41 changes: 41 additions & 0 deletions assets/shaders/quad_41.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#version 410 core

// Constants
const int RENDERING_OPTION_FONT = (1 << 1);
const int RENDERING_OPTION_TRANSPARENT = (1 << 2);

// Input
layout (location = 0) in vec2 textureCoordsOut;
layout (location = 1) flat in int renderOptionsOut;

// Output
layout (location = 0) out vec4 fragColor;

// Uniforms (no binding)
uniform sampler2D textureAtlas;
uniform sampler2D fontAtlas;

void main()
{
vec4 textureColor;

if(bool(renderOptionsOut & RENDERING_OPTION_FONT))
{
textureColor = texelFetch(fontAtlas, ivec2(textureCoordsOut), 0);
if(textureColor.r == 0.0)
{
discard;
}
textureColor = vec4(1.0, 1.0, 1.0, 1.0); // Filling with white
}
else
{
textureColor = texelFetch(textureAtlas, ivec2(textureCoordsOut), 0);
if(textureColor.a == 0.0)
{
discard;
}
}

fragColor = textureColor;
}
61 changes: 61 additions & 0 deletions assets/shaders/quad_41.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#version 410 core

// Constants
const int RENDERING_OPTION_FLIP_X = (1 << 0);

// Input Uniforms
uniform vec2 screenSize;
uniform vec2 cameraPos;
uniform mat4 orthoProjection;

// Input Attributes from VBO
layout (location = 0) in vec2 inPosition;
layout (location = 1) in vec2 inSize;
layout (location = 2) in vec2 inAtlasOffset;
layout (location = 3) in vec2 inSpriteSize;
layout (location = 4) in int inRenderOptions;
layout (location = 5) in float inLayer;

// Output
layout (location = 0) out vec2 textureCoordsOut;
layout (location = 1) flat out int renderOptionsOut;

void main()
{
// Initialize vertices
vec2 vertices[6];
vertices[0] = inPosition;
vertices[1] = inPosition + vec2(0.0, inSize.y);
vertices[2] = inPosition + vec2(inSize.x, 0.0);
vertices[3] = inPosition + vec2(inSize.x, 0.0);
vertices[4] = inPosition + vec2(0.0, inSize.y);
vertices[5] = inPosition + inSize;

// Initialize textureCoords
float left = inAtlasOffset.x;
float top = inAtlasOffset.y;
float right = inAtlasOffset.x + inSpriteSize.x;
float bottom = inAtlasOffset.y + inSpriteSize.y;

if (bool(inRenderOptions & RENDERING_OPTION_FLIP_X))
{
float tmpLeft = left;
left = right;
right = tmpLeft;
}

vec2 textureCoords[6];
textureCoords[0] = vec2(left, top);
textureCoords[1] = vec2(left, bottom);
textureCoords[2] = vec2(right, top);
textureCoords[3] = vec2(right, top);
textureCoords[4] = vec2(left, bottom);
textureCoords[5] = vec2(right, bottom);

// Compute final positions and outputs
vec2 vertexPos = vertices[gl_VertexID];
gl_Position = orthoProjection * vec4(vertexPos, inLayer, 1.0);

textureCoordsOut = textureCoords[gl_VertexID];
renderOptionsOut = inRenderOptions;
}
28 changes: 20 additions & 8 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
defines="-DENGINE"
warnings="-Wno-writable-strings -Wno-format-security -Wno-c++11-extensions -Wno-deprecated-declarations"
includes="-Ithird_party"
timestamp=$(date +%s)
flags="-std=c++14 -g"
timestamp=$(date +%s)

if [[ "$(uname)" == "Linux" ]]; then
echo "Running on Linux"
Expand All @@ -18,13 +19,25 @@ if [[ "$(uname)" == "Linux" ]]; then

elif [[ "$(uname)" == "Darwin" ]]; then
echo "Running on Mac"
libs="-framework Cocoa"

# Check if Homebrew is installed
if ! command -v brew &> /dev/null; then
echo "Homebrew not found. Please install Homebrew and try again."
exit 1
fi

# Build Game Library
rm -f -R *.dylib *.dSYM # Remove old build files
clang++ -g "src/game.cpp" -shared -fPIC -o game_$timestamp.dylib $warnings $defines
mv game_$timestamp.dylib game.dylib
mv game_$timestamp.dylib.dSYM game.dylib.dSYM

# Game Engine Compiler Settings on Mac
HOMEBREW_CELLAR=${HOMEBREW_CELLAR:-/usr/local/Cellar} # Homebrew defaults to /usr/local/Cellar
libs="-framework Cocoa -framework OpenGL -L${HOMEBREW_CELLAR}/glfw/3.3.8/lib -lglfw -L${HOMEBREW_CELLAR}/freetype/2.13.2/lib -lfreetype"
sdkpath=$(xcode-select --print-path)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
includes="-Ithird_party -isysroot ${sdkpath} -I${sdkpath}/System/Library/Frameworks/Cocoa.framework/Headers"
objc_dep="src/mac_platform.m"
includes="-Ithird_party -isysroot ${sdkpath} -I${sdkpath}/System/Library/Frameworks/Cocoa.framework/Headers -I${HOMEBREW_CELLAR}/glfw/3.3.8/include -I${HOMEBREW_CELLAR}/freetype/2.13.2/include/freetype2"
outputFile=schnitzel
# clean up old object files
rm -f src/*.o
else
echo "Not running on Linux"
libs="-luser32 -lgdi32 -lopengl32 -lole32 -Lthird_party/lib -lfreetype.lib"
Expand All @@ -40,8 +53,7 @@ processRunning=$queryProcesses

if [ -z "$processRunning" ]; then
echo "Engine not running, building main..."
clang++ $includes -g "src/main.cpp" $objc_dep -o $outputFile $libs $warnings $defines
clang++ $includes $flags "src/main.cpp" -o $outputFile $libs $warnings $defines
else
echo "Engine running, not building!"
fi

Loading

0 comments on commit c424906

Please sign in to comment.