From 4718c83d9204a0a9e2e0add143e829bbb9c0bff0 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Fri, 26 Nov 2010 12:49:49 +0000 Subject: Implement Sprite::get/setClip for sprite sheet clipping & Creature::Orient for setting a Creature's clip/orientation --- Creature.cpp | 11 +++++++++++ Creature.h | 3 +++ Level.cpp | 8 +++++++- Sprite.cpp | 17 +++++++++++++++++ Sprite.h | 9 +++++++++ intensemarcus.h | 6 ++++++ 6 files changed, 53 insertions(+), 1 deletion(-) 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; +} diff --git a/Creature.h b/Creature.h index 4c9197c..eb0f192 100644 --- a/Creature.h +++ b/Creature.h @@ -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; diff --git a/Level.cpp b/Level.cpp index 6c0f80c..8268af8 100644 --- a/Level.cpp +++ b/Level.cpp @@ -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()); } diff --git a/Sprite.cpp b/Sprite.cpp index d87368a..3b10208 100644 --- a/Sprite.cpp +++ b/Sprite.cpp @@ -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; } diff --git a/Sprite.h b/Sprite.h index 74f9bea..d745376 100644 --- a/Sprite.h +++ b/Sprite.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); -- cgit v1.2.3