Hi I have been using clojure for a week now and I am working on
http://slick.cokeandcode.com/ to create games.

I use a proxy for BasicGame , which supplies me with (init) and the
game loop.

I have to initialize image-based resources either in the game loop or
at init so I defined everything @ init:

///////////////////// CODE:

(ns test
        (:import (org.newdawn.slick BasicGame GameContainer Graphics
SlickException AppGameContainer)
           (org.newdawn.slick.tiled TiledMap)
                   (org.newdawn.slick.util Log))
        )


(def map-file  "data/randmap.tmx")

(defn initialize-game [container]
do(
(Log/info "INIT FN START")
(def tiled-map (TiledMap. map-file))
(Log/info (str "tiled-map: " tiled-map))
(def tile-size (. tiled-map getTileWidth))
(Log/info (str "tiled-size: " tile-size))
(def ground-layer-index (. tiled-map getLayerIndex "ground"))

; caculate some layout values for rendering the tilemap. How many
tiles
; do we need to render to fill the screen in each dimension and how
far
; is
; it from the centre of the screen

(def display-height-in-tiles (/ (. container getHeight) tile-size))
(def display-width-in-tiles (/ (. container getWidth) tile-size))

; Hier enstehen kleine Ungereimtheiten wenn man den topOffset abzieht
; er aber 20,4 anstatt von z.b. 20 betrÃĪgt -> 0,4 verschobene map !

(def top-offset-in-tiles (int (/ display-height-in-tiles 2)))
(def left-offset-in-tiles (int (/ display-width-in-tiles 2)))

(def top-offset-in-tiles-buffer (- top-offset-in-tiles (/ display-
height-in-tiles 2)))

(def left-offset-in-tiles-buffer (- left-offset-in-tiles (/ display-
width-in-tiles 2)))

(Log/info "INIT FN FIN")
)
)

; image-based resources have to be loaded as part of init or the
gameloop
(def simple-test

        (proxy [BasicGame] ["SimpleTest"]

                (init [container] (initialize-game container) )

                (update [container delta] (println "update called"))

                (render [container g] (do (println "render
called!") ))
                                ))


(def container (new AppGameContainer simple-test))

(.start container)

///////////////////// CODE

1. Question:
This simplified code above throws a nullpointer exception.
Here is the exception (after the initialize do is completed, as you
can see with the LOG message):

user=> (use 'test)
Wed Apr 07 10:49:27 CEST 2010 INFO:Slick Build #230
Wed Apr 07 10:49:27 CEST 2010 INFO:LWJGL Version: 2.1.0
Wed Apr 07 10:49:27 CEST 2010 INFO:OriginalDisplayMode: 1280 x 800 x
32 @60Hz
Wed Apr 07 10:49:27 CEST 2010 INFO:TargetDisplayMode: 640 x 480 x 0
@0Hz
0
Wed Apr 07 10:49:27 CEST 2010 INFO:Starting display 640x480
Wed Apr 07 10:49:27 CEST 2010 INFO:Controllers not available
Wed Apr 07 10:49:27 CEST 2010 INFO:INIT FN START
Wed Apr 07 10:49:28 CEST 2010 INFO:tiled-map:
org.newdawn.slick.tiled.tiled...@b
aa466
Wed Apr 07 10:49:28 CEST 2010 INFO:tiled-size: 48
Wed Apr 07 10:49:28 CEST 2010 INFO:INIT FN FIN
java.lang.NullPointerException (test.clj:0)

The exception points to the last row of the do of initialize-game.

user=> (. *e printStackTrace)
java.lang.NullPointerException (test.clj:0)
        at clojure.lang.Compiler.eval(Compiler.java:4543)
        at clojure.lang.Compiler.load(Compiler.java:4857)
        at clojure.lang.RT.loadResourceScript(RT.java:326)
        at clojure.lang.RT.loadResourceScript(RT.java:317)
        at clojure.lang.RT.load(RT.java:395)
        at clojure.lang.RT.load(RT.java:367)
        at clojure.core$load__5058$fn__5061.invoke(core.clj:3734)
        at clojure.core$load__5058.doInvoke(core.clj:3733)
        at clojure.lang.RestFn.invoke(RestFn.java:413)
        at clojure.core$load_one__5010.invoke(core.clj:3578)
        at clojure.core$load_lib__5031.doInvoke(core.clj:3615)
        at clojure.lang.RestFn.applyTo(RestFn.java:147)
        at clojure.core$apply__3243.doInvoke(core.clj:390)
        at clojure.lang.RestFn.invoke(RestFn.java:443)
        at clojure.core$load_libs__5043.doInvoke(core.clj:3641)
        at clojure.lang.RestFn.applyTo(RestFn.java:142)
        at clojure.core$apply__3243.doInvoke(core.clj:390)
        at clojure.lang.RestFn.invoke(RestFn.java:460)
        at clojure.core$use__5052.doInvoke(core.clj:3711)
        at clojure.lang.RestFn.invoke(RestFn.java:413)
        at user$eval__1.invoke(NO_SOURCE_FILE:1)
        at clojure.lang.Compiler.eval(Compiler.java:4532)
        at clojure.core$eval__3990.invoke(core.clj:1728)
        at clojure.main
$repl__5813$read_eval_print__5825.invoke(main.clj:176)
        at clojure.main$repl__5813.doInvoke(main.clj:193)
        at clojure.lang.RestFn.invoke(RestFn.java:426)
        at clojure.main$repl_opt__5853.invoke(main.clj:247)
        at clojure.main$legacy_repl__5878.invoke(main.clj:288)
        at clojure.lang.Var.invoke(Var.java:346)
        at clojure.main.legacy_repl(main.java:29)
        at clojure.lang.Repl.main(Repl.java:20)
Caused by: java.lang.NullPointerException
        at test$initialize_game__7.invoke(test.clj:46)
        at test$fn__10$fn__18.invoke(test.clj:55)
        at clojure.proxy.org.newdawn.slick.BasicGame.init(Unknown
Source)
        at
org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:371)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:
88)
        at
clojure.lang.Reflector.invokeNoArgInstanceMember(Reflector.java:263)
        at test$eval__23.invoke(test.clj:66)
        at clojure.lang.Compiler.eval(Compiler.java:4532)
        ... 30 more
nil


If I remove the "do" at initialize-game it does not lead to an
exception.

Why does it throw the exception?

2. Question:
I read this in on clojure.org:

; Using def to modify the root value of a var at other than the top
level is usually an
; indication that you are using the var as a mutable global, and is
considered bad style.
; Consider either using binding to provide a thread-local value for
the var,
; or putting a ref or agent in the var and using transactions or
actions for mutation.

I have to initialize a tiled-map (special map created with the
tiled.org mapeditor) once.
So I want to create a var.  (If question 1 is resolved and I can use
initialize ... )

Should I use refs or is it okay to use (def ...) in initialize?

Because the values specified in "initialize-game" should not be
mutable!


-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to