Spritesheet cutting working
This commit is contained in:
@@ -45,8 +45,8 @@ public:
|
||||
|
||||
virtual ~Entity();
|
||||
protected:
|
||||
std::string PATH = "C:/Users/Ben/Desktop/crumpet-engine";
|
||||
//std::string PATH = "E:/Games/crumpet-engine";
|
||||
//std::string PATH = "C:/Users/Ben/Desktop/crumpet-engine";
|
||||
std::string PATH = "E:/Games/crumpet-engine";
|
||||
private:
|
||||
std::string m_name;
|
||||
|
||||
|
||||
@@ -6,28 +6,28 @@
|
||||
#define SCREEN_HEIGHT 600
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Game game("Crumpet engine", SCREEN_WIDTH, SCREEN_HEIGHT, 1, 1000 / 60);
|
||||
Game game("Crumpet engine", SCREEN_WIDTH, SCREEN_HEIGHT, 1, 1000 / 60); // 1000 / 60);
|
||||
Timer timer;
|
||||
|
||||
Sprite sans("sans", game.SDLRenderer, SpriteType::SPRITE_ANIMATED);
|
||||
sans.LoadSpriteTextures("/resources/sans-undertale-spritesheet.png");
|
||||
sans.UseSpriteSheet(SpriteState::STATE_RIGHT, 10, 10, 200, 200, 50, 4);
|
||||
sans.Spritestate = SpriteState::STATE_RIGHT;
|
||||
sans.UseSpriteSheet(SpriteState::STATE_FRONT, 30, 9, 230, 300, 10, 4);
|
||||
sans.UseSpriteSheet(SpriteState::STATE_RIGHT, 30, 320, 170, 300, 10, 4);
|
||||
sans.UseSpriteSheet(SpriteState::STATE_LEFT, 40, 640, 170, 300, 10, 4);
|
||||
|
||||
while (!game.IsDisplayClosed()) {
|
||||
// game.PollEvents();
|
||||
game.PollEvents();
|
||||
|
||||
if (timer.GetTimeElapsed() >= game.TargetMsPerUpdate) { // Constant update rate, despite framerate
|
||||
// game logic
|
||||
|
||||
const Uint8 *state = SDL_GetKeyboardState(NULL);
|
||||
// if (state[SDL_SCANCODE_A]) mario.Pos.x -= 10;
|
||||
|
||||
while (SDL_PollEvent(&game.m_event) != 0) {
|
||||
if (game.m_event.type == SDL_QUIT)
|
||||
game.CloseDisplay();
|
||||
};
|
||||
if (state[SDL_SCANCODE_D]) sans.Spritestate = SpriteState::STATE_RIGHT;
|
||||
else if (state[SDL_SCANCODE_A]) sans.Spritestate = SpriteState::STATE_LEFT;
|
||||
else sans.Spritestate = SpriteState::STATE_FRONT;
|
||||
|
||||
sans.AnimateSprite();
|
||||
if (timer.ticks % 5 == 0) {
|
||||
sans.TickAninmation();
|
||||
}
|
||||
|
||||
timer.Tick();
|
||||
}
|
||||
@@ -39,3 +39,5 @@ int main(int argc, char** argv) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//if (state[SDL_SCANCODE_A]) mario.Pos.x -= 10;
|
||||
|
||||
@@ -23,27 +23,36 @@ bool Sprite::LoadSpriteTextures(std::string path) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Sprite::UseSpriteSheet(SpriteState state, int startX, int startY, int width, int height, int seperation, int frames) {
|
||||
void Sprite::UseSpriteSheet(SpriteState state, int startX, int startY, int width, int height, int separation, int frames) {
|
||||
for (int i = 1; i <= frames; i++) {
|
||||
int x = startX * (i + seperation);
|
||||
SDL_Rect temp1 = { x, startY, width, height };
|
||||
SDL_Rect* temp = new SDL_Rect(temp1);
|
||||
m_spriteMaps[state][i] = temp;
|
||||
//temp = { NULL, NULL, NULL, NULL };
|
||||
if (i == 1) {
|
||||
std::cout << startX << " " << startY << " " << width << " " << i << std::endl;
|
||||
SDL_Rect temp1 = { startX, startY, width, height };
|
||||
SDL_Rect* temp = new SDL_Rect(temp1);
|
||||
m_spriteMaps[state][i] = temp;
|
||||
} else {
|
||||
int x = (width * i) + startX + (separation * i - separation) - width;
|
||||
std::cout << x << " " << startY << " " << width << " " << i << std::endl;
|
||||
SDL_Rect temp1 = { x, startY, width, height };
|
||||
SDL_Rect* temp = new SDL_Rect(temp1);
|
||||
m_spriteMaps[state][i] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
Vec2 temp1(width, height);
|
||||
Vec2* temp = new Vec2(temp1);
|
||||
m_spriteSize[state] = temp;
|
||||
}
|
||||
|
||||
void Sprite::AnimateSprite() {
|
||||
void Sprite::TickAninmation() {
|
||||
if (m_lastSpritestate == Spritestate) {
|
||||
m_currentFrame++;
|
||||
} else {
|
||||
m_lastSpritestate = Spritestate;
|
||||
m_currentFrame = 0;
|
||||
m_currentFrame = 1;
|
||||
}
|
||||
if (m_currentFrame >= m_spriteMaps[Spritestate].size()) m_currentFrame = 0;
|
||||
if (m_currentFrame > m_spriteMaps[Spritestate].size()) m_currentFrame = 1;
|
||||
}
|
||||
|
||||
void Sprite::Render() {
|
||||
|
||||
@@ -42,9 +42,9 @@ public:
|
||||
SpriteState Spritestate = SpriteState::STATE_DEFAULT;
|
||||
|
||||
bool LoadSpriteTextures(std::string path);
|
||||
void UseSpriteSheet(SpriteState state, int startX, int startY, int width, int height, int seperation, int frames);
|
||||
void AnimateSprite(SpriteState state);
|
||||
void AnimateSprite();
|
||||
void UseSpriteSheet(SpriteState state, int startX, int startY, int width, int height, int separation, int frames);
|
||||
void TickAninmation(SpriteState state);
|
||||
void TickAninmation();
|
||||
void Move();
|
||||
|
||||
Vec2 Pos;
|
||||
@@ -62,5 +62,5 @@ private:
|
||||
SpriteState m_lastSpritestate = SpriteState::STATE_DEFAULT;
|
||||
int m_spriteSheetW;
|
||||
int m_spriteSheetH;
|
||||
int m_currentFrame;
|
||||
int m_currentFrame = 1;
|
||||
};
|
||||
|
||||
@@ -10,7 +10,7 @@ public:
|
||||
|
||||
void Tick() { pastTicks = SDL_GetTicks(); this->ticks++; }
|
||||
int GetTimeElapsed() { return SDL_GetTicks() - pastTicks; }
|
||||
private:
|
||||
int ticks = 0;
|
||||
private:
|
||||
int pastTicks;
|
||||
};
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 16 KiB |
Reference in New Issue
Block a user