Skip to content

Commit

Permalink
animation added and music added
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinmayMittal committed Apr 15, 2022
1 parent 0eeb7b2 commit cdef8bb
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 22 deletions.
48 changes: 48 additions & 0 deletions Animation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "Animation.h"
#include "MyTexture.h"
#include <SDL2/SDL.h>
#include<iostream>

Animation::Animation( LTexture &texture , int width , int height) : animationTexture(texture)
{
mWidth = width ;
mHeight = height ;
mFrames = 0 ;
mBox = { 0 , 0 } ;
animationSpeed = 20 ;
int numberOfColumns = texture.getWidth() / width;
int numberOfRows = texture.getHeight() / height ;
numberOfAnimationImages = ( numberOfColumns*numberOfRows);
animationImages.resize( numberOfColumns*numberOfRows);
for( int row = 0 ; row < numberOfRows ; row ++ ){
for( int col = 0 ; col < numberOfColumns ; col ++ ){
animationImages[row*numberOfColumns + col ] = {
col*width,
row*height ,
width ,
height
} ;
}
}
}

Animation::~Animation()
{
cleanup() ;
}

void Animation::cleanup()
{
animationTexture.free() ;
}

void Animation::setBox( int x , int y )
{
mBox = { x , y } ;
}
int Animation::render(SDL_Renderer *renderer, SDL_Rect &camera)
{
animationTexture.render( renderer , mBox.x - camera.x, mBox.y - camera.y , &animationImages[mFrames/(animationSpeed)] ) ;
mFrames = (mFrames + 1) % (numberOfAnimationImages * animationSpeed);
return 0 ;
}
22 changes: 22 additions & 0 deletions Animation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<SDL2/SDL.h>
#include<vector>
#include"MyTexture.h"
#include "Renderable.h"

class Animation : public Renderable
{
private :
LTexture &animationTexture ;
int mFrames ;
int animationSpeed ;
std::vector<SDL_Rect> animationImages;
int mWidth , mHeight ;
SDL_Rect mBox ;
int numberOfAnimationImages ;
public :
Animation( LTexture &texture , int width , int height ) ;
~Animation() ;
int render(SDL_Renderer *renderer, SDL_Rect &camera);
void cleanup() ;
void setBox( int x , int y ) ;
} ;
77 changes: 71 additions & 6 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void LGame::render(SDL_Renderer *renderer)
if(players[0].isBusy()){
SDL_SetRenderDrawColor(renderer, 0xFd, 0xb3, 0x36, 0xFF);
offset = taskStatusBarWidth + 10 ;
SDL_Rect fillRect = {window.getWidth() - offset, gyTextOffset, (float)(players[0].getCurrentTaskTimer().getTicks() * (taskStatusBarWidth) )/ (players[0].getCurrentTaskTime()), gyRenderOffset};
SDL_Rect fillRect = {window.getWidth() - offset, gyTextOffset, ((int)players[0].getCurrentTaskTimer().getTicks() * (taskStatusBarWidth) )/ (players[0].getCurrentTaskTime()), gyRenderOffset};
SDL_RenderFillRect(renderer, &fillRect);
SDL_Rect outlineRect = {window.getWidth() - offset, gyTextOffset, taskStatusBarWidth, gyRenderOffset};
SDL_RenderDrawRect(renderer, &outlineRect);
Expand All @@ -139,6 +139,7 @@ void LGame::cleanUp()
players[0].cleanUp();
tiles[0].cleanUp();
delete timeText;
delete sleepingAnimation ;
}

bool LGame::initObjs()
Expand All @@ -149,6 +150,20 @@ bool LGame::initObjs()
printf("Failed to load ash texture!\n");
return false;
}
LTexture* sleepingAnimationTexture = new LTexture() ;
LTexture* burgerAnimationTexture = new LTexture() ;
if (!window.loadTexture(*sleepingAnimationTexture, "resources/sleeping.png"))
{
printf("Failed to load sleeping texture!\n");
return false;
}
if (!window.loadTexture(*burgerAnimationTexture, "resources/burger.png"))
{
printf("Failed to load burger texture!\n");
return false;
}
sleepingAnimation = new Animation( *sleepingAnimationTexture , 32, 32) ;
burgerAnimation = new Animation( *burgerAnimationTexture , 32 , 32) ;

Player ash(ashTexture, *this, 32, 32, 3, 1, 2, 0);
players.push_back(ash);
Expand Down Expand Up @@ -275,6 +290,13 @@ bool LGame::setTiles()

return true;
}
std::function< void(Player &player , std::string &displayText )> getFoodCollideFunc( std :: string a )
{
return [=](Player &player, std::string &displayText)
{
displayText = a ;
} ;
}
std::function< void(Player &player, std::string &displayText)> getHostelCollideFunc( std :: string hostelName)
{
return [=](Player &player, std::string &displayText)
Expand Down Expand Up @@ -304,7 +326,8 @@ std :: function< void (SDL_Event &e, Player &player)> getHostelEventListener( st
if(!player.isBusy()){
player.setCurrentTaskTime(10000) ;
player.getCurrentTaskTimer().start() ;
player.setTaskText("resting ... ") ;
player.setTaskText("resting ... ") ;
player.setTaskAnimation( player.getGame().sleepingAnimation ) ;
player.setUpdateStateParameters({
gMaxPlayerHealth ,
0 ,
Expand Down Expand Up @@ -421,8 +444,29 @@ void LGame::initEntities()
{ displayText = "swimming_pool"; });
Entity oat("oat", [&](Player &player, std::string &displayText)
{ displayText = "oat"; });
Entity hot_dog("hot_dog", [&](Player &player, std::string &displayText)
{ displayText = "hot_dog"; });
Entity hot_dog("hot_dog", getFoodCollideFunc("PRESS H for HOTDOG") ,
[&](SDL_Event &e, Player &player)
{
if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
{
switch (e.key.keysym.sym)
{
case SDLK_h:
if(player.getMoney() > 20 ) {
player.setTaskText("having hotdog... ") ;
player.setCurrentTaskTime(3000) ;
player.getCurrentTaskTimer().start() ;
player.setUpdateStateParameters({
20 ,
-20 ,
0
}) ;
}
break;
}
}
}
);
Entity gas("gas", [&](Player &player, std::string &displayText)
{ displayText = "gas"; });
Entity icecream("icecream", [&](Player &player, std::string &displayText)
Expand Down Expand Up @@ -453,8 +497,29 @@ void LGame::initEntities()
{ displayText = "coffee"; });
Entity hospital("hospital", [&](Player &player, std::string &displayText)
{ displayText = "hospital"; });
Entity burger("burger", [&](Player &player, std::string &displayText)
{ displayText = "burger"; });
Entity burger("burger" , getFoodCollideFunc("PRESS B for BURGER") ,[&](SDL_Event &e, Player &player)
{
if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
{
switch (e.key.keysym.sym)
{
case SDLK_b:
if(player.getMoney() > 20 ) {
player.setTaskText("having burger... ") ;
player.setCurrentTaskTime(6000) ;
player.getCurrentTaskTimer().start() ;
player.setTaskAnimation( player.getGame().burgerAnimation ) ;
player.setUpdateStateParameters({
20 ,
-20 ,
0
}) ;
}
break;
}
}
}
);
Entity vegetable_shop("vegetable_shop", [&](Player &player, std::string &displayText)
{ displayText = "vegetable_shop"; });
Entity bread_shop("bread_shop", [&](Player &player, std::string &displayText)
Expand Down
3 changes: 3 additions & 0 deletions Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class LGame : public LScreen
LTexture tilesTexture;
LTexture ashTexture;


TileAtlas tileAtlas;

int mTilesX;
Expand Down Expand Up @@ -59,6 +60,8 @@ class LGame : public LScreen
int getLevelHeight();
int getWindowWidth();
int getWindowHeight();
Animation* sleepingAnimation ;
Animation* burgerAnimation ;
LTimer getTimer() ;
};

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CXX = g++

#OBJS specifies which files to compile as part of the project
OBJS = Button.cpp collision.cpp Game.cpp main.cpp MainMenu.cpp MyTexture.cpp MyWindow.cpp Player.cpp Screen.cpp SearchOpponent.cpp Text.cpp TextInput.cpp Tile.cpp TileAtlas.cpp Timer.cpp SoundEffect.cpp Entity.cpp
OBJS = Button.cpp collision.cpp Game.cpp main.cpp MainMenu.cpp MyTexture.cpp MyWindow.cpp Player.cpp Screen.cpp SearchOpponent.cpp Text.cpp TextInput.cpp Tile.cpp TileAtlas.cpp Timer.cpp SoundEffect.cpp Entity.cpp Animation.cpp

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
Expand Down
35 changes: 22 additions & 13 deletions Player.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include<SDL2/SDL_mixer.h>
#include "Player.h"
#include "Tile.h"
#include "MyTexture.h"
Expand All @@ -15,14 +16,15 @@

std :: vector < std :: string > hostelNames{ "nilgiri" , "kara" , "aravali" , "jwala" , "kumaon" , "vindy" , "satpura" , "udai_girnar" , "himadri" , "kailash" } ;

Player ::Player(LTexture &myTexture, LGame &game, int playerHeight, int playerWidth, int right, int left, int top, int bottom) : mTexture(myTexture), mGame(game), wallCollisionMusic(std::string("collision.wav"))
Player ::Player(LTexture &myTexture, LGame &game, int playerHeight, int playerWidth, int right, int left, int top, int bottom) : mTexture(myTexture), mGame(game)
{
// Initialize the collision box
mCollisionMusic = Mix_LoadWAV("resources/collision.wav") ;
mBox.x = 32 * 7;
mBox.y = 0;
mBox.w = playerWidth;
mBox.h = playerHeight;

taskAnimation = NULL ;
srand(time(0));
hostelName = hostelNames[rand()%hostelNames.size()];
hostelName = "nilgiri" ; // for testing
Expand All @@ -32,10 +34,6 @@ Player ::Player(LTexture &myTexture, LGame &game, int playerHeight, int playerWi
// Initialize the velocity
mVelX = 0;
mVelY = 0;
// wallCollisionMusic.play() ;
// SDL_Delay(1000) ;
// wallCollisionMusic.play() ;
// SDL_Delay(2000) ;
this->velocity = 10;
this->moveFactor = 1;
this->direction = 'D';
Expand All @@ -51,6 +49,7 @@ Player ::Player(LTexture &myTexture, LGame &game, int playerHeight, int playerWi
this->points = 0;
breakfast = lunch = dinner = false ;
taskText = "" ;
hasTaskAnimation = false ;
int numberOfimages = myTexture.getWidth() / playerWidth;
playerImages.resize(4 * numberOfimages);
this->numOfAnimationImages = numberOfimages;
Expand Down Expand Up @@ -163,7 +162,7 @@ void Player::move()
{
// move back
mBox.x -= mVelX * moveFactor;
// wallCollisionMusic.play() ;
Mix_PlayChannel( -1, mCollisionMusic, 0 );
}

// Move the dot up or down
Expand All @@ -174,7 +173,7 @@ void Player::move()
{
// move back
mBox.y -= mVelY * moveFactor;
// wallCollisionMusic.play() ;
Mix_PlayChannel( -1, mCollisionMusic, 0 );
}
}
}
Expand Down Expand Up @@ -238,7 +237,12 @@ int Player::render(SDL_Renderer *renderer, SDL_Rect &camera)
{
offset = mframes / animationSpeed;
}
mTexture.render(renderer, mBox.x - camera.x, mBox.y - camera.y, &playerImages[dimension + offset]);
if( isBusy() and hasTaskAnimation){
taskAnimation->setBox( mBox.x , mBox.y ) ;
taskAnimation->render( renderer , camera ) ;
}else{
mTexture.render(renderer, mBox.x - camera.x, mBox.y - camera.y, &playerImages[dimension + offset]);
}
mframes = (mframes + 1) % (numOfAnimationImages * animationSpeed);
if( isBusy()) mframes = 0 ;
return 0;
Expand All @@ -247,6 +251,7 @@ int Player::render(SDL_Renderer *renderer, SDL_Rect &camera)
void Player::cleanUp()
{
mTexture.free();
Mix_FreeChunk( mCollisionMusic ) ;
}

void Player::setVelocity(int vel)
Expand Down Expand Up @@ -316,9 +321,7 @@ std::string Player :: getHostelName(){

void Player::update()
{
// std::cout << yuluTimer.isStarted() << std::endl;
// std :: cout << health << "\n" ;
// std :: cout << taskText << "\n" ;

if (yuluTimer.isStarted())
{
moveFactor = 2;
Expand Down Expand Up @@ -356,6 +359,7 @@ void Player::update()
updateStateParameters( updateState) ;
updateState = { 0.0 , 0 , 0 } ;
taskText = "" ;
hasTaskAnimation = false ;
// update player stats
}
}
Expand Down Expand Up @@ -470,4 +474,9 @@ std:: string Player :: getTaskText()
void Player :: setTaskText( std :: string s)
{
taskText = s ;
}
}

void Player::setTaskAnimation( Animation *a) {
taskAnimation = a ;
hasTaskAnimation = true ;
}
8 changes: 7 additions & 1 deletion Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include<SDL2/SDL_mixer.h>
#include "Tile.h"
#include "MyTexture.h"
#include "Renderable.h"
Expand All @@ -11,6 +12,8 @@
#include "Timer.h"
#include "utilities.h"
#include <string>
#include"Animation.h"


class LGame;

Expand Down Expand Up @@ -72,13 +75,15 @@ class Player : public Renderable
void setDinner( bool d) ;
std :: string getTaskText() ;
void setTaskText( std::string s ) ;
void setTaskAnimation( Animation *a) ;
private:
// Collision box of the dot

LGame &mGame;
SDL_Rect mBox;
LTexture &mTexture;
SoundEffect wallCollisionMusic;
Animation* taskAnimation ;
bool hasTaskAnimation ;
// bool touchesWall(std::vector<Tile> tiles);
int playerHeight, playerWidth;
int right, top, left, bottom;
Expand All @@ -103,6 +108,7 @@ class Player : public Renderable
playerStateUpdate updateState ;
bool breakfast , lunch, dinner ;
std::string taskText ;
Mix_Chunk* mCollisionMusic ;
};

#endif
3 changes: 2 additions & 1 deletion SoundEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ void SoundEffect::free()
void SoundEffect::play()
{
int channel = Mix_PlayChannel( -1, mMusic , 0 ) ;
std::cout << ( mMusic == NULL ) << "\n" ;
if( channel == -1 ){
printf("Mix_PlayChannel: %s\n",Mix_GetError());
}else{
std::cout << "playing on channel " << channel << "\n" ;
}
SDL_Delay(1000) ;
// SDL_Delay(1000) ;
// Mix_PlayChannel(-1, chunk.get(), 0);
}
Binary file added resources/burger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added resources/sleeping.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cdef8bb

Please sign in to comment.