diff options
author | Luke Bratch <l_bratch@yahoo.co.uk> | 2010-11-26 12:49:49 +0000 |
---|---|---|
committer | Luke Bratch <l_bratch@yahoo.co.uk> | 2010-11-26 12:49:49 +0000 |
commit | 4718c83d9204a0a9e2e0add143e829bbb9c0bff0 (patch) | |
tree | 51b5192285126af720c8d7df3b45ad6261e8b218 | |
parent | 5dd6f1089a86e69889056f3853b373025f68026f (diff) |
Implement Sprite::get/setClip for sprite sheet clipping & Creature::Orient for setting a
Creature's clip/orientation
-rw-r--r-- | Creature.cpp | 11 | ||||
-rw-r--r-- | Creature.h | 3 | ||||
-rw-r--r-- | Level.cpp | 8 | ||||
-rw-r--r-- | Sprite.cpp | 17 | ||||
-rw-r--r-- | Sprite.h | 9 | ||||
-rw-r--r-- | intensemarcus.h | 6 |
6 files changed, 53 insertions, 1 deletions
diff --git a/Creature.cpp b/Creature.cpp index 7693cb9..ba2a790 100644 --- a/Creature.cpp +++ b/Creature.cpp @@ -38,3 +38,14 @@ int Creature::getHealth() { void Creature::setHealth(int h) { health = h; } + +void Creature::orient() { + if (xVel < 0) + clipNo = ORIENT_LEFT; + else if (xVel > 0) + clipNo = ORIENT_RIGHT; + else if (yVel < 0) + clipNo = ORIENT_BACK; + else + clipNo = ORIENT_FRONT; +} @@ -22,6 +22,9 @@ class Creature : public Sprite { int getHealth(); void setHealth(int h); + // Set correct sprite clip for current situation + void orient(); + protected: // Velocities int xVel, yVel; @@ -32,15 +32,21 @@ void Level::changeLevel(int levelNo) { // Make player into a Marcus player.setImage("marcussheet.png"); + player.setClip(ORIENT_FRONT, MARCUS_WIDTH * 0, 0, MARCUS_WIDTH, MARCUS_HEIGHT); + player.setClip(ORIENT_BACK, MARCUS_WIDTH * 1, 0, MARCUS_WIDTH, MARCUS_HEIGHT); + player.setClip(ORIENT_RIGHT, MARCUS_WIDTH * 2, 0, MARCUS_WIDTH, MARCUS_HEIGHT); + player.setClip(ORIENT_LEFT, MARCUS_WIDTH * 3, 0, MARCUS_WIDTH, MARCUS_HEIGHT); } void Level::move() { + // Player movements player.setX(player.getX() + player.getXVel()); + player.orient(); } void Level::draw(Screen *screen) { // Blit background screen->blit(0, 0, background); - screen->blit(player.getX(), player.getY(), player.image); + screen->blit(player.getX(), player.getY(), player.image, player.getClip()); } @@ -28,4 +28,21 @@ bool Sprite::visible() { bool Sprite::setImage(std::string path) { image = loadImage(path); + // Set initial clip to the whole image + clipNo = 0; + clip[0].x = 0; + clip[0].y = 0; + clip[0].w = image->w; + clip[0].h = image->h; +} + +SDL_Rect* Sprite::getClip() { + return &clip[clipNo]; +} + +void Sprite::setClip(int clipNo, int x, int y, int w, int h) { + clip[clipNo].x = x; + clip[clipNo].y = y; + clip[clipNo].w = w; + clip[clipNo].h = h; } @@ -25,12 +25,21 @@ class Sprite { // This sprite's image SDL_Surface *image; + SDL_Rect* getClip(); + + void setClip(int clipNo, int x, int y, int w, int h); + protected: // Coordinates int x, y; // Visability bool isVisible; + + // Sprite clip + SDL_Rect clip[4]; + // Clip number + int clipNo; }; #endif diff --git a/intensemarcus.h b/intensemarcus.h index 543eafc..45bc78c 100644 --- a/intensemarcus.h +++ b/intensemarcus.h @@ -9,6 +9,12 @@ const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; const int SCREEN_BPP = 32; const int FPS = 30; +const int MARCUS_WIDTH = 64; +const int MARCUS_HEIGHT = 124; +const int ORIENT_FRONT = 0; +const int ORIENT_BACK = 1; +const int ORIENT_RIGHT = 2; +const int ORIENT_LEFT = 3; SDL_Surface *loadImage(std::string filename); |