/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27

« back to all changes in this revision

Viewing changes to zoetrope/core/app.lua

  • Committer: Josh C
  • Date: 2013-08-25 02:26:10 UTC
  • Revision ID: josh@9ix.org-20130825022610-13j6ydir3gmihiwz
.bzrignore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-- Class: App 
 
2
-- An app is where all the magic happens. :) It contains a 
1
3
-- view, the group where all major action happens, as well as the
2
4
-- meta view, which persists across views. Only one app may run at
3
5
-- a time.
7
7
-- An app's job is to get things up and running -- most of its logic
8
8
-- lives in its onRun handler, but for a simple app, you can also
9
9
-- use the onUpdate handler instead of writing a custom <View>.
10
 
--
 
10
-- 
11
11
-- Once an app has begun running, it may be accessed globally via
12
12
-- <the>.app.
13
13
--
28
28
        -- Property: name
29
29
        -- This is shown in the window title bar.
30
30
        name = 'Zoetrope',
31
 
 
 
31
        
32
32
        -- Property: icon
33
33
        -- A path to an image to use as the window icon (a 32x32 PNG is recommended).
34
 
        -- This doesn't affect the actual executable's icon in the taskbar or dock.
 
34
        -- This doesn't affect the actual executable's icon in the taskbar or dock. 
35
35
 
36
36
        -- Property: fps
37
37
        -- Maximum frames per second requested. In practice, your
38
38
        -- FPS may vary from frame to frame. Every event handler (e.g. onUpdate)
39
39
        -- is passed the exact elapsed time in seconds.
40
40
        fps = 60,
41
 
 
 
41
        
42
42
        -- Property: timeScale
43
43
        -- Multiplier for elapsed time; 1.0 is normal, 0 is completely frozen.
44
44
        timeScale = 1,
45
 
 
 
45
        
46
46
        -- Property: active
47
47
        -- If false, nothing receives update-related events, including the meta view.
48
48
        -- These events specifically are onStartFrame, onUpdate, and onEndFrame.
52
52
        -- Should the app automatically set its active property to false when its
53
53
        -- window loses focus?
54
54
        deactivateOnBlur = true,
55
 
 
 
55
        
56
56
        -- Property: view
57
57
        -- The current <View>. When the app is running, this is also accessible
58
58
        -- globally via <the>.view. In order to switch views, you must set this
99
99
                obj = self:extend(obj)
100
100
 
101
101
                -- set icon if possible
102
 
 
 
102
                
103
103
                if self.icon then
104
 
                        love.window.setIcon(Cached:image(self.icon))
 
104
                        love.graphics.setIcon(Cached:image(self.icon))
105
105
                end
106
 
 
 
106
        
107
107
                -- view containers
108
108
 
109
 
                obj.meta = obj.meta or View:new()
110
 
                obj.view = obj.view or View:new()
111
 
 
 
109
                obj.meta = obj.meta or Group:new()
 
110
                obj.view = obj.view or View:new()               
 
111
                
112
112
                -- input
113
113
 
114
114
                if love.keyboard then
136
136
 
137
137
                -- screen dimensions and state
138
138
 
139
 
                obj.width, obj.height, obj.windowflags = love.window.getMode()
140
 
                obj.fullscreen = obj.windowflags.fullscreen
 
139
                obj.width, obj.height, obj.fullscreen = love.graphics.getMode()
141
140
 
142
141
                -- housekeeping
143
 
 
 
142
                
144
143
                the.app = obj
145
144
                if obj.onNew then obj:onNew() end
146
145
                return obj
151
150
        --
152
151
        -- Arguments:
153
152
        --              none
154
 
        --
 
153
        -- 
155
154
        -- Returns:
156
155
        --              nothing
157
156
 
165
164
                -- attach debug console
166
165
 
167
166
                if DEBUG then
168
 
                        debugger.init()
 
167
                        self.console = DebugConsole:new()
 
168
                        self.meta:add(self.console)
169
169
                end
170
170
 
171
171
                -- set up callbacks
172
 
 
173
 
                love.window.setTitle(self.name)
 
172
                
 
173
                love.graphics.setCaption(self.name)
174
174
                love.update = function (elapsed) self:update(elapsed) end
175
175
                love.draw = function() self:draw() end
176
 
                love.focus = function (value) self:onFocus(value) end
 
176
                love.focus = function (value) self:onFocus(value) end   
177
177
 
178
178
                if self.onRun then self:onRun() end
179
 
                self._nextFrameTime = love.timer.getTime()
 
179
                self._nextFrameTime = love.timer.getMicroTime()
180
180
        end,
181
 
 
 
181
        
182
182
        -- Method: quit
183
183
        -- Quits the application immediately.
184
184
        --
200
200
        --
201
201
        -- Returns:
202
202
        --              nothing
203
 
 
 
203
        
204
204
        useSysCursor = function (self, value)
205
205
                if STRICT then
206
206
                        assert(value == true or value == false,
228
228
                        assert(not self.fullscreen, 'asked to enter fullscreen when already in fullscreen')
229
229
                end
230
230
 
231
 
                local modes = love.window.getFullscreenModes()
 
231
                local modes = love.graphics.getModes()
232
232
 
233
233
                if not hint then
234
234
                        if self.width * 9 == self.height * 16 then
246
246
                for _, mode in pairs(modes) do
247
247
                        mode.area = mode.width * mode.height
248
248
 
249
 
                        if (mode.area > bestMode.area) and
 
249
                        if (mode.area > bestMode.area) and 
250
250
                           ((hint == 'letterbox' and mode.width == self.width) or
251
251
                            (hint == 'pillar' and mode.height == self.height)) then
252
252
                                        bestMode = mode
256
256
                -- if we found a match, switch to it
257
257
 
258
258
                if bestMode.width then
259
 
                        love.window.setMode(bestMode.width, bestMode.height, {fullscreen = true})
 
259
                        love.graphics.setMode(bestMode.width, bestMode.height, true)
260
260
                        self.fullscreen = true
261
261
 
262
262
                        -- and adjust inset and scissor
284
284
                if STRICT then
285
285
                        assert(self.fullscreen, 'asked to exit fullscreen when already out of fullscreen')
286
286
                end
287
 
 
288
 
                love.window.setMode(self.width, self.height, {fullscreen = false})
 
287
        
 
288
                love.graphics.setMode(self.width, self.height, false)
289
289
                love.graphics.setScissor(0, 0, self.width, self.height)
290
290
                self.fullscreen = false
291
291
                self.inset.x = 0
335
335
        --
336
336
        -- Arguments:
337
337
        --              sprite - sprite to add
338
 
        --
 
338
        -- 
339
339
        -- Returns:
340
 
        --              sprite added
 
340
        --              nothing
341
341
 
342
342
        add = function (self, sprite)
343
 
                return self.view:add(sprite)
 
343
                self.view:add(sprite)
344
344
        end,
345
345
 
346
346
        -- Method: remove
365
365
                elapsed = elapsed * self.timeScale
366
366
 
367
367
                -- sync the.view with our current view
368
 
 
 
368
                
369
369
                local view = self.view
370
370
                if the.view ~= view then the.view = view end
371
371
 
381
381
 
382
382
                -- update everyone
383
383
                -- all update events bubble up from child to parent
384
 
                -- (we consider the meta view a little
 
384
                -- (we consider the meta view a little 
385
385
 
386
386
                view:startFrame(elapsed)
387
387
                self.meta:startFrame(elapsed)
388
388
                if self.onStartFrame then self:onStartFrame(elapsed) end
389
 
 
390
 
                view:update(elapsed)
 
389
                
 
390
                view:update(elapsed)    
391
391
                self.meta:update(elapsed)
392
392
                if self.onUpdate then self:onUpdate(elapsed) end
393
393
 
395
395
                self.meta:endFrame(elapsed)
396
396
                if self.onEndFrame then self:onEndFrame(elapsed) end
397
397
        end,
398
 
 
 
398
        
399
399
        draw = function (self)
400
 
drawStart = love.timer.getTime()
 
400
drawStart = love.timer.getMicroTime()
401
401
                local inset = self.inset.x ~= 0 or self.inset.y ~= 0
402
402
 
403
403
                if inset then love.graphics.translate(self.inset.x, self.inset.y) end
407
407
 
408
408
                -- sleep off any unneeded time to keep up at our FPS
409
409
 
410
 
                local now = love.timer.getTime()
 
410
                local now = love.timer.getMicroTime()
411
411
the.drawTook = now - drawStart
412
412
                if self._nextFrameTime < now then
413
413
                        self._nextFrameTime = now
437
437
                        result = result .. 'inactive'
438
438
                end
439
439
 
440
 
                return result .. ', ' .. tostring(self.fps) .. ' fps, ' .. self.view:count(true) .. ' sprites)'
 
440
                return result .. ', ' .. self.fps .. ' fps, ' .. self.view:count(true) .. ' sprites)'
441
441
        end
442
442
}