X-Git-Url: https://git.jsancho.org/?p=gacela.git;a=blobdiff_plain;f=src%2Fvideo.scm;h=2cf97a62e8517e7b53967a80950d3128b6d50b74;hp=e0b7a6da10823354c930a724a25bae533771dcf9;hb=e7b815abdbba91fd3e06e0fe3265c665c994cac8;hpb=67e90e6957fd664b8f5f25dfd43aee079b56a03e diff --git a/src/video.scm b/src/video.scm index e0b7a6d..2cf97a6 100644 --- a/src/video.scm +++ b/src/video.scm @@ -20,20 +20,27 @@ #:use-module (gacela gl) #:use-module (gacela ftgl) #:use-module (gacela math) + #:use-module (gacela utils) #:use-module (ice-9 optargs) #:use-module (ice-9 receive) #:export (init-video get-screen-height get-screen-width get-screen-bpp + set-screen-bpp! resize-screen quit-video clear-screen flip-screen + set-screen-title! + get-screen-title set-2d-mode set-3d-mode 3d-mode? - set-frames-per-second + get-frames-per-second + set-frames-per-second! + get-fullscreen + set-fullscreen! init-frame-time get-frame-time delay-frame @@ -43,6 +50,8 @@ progn-textures draw load-texture + load-texture-without-cache + get-texture-properties draw-texture draw-line draw-quad @@ -56,7 +65,12 @@ set-camera camera-look load-font - render-text)) + load-font-without-texture + render-text) + #:re-export (glPushMatrix + glPopMatrix) + #:export-syntax (glmatrix-block)) + ;;; Screen @@ -64,18 +78,20 @@ (define screen #f) (define flags 0) -(define* (init-video width height bpp #:key (mode '2d) (title "")) - (cond ((not screen) - (SDL_Init SDL_INIT_VIDEO) - (let ((info (SDL_GetVideoInfo))) - (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1) - (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE - (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE) - (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL))) - (set! screen (SDL_SetVideoMode width height bpp flags)) - (SDL_WM_SetCaption title "") - (init-gl) - (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))) +(define* (init-video width height bpp #:key (mode '2d) (title "") (fps 20) (fullscreen 'off)) + (SDL_Init SDL_INIT_VIDEO) + (let ((info (SDL_GetVideoInfo))) + (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1) + (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE + (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE) + (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL) + (if (eq? fullscreen 'on) SDL_FULLSCREEN 0))) + (set! screen (SDL_SetVideoMode width height bpp flags)) + (set-screen-title! title) + (set-frames-per-second! fps) + (set-fullscreen! fullscreen #f) + (init-gl) + (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))) (define (get-screen-height) (surface-h screen)) @@ -84,7 +100,11 @@ (surface-w screen)) (define (get-screen-bpp) - (surface-format-BytesPerPixel screen)) + (* (surface-format-BytesPerPixel screen) 8)) + +(define (set-screen-bpp! bpp) + (cond (screen + (set! screen (SDL_SetVideoMode (get-screen-width) (get-screen-height) (get-screen-bpp) flags))))) (define (resize-screen width height) (cond (screen @@ -104,6 +124,16 @@ (SDL_GL_SwapBuffers)) +(define screen-title "") + +(define (set-screen-title! title) + (set! screen-title title) + (SDL_WM_SetCaption title "")) + +(define (get-screen-title) + screen-title) + + (define mode '2d) (define (set-2d-mode) @@ -122,6 +152,19 @@ (eq? mode '3d)) +(define fullscreen 'off) + +(define* (set-fullscreen! fs #:optional (toggle #t)) + (cond ((or (and (eq? fullscreen 'on) (eq? fs 'off)) + (and (eq? fullscreen 'off) (eq? fs 'on))) + (set! fullscreen fs) + (cond (toggle + (SDL_WM_ToggleFullScreen screen)))))) + +(define (get-fullscreen) + fullscreen) + + (define (init-gl) (glShadeModel GL_SMOOTH) (glClearColor 0 0 0 0) @@ -145,29 +188,27 @@ ;;; Frames per second -(define set-frames-per-second #f) -(define init-frame-time #f) -(define get-frame-time #f) -(define delay-frame #f) +(define time 0) +(define frames-per-second 20) +(define time-per-frame 50) ;in ms + +(define (get-frames-per-second) + frames-per-second) -;; (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*))) -;; (set! set-frames-per-second -;; (lambda (fps) -;; (set! time-per-frame (/ 1000.0 fps)))) +(define (set-frames-per-second! fps) + (set! frames-per-second fps) + (set! time-per-frame (/ 1000.0 fps))) -;; (set! init-frame-time -;; (lambda () -;; (set! time (SDL_GetTicks)))) +(define (init-frame-time) + (set! time (SDL_GetTicks))) -;; (set! get-frame-time -;; (lambda () -;; time)) +(define (get-frame-time) + time) -;; (set! delay-frame -;; (lambda () -;; (let ((frame-time (- (SDL_GetTicks) time))) -;; (cond ((< frame-time time-per-frame) -;; (SDL_Delay (- time-per-frame frame-time)))))))) +(define (delay-frame) + (let ((frame-time (- (SDL_GetTicks) time))) + (cond ((< frame-time time-per-frame) + (SDL_Delay (- time-per-frame frame-time)))))) ;;; Drawing @@ -206,7 +247,8 @@ (define (begin-draw number-of-points) (cond ((= number-of-points 2) (glBegin GL_LINES)) ((= number-of-points 3) (glBegin GL_TRIANGLES)) - ((= number-of-points 4) (glBegin GL_QUADS)))) + ((= number-of-points 4) (glBegin GL_QUADS)) + ((> number-of-points 4) (glBegin GL_POLYGON)))) (define (draw-vertexes vertexes) (cond ((not (null? vertexes)) @@ -248,7 +290,7 @@ (else (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height))) (zoomSurface surface zoomx zoomy 0)))))) -(define* (load-texture filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR)) +(define* (load-texture-without-cache filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR)) (progn-textures (receive (image real-w real-h) (load-image-for-texture filename) @@ -266,11 +308,19 @@ (set-texture-size! texture real-w real-h) texture)))))) -(define* (draw-texture texture #:optional (zoom 1)) +(define load-texture (use-cache-with load-texture-without-cache)) + +(define (get-texture-properties texture) + `((width . ,(texture-w texture)) (height . ,(texture-h texture)))) + +(define* (draw-texture texture #:key (zoom 1) (sprite '((0 0) (1 1)))) (cond (texture (let ((width (texture-w texture)) (height (texture-h texture))) - (draw-rectangle (* zoom width) (* zoom height) #:texture texture))))) + (draw-rectangle (* zoom width (- (caadr sprite) (caar sprite))) + (* zoom height (- (cadadr sprite) (cadar sprite))) + #:texture texture + #:texture-coord sprite))))) (define* (draw-line length #:optional color) (let ((l (/ length 2))) @@ -279,31 +329,40 @@ (else (draw (list 0 l) (list 0 (- l))))))) -(define* (draw-quad v1 v2 v3 v4 #:key texture color) +(define (draw-circle radius) + (glBegin GL_POLYGON) + (do ((i 0 (1+ i))) + ((>= i 360)) + (let ((a (degrees-to-radians i))) + (draw-vertex (list (* radius (cos a)) (* radius (sin a)))))) + (glEnd)) + +(define* (draw-quad v1 v2 v3 v4 #:key texture color (texture-coord '((0 0) (1 1)))) (cond (texture (progn-textures (glBindTexture GL_TEXTURE_2D texture) (begin-draw 4) - (draw-vertex v1 #:texture-coord '(0 0)) - (draw-vertex v2 #:texture-coord '(1 0)) - (draw-vertex v3 #:texture-coord '(1 1)) - (draw-vertex v4 #:texture-coord '(0 1)) + (draw-vertex v1 #:texture-coord (car texture-coord)) + (draw-vertex v2 #:texture-coord (list (caadr texture-coord) (cadar texture-coord))) + (draw-vertex v3 #:texture-coord (cadr texture-coord)) + (draw-vertex v4 #:texture-coord (list (caar texture-coord) (cadadr texture-coord))) (glEnd))) (color (with-color color (draw v1 v2 v3 v4))) (else (draw v1 v2 v3 v4)))) -(define* (draw-rectangle width height #:key texture color) +(define* (draw-rectangle width height #:key texture color texture-coord) (let ((w (/ width 2)) (h (/ height 2))) (draw-quad (list (- w) h 0) (list w h 0) (list w (- h) 0) (list (- w) (- h) 0) #:texture texture + #:texture-coord texture-coord #:color color))) -(define* (draw-square #:key (size 1) texture color) +(define* (draw-square size #:key texture color) (draw-rectangle size size #:texture texture #:color color)) (define* (draw-cube #:key (size 1) @@ -324,14 +383,14 @@ (glNormal3f -1 0 0) (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) #:texture (or texture-6 texture) #:color (or color-6 color))))) -(define* (translate x y #:optional (z 0)) +(define* (gtranslate x y #:optional (z 0)) (glTranslatef x y z)) -(define* (rotate #:rest rot) +(define (grotate . rot) (cond ((3d-mode?) (apply 3d-rotate rot)) (else - (apply 2d-rotate rot)))) + (2d-rotate (car (last-pair rot)))))) (define (3d-rotate xrot yrot zrot) (glRotatef xrot 1 0 0) @@ -345,6 +404,13 @@ (glLoadIdentity) (cond ((3d-mode?) (camera-look)))) +(define-macro (glmatrix-block . code) + `(let ((result #f)) + (glPushMatrix) + (set! result (begin ,@code)) + (glPopMatrix) + result)) + ;;; Lights @@ -374,12 +440,178 @@ ;;; Text and fonts -(define* (load-font font-file #:key (size 40) (encoding ft_encoding_unicode)) - (let ((font (ftglCreateTextureFont font-file))) +(define* (load-font-without-cache font-file #:key (size 40) (encoding ft_encoding_unicode)) + (let ((font (ftglCreateTextureFont font-file size))) (ftglSetFontFaceSize font size 72) (ftglSetFontCharMap font encoding) font)) +(define load-font (use-cache-with load-font-without-cache)) + (define* (render-text text font #:key (size #f)) - (cond (size (ftglSetFontFaceSize font size 72))) + (cond (size + (cond ((not (= (ftglGetFontFaceSize font) size)) + (ftglSetFontFaceSize font size 72)))) + ((not (= (ftglGetFontFaceSize font) (font-size font))) + (ftglSetFontFaceSize font (font-size font) 72))) (ftglRenderFont font text FTGL_RENDER_ALL)) + + +;;; Meshes + +(define mesh-type + (make-record-type "mesh" + '(draw translate turn rotate inner-properties inner-property properties properties-set! property property-set!) + (lambda (record port) + (format port "#" port)))) + +(define mesh? (record-predicate mesh-type)) + +(define* (make-mesh type proc) + (apply + (record-constructor mesh-type) + (let ((px 0) (py 0) (pz 0) + (ax 0) (ay 0) (az 0) + (rx 0) (ry 0) (rz 0) + (properties '())) + (let ((inner-properties + (lambda () + `((type . ,type) (x . ,px) (y . ,py) (z . ,pz) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz))))) + (list + (lambda () + "draw" + (glmatrix-block + (grotate ax ay az) + (gtranslate px py pz) + (grotate rx ry rz) + (proc properties))) + (lambda (x y z) + "translate" + (set! px (+ px x)) + (set! py (+ py y)) + (set! pz (+ pz z))) + (lambda (x y z) + "turn" + (set! ax (+ ax x)) + (set! ay (+ ay y)) + (set! az (+ az z))) + (lambda (x y z) + "rotate" + (set! rx (+ rx x)) + (set! ry (+ ry y)) + (set! rz (+ rz z))) + (lambda () + "inner-properties" + (inner-properties)) + (lambda (prop-name) + "inner-property" + (assoc-ref (inner-properties) prop-name)) + (lambda () + "properties" + properties) + (lambda (new-properties) + "properties-set!" + (set! properties new-properties)) + (lambda (prop-name) + "property" + (assoc-ref properties prop-name)) + (lambda (prop-name value) + "property-set!" + (set! properties (assoc-set! properties prop-name value)))))))) + +(define (mesh-draw mesh) + (((record-accessor mesh-type 'draw) mesh))) + +(define (mesh-inner-properties mesh) + (((record-accessor mesh-type 'inner-properties) mesh))) + +(define (mesh-inner-property mesh prop-name) + (((record-accessor mesh-type 'inner-property) mesh) prop-name)) + +(define (mesh-properties mesh) + (((record-accessor mesh-type 'properties) mesh))) + +(define (mesh-properties-set! mesh new-properties) + (((record-accessor mesh-type 'properties-set!) mesh) new-properties)) + +(define (mesh-property mesh prop-name) + (((record-accessor mesh-type 'property) mesh) prop-name)) + +(define (mesh-property-set! mesh prop-name value) + (((record-accessor mesh-type 'property-set!) mesh) prop-name value)) + +(define* (translate mesh x y #:optional (z 0)) + (((record-accessor mesh-type 'translate) mesh) x y z) + mesh) + +(define (turn mesh . params) + (apply ((record-accessor mesh-type 'turn) mesh) + (if (>= (length params) 3) + params + (list 0 0 (car params)))) + mesh) + +(define (rotate mesh . params) + (apply ((record-accessor mesh-type 'rotate) mesh) + (if (>= (length params) 3) + params + (list 0 0 (car params)))) + mesh) + + +;;; Advanced meshes + +(define (mesh-join . meshes) + (make-mesh + 'joined-meshes + (lambda (props) + (for-each (lambda (m) (glmatrix-block (mesh-draw m))) meshes)))) + + +;;; Primitives + +(define-macro (primitive header . body) + (let* ((type (car header)) + (args (cdr header)) + (list-args (names-arguments args))) + `(lambda* ,args + (let ((m (make-mesh + ',type + (lambda (props) + (apply (lambda* ,(cons #:key list-args) ,@body) + (list + ,@(let get-params ((l list-args)) + (cond ((null? l) '()) + (else + (cons (symbol->keyword (car l)) + (cons `(assoc-ref props ',(car l)) + (get-params (cdr l))))))))))))) + (mesh-properties-set! m (list ,@(map (lambda (a) `(cons ',a ,a)) list-args))) + m)))) + +(define-macro (define-primitive header . body) + `(define ,(car header) (primitive ,header ,@body))) + + +;;; Primitives definition + +(define-primitive (square size #:key texture color) + (draw-square size #:texture texture #:color color)) + +(define-primitive (rectangle width height #:key texture color texture-coord) + (draw-rectangle width height #:texture texture #:color color #:texture-coord texture-coord)) + +(define-primitive (circle radius) + (draw-circle radius)) + +(define-primitive (picture filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR) (zoom 1) (sprite '((0 0) (1 1)))) + (draw-texture (load-texture filename #:min-filter min-filter #:mag-filter mag-filter) #:zoom zoom #:sprite sprite)) + + +(module-map (lambda (sym var) + (if (not (eq? sym '%module-public-interface)) + (module-export! (current-module) (list sym)))) + (current-module))