Skip to content

Upgrade to Swift 6.1 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions main/BridgingHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,5 @@
#include "SDL3_ttf/SDL_ttf.h"
#include "pthread.h"
#include "bsp/esp-bsp.h"
#include "filesystem.h"

const char* getBmpFilePath(void);
const char* getDangerFilePath(void);
float getRandomFloat(float min, float max);
void logFloat(double value);
const char* getFontFilePath(void);
void getScoreText(int score, char* buffer, int bufferSize);
#include "esp_vfs.h"
#include "esp_littlefs.h"
4 changes: 2 additions & 2 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Register the app as an IDF component
idf_component_register(
SRCS
"assets.c"
"filesystem.c"
/dev/null
PRIV_INCLUDE_DIRS "."
LDFRAGMENTS "linker.lf"
)
Expand Down Expand Up @@ -62,4 +61,5 @@ enable_language(Swift)
target_sources(${COMPONENT_LIB}
PRIVATE
Main.swift
FileSystem.swift
)
22 changes: 22 additions & 0 deletions main/FileSystem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


func SDL_InitFS() {
print("Initializing File System")

var config = esp_vfs_littlefs_conf_t(
base_path: strdup("/assets"),
partition_label: strdup("assets"),
partition: nil, // Optional partition pointer; use nil if not needed
format_if_mount_failed: 0, // Convert false to UInt8 (0)
read_only: 0, // Use 0 (false) since it's not read-only
dont_mount: 0, // Convert false to UInt8 (0)
grow_on_mount: 0 // Convert false to UInt8 (0) if not needed
)

let result = esp_vfs_littlefs_register(&config)
if result != ESP_OK {
print("Failed to mount or format filesystem")
} else {
print("Filesystem mounted")
}
}
31 changes: 11 additions & 20 deletions main/Main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,12 @@ func pointInRect(x: Float, y: Float, rect: SDL_FRect) -> Bool {
y >= rect.y - margin && y <= rect.y + rect.h + margin
}


// Helper function to get random float (since Float.random(in:) may not be available)
func getRandomFloat_C(_ min: Float, _ max: Float) -> Float {
return getRandomFloat(min, max)
}

// Random number generation helper (since Float.random(in:) may not be available)
// Function to generate a random Float between min and max
func getRandomFloat(min: Float, max: Float) -> Float {
return getRandomFloat_C(min, max)
let scale = Float.random(in: 0...1)
return min + scale * (max - min)
}

var scoreTextBuffer = [CChar](repeating: 0, count: 30)
var scoreDestRect = SDL_FRect(x: 10.0, y: 10.0, w: 120.0, h: 50.0)
var score = 0

Expand Down Expand Up @@ -95,22 +89,20 @@ func sdl_thread_entry_point(arg: UnsafeMutableRawPointer?) -> UnsafeMutableRawPo
SDL_InitFS();

TTF_Init()
let font = TTF_OpenFont(getFontFilePath(), 42);
let font = TTF_OpenFont("/assets/FreeSans.ttf", 42);
if (font == nil) {
print("Font load failed")
}

// let bmpFilePath: StaticString = "assets/espressif.bmp"
let imageSurface = SDL_LoadBMP(getBmpFilePath())
let imageSurface = SDL_LoadBMP("/assets/coin_gold.bmp")
if (imageSurface == nil) {
print("Failed to load image")
}

let coinTexture = SDL_CreateTextureFromSurface(renderer, imageSurface);
SDL_DestroySurface(imageSurface);

// let bmpFilePath: StaticString = "assets/espressif.bmp"
let dangerSurface = SDL_LoadBMP(getDangerFilePath())
let dangerSurface = SDL_LoadBMP("/assets/slime_normal.bmp")
if (dangerSurface == nil) {
print("Failed to load image")
}
Expand All @@ -129,9 +121,6 @@ func sdl_thread_entry_point(arg: UnsafeMutableRawPointer?) -> UnsafeMutableRawPo
SDL_RenderTexture(renderer, coinTexture, nil, &scoreRect);
SDL_RenderPresent(renderer)

var xSpeed: Float = 2.0
var ySpeed: Float = 2.0

// Initialize coins
for _ in 0..<numberOfCoins {
var destRect = SDL_FRect()
Expand Down Expand Up @@ -240,14 +229,16 @@ func sdl_thread_entry_point(arg: UnsafeMutableRawPointer?) -> UnsafeMutableRawPo
}
}

// Update score
getScoreText(Int32(score), &scoreTextBuffer, 20)
let scoreText = "SCORE \(score)"

// Convert the string to a C-compatible null-terminated character buffer (CChar array)
var scoreTextBuffer = Array(scoreText.utf8CString)

// Render text to surface
let fontSurface = TTF_RenderText_Blended(font, &scoreTextBuffer, 0, SDL_Color(r: 40, g: 255, b: 40, a: 255))

// Create texture from surface
var scoreTexture = SDL_CreateTextureFromSurface(renderer, fontSurface)
let scoreTexture = SDL_CreateTextureFromSurface(renderer, fontSurface)
SDL_RenderTexture(renderer, scoreTexture, nil, &scoreDestRect)
SDL_DestroySurface(fontSurface)

Expand Down
30 changes: 0 additions & 30 deletions main/assets.c

This file was deleted.

61 changes: 0 additions & 61 deletions main/filesystem.c

This file was deleted.

10 changes: 0 additions & 10 deletions main/filesystem.h

This file was deleted.