Ported to OpenGL
[uxul-world.git] / game.lisp
index cc389c2..25ac600 100755 (executable)
--- a/game.lisp
+++ b/game.lisp
@@ -1,8 +1,10 @@
-;;; Copyright 2009 Christoph Senjak
+;;; Copyright 2009-2011 Christoph Senjak
 
 (in-package :uxul-world)
 
 (defparameter *cfont* nil)
+(defparameter *zoomx* 1.0)
+(defparameter *zoomy* 1.0)
 
 (defun run-testing-room ()
   (start-game :room-function #'make-testing-room))
   "Start the Game: Call room-function for getting the room-object to
 run. Music is ignored so far. 15-fps makes only every second frame be
 drawn (for very slow computers)"
-  (sdl:set-video-driver "directx")
-     (sdl:with-init (sdl:sdl-init-video sdl:sdl-init-audio)
+;  (sdl:set-video-driver "directx")
+     (sdl:with-init (sdl:sdl-init-video) ;sdl:sdl-init-video sdl:sdl-init-audio)
        (sdl:window +screen-width+ +screen-height+
                   :title-caption "Uxul World"
                   :icon-caption "Uxul World"
+                  :flags sdl:sdl-opengl
                   ;:opengl T
-                  :flags (logior sdl:sdl-hw-accel  sdl:sdl-hw-surface)
-                  :flags (logior sdl:sdl-hw-surface) #| sdl:sdl-fullscreen )|# 
-)
+                  ;:flags (logior sdl:sdl-hw-accel  sdl:sdl-hw-surface)
+                  ;:flags (logior sdl:sdl-hw-surface) #| sdl:sdl-fullscreen )|# 
+                  )
+       (setf cl-opengl-bindings:*gl-get-proc-address*
+            #'sdl-cffi::sdl-gl-get-proc-address)
        ;;(if music (sdl-mixer:OPEN-AUDIO :frequency 44100))
-       (let ((*graphics-table* (make-hash-table :test #'equal)))
+
+       (gl:hint :perspective-correction-hint :nicest)
+
+
+       (let ((*graphics-table* (make-hash-table :test #'equal))
+            (*zoomx* (/ 1.0 +screen-width+))
+            (*zoomxi* (/ .01 +screen-width+))
+            (*zoomy* (/ 1.0 +screen-height+))
+            (*zoomyi* (/ .01 +screen-height+))
+            (*zoom-ash* 0))
         (if 15-fps
             (setf (sdl:frame-rate) 15)
             (setf (sdl:frame-rate) 30))
         
         (setf *current-room* (funcall room-function))
 
-        (sdl:clear-display (sdl:color :r 0 :g 0 :b 0));; :update-p nil)
+        ;(sdl:clear-display (sdl:color :r 0 :g 0 :b 0));; :update-p nil)
 
         ;;(if music (sdl-mixer:play-sample levelmusic))
       
@@ -51,14 +65,21 @@ drawn (for very slow computers)"
                            (cond
                              ((sdl:key= key :SDL-KEY-ESCAPE)
                               (sdl:push-quit-event))
-                             ((sdl:key= key :SDL-KEY-O)
-                              (setf *zoom-ash*
-                                    (max -3 (1- *zoom-ash*))))
-                             ((sdl:key= key :SDL-KEY-I)
-                              (setf *zoom-ash*
-                                    (min 0 (1+ *zoom-ash*))))
+                             ((sdl:key= key :SDL-KEY-U)
+                              (incf *zoomx* *zoomxi*)
+                              (incf *zoomy* *zoomyi*))
+                             ((sdl:key= key :SDL-KEY-D)
+                              (decf *zoomx* *zoomxi*)
+                              (decf *zoomy* *zoomyi*))
                              (T
                               (on-key-down *current-room* key))))
+          (:mouse-button-down-event
+           (:button btn)
+           (cond
+             ((= btn sdl:mouse-wheel-up)
+              (incf *zoomx* *zoomxi*) (incf *zoomy* *zoomyi*))
+             ((= btn sdl:mouse-wheel-down)
+              (decf *zoomx* *zoomxi*) (decf *zoomy* *zoomyi*))))
           (:key-up-event (:key key)
                          (on-key-up *current-room* key))
           (:idle
@@ -66,46 +87,50 @@ drawn (for very slow computers)"
              (invoke *current-room*)
              (when 15-fps
                (invoke *current-room*))
-             (sdl:clear-display (sdl:color :r 128 :g 128 :b 128)); :update-p nil)
+             (gl:clear :color-buffer-bit :depth-buffer-bit)
+             (gl:enable :texture-2d)
+             (gl:enable :blend)
+             (gl:blend-func :src-alpha :one-minus-src-alpha)
+             (gl:load-identity)
              (draw *current-room*)
-             (sdl:update-display)
-        ))))))
+             (gl:flush)
+             (sdl:update-display)))))))
 
 
-;; For Debugging
+;; ;; For Debugging
 
-(defun preview-animation (frameskip &rest images)
+;; (defun preview-animation (frameskip &rest images)
 
-     (sdl:with-init (sdl:sdl-init-video sdl:sdl-init-audio)
-       (sdl:window +screen-width+ +screen-height+
-                  :title-caption "Uxul World"
-                  :icon-caption "Uxul World"
-                  :flags (logior sdl:sdl-hw-accel)
-                  #| :flags (logior sdl:sdl-hw-surface sdl:sdl-fullscreen )|#  )
-       (let ((*graphics-table*
-             #-ecl (trivial-garbage:make-weak-hash-table
-                    :weakness :value
-                    :test #'equal)
-             #+ecl (make-hash-table :test #'equal)
-             )
-            (my-anim (apply #'make-animation frameskip images))
-            )
+;;      (sdl:with-init (sdl:sdl-init-video sdl:sdl-init-audio)
+;;        (sdl:window +screen-width+ +screen-height+
+;;                :title-caption "Uxul World"
+;;                :icon-caption "Uxul World"
+;;                :flags (logior sdl:sdl-hw-accel)
+;;                #| :flags (logior sdl:sdl-hw-surface sdl:sdl-fullscreen )|#  )
+;;        (let ((*graphics-table*
+;;           #-ecl (trivial-garbage:make-weak-hash-table
+;;                  :weakness :value
+;;                  :test #'equal)
+;;           #+ecl (make-hash-table :test #'equal)
+;;           )
+;;          (my-anim (apply #'make-animation frameskip images))
+;;          )
         
-        (setf (sdl:frame-rate) 30)
-        (sdl:clear-display (sdl:color :r 64 :g 64 :b 46));; :update-p nil)
+;;      (setf (sdl:frame-rate) 30)
+;;      (sdl:clear-display (sdl:color :r 64 :g 64 :b 46));; :update-p nil)
       
-        (sdl:with-events ()
-          (:quit-event () t)
-          (:key-down-event (:key key)
-                           (cond
-                             ((sdl:key= key :SDL-KEY-ESCAPE)
-                              (sdl:push-quit-event))))
-          (:idle
-           (progn
-             (sdl:clear-display (sdl:color :r 64 :g 64 :b 46));; :update-p nil)
+;;      (sdl:with-events ()
+;;        (:quit-event () t)
+;;        (:key-down-event (:key key)
+;;                         (cond
+;;                           ((sdl:key= key :SDL-KEY-ESCAPE)
+;;                            (sdl:push-quit-event))))
+;;        (:idle
+;;         (progn
+;;           (sdl:clear-display (sdl:color :r 64 :g 64 :b 46));; :update-p nil)
 
 
-             (draw my-anim)
+;;           (draw my-anim)
              
-             (sdl:update-display)
-        ))))))
+;;           (sdl:update-display)
+;;      ))))))