[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Pingus-CVS] r2679 - branches/pingus_sdl/src
From: |
grumbel at BerliOS |
Subject: |
[Pingus-CVS] r2679 - branches/pingus_sdl/src |
Date: |
Wed, 17 Jan 2007 21:15:01 +0100 |
Author: grumbel
Date: 2007-01-17 21:15:01 +0100 (Wed, 17 Jan 2007)
New Revision: 2679
Modified:
branches/pingus_sdl/src/pixel_buffer.cpp
branches/pingus_sdl/src/sprite.cpp
branches/pingus_sdl/src/sprite.hpp
Log:
- fixed sprite class, should now behave almost correctly
Modified: branches/pingus_sdl/src/pixel_buffer.cpp
===================================================================
--- branches/pingus_sdl/src/pixel_buffer.cpp 2007-01-17 13:59:02 UTC (rev
2678)
+++ branches/pingus_sdl/src/pixel_buffer.cpp 2007-01-17 20:15:01 UTC (rev
2679)
@@ -52,7 +52,7 @@
0x0000ff00,
0x00ff0000,
0xff000000);
- SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, 255, 0, 0, 255));
+ SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, 255, 255, 0, 255));
}
PixelBuffer::~PixelBuffer()
Modified: branches/pingus_sdl/src/sprite.cpp
===================================================================
--- branches/pingus_sdl/src/sprite.cpp 2007-01-17 13:59:02 UTC (rev 2678)
+++ branches/pingus_sdl/src/sprite.cpp 2007-01-17 20:15:01 UTC (rev 2679)
@@ -38,56 +38,66 @@
{
public:
SDL_Surface* surface;
- SpriteDescription desc;
- int frame;
- SpriteImpl(const SpriteDescription& desc_)
- : desc(desc_),
- frame(0)
- {
- load(desc.filename);
- }
+ Vector2i offset;
- SpriteImpl(const std::string& name)
- : frame(0)
- {
- std::ostringstream str;
- str << "data/images/" << name << ".png";
+ Vector2i frame_pos;
+ Size frame_size;
+ int frame_delay;
- load(str.str());
- }
+ Size array;
- SpriteImpl(const PixelBuffer& pixelbuffer)
- : frame(0)
+ /** Current frame */
+ int frame;
+ int tick_count;
+
+ SpriteImpl(const SpriteDescription& desc)
+ : surface(0),
+ frame(0),
+ tick_count(0)
{
- if (pixelbuffer.get_surface())
+ surface = IMG_Load(desc.filename.c_str());
+ if (surface)
{
- surface = SDL_DisplayFormat(pixelbuffer.get_surface());
+ //SDL_SetAlpha(surface, SDL_SRCALPHA, 128);
}
else
{
- surface = 0;
- std::cout << "XXX Surface empty" << std::endl;
+ std::cout << "Error: Couldn't load " << desc.filename << std::endl;
+ surface = IMG_Load("data/images/core/misc/404.png");
+ assert(surface);
}
+
+ frame_pos = desc.frame_pos;
+
+ frame_size.width = (desc.frame_size.width == -1) ? surface->w :
desc.frame_size.width;
+ frame_size.height = (desc.frame_size.height == -1) ? surface->h :
desc.frame_size.height;
+
+ frame_delay = desc.speed;
+
+ array = desc.array;
+
+ offset = calc_origin(desc.origin, frame_size) - desc.offset;
}
- void
- load(const std::string& filename)
+ SpriteImpl(const PixelBuffer& pixelbuffer)
+ : offset(0,0),
+ frame_pos(0,0),
+ frame_size(pixelbuffer.get_width(), pixelbuffer.get_height()),
+ frame_delay(0),
+ array(1,1),
+ frame(0),
+ tick_count(0)
{
- surface = IMG_Load(filename.c_str());
- if (!surface)
+ if (pixelbuffer.get_surface())
{
- std::cout << "Error: Couldn't load " << filename << std::endl;
- surface = IMG_Load("data/images/core/misc/404.png");
- assert(surface);
+ surface = SDL_DisplayFormat(pixelbuffer.get_surface());
}
else
{
- //std::cout << "Loaded sprite: " << filename << std::endl;
+ surface = 0;
+ std::cout << "Sprite: Error trying to create a Sprite out of an empty
PixelBuffer" << std::endl;
}
-
- desc.origin = origin_top_left;
- desc.offset = calc_origin(desc.origin, Size(surface->w, surface->h)) +
desc.offset;
}
~SpriteImpl()
@@ -95,24 +105,30 @@
SDL_FreeSurface(surface);
}
- void draw(float x, float y, SDL_Surface* target)
+ void update(float delta)
{
- SDL_Rect pos;
-
- pos.x = (Sint16)(x - desc.offset.x);
- pos.y = (Sint16)(y - desc.offset.y);
- pos.w = 0;
- pos.h = 0;
+ tick_count += int(delta * 1000.0f);
+ tick_count = tick_count % (frame_delay * (array.width * array.height));
+
+ frame = tick_count / frame_delay;
+ }
+ void draw(float x, float y, SDL_Surface* dst)
+ {
+ SDL_Rect dstrect;
+ dstrect.x = (Sint16)(x - offset.x);
+ dstrect.y = (Sint16)(y - offset.y);
+ dstrect.w = 0;
+ dstrect.h = 0;
+
SDL_Rect srcrect;
-
- srcrect.w = surface->w / desc.array.width;
- srcrect.h = surface->h / desc.array.height;
+ srcrect.w = frame_size.width;
+ srcrect.h = frame_size.height;
- srcrect.x = desc.frame_pos.x + (srcrect.w * (frame%desc.array.width));
- srcrect.y = desc.frame_pos.y + (srcrect.h * (frame/desc.array.width));
+ srcrect.x = frame_pos.x + (srcrect.w * (frame%array.width));
+ srcrect.y = frame_pos.y + (srcrect.h * (frame/array.width));
- SDL_BlitSurface(surface, &srcrect, target, &pos);
+ SDL_BlitSurface(surface, &srcrect, dst, &dstrect);
}
};
@@ -121,11 +137,6 @@
{
}
-Sprite::Sprite(const std::string& name)
- : impl(new SpriteImpl(name))
-{
-}
-
Sprite::Sprite(const PixelBuffer& pixelbuffer)
: impl(new SpriteImpl(pixelbuffer))
{
@@ -152,8 +163,8 @@
int
Sprite::get_width()
{
- if (impl.get() && impl->surface)
- return impl->surface->w;
+ if (impl.get())
+ return impl->frame_size.width;
else
return 0;
}
@@ -161,8 +172,8 @@
int
Sprite::get_height()
{
- if (impl.get() && impl->surface)
- return impl->surface->h;
+ if (impl.get())
+ return impl->frame_size.height;
else
return 0;
}
@@ -175,22 +186,21 @@
void
Sprite:: update(float delta)
{
- // FIXME
- impl->frame += 1;
- if (impl->frame > (impl->desc.array.width * impl->desc.array.height))
- impl->frame = 0;
+ if (impl.get())
+ impl->update(delta);
}
void
Sprite::set_frame(int i)
{
-
+ if (impl.get())
+ impl->frame = i;
}
int
Sprite::get_frame_count() const
{
- return 1;
+ return (impl->array.width * impl->array.height);
}
bool
@@ -202,7 +212,10 @@
int
Sprite::get_current_frame() const
{
- return 0;
+ if (impl.get())
+ return impl->frame;
+ else
+ return 0;
}
void
Modified: branches/pingus_sdl/src/sprite.hpp
===================================================================
--- branches/pingus_sdl/src/sprite.hpp 2007-01-17 13:59:02 UTC (rev 2678)
+++ branches/pingus_sdl/src/sprite.hpp 2007-01-17 20:15:01 UTC (rev 2679)
@@ -40,7 +40,6 @@
public:
Sprite();
Sprite(const SpriteDescription& desc);
- Sprite(const std::string& name);
Sprite(const PixelBuffer& pixelbuffer);
~Sprite();
@@ -56,6 +55,7 @@
bool is_finished() const;
void restart();
operator bool();
+
private:
SharedPtr<SpriteImpl> impl;
};
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Pingus-CVS] r2679 - branches/pingus_sdl/src,
grumbel at BerliOS <=