summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrique Alves <henrique.alves@itsjungle.xyz>2024-04-22 10:32:24 +0300
committerHenrique Alves <henrique.alves@itsjungle.xyz>2024-04-22 10:32:24 +0300
commitcef05d32d6a8e38d8d84a5a7efac4494b00f1ce9 (patch)
treec8275c6c573deca60efa14a96f26d7c7a37847d3
parenta73b626d98e331605fdfd293ab4c006a84cfd8b8 (diff)
downloadgamejam-slgj-2024-cef05d32d6a8e38d8d84a5a7efac4494b00f1ce9.tar.gz
gamejam-slgj-2024-cef05d32d6a8e38d8d84a5a7efac4494b00f1ce9.tar.bz2
gamejam-slgj-2024-cef05d32d6a8e38d8d84a5a7efac4494b00f1ce9.zip
Testing texture API
-rw-r--r--CMakeLists.txt5
-rw-r--r--scripts/main.scm21
-rw-r--r--sources/main.c28
-rw-r--r--sources/text.h12
-rw-r--r--sources/texture.h48
5 files changed, 92 insertions, 22 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 994ea69..2bb8e46 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -37,7 +37,6 @@ target_compile_definitions(${PROJECT_NAME} PUBLIC ASSETS_PATH="./assets/")
target_compile_definitions(${PROJECT_NAME} PUBLIC SCRIPTS_PATH="./scripts/")
add_custom_command(
- TARGET ${CMAKE_PROJECT_NAME} PRE_BUILD
- COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/assets $<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>/assets
- COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/scripts $<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>/scripts
+ TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
+ COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>/${CMAKE_PROJECT_NAME} ${CMAKE_SOURCE_DIR}/${CMAKE_PROJECT_NAME}
)
diff --git a/scripts/main.scm b/scripts/main.scm
index 779a351..1900911 100644
--- a/scripts/main.scm
+++ b/scripts/main.scm
@@ -1,11 +1,16 @@
(define a 0)
+(define b 0)
-(define update
- (lambda ()
- (set! a (+ a 1))
- ))
+(define (update)
+ (set! a (+ a 1))
+ (set! b (rl-load-texture "./assets/test.png"))
+ ;; Not displaying the next lines makes the crash take longer to happen
+ (display a)
+ (newline)
+ (display b)
+ (newline)
+ )
-(define draw
- (lambda ()
- (rl-draw-text (string-append (number->string a) " oi "))
- ))
+(define (draw)
+ (newline)
+ )
diff --git a/sources/main.c b/sources/main.c
index 9dcaad1..b9f702f 100644
--- a/sources/main.c
+++ b/sources/main.c
@@ -1,21 +1,17 @@
#include "raylib.h"
+#include "text.h"
+#include "texture.h"
+
#include <libguile.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
-
-static SCM rl_draw_text(SCM text)
-{
- scm_t_string_failed_conversion_handler handler;
- char* str = scm_to_stringn(text, NULL, NULL, handler);
- DrawText(str, 200, 80, 20, RED);
-}
-
static void* game (void* data)
{
- scm_c_define_gsubr ("rl-draw-text", 1, 0, 0, &rl_draw_text);
+ //rl_text_define_methods();
+ rl_texture_define_methods();
const int screen_width = 800;
const int screen_height = 600;
@@ -32,12 +28,22 @@ static void* game (void* data)
while (!WindowShouldClose())
{
- scm_call_0(guile_update);
+ scm_call_with_blocked_asyncs(guile_update);
+ //scm_call_0(guile_update);
BeginDrawing();
ClearBackground(RAYWHITE);
- scm_call_0(guile_draw);
+ scm_call_with_blocked_asyncs(guile_draw);
+ //scm_call_0(guile_draw);
EndDrawing();
+
+ // This doesn't change anything
+ // SCM_TICK;
+
+ // This line makes memory manageable - until it crashes again (takes more time to crash though)
+ // scm_run_finalizers();
+ // This crashes after just a couple of frames
+ // scm_gc();
}
CloseWindow();
diff --git a/sources/text.h b/sources/text.h
new file mode 100644
index 0000000..2f2584a
--- /dev/null
+++ b/sources/text.h
@@ -0,0 +1,12 @@
+#include "raylib.h"
+#include <libguile.h>
+
+static void rl_draw_text(SCM text) {
+ scm_t_string_failed_conversion_handler handler;
+ char* str = scm_to_stringn(text, NULL, NULL, handler);
+ DrawText(str, 200, 80, 20, RED);
+}
+
+static void rl_text_define_methods() {
+ scm_c_define_gsubr ("rl-draw-text", 1, 0, 0, rl_draw_text);
+}
diff --git a/sources/texture.h b/sources/texture.h
new file mode 100644
index 0000000..f7bd3c3
--- /dev/null
+++ b/sources/texture.h
@@ -0,0 +1,48 @@
+#include "raylib.h"
+#include <libguile.h>
+#include <stdio.h>
+
+struct texture_2d {
+ Texture2D texture;
+};
+
+static SCM texture_2d_type;
+
+static void finalize_texture_2d(SCM texture_2d_scm) {
+ struct texture_2d *texture_2d;
+ texture_2d = scm_foreign_object_ref(texture_2d_scm, 0);
+ UnloadTexture(texture_2d->texture);
+}
+
+static void init_texture_2d_type (void) {
+ SCM name, slots;
+ scm_t_struct_finalize finalizer;
+
+ name = scm_from_utf8_symbol("texture_2d");
+ slots = scm_list_1(scm_from_utf8_symbol("data"));
+ finalizer = finalize_texture_2d;
+
+ texture_2d_type = scm_make_foreign_object_type(name, slots, finalizer);
+}
+
+static SCM rl_load_texture(SCM file_name_scm) {
+ scm_t_string_failed_conversion_handler handler;
+ char* file_name = scm_to_stringn(file_name_scm, NULL, NULL, handler);
+
+ struct texture_2d *new_texture_2d;
+
+ // Tested different mallocs, didn't work
+ /* new_texture_2d = (struct texture_2d *) scm_malloc(sizeof (struct texture_2d)); */
+ new_texture_2d = (struct texture_2d *) scm_gc_malloc(sizeof (struct texture_2d), "texture_2d");
+
+ new_texture_2d->texture = LoadTexture(file_name);
+
+ return scm_make_foreign_object_1(texture_2d_type, new_texture_2d);
+}
+
+static void rl_texture_define_methods() {
+ init_texture_2d_type();
+
+ scm_c_define_gsubr ("rl-load-texture", 1, 0, 0, rl_load_texture);
+}
+