Skip to content

Commit

Permalink
Fixed #70
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmcwatters committed Feb 4, 2016
1 parent 5fed994 commit ab4b650
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 22 deletions.
46 changes: 30 additions & 16 deletions src/conf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,51 @@ local function loadConfig( c )
require( "engine.shared.convar" )
convar.readConfig()

local _INTERACTIVE = c.args[ "-dedicated" ] and
c.args[ "-interactive" ]
local _INTERACTIVE = c.args[ "-dedicated" ] and
c.args[ "-interactive" ]

local r_window_width = convar.getConfig( "r_window_width" )
local r_window_height = convar.getConfig( "r_window_height" )
local r_window_width = convar.getConfig( "r_window_width" )
local r_window_height = convar.getConfig( "r_window_height" )
local r_window_fullscreen = convar.getConfig( "r_window_fullscreen" )
local r_window_borderless = convar.getConfig( "r_window_borderless" )
local r_window_vsync = convar.getConfig( "r_window_vsync" )
local r_window_vsync = convar.getConfig( "r_window_vsync" )
if ( _INTERACTIVE ) then
c.window.width = 661
c.window.width = 661
elseif ( r_window_width ) then
c.window.width = tonumber( r_window_width )
c.window.width = tonumber( r_window_width )
end
if ( _INTERACTIVE ) then
c.window.height = 480
c.window.height = 480
elseif ( r_window_height ) then
c.window.height = tonumber( r_window_height )
c.window.height = tonumber( r_window_height )
end
if ( r_window_fullscreen ) then
c.window.fullscreen = tonumber( r_window_fullscreen ) ~= nil and
tonumber( r_window_fullscreen ) ~= 0
c.window.fullscreen = tonumber( r_window_fullscreen ) ~= nil and
tonumber( r_window_fullscreen ) ~= 0
end
if ( _INTERACTIVE ) then
-- c.window.borderless = true
-- c.window.borderless = true
elseif ( r_window_borderless ) then
c.window.borderless = tonumber( r_window_borderless ) ~= nil and
tonumber( r_window_borderless ) ~= 0
c.window.borderless = tonumber( r_window_borderless ) ~= nil and
tonumber( r_window_borderless ) ~= 0
end
if ( r_window_vsync ) then
c.window.vsync = tonumber( r_window_vsync ) ~= nil and
tonumber( r_window_vsync ) ~= 0
c.window.vsync = tonumber( r_window_vsync ) ~= nil and
tonumber( r_window_vsync ) ~= 0
end

c.sound = c.sound or {}
c.sound.volume = 1
c.sound.desktop = true

local snd_volume = convar.getConfig( "snd_volume" )
local snd_desktop = convar.getConfig( "snd_desktop" )
if ( snd_volume ) then
c.sound.volume = tonumber( snd_volume )
end
if ( snd_desktop ) then
c.sound.desktop = tonumber( snd_desktop ) ~= nil and
tonumber( snd_desktop ) ~= 0
end

_CONF = c
Expand Down
50 changes: 48 additions & 2 deletions src/engine/client/gui/optionsmenu/audiooptionspanel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,49 @@ class "audiooptionspanel" ( gui.frametabpanel )

function audiooptionspanel:audiooptionspanel()
gui.frametabpanel.frametabpanel( self, nil, "Audio Options Panel" )
local options = {}
self.options = options
local c = engine.getConfig()

local name = "Master Volume"
local label = gui.label( self, name, name )
local x = 36
local y = 36
label:setPos( x, y )
label:setFont( self:getScheme( "fontBold" ) )

name = "Master Volume Slider"
local masterVolume = gui.slider( self, name )
self.masterVolume = masterVolume
masterVolume:setMax( 1 )
masterVolume:setValue( c.sound.volume )
masterVolume.onValueChanged = function( slider, oldValue, newValue )
options.masterVolume = newValue
c.sound.volume = newValue
end
y = y + label:getHeight() + 9
masterVolume:setPos( x, y )

name = "Play Sound in Desktop"
local desktopSound = gui.checkbox( self, name, name )
self.desktopSound = desktopSound
options.desktopSound = c.sound.desktop
desktopSound:setChecked( c.sound.desktop )
desktopSound.onCheckedChanged = function( checkbox, checked )
options.desktopSound = checked
c.sound.desktop = checked
end
x = 2 * x + masterVolume:getWidth()
y = 36 + label:getHeight() + 9
desktopSound:setPos( x, y )
end

function audiooptionspanel:activate()
self:saveControlStates()
end

function audiooptionspanel:onOK()
self:updateSound()
end

function audiooptionspanel:onCancel()
Expand All @@ -24,13 +60,23 @@ end
audiooptionspanel.onApply = audiooptionspanel.onOK

function audiooptionspanel:saveControlStates()
local controls = {}
self.controls = controls
local controls = {}
self.controls = controls
controls.masterVolume = self.masterVolume:getValue()
controls.desktopSound = self.desktopSound:isChecked()
end

function audiooptionspanel:resetControlStates()
local controls = self.controls
self.masterVolume:setValue( controls.masterVolume )
self.desktopSound:setChecked( controls.desktopSound )
table.clear( controls )
end

function audiooptionspanel:updateSound()
local options = self.options
convar.setConvar( "snd_volume", options.masterVolume )
convar.setConvar( "snd_desktop", options.desktopSound and 1 or 0 )
end

gui.register( audiooptionspanel, "audiooptionspanel" )
30 changes: 26 additions & 4 deletions src/engine/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
--============================================================================--

-- These values are preserved during real-time scripting.
local _connected = engine and
engine.client and
engine.client.connected or false
local _connected = engine and
engine.client and
engine.client.connected or false
local f = engine and
engine.client and
engine.client.hasFocus() or true

require( "conf" )
require( "engine.client.bind" )
require( "engine.client.graphics" )
require( "engine.client.gui" )
require( "engine.client.sound" )
require( "engine.shared.hook" )
require( "engine.shared.network.payload" )

Expand All @@ -32,6 +36,7 @@ local pcall = pcall
local print = print
local require = require
local scheme = scheme
local sound = sound
local string = string
local table = table
local tostring = tostring
Expand Down Expand Up @@ -182,10 +187,25 @@ function draw()
end
end

local _focus = nil
local _focus = f

local updateDesktopSound = function( f )
local snd_desktop = convar.getConvar( "snd_desktop" )
if ( snd_desktop:getBoolean() ) then
return
end

if ( not f ) then
sound.setVolume( 0 )
else
local snd_volume = convar.getConvar( "snd_volume" )
sound.setVolume( snd_volume:getNumber() )
end
end

function focus( f )
_focus = f
updateDesktopSound( f )
end

local _getAverageDelta = timer.getAverageDelta
Expand Down Expand Up @@ -329,6 +349,8 @@ function load( arg )
graphics.initialize()
gui.initialize()

sound.setVolume( conf.sound.volume )

if ( _G._DEBUG ) then
convar.getConvar( "perf_draw_frame_rate" ):setValue( "1" )
convar.getConvar( "con_enable" ):setValue( "1" )
Expand Down
18 changes: 18 additions & 0 deletions src/engine/client/sound.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@
--
--============================================================================--

require( "engine.shared.hook" )

-- These values are preserved during real-time scripting.
local sounds = sound and sound.sounds or {}

local audio = love.audio

class( "sound" )

local function updateVolume( convar )
local volume = convar:getNumber()
sound.setVolume( volume )
end

local snd_volume = convar( "snd_volume", 1, 0, 1,
"Sets the master volume",
updateVolume )
local snd_desktop = convar( "snd_desktop", "1", nil, nil,
"Toggles playing sound from the desktop" )

function sound.reload( library )
if ( string.sub( library, 1, 7 ) ~= "sounds." ) then
return
Expand Down Expand Up @@ -76,6 +89,11 @@ function sound:getVolume()
end

function sound:setVolume( volume )
if ( type( self ) == "number" ) then
audio.setVolume( self ) -- volume
return
end

local filename = self:getFilename()
if ( sounds[ filename ] ) then
sounds[ filename ].sound:setVolume( volume )
Expand Down

0 comments on commit ab4b650

Please sign in to comment.