;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of The Last Fungus: An Amstrad CPC Game 
;;  Copyright (C) 2021 Miguel Perez Tarruella / Antonio Jose Fernandez Belliure / 
;;                     David Martinez Garcia / Ocacho Games (@OcachoGames)
;;
;;  This program is free software: you can redistribute it and/or modify
;;  it under the terms of the GNU Lesser General Public License as published by
;;  the Free Software Foundation, either version 3 of the License, or
;;  (at your option) any later version.
;;
;;  This program is distributed in the hope that it will be useful,
;;  but WITHOUT ANY WARRANTY; without even the implied warranty of
;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;  GNU Lesser General Public License for more details.
;;
;;  You should have received a copy of the GNU Lesser General Public License
;;  along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;------------------------------------------------------------------------------

;;====================================================================
;; GLOBL INCLUDES
;;====================================================================
.include "music.h.s"
.include "cpctelera.h.s"

;;Global
.include "global/global.h.s"

;;Macros
.include "macros/math.h.s"

;;Managers
.include "man/interruptions.h.s"

;;Songs
.globl _main_menu_song
.globl _game_song

;;====================================================================
;; PRIVATE VARIABLES
;;====================================================================

m_current_volume_menu:      .db #16      ;;Current volume of the song [16 = No volume] [0 = Max volume]
m_loop_volume_index_menu:   .db #40

m_current_volume_game:      .db #16      ;;Current volume of the song [16 = No volume] [0 = Max volume]
m_loop_volume_index_game:   .db #5

;;====================================================================
;; FUNCTIONS 
;;====================================================================
sys_music_menu_init::

    call cpct_akp_stop_asm

    ld de, #_main_menu_song
    call cpct_akp_musicInit_asm

    ld a, #16
    ld (#m_current_volume_menu), a
    ld a, (#m_current_volume_menu)
    call cpct_akp_setFadeVolume_asm

ret

sys_music_game_init::

    ld de, #_game_song
    call cpct_akp_musicInit_asm
    ld a, #16
    call cpct_akp_setFadeVolume_asm

	SET_VARIABLE_VALUE m_current_volume_game, #16 
    SET_VARIABLE_VALUE m_loop_volume_index_game, #5

ret

sys_music_menu_update::
    ld a, (#current_interruption)
    IS_EQUALS 1
    ret nz 
    ;;In case our volume is the highest, ret
    ld a, (#m_current_volume_menu)
    IS_EQUALS 6
    ret z

	DECREMENT_VARIABLE m_loop_volume_index_menu, 1
	
	ld a, (#m_loop_volume_index_menu)
	IS_EQUALS 0
	ret nz

	;;should_increment_volume:
	    SET_VARIABLE_VALUE m_loop_volume_index_menu, 40
	    ld a, (#m_current_volume_menu)
	    dec a
	    ld (#m_current_volume_menu), a

	    call cpct_akp_setFadeVolume_asm
ret

sys_music_game_update::
    ;;In case our volume is the highest, ret
    ld a, (#m_current_volume_game)
    IS_EQUALS 6  
    ret z

	DECREMENT_VARIABLE m_loop_volume_index_game, 1
	
	ld a, (#m_loop_volume_index_game)
	IS_EQUALS 0
	ret nz

	;;should_increment_volume:
	    SET_VARIABLE_VALUE m_loop_volume_index_game, 5
	    ld a, (#m_current_volume_game)
	    dec a
	    ld (#m_current_volume_game), a

	    call cpct_akp_setFadeVolume_asm
ret