shoot: use a scale factor to compute the game dimentions
authorJean Privat <jean@pryen.org>
Mon, 14 Apr 2014 20:10:55 +0000 (16:10 -0400)
committerJean Privat <jean@pryen.org>
Tue, 15 Apr 2014 12:50:57 +0000 (08:50 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

examples/shoot/src/shoot.nit

index 9612992..50d7d3f 100644 (file)
@@ -28,13 +28,13 @@ redef class Sprite
        # Helper function to draw an image centered on the current sprite position
        fun draw_image(app: ShootApp, img: Image)
        do
-               app.display.blit_centered(img, self.x/100, self.y/100)
+               app.display.blit_centered(img, (self.x.to_f/app.scale).to_i, (self.y.to_f/app.scale).to_i)
        end
 
        # Helper function to draw an image translated and rotated on the current sprite position
        fun draw_rotated_image(app: ShootApp, img: Image, dx, dy: Int, angle: Float)
        do
-               app.display.blit_rotated(img, self.x.to_f/100.0, self.y.to_f/100.0, angle)
+               app.display.blit_rotated(img, self.x.to_f/app.scale, self.y.to_f/app.scale, angle)
        end
 end
 
@@ -167,7 +167,7 @@ end
 
 redef class Scene
        fun draw_on_display(app: ShootApp) do end
-       fun input(input_event: InputEvent): Bool do return false
+       fun input(app: ShootApp, input_event: InputEvent): Bool do return false
 end
 
 redef class PlayScene
@@ -177,19 +177,19 @@ redef class PlayScene
                self.sprites.draw(app)
                for i in [0..player.money[
                do
-                       app.display.blit(app.img_money, 10, 590-i)
+                       app.display.blit(app.img_money, 10, app.display.height-10-i)
                end
                for i in [1..player.nbshoots]
                do
-                       app.display.blit(app.img_player_shoot, 30, 590 - i*10)
+                       app.display.blit(app.img_player_shoot, 30, app.display.height-10 - i*10)
                end
                for i in [1..player.nbmissiles]
                do
-                       app.display.blit(app.img_player_missile, 40, 590 - i*20)
+                       app.display.blit(app.img_player_missile, 40, app.display.height-10 - i*20)
                end
        end
 
-       redef fun input( input_event )
+       redef fun input(app, input_event)
        do
                var speed = 400
                if input_event isa KeyEvent then
@@ -220,7 +220,9 @@ redef class PlayScene
                        end
                        return true
                else if input_event isa PointerEvent then
-                       player.goes_to((input_event.x*100.0).to_i, (input_event.y*100.0).to_i, speed)
+                       var x = (input_event.x * app.scale).to_i
+                       var y = (input_event.y * app.scale).to_i
+                       player.goes_to(x, y, speed)
                        return true
                end
                return false # unknown event, can be handled by something else
@@ -232,14 +234,23 @@ end
 redef class MenuScene
        redef fun draw_on_display(app)
        do
-               app.display.blit(app.img_splash, 0, 0)
+               var display = app.display
+               assert display != null
+               blit_fs(display, app.img_splash)
                sprites.draw(app)
                if not play or ttl%10 > 5 then
-                       app.display.blit(app.img_splash_play, 0, 0)
+                       blit_fs(display, app.img_splash_play)
                end
        end
 
-       redef fun input(input_event)
+       fun blit_fs(d: Display, img: Image)
+       do
+               var w = d.width.to_f
+               var h = d.height.to_f
+               d.blit_stretched(img, 0.0,0.0, 0.0,h, w,h, w,0.0)
+       end
+
+       redef fun input(app, input_event)
        do
                if input_event isa KeyEvent then
                        play = true
@@ -264,17 +275,17 @@ class ShootApp
        do
                s.draw_on_display(self)
                if debug and s.width != 0 and s.height != 0 then
-                       var left = s.left.to_f/100.0
-                       var right = s.right.to_f/100.0
-                       var top = s.top.to_f/100.0
-                       var bot = s.bottom.to_f/100.0
+                       var left = s.left.to_f/scale
+                       var right = s.right.to_f/scale
+                       var top = s.top.to_f/scale
+                       var bot = s.bottom.to_f/scale
                        display.blit_stretched(img_hitbox, right, top, right, bot, left, bot, left, top)
                end
        end
 
        init do super
 
-       var scene: Scene
+       var scene: ShotScene
 
        var img_hitbox: Image
 
@@ -308,6 +319,8 @@ class ShootApp
        do
                super
 
+               scale = (800.0 * 600.0 / display.width.to_f / display.height.to_f).sqrt * 100.0
+
                # TODO load assets here
                # ex: img = load_image( "img.png" )
                #     to get file located at assets/img.png before deployement
@@ -338,7 +351,19 @@ class ShootApp
                self.img_boss_left = load_image("boss_left.png")
                self.img_boss_right = load_image("boss_right.png")
 
-               self.scene = new MenuScene
+               var w = (display.width.to_f * scale).to_i
+               var h = (display.height.to_f * scale).to_i
+               self.scene = new MenuScene(w, h)
+       end
+
+       # Whole scaling to convert display pixels to game pixels
+       var scale: Float = 200.0
+
+       redef fun load_image(filename)
+       do
+               var res = super
+               res.scale = 100.0 / self.scale
+               return res
        end
 
        redef fun frame_core( display )
@@ -376,7 +401,7 @@ class ShootApp
                end
 
                # Maybe the event is specific to the scene
-               return self.scene.input(input_event)
+               return self.scene.input(self, input_event)
        end
 end