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 24, 2023
1 parent e9d07c0 commit 184bcc7
Show file tree
Hide file tree
Showing 17 changed files with 889 additions and 280 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
66 changes: 66 additions & 0 deletions MAC_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Schnitzel Motor Engine: Mac Build Notes

The goal in bring the Schnitzel Motor Engine to the Mac platform was to provide a seamless and consistent integration with the supported platforms, focusing on simplicity and foundational elements that make this engine a terrific choice for students learning to write games in C++ for the first time. This document details the technical considerations, challenges, and rationale we encountered and the solution we settled upon.

## OpenGL Support on macOS

The Schnitzel Motor Engine utilizes OpenGL 4.1, supported by Apple up to macOS 10.11 Mojave. Key factors influenced this choice:

- **Ease of Adoption**: OpenGL 4.1 is native to macOS from version 10.11 Mojave onwards, eliminating additional software installations. Including necessary headers and libraries is straightforward.
- **Portability**: Transitioning the OpenGL Schnitzel Motor Engine to macOS necessitates minimal code alterations, ensuring efficiency.
- **Beginner-Friendly**: OpenGL is an intuitive graphics API, suitable for those new to game development.

However, there are limitations:
- **Deprecation**: OpenGL 4.1 is deprecated, risking removal in future macOS versions.
- **Apple's Metal Recommendation**: Apple advocates Metal for graphics rendering on macOS and iOS, hinting at a potential phase-out of OpenGL support.

## OpenGL Implementation Challenges on Mac

Support for OpenGL 4.3 and above is absent on macOS, requiring reliance on OpenGL 4.1. This alignment allows progression with minimal codebase adjustments, albeit with certain missing features:

- **Shader Storage Buffer Objects (SSBOs)**: Enable shaders to read from and write to buffer objects.
- **Debug Output**: Furnishes debugging and performance information from the OpenGL driver.
- **Texture Views**: Permit 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 for robust engine 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 certain missing features. Various strategies ensured a successful transition with performance remaining close to the original version.

### 1. **Buffer Management**:
- The `USE_OPENGL410` directive segregates buffer data handling for OpenGL 4.1 and 4.3.
- 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 absent in OpenGL 4.1, requiring a different buffer management strategy.

### **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 approach in OpenGL 4.3.
- **Shader Compilation**: Consistent shader compilation and error handling across both versions aids in debugging and ensures shader correctness.

## Solution

We opted for OpenGL as it aligns with the graphics API used on Windows and Linux, despite certain caveats on Mac due to its limitation to OpenGL 4.1 and the API's deprecation on this platform.

### **Pros**:
- **Performance**: Employed strategies upheld performance despite OpenGL 4.1 restrictions.
- **Learning Opportunity**: Porting shader code across OpenGL versions enlightens on the API's evolution.

Due to the absence of SSBOs in OpenGL 4.1, we opted for Vertex Buffer Objects (VBO) due to their speed, performance, simplicity, and alignment with our educational goals. VBOs, present since OpenGL 1.5, cater to our engine's needs efficiently. While the Schnitzel Motor Engine minimizes third-party library dependencies, we employed GLFW for its robust window management and streamlined OpenGL setup. GLFW simplifies build configurations, enabling a consistent build process across macOS, Windows, and Linux using the same "Single Compilation Unit" (SCU).

## Summary

The utilization of OpenGL 4.1 for the Schnitzel Motor Engine on macOS is driven by its ease of adoption, portability, and beginner-friendly nature, aligning with our educational goals. The deprecation of OpenGL 4.1 and the absence of certain features necessitate alternative strategies for robust engine operation. The use of GLFW enables a streamlined OpenGL setup on macOS, aligning with the engine's design principle of minimizing third-party dependencies.

Looking ahead, the aging OpenGL 4.1 may necessitate exploring other graphics APIs or projects to ensure the engine's longevity and adaptability to modern graphics programming standards.

### Resources
- [OpenGL Official Website](https://www.opengl.org/)
- [GLFW Official Website](https://www.glfw.org/)
- [Vulkan Official Website](https://www.khronos.org/vulkan/)
- [Metal - Apple Developer](https://developer.apple.com/metal/)
- [MGL Project on GitHub](https://github.com/openglonmetal/MGL)
- [Shader Storage Buffer Object - OpenGL Wiki](https://www.khronos.org/opengl/wiki/Shader_Storage_Buffer_Object)
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;
}
32 changes: 22 additions & 10 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defines="-DENGINE"
warnings="-Wno-writable-strings -Wno-format-security -Wno-c++11-extensions -Wno-deprecated-declarations"
includes="-Ithird_party"
timestamp=$(date +%s)
flags="-g"

if [[ "$(uname)" == "Linux" ]]; then
echo "Running on Linux"
Expand All @@ -13,35 +14,46 @@ if [[ "$(uname)" == "Linux" ]]; then

# fPIC position independent code
rm -f game_* # Remove old game_* files
clang++ -g "src/game.cpp" -shared -fPIC -o game_$timestamp.so $warnings $defines
clang++ $flags "src/game.cpp" -shared -fPIC -o game_$timestamp.so $warnings $defines
mv game_$timestamp.so game.so

elif [[ "$(uname)" == "Darwin" ]]; then
echo "Running on Mac"
libs="-framework Cocoa"
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"

# 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

flags="-g -std=c++14"
# Build Game Library
rm -f -R *.dylib *.dSYM # Remove old build files
clang++ $flags -dynamiclib "src/game.cpp" -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"
includes="-Ithird_party -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"
outputFile=schnitzel.exe
queryProcesses=$(tasklist | grep $outputFile)

rm -f game_* # Remove old game_* files
clang++ -g "src/game.cpp" -shared -o game_$timestamp.dll $warnings $defines
clang++ $flags "src/game.cpp" -shared -o game_$timestamp.dll $warnings $defines
mv game_$timestamp.dll game.dll
fi

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 184bcc7

Please sign in to comment.