通过FFI调用Raylib时在Racket中使用SIGSEGV MAPERR

时间:2019-01-10 11:08:58

标签: scheme lisp racket ffi

我正在尝试通过FFI使用Racket代码中的Raylib(https://www.raylib.com/https://github.com/raysan5/raylib)。这是最简单的示例:

#lang racket
(require ffi/unsafe
         ffi/unsafe/define)

; raylib shared object must be available for Racket
; for example, in Linux it must be in
; ~/.racket/<racket-version>/lib> or /usr/lib/racket
(define-ffi-definer define-raylib (ffi-lib "libraylib" #:global? #t))

(define-raylib BeginDrawing (_fun  -> _void))
(define-raylib CloseWindow (_fun -> _void))
(define-raylib EndDrawing (_fun -> _void))
(define-raylib InitWindow (_fun _int _int _string -> _void))
(define-raylib SetTargetFPS (_fun _int -> _void))
(define-raylib WindowShouldClose (_fun -> _int))

(void InitWindow 640 480 "Test window")
(void SetTargetFPS 60)

(define (main-loop)
        (BeginDrawing)
        (EndDrawing)
        (if (= (WindowShouldClose) 0)
            (main-loop)
            (CloseWindow)))

(main-loop)

但是,即使这个非常简单的示例也会因以下消息而崩溃:

SIGSEGV MAPERR si_code 1 fault on addr (nil)
Aborted (core dumped)

看起来像在调用BeginDrawing()函数时崩溃了。此函数的代码也非常简单:

// Setup canvas (framebuffer) to start drawing
void BeginDrawing(void)
{
    currentTime = GetTime();            // Number of elapsed seconds since InitTimer()
    updateTime = currentTime - previousTime;
    previousTime = currentTime;

    rlLoadIdentity();                   // Reset current matrix (MODELVIEW)
    rlMultMatrixf(MatrixToFloat(downscaleView));       // If downscale required, apply it here
}

前缀为rl的函数是OpenGL包装器。可能是OpenGL上下文问题吗?

我试图在Guile Scheme中调用相同的函数,但在Guile中,所有功能都很好。

1 个答案:

答案 0 :(得分:1)

您似乎并没有真正致电InitWindowSetFPS

尝试使用(void InitWindow 640 480 "Test window")而不是(InitWindow 640 480 "Test window")