|
| 1 | +--- Calculates and sets camera zoom to maintain aspect ratio |
| 2 | +local function camera_update_zoom(self) |
| 3 | + local display_width = sys.get_config_int("display.width") |
| 4 | + local display_height = sys.get_config_int("display.height") |
| 5 | + local window_width,window_height = window.get_size() |
| 6 | + local scale = window.get_display_scale() |
| 7 | + local zoom = math.min(window_width / scale / display_width, window_height / scale / display_height) |
| 8 | + go.set("/camera#camera", "orthographic_zoom", zoom) |
| 9 | +end |
| 10 | + |
| 11 | +--- Fills the noise texture buffer with new values based on the current position and scale. |
| 12 | +local function noise_fill_stream(data) |
| 13 | + local stream = data.stream |
| 14 | + local i = 1 |
| 15 | + local h = data.height |
| 16 | + local w = data.width |
| 17 | + local scale = data.scale |
| 18 | + for y = 1, h do |
| 19 | + for x = 1, w do |
| 20 | + local v = simplex.noise2(data.x + x * scale, data.y + y * scale) * 255 |
| 21 | + stream[i + 0] = v -- R |
| 22 | + stream[i + 1] = v -- G |
| 23 | + stream[i + 2] = v -- B |
| 24 | + stream[i + 3] = 255 -- A |
| 25 | + |
| 26 | + i = i + 4 |
| 27 | + end |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +--- Initializes the noise texture. Returns initialized data. |
| 32 | +local function noise_init() |
| 33 | + local data = { |
| 34 | + x = 0, |
| 35 | + y = 0, |
| 36 | + scale = 0.05, |
| 37 | + width = 128, |
| 38 | + height = 128, |
| 39 | + |
| 40 | + texture_path = "/example/noise_texture.texturec" |
| 41 | + } |
| 42 | + |
| 43 | + local buf = buffer.create(data.width * data.height, { { name=hash("rgba"), type=buffer.VALUE_TYPE_UINT8, count=4 } } ) |
| 44 | + local stream = buffer.get_stream(buf, hash("rgba")) |
| 45 | + data.buffer = buf |
| 46 | + data.stream = stream |
| 47 | + |
| 48 | + noise_fill_stream(data) |
| 49 | + |
| 50 | + data.texture_params = { |
| 51 | + width = data.width, |
| 52 | + height = data.height, |
| 53 | + type = resource.TEXTURE_TYPE_2D, |
| 54 | + format = resource.TEXTURE_FORMAT_RGBA, |
| 55 | + num_mip_maps = 1, |
| 56 | + min_filter = graphics.TEXTURE_FILTER_LINEAR, |
| 57 | + mag_filter = graphics.TEXTURE_FILTER_LINEAR, |
| 58 | + } |
| 59 | + |
| 60 | + local texture_id = resource.create_texture(data.texture_path, data.texture_params, buf) |
| 61 | + go.set("/debug_view#model", "texture0", texture_id) |
| 62 | + |
| 63 | + return data |
| 64 | +end |
| 65 | + |
| 66 | +--- Updates the noise texture with new values based on time. |
| 67 | +local function noise_update(data, dt) |
| 68 | + data.x = data.x + 1 * dt |
| 69 | + |
| 70 | + noise_fill_stream(data) |
| 71 | + |
| 72 | + resource.set_texture(data.texture_path, data.texture_params, data.buffer) |
| 73 | +end |
| 74 | + |
| 75 | +--- Calculates and displays the current frames per second using a rolling average. |
| 76 | +local function fps_update(self) |
| 77 | + -- fps meter is from examples/bunnymark |
| 78 | + if not self.frames then |
| 79 | + self.frames = {} |
| 80 | + end |
| 81 | + self.frames[#self.frames + 1] = socket.gettime() |
| 82 | + local fps = 0 |
| 83 | + if #self.frames == 61 then |
| 84 | + table.remove(self.frames, 1) |
| 85 | + fps = 1 / ((self.frames[#self.frames] - self.frames[1]) / (#self.frames - 1)) |
| 86 | + end |
| 87 | + label.set_text("/debug_label#label", ("FPS: %.2f"):format(fps)) |
| 88 | +end |
| 89 | + |
| 90 | +-- |
| 91 | +-- Script |
| 92 | +-- |
| 93 | + |
1 | 94 | function init(self) |
2 | | - |
| 95 | + self.data = noise_init() |
3 | 96 | end |
4 | 97 |
|
5 | 98 | function final(self) |
6 | 99 | end |
7 | 100 |
|
8 | 101 | function update(self, dt) |
| 102 | + camera_update_zoom(self) |
| 103 | + noise_update(self.data, dt) |
| 104 | + fps_update(self) |
9 | 105 | end |
10 | 106 |
|
11 | 107 | function on_message(self, message_id, message, sender) |
|
0 commit comments