From 5e764e9edd3c5e33fa9a384b49b74e5c4eb370a3 Mon Sep 17 00:00:00 2001 From: gonzo Date: Fri, 25 Oct 2024 19:20:18 +0200 Subject: first commit --- .gitignore | 1 + README.md | 20 +++++++ drawing.scm | 59 ++++++++++++++++++++ input.scm | 111 ++++++++++++++++++++++++++++++++++++++ makefile | 6 +++ raylib.sld | 79 +++++++++++++++++++++++++++ shapes.scm | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ structs.scm | 125 ++++++++++++++++++++++++++++++++++++++++++ timing.scm | 23 ++++++++ 9 files changed, 601 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 drawing.scm create mode 100644 input.scm create mode 100644 makefile create mode 100644 raylib.sld create mode 100644 shapes.scm create mode 100644 structs.scm create mode 100644 timing.scm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..617b09b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/*.o* diff --git a/README.md b/README.md new file mode 100644 index 0000000..593a98b --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# Custom Gambit-c R7RS bindings for raylib +## For now has the following files: +- raylib.sld + - R7RS library definition +- structs.scm + - raylib specific structs +- drawing.scm + - raylib drawing-related functions +- timing.scm + - raylib timing-related functions +- input.scm + - raylib input-related functions for keyboard & mouse +- shapes.scm + - rshapes module + +## Additionally defines the two hygienic macros +- `with-drawing` + - opens a raylib drawing context, evals body, then closes the context +- `in-scissor-mode` + - opens scissor-mode, evals body, then closes scissor-mode diff --git a/drawing.scm b/drawing.scm new file mode 100644 index 0000000..41a4e26 --- /dev/null +++ b/drawing.scm @@ -0,0 +1,59 @@ +;;; ------------------------- +;;; Drawing-related functions +;;; ------------------------- + +;; Set background color (framebuffer clear color) +;; void ClearBackground(Color color); +(define clear-background + (c-lambda (color) void "ClearBackground")) + +;; Setup canvas (framebuffer) to start drawing +;; void BeginDrawing(void); +(define begin-drawing + (c-lambda () void "BeginDrawing")) + +;; End canvas drawing and swap buffers (double buffering) +;; void EndDrawing(void); +(define end-drawing + (c-lambda () void "EndDrawing")) + +;; Begin 2D mode with custom camera (2D) +;; void BeginMode2D(Camera2D camera); + +;; Ends 2D mode with custom camera +;; void EndMode2D(void); + +;; Begin 3D mode with custom camera (3D) +;; void BeginMode3D(Camera3D camera); + +;; Ends 3D mode and returns to default 2D orthographic mode +;; void EndMode3D(void); + +;; Begin drawing to render texture +;; void BeginTextureMode(RenderTexture2D target); + +;; Ends drawing to render texture +;; void EndTextureMode(void); + +;; Begin custom shader drawing +;; void BeginShaderMode(Shader shader); + +;; End custom shader drawing (use default shader) +;; void EndShaderMode(void); + +;; Begin blending mode (alpha, additive, multiplied, subtract, custom) +;; void BeginBlendMode(int mode); + +;; End blending mode (reset to default: alpha blending) +;; void EndBlendMode(void); + +;; Begin scissor mode (define screen area for following drawing) +;; void BeginScissorMode(int x, int y, int width, int height); +(define begin-scissor-mode + (c-lambda (int int int int) void "BeginScissorMode")) + +;; End scissor mode +;; void EndScissorMode(void); +(define end-scissor-mode + (c-lambda () void "EndScissorMode")) + diff --git a/input.scm b/input.scm new file mode 100644 index 0000000..ef5dd00 --- /dev/null +++ b/input.scm @@ -0,0 +1,111 @@ +;;; +;;; Input-related functions: keyboard +;;; + +;; Check if a key has been pressed once +;; bool IsKeyPressed(int key); +(define key-pressed? + (c-lambda (int) bool "IsKeyPressed")) + +;; Check if a key has been pressed again (Only PLATFORM_DESKTOP) +;; bool IsKeyPressedRepeat(int key); +(define key-pressed-repeat? + (c-lambda (int) bool "IsKeyPressedRepeat")) + +;; Check if a key is being pressed +;; bool IsKeyDown(int key); +(define key-down? + (c-lambda (int) bool "IsKeyDown")) + +;; Check if a key has been released once +;; bool IsKeyReleased(int key); +(define key-released? + (c-lambda (int) bool "IsKeyReleased")) + +;; Check if a key is NOT being pressed +;; bool IsKeyUp(int key); +(define key-up? + (c-lambda (int) bool "IsKeyUp")) + +;; Get key pressed (keycode), call it multiple times for keys queued, returns 0 when the queue is empty +;; int GetKeyPressed(void); +(define get-pressed-key + (c-lambda () int "GetKeyPressed")) + +;; Get char pressed (unicode), call it multiple times for chars queued, returns 0 when the queue is empty +;; int GetCharPressed(void); +(define get-pressed-char + (c-lambda () int "GetCharPressed")) + +;; Set a custom key to exit program (default is ESC) +;; void SetExitKey(int key); +(define set-exit-key + (c-lambda (int) void "SetExitKey")) + + +;;; +;;; Input-related functions: mouse +;;; + +;; Check if a mouse button has been pressed once +;; bool IsMouseButtonPressed(int button); +(define mouse-btn-pressed? + (c-lambda (int) bool "IsMouseButtonPressed")) + +;; Check if a mouse button is being pressed +;; bool IsMouseButtonDown(int button); +(define mouse-btn-down? + (c-lambda (int) bool "IsMouseButtonDown")) + +;; Check if a mouse button has been released once +;; bool IsMouseButtonReleased(int button); +(define mouse-btn-released? + (c-lambda (int) bool "IsMouseButtonReleased")) + +;; Check if a mouse button is NOT being pressed +;; bool IsMouseButtonUp(int button); +(define mouse-btn-up? + (c-lambda (int) bool "IsMouseButtonUp")) + +;; Get mouse position X +;; int GetMouseX(void); +(define get-mouse-x + (c-lambda () int "GetMouseX")) + +;; Get mouse position Y +;; int GetMouseY(void); +(define get-mouse-y + (c-lambda () int "GetMouseY")) + +;; Get mouse position XY +;; Vector2 GetMousePosition(void); +;; replaced by this: +(define get-mouse-pos + (lambda () + (cons (get-mouse-x) + (get-mouse-y)))) + +;; Get mouse delta between frames +;; Vector2 GetMouseDelta(void); + +;; Set mouse position XY +;; void SetMousePosition(int x, int y); +(define set-mouse-pos + (c-lambda (int int) void "SetMousePosition")) + +;; Set mouse offset +;; void SetMouseOffset(int offsetX, int offsetY); +(define set-mouse-offset + (c-lambda (int int) void "SetMouseOffset")) + +;; Set mouse scaling +;; void SetMouseScale(float scaleX, float scaleY); + +;; Get mouse wheel movement for X or Y, whichever is larger +;; float GetMouseWheelMove(void); + +;; Get mouse wheel movement for both X and Y +;; Vector2 GetMouseWheelMoveV(void); + +;; Set mouse cursor +;; void SetMouseCursor(int cursor); diff --git a/makefile b/makefile new file mode 100644 index 0000000..c471fae --- /dev/null +++ b/makefile @@ -0,0 +1,6 @@ +all: loadable + +loadable: + gsc -:s -debug -dynamic \ + -pkg-config gl -pkg-config raylib \ + raylib.sld diff --git a/raylib.sld b/raylib.sld new file mode 100644 index 0000000..4b9ab17 --- /dev/null +++ b/raylib.sld @@ -0,0 +1,79 @@ +(define-library (raylib) + (export init-window close-window) + (export with-drawing begin-drawing end-drawing) + (export in-scissor-mode begin-scissor-mode end-scissor-mode) + (import (gambit)) + + ;;; ------- + ;;; structs + ;;; ------- + (include "structs.scm") + ;;; ------------------------- + ;;; Drawing-related functions + ;;; ------------------------- + (include "drawing.scm") + ;;; ------------------------ + ;;; Timing-related functions + ;;; ------------------------ + (include "timing.scm") + ;;; ----------------------------------------- + ;;; Input-related functions: keyboard & mouse + ;;; ----------------------------------------- + (include "input.scm") + ;;; -------------- + ;;; rshapes module + ;;; -------------- + (include "shapes.scm") + + (begin + (c-declare "#include \"raylib.h\"") + + ;;; ------------------------ + ;;; Window-related functions + ;;; ------------------------ + ;; Initialize window and OpenGL context + ;; void InitWindow(int width, int height, const char *title); + (define init-window + (c-lambda (int int char-string) void + "InitWindow")) + + ;; Close window and unload OpenGL context + ;; void CloseWindow(void); + (define close-window + (c-lambda () void "CloseWindow")) + + (define window-sould-close + (c-lambda () bool "WindowShouldClose")) + + + (define draw-fps + (c-lambda (int int) void "DrawFPS")) + + + ;;; MACROS + + ;;; hygienic drawing context (r7rs only!) + (define-syntax with-drawing + (syntax-rules () + ((with-drawing body ...) + (dynamic-wind + begin-drawing + (lambda () (begin body ...)) + end-drawing)))) + + ;;; less hygienic drawing context for R5RS + ;; (define-macro (with-drawing . body) + ;; `(dynamic-wind + ;; begin-drawing + ;; (lambda () (begin ,@body)) + ;; end-drawing)) + + ;;; hygienic scissor-mode (r7rs only!) + (define-syntax in-scissor-mode + (syntax-rules () + ((in-scissor-mode (x y width height) body ...) + (dynamic-wind + (lambda () (begin-scissor-mode x y width height)) + (lambda () (begin body ...)) + end-scissor-mode)))) + )) diff --git a/shapes.scm b/shapes.scm new file mode 100644 index 0000000..36194af --- /dev/null +++ b/shapes.scm @@ -0,0 +1,177 @@ +;; Set texture and rectangle to be used on shapes drawing +;; void SetShapesTexture(Texture2D texture, Rectangle source); + + +;;; +;;; Basic shapes drawing functions +;;; + +;; Draw a pixel +;; void DrawPixel(int posX, int posY, Color color); +(define draw-pixel + (c-lambda (int int color) void "DrawPixel")) + +;; Draw a pixel (Vector version) +;; void DrawPixelV(Vector2 position, Color color); +(define draw-pixel-V + (c-lambda (vec2 color) void "DrawPixelV")) + +;; Draw a line +;; void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color); +(define draw-line + (c-lambda (int int int int color) void "DrawLine")) + +;; Draw a line (using gl lines) +;; void DrawLineV(Vector2 startPos, Vector2 endPos, Color color); +(define draw-line-V + (c-lambda (vec2 vec2 color) void "DrawLineV")) + +;; Draw a line (using triangles/quads) +;; void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color); + +;; Draw lines sequence (using gl lines) +;; void DrawLineStrip(Vector2 *points, int pointCount, Color color); + +;; Draw line segment cubic-bezier in-out interpolation +;; void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color); + +;; Draw a color-filled circle +;; void DrawCircle(int centerX, int centerY, float radius, Color color); +(define draw-circle + (c-lambda (int int float color) void "DrawCircle")) + +;; Draw a piece of a circle +;; void DrawCircleSector(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color); + +;; Draw circle sector outline +;; void DrawCircleSectorLines(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color); + +;; Draw a gradient-filled circle +;; void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Color color2); +(define draw-circle-gradient + (c-lambda (int int float color color) void "DrawCircleGradient")) + +;; Draw a color-filled circle (Vector version) +;; void DrawCircleV(Vector2 center, float radius, Color color); +(define draw-circle-V + (c-lambda (vec2 float color) void "DrawCircleV")) + +;; Draw circle outline +;; void DrawCircleLines(int centerX, int centerY, float radius, Color color); + +;; Draw circle outline (Vector version) +;; void DrawCircleLinesV(Vector2 center, float radius, Color color); + +;; Draw ellipse +;; void DrawEllipse(int centerX, int centerY, float radiusH, float radiusV, Color color); + +;; Draw ellipse outline +;; void DrawEllipseLines(int centerX, int centerY, float radiusH, float radiusV, Color color); + +;; Draw ring +;; void DrawRing(Vector2 center, float innerRadius, float outerRadius, float startAngle, float endAngle, int segments, Color color); + +;; Draw ring outline +;; void DrawRingLines(Vector2 center, float innerRadius, float outerRadius, float startAngle, float endAngle, int segments, Color color); + +;; Draw a color-filled rectangle +;; void DrawRectangle(int posX, int posY, int width, int height, Color color); +(define draw-rectangle + (c-lambda (int int int int color) void "DrawRectangle")) + +;; Draw a color-filled rectangle (Vector version) +;; void DrawRectangleV(Vector2 position, Vector2 size, Color color); +(define draw-rectangle-V + (c-lambda (vec2 vec2 color) void "DrawRectangleV")) + +;; Draw a color-filled rectangle +;; void DrawRectangleRec(Rectangle rec, Color color); +(define draw-rec + (c-lambda (rec color) void "DrawRectangleRec")) + +;; Draw a color-filled rectangle with pro parameters +;; void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color); + +;; Draw a vertical-gradient-filled rectangle +;; void DrawRectangleGradientV(int posX, int posY, int width, int height, Color color1, Color color2) + +;; Draw a horizontal-gradient-filled rectangle +;; void DrawRectangleGradientH(int posX, int posY, int width, int height, Color color1, Color color2) + +;; Draw a gradient-filled rectangle with custom vertex colors +;; void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3, Color col4); + +;; Draw rectangle outline +;; void DrawRectangleLines(int posX, int posY, int width, int height, Color color); +(define draw-rectangle-lines + (c-lambda (int int int int color) void "DrawRectangleLines")) + +;; Draw rectangle outline with extended parameters +;; void DrawRectangleLinesEx(Rectangle rec, float lineThick, Color color); +(define draw-rec-lines + (c-lambda (rec float color) void "DrawRectangleLinesEx")) + +;; Draw rectangle with rounded edges +;; void DrawRectangleRounded(Rectangle rec, float roundness, int segments, Color color); + +;; Draw rectangle with rounded edges outline +;; void DrawRectangleRoundedLines(Rectangle rec, float roundness, int segments, float lineThick, Color color); + +;; Draw a color-filled triangle (vertex in counter-clockwise order!) +;; void DrawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Color color); +(define draw-triangle + (c-lambda (vec2 vec2 vec2 color) void "DrawTriangle")) + +;; Draw triangle outline (vertex in counter-clockwise order!) +;; void DrawTriangleLines(Vector2 v1, Vector2 v2, Vector2 v3, Color color); +(define draw-triangle-lines + (c-lambda (vec2 vec2 vec2 color) void "DrawTriangleLines")) + +;; Draw a triangle fan defined by points (first vertex is the center) +;; void DrawTriangleFan(Vector2 *points, int pointCount, Color color); + +;; Draw a triangle strip defined by points +;; void DrawTriangleStrip(Vector2 *points, int pointCount, Color color); + +;; Draw a regular polygon (Vector version) +;; void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color color); + +;; Draw a polygon outline of n sides +;; void DrawPolyLines(Vector2 center, int sides, float radius, float rotation, Color color); + +;; Draw a polygon outline of n sides with extended parameters +;; void DrawPolyLinesEx(Vector2 center, int sides, float radius, float rotation, float lineThick, Color color); + + +;;; +;;; Splines drawing functions +;;; +;; Draw spline: Linear, minimum 2 points +;; void DrawSplineLinear(Vector2 *points, int pointCount, float thick, Color color); + +;; Draw spline: B-Spline, minimum 4 points +;; void DrawSplineBasis(Vector2 *points, int pointCount, float thick, Color color); + +;; Draw spline: Catmull-Rom, minimum 4 points +;; void DrawSplineCatmullRom(Vector2 *points, int pointCount, float thick, Color color); + +;; Draw spline: Quadratic Bezier, minimum 3 points (1 control point): [p1, c +;; void DrawSplineBezierQuadratic(Vector2 *points, int pointCount, float thick, Color color); + +;; Draw spline: Cubic Bezier, minimum 4 points (2 control points): [p1, c2, +;; void DrawSplineBezierCubic(Vector2 *points, int pointCount, float thick, Color color); + +;; Draw spline segment: Linear, 2 points +;; void DrawSplineSegmentLinear(Vector2 p1, Vector2 p2, float thick, Color color); + +;; Draw spline segment: B-Spline, 4 points +;; void DrawSplineSegmentBasis(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float thick, Color color); + +;; Draw spline segment: Catmull-Rom, 4 points +;; void DrawSplineSegmentCatmullRom(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float thick, Color color); + +;; Draw spline segment: Quadratic Bezier, 2 points, 1 control point +;; void DrawSplineSegmentBezierQuadratic(Vector2 p1, Vector2 c2, Vector2 p3, float thick, Color color); + +;; Draw spline segment: Cubic Bezier, 2 points, 2 control points +;; void DrawSplineSegmentBezierCubic(Vector2 p1, Vector2 c2, Vector2 c3, Vector2 p4, float thick, Color color); diff --git a/structs.scm b/structs.scm new file mode 100644 index 0000000..44ed30b --- /dev/null +++ b/structs.scm @@ -0,0 +1,125 @@ +;;; ------- +;;; structs +;;; ------- + +;; Color, 4 components, R8G8B8A8 (32bit) +(c-define-type color (struct "Color")) + +;; color constructor +(define make-color + (c-lambda (unsigned-int8 unsigned-int8 unsigned-int8 unsigned-int8) color + #<< c-lambda + Color col = (Color){___arg1, ___arg2, ___arg3, ___arg4 }; + ___return(col); + c-lambda + )) + +;; color getters +(define color-red + (c-lambda (color) unsigned-int8 + #<< c-lambda + ___return(___arg1.r); + c-lambda + )) +(define color-blue + (c-lambda (color) unsigned-int8 + #<< c-lambda + ___return(___arg1.b); + c-lambda + )) +(define color-green + (c-lambda (color) unsigned-int8 + #<< c-lambda + ___return(___arg1.g); + c-lambda + )) +(define color-alpha + (c-lambda (color) unsigned-int8 + #<< c-lambda + ___return(___arg1.a); + c-lambda + )) + +;; color setters +(define color-red-set! + (c-lambda (color unsigned-int8) void + #<< c-lambda + ___arg1.r = ___arg2; + c-lambda + )) +(define color-blue-set! + (c-lambda (color unsigned-int8) void + #<< c-lambda + ___arg1.b = ___arg2; + c-lambda + )) +(define color-green-set! + (c-lambda (color unsigned-int8) void + #<< c-lambda + ___arg1.g = ___arg2; + c-lambda + )) +(define color-alpha-set! + (c-lambda (color unsigned-int8) void + #<< c-lambda + ___arg1.a = ___arg2; + c-lambda + )) + + +;; vector2, 2 float components, x32y32 (64 bits) +(c-define-type vec2 (struct "Vector2")) + +;; vec2 constructor +(define make-vec2 + (c-lambda (float float) vec2 + #<< c-lambda + Vector2 v = (Vector2){___arg1, ___arg2}; + ___return(v); + c-lambda + )) + +;; vec2 getters +(define vec2-x + (c-lambda (vec2) float "___return(___arg1.x);")) +(define vec2-y + (c-lambda (vec2) float "___return(___arg1.y);")) + +;; vec2 setters +(define vec2-x-set! + (c-lambda (vec2 float) void "___arg1.x = ___arg2;")) +(define vec2-y-set! + (c-lambda (vec2 float) void "___arg1.y = ___arg2;")) + + +;; Rectangle, 4 float components, x32y32w32h32 (128 bits) +(c-define-type rec (struct "Rectangle")) + +;; vec2 constructor +(define make-rec + (c-lambda (float float float float) rec + #<< c-lambda + Rectangle r = (Rectangle){___arg1, ___arg2, ___arg3, ___arg4}; + ___return(r); + c-lambda + )) + +;; vec2 getters +(define rec-x + (c-lambda (rec) float "___return(___arg1.x);")) +(define rec-y + (c-lambda (rec) float "___return(___arg1.y);")) +(define rec-width + (c-lambda (rec) float "___return(___arg1.width);")) +(define rec-height + (c-lambda (rec) float "___return(___arg1.height);")) + +;; vec2 setters +(define rec-x-set! + (c-lambda (rec float) void "___arg1.x = ___arg2;")) +(define rec-y-set! + (c-lambda (rec float) void "___arg1.y = ___arg2;")) +(define rec-width-set! + (c-lambda (rec float) void "___arg1.width = ___arg2;")) +(define rec-height-set! + (c-lambda (rec float) void "___arg1.height = ___arg2;")) diff --git a/timing.scm b/timing.scm new file mode 100644 index 0000000..4887af3 --- /dev/null +++ b/timing.scm @@ -0,0 +1,23 @@ +;;; ------------------------ +;;; Timing-related functions +;;; ------------------------ + +;; Set target FPS (maximum) +;; void SetTargetFPS(int fps); +(define set-target-fps + (c-lambda (int) void "SetTargetFPS")) + +;; Get time in seconds for last frame drawn (delta time) +;; float GetFrameTime(void); +(define get-frame-time + (c-lambda () float "GetFrameTime")) + +;; Get elapsed time in seconds since InitWindow() +;; double GetTime(void); +(define get-time + (c-lambda () double "GetTime")) + +;; Get current FPS +;; int GetFPS(void); +(define get-fps + (c-lambda () int "GetFPS")) -- cgit v1.2.3