aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authornathansmith117 <nathansmith117@sdf.org>2025-02-03 15:43:19 +0000
committernathansmith117 <nathansmith117@sdf.org>2025-02-03 15:43:19 +0000
commit69733d1f2171978e3d15078d24323af39d54c518 (patch)
tree4dacf17246b9a873fb1f505968eecac899e684fe /src
parent305c224104be6558f2ece595a47cf29348b60d66 (diff)
downloadsldj-69733d1f2171978e3d15078d24323af39d54c518.tar.gz
sldj-69733d1f2171978e3d15078d24323af39d54c518.tar.bz2
sldj-69733d1f2171978e3d15078d24323af39d54c518.zip
Scripting working so far
Diffstat (limited to 'src')
-rw-r--r--src/implementations.c3
-rw-r--r--src/scripting.c87
-rw-r--r--src/scripting.h33
-rw-r--r--src/sldj.c66
-rw-r--r--src/sldj.h17
-rw-r--r--src/sldjConfig.h8
6 files changed, 183 insertions, 31 deletions
diff --git a/src/implementations.c b/src/implementations.c
new file mode 100644
index 0000000..bface50
--- /dev/null
+++ b/src/implementations.c
@@ -0,0 +1,3 @@
+#define RAYGUI_IMPLEMENTATION
+#include "raygui.h"
+
diff --git a/src/scripting.c b/src/scripting.c
new file mode 100644
index 0000000..af5d993
--- /dev/null
+++ b/src/scripting.c
@@ -0,0 +1,87 @@
+#include "scripting.h"
+
+void handle_tcc_error(void* opaque, const char* msg)
+{
+ TraceLog(LOG_ERROR, msg);
+}
+
+void initScripting(SldjScripting* scripting)
+{
+ scripting->state = tcc_new();
+ scripting->lineScanner = NULL;
+ scripting->fileBuf = NULL;
+
+ if (!scripting->state)
+ {
+ TraceLog(LOG_ERROR, "Could not open tcc state");
+ return;
+ }
+
+ tcc_set_error_func(scripting->state, stderr, handle_tcc_error);
+}
+
+void closeScripting(SldjScripting* scripting)
+{
+ tcc_delete(scripting->state);
+
+ if (scripting->fileBuf != NULL)
+ {
+ free(scripting->fileBuf);
+ }
+}
+
+void compileScript(SldjScripting* scripting, const char filePath[SLDJ_NAMEMAX])
+{
+ // Get file size.
+ struct stat fileState;
+
+ if (stat(filePath, &fileState) < 0)
+ {
+ TraceLog(LOG_ERROR, strerror(errno));
+ return;
+ }
+
+ scripting->fileSize = fileState.st_size;
+
+ // Load file information into a buffer.
+ if (scripting->fileBuf == NULL)
+ {
+ scripting->fileBuf = (char*)malloc(scripting->fileSize);
+ } else {
+ scripting->fileBuf = (char*)realloc(scripting->fileBuf, scripting->fileSize);
+ }
+
+ if (scripting->fileBuf == NULL)
+ {
+ TraceLog(LOG_ERROR, strerror(errno));
+ return;
+ }
+
+ FILE* file = fopen(filePath, "r");
+
+ if (file == NULL)
+ {
+ TraceLog(LOG_ERROR, strerror(errno));
+ return;
+ }
+
+ fread(scripting->fileBuf, scripting->fileSize, 1, file);
+ scripting->fileBuf[scripting->fileSize - 1] = '\0';
+ fclose(file);
+
+ // Compile shit now.
+ tcc_set_output_type(scripting->state, TCC_OUTPUT_MEMORY);
+
+ if (tcc_compile_string(scripting->state, scripting->fileBuf) == -1)
+ {
+ return;
+ }
+
+ if (tcc_relocate(scripting->state) < 0)
+ {
+ return;
+ }
+
+ scripting->lineScanner = tcc_get_symbol(scripting->state, "lineScanner");
+}
+
diff --git a/src/scripting.h b/src/scripting.h
new file mode 100644
index 0000000..fab64b2
--- /dev/null
+++ b/src/scripting.h
@@ -0,0 +1,33 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include <raylib.h>
+#include <libtcc.h>
+
+#include "sldjConfig.h"
+
+#ifndef SCRIPTING_H
+#define SCRIPTING_H
+
+typedef Color (*LineScanner)(uint16_t x, uint16_t y, uint32_t frameNumber);
+
+typedef struct SldjScripting {
+ TCCState* state;
+
+ LineScanner lineScanner;
+
+ char* fileBuf;
+ size_t fileSize;
+} SldjScripting;
+
+void initScripting(SldjScripting* scripting);
+void closeScripting(SldjScripting* scripting);
+void compileScript(SldjScripting* scripting, const char filePath[SLDJ_NAMEMAX]);
+
+#endif
diff --git a/src/sldj.c b/src/sldj.c
index a2b0c47..ca566b7 100644
--- a/src/sldj.c
+++ b/src/sldj.c
@@ -2,21 +2,25 @@
void initSldj(Sldj* sldj)
{
+ // Create window.
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Scanline DJ");
SetWindowState(FLAG_WINDOW_RESIZABLE);
- SetTargetFPS(FRAME_RATE);
+ SetTargetFPS(DEFAULT_FRAME_RATE);
- sldj->screenTexture = LoadRenderTexture(SCREEN_WIDTH, SCREEN_HEIGHT);
+ sldj->viewport = LoadRenderTexture(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT);
sldj->frameCounter = 0;
-}
+ sldj->xCount = UINT16_MAX / DEFAULT_SCREEN_WIDTH;
+ sldj->yCount = UINT16_MAX / DEFAULT_SCREEN_HEIGHT;
-uint16_t noise = 0;
+ // Scripting.
+ initScripting(&sldj->scripting);
-Color scanPoint(uint16_t x, uint16_t y, int frame)
-{
- noise ^= frame;
- noise ^= x ^ y;
- return (Color){.r = (y >> (6 - noise)) ^ (frame << 4), .g = noise >> x, .b = 0, .a = 255};
+ 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);
+ }
}
void updateSldj(Sldj* sldj)
@@ -24,31 +28,36 @@ void updateSldj(Sldj* sldj)
BeginDrawing();
// Scan image.
- BeginTextureMode(sldj->screenTexture);
-
- uint16_t xScaled = 0;
- uint16_t yScaled = 0;
+ BeginTextureMode(sldj->viewport);
- for (uint16_t y = 0; y < SCREEN_HEIGHT; ++y)
+ if (sldj->scripting.lineScanner != NULL)
{
- xScaled = 0;
+ uint16_t xScan = 0;
+ uint16_t yScan = 0;
- for (uint16_t x = 0; x < SCREEN_WIDTH; ++x)
+ for (int y = 0; y < sldj->viewport.texture.height; ++y)
{
- DrawPixel(x, y, scanPoint(xScaled, yScaled, sldj->frameCounter));
- xScaled += 65536 / SCREEN_WIDTH;
- }
+ xScan = 0;
+
+ for (int x = 0; x < sldj->viewport.texture.width; ++x)
+ {
+ DrawPixel(x, y, sldj->scripting.lineScanner(xScan, yScan, sldj->frameCounter));
+ ++xScan;
+ }
- yScaled += 65536 / SCREEN_HEIGHT;
+ ++yScan;
+ }
}
EndTextureMode();
// Draw scanned image.
+ Texture viewport = sldj->viewport.texture;
+
DrawTexturePro(
- sldj->screenTexture.texture,
- (Rectangle){0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT},
- (Rectangle){0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT},
+ viewport,
+ (Rectangle){0.0, 0.0, viewport.width, viewport.height},
+ (Rectangle){0.0, 0.0, viewport.width, viewport.height},
(Vector2){0.0, 0.0},
0.0,
WHITE
@@ -61,8 +70,17 @@ void updateSldj(Sldj* sldj)
void closeSldj(Sldj* sldj)
{
- UnloadRenderTexture(sldj->screenTexture);
+ UnloadRenderTexture(sldj->viewport);
+ closeScripting(&sldj->scripting);
CloseWindow();
}
+void resetViewport(Sldj* sldj, int width, int height)
+{
+ UnloadRenderTexture(sldj->viewport);
+ sldj->viewport = LoadRenderTexture(width, height);
+ sldj->xCount = UINT16_MAX / width;
+ sldj->yCount = UINT16_MAX / height;
+}
+
diff --git a/src/sldj.h b/src/sldj.h
index 4db6afb..1db4331 100644
--- a/src/sldj.h
+++ b/src/sldj.h
@@ -1,22 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
-#include <raylib.h>
-#include <math.h>
#include <stdint.h>
+#include <math.h>
+
+#include <raylib.h>
#include "sldjConfig.h"
#include "util.h"
+#include "scripting.h"
#ifndef SLDJ_H
#define SLDJ_H
typedef struct Sldj {
- RenderTexture screenTexture;
- int frameCounter;
+ RenderTexture viewport;
+
+ uint32_t frameCounter;
+ uint16_t xCount;
+ uint16_t yCount;
+
+ SldjScripting scripting;
} Sldj;
void initSldj(Sldj* sldj);
void updateSldj(Sldj* sldj);
void closeSldj(Sldj* sldj);
+void resetViewport(Sldj* sldj, int width, int height);
+
#endif
diff --git a/src/sldjConfig.h b/src/sldjConfig.h
index 53fa4e9..4865538 100644
--- a/src/sldjConfig.h
+++ b/src/sldjConfig.h
@@ -4,9 +4,11 @@
#define WINDOW_WIDTH 960
#define WINDOW_HEIGHT 720
-#define SCREEN_WIDTH 640
-#define SCREEN_HEIGHT 480
+#define DEFAULT_SCREEN_WIDTH 640
+#define DEFAULT_SCREEN_HEIGHT 480
-#define FRAME_RATE 24
+#define DEFAULT_FRAME_RATE 24
+
+#define SLDJ_NAMEMAX 255
#endif