Sprite::Sprite(string *nomeArquivo, Vector2 *pos, int nframes, int spacing) { animationSurfaces = (std::vector<SDL_Surface*>***) calloc(1, sizeof(std::vector<SDL_Surface*>**)); animationSurfaces[0] = (std::vector<SDL_Surface*>**) calloc(1, sizeof(std::vector<SDL_Surface*>)); SDL_Surface *animationSheet; int stepX, stepY; GameData *gameData = GameData::getInstance(); animationSheet = gameData->loadIMG(nomeArquivo->c_str()); if(animationSheet == NULL) { fprintf(stderr, "Arquivo %s n encontrado. Abortando... \n", nomeArquivo->c_str()); exit(1); } stepX = (animationSheet->w - spacing*(nframes - 1))/nframes; stepY = (animationSheet->h); int k = 0; for(k = 0; k < nframes; k++) { animationSurfaces[0][0]->at(k) = cropAnimationSheet(animationSheet, k, 0, stepX, stepY); } SDL_FreeSurface(animationSheet); }
/** * Construtor da classe sprite. * * @param nomeArq nome do arquivo a ser lido do sprite * @param x posicao x inicial do sprite * @param y posicao y inicial do sprite. * */ Sprite::Sprite(string *nomeArq, Vector2 *pos) { // lendo a textura SDL_Surface *texture1; GameData *gameData = GameData::getInstance(); std::cout << nomeArq->c_str() << std::endl; texture1 = gameData->loadIMG(nomeArq->c_str()); /* SDL_Rect rx = {0, 0, texture1->w, texture1->h}; texture = SDL_CreateRGBSurface(SDL_SWSURFACE, texture1->w, texture1->h, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); if((int)texture1->format->BitsPerPixel == 32) { SDL_gfxBlitRGBA(texture1, NULL, texture, &rx); } else { SDL_BlitSurface(texture1, NULL, texture, &rx); } */ texture = texture1; //texture = NULL; //while(texture == NULL) { // texture = SDL_DisplayFormatAlpha(texture1); //} //SDL_FreeSurface(texture1); animationLoopedOnce = false; this->pos = pos; // criando o rect do sprite rectangle.x = pos->x; rectangle.y = pos->y; rectangle.w = texture->w; rectangle.h = texture->h; width = rectangle.w; height = rectangle.h; rotAngle = 0; zoom = 1; scrollingVelocity.x = 1; scrollingVelocity.y = 1; textureRotoZoomed = NULL; scrollable = true; scrollManager = ScrollManager::getInstance(); renderPosition.x = 0; renderPosition.y = 0; loopX = false; loopY = false; gameObjectInstance = NULL; gameObjectAnimations = NULL; isAnimation = false; }
Sprite::Sprite(GameObjectInstance *gameObjectInstance, Vector2 *pos) : gameObjectInstance(gameObjectInstance) { GameData *gameData = GameData::getInstance(); animationLoopedOnce = false; gameObjectAnimations = gameObjectInstance->gameObject->gameObjectAnimations; animationSurfaces = (std::vector<SDL_Surface*>***) calloc(gameObjectAnimations->size(), sizeof(std::vector<SDL_Surface*>**)); animationDatas.resize(boost::extents[gameObjectAnimations->size()][32]); int i = 0; for(std::vector<GameObjectAnimation*>::iterator it = gameObjectAnimations->begin(); it != gameObjectAnimations->end(); ++it) { GameObjectAnimation* gameObjectAnimation = *it; animationSurfaces[i] = (std::vector<SDL_Surface*>**) calloc(32, sizeof(std::vector<SDL_Surface*>)); for(int j = 0; j < 32; j++) { int invertFlags = 0; if(gameObjectAnimation->types->at(j) != 0) { if(gameObjectAnimation->horizontalInvert.at(j)) { invertFlags = invertFlags | FLIP_HORIZONTAL; } if(gameObjectAnimation->verticalInvert.at(j)) { invertFlags = invertFlags | FLIP_VERTICAL; } } std::string spriteDir; switch(gameObjectAnimation->types->at(j)) { case GameObjectAnimation::NON_EXISTANT: animationSurfaces[i][j] = NULL; break; case GameObjectAnimation::STATIC: animationSurfaces[i][j] = new std::vector<SDL_Surface*>(1); spriteDir = std::string("objects/"); spriteDir.append(gameObjectAnimation->sprites->at(j)->spriteFileName.c_str()); if(invertFlags) { animationSurfaces[i][j]->at(0) = flipSurface(gameData->loadIMG(spriteDir), invertFlags); } else { animationSurfaces[i][j]->at(0) = gameData->loadIMG(spriteDir); } if(animationSurfaces[i][j]->at(0) == NULL) { fprintf(stderr, "Arquivo %s n encontrado. Abortando... \n", gameObjectAnimation->sprites->at(j)->spriteFileName.c_str()); exit(1); } break; case GameObjectAnimation::ANIMATED: AnimationData* animationData = gameObjectAnimation->animations->at(j); SDL_Surface *animationSheet; int stepX, stepY; animationDatas[i][j] = *animationData; animationSheet = gameData->loadIMG(animationData->fileName.c_str()); if(animationSheet == NULL) { fprintf(stderr, "Arquivo %s n encontrado. Abortando... \n", animationData->fileName.c_str()); exit(1); } animationSurfaces[i][j] = new std::vector<SDL_Surface*>((animationData->endX - animationData->startX + 1)* (animationData->endY - animationData->startY + 1)); stepX = (animationSheet->w - animationData->horizontalSpacing*(animationData->horizontalFrameNumber - 1))/animationData->horizontalFrameNumber; stepY = (animationSheet->h - animationData->verticalSpacing*(animationData->verticalFrameNumber - 1))/animationData->verticalFrameNumber; int k = 0; if(animationData->order == 0) { for(int x = animationData->startX - 1; x < animationData->endX; x++) { for(int y = animationData->startY - 1; y < animationData->endY; y++) { if(invertFlags) { animationSurfaces[i][j]->at(k) = flipSurface(cropAnimationSheet(animationSheet, x, y, stepX, stepY), invertFlags); } else { animationSurfaces[i][j]->at(k) = cropAnimationSheet(animationSheet, x, y, stepX, stepY); } k++; } } } else { for(int y = animationData->startY - 1; y < animationData->endY; y++) { for(int x = animationData->startX - 1; x < animationData->endX; x++) { if(invertFlags) { animationSurfaces[i][j]->at(k) = flipSurface(cropAnimationSheet(animationSheet, x, y, stepX, stepY), invertFlags); } else { animationSurfaces[i][j]->at(k) = cropAnimationSheet(animationSheet, x, y, stepX, stepY); } k++; } } } SDL_FreeSurface(animationSheet); break; } } // ajustando as direcoes nulas int start = -1; int subStart; // encontra um que não é null for(int j = 0; j < 32; j++) { if(animationSurfaces[i][j] != NULL) { start = j; subStart = j; break; } } if(start != -1) { int j; for(j = start + 1; j%32 != start; j++) { if(animationSurfaces[i][j%32] != NULL) { fillAnimationNulls(subStart, i, j); subStart = j; } //std::cout << "run: " << j << std::endl; } fillAnimationNulls(subStart, i, j); } i++; } texture = animationSurfaces[0][0]->at(0); std::cout << "Tamanho: " << sizeof(animationSurfaces[0][0])/sizeof(SDL_Surface*) << std::endl; this->pos = pos; // criando o rect do sprite rectangle.x = pos->x; rectangle.y = pos->y; rectangle.w = texture->w; rectangle.h = texture->h; width = rectangle.w; height = rectangle.h; rotAngle = 0; zoom = 1; scrollingVelocity.x = 1; scrollingVelocity.y = 1; textureRotoZoomed = NULL; scrollable = true; scrollManager = ScrollManager::getInstance(); renderPosition.x = 0; renderPosition.y = 0; loopX = false; loopY = false; currentAnimation = 0; currentDirection = 0; currentFrame = 0; if(gameObjectAnimations->at(currentAnimation)->types->at(currentDirection) == GameObjectAnimation::ANIMATED) { frameTime = 100 - gameObjectAnimations->at(currentAnimation)->animations->at(currentDirection)->velocity; } frameTimeAcc = 0; isAnimation = true; }