Gauche-SDLã§ã©ã¤ãã²ã¼ã
æè¿ã«ãªã£ã¦Gaucheç¨ã®SDLã©ããã¼ãä½æãã¦ããã®ã§ãããã§ä¸ã¤å®£ä¼ããã¦ããã¾ãã
SDLã®è©³ç´°ã«é¢ãã¦ã¯æ¬å®¶ã¨ãã¦ã£ãããã£ã¢ã¨ãã§ã
ä½æãã¦ããGauche-SDLã¯ãã¡ãã®githubä¸ã«ããã¾ãã
ã¤ã³ã¹ãã¼ã«æ¹æ³ãªã©ããããã¯åããgithubä¸ã®Wikiã«ããã®ã§åç
§ãã¦ãã ããã
Windowsä¸ã§ã®åä½ã確èªã§ãã¦ãã¾ãããã³ã³ãã¤ã«ã«ã¯MinGWãå¿
è¦ã§ããæ°ãåãããã³ã³ãã¤ã«æ¸ã¿ã®ãã¤ããªãã©ããã§é
å¸ããããã
æå¾ã«ä¸ã¤ãµã³ãã«ãè¼ãã¦ããã¾ãã
ãã¡ãã®ãµã¤ããåèã«ãã¦ãã©ã¤ãã²ã¼ã ãGauche-SDLãå©ç¨ãã¦æ¸ãã¦ã¿ã¾ããã
(use sdl) (use sdl.gfx) (use math.mt-random) (use gauche.uvector) (define screen #f) (define-constant stride 100) (define world (make-u8vector (* stride stride) 0)) (define-macro (world-ref world y x) `(ref ,world (+ (* ,y ,stride) ,x))) (define-macro (world-set! world y x val) `(set! (ref ,world (+ (* ,y ,stride) ,x)) ,val)) (define (count-neighboring-individual y x world) (let ([next-y (if (eq? y (- stride 1)) 0 (+ y 1))] [prev-y (if (zero? y) (- stride 1) (- y 1))] [next-x (if (eq? x (- stride 1)) 0 (+ x 1))] [prev-x (if (zero? x) (- stride 1) (- x 1))]) (+ (world-ref world prev-y prev-x) (world-ref world prev-y x) (world-ref world prev-y next-x) (world-ref world y prev-x) (world-ref world y next-x) (world-ref world next-y prev-x) (world-ref world next-y x) (world-ref world next-y next-x)))) (define (update-next-generation world) (let ([w (u8vector-copy world)]) (dotimes [y stride #f] (dotimes [x stride #f] (let ([count (count-neighboring-individual y x world)]) (cond [(zero? (world-ref world y x)) (when (eq? count 3) (world-set! w y x 1))] [(or (>= count 4) (<= count 1)) (world-set! w y x 0)])))) w)) (define (initialize-world) (let ([m (make <mersenne-twister>)]) (dotimes [y stride #f] (dotimes [x stride #f] (world-set! world y x (if (< (mt-random-integer m 10) 1) 1 0)))))) (define update (let ([wait (/ 1000 3)] [next 0]) (lambda () (set! next (if (>= (sdl-get-ticks) next) (begin (set! world (update-next-generation world)) (+ next wait)) next))))) (define (draw) (let ([white (make-sdl-color 255 255 255)] [black (make-sdl-color 0 0 0)]) (dotimes [y stride #f] (dotimes [x stride #f] (gfx-box-color screen (* x 4) (* y 4) (+ (* x 4) 4) (+ (* y 4) 4) (if (zero? (world-ref world y x)) black white))))) (sdl-update-rect screen 0 0 0 0)) (define (initialize) (sdl-init SDL_INIT_VIDEO) (sdl-wm-set-caption "LifeGame -Gauche SDL-" #f) (set! screen (sdl-set-video-mode 400 400 32 SDL_SWSURFACE)) (initialize-world) ) (define-constant wait (/ 1000 60)) (define (main-loop) (let loop ([next-frame (sdl-get-ticks)]) (unless (let proc-event ([event (sdl-poll-event)]) (and event (or (eq? (ref event 'type) SDL_QUIT) (and (eq? (ref event 'type) SDL_KEYUP) (eq? (ref (ref event 'keysym) 'sym) SDLK_ESCAPE)) (proc-event (sdl-poll-event))))) (let ([next (if (>= (sdl-get-ticks) next-frame) (begin (update) (when (< (sdl-get-ticks) (+ next-frame wait)) (draw)) (+ next-frame wait)) next-frame)]) (sdl-delay 0) (loop next))))) (define (finalize) (sdl-quit)) (initialize) (main-loop) (finalize)
åä½ãã¦ããç»åã¯ãã¡ãã
ãµã³ãã«ãä½ã£ã¦ã¿ã¦æ°ä»ãããã©ãèãã©ããã¼ã ãã ã¨ã©ããSchemeã£ã½ãã³ã¼ãã«ãªããªãã
ããã¡ãã£ã¨æ½è±¡åº¦ãä¸ããã¬ã¤ã¤ãã¤ãããããªãã