From: christoph Date: Mon, 3 Jan 2011 02:07:25 +0000 (+0100) Subject: Now using OpenGL-VBOs. X-Git-Url: http://uxul.de/gitweb/?a=commitdiff_plain;h=8f6d2e9fa2cee1be6687044f2f4813630305682b;p=uxul-world.git Now using OpenGL-VBOs. --- diff --git a/files.lisp b/files.lisp index 3d1a253..f113408 100755 --- a/files.lisp +++ b/files.lisp @@ -36,6 +36,7 @@ relevant." (defvar *spritesheet*) (defvar *spritesheet-id*) +(defvar *buffer-id*) (defun init-bmp-files () (let* ((names (remove-if-not #'bmp-p diff --git a/game.lisp b/game.lisp index 1c02cc0..d238186 100755 --- a/game.lisp +++ b/game.lisp @@ -3,8 +3,10 @@ (in-package :uxul-world) (defparameter *cfont* nil) -(defparameter *zoomx* 1.0) -(defparameter *zoomy* 1.0) +(defvar *zoomx* 1.0) +(defvar *zoomy* 1.0) +(defvar *offset*) +(defvar *ptr*) (defun run-testing-room () (start-game :room-function #'make-testing-room)) @@ -16,6 +18,7 @@ (defun start-game (&key (music nil) (room-function #'make-additional-testing-room) (15-fps nil)) + (declare (ignore music)) "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)" @@ -37,11 +40,11 @@ drawn (for very slow computers)" (let ((*graphics-table* (make-hash-table :test #'equal)) (*spritesheet-id* (load-spritesheet)) + (*buffer-id* (car (gl:gen-buffers 1))) (*zoomx* (/ 1.0 +screen-width+)) - (*zoomxi* (/ .01 +screen-width+)) + (zoomxi (/ .01 +screen-width+)) (*zoomy* (/ 1.0 +screen-height+)) - (*zoomyi* (/ .01 +screen-height+)) - (*zoom-ash* 0)) + (zoomyi (/ .01 +screen-height+))) (if 15-fps (setf (sdl:frame-rate) 15) (setf (sdl:frame-rate) 30)) @@ -66,37 +69,68 @@ drawn (for very slow computers)" ((sdl:key= key :SDL-KEY-ESCAPE) (sdl:push-quit-event)) ((sdl:key= key :SDL-KEY-U) - (incf *zoomx* *zoomxi*) - (incf *zoomy* *zoomyi*)) + (incf *zoomx* zoomxi) + (incf *zoomy* zoomyi)) ((sdl:key= key :SDL-KEY-D) - (decf *zoomx* *zoomxi*) - (decf *zoomy* *zoomyi*)) + (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*)) + (incf *zoomx* zoomxi) (incf *zoomy* zoomyi)) ((= btn sdl:mouse-wheel-down) - (decf *zoomx* *zoomxi*) (decf *zoomy* *zoomyi*)))) + (decf *zoomx* zoomxi) (decf *zoomy* zoomyi)))) (:key-up-event (:key key) (on-key-up *current-room* key)) (:idle - (progn (invoke *current-room*) (when 15-fps (invoke *current-room*)) + (let ((float-size (cffi:foreign-type-size :float)) + (obj-num 0)) + (dolist (i (get-objects *current-room* + 'uxul-world::game-object)) + (and (redraw i) (visible i) (incf obj-num))) + (gl:clear :color-buffer-bit :depth-buffer-bit) - (gl:enable :texture-2d) - (gl:enable :blend) + (gl:enable :texture-2d :blend) (gl:blend-func :src-alpha :one-minus-src-alpha) (gl:load-identity) - (draw *current-room*) + (gl:bind-buffer :array-buffer *buffer-id*) + (%gl:buffer-data :array-buffer + (* float-size + 4 ; vertices per sprite + 4 ; components per vertice + obj-num) + (cffi:null-pointer) :stream-draw) + (gl:with-mapped-buffer + (p :array-buffer :write-only) + (let ((*ptr* p) (*offset* 0)) + (draw *current-room*))) + (%gl:tex-coord-pointer 2 :float + (* 4 ; components per vertex + float-size) + (cffi:null-pointer)) + (%gl:vertex-pointer 2 :float + (* 4 ; components per vertex + (cffi::foreign-type-size :float)) + (cffi:make-pointer (* 2 + float-size))) + (gl:enable-client-state :texture-coord-array) + (gl:enable-client-state :vertex-array) + (gl:bind-texture :texture-2d *spritesheet-id*) + (%gl:draw-arrays :quads 0 (* 4 obj-num)) + (gl:disable-client-state :vertex-array) + (gl:disable-client-state :texture-coord-array) (gl:flush) + (gl:bind-buffer :array-buffer 0) (sdl:update-display))))))) + ;; ;; For Debugging ;; (defun preview-animation (frameskip &rest images) diff --git a/opengl.lisp b/opengl.lisp index 61c8e90..e35026d 100644 --- a/opengl.lisp +++ b/opengl.lisp @@ -31,10 +31,12 @@ (destructuring-bind (x1 y1 x2 y2) imgs (setf x (- x +screen-width+)) (setf y (- y +screen-height+)) - (gl:bind-texture :texture-2d *spritesheet-id*) - (gl:with-primitive :quads - (gl:tex-coord x1 y1) (gl:vertex x (+ y h)) - (gl:tex-coord x2 y1) (gl:vertex (+ x w) (+ y h)) - (gl:tex-coord x2 y2) (gl:vertex (+ x w) y) - (gl:tex-coord x1 y2) (gl:vertex x y)))) - + ;(gl:bind-texture :texture-2d *spritesheet-id*) + (macrolet ((writedown (&rest vars) + `(progn + ,@(mapcar #'(lambda (var) + `(setf (cffi:mem-aref uxul-world::*ptr* :float (1- (incf uxul-world::*offset*))) (float ,var 0.0))) vars)))) + (writedown x1 y1 x (+ y h) + x2 y1 (+ x w) (+ y h) + x2 y2 (+ x w) y + x1 y2 x y)))) \ No newline at end of file