X-Git-Url: http://nitlanguage.org diff --git a/lib/gamnit/flat.nit b/lib/gamnit/flat.nit index c58658c..b70d010 100644 --- a/lib/gamnit/flat.nit +++ b/lib/gamnit/flat.nit @@ -30,26 +30,70 @@ # devices. It is used to position the sprites in `App::ui_sprites`. # # See the sample game at `contrib/asteronits/` and the basic project template -# at `lib/gamnit/examples/template_flat/`. +# at `lib/gamnit/examples/template/`. module flat import glesv2 intrude import geometry::points_and_lines # For _x, _y and _z +intrude import matrix import matrix::projection import more_collections import performance_analysis import gamnit -import gamnit::cameras +intrude import gamnit::cameras +intrude import gamnit::cameras_cache +import gamnit::dynamic_resolution import gamnit::limit_fps +import gamnit::camera_control -import android_two_fingers_motion is conditional(android) - -# Draw a `texture` at `center` +# Visible 2D entity in the game world or UI +# +# Similar to `gamnit::Actor` which is in 3D. # -# An instance of `Sprite` can only belong to a single `SpriteSet` at -# a time. The on screen position depends on the camera associated +# Each sprite associates a `texture` to the position `center`. +# The appearance is modified by `rotation`, `invert_x`, +# `scale`, `red`, `green`, `blue` and `alpha`. +# These values can be changed at any time and will trigger an update +# of the data on the GPU side, having a small performance cost. +# +# For a sprite to be visible, it must be added to either the world `sprites` +# or the `ui_sprites`. +# However, an instance of `Sprite` can only belong to a single `SpriteSet` +# at a time. The final on-screen position depends on the camera associated # to the `SpriteSet`. +# +# ~~~ +# # Load texture and create sprite +# var texture = new Texture("path/in/assets.png") +# var sprite = new Sprite(texture, new Point3d[Float](0.0, 0.0, 0.0)) +# +# # Add sprite to the visible game world +# app.sprites.add sprite +# +# # Extra configuration of the sprite +# sprite.rotation = pi/2.0 +# sprite.scale = 2.0 +# +# # Show only the blue colors +# sprite.red = 0.0 +# sprite.green = 0.0 +# ~~~ +# +# To add a sprite to the UI it can be anchored to screen borders +# with `ui_camera.top_left` and the likes. +# +# ~~~nitish +# # Place it a bit off the top left of the screen +# var pos = app.ui_camera.top_left.offset(128.0, -128.0, 0) +# +# # Load texture and create sprite +# var texture = new Texture("path/in/assets.png") +# var sprite = new Sprite(texture, pos) +# +# # Add it to the UI (above world sprites) +# app.ui_sprites.add sprite +# ~~~ class Sprite # Texture drawn to screen @@ -79,10 +123,10 @@ class Sprite center_direct = value end - # Rotation on the Z axis, positive values go counterclockwise + # Rotation on the Z axis, positive values turn counterclockwise var rotation = 0.0 is writable(rotation_direct=) - # Rotation on the Z axis, positive values go counterclockwise + # Rotation on the Z axis, positive values turn counterclockwise fun rotation=(value: Float) do if isset _rotation and value != rotation then needs_update @@ -101,10 +145,12 @@ class Sprite # Scale applied to this sprite # - # The default size of `self` depends on the size in pixels of `texture`. + # The basic size of `self` depends on the size in pixels of `texture`. var scale = 1.0 is writable(scale_direct=) - # Scale applied to this sprite, see `scale` + # Scale applied to this sprite + # + # The basic size of `self` depends on the size in pixels of `texture`. fun scale=(value: Float) do if isset _scale and value != scale then needs_update @@ -217,25 +263,25 @@ redef class App # Camera for world `sprites` and `depth::actors` with perspective # - # By default, the camera is configured to respect the resolution - # of the screen in world coordinates at `z == 0.0`. + # By default, the camera is configured to a height of 1080 units + # of world coordinates at `z == 0.0`. var world_camera: EulerCamera is lazy do var camera = new EulerCamera(app.display.as(not null)) - # Aim for pixel resolution at level 0 - camera.reset_height - camera.near = 100.0 + # Aim for full HD pixel resolution at level 0 + camera.reset_height 1080.0 + camera.near = 10.0 return camera end # Camera for `ui_sprites` using an orthogonal view - var ui_camera: UICamera = new UICamera(app.display.as(not null)) is lazy + var ui_camera = new UICamera(app.display.as(not null)) is lazy - # World sprites to draw as seen by `world_camera` + # World sprites drawn as seen by `world_camera` var sprites: Set[Sprite] = new SpriteSet - # UI sprites to draw as seen by `ui_camera`, drawn over world `sprites` + # UI sprites drawn as seen by `ui_camera`, over world `sprites` var ui_sprites: Set[Sprite] = new SpriteSet # Main method to refine in clients to update game logic and `sprites` @@ -248,9 +294,7 @@ redef class App do texture.load - ui_camera.reset_height 1080.0 - - var splash = new Sprite(texture, ui_camera.center) + var splash = new Sprite(texture, ui_camera.center.offset(0.0, 0.0, 0.0)) ui_sprites.add splash var display = display @@ -328,11 +372,19 @@ redef class App if display != null then display.close end - redef fun frame_core(display) + redef fun on_resize(display) do - # Prepare to draw, clear buffers - glClear(gl_COLOR_BUFFER_BIT | gl_DEPTH_BUFFER_BIT) + super + + world_camera.mvp_matrix_cache = null + ui_camera.mvp_matrix_cache = null + # Update all sprites in the UI + for sprite in ui_sprites do sprite.needs_update + end + + redef fun frame_core(display) + do # Check errors var gl_error = glGetError assert gl_error == gl_NO_ERROR else print_error gl_error @@ -341,6 +393,7 @@ redef class App perf_clock_main.lapse var dt = clock.lapse.to_f update dt + frame_dt = dt sys.perfs["gamnit flat update client"].add perf_clock_main.lapse # Draw and flip screen @@ -352,16 +405,21 @@ redef class App assert gl_error == gl_NO_ERROR else print_error gl_error end + private var frame_dt = 0.0 + # Draw the whole screen, all `glDraw...` calls should be executed here protected fun frame_core_draw(display: GamnitDisplay) do - perf_clock_main.lapse + frame_core_dynamic_resolution_before display + perf_clock_main.lapse frame_core_world_sprites display perfs["gamnit flat world_sprites"].add perf_clock_main.lapse frame_core_ui_sprites display perfs["gamnit flat ui_sprites"].add perf_clock_main.lapse + + frame_core_dynamic_resolution_after display end private fun frame_core_sprites(display: GamnitDisplay, sprite_set: SpriteSet, camera: Camera) @@ -370,6 +428,9 @@ redef class App simple_2d_program.use simple_2d_program.mvp.uniform camera.mvp_matrix + sprite_set.time += frame_dt*sprite_set.time_mod + simple_2d_program.time.uniform sprite_set.time + # draw sprite_set.draw end @@ -452,6 +513,9 @@ private class Simple2dProgram // Model view projection matrix uniform mat4 mvp; + // Current world time, in seconds + uniform float time; + // Rotation matrix attribute vec4 rotation_row0; attribute vec4 rotation_row1; @@ -492,7 +556,7 @@ private class Simple2dProgram { if(use_texture) { gl_FragColor = v_color * texture2D(texture0, v_coord); - if (gl_FragColor.a <= 0.1) discard; + if (gl_FragColor.a <= 0.01) discard; } else { gl_FragColor = v_color; } @@ -537,12 +601,6 @@ private class Simple2dProgram end redef class Point3d[N] - # Get a new `Point3d[Float]` with an offset of each axis of `x, y, z` - fun offset(x, y, z: Numeric): Point3d[Float] - do - return new Point3d[Float](self.x.to_f+x.to_f, self.y.to_f+y.to_f, self.z.to_f+z.to_f) - end - # --- # Associate each point to its sprites @@ -593,12 +651,32 @@ redef class Point3d[N] end end +redef class OffsetPoint3d + redef fun x=(v) + do + if isset _x and v != x then needs_update + super + end + + redef fun y=(v) + do + if isset _y and v != y then needs_update + super + end + + redef fun z=(v) + do + if isset _z and v != z then needs_update + super + end +end + # Set of sprites sorting them into different `SpriteContext` private class SpriteSet super HashSet[Sprite] # Map texture then static vs dynamic to a `SpriteContext` - var contexts_map = new HashMap2[GamnitRootTexture, Bool, SpriteContext] + var contexts_map = new HashMap2[RootTexture, Bool, SpriteContext] # Contexts in `contexts_map` var contexts_items = new Array[SpriteContext] @@ -606,6 +684,12 @@ private class SpriteSet # Sprites needing resorting in `contexts_map` var sprites_to_remap = new Array[Sprite] + # Animation speed multiplier (0.0 to pause, 1.0 for normal speed, etc.) + var time_mod = 1.0 is writable + + # Seconds elapsed since the launch of the program, in world time responding to `time_mod` + var time = 0.0 + # Add a sprite to the appropriate context fun map_sprite(sprite: Sprite) do @@ -693,7 +777,7 @@ private class SpriteContext # Context config and state # Only root texture drawn by this context - var texture: nullable GamnitRootTexture + var texture: nullable RootTexture # OpenGL ES usage of `buffer_array` and `buffer_element` var usage: GLBufferUsage @@ -856,7 +940,7 @@ private class SpriteContext else rot = new Matrix.rotation(sprite.rotation, 0.0, 0.0, 1.0) end - data.fill_from(rot.items, o+15) + data.fill_from_matrix(rot, o+15) o += float_per_vertex end @@ -1036,6 +1120,8 @@ end # The data can be compressed by a call to `defragment`. # # ~~~ +# intrude import gamnit::flat +# # var array = new GroupedArray[String] # assert array.to_s == "" # @@ -1074,7 +1160,7 @@ end # # array.add "E" # assert array.to_s == "[A,B,c,D,E]" -# assert array.capacity == 5 +# assert array.capacity == 6 # assert array.length == 5 # # array.remove "A" @@ -1195,6 +1281,8 @@ private class GroupedArray[E] # Returns the elements that moved as a list. # # ~~~ + # intrude import gamnit::flat + # # var array = new GroupedArray[String] # array.add "a" # array.add "b" @@ -1254,3 +1342,21 @@ private class GroupedArray[E] return ss.join end end + +redef class GLfloatArray + private fun fill_from_matrix(matrix: Matrix, dst_offset: nullable Int) + do + dst_offset = dst_offset or else 0 + var mat_len = matrix.width*matrix.height + assert length >= mat_len + dst_offset + native_array.fill_from_matrix_native(matrix.items, dst_offset, mat_len) + end +end + +redef class NativeGLfloatArray + private fun fill_from_matrix_native(matrix: matrix::NativeDoubleArray, dst_offset, len: Int) `{ + int i; + for (i = 0; i < len; i ++) + self[i+dst_offset] = (GLfloat)matrix[i]; + `} +end