Skip to content

Commit

Permalink
mange textures in gui_app
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Feb 15, 2025
1 parent 90148c9 commit 3a302ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
17 changes: 17 additions & 0 deletions system/ui/lib/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, width: int, height: int):
self._fonts: dict[FontWeight, rl.Font] = {}
self._width = width
self._height = height
self._textures: list[rl.Texture] = []

def init_window(self, title: str, fps: int=DEFAULT_FPS):
atexit.register(self.close) # Automatically call close() on exit
Expand All @@ -35,7 +36,23 @@ def init_window(self, title: str, fps: int=DEFAULT_FPS):
self._set_styles()
self._load_fonts()

def load_texture_from_image(self, file_name: str, width: int, height: int):
"""Load and resize a texture, storing it for later automatic unloading."""
image = rl.load_image(file_name)
rl.image_resize(image, width, height)
texture = rl.load_texture_from_image(image)
# Set texture filtering to smooth the result
rl.set_texture_filter(texture, rl.TextureFilter.TEXTURE_FILTER_BILINEAR)

rl.unload_image(image)

self._textures.append(texture)
return texture

def close(self):
for texture in self._textures:
rl.unload_texture(texture)

for font in self._fonts.values():
rl.unload_font(font)

Expand Down
18 changes: 2 additions & 16 deletions system/ui/spinner.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@
def clamp(value, min_value, max_value):
return max(min(value, max_value), min_value)

def load_texture_resized(file_name, size):
image = rl.load_image(file_name.encode('utf-8'))
rl.image_resize(image, size, size)
texture = rl.load_texture_from_image(image)
# Set texture filtering to smooth the result
rl.set_texture_filter(texture, rl.TEXTURE_FILTER_BILINEAR)

rl.unload_image(image)
return texture

def check_input_non_blocking():
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
return sys.stdin.readline().strip()
Expand All @@ -37,8 +27,8 @@ def main():
gui_app.init_window("Spinner")

# Load textures
comma_texture = load_texture_resized(os.path.join(BASEDIR, "selfdrive/assets/img_spinner_comma.png"), TEXTURE_SIZE)
spinner_texture = load_texture_resized(os.path.join(BASEDIR, "selfdrive/assets/img_spinner_track.png"), TEXTURE_SIZE)
comma_texture = gui_app.load_texture_from_image(os.path.join(BASEDIR, "selfdrive/assets/img_spinner_comma.png"), TEXTURE_SIZE, TEXTURE_SIZE)
spinner_texture = gui_app.load_texture_from_image(os.path.join(BASEDIR, "selfdrive/assets/img_spinner_track.png"), TEXTURE_SIZE, TEXTURE_SIZE)

# Initial values
rotation = 0.0
Expand Down Expand Up @@ -81,10 +71,6 @@ def main():

rl.end_drawing()

# Clean up
rl.unload_texture(comma_texture)
rl.unload_texture(spinner_texture)


if __name__ == "__main__":
main()

0 comments on commit 3a302ae

Please sign in to comment.