Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
0d7156fc
Commit
0d7156fc
authored
Jul 28, 2015
by
Viktor Lidholt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #315 from vlidholt/master
Optimizations for sprite code
parents
b79a7313
b71d0dfb
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
32 deletions
+65
-32
node.dart
packages/flutter/example/game/lib/node.dart
+18
-16
sprite.dart
packages/flutter/example/game/lib/sprite.dart
+15
-11
sprite_box.dart
packages/flutter/example/game/lib/sprite_box.dart
+32
-5
No files found.
packages/flutter/example/game/lib/node.dart
View file @
0d7156fc
...
...
@@ -37,6 +37,7 @@ class Node {
int
_addedOrder
;
int
_childrenLastAddedOrder
=
0
;
bool
_childrenNeedSorting
=
false
;
Matrix4
_savedTotalMatrix
;
/// Decides if the node and its children is currently paused.
///
...
...
@@ -64,6 +65,7 @@ class Node {
ActionController
get
actions
{
if
(
_actions
==
null
)
{
_actions
=
new
ActionController
();
if
(
_spriteBox
!=
null
)
_spriteBox
.
_actionControllers
=
null
;
}
return
_actions
;
}
...
...
@@ -200,7 +202,7 @@ class Node {
child
.
_spriteBox
=
this
.
_spriteBox
;
_childrenLastAddedOrder
+=
1
;
child
.
_addedOrder
=
_childrenLastAddedOrder
;
if
(
_spriteBox
!=
null
)
_spriteBox
.
_
eventTargets
=
null
;
if
(
_spriteBox
!=
null
)
_spriteBox
.
_
registerNode
(
child
)
;
}
/// Removes a child from this node.
...
...
@@ -211,7 +213,7 @@ class Node {
if
(
_children
.
remove
(
child
))
{
child
.
_parent
=
null
;
child
.
_spriteBox
=
null
;
if
(
_spriteBox
!=
null
)
_spriteBox
.
_
eventTargets
=
null
;
if
(
_spriteBox
!=
null
)
_spriteBox
.
_
deregisterNode
(
child
)
;
}
}
...
...
@@ -233,7 +235,7 @@ class Node {
}
_children
=
[];
_childrenNeedSorting
=
false
;
if
(
_spriteBox
!=
null
)
_spriteBox
.
_
eventTargets
=
null
;
if
(
_spriteBox
!=
null
)
_spriteBox
.
_
deregisterNode
(
null
)
;
}
void
_sortChildren
()
{
...
...
@@ -414,7 +416,7 @@ class Node {
}
void
_prePaint
(
PaintingCanvas
canvas
)
{
canvas
.
save
();
_savedTotalMatrix
=
canvas
.
getTotalMatrix
();
// Get the transformation matrix and apply transform
canvas
.
concat
(
transformMatrix
.
storage
);
...
...
@@ -465,7 +467,7 @@ class Node {
}
void
_postPaint
(
PaintingCanvas
canvas
)
{
canvas
.
restore
(
);
canvas
.
setMatrix
(
_savedTotalMatrix
);
}
// Receiving update calls
...
...
packages/flutter/example/game/lib/sprite.dart
View file @
0d7156fc
...
...
@@ -29,6 +29,8 @@ class Sprite extends NodeWithSize {
/// mySprite.transferMode = TransferMode.plusMode;
TransferMode
transferMode
;
Paint
_cachedPaint
=
new
Paint
();
/// Creates a new sprite from the provided [texture].
///
/// var mySprite = new Sprite(myTexture)
...
...
@@ -65,7 +67,8 @@ class Sprite extends NodeWithSize {
}
void
paint
(
PaintingCanvas
canvas
)
{
canvas
.
save
();
// Store old matrix
Matrix4
savedMatrix
=
canvas
.
getTotalMatrix
();
// Account for pivot point
applyTransformForPivot
(
canvas
);
...
...
@@ -93,22 +96,23 @@ class Sprite extends NodeWithSize {
canvas
.
scale
(
scaleX
,
scaleY
);
// Setup paint object for opacity and transfer mode
Paint
paint
=
new
Paint
();
paint
.
color
=
new
Color
.
fromARGB
((
255.0
*
_opacity
).
toInt
(),
255
,
255
,
255
);
_cachedPaint
.
color
=
new
Color
.
fromARGB
((
255.0
*
_opacity
).
toInt
(),
255
,
255
,
255
);
if
(
colorOverlay
!=
null
)
{
p
aint
.
setColorFilter
(
new
ColorFilter
.
mode
(
colorOverlay
,
TransferMode
.
srcATop
));
_cachedP
aint
.
setColorFilter
(
new
ColorFilter
.
mode
(
colorOverlay
,
TransferMode
.
srcATop
));
}
if
(
transferMode
!=
null
)
{
p
aint
.
setTransferMode
(
transferMode
);
_cachedP
aint
.
setTransferMode
(
transferMode
);
}
// Do actual drawing of the sprite
texture
.
drawTexture
(
canvas
,
Point
.
origin
,
p
aint
);
texture
.
drawTexture
(
canvas
,
Point
.
origin
,
_cachedP
aint
);
}
else
{
// Paint a red square for missing texture
canvas
.
drawRect
(
new
Rect
.
fromLTRB
(
0.0
,
0.0
,
size
.
width
,
size
.
height
),
new
Paint
()..
color
=
const
Color
.
fromARGB
(
255
,
255
,
0
,
0
));
new
Paint
()..
color
=
new
Color
.
fromARGB
(
255
,
255
,
0
,
0
));
}
canvas
.
restore
();
// Restore matrix
canvas
.
setMatrix
(
savedMatrix
);
}
}
packages/flutter/example/game/lib/sprite_box.dart
View file @
0d7156fc
...
...
@@ -71,6 +71,8 @@ class SpriteBox extends RenderBox {
List
<
Node
>
_eventTargets
;
List
<
ActionController
>
_actionControllers
;
Rect
_visibleArea
;
Rect
get
visibleArea
{
...
...
@@ -131,6 +133,18 @@ class SpriteBox extends RenderBox {
_callSpriteBoxPerformedLayout
(
_rootNode
);
}
// Adding and removing nodes
_registerNode
(
Node
node
)
{
_actionControllers
=
null
;
_eventTargets
=
null
;
}
_deregisterNode
(
Node
node
)
{
_actionControllers
=
null
;
_eventTargets
=
null
;
}
// Event handling
void
_addEventTargets
(
Node
node
,
List
<
Node
>
eventTargets
)
{
...
...
@@ -342,19 +356,32 @@ class SpriteBox extends RenderBox {
// if (_numFrames % 60 == 0)
// print("delta: $delta fps: $_frameRate");
_runActions
(
_rootNode
,
delta
);
_runActions
(
delta
);
_callUpdate
(
_rootNode
,
delta
);
// Schedule next update
_scheduleTick
();
// Make sure the node graph is redrawn
markNeedsPaint
();
}
void
_runActions
(
Node
node
,
double
dt
)
{
if
(
node
.
_actions
!=
null
)
{
node
.
_actions
.
step
(
dt
);
void
_runActions
(
double
dt
)
{
if
(
_actionControllers
==
null
)
{
_actionControllers
=
[];
_addActionControllers
(
_rootNode
,
_actionControllers
);
}
for
(
ActionController
actions
in
_actionControllers
)
{
actions
.
step
(
dt
);
}
}
void
_addActionControllers
(
Node
node
,
List
<
ActionController
>
controllers
)
{
if
(
node
.
_actions
!=
null
)
controllers
.
add
(
node
.
_actions
);
for
(
int
i
=
node
.
children
.
length
-
1
;
i
>=
0
;
i
--)
{
Node
child
=
node
.
children
[
i
];
_
runActions
(
child
,
dt
);
_
addActionControllers
(
child
,
controllers
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment