bool loadCommonTextureExt(char const* fname, GLuint textureId, bool flip) { int width, height, n; unsigned char *textureData = stbi_load(fname, &width, &height, &n, 3); if(textureData == nullptr) { std::cout << "ERROR: loadCommonTextureExt failed, fname = " << fname << ", stbi_load returned nullptr" << std::endl; return false; } defer(stbi_image_free(textureData)); if(flip) flipTexture(textureData, width, height, n); glBindTexture(GL_TEXTURE_2D, textureId); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData); glGenerateMipmap(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // glBindTexture(GL_TEXTURE_2D, 0); // unbind return true; }
Player::Player(PlatformState& platformState, BoxWorld* world, sf::Vector2f& size, sf::Vector2f& position, ResourceCollection& resource, LightSolver* lightSolver, StatManager& stats) : Entity(world, size, position) , mPlatformState(platformState) , groundCallBack(nullptr) , leftButton(false) , rightButton(false) , downButton(false) , mResource(resource) , flashLight(lightSolver->createLight(FLASHLIGHT_SIZE.x, FLASHLIGHT_SIZE.y)) , maskRight(&resource.getTexture("Assets/Shader/mask.png")) , maskLeft(flipTexture(maskRight)) , activeStreetLight(nullptr) , collisionFixture(body->GetFixtureList()) , mStats(stats) , flashLightBody(world->createEntityBody(sf::Vector2f(0.0f, 0.0f), static_cast<sf::Vector2f>(FLASHLIGHT_SIZE), false)) { flashLight->setColor(sf::Color(255, 255, 0, 150)); int group = flashLight->getFilterGroup(); group |= (1 << 8); flashLight->setFilterGroup(group); setFilterGroup(getFilterGroup() | (1 << 8)); setState(PLAYER_STATE::NORMAL); /* Walking/run animation */ auto &walking = mResource.getTexture("Assets/Characters/Stella Left.png"); sf::Vector2i walkingSize = static_cast<sf::Vector2i>(walking.getSize()); sf::Vector2i frameSize(256, 256); SpriteSheet spritesheet1(frameSize, walkingSize); std::vector<sf::IntRect> frames = spritesheet1.getAllFrames(); mWalking = new Animation(frames,walking); /* Idle animation */ auto &idle = mResource.getTexture("Assets/Characters/Stella idle.png"); sf::Vector2i idleSize = static_cast<sf::Vector2i>(idle.getSize()); SpriteSheet idleSheet(frameSize,idleSize); std::vector<sf::IntRect> idleFrames = idleSheet.getAllFrames(); mIdle = new Animation(idleFrames,idle); /* Jump animation */ auto &jump = mResource.getTexture("Assets/Characters/Stella jump.png"); sf::Vector2i jumpSize = static_cast<sf::Vector2i>(jump.getSize()); SpriteSheet jumpSheet(frameSize,jumpSize); std::vector<sf::IntRect> jumpFrames = jumpSheet.getAllFrames(); mJump = new Animation(jumpFrames,jump); /* Grab animation */ auto &grab = mResource.getTexture("Assets/Characters/Stella grab.png"); sf::Vector2i grabSize = static_cast<sf::Vector2i>(grab.getSize()); SpriteSheet grabSheet (frameSize, grabSize); std::vector<sf::IntRect> grabFrames = grabSheet.getAllFrames(); mGrab = new Animation(grabFrames,grab); /* Fall Animation*/ auto &fall = mResource.getTexture("Assets/Characters/Stella fall.png"); sf::Vector2i fallSize = static_cast<sf::Vector2i>(fall.getSize()); SpriteSheet fallSheet(frameSize, fallSize); std::vector<sf::IntRect> fallFrames = jumpSheet.getAllFrames(); mFall = new Animation(fallFrames,fall); /* Hit Animation*/ auto &hit = mResource.getTexture("Assets/Characters/Stella hit.png"); sf::Vector2i hitSize = static_cast<sf::Vector2i>(hit.getSize()); SpriteSheet hitSheet(frameSize, hitSize); std::vector<sf::IntRect> hitFrames = hitSheet.getAllFrames(); mHit = new Animation(hitFrames,hit); anime.setAnimation(*mIdle); updateSpriteOrigin(); mJumpSound = new sf::Sound; mJumpSound->setBuffer(*mResource.getSound("Assets/Sound/Jump.wav")); mWalkSound = new sf::Sound; mWalkSound->setBuffer(*mResource.getSound("Assets/Sound/Stella_Run_Loop_1.wav")); mWalkSound->setVolume(40); mHurtSound = new sf::Sound; mHurtSound->setBuffer(*mResource.getSound("Assets/Sound/Stella get hurt.wav")); setupSensors(position, size); body->SetLinearDamping(1.0f); /* Set filter for collisions */ b2Filter filter = collisionFixture->GetFilterData(); filter.categoryBits = PLAYER; //filter.maskBits =ALL, ENEMY_CHASE, ENEMY_ATTACK; filter.groupIndex = ALL, ENEMY_CHASE, ENEMY_ATTACK; collisionFixture->SetFilterData(filter); knockForce = 35; toggle = false; }