没有合适的资源?快使用搜索试试~ 我知道了~
GFXwithSDLPart3
需积分: 0 2 下载量 64 浏览量
2009-03-05
20:18:14
上传
评论
收藏 270KB PDF 举报
温馨提示
试读
12页
SDL入门教程 Lesson 3: Sprites
资源详情
资源评论
资源推荐
GFX with SDL
Lesson 3: Sprites
by Marius Andra
Hello and welcome to this yet-another-tutorial. This lesson will be about the
things that most 2D games use - sprites. In a way sprites are like objects. I'm
sure that at some point in your life you have seen or have played Super Mario or
some other similar 2D game. Think about all the enemies, blocks, coins, etc. that
you saw in that game. All these are called sprites. The main character (Mario) is
a sprite, the enemies are sprites, the coins are sprites, the tubes are sprites,
even these things that you can stand on are sprites. Everything that impacts the
game/player/monster in some way (eg: you can touch it) is basically a sprite. The
background is simply a background - you can't touch it in any way. Sometimes
some sprites are parts of the background (eg: a sun that is moving/rotating, but
can't affect you in any way). Let's call them passive sprites. And let's call the
sprites that can affect the player (and the player itself) active sprites. In our little
sprite demo that we will create today, we won't seperate active and passive
sprites. Altough the vikings could very well be active sprites and the sun a
passive sprite. Active sprites need to collide with each other. And if a collision
occurs, respond. That's all called collision detection and response. More on it in a
future lesson. Besides being active and passive, sprites can also be animated or
non-animated. And all sprites can have the possibility to change their x and y
location on the screen. Sprite animation doesn't only mean that a sprite moves
1
from point A to point B, but MOVES from point A to point B. Examples of sprite
animation can be: a ball rolling (rolling animation while changing the ball
position), somebody walking (animation of legs moving while moving the
position), a blinking traffic light, a lighthouse, etc, etc.
The sprite system that we will build today will consist of 2 parts: the sprite base,
that we will use to store all the frames of the animation, and the sprite itself. The
sprite itself is the thingy that moves on the screen. The reason why we have 2
classes in our sprite system is really simple. Usually in a game you have many
sprites that look the same. In our sprite classes we will load all the animation
data (frames) into the sprite base and then each individual sprite will access it
from there. If we would load in all the data (images) with each sprite, then that
would boost up the memory requirements a lot.
(Our) sprites can also be partly transparent. The sprite system that we will be
using will (currently) only support RGB transparency. That means that some part
of the sprite (animation frame image), that is of a crentain color (like full green for
example), will be transparent. RGB transparency in SDL is REALLY easy.
This little statement will make some RGB value equal transparency:
SDL_SetColorKey(surface, SDL_SRCCOLORKEY,
SDL_MapRGB(surface->format, r, g, b));
Where surface is the surface you want to modify and r, g and b are the r, g, b of
RGB. Sometimes you may want to remove the transparency after setting it. To
do that simply replace SDL_SRCCOLORKEY with 0.
So, let's get coding the sprite classes.
Our 2 sprite classes will also have one helper structure called CSpriteFrame.
CSpriteFrame is used to represent each frame of the animation. It contains one
SDL_Structure for the image of the frame and one integer pause, that says
how many milliseconds to pause after drawing the picture.
struct CSpriteFrame
{
SDL_Surface *image;
int pause;
};
So let's start with the sprite base class. It's called CSpriteBase. CSpriteBase
has one function, init() to initialize the class - load in all the frames.
CSpriteBase also has one pointer to the structure of type CSpriteFrame that
we use to store the animation and some more variables that say whether the
class is built (via init()), the number of frames in the animation and the width
and height of the animation.
2
class CSpriteBase
{
public:
int init(char *dir);
CSpriteFrame *mAnim;
int mBuilt, mNumframes, mW, mH;
};
The init function is the core of the CSpriteBase class. init() takes one
parameter - an array of characters to the directory to get the frame animation
from. init() then looks for the file "info" from that directory and reads in some
stuff. All the frame images should also be in that same directory that was passed
to init.
Ok, now in init we first define 3 char arrays that we will later use to temporarily
store some stuff. We will also have 4 (temporary) integers: pause, r, g and b
and a FILE pointer fp.
int CSpriteBase::init(char *dir)
{
char buffer[255];
char filename[255];
char name[255];
int pause=0, r=0, g=0, b=0;
FILE *fp;
Now we use the sprintf function. sprintf() is just like printf(), only it
prints the output into a string, not the screen. We print the full location of the info
file into the string filename and then try to open it.
sprintf(filename, "%s/info", dir);
if((fp=fopen(filename, "r")) == NULL)
{
printf("ERROR opening file %s\n\n", filename);
return -1;
}
We'll then grab one line from the file into the string buffer. Since we know that
this is the first line of the file and that it starts with "FILES: nr", where nr is the
number of frames in the animation, we can then get the number using sscanf.
sscanf is just like scanf, only it gets the input from a string. We'll store the
number of frames in the class member mNumframes. And after all that we'll
allocate space for an array of CSpriteFrames.
fgets( ffer, 55, fp); bu 2
sscanf(buffer, "FILES: %d", &mNumframes);
mAnim = new CSpriteFrame[mNumframes];
3
剩余11页未读,继续阅读
renhaihang
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0