aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornathansmith117 <nathansmith117@sdf.org>2025-02-04 11:29:07 +0000
committernathansmith117 <nathansmith117@sdf.org>2025-02-04 11:29:07 +0000
commitfe9b718f8978cfd792f7303214b2dd45172b8d4b (patch)
tree8efd867eae12ad5d55d29e964272974ee3d71b18
parentfb7ccc0c046ed80fdac2e829b8c367841600e211 (diff)
downloadsldj-fe9b718f8978cfd792f7303214b2dd45172b8d4b.tar.gz
sldj-fe9b718f8978cfd792f7303214b2dd45172b8d4b.tar.bz2
sldj-fe9b718f8978cfd792f7303214b2dd45172b8d4b.zip
Working on script context
-rw-r--r--CMakeLists.txt6
-rw-r--r--include/libsldj/util.h35
-rw-r--r--src/libsldj/util.c (renamed from src/util.c)0
-rw-r--r--src/main.c13
-rw-r--r--src/scripting.c1
-rw-r--r--src/scripting.h3
-rw-r--r--src/sldj.c22
-rw-r--r--src/sldj.h7
-rw-r--r--src/util.h13
-rw-r--r--test/scanTest1.c9
-rw-r--r--test/scanTest2.c12
11 files changed, 88 insertions, 33 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index bb6c55d..b8f1a57 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,10 +8,12 @@ set(C_STANDARD 99)
set(C_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
-file(GLOB SRC_FILES src/*.c)
+file(GLOB SRC_FILES src/*.c src/libsldj/*.c)
add_executable(${PROJECT_NAME} ${SRC_FILES})
-target_include_directories(${PROJECT_NAME} PUBLIC include src)
+target_include_directories(${PROJECT_NAME} PUBLIC include include/libsldj src)
target_link_libraries(${PROJECT_NAME} raylib m tcc)
+file(COPY include/libsldj DESTINATION ${CMAKE_BINARY_DIR})
+
diff --git a/include/libsldj/util.h b/include/libsldj/util.h
new file mode 100644
index 0000000..fd89d1c
--- /dev/null
+++ b/include/libsldj/util.h
@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#ifndef UTIL_H
+#define UTIL_H
+
+#define SET_BIT(b, n) (b | (0x1 << n))
+#define CLEAR_BIT(b, n) (b & ~(0x1 << n))
+#define IS_BIT_SET(b, n) ((b >> n) & 0x1)
+#define TOGGLE_BIT(b, n) (b ^ (0x1 << n))
+#define HAS_FLAG(v, f) ((v & f) == f)
+
+// Just the same color structure as the raylib name.
+#ifndef RL_COLOR_TYPE
+#define RL_COLOR_TYPE
+typedef struct Color {
+ unsigned char r;
+ unsigned char g;
+ unsigned char b;
+ unsigned char a;
+} Color;
+#endif
+
+// Used for sending information to a scanner.
+typedef struct SldjContext {
+ uint16_t viewportWidth;
+ uint16_t viewportHeight;
+ uint8_t targetFps;
+ uint16_t xCount;
+ uint16_t yCount;
+} SldjContext;
+
+#endif
+
diff --git a/src/util.c b/src/libsldj/util.c
index b7e445c..b7e445c 100644
--- a/src/util.c
+++ b/src/libsldj/util.c
diff --git a/src/main.c b/src/main.c
index a02b023..298c65b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,11 +1,20 @@
#include <raylib.h>
#include "sldj.h"
+#include "sldjConfig.h"
-int main()
+int main(int argc, char** argv)
{
Sldj sldj;
initSldj(&sldj);
-
+
+ // Add script from argument.
+ if (argc > 1)
+ {
+ strncpy(sldj.scriptFilepath, argv[1], SLDJ_NAMEMAX);
+ reloadScript(&sldj);
+ }
+
+ // Run update loop.
while (!WindowShouldClose())
{
updateSldj(&sldj);
diff --git a/src/scripting.c b/src/scripting.c
index f0a2582..e43f4b5 100644
--- a/src/scripting.c
+++ b/src/scripting.c
@@ -85,6 +85,7 @@ void compileScript(SldjScripting* scripting, const char filePath[SLDJ_NAMEMAX])
}
scripting->lineScanner = tcc_get_symbol(scripting->state, "lineScanner");
+ scripting->loadContext = tcc_get_symbol(scripting->state, "loadContext");
}
void closeScripting(SldjScripting* scripting)
diff --git a/src/scripting.h b/src/scripting.h
index fab64b2..2a7220a 100644
--- a/src/scripting.h
+++ b/src/scripting.h
@@ -11,16 +11,19 @@
#include <libtcc.h>
#include "sldjConfig.h"
+#include "libsldj/util.h"
#ifndef SCRIPTING_H
#define SCRIPTING_H
typedef Color (*LineScanner)(uint16_t x, uint16_t y, uint32_t frameNumber);
+typedef void (*LoadContext)(SldjContext context);
typedef struct SldjScripting {
TCCState* state;
LineScanner lineScanner;
+ LoadContext loadContext;
char* fileBuf;
size_t fileSize;
diff --git a/src/sldj.c b/src/sldj.c
index 349229b..a7483f2 100644
--- a/src/sldj.c
+++ b/src/sldj.c
@@ -14,13 +14,7 @@ void initSldj(Sldj* sldj)
// Scripting.
initScripting(&sldj->scripting);
-
- //compileScript(&sldj->scripting, "/home/nathan/Documents/dev/sldj/test/scanTest1.c");
-
- if (sldj->scripting.lineScanner != NULL)
- {
- printf("%d", sldj->scripting.lineScanner(0, 0, 0).b);
- }
+ sldj->scriptFilepath[0] = '\0';
}
void updateSldj(Sldj* sldj)
@@ -68,7 +62,7 @@ void updateSldj(Sldj* sldj)
if (IsKeyPressed(KEY_R))
{
- compileScript(&sldj->scripting, "/home/nathan/Documents/dev/sldj/test/scanTest1.c");
+ reloadScript(sldj);
}
EndDrawing();
@@ -92,3 +86,15 @@ void resetViewport(Sldj* sldj, int width, int height)
sldj->yCount = UINT16_MAX / height;
}
+void reloadScriptContext(Sldj* sldj)
+{
+}
+
+void reloadScript(Sldj* sldj)
+{
+ if (sldj->scriptFilepath[0] != '\0')
+ {
+ compileScript(&sldj->scripting, sldj->scriptFilepath);
+ }
+}
+
diff --git a/src/sldj.h b/src/sldj.h
index 1db4331..7788a86 100644
--- a/src/sldj.h
+++ b/src/sldj.h
@@ -6,8 +6,8 @@
#include <raylib.h>
#include "sldjConfig.h"
-#include "util.h"
#include "scripting.h"
+#include "libsldj/util.h"
#ifndef SLDJ_H
#define SLDJ_H
@@ -16,10 +16,13 @@ typedef struct Sldj {
RenderTexture viewport;
uint32_t frameCounter;
+
+ // Used for always using a 0 to 2**16 range when scanning.
uint16_t xCount;
uint16_t yCount;
SldjScripting scripting;
+ char scriptFilepath[SLDJ_NAMEMAX];
} Sldj;
void initSldj(Sldj* sldj);
@@ -27,5 +30,7 @@ void updateSldj(Sldj* sldj);
void closeSldj(Sldj* sldj);
void resetViewport(Sldj* sldj, int width, int height);
+void reloadScriptContext(Sldj* sldj);
+void reloadScript(Sldj* sldj);
#endif
diff --git a/src/util.h b/src/util.h
deleted file mode 100644
index 110aae7..0000000
--- a/src/util.h
+++ /dev/null
@@ -1,13 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-#ifndef UTIL_H
-#define UTIL_H
-
-#define SET_BIT(b, n) (b | (0x1 << n))
-#define CLEAR_BIT(b, n) (b & ~(0x1 << n))
-#define IS_BIT_SET(b, n) ((b >> n) & 0x1)
-#define TOGGLE_BIT(b, n) (b ^ (0x1 << n))
-#define HAS_FLAG(v, f) ((v & f) == f)
-
-#endif
diff --git a/test/scanTest1.c b/test/scanTest1.c
index ac67115..19d78b5 100644
--- a/test/scanTest1.c
+++ b/test/scanTest1.c
@@ -3,19 +3,14 @@
#include <stdlib.h>
#include <math.h>
-typedef struct Color {
- unsigned char r;
- unsigned char g;
- unsigned char b;
- unsigned char a;
-} Color;
+#include "libsldj/util.h"
Color lineScanner(uint16_t x, uint16_t y, uint32_t frameNumber)
{
uint8_t c = 0;
uint8_t scale = random() % 2 + 7;
- if (frameNumber % 1024 >= 512)
+ if (frameNumber % 1024 >= 600)
{
uint8_t section = frameNumber % 300;
diff --git a/test/scanTest2.c b/test/scanTest2.c
new file mode 100644
index 0000000..40c82fe
--- /dev/null
+++ b/test/scanTest2.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <math.h>
+
+#include "libsldj/util.h"
+
+Color lineScanner(uint16_t x, uint16_t y, uint32_t frameNumber)
+{
+ return (Color){.r = x % 100 == 1 ? 255 : 0, .g = 0, .b = 0, .a = 255};
+}
+