diff --git a/main/BridgingHeader.h b/main/BridgingHeader.h index 7967833..a3ade9f 100644 --- a/main/BridgingHeader.h +++ b/main/BridgingHeader.h @@ -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" diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 26eb924..56f35bd 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -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" ) @@ -62,4 +61,5 @@ enable_language(Swift) target_sources(${COMPONENT_LIB} PRIVATE Main.swift + FileSystem.swift ) diff --git a/main/FileSystem.swift b/main/FileSystem.swift new file mode 100644 index 0000000..4f8837f --- /dev/null +++ b/main/FileSystem.swift @@ -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") + } +} diff --git a/main/Main.swift b/main/Main.swift index aeb5776..6f60741 100644 --- a/main/Main.swift +++ b/main/Main.swift @@ -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 @@ -95,13 +89,12 @@ 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") } @@ -109,8 +102,7 @@ func sdl_thread_entry_point(arg: UnsafeMutableRawPointer?) -> UnsafeMutableRawPo 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") } @@ -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.. 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) diff --git a/main/assets.c b/main/assets.c deleted file mode 100644 index d01239c..0000000 --- a/main/assets.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include - -const char* getBmpFilePath(void) { - return "/assets/coin_gold.bmp"; -} - - -const char* getDangerFilePath(void) { - return "/assets/slime_normal.bmp"; -} - -const char* getFontFilePath(void) { - return "/assets/FreeSans.ttf"; -} - -// Function to get a random float between min and max -float getRandomFloat(float min, float max) { - float scale = rand() / (float) RAND_MAX; - return min + scale * (max - min); -} - -void logFloat( double value) { - printf("> %f\n", value); -} - -// Function to generate the score text -void getScoreText(int score, char* buffer, int bufferSize) { - snprintf(buffer, bufferSize, "SCORE %d", score); -} \ No newline at end of file diff --git a/main/filesystem.c b/main/filesystem.c deleted file mode 100644 index 25ea7cb..0000000 --- a/main/filesystem.c +++ /dev/null @@ -1,61 +0,0 @@ -#include "filesystem.h" -#include -#include "esp_vfs.h" -#include "esp_littlefs.h" - -void SDL_InitFS(void) { - printf("Initialising File System\n"); - - esp_vfs_littlefs_conf_t conf = { - .base_path = "/assets", - .partition_label = "assets", - .format_if_mount_failed = false, - .dont_mount = false, - }; - - esp_err_t err = esp_vfs_littlefs_register(&conf); - if (err != ESP_OK) { - printf("Failed to mount or format filesystem\n"); - } else { - printf("Filesystem mounted\n"); - listFiles("/assets"); - } -} - -void listFiles(const char *dirname) { - DIR *dir = opendir(dirname); - if (!dir) { - printf("Failed to open directory: %s\n", dirname); - return; - } - - struct dirent *entry; - while ((entry = readdir(dir)) != NULL) { - struct stat entry_stat; - char path[1024]; - snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name); - - if (stat(path, &entry_stat) == -1) { - printf("Failed to stat %s\n", path); - continue; - } - - if (S_ISDIR(entry_stat.st_mode)) { - printf("[DIR] %s\n", entry->d_name); - } else if (S_ISREG(entry_stat.st_mode)) { - printf("[FILE] %s (Size: %ld bytes)\n", entry->d_name, entry_stat.st_size); - } - } - - closedir(dir); -} - -void TestFileOpen(const char *file) { - SDL_IOStream *rw = SDL_IOFromFile(file, "rb"); - if (rw == NULL) { - printf("Failed to open file: %s\n", SDL_GetError()); - } else { - printf("File opened successfully.\n"); - SDL_CloseIO(rw); - } -} diff --git a/main/filesystem.h b/main/filesystem.h deleted file mode 100644 index 52ec816..0000000 --- a/main/filesystem.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef FILESYSTEM_H -#define FILESYSTEM_H - -#include "SDL3/SDL.h" - -void SDL_InitFS(void); -void listFiles(const char *dirname); -void TestFileOpen(const char *file); - -#endif // FILESYSTEM_H