Skip to content

Commit dd4d7b6

Browse files
Merge pull request #50 from ricardoquesada/general_fixes
fixes
2 parents b3573e1 + 510a801 commit dd4d7b6

File tree

4 files changed

+231
-133
lines changed

4 files changed

+231
-133
lines changed

13.md

+33-22
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
# Chapter 13: Audio
22

3-
Your game will surely need sound! Cocos2d-x provides an audio engine so wasy we
4-
call it `SimpleAudioEngine`. `SimpleAudioEngine` can be used to play background
5-
music as well as sound effects through out your game play. `SimpleAudioEngine` is
6-
a shared singleton object so you can simple call it fromanywhere in your code. When
7-
creating a sample `HelloWorld` project we do all the setup required for you, out of
8-
the box. `SimpleAudioEgnine` support a number of formats, including *MP3* and
3+
Your game will surely need sound! Cocos2d-x provides an audio engine so ways we
4+
call it `SimpleAudioEngine`. `SimpleAudioEngine` can be used to play background
5+
music as well as sound effects through out your game play. `SimpleAudioEngine` is
6+
a shared singleton object so you can simple call it from anywhere in your code. When
7+
creating a sample `HelloWorld` project we do all the setup required for you, out of
8+
the box. `SimpleAudioEgnine` support a number of formats, including *MP3* and
99
*Core Audio Format*
1010

1111
## Getting Started
12-
There are considerations to keep in mind when using audio in your game. Mostly
13-
when operating on mobile devices such as phones and tablets. What happens when
14-
you multi-task on your phone and are switching bewteen apps? Or when a phone
15-
call comes in? You need to handle these exceptions in your game. Fortunately we
12+
13+
FIXME: this is not "getting started". Before this I just say: This is how to play a sound,
14+
this is how to play a music, and stop
15+
16+
FIXME: And an "advanced" section I will mention the setup, multi-tasking etc.
17+
18+
There are considerations to keep in mind when using audio in your game. Mostly
19+
when operating on mobile devices such as phones and tablets. What happens when
20+
you multi-task on your phone and are switching between apps? Or when a phone
21+
call comes in? You need to handle these exceptions in your game. Fortunately we
1622
help you here.
1723

1824
In `AppDelegate.cpp`, notice the following methods:
@@ -35,32 +41,37 @@ void AppDelegate::applicationWillEnterForeground() {
3541
}
3642
```
3743

38-
Notice the commented out lines for `SimpleAudioEngine` make sure to
39-
uncomment these if you are using `SimpleAudioEngine` for background sounds
44+
Notice the commented out lines for `SimpleAudioEngine` make sure to
45+
uncomment these if you are using `SimpleAudioEngine` for background sounds
4046
and sound effects.
4147

4248
## A simple to use API
4349
The API for `SimpleAudioEngine` is, of course, simple!
4450

4551
### Play background music
46-
Play an audio file for use as background music. This can be repeated
52+
Play an audio file for use as background music. This can be repeated
4753
continuously.
54+
55+
FIXE: Don't call getInstance() twice
56+
4857
```cpp
49-
// set the background music and continusously play it.
50-
SimpleAudioEngine::getInstance()->playBackgroundMusic("mymusic.mp3", true);
58+
auto audio = SimpleAudioEngine::getInstance();
59+
60+
// set the background music and continuously play it.
61+
audio->playBackgroundMusic("mymusic.mp3", true);
5162

5263
// set the background music and play it just once.
53-
SimpleAudioEngine::getInstance()->playBackgroundMusic("mymusic.mp3", false);
64+
audio->playBackgroundMusic("mymusic.mp3", false);
5465
```
5566
### Play a sound effect.
56-
Play a sound effect.
67+
Play a sound effect.
5768
```cpp
5869
// play a sound effect, just once.
5970
SimpleAudioEngine::getInstance()->playEffect("myEffect.mp3", false, 1.0f, 1.0f, 1.0f);
6071
```
6172

62-
### Pausing, stopping, resuming music and sound effects
63-
After you start to play music and sound effects you might need to pause,
73+
### Pausing, stopping, resuming music and sound effects
74+
After you start to play music and sound effects you might need to pause,
6475
stop or resume after certain operations. This can be done easily.
6576
```cpp
6677
// pause background music.
@@ -94,15 +105,15 @@ SimpleAudioEngine::getInstance()->resumeAllEffects();
94105
```
95106

96107
### Advanced audio functionality
97-
There is more to the API that you can take advantage of. Let's go ahead
108+
There is more to the API that you can take advantage of. Let's go ahead
98109
and mention a few more items.
99110

100111
```cpp
101112
// setting the volume specifying value as a float
102113
SimpleAudioEngine::getInstance()->setEffectsVolume(5.0f);
103114

104115
// pre-loading background music and effects. You could pre-load
105-
// effects, perhaps on app startup so they are already loaded
116+
// effects, perhaps on app startup so they are already loaded
106117
// when you want to use them.
107118
SimpleAudioEngine::getInstance()->preloadBackgroundMusic("myMusic1.mp3");
108119
SimpleAudioEngine::getInstance()->preloadBackgroundMusic("myMusic2.mp3");
@@ -114,4 +125,4 @@ SimpleAudioEngine::getInstance()->preloadEffect("myEffect2.mp3");
114125
// you wont use it anymore in your game. unload it to free up
115126
// resources.
116127
SimpleAudioEngine::getInstance()->unloadEffect("myEffect1.mp3");
117-
```
128+
```

3.md

+77-39
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,80 @@
11
# Chapter 3: Sprites
22

33
## What are Sprites
4-
A Sprite is a 2D image that can be animated or transformed by changing the properties
5-
of it, including `rotation`, `position`, `scale`, `color`, etc.
4+
A Sprite is a 2D image that can be animated or transformed by changing its properties,
5+
including `rotation`, `position`, `scale`, `color`, etc.
66

77
## Creating Sprites
8-
There are different ways to create Sprites depending upon what you need to accomplish. You
9-
can create a `Sprite` from a number of graphic formats including: PNG, JPEG, TIFF. PVRv2,
10-
PVRv2, ETC1 and others. Let's go through some create methods and talk about each one.
8+
There are different ways to create Sprites depending upon what you need to accomplish. You
9+
can create a `Sprite` from a number of graphic formats including: PNG, JPEG, TIFF, and others.
10+
Let's go through some create methods and talk about each one.
1111

1212
### Creating a Sprite
13-
A `Sprite` can be created by specifying a specific image file to use.
13+
A `Sprite` can be created by specifying an image file to use.
1414

1515
```cpp
1616
auto mySprite = Sprite::create("mysprite.png");
1717
```
1818

1919
![](3/i1.png "")
20-
This creates a `Sprite` from 'mysprite.png'. The result is a `Sprite` of the graphic you
21-
loaded. It has the same width and height. If the file is 200x200 the resulting `Sprite` is
22-
the same size.
2320

24-
### Creating a Sprite From a Rect
25-
This creates a `Sprite` by specifying a file name and a Rect to use as the selection of
26-
what the sprites representation is. `setRect()` is the only way to resize a `Sprite`
21+
This creates a `Sprite` using the 'mysprite.png' image. The result is that the
22+
created `Sprite` uses the whole image. `Sprite` has the same dimensions
23+
of `mysprite.png`. If the image file is 200x200 the resulting `Sprite` is 200x200.
24+
25+
### Creating a Sprite with a Rect
26+
27+
In the previous example, the created `Sprite` has the same size as the original PNG.
28+
If you want to create a `Sprite` with only a certain portion of the image file, you can
29+
do it by specifying a `Rect`.
30+
31+
`Rect` has 4 values: origin x, origin y, width and height.
32+
2733
```cpp
2834
auto mySprite = Sprite::create("mysprite.png", Rect(0,0,40,40));
2935
```
36+
3037
![](3/i4.png "")
3138

39+
FIXME: Sprites in screenshot must have same position
40+
41+
FIXME: Explain that `Rect` uses the top-left origin
42+
43+
The result is that we are creating a `Sprite` with only a portion of the PNG.
44+
In this case, the `Sprite` size will be 40x40.
45+
46+
FIXME: Say that these 2 examples are equivalents for this particular case
47+
48+
```cpp
49+
auto mySprite = Sprite::create("mysprite.png");
50+
auto mySprite = Sprite::create("mysprite.png", Rect(0,0,200,200));
51+
```
52+
53+
3254
## Creating a Sprite from a Sprite Sheet
33-
A `sprite sheet` a way to combine sprites into a single texture while reducing
34-
the overall size compared to loading each `Sprite` individually. Why does this
35-
matter? When loading a `Sprite` each hardware platform has constraints of a
36-
minimum texture size that it will need to load. As you can imagine there is
37-
a lot of unused space and this increases memory consumption, draw times and
38-
lowers your games frame rate.
39-
40-
Using a `sprite sheet` allows all of your `Sprite` objects to be condensed filing
41-
unused space. Now only the entire spritesheet must meet the hardwares constraints
42-
and can be loaded all at the same time. This means you will significantly reduce
43-
memory usage, reduce the time it takes OpenGL to draw your sprites and keep the
44-
frame rate of your game high. Behind the scenes a sprite sheet is a combination
45-
of 2 files. The first is a file that describes each `Sprite` in the sprite sheet
46-
and its size in `Rect` format. The second is the actual graphic images compacted
47-
together fo create as small a file as possible.
48-
49-
When using a `sprite sheet`, it is first loaded, in it's entirety, into the
50-
`SpriteFrameCache`. `SpriteFrameCache` is a caching class that retains Sprites
51-
quicker future access. This prevents needing to load a `Sprite` multiple times,
52-
over and over. The `Sprite` is loaded once and reatined in the `SpriteCache`
55+
A _sprite sheet_ is a way to combine sprites into a single file. This reduces the
56+
overall file size compared to having individual files for each `Sprite`. This means
57+
you will significantly reduce memory usage, file size and loading time.
58+
59+
Also, using sprite sheets is a needed condition in order to achive better performance
60+
by _batching_ the _draw calls_. More on this in the Advanced Chapter.
61+
62+
When using a `sprite sheet`, it is first loaded, in its entirety, into the
63+
`SpriteFrameCache`. `SpriteFrameCache` is a caching class that retains `SpriteFrame` for
64+
future quicker access.
65+
66+
And a `SpriteFrame` is a object that contains the image file name and a `Rect` that
67+
specifies the size of the sprite.
68+
69+
The `SpriteFrameCache` prevents needing to load a `SpriteFrame` multiple times,
70+
over and over. The `SpriteFrame` is loaded once and retained in the `SpriteFrameCache`
5371

5472
Here is an example sprite sheet:
5573

74+
FIXME: Low priority. Show a sprite sheet that maximes the space.
75+
76+
FIXME: Horizontal spritesheet is better because it takes less space
77+
5678
![](3/3_1.png "example SpriteSheet")
5779

5880
Let's tie this all together!
@@ -62,30 +84,40 @@ Load your `sprite sheet` into the `SpriteFrameCache`, probably in `AppDelegate`:
6284
```cpp
6385
// load the Sprite Sheet
6486
auto spritecache = SpriteFrameCache::getInstance();
87+
88+
// the .pist file can be generated with any of the tools mentioned below
6589
spritecache->addSpriteFramesWithFile("sprites.plist");
6690
```
67-
Now that we have a `sprite sheet` loaded into `SpriteFrameCache` we can create `Sprite` objects
91+
Now that we have a `sprite sheet` loaded into `SpriteFrameCache` we can create `Sprite` objects
6892
by utilizing it.
6993
7094
### Creating a Sprite from SpriteFrameCache
71-
This creates a `Sprite` by pulling it from the `SpriteFrameCache`.
95+
This creates a `Sprite` by pulling it from the `SpriteFrameCache`.
7296
```cpp
7397
auto mysprite = Sprite::createWithSpriteFrameName("mysprite.png");
7498
```
99+
75100
![](3/i3.png "")
76101

77102
### Creating a Sprite from a SpriteFrame
78-
A `Sprite` can be created from a `SpriteFrame`. A `SpriteFrame` is a specified texture
79-
and defined by a specific size `Rect`
103+
104+
Another way to create the same `Sprite` is by fetching the `SpriteFrame` from the
105+
`SpriteFrameCache`, and then creating the `Sprite` with the `SpriteFrame`.
106+
Example:
107+
80108
```cpp
81-
auto newspriteFrame = SpriteFrame::create("Blue_Front1.png", Rect(0,0,50,50));
82-
109+
// this is equivalent to the previous example,
110+
// but you need to type more
111+
auto newspriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName("mysprite.png");
83112
auto newSprite = Sprite::createWithSpriteFrame(newspriteFrame);
84113
```
85114
![](3/i2.png "")
86115
87116
### Tools for creating Sprite Sheets
88117
118+
FIXME: add some text, like... creating spritesheet manually is a tedious work, fortunately
119+
there are tools that generates them automatically.
120+
89121
[Cocos Studio](http://www.cocos2d-x.org/wiki/CocoStudio)
90122
91123
[Texture Packer](https://www.codeandweb.com/texturepacker)
@@ -101,8 +133,14 @@ auto mySprite = Sprite::create("mysprite.png");
101133
```
102134
![](3/i1.png "")
103135

136+
FIXME: describe color and opacity then describe the properties first: position, rotation, scale, skew. And then
137+
describe in more detail what Anchor point is, the coordinate system is uses, etc.
138+
104139
### Scale
105140
Changes the *Sprites* scale, either by x, y or uniformly for both x and y.
141+
142+
FIXME: A scale of 2 will take less space for the screenshot
143+
106144
```cpp
107145
// increases X and Y size by 4.0 uniformly
108146
mySprite->setScale(4.0);
@@ -116,7 +154,7 @@ mySprite->setScaleY(4.0);
116154
![](3/i5.png "")
117155
118156
### Anchor Point and Position
119-
`Anchor Point` is a point that you set as a way of specifying what part of
157+
`Anchor Point` is a point that you set as a way of specifying what part of
120158
the `Sprite` will be used when setting the position of it.
121159
```cpp
122160
// bottom left

0 commit comments

Comments
 (0)