Skip to content
Snippets Groups Projects
Commit 096d7319 authored by pippin's avatar pippin
Browse files

clouds,mandelbrot: update with modesetting API changes

parent 40e8bb6a
No related branches found
No related tags found
No related merge requests found
...@@ -71,6 +71,10 @@ class App(Application): ...@@ -71,6 +71,10 @@ class App(Application):
for c in self.clouds: for c in self.clouds:
c.draw(ctx) c.draw(ctx)
def on_enter(self, vm):
super().on_enter(vm)
sys_display.set_mode(24 + sys_display.osd)
if __name__ == "__main__": if __name__ == "__main__":
from st3m.run import run_app from st3m.run import run_app
......
from st3m.application import Application
import sys_display, math, random
class App(Application):
def __init__(self, app_ctx):
super().__init__(app_ctx)
def on_enter(self, vm):
super().on_enter(vm)
sys_display.set_mode(1)
pal = bytearray(256 * 3)
modr = random.getrandbits(7) + 1
modg = random.getrandbits(7) + 1
modb = random.getrandbits(7) + 1
pal[0 * 3] = 0
pal[0 * 3 + 1] = 0
pal[0 * 3 + 2] = 0
pal[1 * 3] = 255
pal[1 * 3 + 1] = 255
pal[1 * 3 + 2] = 255
sys_display.set_palette(pal)
self.y = 0
self.xa = -1.5
self.xb = 1.5
self.ya = -2.0
self.yb = 1.0
def draw(self, ctx: Context):
fb_info = sys_display.fb(0)
fb = fb_info[0]
max_iterations = 30
width = fb_info[1]
height = fb_info[2]
stride = fb_info[3]
for chunk in range(3): # do 3 scanlines at a time
y = self.y
if y < height:
zy = y * (self.yb - self.ya) / (height - 1) + self.ya
inners = 0
for x in range(width):
zx = x * (self.xb - self.xa) / (width - 1) + self.xa
z = zy + zx * 1j
c = z
reached = 0
for i in range(max_iterations):
if abs(z) > 2.0:
break
z = z * z + c
reached = i
val = reached * 255 / max_iterations
val = int(math.sqrt(val / 255) * 16) & 1
fb[int((y * stride * 8 + x) / 8)] |= int(val) << (x & 7)
self.y += 1
if __name__ == "__main__":
import st3m.run
st3m.run.run_app(App)
...@@ -16,20 +16,22 @@ class App(Application): ...@@ -16,20 +16,22 @@ class App(Application):
self.yb = 1.0 self.yb = 1.0
def draw(self, ctx: Context): def draw(self, ctx: Context):
fb = sys_display.fb(4) fb_info = sys_display.fb(0)
fb = fb_info[0]
max_iterations = 30 max_iterations = 30
imgx = 240 width = fb_info[1]
imgy = 240 height = fb_info[2]
stride = fb_info[3]
for chunk in range(3): # do 3 scanlines at a time for chunk in range(3): # do 3 scanlines at a time
y = self.y y = self.y
if y < 240: if y < height:
zy = y * (self.yb - self.ya) / (imgy - 1) + self.ya zy = y * (self.yb - self.ya) / (height - 1) + self.ya
inners = 0 inners = 0
for x in range(imgx): for x in range(width):
zx = x * (self.xb - self.xa) / (imgx - 1) + self.xa zx = x * (self.xb - self.xa) / (width - 1) + self.xa
z = zy + zx * 1j z = zy + zx * 1j
c = z c = z
reached = 0 reached = 0
...@@ -42,8 +44,7 @@ class App(Application): ...@@ -42,8 +44,7 @@ class App(Application):
val = math.sqrt(val / 255) * 16 val = math.sqrt(val / 255) * 16
if val > 15: if val > 15:
val = 15 val = 15
oval = fb[int((y * 240 + x) / 2)] fb[int((y * stride * 2 + x) / 2)] |= int(val) << ((x & 1) * 4)
fb[int((y * 240 + x) / 2)] = oval | (int(val) << ((x & 1) * 4))
self.y += 1 self.y += 1
def think(self, ins, delta_ms): def think(self, ins, delta_ms):
......
...@@ -8,7 +8,7 @@ class App(Application): ...@@ -8,7 +8,7 @@ class App(Application):
def on_enter(self, vm): def on_enter(self, vm):
super().on_enter(vm) super().on_enter(vm)
sys_display.set_mode(8) sys_display.set_mode(sys_display.cool | sys_display.x2)
self.y = 0 self.y = 0
self.xa = -1.5 self.xa = -1.5
self.xb = 1.5 self.xb = 1.5
...@@ -16,24 +16,26 @@ class App(Application): ...@@ -16,24 +16,26 @@ class App(Application):
self.yb = 1.0 self.yb = 1.0
def draw(self, ctx: Context): def draw(self, ctx: Context):
fb = sys_display.fb(8) fb_info = sys_display.fb(sys_display.cool)
fb = fb_info[0]
max_iterations = 30 max_iterations = 30
imgx = 240 width = fb_info[1]
imgy = 240 height = fb_info[2]
stride = fb_info[3]
for chunk in range(3): # do 3 scanlines at a time for chunk in range(3): # do 3 scanlines at a time
y = self.y y = self.y
if y < 240: if y < height:
zy = y * (self.yb - self.ya) / (imgy - 1) + self.ya zy = y * (self.yb - self.ya) / (height - 1) + self.ya
inners = 0 inners = 0
for x in range(imgx / 2): for x in range(width / 2):
zx = x * (self.xb - self.xa) / (imgx - 1) + self.xa zx = x * (self.xb - self.xa) / (width - 1) + self.xa
z = zy + zx * 1j z = zy + zx * 1j
c = z c = z
reached = 0 reached = 0
if inners > 10 and y < 180: if inners > 10 and y < height * 0.7:
reached = max_iterations - 1 reached = max_iterations - 1
else: else:
for i in range(max_iterations): for i in range(max_iterations):
...@@ -45,8 +47,8 @@ class App(Application): ...@@ -45,8 +47,8 @@ class App(Application):
inners += 1 inners += 1
val = reached * 255 / max_iterations val = reached * 255 / max_iterations
val = math.sqrt(val / 255) * 255 val = math.sqrt(val / 255) * 255
fb[y * 240 + x] = int(val) fb[y * stride + x] = int(val)
fb[y * 240 + 239 - x] = int(val) fb[y * stride + width - 1 - x] = int(val)
self.y += 1 self.y += 1
def think(self, ins, delta_ms): def think(self, ins, delta_ms):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment