can we make window icon dynamic?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Li yuanlin
Regular
Posts: 94
Joined: Sat Aug 04, 2018 8:42 pm
Location: Hong Kong
Contact:

can we make window icon dynamic?

#1 Post by Li yuanlin »

Such as when game is playing music changes icon into another one.

define config.window_icon = "gui/window_icon.png"
stay hungry,stay foolish.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2410
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: can we make window icon dynamic?

#2 Post by Ocelot »

In Pygame you can do simply pygame.display.set_icon(your_icon), and RenPy's version of pygame can be imported into renpy script, but I am not sure how to most efficiently get pygame surface to use from within renpy.
< < insert Rick Cook quote here > >

User avatar
Li yuanlin
Regular
Posts: 94
Joined: Sat Aug 04, 2018 8:42 pm
Location: Hong Kong
Contact:

Re: can we make window icon dynamic?

#3 Post by Li yuanlin »

anyone else knows how to change configs in game?
stay hungry,stay foolish.

jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: can we make window icon dynamic?

#4 Post by jeffster »

Li yuanlin wrote: Sat Mar 30, 2024 7:55 am anyone else knows how to change configs in game?
The documentation says:
Ren'Py's implementation makes the assumption that, once the GUI system has initialized, configuration variables will not change. Changing configuration variables outside of init blocks can lead to undefined behavior.
https://renpy.org/doc/html/config.html

In other words, config variables can't be changed during the game.

To change the game (window) icon, you would need workarounds, like using Pygame or SDL:
http://web.archive.org/web/202311131848 ... w.set_icon

User avatar
Andredron
Miko-Class Veteran
Posts: 721
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: can we make window icon dynamic?

#5 Post by Andredron »

viewtopic.php?p=45633#p45633

In Ren'Py, you can change the window icon at runtime using the renpy.display.set_window_icon() function. However, you must import the appropriate module before doing so.

Code: Select all

init python:
    import pygame
    from renpy.display import core

    # Load the image for the new icon
    icon_surface = pygame.image.load("path/to/your/icon.png")

    # Convert the image to a format suitable for the window icon
    icon_surface = pygame.transform.smoothscale(icon_surface, (64, 64))
    icon_surface = pygame.display.set_icon(icon_surface)

    # Create an instance of CoreMappedSurface from the processed image
    icon_mapped_surface = core.CoreMappedSurface(icon_surface.get_width(), icon_surface.get_height())
    icon_mapped_surface.blit_2d(icon_surface, (0, 0))

    # Save the new icon for later use
    custom_window_icon = icon_mapped_surface

# Function to change the window icon
def set_custom_window_icon():
    renpy.display.set_window_icon(custom_window_icon)

# Function to play music and change the window icon
def play_music_and_set_icon():
    renpy.music.play("music/your_music_track.ogg")
    set_custom_window_icon()

Then, you can use play_music_and_set_icon() to play music and change the window icon:

Code: Select all

label start:
    $ renpy.call_screen("main_menu")
    $ renpy.play_music_and_set_icon()
But this is roughly, since I'm writing from my phone and there is no possibility to check it on PC


ChatGPT That's what I suggested

Code: Select all


init python:
    import renpy.display

# Define the default window icon
define config.window_icon = "gui/window_icon.png"

# Define the music window icon
$ renpy.display.music_window_icon = renpy.image.Image("gui/music_window_icon.png")

# Function to change window icon when music is playing
$ renpy.music_playing = False

$ renpy.set_music_icon = False

def change_window_icon():
    if renpy.music_playing and not renpy.set_music_icon:
        renpy.display.set_window_icon(renpy.display.music_window_icon)
        renpy.set_music_icon = True
    elif not renpy.music_playing and renpy.set_music_icon:
        renpy.display.set_window_icon(config.window_icon)
        renpy.set_music_icon = False

# Call the function every 0.5 seconds to check if music is playing
$ renpy.call_repeatedly(0.5, change_window_icon)

# Function to play music and change the flag
def play_music(music_file):
    renpy.music_playing = True
    renpy.music.play(music_file)

# Function to stop music and change the flag
def stop_music():
    renpy.music_playing = False
    renpy.music.stop()


jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: can we make window icon dynamic?

#6 Post by jeffster »

Andredron wrote: Sun Mar 31, 2024 3:31 am In Ren'Py, you can change the window icon at runtime using the renpy.display.set_window_icon() function. However, you must import the appropriate module before doing so.

Code: Select all

init python:
    import pygame
    #...
That code doesn't really work. You can check how Ren'Py function set_icon() works, in
renpy/display/core.py
https://github.com/renpy/renpy/blob/f88 ... e.py#L1112

Basically it makes sure that the icon image is square an not too large, and then sets the window icon. If you take only the most relevant part of the code (provided your icon images are square and small enough), you can change icons this way:

Code: Select all

define icon1_file = "img/dice.png"
define icon2_file = "img/bug.png"

init python:
    import pygame
    try:
        with renpy.loader.load(icon1_file, directory="images") as f:
            icon1 = renpy.display.scale.image_load_unscaled(f, icon1_file)

        with renpy.loader.load(icon2_file, directory="images") as f:
            icon2 = renpy.display.scale.image_load_unscaled(f, icon2_file)

    except renpy.webloader.DownloadNeeded:
        pass

    def change_icon(a):
        try:
            pygame.display.set_icon(a)
        except:
            pass

label start:
    "Icon 1"
    $ change_icon(icon1)
    "Icon 2"
    $ change_icon(icon2)
    jump start

User avatar
Andredron
Miko-Class Veteran
Posts: 721
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: can we make window icon dynamic?

#7 Post by Andredron »

jeffster wrote: Sun Mar 31, 2024 7:03 am
Andredron wrote: Sun Mar 31, 2024 3:31 am In Ren'Py, you can change the window icon at runtime using the renpy.display.set_window_icon() function. However, you must import the appropriate module before doing so.

Code: Select all

init python:
    import pygame
    #...
That code doesn't really work. You can check how Ren'Py function set_icon() works, in
renpy/display/core.py
https://github.com/renpy/renpy/blob/f88 ... e.py#L1112

Basically it makes sure that the icon image is square an not too large, and then sets the window icon. If you take only the most relevant part of the code (provided your icon images are square and small enough), you can change icons this way:

Code: Select all


init python:
    import pygame
    try:
        with renpy.loader.load(icon1_file, directory="images") as f:
            icon1 = renpy.display.scale.image_load_unscaled(f, icon1_file)

        with renpy.loader.load(icon2_file, directory="images") as f:
            icon2 = renpy.display.scale.image_load_unscaled(f, icon2_file)

    except renpy.webloader.DownloadNeeded:
        pass

    def change_icon(a):
        try:
            pygame.display.set_icon(a)
        except:
            pass

label start:
    "Icon 1"
    $ change_icon(icon1)
    "Icon 2"
    $ change_icon(icon2)
    jump start

The best! It remains to write a function that would switch the icon and screen with a button that when pressed would switch the icon and screen

Code: Select all

# Define paths to icon files
define icon1_file = "img/dice.png" # Path to the first icon file
define icon2_file = "img/bug.png" # Path to the second icon file

# Initialize and load icons
init python:
    import pygame # Import the pygame library

    # Load the icons with renpy.load_image
    try:
        icon1 = renpy.load_image(icon1_file)
        icon2 = renpy.load_image(icon2_file)
    except Exception as e:
        print(f "Error loading icons: {e}") # Print an error message if icons are not loaded

    # Function for changing the window icon
    def change_icon(a):
        Try:
            # Set the new icon using pygame.display.set_icon
            pygame.display.set_icon(a.surface)
        except Exception as e:
            print(f "Error changing icon: {e}") # Print an error message if the icon is not changed

# Define the ImageButton screen
screen my_screen():
    # Define an image button
    imagebutton:
        idle "button/idle.png" # Image of button in inactive state
        hover "button/hover.png" # Image of the button when hovering the cursor.

        # Action when the button is clicked
        action Function(change_icon, icon1) # Change icon to icon1 when button is clicked

# Define a screen with ImageButton and icon switching
screen my_screen_toggle():
    $ _icon = icon1 # Initial icon value

    # Define an image button
      imagebutton:
        idle "button/idle.png" # Image of the button in the inactive state
        hover "button/hover.png" # Image of the button when hovering the cursor.

        # Action when the button is clicked
        action Function(toggle_icon) # Switch the icon when the button is clicked.

# Function to toggle the icon
init python:
    def toggle_icon():
        global _icon # Declare the global variable _icon

        # Switch the icon between icon1 and icon2
        if _icon == icon1:
            _icon = icon2
        else:
            _icon = icon1

        # Call the change_icon function to change the icon
        change_icon(_icon)

label start:
    # Set icon1 icon
    "Icon 1"
    $ change_icon(icon1)

    # Show screen with ImageButton and icon change when clicked
    show screen my_screen

    # Set icon2 icon
    "Icon 2"
    $ change_icon(icon2)

    # Show screen with ImageButton and icon switching
    show screen my_screen_toggle

    # Jump to the start label
    jump start


Post Reply

Who is online

Users browsing this forum: BBN_VN, Bing [Bot], Majestic-12 [Bot]