From c0cf88d754a6df204df73d4d043905a2760cfb85 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Tue, 8 Oct 2019 12:25:48 +0200 Subject: [PATCH] Scenes management and draw GUI elements --- examples/01.HelloWorld.scm | 52 ++++++++++++++++ irrlicht.scm | 59 ++++++++++++++++++- irrlicht/bindings.scm | 6 ++ irrlicht/bindings/core.scm | 4 ++ .../hello.scm => irrlicht/bindings/gui.scm | 25 ++++---- irrlicht/bindings/video.scm | 22 ++++++- 6 files changed, 153 insertions(+), 15 deletions(-) create mode 100644 examples/01.HelloWorld.scm rename examples/hello.scm => irrlicht/bindings/gui.scm (67%) diff --git a/examples/01.HelloWorld.scm b/examples/01.HelloWorld.scm new file mode 100644 index 0000000..8ad550d --- /dev/null +++ b/examples/01.HelloWorld.scm @@ -0,0 +1,52 @@ +;;; guile-irrlicht --- FFI bindings for Irrlicht Engine +;;; Copyright (C) 2019 Javier Sancho +;;; +;;; This file is part of guile-irrlicht. +;;; +;;; Guile-irrlicht is free software; you can redistribute it and/or modify +;;; it under the terms of the GNU Lesser General Public License as +;;; published by the Free Software Foundation; either version 3 of the +;;; License, or (at your option) any later version. +;;; +;;; Guile-irrlicht is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;; General Public License for more details. +;;; +;;; You should have received a copy of the GNU Lesser General Public +;;; License along with guile-irrlicht. If not, see +;;; . + + +;;; Irrlicht 01.HelloWorld example +;;; http://irrlicht.sourceforge.net/docu/example001.html + + +(use-modules (irrlicht)) + +;; start up the engine +(define device (create-device 'software '(640 480) 16 #f #f #f)) +(when (not device) + (exit #f)) + +(set-window-caption! device "Hello World! - Irrlicht Engine Demo") + +(define driver (get-video-driver device)) +(define scene-manager (get-scene-manager device)) +(define gui-env (get-gui-environment device)) + +;; static text +(add-static-text! gui-env + "Hello World! This is the Irrlicht Software renderer!" + '(10 10 260 22) + #t) + +;; draw everything +(while (device-run? device) + (begin-scene driver #t #t '(255 100 101 140)) + (gui-draw-all gui-env) + (end-scene driver)) + +;; delete device +(device-drop! device) +(exit #t) diff --git a/irrlicht.scm b/irrlicht.scm index 0ff6e3a..1946da1 100644 --- a/irrlicht.scm +++ b/irrlicht.scm @@ -23,14 +23,24 @@ #:use-module (system foreign) #:use-module ((irrlicht bindings) #:prefix ffi:) #:use-module ((irrlicht bindings core) #:prefix ffi-core:) + #:use-module ((irrlicht bindings gui) #:prefix ffi-gui:) #:use-module ((irrlicht bindings video) #:prefix ffi-video:) - #:export (create-device + #:export (;; device + create-device get-video-driver + get-gui-environment get-scene-manager set-window-caption! device-run? - device-drop!)) + device-drop! + ;; driver + begin-scene + end-scene + ;; gui + add-static-text! + gui-draw-all)) +;; Device functions (define* (create-device #:optional (device-type 'software) (window-size '(640 480)) @@ -56,6 +66,9 @@ (define (get-video-driver device) (ffi:get-video-driver device)) +(define (get-gui-environment device) + (ffi:get-gui-environment device)) + (define (get-scene-manager device) (ffi:get-scene-manager device)) @@ -67,3 +80,45 @@ (define (device-drop! device) (if (> (ffi:drop device) 0) #t #f)) + + +;; Driver functions +(define* (begin-scene driver + #:optional + (back-buffer #t) + (z-buffer #t) + (color '(255 0 0 0)) + (video-data %null-pointer) + (source-rect '())) + (ffi-video:begin-scene driver + (if back-buffer 1 0) + (if z-buffer 1 0) + (make-c-struct ffi-video:scolor color) + video-data + (if (null? source-rect) + %null-pointer + (make-c-struct ffi-core:rect source-rect)))) + +(define (end-scene driver) + (ffi-video:end-scene driver)) + + +;; GUI functions +(define* (add-static-text! gui-env text rectangle + #:optional + (border #f) + (word-wrap #t) + (parent %null-pointer) + (id -1) + (fill-background #f)) + (ffi-gui:add-static-text gui-env + (string->pointer text) + (make-c-struct ffi-core:rect rectangle) + (if border 1 0) + (if word-wrap 1 0) + parent + id + (if fill-background 1 0))) + +(define (gui-draw-all gui-env) + (ffi-gui:draw-all gui-env)) diff --git a/irrlicht/bindings.scm b/irrlicht/bindings.scm index e54de96..f93ee7c 100644 --- a/irrlicht/bindings.scm +++ b/irrlicht/bindings.scm @@ -35,6 +35,12 @@ (dynamic-func "irr_IrrlichtDevice_getVideoDriver" cirr) (list '*))) +(define-public get-gui-environment + (pointer->procedure + '* + (dynamic-func "irr_IrrlichtDevice_getGUIEnvironment" cirr) + (list '*))) + (define-public get-scene-manager (pointer->procedure '* diff --git a/irrlicht/bindings/core.scm b/irrlicht/bindings/core.scm index 2058d8a..c82b728 100644 --- a/irrlicht/bindings/core.scm +++ b/irrlicht/bindings/core.scm @@ -24,3 +24,7 @@ ;; dimension2d struct (define-public dimension2d (list uint32 uint32)) + +;; rect struct +(define-public rect + (list int32 int32 int32 int32)) diff --git a/examples/hello.scm b/irrlicht/bindings/gui.scm similarity index 67% rename from examples/hello.scm rename to irrlicht/bindings/gui.scm index 283462d..f86592f 100644 --- a/examples/hello.scm +++ b/irrlicht/bindings/gui.scm @@ -18,18 +18,19 @@ ;;; . -(use-modules (irrlicht)) +(define-module (irrlicht bindings gui) + #:use-module (system foreign)) -;; Create device -(define device (create-device 'software '(640 480) 16 #f #f #f)) -(when (not device) - (exit #f)) +(define cirr (dynamic-link "libCIrrlicht")) -(define driver (get-video-driver device)) -(define scene-manager (get-scene-manager device)) +(define-public add-static-text + (pointer->procedure + '* + (dynamic-func "irr_gui_IGUIEnvironment_addStaticText" cirr) + (list '* '* '* int int '* int int))) -(set-window-caption! device "Hello World!") - -(while (device-run? device)) - -(device-drop! device) +(define-public draw-all + (pointer->procedure + void + (dynamic-func "irr_gui_IGUIEnvironment_drawAll" cirr) + (list '*))) diff --git a/irrlicht/bindings/video.scm b/irrlicht/bindings/video.scm index d02db52..c77f9d9 100644 --- a/irrlicht/bindings/video.scm +++ b/irrlicht/bindings/video.scm @@ -18,7 +18,10 @@ ;;; . -(define-module (irrlicht bindings video)) +(define-module (irrlicht bindings video) + #:use-module (system foreign)) + +(define cirr (dynamic-link "libCIrrlicht")) ;; E_DRIVER_TYPE enum (define-public EDT_NULL 0) @@ -28,3 +31,20 @@ (define-public EDT_DIRECT3D9 4) (define-public EDT_OPENGL 5) (define-public EDT_COUNT 6) + +;; scolor struct +(define-public scolor + (list uint32 uint32 uint32 uint32)) + +;; Driver functions +(define-public begin-scene + (pointer->procedure + int + (dynamic-func "irr_video_IVideoDriver_beginScene" cirr) + (list '* int int '* '* '*))) + +(define-public end-scene + (pointer->procedure + int + (dynamic-func "irr_video_IVideoDriver_endScene" cirr) + (list '*))) -- 2.39.5