#:use-module (gacela sdl)
#:use-module (gacela gl)
#:use-module (gacela ftgl)
+ #:use-module (gacela math)
#:use-module (ice-9 optargs)
#:use-module (ice-9 receive)
- #:export (with-color
+ #:export (init-video
+ get-screen-height
+ get-screen-width
+ get-screen-bpp
+ resize-screen
+ quit-video
+ clear-screen
+ flip-screen
+ set-2d-mode
+ set-3d-mode
+ 3d-mode?
+ set-frames-per-second
+ init-frame-time
+ get-frame-time
+ delay-frame
+ get-current-color
+ set-current-color
+ with-color
progn-textures
draw
- load-image
- resize-surface
load-texture
draw-texture
draw-line
draw-rectangle
draw-square
draw-cube
- add-light
translate
rotate
to-origin
+ add-light
set-camera
camera-look
load-font
render-text))
-(define get-current-color #f)
-(define set-current-color #f)
+;;; Screen
+
+(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 (get-screen-height)
+ (surface-h screen))
+
+(define (get-screen-width)
+ (surface-w screen))
+
+(define (get-screen-bpp)
+ (surface-format-BytesPerPixel screen))
+
+(define (resize-screen width height)
+ (cond (screen
+ (set! screen (SDL_SetVideoMode width height (get-screen-bpp) flags))
+ (resize-screen-GL width height))))
+
+(define (quit-video)
+ (cond (screen
+ (SDL_FreeSurface screen)
+ (set! screen #f)
+ (SDL_Quit))))
+
+(define (clear-screen)
+ (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)))
+
+(define (flip-screen)
+ (SDL_GL_SwapBuffers))
+
+
+(define mode '2d)
+
+(define (set-2d-mode)
+ (set! mode '2d)
+ (glDisable GL_DEPTH_TEST)
+ (resize-screen-GL (get-screen-width) (get-screen-height)))
+
+(define (set-3d-mode)
+ (set! mode '3d)
+ (glClearDepth 1)
+ (glEnable GL_DEPTH_TEST)
+ (glDepthFunc GL_LEQUAL)
+ (resize-screen-GL (get-screen-width) (get-screen-height)))
+
+(define (3d-mode?)
+ (eq? mode '3d))
+
+
+(define (init-gl)
+ (glShadeModel GL_SMOOTH)
+ (glClearColor 0 0 0 0)
+ (glEnable GL_BLEND)
+ (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
+ (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST))
+
+(define (resize-screen-GL width height)
+ (glViewport 0 0 width height)
+ (glMatrixMode GL_PROJECTION)
+ (glLoadIdentity)
+ (cond ((3d-mode?)
+ (let ((ratio (if (= height 0) width (/ width height))))
+ (gluPerspective 45 ratio 0.1 100)))
+ (else
+ (let* ((w (/ width 2)) (h (/ height 2)))
+ (glOrtho (- w) w (- h) h 0 1))))
+ (glMatrixMode GL_MODELVIEW)
+ (glLoadIdentity))
+
+
+;;; Frames per second
+
+(define set-frames-per-second #f)
+(define init-frame-time #f)
+(define get-frame-time #f)
+(define delay-frame #f)
+
+;; (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))))
+
+;; (set! init-frame-time
+;; (lambda ()
+;; (set! time (SDL_GetTicks))))
+
+;; (set! get-frame-time
+;; (lambda ()
+;; time))
+
+;; (set! delay-frame
+;; (lambda ()
+;; (let ((frame-time (- (SDL_GetTicks) time)))
+;; (cond ((< frame-time time-per-frame)
+;; (SDL_Delay (- time-per-frame frame-time))))))))
+
+
+;;; Drawing
-(let ((current-color '(1 1 1 1)))
- (set! get-current-color
- (lambda ()
- current-color))
+(define current-color '(1 1 1 1))
- (set! set-current-color
- (lambda* (red green blue #:optional (alpha 1))
- (set! current-color (list red green blue alpha))
- (glColor4f red green blue alpha))))
+(define (get-current-color)
+ current-color)
+
+(define* (set-current-color red green blue #:optional (alpha 1))
+ (set! current-color (list red green blue alpha))
+ (glColor4f red green blue alpha))
(define-macro (with-color color . code)
(cond (color
(SDL_DisplayFormatAlpha image)))))
(define (load-image-for-texture filename)
- (init-sdl)
(let ((image (load-image filename)))
(cond (image
(let* ((width (surface-w image)) (height (surface-h image))
(zoomSurface surface zoomx zoomy 0))))))
(define* (load-texture filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR))
- (let* ((key (list filename min-filter mag-filter))
- (res (get-resource-from-cache key)))
- (cond (res res)
- (else
- (progn-textures
- (receive
- (image real-w real-h) (load-image-for-texture filename)
- (cond (image
- (let ((width (surface-w image)) (height (surface-h image))
- (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
- (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
- (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
- (texture (car (glGenTextures 1))))
-
- (glBindTexture GL_TEXTURE_2D texture)
- (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
- (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
- (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
- (set-texture-size! texture real-w real-h)
- (insert-resource-into-cache key texture)
- texture)))))))))
-
-;; (define* (draw-image filename #:optional (zoom 1))
-;; (let ((texture (load-texture filename)))
-;; (cond (texture (draw-texture texture zoom)))))
+ (progn-textures
+ (receive
+ (image real-w real-h) (load-image-for-texture filename)
+ (cond (image
+ (let ((width (surface-w image)) (height (surface-h image))
+ (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
+ (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
+ (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
+ (texture (car (glGenTextures 1))))
+
+ (glBindTexture GL_TEXTURE_2D texture)
+ (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
+ (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
+ (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
+ (set-texture-size! texture real-w real-h)
+ texture))))))
(define* (draw-texture texture #:optional (zoom 1))
(cond (texture
(glNormal3f 0 1 0)
(draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) #:texture (or texture-3 texture) #:color (or color-3 color))
(glNormal3f 0 -1 0)
- (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture (or texture-4 texture) #:color (or color-4 color))
+ (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) #:texture (or texture-4 texture) #:color (or color-4 color))
(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-5 texture) #:color (or color-5 color))
+ (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) #:texture (or texture-5 texture) #:color (or color-5 color))
(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* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
- (init-lighting)
- (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
- (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
- (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
- (and turn-on (glEnable id))
- id)
+ (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))
(glTranslatef x y z))
(define* (rotate #:rest rot)
- (cond ((3d-mode?) (apply 3d-rotate rot))
- (else (apply 2d-rotate rot))))
+ (cond ((3d-mode?)
+ (apply 3d-rotate rot))
+ (else
+ (apply 2d-rotate rot))))
(define (3d-rotate xrot yrot zrot)
(glRotatef xrot 1 0 0)
(glLoadIdentity)
(cond ((3d-mode?) (camera-look))))
-(define set-camera #f)
-(define camera-look #f)
-(let ((camera-eye '(0 0 0)) (camera-center '(0 0 -100)) (camera-up '(0 1 0)))
- (set! set-camera
- (lambda* (#:key eye center up)
- (cond (eye (set! camera-eye eye)))
- (cond (center (set! camera-center center)))
- (cond (up (set! camera-up up)))))
+;;; Lights
+
+;; (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
+;; (init-lighting)
+;; (and light (glLightfv id GL_DIFFUSE (car light) (cadr light) (caddr light) (cadddr light)))
+;; (and light position (glLightfv GL_POSITION (car position) (cadr position) (caddr position) (cadddr position)))
+;; (and ambient (glLightfv id GL_AMBIENT (car ambient) (cadr ambient) (caddr ambient) (cadddr ambient)))
+;; (and turn-on (glEnable id))
+;; id)
+
+
+;;; Camera
+
+(define camera-eye '(0 0 0))
+(define camera-center '(0 0 -100))
+(define camera-up '(0 1 0))
+
+(define* (set-camera #:key eye center up)
+ (cond (eye (set! camera-eye eye)))
+ (cond (center (set! camera-center center)))
+ (cond (up (set! camera-up up))))
- (set! camera-look
- (lambda ()
- (apply gluLookAt (append camera-eye camera-center camera-up)))))
+(define (camera-look)
+ (apply gluLookAt (append camera-eye camera-center camera-up)))
;;; Text and fonts
(define* (load-font font-file #:key (size 40) (encoding ft_encoding_unicode))
- (let* ((key (list font-file))
- (font (get-resource-from-cache key)))
- (cond ((not font)
- (set! font (ftglCreateTextureFont font-file))
- (insert-resource-into-cache key font)))
+ (let ((font (ftglCreateTextureFont font-file)))
(ftglSetFontFaceSize font size 72)
(ftglSetFontCharMap font encoding)
font))