]> git.jsancho.org Git - gacela.git/blobdiff - gacela.lisp
(no commit message)
[gacela.git] / gacela.lisp
index 617ad3ccd76185f18473dd2bc489962601917fe4..cd29c3e7e8a1a4a3c758eae9d975f275cba00cb9 100644 (file)
 (defvar *height-screen* 480)
 (defvar *bpp-screen* 32)
 (defvar *title-screen* "Happy Hacking!!")
-(defvar *gacela-freq* 30)
+(defvar *frames-per-second* 20)
 (defvar *transparent-color* '(:red 0 :green 0 :blue 0))
 (defvar *background-color* '(:red 0 :green 0 :blue 0))
-(defvar *zoom* -10)
 
 ;;; SDL Initialization Subsystem
 (let (initialized)
 
 
 (defun init-GL ()
+  (2d-mode)
   (glShadeModel GL_SMOOTH)
   (glClearColor 0 0 0 0)
   (glClearDepth 1)
-  (glEnable GL_DEPTH_TEST)
   (glDepthFunc GL_LEQUAL)
 ;  (glEnable GL_BLEND)
 ;  (glBlendFunc GL_SRC_ALPHA GL_ONE)
   (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
   t)
 
+(defmacro progn-textures (&body code)
+  `(let (values)
+     (init-video-mode)
+     (glEnable GL_TEXTURE_2D)
+     (setq values (multiple-value-list (progn ,@code)))
+     (glDisable GL_TEXTURE_2D)
+     (apply #'values values)))
+
 (defun init-textures ()
   (init-video-mode)
   (glEnable GL_TEXTURE_2D))
 
 (defun resize-screen-GL (width height)
   (let ((ratio (if (= height 0) width (/ width height))))
-    (glViewPort 0 0 width height)
+;    (glViewPort 0 0 width height)
     (glMatrixMode GL_PROJECTION)
     (glLoadIdentity)
     (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
       (glOrtho -w w -h h 0 1))
-    (gluPerspective 45 ratio 0.1 100)
+;    (gluPerspective 45 ratio 0.1 100)
     (glMatrixMode GL_MODELVIEW)
     (glLoadIdentity)
     t))
 
-(defun copy-surface (source)
-  (cond ((surface-p source)
-        (let ((new-surface
-               (make-surface :address (copy-SDL_Surface (surface-address source))
-                             :clip-w (surface-clip-w source)
-                             :clip-h (surface-clip-h source)
-                             :shape (surface-shape source))))
-          (set-resource 'image new-surface (gentemp))
-          new-surface))))
+(let ((current-color '(1 1 1 1)))
+  (defun get-current-color ()
+    current-color)
+
+  (defun set-current-color (red green blue &optional (alpha 1))
+    (setq current-color (list red green blue alpha))
+    (glColor4f red green blue alpha)))
 
 (defun load-image (image-file &key (transparent-color nil))
   (init-video-mode)
   (flip))
 
 
-(defun filled-circle (radius &optional (color '(:red 255 :green 255 :blue 255)))
-  (init-video-mode)
-  (let ((new-surface (create-surface (1+ (* radius 2)) (1+ (* radius 2)))))
-    (sge_FilledCircle (surface-address new-surface)
-                     radius radius radius
-                     (getf color :red)
-                     (getf color :green)
-                     (getf color :blue))
-    (setf (surface-shape new-surface)
-         `((,radius ,radius) ,radius))
-    new-surface))
-
-
-(defun filled-rect (width height &optional (color '(:red 255 :green 255 :blue 255)))
-  (init-video-mode)
-  (let ((new-surface (create-surface width height)))
-    (sge_FilledRect (surface-address new-surface)
-                   0 0 width height
-                   (getf color :red)
-                   (getf color :green)
-                   (getf color :blue))
-    (setf (surface-shape new-surface)
-         (make-rectangle 0 0 width height))
-    new-surface))
-
-
 ;;; TTF Subsystem
 (defstruct font address)
 
 
 
 ;;; Resources Manager
-(defstruct resource address free-function object)
+(defstruct resource plist free-function time)
+
+(defun make-texture (&key filename min-filter mag-filter)
+  `(:type texture :filename ,filename :min-filter ,min-filter :mag-filter ,mag-filter))
 
 (let ((resources-table (make-hash-table :test 'equal)))
 
-  (defun set-resource (type object &rest key)
-    (let ((res
-          (cond ((surface-p object)
-                 (make-resource :address (surface-address object)
-                                :free-function #'SDL_FreeSurface
-                                :object object))
-                ((font-p object)
-                 (make-resource :address (font-address object)
-                                :free-function #'TTF_CloseFont
-                                :object object))
-                ((cp-space-p object)
-                 (make-resource :address (cp-space-address object)
-                                :free-function #'cpSpaceFree
-                                :object object))
-                ((cp-body-p object)
-                 (make-resource :address (cp-body-address object)
-                                :free-function #'cpBodyFree
-                                :object object))
-                ((cp-shape-p object)
-                 (make-resource :address (cp-shape-address object)
-                                :free-function #'cpShapeFree
-                                :object object))
-                (t nil))))
-      (cond (res (setf (gethash `(,type ,@key) resources-table) res)))))
-
-  (defun get-resource (type &rest key)
-    (let ((resource (gethash `(,type ,@key) resources-table)))
+  (defun set-resource (key plist free-function &key static)
+    (setf (gethash key resources-table)
+         (make-resource :plist plist
+                        :free-function free-function
+                        :time (if static -1 (SDL_GetTicks)))))
+
+  (defun get-resource (key)
+    (let ((resource (gethash key resources-table)))
       (cond ((null resource) nil)
-           (t (resource-object resource)))))
+           (t (cond ((/= (resource-time resource) -1)
+                     (setf (resource-time resource) (SDL_GetTicks))
+                     (setf (gethash key resources-table) resource)))
+              (resource-plist resource)))))
 
   (defun free-all-resources ()
     (maphash (lambda (key res) (funcall (resource-free-function res) (resource-address res)))
 
 
 ;;; GaCeLa Functions
-;(defun game-loop (code)
-;  (process-events)
-;  (cond ((quit?) nil)
-;      (t
-;       (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
-;       (glLoadIdentity)
-;       (translate 0 0 *zoom*)
-;       (funcall code)
-;       (SDL_GL_SwapBuffers)
-;       (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))
-;       (game-loop code))))
-
 (let (commands)
   (defun prog-command (command)
     (setq commands (cons command commands)))
                                             (eval (read-from-string (concatenate 'string "(progn " (car comlst) ")")))))))
                     (run-com running)))))))
 
+(let (time (time-per-frame (/ 1000.0 *frames-per-second*)))
+  (defun set-frames-per-second (fps)
+    (setq time-per-frame (/ 1000.0 fps)))
+
+  (defun init-frame-time ()
+    (setq time (SDL_GetTicks)))
+
+  (defun delay-frame ()
+    (let ((frame-time (- (SDL_GetTicks) time)))
+      (cond ((< frame-time time-per-frame)
+            (SDL_Delay (- time-per-frame frame-time)))))))
+      
+
 (defmacro run-game (title &body code)
   `(progn
      (init-video-mode)
      (SDL_WM_SetCaption ,title "")
+     (init-frame-time)
      (process-events)
      (do () ((quit?))
         (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
         (glLoadIdentity)
-        (translate 0 0 *zoom*)
         ,@code
         (SDL_GL_SwapBuffers)
-        (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))
+        (delay-frame)
+        (init-frame-time)
         (process-events)
         (setq running nil))))
 
-;(defun run-game ()
-;  (init-video-mode)
-;  (SDL_WM_SetCaption *title-screen* "")
-;  (refresh-active-procs)
-;  (enjoy!)
-;  (do () ((quit?))
-;      (process-events)
-;      (logic-procs)
-;      (motion-procs)
-;      (refresh-active-procs)
-;      (refresh-screen)
-;      (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))))
-
 (defun quit-game ()
 ;  (free-all-resources)
 ;  (quit-audio)