Merge: Some improvement of benchmarks
authorJean Privat <jean@pryen.org>
Wed, 16 Sep 2015 17:11:54 +0000 (13:11 -0400)
committerJean Privat <jean@pryen.org>
Wed, 16 Sep 2015 17:11:54 +0000 (13:11 -0400)
Some work in the benchmarks directory. The most important stuff is some `make check` to test the brokenness of specific benches.

Pull-Request: #1723
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Romain Chanoir <chanoir.romain@courrier.uqam.ca>

62 files changed:
contrib/crazy_moles/Makefile
contrib/friendz/Makefile
contrib/friendz/res/raw/music.ogg [new file with mode: 0644]
contrib/friendz/src/friendz.nit
contrib/friendz/src/grid.nit
contrib/friendz/src/level.nit
contrib/mnit_test/Makefile
contrib/mnit_test/org.nitlanguage.test_minimal.txt [new file with mode: 0644]
contrib/opportunity/src/opportunity_controller.nit
contrib/opportunity/src/opportunity_model.nit
contrib/tinks/Makefile
contrib/tinks/art/icon.svg
contrib/tinks/net.xymus.tinks.txt [new file with mode: 0644]
examples/calculator/Makefile
examples/calculator/art/icon.svg
examples/calculator/org.nitlanguage.calculator.txt [new file with mode: 0644]
examples/mnit_ballz/Makefile
examples/mnit_dino/Makefile
examples/rosettacode/sha_1.nit
examples/shoot/Makefile
lib/android/audio.nit
lib/android/native_app_glue.nit
lib/base64.nit
lib/c.nit
lib/core/bytes.nit
lib/core/stream.nit
lib/core/text/flat.nit
lib/egl.nit
lib/gamnit/display.nit [new file with mode: 0644]
lib/gamnit/display_android.nit [new file with mode: 0644]
lib/gamnit/display_linux.nit [new file with mode: 0644]
lib/gamnit/egl.nit [new file with mode: 0644]
lib/gamnit/examples/triangle/Makefile [new file with mode: 0644]
lib/gamnit/examples/triangle/art/icon.svg [new file with mode: 0644]
lib/gamnit/examples/triangle/bin/.gitignore [new file with mode: 0644]
lib/gamnit/examples/triangle/package.ini [new file with mode: 0644]
lib/gamnit/examples/triangle/res/.gitignore [new file with mode: 0644]
lib/gamnit/examples/triangle/src/portable_triangle.nit [new file with mode: 0644]
lib/gamnit/examples/triangle/src/standalone_triangle.nit [new file with mode: 0644]
lib/gamnit/gamnit.nit
lib/gamnit/gamnit_android.nit [new file with mode: 0644]
lib/glesv2/examples/opengles2_hello_triangle.nit
lib/glesv2/glesv2.nit
lib/linux/linux.nit
lib/matrix/matrix.nit [new file with mode: 0644]
lib/matrix/package.ini [new file with mode: 0644]
lib/matrix/projection.nit [new file with mode: 0644]
lib/mnit/linux/linux_app.nit
lib/sha1.nit
lib/websocket/websocket.nit
misc/README.md
misc/jenkins/check_contrib.sh
misc/vim/plugin/nit.vim
src/compiler/compiler_ffi/compiler_ffi.nit
src/nitcatalog.nit
src/platform/android.nit
tests/sav/Darwin/todo
tests/sav/nitcg/test_text_stat.res
tests/sav/nitserial_args1.res
tests/sav/test_bytes_hexdigit.res [new file with mode: 0644]
tests/sav/test_text_stat.res
tests/test_bytes_hexdigit.nit [new file with mode: 0644]

index 3fc90f7..113329d 100644 (file)
@@ -1,28 +1,35 @@
+
+SVG2ICONS=../inkscape_tools/bin/svg_to_icons
+SVG2PNG=../inkscape_tools/bin/svg_to_png_and_nit
+
 default: bin/moles
 
 bin/moles: $(shell ../../bin/nitls -M src/moles_linux.nit) assets/images/drawing.png
        mkdir -p bin
        ../../bin/nitc -o bin/moles src/moles_linux.nit
 
+android: bin/moles.apk
 bin/moles.apk: android-icons $(shell ../../bin/nitls -M src/moles_android.nit) assets/images/drawing.png
        mkdir -p bin
        ../../bin/nitc -o bin/moles.apk src/moles_android.nit
 
-android: bin/moles.apk
+android-release: android-icons $(shell ../../bin/nitls -M src/moles_android.nit) assets/images/drawing.png
+       mkdir -p bin
+       ../../bin/nitc -o bin/moles.apk src/moles_android.nit --release
 
-../inkscape_tools/bin/svg_to_icons:
+$(SVG2ICONS):
        $(MAKE) -C ../inkscape_tools
 
-android-icons: ../../contrib/inkscape_tools/bin/svg_to_icons art/icon.svg
+android-icons: $(SVG2ICONS) art/icon.svg
        mkdir -p res
-       ../inkscape_tools/bin/svg_to_icons art/icon.svg --android --out res/
+       $(SVG2ICONS) art/icon.svg --android --out res/
 
 android-install: bin/moles.apk
        adb install -rf bin/moles.apk
 
-assets/images/drawing.png: art/drawing.svg ../../contrib/inkscape_tools/bin/svg_to_icons
+assets/images/drawing.png: art/drawing.svg $(SVG2ICONS)
        mkdir -p assets/images
-       ../inkscape_tools/bin/svg_to_png_and_nit --src src/ --scale 2.0 art/drawing.svg
+       $(SVG2PNG) --src src/ --scale 2.0 art/drawing.svg
 
 check-android: bin/moles.apk
        ../../misc/jenkins/check_android.sh bin/moles.apk
index f236a1d..0cedb4c 100644 (file)
@@ -4,11 +4,18 @@ linux:
        mkdir -p bin
        ../../bin/nitc -o bin/friendz src/friendz_linux.nit
 
-android:
-       mkdir -p bin res
-       ../inkscape_tools/bin/svg_to_icons art/icon.svg --android --out res/
+android: res/drawable-hdpi/icon.png
+       mkdir -p bin
        ../../bin/nitc -o bin/friendz.apk src/friendz_android.nit
 
+android-release: res/drawable-hdpi/icon.png
+       mkdir -p bin
+       ../../bin/nitc -o bin/friendz.apk src/friendz_android.nit --release
+
+res/drawable-hdpi/icon.png: art/icon.svg
+       mkdir -p res
+       ../inkscape_tools/bin/svg_to_icons art/icon.svg --android --out res/
+
 doc:
        mkdir -p doc
        ../../bin/nitdoc -d doc/ src/friendz.nit src/friendz_linux.nit
diff --git a/contrib/friendz/res/raw/music.ogg b/contrib/friendz/res/raw/music.ogg
new file mode 100644 (file)
index 0000000..8dcfa3b
Binary files /dev/null and b/contrib/friendz/res/raw/music.ogg differ
index 9425abe..c0554fe 100644 (file)
@@ -19,7 +19,6 @@ import realtime
 import solver
 import mnit::tileset
 import app::data_store
-import md5
 
 intrude import grid
 intrude import level
@@ -217,11 +216,11 @@ class LevelButton
 
                self.over = self.level.fullname
                if self.level.get_state >= l.l_won then
-                       if game.levels[9].get_state >= l.l_won then self.over += " --- {self.level.score}/{self.level.par}"
+                       if game.levels[9].get_state >= l.l_won then self.over += " --- {self.level.score}/{self.level.gold}"
                else if self.level.get_state >= l.l_open then
-                       if game.levels[9].get_state >= l.l_open then self.over +=  " --- ?/{self.level.par}"
+                       if game.levels[9].get_state >= l.l_open then self.over +=  " --- ?/{self.level.gold}"
                end
-               #self.enabled = l.get_state >= l.l_open
+               self.enabled = l.get_state >= l.l_open or game.cheated
        end
 
        redef fun draw(ctx)
@@ -242,7 +241,7 @@ class LevelButton
                end
                ctx.blit(game.img[ix,iy], self.x, self.y)
 
-               if s == l.l_par then
+               if s == l.l_gold then
                        ctx.blit(game.img2[7,0], self.x + bw*5/8, self.y-bh*1/8)
                end
                ctx.textx(self.level.name, self.x+5, self.y+5, 24, null, null)
@@ -730,9 +729,9 @@ class Score
                end
                if game.levels[9].get_state >= level.l_won then
                        if level.is_challenge then
-                               ctx.textx("GOAL: {level.par}",self.x,self.y+44,21,"yellow",null)
+                               ctx.textx("GOAL: {level.gold}",self.x,self.y+44,21,"yellow",null)
                        else
-                               ctx.textx("PAR: {level.par}",self.x,self.y+44,21,"yellow",null)
+                               ctx.textx("GOLD: {level.gold}",self.x,self.y+44,21,"yellow",null)
                        end
                end
        end
@@ -777,7 +776,7 @@ class StatusBar
        do
                print "***STATUS** {txt}"
                self.tmp_txt = txt
-               self.tmp_txt_ttl = 20
+               self.tmp_txt_ttl = 60
                self.tmp_txt_color = color
        end
 
@@ -873,26 +872,6 @@ redef class Game
        # Font
        var font = new TileSetFont(app.load_image("deltaforce_font.png"), 16, 17, "ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789.:;!?\"'() -,/")
 
-       var xxx = """
-       fun save_cookie(name, val:String) do
-       var days = 365
-       var date = new Date()
-       date.setTime(date.getTime()+(days*24*60*60*1000))
-       document.cookie = name+"="+val+"; expires="+date.toGMTString()+"; path=/"
-       end
-
-       fun read_cookie(name:String):String do
-       var key = name + "="
-       var ca = document.cookie.split(';')
-       for(var i=0; i<ca.length; i++) do
-       var c = ca[i]
-       while (c[0]==' ') c = c.substring(1, c.length)
-       if (c.indexOf(key) == 0) return c.substring(key.length)
-       end
-       return null
-       end
-       """
-
        # DISPLAY *****************************************************************
 
        # Is the game in editing mode
@@ -904,10 +883,10 @@ redef class Game
        # SOUND
 
        # Is the music muted?
-       var music_muted: Bool = false #read_cookie("music_muted")
+       var music_muted: Bool = app.data_store["music_muted"] == true
 
        # Is the sound effects muted?
-       var sfx_muted: Bool = false #read_cookie("sfx_muted")
+       var sfx_muted: Bool = app.data_store["sfx_muted"] == true
 
        # The background music resource. */
        var music = new Music("music.ogg")
@@ -957,12 +936,16 @@ redef class Game
        # ResizeButton
        var button_size = new ResizeButton(self)
 
+       # Cheat mode enabled?
+       var cheated = false
+
        init
        do
-               load_levels
                init_buttons
                entities.clear
                title
+
+               if not music_muted then music.play
        end
 
        # fill `buttons`
@@ -976,8 +959,9 @@ redef class Game
        # Play a level in player mode.
        fun play(l: Level)
        do
+               save # save the previous level grid
                level = l
-               grid.load(level.str)
+               grid.load(level.saved_str or else level.str)
                init_play_menu(false)
                if level.status != "" then
                        statusbar.main_txt = level.status
@@ -1071,6 +1055,7 @@ redef class Game
        # Helper function to initialize the menu (and tile) screen
        fun init_menu
        do
+               save # save the previous level grid
                init_game
                level = null
                var i = levels.first
@@ -1094,7 +1079,7 @@ redef class Game
                end
                t = new Achievement(self, 0, "Training")
                entities.push(t)
-               t = new Achievement(self, 1, "Par")
+               t = new Achievement(self, 1, "Gold")
                entities.push(t)
                t = new Achievement(self, 2, "Editor")
                entities.push(t)
@@ -1225,8 +1210,19 @@ redef class Game
        fun onKeyDown(ev: Event) do
                var kc = ev.char_code
                if kc == "e" then
+                       set_tmp("RUN EDITOR")
                        grid_edit = grid.copy(true)
                        edit_grid(grid)
+               else if kc == "c" then
+                       if cheated then
+                               set_tmp("CHEAT: OFF")
+                               snd_duh.play
+                               cheated = false
+                       else
+                               set_tmp("CHEAT: ON")
+                               snd_win.play
+                               cheated = true
+                       end
                else if kc == "s" then
                        if solver == null then
                                solver = (new FriendzProblem(grid)).solve
@@ -1234,20 +1230,28 @@ redef class Game
                        else
                                solver_pause = not solver_pause
                        end
+                       if solver_pause then
+                               set_tmp("SOLVER: PAUSED")
+                       else
+                               set_tmp("SOLVER: ON")
+                       end
                        #solver.step
                else if kc == "d" then
                        if solver == null then
                                solver = (new FriendzProblem(grid)).solve
                                solver_pause = true
+                               set_tmp("SOLVER: ON")
                        else
+                               solver_pause = true
                                solver.run_steps(1)
+                               set_tmp("SOLVER: ONE STEP")
                        end
                else if kc == "+" then
                        solver_steps += 100
-                       print solver_steps
+                       set_tmp("SOLVER: {solver_steps} STEPS")
                else if kc == "-" then
                        solver_steps -= 100
-                       print solver_steps
+                       set_tmp("SOLVER: {solver_steps} STEPS")
                else for g in entities do
                        if kc == g.shortcut then
                                g.click(ev)
@@ -1256,15 +1260,33 @@ redef class Game
                end
        end
 
+       fun set_tmp(s: String)
+       do
+               statusbar.set_tmp(s, "cyan")
+       end
+
        redef fun load_levels
        do
                super
 
                for level in levels do
-                       var score = app.data_store["s{level.str.md5}"]
+                       var score = app.data_store["s{level.str}"]
                        if score isa Int then
                                level.score = score
                        end
+                       var saved_str = app.data_store["g{level.str}"]
+                       if saved_str isa String then
+                               print "LOAD {level.name}: {saved_str}"
+                               level.saved_str = saved_str
+                       end
+               end
+       end
+
+       fun save
+       do
+               var l = level
+               if l != null then
+                       l.save
                end
        end
 end
@@ -1359,12 +1381,13 @@ class MusicButton
        init(game: Game)
        do
                super(game, "MUSIC", 470, 412, "purple", "Mute the music", "Unmute the music")
+               toggled = game.music_muted
        end
        redef fun click2(ev)
        do
                game.music_muted = self.toggled
                if game.music_muted then game.music.pause else game.music.play
-               #game.save_cookie("music_muted",music_muted?"true":"")
+               app.data_store["music_muted"] = game.music_muted
        end
 end
 
@@ -1373,13 +1396,14 @@ class SFXButton
        init(game: Game)
        do
                super(game, "SOUND FX", 470, 382, "purple", "Mute the sound effects", "Unmute the sound effects")
+               toggled = game.sfx_muted
        end
 
        redef fun click2(ev)
        do
                game.sfx_muted = self.toggled
                if not game.sfx_muted then game.snd_whip.play # Because the automatic one was muted
-               #save_cookie("sfx_muted",sfx_muted?"true":"")
+               app.data_store["sfx_muted"] = game.sfx_muted
        end
 end
 
@@ -1579,6 +1603,12 @@ redef class App
                # img loading?
        end
 
+       redef fun on_pause
+       do
+               super
+               game.save
+       end
+
        # Maximum wanted frame per second
        var max_fps = 30
 
@@ -1636,8 +1666,16 @@ redef class KeyEvent
 end
 
 redef class Level
-       redef fun save
+       # Save the score and grid of the level
+       fun save
        do
-               app.data_store["s{str.md5}"] = if score > 0 then score else null
+               app.data_store["s{str}"] = if score > 0 then score else null
+               var saved = game.grid.save
+               saved_str = saved
+               app.data_store["g{str}"] = saved
+               print "SAVE: {name}: {saved}"
        end
+
+       # The saved player grid (to continue games)
+       var saved_str: nullable String = null
 end
index a097ff5..3a141f6 100644 (file)
@@ -191,14 +191,14 @@ class Grid
        fun save: String
        do
                var res = ""
-               var str = ".#ABCDEFGHI"
+               var str = ".abcdefghi#ABCDEFGHI"
                for y in [0..height[ do
                        var rle = 0
                        var last: nullable Int = null
                        for x in [0..width[ do
                                var t = self.grid[x][y]
-                               var tk = 0
-                               if t.fixed then tk = t.kind + 1
+                               var tk = t.kind
+                               if t.fixed then tk += 10
                                if tk == last and rle<9 then
                                        rle += 1
                                else
@@ -243,6 +243,7 @@ class Grid
                                        x += 1
                                else if c == '#' then
                                        var t = self.get(x,y)
+                                       assert t != null
                                        t.fixed = true
                                        x += 1
                                else if c >= 'A' and c <= 'I' then
@@ -251,6 +252,11 @@ class Grid
                                        t.update(c.ascii-'A'.ascii+1)
                                        t.fixed = true
                                        x += 1
+                               else if c >= 'a' and c <= 'i' then
+                                       var t = self.get(x,y)
+                                       assert t != null
+                                       t.update(c.ascii-'a'.ascii+1)
+                                       x += 1
                                else if c >= '1' and c <= '9' then
                                        rle = c.to_i
                                else
@@ -261,7 +267,7 @@ class Grid
                if x>0 then y += 1
                if x > mx then mx = x
                if y > my then my = y
-               if mx<3 or my<3 or mx>=max_width or my>=max_height then
+               if mx<3 or my<3 or mx>max_width or my>max_height then
                        return false
                end
                self.resize(mx,my)
index 9d8029e..b09f1f2 100644 (file)
@@ -23,7 +23,7 @@ class Level
                var ls = code.split(";")
                self.number = i
                self.str = ls[0]
-               self.par = ls[1].to_i
+               self.gold = ls[1].to_i
                if ls.length >= 3 then
                        self.status = ls[2]
                end
@@ -47,8 +47,8 @@ class Level
        # initial grid position
        var str: String
 
-       # top score
-       var par: Int
+       # top score to get gold
+       var gold: Int
 
        # Help message if any
        var status: String = ""
@@ -72,32 +72,28 @@ class Level
        var l_disabled = 1
        var l_open = 2
        var l_won = 3
-       var l_par = 4
+       var l_gold = 4
 
        fun get_state: Int
        do
                if self.score == 0 then
                        if self.number == 0 or game.levels[self.number-1].score > 0 then return l_open
                        if self.number == 25 and game.levels[19].score > 0 then return l_open else return l_disabled
-               else if self.score < self.par or not game.levels[9].score > 0 then
+               else if self.score < self.gold or not game.levels[9].score > 0 then
                        return l_won
-               else return l_par
+               else return l_gold
        end
 
        # Returns true if g is a wining condition for the level.
        fun check_won(g: Grid): Bool
        do
-               var w = g.won and (not self.is_challenge or g.number >= self.par)
+               var w = g.won and (not self.is_challenge or g.number >= self.gold)
                if not w then return false
                if g.number > self.score then
                        self.score = g.number
-                       self.save
                end
                return true
        end
-
-       # Save the score of the level
-       fun save do end
 end
 
 # main game object
index 73ed663..a198c27 100644 (file)
@@ -9,5 +9,10 @@ android:
        ../../bin/nitc -o bin/complete.apk src/complete_simple_android.nit --semi-global
        ../../bin/nitc -o bin/minimal.apk src/simple_android.nit --semi-global
 
+android-release:
+       mkdir -p bin
+       ../../bin/nitc -o bin/complete.apk src/complete_simple_android.nit --semi-global --release
+       ../../bin/nitc -o bin/minimal.apk src/simple_android.nit --semi-global --release
+
 clean:
        rm -rf bin
diff --git a/contrib/mnit_test/org.nitlanguage.test_minimal.txt b/contrib/mnit_test/org.nitlanguage.test_minimal.txt
new file mode 100644 (file)
index 0000000..9f3bb3c
--- /dev/null
@@ -0,0 +1,10 @@
+Categories:Nit
+License:Apache2
+Web Site:http://nitlanguage.org
+Source Code:http://nitlanguage.org/nit.git/tree/HEAD:/contrib/mnit_test
+Issue Tracker:https://github.com/nitlang/nit/issues
+
+Summary:Minimal Demo for MNit
+Description:
+A useless application that test various basic API usage of the mnit framework on Android.
+.
index 868dd9e..26a4ef6 100644 (file)
@@ -16,7 +16,6 @@
 module opportunity_controller
 
 import nitcorn
-import sha1
 import templates
 import opportunity_model
 
index ef7654d..cc72f23 100644 (file)
@@ -247,7 +247,7 @@ class Meetup
        redef fun commit(db) do
                if id == "" then
                        var time = get_time
-                       var tmpid = (name + date + place + time.to_s).sha1_to_s
+                       var tmpid = (name + date + place + time.to_s).sha1.hexdigest
                        if not db.execute("INSERT INTO meetups (id, name, date, place, answer_mode) VALUES({tmpid.to_sql_string}, {name.html_escape.to_sql_string}, {date.html_escape.to_sql_string}, {place.html_escape.to_sql_string}, {answer_mode});") then
                                print "Error recording entry Meetup {self}"
                                print db.error or else "Null error"
index b22453a..ee13360 100644 (file)
@@ -32,9 +32,12 @@ src/server/server_serialize.nit: $(shell ../../bin/nitls -M src/server/dedicated
        ../../bin/nitserial -o src/server/server_serialize.nit src/server/dedicated.nit
 
 # Android
+android: bin/tinks.apk
 bin/tinks.apk: assets/images/drawing.png src/client/client_serialize.nit res/drawable-ldpi/icon.png $(shell ../../bin/nitls -M src/client/android_client.nit)
-       ../../bin/nitc -o bin/tinks.apk src/client/android_client.nit -m src/client/client_serialize.nit --compile-dir nit_compile
-       adb install -r bin/tinks.apk
+       ../../bin/nitc -o bin/tinks.apk src/client/android_client.nit -m src/client/client_serialize.nit
+
+android-release: assets/images/drawing.png src/client/client_serialize.nit res/drawable-ldpi/icon.png $(shell ../../bin/nitls -M src/client/android_client.nit)
+       ../../bin/nitc -o bin/tinks.apk src/client/android_client.nit -m src/client/client_serialize.nit --release
 
 res/drawable-ldpi/icon.png: art/icon.svg
        ../inkscape_tools/bin/svg_to_icons art/icon.svg --android --out res/
index 20a590d..32229e4 100644 (file)
      inkscape:pageopacity="0.0"
      inkscape:pageshadow="2"
      inkscape:zoom="1.979899"
-     inkscape:cx="107.98668"
-     inkscape:cy="65.759563"
+     inkscape:cx="357.89925"
+     inkscape:cy="19.247995"
      inkscape:document-units="px"
      inkscape:current-layer="0splash"
      showgrid="false"
          transform="matrix(0.9571749,0,0,0.9571749,14.241487,126.08446)">
         <path
            style="font-size:52.50814056px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:-1px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Armalite Rifle;-inkscape-font-specification:Armalite Rifle"
-           d="m 258.625,595.8125 c -4.89966,1.4313 -12.56953,-1.70866 -15.1875,4.5625 -0.4775,5.90634 -3.5509,14.50575 2.71875,18.6875 2.28891,1.18746 5.12,1.54986 7.5625,0.59375 0.52227,3.72431 1.0541,7.99473 -2.03125,10.59375 -3.51238,6.69348 3.86364,14.87896 10.90625,12.96875 6.54546,1.2404 16.20527,-0.87285 16.34375,-9 1.15076,-4.65522 -4.58661,-6.69297 -3.46875,-11.1875 -0.95739,-2.94159 3.31489,-1.69978 4.46875,-3.78125 5.83868,-2.11249 5.55685,-9.07334 5.53125,-14.34375 1.81324,-6.47371 -5.8435,-9.85183 -11.15625,-8.5625 -4.34595,0.15294 -9.06169,-1.83389 -13.21875,-0.125 0.0516,-2.21144 -1.36209,-0.68823 -2.46875,-0.40625 z"
+           d="m 258.625,595.8125 c -4.89966,1.4313 -12.56953,-1.70866 -15.1875,4.5625 -0.4775,5.90634 -3.5509,14.50575 2.71875,18.6875 2.28891,1.18746 5.12,1.54986 7.5625,0.59375 0.52227,3.72431 1.0541,7.99473 -2.03125,10.59375 -3.51238,6.69348 3.86364,14.87896 10.90625,12.96875 6.54546,1.2404 16.20527,-0.87285 16.34375,-9 1.15076,-4.65522 0.20769,-5.09487 1.32555,-9.5894 -0.95739,-2.94159 -1.47941,-3.29788 -0.32555,-5.37935 5.83868,-2.11249 5.55685,-9.07334 5.53125,-14.34375 1.81324,-6.47371 -5.8435,-9.85183 -11.15625,-8.5625 -4.34595,0.15294 -9.06169,-1.83389 -13.21875,-0.125 0.0516,-2.21144 -1.36209,-0.68823 -2.46875,-0.40625 z"
            id="path3211"
-           inkscape:connector-curvature="0" />
+           inkscape:connector-curvature="0"
+           sodipodi:nodetypes="ccccccccccccc" />
         <path
            style="font-size:52.50814056px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:-1px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Armalite Rifle;-inkscape-font-specification:Armalite Rifle"
            d="m 286.25,592.6875 c -2.39783,0.63195 -3.74457,2.90332 -6.59375,2.8125 -5.4632,2.29227 -6.35616,10.35289 -2.0625,14.25 -0.60542,4.15669 0.70607,8.39731 0.4375,12.3125 -0.0599,3.57375 -1.98208,6.31549 -3.3125,9.34375 -1.62554,5.38708 2.55698,12.80186 8.96875,10.96875 6.47834,0.22337 16.30714,2.49861 19.125,-5.53125 1.23942,-3.73894 -0.38135,-7.90712 -3.59375,-10.09375 0.3082,-5.37065 -0.71586,-11.3191 0.59375,-16.375 6.74005,-4.69817 1.76294,-18.17585 -6.625,-15.0625 -2.79844,0.69065 -4.06188,-3.31293 -6.9375,-2.625 z"
            inkscape:connector-curvature="0" />
         <path
            style="font-size:52.50814056px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:-1px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Armalite Rifle;-inkscape-font-specification:Armalite Rifle"
-           d="m 362.71875,595.53125 c -4.88629,0.1191 -10.06204,0.70685 -14.78125,0.75 -5.47787,-0.76679 -11.36437,-0.40783 -16.78125,-0.15625 -5.17104,2.08849 -5.75771,9.90066 -1.65625,13.3125 -0.73815,5.29915 0.55713,11.35287 -0.28125,16.21875 -1.57391,2.43976 0.0174,5.43494 -1.9375,7.65625 -1.21338,8.01052 8.60308,12.27891 15.15625,10.1875 4.10866,1.15111 8.03361,-0.96492 11.90625,0.75 4.70244,-0.0919 9.41322,-0.76127 13.9375,-1.21875 6.34357,-2.47232 7.27867,-12.62071 1.1875,-15.90625 -2.84504,-3.85906 -6.71699,-8.25024 -7.5,-12.8125 2.57537,-4.33547 10.38664,-3.74912 10.125,-10.0625 0.19504,-4.87459 -4.47645,-9.43549 -9.375,-8.71875 z"
+           d="m 362.71875,595.53125 c -4.88629,0.1191 -10.06204,0.70685 -14.78125,0.75 -5.47787,-0.76679 -11.36437,-0.40783 -16.78125,-0.15625 -5.17104,2.08849 -5.75771,9.90066 -1.65625,13.3125 -0.73815,5.29915 0.55713,11.35287 -0.28125,16.21875 -1.57391,2.43976 0.0174,5.43494 -1.9375,7.65625 -1.21338,8.01052 8.60308,12.27891 15.15625,10.1875 4.10866,1.15111 8.03361,-0.96492 11.90625,0.75 4.70244,-0.0919 9.41322,-0.76127 13.9375,-1.21875 6.34357,-2.47232 7.27867,-12.62071 1.1875,-15.90625 -2.84504,-3.85906 -3.69836,-8.25024 -4.48137,-12.8125 2.57537,-4.33547 7.36801,-3.74912 7.10637,-10.0625 0.19504,-4.87459 -4.47645,-9.43549 -9.375,-8.71875 z"
            id="path3217"
-           inkscape:connector-curvature="0" />
+           inkscape:connector-curvature="0"
+           sodipodi:nodetypes="ccccccccccccc" />
         <path
            style="font-size:52.50814056px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:-1px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Armalite Rifle;-inkscape-font-specification:Armalite Rifle"
            d="m 387.9375,595.15625 c -4.67722,2.02978 -9.78906,-1.62403 -14.46875,1.25 -9.65979,4.02935 -13.06716,17.5954 -7.09375,25.71875 -5.60204,3.65765 -3.86108,11.71823 -2.5625,17.15625 1.89135,4.23664 7.4795,4.74734 11.125,3.125 3.94271,1.07416 7.9082,-0.0824 11.78125,1.1875 12.90654,0.16282 22.05847,-17.35307 13.34375,-27.46875 4.21701,-4.46396 3.58479,-12.34862 1.53125,-17.90625 -3.12179,-4.23683 -8.89538,-1.69513 -13.1875,-3.09375 l -0.46875,0.0312 z"
            inkscape:connector-curvature="0" />
         <path
            style="font-size:52.50814056px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:-1px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Armalite Rifle;-inkscape-font-specification:Armalite Rifle"
-           d="m 413.09375,593.625 c -5.989,0.79809 -12.31873,3.77439 -13.59375,10.28125 -2.37552,6.92367 2.58427,13.98287 3.0625,20.28125 -6.66505,6.69348 -1.32209,20.82556 8.75,19.25 7.10297,0.39661 12.84047,-7.75209 9.8125,-14.21875 0.3156,-2.89071 -3.70654,-4.76237 -1.46875,-7.53125 2.44848,-7.39695 6.24638,-18.36454 -1.53125,-23.75 -0.23875,-2.6484 -2.19852,-4.85675 -5.03125,-4.3125 z"
+           d="m 413.09375,593.625 c -5.989,0.79809 -12.31873,3.77439 -13.59375,10.28125 -2.77433,6.83505 1.00514,13.63195 3.0625,20.28125 -6.66505,6.69348 -1.32209,20.82556 8.75,19.25 7.10297,0.39661 12.84047,-7.75209 9.8125,-14.21875 0.3156,-2.89071 -3.70654,-4.76237 -1.46875,-7.53125 2.44848,-7.39695 6.24638,-18.36454 -1.53125,-23.75 -0.23875,-2.6484 -2.19852,-4.85675 -5.03125,-4.3125 z"
            id="path3221"
-           inkscape:connector-curvature="0" />
+           inkscape:connector-curvature="0"
+           sodipodi:nodetypes="cccccccc" />
       </g>
       <g
          style="stroke:#000000;stroke-width:3.85294461;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
            x="251.05626"
            id="tspan2996"
            sodipodi:role="line">TINKS!</tspan></text>
+      <text
+         xml:space="preserve"
+         style="font-size:50.25947571px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#e6e6e6;fill-opacity:1;stroke:none;font-family:Sans"
+         x="251.05626"
+         y="809.98505"
+         id="text3032"
+         sodipodi:linespacing="125%"><tspan
+           sodipodi:role="line"
+           id="tspan3034"
+           x="251.05626"
+           y="809.98505"
+           style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;letter-spacing:-0.9571749px;fill:#e6e6e6;fill-opacity:1;font-family:Armalite Rifle;-inkscape-font-specification:Armalite Rifle">TINKS!</tspan></text>
     </g>
   </g>
 </svg>
diff --git a/contrib/tinks/net.xymus.tinks.txt b/contrib/tinks/net.xymus.tinks.txt
new file mode 100644 (file)
index 0000000..ceed2bc
--- /dev/null
@@ -0,0 +1,13 @@
+Categories:Nit,Games
+License:Apache2
+Web Site:http://nitlanguage.org
+Source Code:http://nitlanguage.org/nit.git/tree/HEAD:/contrib/tinks
+Issue Tracker:https://github.com/nitlang/nit/issues
+
+Summary:Multiplayer crossplatform action game with destructible procedurally generated worlds
+Description:
+Each player controls a tank, opens fire from the turret and navigates between the terrain features.
+Explosions from turret fire and tank destruction have different forces and destroy the terrain.
+Each tank needs 4 hits to be destroyed.
+Destroyed tanks drop health power-ups which can repair other tanks.
+.
index edcfc14..24b10ab 100644 (file)
@@ -1,17 +1,26 @@
 NITC=../../bin/nitc
 NITLS=../../bin/nitls
 
-all: bin/calculator bin/calculator.apk bin/test
+all: bin/calculator bin/test
 
 bin/calculator: $(shell ${NITLS} -M src/calculator.nit -m linux) ${NITC}
        mkdir -p bin
        ${NITC} -o $@ src/calculator.nit -m linux
 
-bin/calculator.apk: $(shell ${NITLS} -M src/calculator.nit -m android) ${NITC} ../../contrib/inkscape_tools/bin/svg_to_icons
-       mkdir -p bin res
-       ../../contrib/inkscape_tools/bin/svg_to_icons art/icon.svg --android --out res/
+android: bin/calculator.apk
+
+bin/calculator.apk: $(shell ${NITLS} -M src/calculator.nit -m android) ${NITC} res/drawable-ldpi/icon.png
+       mkdir -p bin
        ${NITC} -o $@ src/calculator.nit -m ../../lib/android/ui/ -D debug
 
+android-release: $(shell ${NITLS} -M src/calculator.nit -m android) ${NITC} res/drawable-ldpi/icon.png
+       mkdir -p bin
+       ${NITC} -o bin/calculator.apk src/calculator.nit -m ../../lib/android/ui/ --release
+
+res/drawable-ldpi/icon.png: art/icon.svg ../../contrib/inkscape_tools/bin/svg_to_icons
+       mkdir -p res
+       ../../contrib/inkscape_tools/bin/svg_to_icons art/icon.svg --android --out res/
+
 ../../contrib/inkscape_tools/bin/svg_to_icons:
        make -C ../../contrib/inkscape_tools/
 
index ccd533c..618dc3d 100644 (file)
@@ -24,9 +24,9 @@
      borderopacity="1.0"
      inkscape:pageopacity="0.0"
      inkscape:pageshadow="2"
-     inkscape:zoom="0.9899495"
-     inkscape:cx="58.64005"
-     inkscape:cy="380.00465"
+     inkscape:zoom="1.4"
+     inkscape:cx="160.56125"
+     inkscape:cy="282.40083"
      inkscape:document-units="px"
      inkscape:current-layer="layer1"
      showgrid="false"
        width="211.42856"
        id="rect2995"
        style="fill:#cccccc;fill-opacity:1;stroke:#000000;stroke-opacity:1;stroke-width:5;stroke-miterlimit:4;stroke-dasharray:none" />
-    <text
-       xml:space="preserve"
+    <g
        style="font-size:269.1137085px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Droid Sans;-inkscape-font-specification:Droid Sans"
-       x="299.74573"
-       y="770.06946"
-       id="text2997"
-       sodipodi:linespacing="125%"><tspan
-         sodipodi:role="line"
-         id="tspan2999"
-         x="299.74573"
-         y="770.06946">+</tspan></text>
+       id="text2997">
+      <path
+         d="m 364.00188,685.0516 -50.85303,0 0,-19.71047 50.85303,0 0,-51.11584 19.71048,0 0,51.11584 50.85303,0 0,19.71047 -50.85303,0 0,50.59023 -19.71048,0 0,-50.59023"
+         style=""
+         id="path2996" />
+    </g>
     <path
        inkscape:connector-curvature="0"
        id="path3018"
        id="path2997"
        style="font-size:202.30386353px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Droid Sans;-inkscape-font-specification:Droid Sans"
        d="m 179.68975,735.88154 c -1e-5,-2.50244 0.32926,-4.60977 0.98782,-6.32199 0.65852,-1.77805 1.54755,-3.19391 2.66709,-4.2476 1.1195,-1.11949 2.43658,-1.90974 3.95125,-2.37074 1.51462,-0.52681 3.16097,-0.79023 4.93905,-0.79025 1.71218,2e-5 3.32561,0.26344 4.84028,0.79025 1.58047,0.461 2.93048,1.25125 4.05003,2.37074 1.11948,1.05369 2.00851,2.46955 2.66709,4.2476 0.65851,1.71222 0.98778,3.81955 0.98782,6.32199 -4e-5,2.43661 -0.32931,4.54394 -0.98782,6.322 -0.65858,1.71221 -1.54761,3.12807 -2.66709,4.24759 -1.11955,1.11952 -2.46956,1.94269 -4.05003,2.46953 -1.51467,0.52683 -3.1281,0.79024 -4.84028,0.79025 -1.77808,-10e-6 -3.42443,-0.26342 -4.93905,-0.79025 -1.51467,-0.52684 -2.83175,-1.35001 -3.95125,-2.46953 -1.11954,-1.11952 -2.00857,-2.53538 -2.66709,-4.24759 -0.65856,-1.77806 -0.98783,-3.88539 -0.98782,-6.322" />
-    <text
-       xml:space="preserve"
+    <g
        style="font-size:269.1137085px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Droid Sans;-inkscape-font-specification:Droid Sans"
-       x="64.031456"
-       y="1006.8839"
-       id="text3013"
-       sodipodi:linespacing="125%"><tspan
-         sodipodi:role="line"
-         id="tspan3015"
-         x="64.031456"
-         y="1006.8839">=</tspan></text>
+       id="text3013">
+      <path
+         d="m 77.43458,894.9284 0,-19.57907 121.41654,0 0,19.57907 -121.41654,0 m 0,53.87531 0,-19.71048 121.41654,0 0,19.71048 -121.41654,0"
+         style=""
+         id="path2999" />
+    </g>
   </g>
 </svg>
diff --git a/examples/calculator/org.nitlanguage.calculator.txt b/examples/calculator/org.nitlanguage.calculator.txt
new file mode 100644 (file)
index 0000000..264d18b
--- /dev/null
@@ -0,0 +1,12 @@
+Categories:Nit
+License:Apache2
+Web Site:http://nitlanguage.org
+Source Code:http://nitlanguage.org/nit.git/tree/HEAD:/examples/calculator
+Issue Tracker:https://github.com/nitlang/nit/issues
+
+Summary:A Basic Calculator
+Description:
+10 digits, 4 operations, hours of fun.
+
+This is the official example of a portable GUI application built using app.nit.
+.
index 4120b52..b9a0dfc 100644 (file)
@@ -7,6 +7,10 @@ android: icon
        mkdir -p bin
        ../../bin/nitc -o bin/ballz.apk src/ballz_android.nit
 
+android-release: icon
+       mkdir -p bin
+       ../../bin/nitc -o bin/ballz.apk src/ballz_android.nit --release
+
 linux:
        mkdir -p bin
        ../../bin/nitc -o bin/ballz src/ballz_linux.nit
index 42364bf..14faa98 100644 (file)
@@ -8,6 +8,10 @@ android: android-icons
        mkdir -p bin
        ../../bin/nitc -o bin/dino.apk src/dino_android.nit
 
+android-release: android-icons
+       mkdir -p bin
+       ../../bin/nitc -o bin/dino.apk src/dino_android.nit --release
+
 ../../contrib/inkscape_tools/bin/svg_to_icons:
        $(MAKE) -C ../../contrib/inkscape_tools
 
index 2eaf471..8776d48 100644 (file)
@@ -9,4 +9,4 @@ module sha_1
 
 import sha1
 
-print "Rosetta Code".sha1_to_s
+print "Rosetta Code".sha1.hexdigest
index 5e564d7..a42ffde 100644 (file)
@@ -8,6 +8,10 @@ android:
        mkdir -p bin
        ../../bin/nitc -o bin/shoot.apk src/shoot_android.nit
 
+android-release:
+       mkdir -p bin
+       ../../bin/nitc -o bin/shoot.apk src/shoot_android.nit --release
+
 null:
        mkdir -p bin
        ../../bin/nitc -o bin/shoot_null src/shoot_null.nit
index b729219..9cc54d7 100644 (file)
@@ -138,7 +138,6 @@ private extern class NativeMediaPlayer in "Java" `{ android.media.MediaPlayer `}
                        self.setDataSource(fd, start_offset, length);
                        return 1;
                }catch(Exception e) {
-                       Log.e("Error loading the Media Player with a file descriptor", e.getMessage());
                        return 0;
                }
        `}
@@ -378,6 +377,16 @@ class MediaPlayer
 
        # Load a sound for a given resource id
        fun load_sound(id: Int, context: NativeActivity): Music do
+               # FIXME: maybe find a better way to handle this situation
+               # If two different music are loaded with the same `MediaPlayer`,
+               # a new `NativeMediaPlayer` will be created for the secondd music
+               # and the nit program will loose the handle to the previous one
+               # If the previous music is playing, we need to stop it
+               if playing then
+                       stop
+                       reset
+                       destroy
+               end
                self.nmedia_player = self.nmedia_player.create(context, id)
                if self.nmedia_player.is_java_null then
                        self.error = new Error("Failed to load a sound")
@@ -483,6 +492,10 @@ class MediaPlayer
 end
 
 redef class PlayableAudio
+       # Flag to know if the user paused the sound
+       # Used when the app pause all sounds or resume all sounds
+       var paused: Bool = false
+
        redef init do add_to_sounds(self)
 end
 
@@ -537,19 +550,21 @@ redef class Sound
        end
 
        redef fun play do
-               if self.error != null then return
                if not is_loaded then load
+               if self.error != null then return
                soundpool.play(soundpool_id)
        end
 
        redef fun pause do
                if self.error != null or not self.is_loaded then return
                soundpool.pause_stream(soundpool_id)
+               paused = true
        end
 
        redef fun resume do
                if self.error != null or not self.is_loaded then return
                soundpool.resume(soundpool_id)
+               paused = false
        end
 
 end
@@ -599,19 +614,21 @@ redef class Music
        end
 
        redef fun play do
-               if self.error != null then return
                if not is_loaded then load
+               if self.error != null then return
                media_player.start
        end
 
        redef fun pause do
                if self.error != null or not self.is_loaded then return
                media_player.pause
+               paused = true
        end
 
        redef fun resume do
                if self.error != null or not self.is_loaded then return
                play
+               paused = false
        end
 end
 
@@ -675,7 +692,16 @@ redef class App
 
        redef fun on_pause do
                super
-               for s in sounds do s.pause
+               for s in sounds do
+                       # Pausing sounds that are not already paused by user
+                       # `s.paused` is set to false because `pause` set it to true
+                       # and we want to know which sound has been paused by the user
+                       # and which one has been paused by the app
+                       if not s.paused then
+                               s.pause
+                               s.paused = false
+                       end
+               end
                audio_manager.abandon_audio_focus
        end
 
@@ -688,7 +714,10 @@ redef class App
        redef fun on_resume do
                super
                audio_manager.request_audio_focus
-               for s in sounds do s.resume
+               for s in sounds do
+                       # Resumes only the sounds paused by the App
+                       if not s.paused then s.resume
+               end
        end
 end
 
index 2202827..d99f5a9 100644 (file)
@@ -368,4 +368,13 @@ end
 
 # Android NDK's structure to control the native window for drawing
 extern class ANativeWindow `{ ANativeWindow* `}
+       # Change the format and size of the window buffers
+       #
+       # All arguments can be set to 0 to use the default devices values.
+       # `width` and `height` must both be set to 0 or have significant values.
+       #
+       # `format` is a value specified by EGL.
+       fun set_buffers_geometry(width, height, format: Int): Bool `{
+               return ANativeWindow_setBuffersGeometry(self, (int32_t)width, (int32_t)height, (int32_t)format);
+       `}
 end
index 5f1c920..4eae395 100644 (file)
 # Offers the base 64 encoding and decoding algorithms
 module base64
 
-redef class String
-
+redef class NativeString
        # Alphabet used by the base64 algorithm
-       private fun base64_chars : String
+       private fun base64_chars : SequenceRead[Byte]
        do
-               return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
+               return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".bytes
        end
+
+       # Reversed alphabet for base64
        private fun inverted_base64_chars : HashMap[Byte, Byte]
        do
                var inv_base64_chars = new HashMap[Byte, Byte]
-               for k in [0..base64_chars.bytelen[ do
-                       inv_base64_chars[base64_chars.bytes[k]] = k.to_b
+               var l = base64_chars.length
+               for k in [0 .. l[ do
+                       inv_base64_chars[base64_chars[k]] = k.to_b
                end
                return inv_base64_chars
        end
 
-       # Encodes the receiver string to base64.
+       # Encodes `self` to base64.
+       #
        # By default, uses "=" for padding.
-       fun encode_base64 : String do return encode_base64_custom_padding('='.ascii.to_b)
-
-       # Encodes the receiver string to base64 using a custom padding character.
        #
-       # If using the default padding character `=`, see `encode_base64`.
-       fun encode_base64_custom_padding(padding : Byte) : String
-       do
-               var base64_bytes = once base64_chars.bytes
-               var length = bytelen
-
+       #     assert "string".encode_base64 == "c3RyaW5n"
+       private fun encode_base64(length: Int, padding: nullable Byte): Bytes do
+               var base64_bytes = once base64_chars
+               if padding == null then padding = '='.ascii.to_b
                var steps = length / 3
                var bytes_in_last_step = length % 3
                var result_length = steps * 4
                if bytes_in_last_step > 0 then result_length += 4
-               var result = new NativeString(result_length + 1)
-               var bytes = self.bytes
-               result[result_length] = 0u8
-
-               var mask_6bit = 0b0011_1111
+               var result = new Bytes.with_capacity(result_length)
 
+               var in_off = 0
                for s in [0 .. steps[ do
-                       var e = 0
-                       for ss in [0 .. 3[ do
-                               e += bytes[s * 3 + ss].to_i << ((2 - ss) * 8)
-                       end
-                       for ss in [0..4[ do
-                               result[s * 4 + 3 - ss] = base64_bytes[(e >> (ss * 6)) & mask_6bit]
-                       end
+                       var ind = ((self[in_off] & 0b1111_1100u8) >> 2).to_i
+                       result.add base64_bytes[ind]
+                       ind = ((self[in_off] & 0b0000_0011u8) << 4).to_i | ((self[in_off + 1] & 0b1111_0000u8) >> 4).to_i
+                       result.add base64_bytes[ind]
+                       ind = ((self[in_off + 1] & 0b0000_1111u8) << 2).to_i | ((self[in_off + 2] & 0b1100_0000u8) >> 6).to_i
+                       result.add base64_bytes[ind]
+                       ind = (self[in_off + 2] & 0b0011_1111u8).to_i
+                       result.add base64_bytes[ind]
+                       in_off += 3
                end
-
-               var out_off = result_length - 4
-               var in_off = length - bytes_in_last_step
                if bytes_in_last_step == 1 then
-                       result[out_off] = base64_bytes[((bytes[in_off] & 0b1111_1100u8) >> 2).to_i]
-                       result[out_off + 1] = base64_bytes[((bytes[in_off] & 0b0000_0011u8) << 4).to_i]
-                       out_off += 2
+                       result.add base64_bytes[((self[in_off] & 0b1111_1100u8) >> 2).to_i]
+                       result.add base64_bytes[((self[in_off] & 0b0000_0011u8) << 4).to_i]
                else if bytes_in_last_step == 2 then
-                       result[out_off] = base64_bytes[((bytes[in_off] & 0b1111_1100u8) >> 2).to_i]
-                       result[out_off + 1] = base64_bytes[(((bytes[in_off] & 0b0000_0011u8) << 4) | ((bytes[in_off + 1] & 0b1111_0000u8) >> 4)).to_i]
-                       result[out_off + 2] = base64_bytes[((bytes[in_off + 1] & 0b0000_1111u8) << 2).to_i]
-                       out_off += 3
-               end
-               if bytes_in_last_step > 0 then
-                       for i in [out_off .. result_length[ do result[i] = padding
+                       result.add base64_bytes[((self[in_off] & 0b1111_1100u8) >> 2).to_i]
+                       result.add base64_bytes[(((self[in_off] & 0b0000_0011u8) << 4) | ((self[in_off + 1] & 0b1111_0000u8) >> 4)).to_i]
+                       result.add base64_bytes[((self[in_off + 1] & 0b0000_1111u8) << 2).to_i]
                end
+               var rempad = if bytes_in_last_step > 0 then 3 - bytes_in_last_step else 0
+               for i in [0 .. rempad[ do result.add padding
 
-               return result.to_s_with_length(result_length)
+               return result
        end
 
-       # Decodes the receiver string from base64.
-       # By default, uses "=" for padding.
-       fun decode_base64 : String do return decode_base64_custom_padding('='.ascii.to_b)
-
-       # Decodes the receiver string to base64 using a custom padding character.
+       # Decodes `self` from base64
        #
-       # If using the default padding character `=`, see `decode_base64`.
-       fun decode_base64_custom_padding(padding : Byte) : String
-       do
+       #      assert "c3RyaW5n".decode_base64 == "string"
+       #
+       # REQUIRE: `length % 4 == 0`
+       private fun decode_base64(length: Int, padding: nullable Byte): Bytes do
+               if padding == null then padding = '='.ascii.to_b
                var inv = once inverted_base64_chars
-               var length = bytelen
-               if length == 0 then return ""
+               if length == 0 then return new Bytes.empty
                assert length % 4 == 0 else print "base64::decode_base64 only supports strings of length multiple of 4"
 
-               var bytes = self.bytes
+               var bytes = self
                var steps = length / 4
                var result_length = steps * 3
 
@@ -113,17 +101,16 @@ redef class String
                if padding_len == 1 then result_length -= 1
                if padding_len == 2 then result_length -= 2
 
-               var result = new NativeString(result_length + 1)
-               result[result_length] = 0u8
+               var result = new Bytes.with_capacity(result_length + 1)
 
                for s in [0 .. steps[ do
                        var c0 = inv[bytes[s * 4]]
                        var c1 = inv[bytes[s * 4 + 1]]
                        var c2 = inv[bytes[s * 4 + 2]]
                        var c3 = inv[bytes[s * 4 + 3]]
-                       result[s * 3] = ((c0 & 0b0011_1111u8) << 2) | ((c1 & 0b0011_0000u8) >> 4)
-                       result[s * 3 + 1] = ((c1 & 0b0000_1111u8) << 4) | ((c2 & 0b0011_1100u8) >> 2)
-                       result[s * 3 + 2] = ((c2 & 0b0000_0011u8) << 6) | (c3 & 0b0011_1111u8)
+                       result.add (((c0 & 0b0011_1111u8) << 2) | ((c1 & 0b0011_0000u8) >> 4))
+                       result.add (((c1 & 0b0000_1111u8) << 4) | ((c2 & 0b0011_1100u8) >> 2))
+                       result.add (((c2 & 0b0000_0011u8) << 6) | (c3 & 0b0011_1111u8))
                end
 
                var last_start = steps * 4
@@ -131,14 +118,52 @@ redef class String
                        var c0 = inv[bytes[last_start]]
                        var c1 = inv[bytes[last_start + 1]]
                        var c2 = inv[bytes[last_start + 2]]
-                       result[result_length - 2] = ((c0 & 0b0011_1111u8) << 2) | ((c1 & 0b0011_0000u8) >> 4)
-                       result[result_length - 1] = ((c1 & 0b0000_1111u8) << 4) | ((c2 & 0b0011_1100u8) >> 2)
+                       result.add (((c0 & 0b0011_1111u8) << 2) | ((c1 & 0b0011_0000u8) >> 4))
+                       result.add (((c1 & 0b0000_1111u8) << 4) | ((c2 & 0b0011_1100u8) >> 2))
                else if padding_len == 2 then
                        var c0 = inv[bytes[last_start]]
                        var c1 = inv[bytes[last_start + 1]]
-                       result[result_length - 1] = ((c0 & 0b0011_1111u8) << 2) | ((c1 & 0b0011_0000u8) >> 4)
+                       result.add (((c0 & 0b0011_1111u8) << 2) | ((c1 & 0b0011_0000u8) >> 4))
                end
 
-               return result.to_s_with_length(result_length)
+               return result
+       end
+end
+
+redef class Bytes
+
+       # Encodes the receiver string to base64 using a custom padding character.
+       #
+       # If using the default padding character `=`, see `encode_base64`.
+       fun encode_base64(padding: nullable Byte): Bytes
+       do
+               return items.encode_base64(length, padding)
+       end
+
+       # Decodes the receiver string to base64 using a custom padding character.
+       #
+       # Default padding character `=`
+       fun decode_base64(padding : nullable Byte) : Bytes
+       do
+               return items.decode_base64(length, padding)
+       end
+end
+
+redef class String
+
+       # Encodes the receiver string to base64 using a custom padding character.
+       #
+       # If using the default padding character `=`, see `encode_base64`.
+       fun encode_base64(padding: nullable Byte): String
+       do
+               return to_cstring.encode_base64(bytelen, padding).to_s
+       end
+
+       # Decodes the receiver string to base64 using a custom padding character.
+       #
+       # Default padding character `=`
+       fun decode_base64(padding : nullable Byte) : String
+       do
+               return to_cstring.decode_base64(bytelen, padding).to_s
        end
 end
index deb6765..a9b04e1 100644 (file)
--- a/lib/c.nit
+++ b/lib/c.nit
@@ -93,8 +93,8 @@ class CIntArray
                super size
        end
 
-       # Build from an `Array[Int]`
-       new from(array: Array[Int])
+       # Create from an `SequenceRead[Int]`
+       new from(array: SequenceRead[Int])
        do
                var carray = new CIntArray(array.length)
                for i in array.length.times do
@@ -129,7 +129,7 @@ class CByteArray
                super size
        end
 
-       # Build from a `SequenceRead[Byte]`
+       # Create from a `SequenceRead[Byte]`
        new from(array: SequenceRead[Byte])
        do
                var carray = new CByteArray(array.length)
@@ -166,8 +166,8 @@ class CNativeStringArray
                super size
        end
 
-       # Build from an `Array[NativeString]`
-       new from(array: Array[NativeString])
+       # Create from an `SequenceRead[NativeString]`
+       new from(array: SequenceRead[NativeString])
        do
                var carray = new CNativeStringArray(array.length)
                for i in array.length.times do
index 59c4c5f..356f386 100644 (file)
@@ -19,6 +19,61 @@ import kernel
 import collection::array
 intrude import text::flat
 
+redef class Byte
+       # Write self as a string into `ns` at position `pos`
+       private fun add_digest_at(ns: NativeString, pos: Int) do
+               var tmp = (0xF0u8 & self) >> 4
+               ns[pos] = if tmp >= 0x0Au8 then tmp + 0x37u8 else tmp + 0x30u8
+               tmp = 0x0Fu8 & self
+               ns[pos + 1] = if tmp >= 0x0Au8 then tmp + 0x37u8 else tmp + 0x30u8
+       end
+
+       # Is `self` a valid hexadecimal digit (in ASCII)
+       #
+       # ~~~nit
+       # intrude import core::bytes
+       # assert not '/'.ascii.to_b.is_valid_hexdigit
+       # assert '0'.ascii.to_b.is_valid_hexdigit
+       # assert '9'.ascii.to_b.is_valid_hexdigit
+       # assert not ':'.ascii.to_b.is_valid_hexdigit
+       # assert not '@'.ascii.to_b.is_valid_hexdigit
+       # assert 'A'.ascii.to_b.is_valid_hexdigit
+       # assert 'F'.ascii.to_b.is_valid_hexdigit
+       # assert not 'G'.ascii.to_b.is_valid_hexdigit
+       # assert not '`'.ascii.to_b.is_valid_hexdigit
+       # assert 'a'.ascii.to_b.is_valid_hexdigit
+       # assert 'f'.ascii.to_b.is_valid_hexdigit
+       # assert not 'g'.ascii.to_b.is_valid_hexdigit
+       # ~~~
+       private fun is_valid_hexdigit: Bool do
+               return (self >= 0x30u8 and self <= 0x39u8) or
+                      (self >= 0x41u8 and self <= 0x46u8) or
+                      (self >= 0x61u8 and self <= 0x66u8)
+       end
+
+       # `self` as a hexdigit to its byte value
+       #
+       # ~~~nit
+       # intrude import core::bytes
+       # assert 0x39u8.hexdigit_to_byteval == 0x09u8
+       # assert 0x43u8.hexdigit_to_byteval == 0x0Cu8
+       # ~~~
+       #
+       # REQUIRE: `self.is_valid_hexdigit`
+       private fun hexdigit_to_byteval: Byte do
+               if self >= 0x30u8 and self <= 0x39u8 then
+                       return self - 0x30u8
+               else if self >= 0x41u8 and self <= 0x46u8 then
+                       return self - 0x37u8
+               else if self >= 0x61u8 and self <= 0x66u8 then
+                       return self - 0x57u8
+               end
+               # Happens only if the requirement is not met.
+               # i.e. this abort is here to please the compiler
+               abort
+       end
+end
+
 # A buffer containing Byte-manipulation facilities
 #
 # Uses Copy-On-Write when persisted
@@ -26,7 +81,7 @@ class Bytes
        super AbstractArray[Byte]
 
        # A NativeString being a char*, it can be used as underlying representation here.
-       private var items: NativeString
+       var items: NativeString
 
        # Number of bytes in the array
        redef var length
@@ -63,6 +118,20 @@ class Bytes
                return items[i]
        end
 
+       # Returns self as a hexadecimal digest
+       fun hexdigest: String do
+               var elen = length * 2
+               var ns = new NativeString(elen)
+               var i = 0
+               var oi = 0
+               while i < length do
+                       self[i].add_digest_at(ns, oi)
+                       i += 1
+                       oi += 2
+               end
+               return new FlatString.full(ns, elen, 0, elen - 1, elen)
+       end
+
        #     var b = new Bytes.with_capacity(1)
        #     b[0] = 101u8
        #     assert b.to_s == "e"
@@ -146,80 +215,13 @@ class Bytes
        redef fun to_s do
                persisted = true
                var b = self
-               if not is_utf8 then
-                       b = clean_utf8
-                       persisted = false
-               end
-               return new FlatString.with_infos(b.items, b.length, 0, b.length -1)
+               var r = b.items.to_s_with_length(length)
+               if r != items then persisted = false
+               return r
        end
 
        redef fun iterator do return new BytesIterator.with_buffer(self)
 
-       # Is the byte collection valid UTF-8 ?
-       fun is_utf8: Bool do
-               var charst = once [0x80u8, 0u8, 0xE0u8, 0xC0u8, 0xF0u8, 0xE0u8, 0xF8u8, 0xF0u8]
-               var lobounds = once [0, 0x80, 0x800, 0x10000]
-               var hibounds = once [0x7F, 0x7FF, 0xFFFF, 0x10FFFF]
-               var pos = 0
-               var len = length
-               var mits = items
-               while pos < len do
-                       var nxst = mits.length_of_char_at(pos)
-                       var charst_index = (nxst - 1) * 2
-                       if mits[pos] & charst[charst_index] == charst[charst_index + 1] then
-                               var c = mits.char_at(pos)
-                               var cp = c.ascii
-                               if cp <= hibounds[nxst - 1] and cp >= lobounds[nxst - 1] then
-                                       if cp >= 0xD800 and cp <= 0xDFFF or
-                                          cp == 0xFFFE or cp == 0xFFFF then return false
-                               else
-                                       return false
-                               end
-                       else
-                               return false
-                       end
-                       pos += nxst
-               end
-               return true
-       end
-
-       # Cleans the bytes of `self` to be UTF-8 compliant
-       private fun clean_utf8: Bytes do
-               var charst = once [0x80u8, 0u8, 0xE0u8, 0xC0u8, 0xF0u8, 0xE0u8, 0xF8u8, 0xF0u8]
-               var badchar = once [0xEFu8, 0xBFu8, 0xBDu8]
-               var lobounds = once [0, 0x80, 0x800, 0x10000]
-               var hibounds = once [0x7F, 0x7FF, 0xFFFF, 0x10FFFF]
-               var pos = 0
-               var len = length
-               var ret = new Bytes.with_capacity(len)
-               var mits = items
-               while pos < len do
-                       var nxst = mits.length_of_char_at(pos)
-                       var charst_index = (nxst - 1) * 2
-                       if mits[pos] & charst[charst_index] == charst[charst_index + 1] then
-                               var c = mits.char_at(pos)
-                               var cp = c.ascii
-                               if cp <= hibounds[nxst - 1] and cp >= lobounds[nxst - 1] then
-                                       if cp >= 0xD800 and cp <= 0xDFFF or
-                                          cp == 0xFFFE or cp == 0xFFFF then
-                                               ret.append badchar
-                                               pos += 1
-                                       else
-                                               var pend = pos + nxst
-                                               for i in [pos .. pend[ do ret.add mits[i]
-                                               pos += nxst
-                                       end
-                               else
-                                       ret.append badchar
-                                       pos += 1
-                               end
-                       else
-                               ret.append badchar
-                               pos += 1
-                       end
-               end
-               return ret
-       end
 end
 
 private class BytesIterator
@@ -231,7 +233,7 @@ private class BytesIterator
 
        var max: Int
 
-       init with_buffer(b: Bytes) do init(b.items, 0, b.length - 1)
+       init with_buffer(b: Bytes) do init(b.items, 0, b.length)
 
        redef fun is_ok do return index < max
 
@@ -253,6 +255,15 @@ redef class Text
                return b
        end
 
+       # Is `self` a valid hexdigest ?
+       #
+       #     assert "0B1d3F".is_valid_hexdigest
+       #     assert not "5G".is_valid_hexdigest
+       fun is_valid_hexdigest: Bool do
+               for i in bytes do if not i.is_valid_hexdigit then return false
+               return true
+       end
+
        # Appends `self.bytes` to `b`
        fun append_to_bytes(b: Bytes) do
                for s in substrings do
@@ -260,6 +271,24 @@ redef class Text
                        b.append_ns_from(s.items, s.bytelen, from)
                end
        end
+
+       # Returns a new `Bytes` instance with the digest as content
+       #
+       #     assert "0B1F4D".hexdigest_to_bytes == [0x0Bu8, 0x1Fu8, 0x4Du8]
+       #
+       # REQUIRE: `self` is a valid hexdigest and hexdigest.length % 2 == 0
+       fun hexdigest_to_bytes: Bytes do
+               var b = bytes
+               var pos = 0
+               var max = bytelen
+               var ret = new Bytes.with_capacity(max / 2)
+               while pos < max do
+                       ret.add((b[pos].hexdigit_to_byteval << 4) |
+                       b[pos + 1].hexdigit_to_byteval)
+                       pos += 2
+               end
+               return ret
+       end
 end
 
 redef class FlatText
index 2db319a..4b1e826 100644 (file)
@@ -173,12 +173,13 @@ abstract class Reader
        # ~~~
        fun read_all: String do
                var s = read_all_bytes
-               if not s.is_utf8 then s = s.clean_utf8
                var slen = s.length
                if slen == 0 then return ""
                var rets = ""
                var pos = 0
-               var sits = s.items
+               var str = s.items.clean_utf8(slen)
+               slen = str.bytelen
+               var sits = str.items
                var remsp = slen
                while pos < slen do
                        # The 129 size was decided more or less arbitrarily
index c8b6ecd..52de988 100644 (file)
@@ -985,8 +985,7 @@ redef class NativeString
        redef fun to_s_with_length(length): FlatString
        do
                assert length >= 0
-               var str = new FlatString.with_infos(self, length, 0, length - 1)
-               return str
+               return clean_utf8(length)
        end
 
        redef fun to_s_full(bytelen, unilen) do
@@ -997,6 +996,8 @@ redef class NativeString
        redef fun to_s_with_copy: FlatString
        do
                var length = cstring_length
+               var r = clean_utf8(length)
+               if r.items != self then return r
                var new_self = new NativeString(length + 1)
                copy_to(new_self, length, 0, 0)
                var str = new FlatString.with_infos(new_self, length, 0, length - 1)
@@ -1005,6 +1006,81 @@ redef class NativeString
                return str
        end
 
+       # Cleans a NativeString if necessary
+       fun clean_utf8(len: Int): FlatString do
+               var replacements: nullable Array[Int] = null
+               var end_length = len
+               var pos = 0
+               var chr_ln = 0
+               while pos < len do
+                       var b = self[pos]
+                       var nxst = length_of_char_at(pos)
+                       var ok_st: Bool
+                       if nxst == 1 then
+                               ok_st = b & 0x80u8 == 0u8
+                       else if nxst == 2 then
+                               ok_st = b & 0xE0u8 == 0xC0u8
+                       else if nxst == 3 then
+                               ok_st = b & 0xF0u8 == 0xE0u8
+                       else
+                               ok_st = b & 0xF8u8 == 0xF0u8
+                       end
+                       if not ok_st then
+                               if replacements == null then replacements = new Array[Int]
+                               replacements.add pos
+                               end_length += 2
+                               pos += 1
+                               chr_ln += 1
+                               continue
+                       end
+                       var ok_c: Bool
+                       var c = char_at(pos)
+                       var cp = c.ascii
+                       if nxst == 1 then
+                               ok_c = cp >= 0 and cp <= 0x7F
+                       else if nxst == 2 then
+                               ok_c = cp >= 0x80 and cp <= 0x7FF
+                       else if nxst == 3 then
+                               ok_c = cp >= 0x800 and cp <= 0xFFFF
+                               ok_c = ok_c and not (cp >= 0xD800 and cp <= 0xDFFF) and cp != 0xFFFE and cp != 0xFFFF
+                       else
+                               ok_c = cp >= 0x10000 and cp <= 0x10FFFF
+                       end
+                       if not ok_c then
+                               if replacements == null then replacements = new Array[Int]
+                               replacements.add pos
+                               end_length += 2
+                               pos += 1
+                               chr_ln += 1
+                               continue
+                       end
+                       pos += c.u8char_len
+                       chr_ln += 1
+               end
+               var ret = self
+               if end_length != len then
+                       ret = new NativeString(end_length)
+                       var old_repl = 0
+                       var off = 0
+                       var repls = replacements.as(not null)
+                       var r = repls.items.as(not null)
+                       var imax = repls.length
+                       for i in [0 .. imax[ do
+                               var repl_pos = r[i]
+                               var chkln = repl_pos - old_repl
+                               copy_to(ret, chkln, old_repl, off)
+                               off += chkln
+                               ret[off] = 0xEFu8
+                               ret[off + 1] = 0xBFu8
+                               ret[off + 2] = 0xBDu8
+                               old_repl = repl_pos + 1
+                               off += 3
+                       end
+                       copy_to(ret, len - old_repl, old_repl, off)
+               end
+               return new FlatString.full(ret, end_length, 0, end_length - 1, chr_ln)
+       end
+
        # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
        #
        # Very unsafe, make sure to have room for this char prior to calling this function.
@@ -1109,7 +1185,7 @@ redef class Array[E]
                        end
                        i += 1
                end
-               return ns.to_s_with_length(sl)
+               return new FlatString.with_infos(ns, sl, 0, sl - 1)
        end
 end
 
@@ -1146,7 +1222,7 @@ redef class NativeArray[E]
                        end
                        i += 1
                end
-               return ns.to_s_with_length(sl)
+               return new FlatString.with_infos(ns, sl, 0, sl - 1)
        end
 end
 
index 8934b7e..69df8e5 100644 (file)
@@ -454,3 +454,6 @@ end
 fun egl_bind_opengl_api: Bool `{ return eglBindAPI(EGL_OPENGL_API); `}
 fun egl_bind_opengl_es_api: Bool `{ return eglBindAPI(EGL_OPENGL_ES_API); `}
 fun egl_bind_openvg_api: Bool `{ return eglBindAPI(EGL_OPENVG_API); `}
+
+# Handle to the default display to use with EGL
+fun egl_default_display: Pointer `{ return EGL_DEFAULT_DISPLAY; `}
diff --git a/lib/gamnit/display.nit b/lib/gamnit/display.nit
new file mode 100644 (file)
index 0000000..94ece8a
--- /dev/null
@@ -0,0 +1,49 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Abstract display services
+module display
+
+import ::glesv2
+
+import display_linux is conditional(linux)
+import display_android is conditional(android)
+
+# Should Gamnit be more verbose?
+fun debug_gamnit: Bool do return false
+
+# General display class, is sized and drawable
+class GamnitDisplay
+
+       # Width of the display, in pixels
+       fun width: Int is abstract
+
+       # Height of the display, in pixels
+       fun height: Int is abstract
+
+       # Prepare this display
+       #
+       # The implementation varies per platform.
+       fun setup is abstract
+
+       # Close this display and free underlying resources
+       #
+       # The implementation varies per platform.
+       fun close do end
+
+       # Flip the display buffers
+       #
+       # The implementation varies per platform.
+       fun flip do end
+end
diff --git a/lib/gamnit/display_android.nit b/lib/gamnit/display_android.nit
new file mode 100644 (file)
index 0000000..d0e5235
--- /dev/null
@@ -0,0 +1,47 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Gamnit display implementation for Android
+#
+# Generated APK files require OpenGL ES 2.0.
+#
+# This modules uses `android::native_app_glue` and the Android NDK.
+module display_android is
+       android_manifest """<uses-feature android:glEsVersion="0x00020000"/>"""
+end
+
+import ::android
+
+private import gamnit::egl
+
+redef class GamnitDisplay
+
+       redef fun setup
+       do
+               var native_display = egl_default_display
+               var native_window = app.native_app_glue.window
+
+               setup_egl_display native_display
+
+               # We need 8 bits per color for selection by color
+               select_egl_config(8, 8, 8, 0, 8, 0, 0)
+
+               var format = egl_config.attribs(egl_display).native_visual_id
+               native_window.set_buffers_geometry(0, 0, format)
+
+               setup_egl_context native_window
+       end
+
+       redef fun close do close_egl
+end
diff --git a/lib/gamnit/display_linux.nit b/lib/gamnit/display_linux.nit
new file mode 100644 (file)
index 0000000..109ff51
--- /dev/null
@@ -0,0 +1,91 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Gamnit display implementation for GNU/Linux using `egl`, `sdl` and `x11`
+module display_linux
+
+import sdl
+import x11
+
+import egl # local to gamnit
+import display
+
+redef class GamnitDisplay
+
+       # Actual width or desired width of the window, can be set before calling `setup`
+       fun width=(value: Int) do requested_width = value
+       private var requested_width = 1920
+
+       # Actual height or desired height of the window, can be set before calling `setup`
+       fun height=(value: Int) do requested_height = value
+       private var requested_height = 1080
+
+       # Setup SDL, X11, EGL in order
+       redef fun setup
+       do
+               if debug_gamnit then print "Setting up SDL"
+               self.sdl_display = setup_sdl(requested_width, requested_height)
+
+               if debug_gamnit then print "Setting up X11"
+               var x11_display = setup_x11
+               var window_handle = window_handle
+               setup_egl_display x11_display
+
+               if debug_gamnit then print "Setting up EGL context"
+               select_egl_config(8, 8, 8, 8, 8, 0, 0)
+               setup_egl_context window_handle
+       end
+
+       # Close EGL and SDL in reverse order of `setup` (nothing to do for X11)
+       redef fun close
+       do
+               close_egl
+               close_sdl
+       end
+
+       # ---
+       # SDL
+
+       # The SDL display managing the window and events
+       var sdl_display: SDLDisplay is noautoinit
+
+       # Setup the SDL display and lib
+       fun setup_sdl(window_width, window_height: Int): SDLDisplay
+       do
+               var sdl_display = new SDLDisplay(window_width, window_height)
+               assert not sdl_display.address_is_null else print "Opening SDL display failed"
+               return sdl_display
+       end
+
+       # Close the SDL display
+       fun close_sdl do sdl_display.destroy
+
+       # Get a native handle to the current SDL window
+       fun window_handle: Pointer
+       do
+               var sdl_wm_info = new SDLSystemWindowManagerInfo
+               return sdl_wm_info.x11_window_handle
+       end
+
+       # ---
+       # X11
+
+       # Get a native handle to the current X11 display
+       fun setup_x11: Pointer
+       do
+               var x11_display = x_open_default_display
+               assert not x11_display.address_is_null else print "Opening X11 display failed"
+               return x11_display
+       end
+end
diff --git a/lib/gamnit/egl.nit b/lib/gamnit/egl.nit
new file mode 100644 (file)
index 0000000..3fcb8c8
--- /dev/null
@@ -0,0 +1,115 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Use of EGL to implement Gamnit on GNU/Linux and Android
+module egl
+
+import ::egl
+
+import gamnit::display
+
+redef class GamnitDisplay
+
+       # The EGL display
+       var egl_display: EGLDisplay is noautoinit
+
+       # The EGL context
+       var egl_context: EGLContext is noautoinit
+
+       # The EGL surface for the window
+       var window_surface: EGLSurface is noautoinit
+
+       # The selected EGL configuration
+       var egl_config: EGLConfig is noautoinit
+
+       # Setup the EGL display for the given `x11_display`
+       protected fun setup_egl_display(x11_display: Pointer)
+       do
+               var egl_display = new EGLDisplay(x11_display)
+               assert egl_display.is_valid else print "new EGL display is not valid"
+
+               egl_display.initialize
+               assert egl_display.is_valid else print "EGL initialize error: {egl_display.error}"
+
+               self.egl_display = egl_display
+       end
+
+       # Select an EGL config
+       protected fun select_egl_config(blue, green, red, alpha, depth, stencil, sample: Int)
+       do
+               var config_chooser = new EGLConfigChooser
+               config_chooser.renderable_type_egl
+               config_chooser.surface_type_egl
+               config_chooser.blue_size = blue
+               config_chooser.green_size = green
+               config_chooser.red_size = red
+               if alpha > 0 then config_chooser.alpha_size = alpha
+               if depth > 0 then config_chooser.depth_size = depth
+               if stencil > 0 then config_chooser.stencil_size = stencil
+               if sample > 0 then config_chooser.sample_buffers = sample
+               config_chooser.close
+
+               var configs = config_chooser.choose(egl_display)
+               assert configs != null else print "Choosing EGL config failed: {egl_display.error}"
+               assert not configs.is_empty else print "Found no EGL config"
+
+               if debug_gamnit then
+                       print "EGL available configurations:"
+                       for config in configs do
+                               var attribs = config.attribs(egl_display)
+                               print "* Conformant to: {attribs.conformant}"
+                               print "  Caveats: {attribs.caveat}"
+                               print "  Size of RGBA: {attribs.red_size} {attribs.green_size} {attribs.blue_size} {attribs.alpha_size}"
+                               print "  Buffer, depth, stencil: {attribs.buffer_size} {attribs.depth_size} {attribs.stencil_size}"
+                       end
+               end
+
+               # We use the first one, it is recommended
+               self.egl_config = configs.first
+       end
+
+       # Setup the EGL context for the given `window_handle`
+       protected fun setup_egl_context(window_handle: Pointer)
+       do
+               var window_surface = egl_display.create_window_surface(egl_config, window_handle, [0])
+               assert window_surface.is_ok else print "Creating EGL window surface failed: {egl_display.error}"
+               self.window_surface = window_surface
+
+               egl_context = egl_display.create_context(egl_config)
+               assert egl_context.is_ok else print "Creating EGL context failed: {egl_display.error}"
+
+               var make_current_res = egl_display.make_current(window_surface, window_surface, egl_context)
+               assert make_current_res else print "Creating EGL make current failed: {egl_display.error}"
+
+               # TODO make the API selection configurable per platform
+               assert egl_bind_opengl_es_api else print "EGL bind API failed: {egl_display.error}"
+       end
+
+       redef fun width do return window_surface.attribs(egl_display).width
+
+       redef fun height do return window_surface.attribs(egl_display).height
+
+       # Close the EGL context
+       fun close_egl
+       do
+               egl_display.make_current(new EGLSurface.none, new EGLSurface.none, new EGLContext.none)
+               egl_display.destroy_context(egl_context)
+               egl_display.destroy_surface(window_surface)
+       end
+
+       redef fun flip
+       do
+               egl_display.swap_buffers(window_surface)
+       end
+end
diff --git a/lib/gamnit/examples/triangle/Makefile b/lib/gamnit/examples/triangle/Makefile
new file mode 100644 (file)
index 0000000..22172de
--- /dev/null
@@ -0,0 +1,21 @@
+NITC=../../../../bin/nitc
+NITLS=../../../../bin/nitls
+
+all: bin/standalone_triangle bin/triangle bin/triangle.apk
+
+bin/standalone_triangle: $(shell ${NITLS} -M src/standalone_triangle.nit linux) ${NITC}
+       ${NITC} src/standalone_triangle.nit -m linux -o $@
+
+bin/triangle: $(shell ${NITLS} -M src/portable_triangle.nit linux) ${NITC}
+       ${NITC} src/portable_triangle.nit -m linux -o $@
+
+check: bin/standalone_triangle bin/triangle
+       bin/standalone_triangle
+       bin/triangle
+
+android: bin/triangle.apk
+bin/triangle.apk: $(shell ${NITLS} -M src/portable_triangle.nit android) ${NITC} res/drawable-hdpi/icon.png
+       ${NITC} src/portable_triangle.nit -m android -o $@
+
+res/drawable-hdpi/icon.png: art/icon.svg
+       ../../../../contrib/inkscape_tools/bin/svg_to_icons --out res --android art/icon.svg
diff --git a/lib/gamnit/examples/triangle/art/icon.svg b/lib/gamnit/examples/triangle/art/icon.svg
new file mode 100644 (file)
index 0000000..26f7c04
--- /dev/null
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+
+<svg
+   xmlns:dc="http://purl.org/dc/elements/1.1/"
+   xmlns:cc="http://creativecommons.org/ns#"
+   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+   xmlns:svg="http://www.w3.org/2000/svg"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+   width="512"
+   height="512"
+   id="svg2"
+   version="1.1"
+   inkscape:version="0.48.5 r10040"
+   sodipodi:docname="New document 1">
+  <defs
+     id="defs4" />
+  <sodipodi:namedview
+     id="base"
+     pagecolor="#ffffff"
+     bordercolor="#666666"
+     borderopacity="1.0"
+     inkscape:pageopacity="0.0"
+     inkscape:pageshadow="2"
+     inkscape:zoom="0.98994949"
+     inkscape:cx="292.54393"
+     inkscape:cy="311.81525"
+     inkscape:document-units="px"
+     inkscape:current-layer="layer1"
+     showgrid="false"
+     inkscape:window-width="1279"
+     inkscape:window-height="1379"
+     inkscape:window-x="3840"
+     inkscape:window-y="27"
+     inkscape:window-maximized="0" />
+  <metadata
+     id="metadata7">
+    <rdf:RDF>
+      <cc:Work
+         rdf:about="">
+        <dc:format>image/svg+xml</dc:format>
+        <dc:type
+           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
+        <dc:title></dc:title>
+      </cc:Work>
+    </rdf:RDF>
+  </metadata>
+  <g
+     inkscape:label="Layer 1"
+     inkscape:groupmode="layer"
+     id="layer1"
+     transform="translate(0,-540.36218)">
+    <rect
+       style="fill:#ab00cf;fill-opacity:1;stroke:none"
+       id="rect2985"
+       width="512"
+       height="512"
+       x="0"
+       y="540.36218"
+       ry="69.507881"
+       rx="69.507881" />
+    <path
+       sodipodi:type="star"
+       style="fill:#ff001b;fill-opacity:1;stroke:none"
+       id="path3755"
+       sodipodi:sides="3"
+       sodipodi:cx="22.857143"
+       sodipodi:cy="-299.06638"
+       sodipodi:r1="235.13782"
+       sodipodi:r2="118.85303"
+       sodipodi:arg1="-1.5707963"
+       sodipodi:arg2="-0.52359875"
+       inkscape:flatsided="false"
+       inkscape:rounded="0"
+       inkscape:randomized="0"
+       d="m 22.85715,-534.20419 102.92974,175.7113 100.70557,176.99543 -203.63532,1.28411 -203.63532,-1.28412 100.705584,-176.99542 z"
+       inkscape:transform-center-y="-22.453664"
+       transform="translate(232.85714,1144.2857)" />
+  </g>
+</svg>
diff --git a/lib/gamnit/examples/triangle/bin/.gitignore b/lib/gamnit/examples/triangle/bin/.gitignore
new file mode 100644 (file)
index 0000000..72e8ffc
--- /dev/null
@@ -0,0 +1 @@
+*
diff --git a/lib/gamnit/examples/triangle/package.ini b/lib/gamnit/examples/triangle/package.ini
new file mode 100644 (file)
index 0000000..f673878
--- /dev/null
@@ -0,0 +1,11 @@
+[package]
+name=triangle
+tags=example
+maintainer=Alexis Laferrière <alexis.laf@xymus.net>
+license=Apache-2.0
+[upstream]
+browse=https://github.com/nitlang/nit/tree/master/lib/gamnit/examples/triangle/
+git=https://github.com/nitlang/nit.git
+git.directory=lib/gamnit/examples/triangle/
+homepage=http://nitlanguage.org
+issues=https://github.com/nitlang/nit/issues
diff --git a/lib/gamnit/examples/triangle/res/.gitignore b/lib/gamnit/examples/triangle/res/.gitignore
new file mode 100644 (file)
index 0000000..72e8ffc
--- /dev/null
@@ -0,0 +1 @@
+*
diff --git a/lib/gamnit/examples/triangle/src/portable_triangle.nit b/lib/gamnit/examples/triangle/src/portable_triangle.nit
new file mode 100644 (file)
index 0000000..ac8e0e6
--- /dev/null
@@ -0,0 +1,138 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Portable example of using Gamnit with custom calls to OpenGL ES 2.0
+#
+# References:
+# * The book OpenGL ES 2.0 Programming Guide
+# * https://code.google.com/p/opengles-book-samples/source/browse/trunk/LinuxX11/Chapter_2/Hello_Triangle/Hello_Triangle.c
+module portable_triangle is
+       app_name "gamnit Triangle"
+       app_namespace "org.nitlanguage.triangle"
+       app_version(1, 0, git_revision)
+end
+
+import gamnit
+
+redef class App
+
+       # Our only program for the graphic card
+       var program: GLProgram is noautoinit
+
+       # The only vertex sharder
+       var vertex_shader: GLVertexShader is noautoinit
+
+       # The only fragment sharder
+       var fragment_shader: GLFragmentShader is noautoinit
+
+       # Vertex data for the triangle
+       var vertex_array: VertexArray is noautoinit
+
+       redef fun on_create
+       do
+               super
+
+               var display = display
+               assert display != null
+
+               print "Width: {display.width}"
+               print "Height: {display.height}"
+
+               assert_no_gl_error
+               assert gl.shader_compiler else print "Cannot compile shaders"
+
+               # GL program
+               program = new GLProgram
+               if not program.is_ok then
+                       print "Program is not ok: {gl.error.to_s}\nLog:"
+                       print program.info_log
+                       abort
+               end
+               assert_no_gl_error
+
+               # Vertex shader
+               vertex_shader = new GLVertexShader
+               assert vertex_shader.is_ok else print "Vertex shader is not ok: {gl.error}"
+               vertex_shader.source = """
+               attribute vec4 vPosition;
+               void main()
+               {
+                 gl_Position = vPosition;
+               }
+               """@glsl_vertex_shader.to_cstring
+               vertex_shader.compile
+               assert vertex_shader.is_compiled else print "Vertex shader compilation failed with: {vertex_shader.info_log} {program.info_log}"
+               assert_no_gl_error
+
+               # Fragment shader
+               fragment_shader = new GLFragmentShader
+               assert fragment_shader.is_ok else print "Fragment shader is not ok: {gl.error}"
+               fragment_shader.source = """
+               precision mediump float;
+               void main()
+               {
+                       gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
+               }
+               """@glsl_fragment_shader.to_cstring
+               fragment_shader.compile
+               assert fragment_shader.is_compiled else print "Fragment shader compilation failed with: {fragment_shader.info_log}"
+               assert_no_gl_error
+
+               # Attach to program
+               program.attach_shader vertex_shader
+               program.attach_shader fragment_shader
+               program.bind_attrib_location(0, "vPosition")
+               program.link
+               assert program.is_linked else print "Linking failed: {program.info_log}"
+               assert_no_gl_error
+
+               # Draw!
+               var vertices = [0.0, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0]
+               vertex_array = new VertexArray(0, 3, vertices)
+               vertex_array.attrib_pointer
+       end
+
+       redef fun frame_core
+       do
+               var display = display
+               if display != null then
+                       gl.clear_color(0.5, 0.0, 0.5, 1.0)
+
+                       assert_no_gl_error
+                       gl.viewport(0, 0, display.width, display.height)
+                       gl.clear((new GLBuffer).color)
+                       program.use
+                       vertex_array.enable
+
+                       glDrawArrays(new GLDrawMode.triangles, 0, 3)
+
+                       display.flip
+               end
+       end
+
+       redef fun on_stop
+       do
+               # Clean up
+               program.delete
+               vertex_shader.delete
+               fragment_shader.delete
+
+               # Close gamnit
+               var display = display
+               if display != null then display.close
+       end
+end
+
+if "NIT_TESTING".environ == "true" then exit(0)
+super
diff --git a/lib/gamnit/examples/triangle/src/standalone_triangle.nit b/lib/gamnit/examples/triangle/src/standalone_triangle.nit
new file mode 100644 (file)
index 0000000..04e14db
--- /dev/null
@@ -0,0 +1,112 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Example of using `GamnitDisplay` to setup a screen for custom calls to OpenGL ES 2.0
+#
+# This example does not support the lifecycle of mobile platforms, as such it only works on desktop computers.
+#
+# References:
+# * The book OpenGL ES 2.0 Programming Guide
+# * https://code.google.com/p/opengles-book-samples/source/browse/trunk/LinuxX11/Chapter_2/Hello_Triangle/Hello_Triangle.c
+module standalone_triangle
+
+import app
+import gamnit::display
+
+if "NIT_TESTING".environ == "true" then exit(0)
+
+# Setup gamnit
+var display = new GamnitDisplay
+display.setup
+
+var width = display.width
+var height = display.height
+print "Width: {width}"
+print "Height: {height}"
+
+# Custom calls to OpenGL ES 2.0
+assert_no_gl_error
+assert gl.shader_compiler else print "Cannot compile shaders"
+
+# GL program
+print gl.error.to_s
+var program = new GLProgram
+if not program.is_ok then
+       print "Program is not ok: {gl.error.to_s}\nLog:"
+       print program.info_log
+       abort
+end
+assert_no_gl_error
+
+# Vertex shader
+var vertex_shader = new GLVertexShader
+assert vertex_shader.is_ok else print "Vertex shader is not ok: {gl.error}"
+vertex_shader.source = """
+attribute vec4 vPosition;
+void main()
+{
+  gl_Position = vPosition;
+}
+"""@glsl_vertex_shader.to_cstring
+vertex_shader.compile
+assert vertex_shader.is_compiled else print "Vertex shader compilation failed with: {vertex_shader.info_log} {program.info_log}"
+assert_no_gl_error
+
+# Fragment shader
+var fragment_shader = new GLFragmentShader
+assert fragment_shader.is_ok else print "Fragment shader is not ok: {gl.error}"
+fragment_shader.source = """
+precision mediump float;
+void main()
+{
+       gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
+}
+"""@glsl_fragment_shader.to_cstring
+fragment_shader.compile
+assert fragment_shader.is_compiled else print "Fragment shader compilation failed with: {fragment_shader.info_log}"
+assert_no_gl_error
+
+# Attach to program
+program.attach_shader vertex_shader
+program.attach_shader fragment_shader
+program.bind_attrib_location(0, "vPosition")
+program.link
+assert program.is_linked else print "Linking failed: {program.info_log}"
+assert_no_gl_error
+
+# Draw!
+var vertices = [0.0, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0]
+var vertex_array = new VertexArray(0, 3, vertices)
+vertex_array.attrib_pointer
+gl.clear_color(0.5, 0.0, 0.5, 1.0)
+for i in [0..1000[ do
+       printn "."
+       assert_no_gl_error
+       gl.viewport(0, 0, width, height)
+       gl.clear((new GLBuffer).color)
+       program.use
+       vertex_array.enable
+
+       glDrawArrays(new GLDrawMode.triangles, 0, 3)
+
+       display.flip
+end
+
+# Clean up
+program.delete
+vertex_shader.delete
+fragment_shader.delete
+
+# Close gamnit
+display.close
index 2fa5b8e..43ec95c 100644 (file)
 
 # Game and multimedia framework for Nit
 module gamnit
+
+import app
+
+import display
+
+import gamnit_android is conditional(android)
+
+redef class App
+
+       # Main `GamnitDisplay` initialized by `on_create`
+       var display: nullable GamnitDisplay = null
+
+       redef fun on_create
+       do
+               super
+
+               var display = new GamnitDisplay
+               display.setup
+               self.display = display
+       end
+
+       # Core of the frame logic, executed only when the display is visible
+       #
+       # This method should be redefined by user modules to customize the behavior of the game.
+       protected fun frame_core do end
+
+       # Full frame logic, executed even if the display is not visible
+       #
+       # This method wraps `frame_core` and other services to be executed in the main app loop.
+       #
+       # To customize the behavior on each turn, it is preferable to redefined `frame_core`.
+       # Still, `frame_full` can be redefined with care for more control.
+       protected fun frame_full
+       do
+               var display = display
+               if display != null then frame_core
+
+               feed_events
+       end
+
+       redef fun run
+       do
+               # TODO manage exit condition
+               loop frame_full
+       end
+
+       # Loop on available events and feed them back to the app
+       #
+       # The implementation varies per platform.
+       private fun feed_events do end
+end
diff --git a/lib/gamnit/gamnit_android.nit b/lib/gamnit/gamnit_android.nit
new file mode 100644 (file)
index 0000000..647c741
--- /dev/null
@@ -0,0 +1,24 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Support services for Gamnit on Android
+module gamnit_android
+
+import android
+
+intrude import gamnit
+
+redef class App
+       redef fun feed_events do app.poll_looper 0
+end
index 48d49ab..3a1e1c5 100644 (file)
@@ -168,7 +168,7 @@ for i in [0..10000[ do
        gl.clear((new GLBuffer).color)
        program.use
        vertex_array.enable
-       vertex_array.draw_arrays_triangles
+       glDrawArrays(new GLDrawMode.triangles, 0, 3)
        egl_display.swap_buffers(surface)
 end
 
index 4e71e1f..9c3e1d7 100644 (file)
@@ -37,6 +37,7 @@ module glesv2 is
 end
 
 import android::aware
+intrude import c
 
 in "C Header" `{
        #include <GLES2/gl2.h>
@@ -296,38 +297,106 @@ class VertexArray
        # Number of data per vertex
        var count: Int
 
-       protected var glfloat_array: GLfloatArray
+       protected var glfloat_array: NativeGLfloatArray
 
        init(index, count: Int, array: Array[Float])
        do
                self.index = index
                self.count = count
-               self.glfloat_array = new GLfloatArray(array)
+               self.glfloat_array = new NativeGLfloatArray(array.length)
+               for k in [0..array.length[ do
+                       glfloat_array[k] = array[k]
+               end
        end
 
        fun attrib_pointer do attrib_pointer_intern(index, count, glfloat_array)
-       private fun attrib_pointer_intern(index, count: Int, array: GLfloatArray) `{
+       private fun attrib_pointer_intern(index, count: Int, array: NativeGLfloatArray) `{
                glVertexAttribPointer(index, count, GL_FLOAT, GL_FALSE, 0, array);
        `}
 
-       fun enable do enable_intern(index)
-       private fun enable_intern(index: Int) `{ glEnableVertexAttribArray(index); `}
+       # Enable this vertex attribute array
+       fun enable do glEnableVertexAttribArray(index)
 
-       fun draw_arrays_triangles do draw_arrays_triangles_intern(index, count)
-       private fun draw_arrays_triangles_intern(index, count: Int) `{
-               glDrawArrays(GL_TRIANGLES, index, count);
-       `}
+       # Disable this vertex attribute array
+       fun disable do glDisableVertexAttribArray(index)
 end
 
+# Enable the generic vertex attribute array at `index`
+fun glEnableVertexAttribArray(index: Int) `{ glEnableVertexAttribArray(index); `}
+
+# Disable the generic vertex attribute array at `index`
+fun glDisableVertexAttribArray(index: Int) `{ glDisableVertexAttribArray(index); `}
+
+# Render primitives from array data
+fun glDrawArrays(mode: GLDrawMode, from, count: Int) `{ glDrawArrays(mode, from, count); `}
+
+# Define an array of generic vertex attribute data
+fun glVertexAttribPointer(index, size: Int, typ: GLDataType, normalized: Bool, stride: Int, array: NativeGLfloatArray) `{
+       glVertexAttribPointer(index, size, typ, normalized, stride, array);
+`}
+
+# Specify the value of a generic vertex attribute
+fun glVertexAttrib1f(index: Int, x: Float) `{ glVertexAttrib1f(index, x); `}
+
+# Specify the value of a generic vertex attribute
+fun glVertexAttrib2f(index: Int, x, y: Float) `{ glVertexAttrib2f(index, x, y); `}
+
+# Specify the value of a generic vertex attribute
+fun glVertexAttrib3f(index: Int, x, y, z: Float) `{ glVertexAttrib3f(index, x, y, z); `}
+
+# Specify the value of a generic vertex attribute
+fun glVertexAttrib4f(index: Int, x, y, z, w: Float) `{ glVertexAttrib4f(index, x, y, z, w); `}
+
+# Specify the value of a uniform variable for the current program object
+fun glUniform1i(index, x: Int) `{ glUniform1i(index, x); `}
+
+# Specify the value of a uniform variable for the current program object
+fun glUniform2i(index, x, y: Int) `{ glUniform2i(index, x, y); `}
+
+# Specify the value of a uniform variable for the current program object
+fun glUniform3i(index, x, y, z: Int) `{ glUniform3i(index, x, y, z); `}
+
+# Specify the value of a uniform variable for the current program object
+fun glUniform4i(index, x, y, z, w: Int) `{ glUniform4i(index, x, y, z, w); `}
+
+# TODO glUniform*f
+
 # Low level array of `Float`
-extern class GLfloatArray `{GLfloat *`}
-       new (array: Array[Float]) import Array[Float].length, Array[Float].[] `{
-               int i;
-               int len = Array_of_Float_length(array);
-               GLfloat *vertex_array = malloc(sizeof(GLfloat)*len);
-               for (i = 0; i < len; i ++) vertex_array[i] = Array_of_Float__index(array, i);
-               return vertex_array;
-       `}
+class GLfloatArray
+       super CArray[Float]
+       redef type NATIVE: NativeGLfloatArray
+
+       init do native_array = new NativeGLfloatArray(length)
+
+       # Create with the content of `array`
+       new from(array: Array[Float])
+       do
+               var arr = new GLfloatArray(array.length)
+               arr.fill_from array
+               return arr
+       end
+
+       # Fill with the content of `array`
+       fun fill_from(array: Array[Float])
+       do
+               assert length >= array.length
+               for k in [0..array.length[ do
+                       self[k] = array[k]
+               end
+       end
+end
+
+# An array of `GLfloat` in C (`GLfloat*`)
+extern class NativeGLfloatArray `{ GLfloat* `}
+       super NativeCArray
+       redef type E: Float
+
+       new(size: Int) `{ return calloc(size, sizeof(GLfloat)); `}
+
+       redef fun [](index) `{ return self[index]; `}
+       redef fun []=(index, val) `{ self[index] = val; `}
+
+       redef fun +(offset) `{ return self + offset; `}
 end
 
 # General type for OpenGL enumerations
@@ -375,30 +444,47 @@ do
        end
 end
 
-# Texture minifying function
-#
-# Used by: `GLES::tex_parameter_min_filter`
-extern class GLTextureMinFilter
-       super GLEnum
+# Does `name` corresponds to a texture?
+fun glIsTexture(name: Int): Bool `{ return glIsTexture(name); `}
 
-       new nearest `{ return GL_NEAREST; `}
-       new linear `{ return GL_LINEAR; `}
-end
+# Bind the named `texture` to a `target`
+fun glBindTexture(target: GLTextureTarget, texture: Int) `{ glBindTexture(target, texture); `}
 
-# Texture magnification function
-#
-# Used by: `GLES::tex_parameter_mag_filter`
-extern class GLTextureMagFilter
+# Set pixel storage modes
+fun glPixelStorei(parameter: GLPack, val: Int) `{ glPixelStorei(parameter, val); `}
+
+# Symbolic name of the parameter to be set with `glPixelStorei`
+extern class GLPack
        super GLEnum
+end
+
+# Parameter to specify the alignment requirements for the start of each pixel row in memory
+fun gl_PACK_ALIGNEMENT: GLPack `{ return GL_PACK_ALIGNMENT; `}
+
+# Parameter to specify the alignment requirements for the start of each pixel row in memory
+fun gl_UNPACK_ALIGNEMENT: GLPack `{ return GL_UNPACK_ALIGNMENT; `}
+
+# TODO GL_PACK_ROW_LENGTH, GL_PACK_IMAGE_HEIGHT, GL_PACK_SKIP_PIXELS, GL_PACK_SKIP_ROWS, GL_PACK_SKIP_IMAGES
+# GL_UNPACK_ROW_LENGTH, GL_UNPACK_IMAGE_HEIGHT, GL_UNPACK_SKIP_PIXELS, GL_UNPACK_SKIP_ROWS, GL_UNPACK_SKIP_IMAGES
 
-       new nearest `{ return GL_NEAREST; `}
-       new linear `{ return GL_LINEAR; `}
-       new nearest_mipmap_nearest `{ return GL_NEAREST_MIPMAP_NEAREST; `}
-       new linear_mipmap_nearest `{ return GL_LINEAR_MIPMAP_NEAREST; `}
-       new nearest_mipmap_linear `{ return GL_NEAREST_MIPMAP_LINEAR; `}
-       new linear_mipmap_linear `{ return GL_LINEAR_MIPMAP_LINEAR; `}
+# Specify a two-dimensional texture image
+fun glTexImage2D(target: GLTextureTarget, level, internalformat, width, height, border: Int,
+                 format: GLPixelFormat, typ: GLPixelType, data: NativeCByteArray) `{
+       glTexImage2D(target, level, internalformat, width, height, border, format, typ, data);
+`}
+
+# Texture minifying and magnifying function
+extern class GLTextureFilter
+       super GLEnum
 end
 
+fun gl_NEAREST: GLTextureFilter `{ return GL_NEAREST; `}
+fun gl_LINEAR: GLTextureFilter `{ return GL_LINEAR; `}
+fun gl_NEAREST_MIPMAP_NEAREST: GLTextureFilter `{ return GL_NEAREST_MIPMAP_NEAREST; `}
+fun gl_LINEAR_MIPMAP_NEAREST: GLTextureFilter `{ return GL_LINEAR_MIPMAP_NEAREST; `}
+fun gl_NEAREST_MIPMAP_NINEAR: GLTextureFilter `{ return GL_NEAREST_MIPMAP_LINEAR; `}
+fun gl_LINEAR_MIPMAP_LINEAR: GLTextureFilter `{ return GL_LINEAR_MIPMAP_LINEAR; `}
+
 # Wrap parameter of a texture
 #
 # Used by: `tex_parameter_wrap_*`
@@ -411,15 +497,19 @@ extern class GLTextureWrap
 end
 
 # Target texture
-#
-# Used by: `tex_parameter_*`
 extern class GLTextureTarget
        super GLEnum
-
-       new flat `{ return GL_TEXTURE_2D; `}
-       new cube_map `{ return GL_TEXTURE_CUBE_MAP; `}
 end
 
+# Two-dimensional texture
+fun gl_TEXTURE_2D: GLTextureTarget `{ return GL_TEXTURE_2D; `}
+
+# Cube map texture
+fun gl_TEXTURE_CUBE_MAP: GLTextureTarget `{ return GL_TEXTURE_CUBE_MAP; `}
+
+# TODO GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
+# GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
+
 # A server-side capability
 class GLCap
 
@@ -439,6 +529,52 @@ class GLCap
        redef fun hash do return val
        redef fun ==(o) do return o != null and is_same_type(o) and o.hash == self.hash
 end
+
+# Attach a renderbuffer object to a framebuffer object
+fun glFramebufferRenderbuffer(target: GLFramebufferTarget, attachment: GLAttachment,
+                              renderbuffertarget: GLRenderbufferTarget, renderbuffer: Int) `{
+       glFramebufferRenderbuffer(target, attachment, renderbuffertarget, renderbuffer);
+`}
+
+# Establish data storage, `format` and dimensions of the `target` renderbuffer object's image
+fun glRenderbufferStorage(target: GLRenderbufferTarget, format: GLRenderbufferFormat, width, height: Int) `{
+       glRenderbufferStorage(GL_RENDERBUFFER, format, width, height);
+`}
+
+# Format for a renderbuffer
+extern class GLRenderbufferFormat
+       super GLEnum
+end
+
+# 4 red, 4 green, 4 blue, 4 alpha bits format
+fun gl_RGBA4: GLRenderbufferFormat `{ return GL_RGBA4; `}
+
+# 5 red, 6 green, 5 blue bits format
+fun gl_RGB565: GLRenderbufferFormat `{ return GL_RGB565; `}
+
+# 5 red, 5 green, 5 blue, 1 alpha bits format
+fun gl_RGB_A1: GLRenderbufferFormat `{ return GL_RGB5_A1; `}
+
+# 16 depth bits format
+fun gl_DEPTH_COMPNENT16: GLRenderbufferFormat `{ return GL_DEPTH_COMPONENT16; `}
+
+# 8 stencil bits format
+fun gl_STENCIL_INDEX8: GLRenderbufferFormat `{ return GL_STENCIL_INDEX8; `}
+
+# Renderbuffer attachment point to a framebuffer
+extern class GLAttachment
+       super GLEnum
+end
+
+# First color attachment point
+fun gl_COLOR_ATTACHMENT0: GLAttachment `{ return GL_COLOR_ATTACHMENT0; `}
+
+# Depth attachment point
+fun gl_DEPTH_ATTACHMENT: GLAttachment `{ return GL_DEPTH_ATTACHMENT; `}
+
+# Stencil attachment
+fun gl_STENCIL_ATTACHMENT: GLAttachment `{ return GL_STENCIL_ATTACHMENT; `}
+
 redef class Sys
        private var gles = new GLES is lazy
 end
@@ -544,20 +680,20 @@ class GLES
        #
        # Foreign: glReadPixel
        fun read_pixels(x, y, width, height: Int, format: GLPixelFormat, typ: GLPixelType, data: Pointer) `{
-               glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
+               glReadPixels(x, y, width, height, format, typ, data);
        `}
 
        # Set the texture minifying function
        #
        # Foreign: glTexParameter with GL_TEXTURE_MIN_FILTER
-       fun tex_parameter_min_filter(target: GLTextureTarget, value: GLTextureMinFilter) `{
+       fun tex_parameter_min_filter(target: GLTextureTarget, value: GLTextureFilter) `{
                glTexParameteri(target, GL_TEXTURE_MIN_FILTER, value);
        `}
 
        # Set the texture magnification function
        #
        # Foreign: glTexParameter with GL_TEXTURE_MAG_FILTER
-       fun tex_parameter_mag_filter(target: GLTextureTarget, value: GLTextureMagFilter) `{
+       fun tex_parameter_mag_filter(target: GLTextureTarget, value: GLTextureFilter) `{
                glTexParameteri(target, GL_TEXTURE_MAG_FILTER, value);
        `}
 
@@ -619,6 +755,23 @@ fun glHint(target: GLHintTarget, mode: GLHintMode) `{
        glHint(target, mode);
 `}
 
+# Generate and fill set of mipmaps for the texture object `target`
+fun glGenerateMipmap(target: GLTextureTarget) `{ glGenerateMipmap(target); `}
+
+# Bind the named `buffer` object
+fun glBindBuffer(target: GLArrayBuffer, buffer: Int) `{ glBindBuffer(target, buffer); `}
+
+# Target to which bind the buffer with `glBindBuffer`
+extern class GLArrayBuffer
+       super GLEnum
+end
+
+# Array buffer target
+fun gl_ARRAY_BUFFER: GLArrayBuffer `{ return GL_ARRAY_BUFFER; `}
+
+# Element array buffer
+fun gl_ELEMENT_ARRAY_BUFFER: GLArrayBuffer `{ return GL_ELEMENT_ARRAY_BUFFER; `}
+
 # Completeness status of a framebuffer object
 fun glCheckFramebufferStatus(target: GLFramebufferTarget): GLFramebufferStatus `{
        return glCheckFramebufferStatus(target);
@@ -686,6 +839,12 @@ fun gl_NICEST: GLHintMode `{ return GL_NICEST; `}
 # No preference
 fun gl_DONT_CARE: GLHintMode `{ return GL_DONT_CARE; `}
 
+# Attach a level of a texture object as a logical buffer to the currently bound framebuffer object
+fun glFramebufferTexture2D(target: GLFramebufferTarget, attachment: GLAttachment,
+                           texture_target: GLTextureTarget,  texture, level: Int) `{
+       glFramebufferTexture2D(target, attachment, texture_target, texture, level);
+`}
+
 # Entry point to OpenGL server-side capabilities
 class GLCapabilities
 
index 8b90102..04a0c5d 100644 (file)
 module linux
 
 import app
+
+redef class App
+       redef fun setup
+       do
+               super
+
+               on_create
+               on_restore_state
+               on_start
+               on_resume
+       end
+
+       redef fun run
+       do
+               super
+
+               on_pause
+               on_save_state
+               on_stop
+               on_destroy
+       end
+end
diff --git a/lib/matrix/matrix.nit b/lib/matrix/matrix.nit
new file mode 100644 (file)
index 0000000..73131ca
--- /dev/null
@@ -0,0 +1,295 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Services for matrices of `Float` values
+module matrix
+
+# A rectangular array of `Float`
+#
+# Require: `width > 0 and height > 0`
+class Matrix
+       super Cloneable
+
+       # Number of columns
+       var width: Int
+
+       # Number of rows
+       var height: Int
+
+       # Items of this matrix, rows by rows
+       private var items: Array[Float] is lazy do
+               return new Array[Float].filled_with(0.0, width*height)
+       end
+
+       # Create a matrix from nested sequences
+       #
+       # Require: all rows are of the same length
+       #
+       # ~~~
+       # var matrix = new Matrix.from([[1.0, 2.0],
+       #                               [3.0, 4.0]])
+       # assert matrix.to_s == """
+       # 1.0 2.0
+       # 3.0 4.0"""
+       # ~~~
+       init from(items: SequenceRead[SequenceRead[Float]])
+       do
+               if items.is_empty then
+                       init(0, 0)
+                       return
+               end
+
+               init(items.first.length, items.length)
+
+               for j in height.times do assert items[j].length == width
+
+               for j in height.times do
+                       for i in width.times do
+                               self[j, i] = items[j][i]
+                       end
+               end
+       end
+
+       # Get each row of this matrix in nested arrays
+       #
+       # ~~~
+       # var items = [[1.0, 2.0],
+       #              [3.0, 4.0]]
+       # var matrix = new Matrix.from(items)
+       # assert matrix.to_a == items
+       # ~~~
+       fun to_a: Array[Array[Float]]
+       do
+               var a = new Array[Array[Float]]
+               for j in height.times do
+                       var row = new Array[Float]
+                       for i in width.times do
+                               row.add self[j, i]
+                       end
+                       a.add row
+               end
+               return a
+       end
+
+       # Create a matrix from an `Array[Float]` composed of rows after rows
+       #
+       # Require: `width > 0 and height > 0`
+       # Require: `array.length >= width*height`
+       #
+       # ~~~
+       # var matrix = new Matrix.from_array(2, 2, [1.0, 2.0,
+       #                                           3.0, 4.0])
+       # assert matrix.to_s == """
+       # 1.0 2.0
+       # 3.0 4.0"""
+       # ~~~
+       init from_array(width, height: Int, array: SequenceRead[Float])
+       do
+               assert width > 0
+               assert height > 0
+               assert array.length >= width*height
+
+               init(width, height)
+
+               for i in height.times do
+                       for j in width.times do
+                               self[j, i] = array[i + j*width]
+                       end
+               end
+       end
+
+       # Create an identity matrix
+       #
+       # Require: `size >= 0`
+       #
+       # ~~~
+       # var i = new Matrix.identity(3)
+       # assert i.to_s == """
+       # 1.0 0.0 0.0
+       # 0.0 1.0 0.0
+       # 0.0 0.0 1.0"""
+       # ~~~
+       new identity(size: Int)
+       do
+               assert size >= 0
+
+               var matrix = new Matrix(size, size)
+               for i in size.times do
+                       for j in size.times do
+                               matrix[j, i] = if i == j then 1.0 else 0.0
+                       end
+               end
+               return matrix
+       end
+
+       # Create a new clone of this matrix
+       redef fun clone do return new Matrix.from_array(width, height, items.clone)
+
+       # Get the value at column `y` and row `x`
+       #
+       # Require: `x >= 0 and x <= width and y >= 0 and y <= height`
+       #
+       # ~~~
+       # var matrix = new Matrix.from([[0.0, 0.1],
+       #                               [1.0, 1.1]])
+       #
+       # assert matrix[0, 0] == 0.0
+       # assert matrix[0, 1] == 0.1
+       # assert matrix[1, 0] == 1.0
+       # assert matrix[1, 1] == 1.1
+       # ~~~
+       fun [](y, x: Int): Float
+       do
+               assert x >= 0 and x < width
+               assert y >= 0 and y < height
+
+               return items[x + y*width]
+       end
+
+       # Set the `value` at row `y` and column `x`
+       #
+       # Require: `x >= 0 and x <= width and y >= 0 and y <= height`
+       #
+       # ~~~
+       # var matrix = new Matrix.identity(2)
+       #
+       # matrix[0, 0] = 0.0
+       # matrix[0, 1] = 0.1
+       # matrix[1, 0] = 1.0
+       # matrix[1, 1] = 1.1
+       #
+       # assert matrix.to_s == """
+       # 0.0 0.1
+       # 1.0 1.1"""
+       # ~~~
+       fun []=(y, x: Int, value: Float)
+       do
+               assert x >= 0 and x < width
+               assert y >= 0 and y < height
+
+               items[x + y*width] = value
+       end
+
+       # Matrix product (×)
+       #
+       # Require: `self.width == other.height`
+       #
+       # ~~~
+       # var m = new Matrix.from([[3.0, 4.0],
+       #                          [5.0, 6.0]])
+       # var i = new Matrix.identity(2)
+       #
+       # assert m * i == m
+       # assert (m * m).to_s == """
+       # 29.0 36.0
+       # 45.0 56.0"""
+       #
+       # var a = new Matrix.from([[1.0, 2.0, 3.0],
+       #                          [4.0, 5.0, 6.0]])
+       # var b = new Matrix.from([[1.0],
+       #                          [2.0],
+       #                          [3.0]])
+       # var c = a * b
+       # assert c.to_s == """
+       # 14.0
+       # 32.0"""
+       # ~~~
+       fun *(other: Matrix): Matrix
+       do
+               assert self.width == other.height
+
+               var out = new Matrix(other.width, self.height)
+               for j in self.height.times do
+                       for i in other.width.times do
+                               var sum = items.first.zero
+                               for k in self.width.times do sum += self[j, k] * other[k, i]
+                               out[j, i] = sum
+                       end
+               end
+               return out
+       end
+
+       # Get the transpose of this matrix
+       #
+       # ~~~
+       # var matrix = new Matrix.from([[1.0, 2.0, 3.0],
+       #                               [4.0, 5.0, 6.0]])
+       # assert matrix.transposed.to_a == [[1.0, 4.0],
+       #                                   [2.0, 5.0],
+       #                                   [3.0, 6.0]]
+       #
+       # var i = new Matrix.identity(3)
+       # assert i.transposed == i
+       # ~~~
+       fun transposed: Matrix
+       do
+               var out = new Matrix(height, width)
+               for k, v in self do out[k.x, k.y] = v
+               return out
+       end
+
+       # Iterate over the values in this matrix
+       fun iterator: MapIterator[MatrixCoordinate, Float] do return new MatrixIndexIterator(self)
+
+       redef fun to_s
+       do
+               var lines = new Array[String]
+               for y in height.times do
+                       lines.add items.subarray(y*width, width).join(" ")
+               end
+               return lines.join("\n")
+       end
+
+       redef fun ==(other) do return other isa Matrix and other.items == self.items
+       redef fun hash do return items.hash
+end
+
+private class MatrixIndexIterator
+       super MapIterator[MatrixCoordinate, Float]
+
+       var matrix: Matrix
+
+       redef var key = new MatrixCoordinate(0, 0)
+
+       redef fun is_ok do return key.y < matrix.height
+
+       redef fun item
+       do
+               assert is_ok
+               return matrix[key.y, key.x]
+       end
+
+       redef fun next
+       do
+               assert is_ok
+               var key = key
+               if key.x == matrix.width - 1 then
+                       key.x = 0
+                       key.y += 1
+               else
+                       key.x += 1
+               end
+       end
+end
+
+# Position key when iterating over the values of a matrix
+class MatrixCoordinate
+       # Index of the current column
+       var x: Int
+
+       # Index of the current row
+       var y: Int
+
+       redef fun to_s do return "({x},{y})"
+end
diff --git a/lib/matrix/package.ini b/lib/matrix/package.ini
new file mode 100644 (file)
index 0000000..4bd9013
--- /dev/null
@@ -0,0 +1,11 @@
+[package]
+name=matrix
+tags=lib
+maintainer=Alexis Laferrière <alexis.laf@xymus.net>
+license=Apache-2.0
+[upstream]
+browse=https://github.com/nitlang/nit/tree/master/lib/matrix/
+git=https://github.com/nitlang/nit.git
+git.directory=lib/matrix/
+homepage=http://nitlanguage.org
+issues=https://github.com/nitlang/nit/issues
diff --git a/lib/matrix/projection.nit b/lib/matrix/projection.nit
new file mode 100644 (file)
index 0000000..908761a
--- /dev/null
@@ -0,0 +1,153 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Services on `Matrix` to transform and project 3D coordinates
+module projection
+
+intrude import matrix
+
+redef class Matrix
+
+       # Create an orthogonal projection matrix
+       #
+       # `left, right, bottom, top, near, far` defines the world clip planes.
+       new orthogonal(left, right, bottom, top, near, far: Float)
+       do
+               var dx = right - left
+               var dy = top - bottom
+               var dz = far - near
+
+               assert dx != 0.0 and dy != 0.0 and dz != 0.0
+
+               var mat = new Matrix.identity(4)
+               mat[0, 0] = 2.0 / dx
+               mat[3, 0] = -(right + left) / dx
+               mat[1, 1] = 2.0 / dy
+               mat[3, 1] = -(top + bottom) / dy
+               mat[2, 2] = 2.0 / dz
+               mat[3, 2] = -(near + far) / dz
+               return mat
+       end
+
+       # Create a perspective transformation matrix
+       #
+       # Using the given vertical `field_of_view_y` in radians, the `aspect_ratio`
+       # and the `near`/`far` world distances.
+       new perspective(field_of_view_y, aspect_ratio, near, far: Float)
+       do
+               var frustum_height = (field_of_view_y/2.0).tan * near
+               var frustum_width = frustum_height * aspect_ratio
+
+               return new Matrix.frustum(-frustum_width, frustum_width,
+                                         -frustum_height, frustum_height,
+                                         near, far)
+       end
+
+       # Create a frustum transformation matrix
+       #
+       # `left, right, bottom, top, near, far` defines the world clip planes.
+       new frustum(left, right, bottom, top, near, far: Float)
+       do
+               var dx = right - left
+               var dy = top - bottom
+               var dz = far - near
+
+               assert near > 0.0
+               assert far > 0.0
+               assert dx > 0.0
+               assert dy > 0.0
+               assert dz > 0.0
+
+               var mat = new Matrix(4, 4)
+
+               mat[0, 0] = 2.0 * near / dx
+               mat[0, 1] = 0.0
+               mat[0, 2] = 0.0
+               mat[0, 3] = 0.0
+
+               mat[1, 0] = 0.0
+               mat[1, 1] = 2.0 * near / dy
+               mat[1, 2] = 0.0
+               mat[1, 3] = 0.0
+
+               mat[2, 0] = (right + left) / dx
+               mat[2, 1] = (top + bottom) / dy
+               mat[2, 2] = -(near + far) / dz
+               mat[2, 3] = -1.0
+
+               mat[3, 0] = 0.0
+               mat[3, 1] = 0.0
+               mat[3, 2] = -2.0 * near * far / dz
+               mat[3, 3] = 0.0
+
+               return mat
+       end
+
+       # Apply a translation by `x, y, z` to this matrix
+       fun translate(x, y, z: Float)
+       do
+               for i in [0..3] do
+                       self[3, i] = self[3,i] + self[0, i] * x + self[1, i] * y + self[2, i] * z
+               end
+       end
+
+       # Apply scaling on `x, y, z` to this matrix
+       fun scale(x, y, z: Float)
+       do
+               for i in [0..3] do
+                       self[0, i] = self[0, i] * x
+                       self[1, i] = self[1, i] * y
+                       self[2, i] = self[2, i] * z
+               end
+       end
+
+       # Create a rotation matrix by `angle` around the vector defined by `x, y, z`
+       new rotation(angle, x, y, z: Float)
+       do
+               var mat = new Matrix.identity(4)
+
+               var mag = (x*x + y*y + z*z).sqrt
+               var sin = angle.sin
+               var cos = angle.cos
+
+               if mag > 0.0 then
+                       x = x / mag
+                       y = y / mag
+                       z = z / mag
+
+                       var inv_cos = 1.0 - cos
+
+                       mat[0, 0] = inv_cos*x*x + cos
+                       mat[0, 1] = inv_cos*x*y - z*sin
+                       mat[0, 2] = inv_cos*z*x + y*sin
+
+                       mat[1, 0] = inv_cos*x*y + z*sin
+                       mat[1, 1] = inv_cos*y*y + cos
+                       mat[1, 2] = inv_cos*y*z - x*sin
+
+                       mat[2, 0] = inv_cos*z*x - y*sin
+                       mat[2, 1] = inv_cos*y*z + x*sin
+                       mat[2, 2] = inv_cos*z*z + cos
+               end
+               return mat
+       end
+
+       # Apply a rotation of `angle` radians around the vector `x, y, z`
+       fun rotate(angle, x, y, z: Float)
+       do
+               var rotation = new Matrix.rotation(angle, x, y, z)
+               var rotated = self * rotation
+               self.items = rotated.items
+       end
+end
index 938bfd8..7bc03a0 100644 (file)
@@ -35,21 +35,6 @@ redef class App
                display = new Opengles1Display
 
                super
-
-               on_create
-               on_restore_state
-               on_start
-               on_resume
-       end
-
-       redef fun run
-       do
-               super
-
-               on_pause
-               on_save_state
-               on_stop
-               on_destroy
        end
 
        redef fun generate_input
index 8a5acc9..2bd8fbe 100644 (file)
@@ -1,7 +1,5 @@
 # This file is part of NIT (http://www.nitlanguage.org).
 #
-# Copyright 2014 Lucas Bajolet <r4pass@hotmail.com>
-#
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
@@ -219,22 +217,12 @@ in "C Header" `{
        }
 `}
 
-redef class String
-
-       # Computes the SHA1 of the receiver
-       #
-       # Returns a digest of 20 bytes as a String,
-       # note that all the characters are not necessarily ASCII.
-       # If you want the hex string version of the digest, use
-       # sha1_to_s.
-       #
-       #     import base64
-       #     assert "The quick brown fox jumps over the lazy dog".sha1.encode_base64 == "L9ThxnotKPzthJ7hu3bnORuT6xI="
-       fun sha1: String import String.to_cstring, String.length, NativeString.to_s_with_length `{
+redef class NativeString
+       private fun sha1_intern(len: Int): NativeString `{
                sha1nfo s;
 
                sha1_init(&s);
-               sha1_write(&s, String_to_cstring(self), String_length(self));
+               sha1_write(&s, self, len);
                uint8_t* digest = sha1_result(&s);
 
                char* digested = malloc(21);
@@ -243,35 +231,30 @@ redef class String
 
                digested[20] = '\0';
 
-               return NativeString_to_s_with_length(digested, 20);
+               return digested;
        `}
+end
+
+redef class String
+
+       # Computes the SHA1 of the receiver
+       #
+       # Returns a digest of 20 bytes as a NativeString,
+       # note that all the characters are not necessarily ASCII.
+       # If you want the hex string version of the digest, use
+       # sha1_hexdigest.
+       #
+       #     import base64
+       #     assert "The quick brown fox jumps over the lazy dog".sha1 == [0x2Fu8, 0xD4u8, 0xE1u8, 0xC6u8, 0x7Au8, 0x2Du8, 0x28u8, 0xFCu8, 0xEDu8, 0x84u8, 0x9Eu8, 0xE1u8, 0xBBu8, 0x76u8, 0xE7u8, 0x39u8, 0x1Bu8, 0x93u8, 0xEBu8, 0x12u8]
+       fun sha1: Bytes do
+               return new Bytes(to_cstring.sha1_intern(bytelen), 20, 20)
+       end
 
        # Computes the SHA1 of the receiver.
        #
        # Returns a 40 char String containing the Hexadecimal
        # Digest in its Char form.
        #
-       #     assert "The quick brown fox jumps over the lazy dog".sha1_to_s == "2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12"
-       fun sha1_to_s: String import String.to_cstring, String.length, NativeString.to_s_with_length `{
-               sha1nfo s;
-
-               sha1_init(&s);
-               sha1_write(&s, String_to_cstring(self), String_length(self));
-               uint8_t* digest = sha1_result(&s);
-
-               char* ret_str = malloc(41);
-               char* hexmap = "0123456789ABCDEF";
-
-               int i;
-               for(i=0;i<20;i++){
-                       uint8_t q = digest[i];
-                       ret_str[i*2] = hexmap[q >> 4];
-                       ret_str[(i*2)+1] = hexmap[q & 0x0F];
-               }
-               ret_str[40] = '\0';
-
-               return NativeString_to_s_with_length(ret_str, 40);
-       `}
-
+       #     assert "The quick brown fox jumps over the lazy dog".sha1_hexdigest == "2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12"
+       fun sha1_hexdigest: String do return sha1.hexdigest
 end
-
index c3fdaad..2a3c32f 100644 (file)
@@ -114,7 +114,7 @@ class WebsocketConnection
                resp_map["Connection:"] = "Upgrade"
                var key = heads["Sec-WebSocket-Key"]
                key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
-               key = key.sha1.encode_base64
+               key = key.sha1.encode_base64.to_s
                resp_map["Sec-WebSocket-Accept:"] = key
                var resp = resp_map.join("\r\n", " ")
                resp += "\r\n\r\n"
index 9b4bf06..8d09d74 100644 (file)
@@ -102,7 +102,7 @@ The command `:Nitdoc` searches the documentation for the word under the cursor.
 The results are displayed in the preview window in order of relevance.
 You can search for any word by passing it as an argument, as in `:Nitdoc modulo`.
 The Nitdoc command uses the same metadata files as the omnifunc.
-You may want to map the function to a shortcut by adding the following code to `~/.vimrc`.
+You may want to map the command to a shortcut by adding the following code to `~/.vimrc`.
 
 ~~~
 " Map displaying Nitdoc to Ctrl-D
@@ -119,3 +119,18 @@ You may want to map the function to a shortcut by adding the following code to `
 " Map the NitGitGrep function to Ctrl-G
 map <C-g> :call NitGitGrep()<enter>
 ~~~
+
+## Execute the current file
+
+The command `:NitExecute` calls `nit` to interpret the current file.
+
+If modified, the current buffer is saved to a temporary file before being executed.
+This may cause failures if the current buffer imports modules relative to the source package.
+In such cases, save the file before calling `:NitExecute`.
+
+You may want to map the command to a shortcut by adding the following code to `~/.vimrc`.
+
+~~~
+" Map the NitExecute function to Ctrl-F
+map <C-f> :NitExecute<enter>
+~~~
index fafeb92..df81660 100755 (executable)
@@ -29,19 +29,22 @@ failed=
 for p in $projects; do
        dir=`dirname "$p"`
        name=`basename "$dir"`
-       echo "*** make $dir ***"
-       if misc/jenkins/unitrun.sh "cmd-$name-make" make -C "$dir"; then
-               # Make OK. Check additional rules if they exists
-               for rule in $rules; do
-                       make -C "$dir" $rule -n 2>/dev/null ||
-                               continue
-                       echo "*** make$rule $dir ***"
-                       misc/jenkins/unitrun.sh "cmd-$name-make$rule" make -C "$dir" $rule ||
-                               failed="$failed $name-$rule"
-               done
-       else
-               failed="$failed $name"
-       fi
+       echo "### in $dir ###"
+       # Check each rules, if they exists
+       for rule in $rules; do
+               make -C "$dir" $rule -n >/dev/null 2>/dev/null || {
+                       # Special case for `all` that falls back as the default target
+                       if [ "$rule" = "all" ]; then
+                               echo "*** make -C $dir ***"
+                               misc/jenkins/unitrun.sh "cmd-$name-make" make -C "$dir" ||
+                                       failed="$failed $name"
+                       fi
+                       continue
+               }
+               echo "*** make $rule -C $dir ***"
+               misc/jenkins/unitrun.sh "cmd-$name-make$rule" make -C "$dir" $rule ||
+                       failed="$failed $name-$rule"
+       done
 done
 grep '<error message' *-make*.xml
 if test -n "$failed"; then
index 363a8f9..6824882 100644 (file)
@@ -382,6 +382,19 @@ fun NitGitGrep()
        redraw!
 endfun
 
+" Call `nit` on the current file
+fun NitExecute()
+       let path = expand('%')
+
+       if &modified
+               let path = tempname() . '.nit'
+               execute '%write '. path
+       endif
+
+       execute '!nit "' . path . '"'
+endfun
+command NitExecute call NitExecute()
+
 if !exists("g:nit_disable_omnifunc") || !g:nit_disable_omnifunc
        " Activate the omnifunc on Nit files
        autocmd FileType nit set omnifunc=NitOmnifunc
index edf0a16..79c2f14 100644 (file)
@@ -280,7 +280,7 @@ redef class MExplicitCast
 
                        var from_var = nitni_visitor.var_from_c("from", from)
                        from_var = nitni_visitor.box_extern(from_var, from)
-                       var recv_var = nitni_visitor.type_test(from_var, to, "FFI isa")
+                       var recv_var = nitni_visitor.type_test(from_var, to, "isa")
                        nitni_visitor.add("return {recv_var};")
 
                        nitni_visitor.add("\}")
@@ -316,7 +316,7 @@ redef class MExplicitCast
                        from_var = nitni_visitor.box_extern(from_var, from)
 
                        ## test type
-                       var check = nitni_visitor.type_test(from_var, to, "FFI cast")
+                       var check = nitni_visitor.type_test(from_var, to, "as")
                        nitni_visitor.add("if (!{check}) \{")
                        nitni_visitor.add_abort("FFI cast failed")
                        nitni_visitor.add("\}")
index ba0cb1c..28ae918 100644 (file)
@@ -86,6 +86,12 @@ class CatalogPage
        # Placeholder to include additional things before the `</head>`.
        var more_head = new Template
 
+       # Relative path to the root directory (with the index file).
+       #
+       # Use "" for pages in the root directory
+       # Use ".." for pages in a subdirectory
+       var rootpath: String
+
        redef init
        do
                add """
@@ -94,7 +100,7 @@ class CatalogPage
 <head>
        <meta charset="utf-8">
        <link rel="stylesheet" media="all" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
-       <link rel="stylesheet" media="all" href="style.css">
+       <link rel="stylesheet" media="all" href="{{{rootpath / "style.css"}}}">
 """
                add more_head
 
@@ -116,7 +122,7 @@ class CatalogPage
     </div>
     <div class='collapse navbar-collapse' id='topmenu-collapse'>
      <ul class='nav navbar-nav'>
-      <li><a href="index.html">Catalog</a></li>
+      <li><a href="{{{rootpath / "index.html"}}}">Catalog</a></li>
      </ul>
     </div>
    </div>
@@ -270,7 +276,7 @@ class Catalog
        # Compute information and generate a full HTML page for a package
        fun package_page(mpackage: MPackage): Writable
        do
-               var res = new CatalogPage
+               var res = new CatalogPage("..")
                var score = score[mpackage].to_f
                var name = mpackage.name.html_escape
                res.more_head.add """<title>{{{name}}}</title>"""
@@ -369,7 +375,7 @@ class Catalog
                                if cat == null then cat = t
                                tag2proj[t].add mpackage
                                t = t.html_escape
-                               ts2.add "<a href=\"index.html#tag_{t}\">{t}</a>"
+                               ts2.add "<a href=\"../index.html#tag_{t}\">{t}</a>"
                        end
                        res.add_list(ts2, ", ", ", ")
                end
@@ -377,7 +383,7 @@ class Catalog
                        var t = "none"
                        cat = t
                        tag2proj[t].add mpackage
-                       res.add "<a href=\"index.html#tag_{t}\">{t}</a>"
+                       res.add "<a href=\"../index.html#tag_{t}\">{t}</a>"
                end
                if cat != null then cat2proj[cat].add mpackage
                score += ts2.length.score
@@ -496,7 +502,7 @@ class Catalog
        fun li_package(p: MPackage): String
        do
                var res = ""
-               var f = "{p.name}.html"
+               var f = "p/{p.name}.html"
                res += "<a href=\"{f}\">{p}</a>"
                var d = p.mdoc_or_fallback
                if d != null then res += " - {d.html_synopsis.write_to_string}"
@@ -614,7 +620,7 @@ class Catalog
                res.add "</tr></thead>"
                for p in mpackages do
                        res.add "<tr>"
-                       res.add "<td><a href=\"{p.name}.html\">{p.name}</a></td>"
+                       res.add "<td><a href=\"p/{p.name}.html\">{p.name}</a></td>"
                        var maint = "?"
                        if p.maintainers.not_empty then maint = p.maintainers.first
                        res.add "<td>{maint}</td>"
@@ -700,7 +706,7 @@ if not opt_no_model.value then
 end
 
 var out = opt_dir.value or else "catalog.out"
-out.mkdir
+(out/"p").mkdir
 
 # Generate the css (hard coded)
 var css = """
@@ -806,13 +812,13 @@ css.write_to_file(out/"style.css")
 
 for p in model.mpackages do
        # print p
-       var f = "{p.name}.html"
+       var f = "p/{p.name}.html"
        catalog.package_page(p).write_to_file(out/f)
 end
 
 # INDEX
 
-var index = new CatalogPage
+var index = new CatalogPage("")
 index.more_head.add "<title>Packages in Nit</title>"
 
 index.add """
@@ -859,7 +865,7 @@ index.write_to_file(out/"index.html")
 
 # PEOPLE
 
-var page = new CatalogPage
+var page = new CatalogPage("")
 page.more_head.add "<title>People of Nit</title>"
 page.add """<div class="content">\n<h1>People of Nit</h1>\n"""
 page.add "<h2>By Maintainer</h2>\n"
@@ -871,7 +877,7 @@ page.write_to_file(out/"people.html")
 
 # TABLE
 
-page = new CatalogPage
+page = new CatalogPage("")
 page.more_head.add "<title>Projets of Nit</title>"
 page.add """<div class="content">\n<h1>People of Nit</h1>\n"""
 page.add "<h2>Table of Projets</h2>\n"
index 72c219f..1a6f458 100644 (file)
@@ -337,11 +337,16 @@ $(call import-module,android/native_app_glue)
                        var tsa_server= "TSA_SERVER".environ
 
                        if key_alias.is_empty then
-                               toolcontext.error(null,
-                                       "Error: the environment variable `KEY_ALIAS` must be set to use the `--release` option on Android projects.")
+                               toolcontext.warning(null, "key-alias",
+                                       "Warning: the environment variable `KEY_ALIAS` is not set, the APK file will not be signed.")
+
+                               # Just move the unsigned APK to outname
+                               args = ["mv", apk_path, outname]
+                               toolcontext.exec_and_check(args, "Android project error")
                                return
                        end
 
+                       # We have a key_alias, try to sign the APK
                        args = ["jarsigner", "-sigalg", "MD5withRSA", "-digestalg", "SHA1", apk_path, key_alias]
 
                        ## Use a custom keystore
index c3eac84..5a6669c 100644 (file)
@@ -1,4 +1,5 @@
 fatal error: 'endian.h' file not found
 Error: package `glesv1_cm` unknown by `pkg-config`, make sure the development package is be installed
 Error: package `glesv2` unknown by `pkg-config`, make sure the development package is be installed
+Error: package `egl` unknown by `pkg-config`, make sure the development package is be installed
 fatal error: 'libintl.h' file not found
index 1066456..ff641cc 100644 (file)
@@ -21,7 +21,7 @@ Calls to bytepos for each type:
        FlatString = 18
 Calls to first_byte on FlatString 153
 Calls to last_byte on FlatString 103
-FlatStrings allocated with length 81 (85.417%)
+FlatStrings allocated with length 82 (86.458%)
 Length of travel for index distribution:
 * null = 20 => occurences 83.333%, cumulative 83.333% 
 * 1 = 8 => occurences 21.053%, cumulative 73.684% 
index fe675df..ce7118b 100644 (file)
@@ -13,6 +13,7 @@ redef class Deserializer
                if name == "Array[nullable Object]" then return new Array[nullable Object].from_deserializer(self)
                if name == "Array[Serializable]" then return new Array[Serializable].from_deserializer(self)
                if name == "Array[Object]" then return new Array[Object].from_deserializer(self)
+               if name == "Array[Int]" then return new Array[Int].from_deserializer(self)
                if name == "Array[Match]" then return new Array[Match].from_deserializer(self)
                if name == "Array[nullable Match]" then return new Array[nullable Match].from_deserializer(self)
                return super
diff --git a/tests/sav/test_bytes_hexdigit.res b/tests/sav/test_bytes_hexdigit.res
new file mode 100644 (file)
index 0000000..f00af0c
--- /dev/null
@@ -0,0 +1,3 @@
+0x0b
+0x1f
+0x4d
index 49e9adc..f6e7b69 100644 (file)
@@ -21,7 +21,7 @@ Calls to bytepos for each type:
        FlatString = 18
 Calls to first_byte on FlatString 153
 Calls to last_byte on FlatString 103
-FlatStrings allocated with length 81 (85.417%)
+FlatStrings allocated with length 82 (86.458%)
 Length of travel for index distribution:
 * 0 = 20 => occurences 83.333%, cumulative 83.333% 
 * 1 = 8 => occurences 21.053%, cumulative 73.684% 
diff --git a/tests/test_bytes_hexdigit.nit b/tests/test_bytes_hexdigit.nit
new file mode 100644 (file)
index 0000000..28f51e3
--- /dev/null
@@ -0,0 +1,17 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+var s = "0B1F4D".hexdigest_to_bytes
+
+for i in s do print i