std::list<Sprite> Sprite::getSubImages( const std::string image_path, int nx, int ny) { std::list<Sprite> sprites; SDL_Surface* image = loadImage( image_path ); int width = image->w / nx; int height = image->h / ny; SDL_Surface* sub_surface = NULL; for ( int yp = 0 ; yp < ny ; yp++ ) { for ( int xp = 0 ; xp < nx ; xp++ ) { //create new universal-surface, used to save all subimages sub_surface = SDL_CreateRGBSurface( SDL_SWSURFACE, width, height, image->format->BitsPerPixel, image->format->Rmask, image->format->Gmask, image->format->Bmask, image->format->Amask ); flipSurface( image, sub_surface, Vec2i( (xp * width), (yp * height) ), Vec2i( (xp * width) + width, (yp * height) + height )); sprites.push_back( Sprite( sub_surface )); //SDL_FreeSurface( sub_surface ); !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11 } } SDL_FreeSurface( image ); return sprites; }
SDL_Surface *loadBitmap(const char filename[]) { SDL_Surface *bitmap = SDL_LoadBMP("data/nehe.bmp"); if (!bitmap) return NULL; SDL_Surface *flipped = flipSurface(bitmap); SDL_FreeSurface(bitmap); return flipped; }
void CubeMap::update() { glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, textureObject); int i = 0; for( vector<ofVideoGrabber>::iterator cIt = mCaptures.begin(); cIt != mCaptures.end(); ++cIt ) { int GLCubeDir = -1; if( (*cIt)->checkNewFrame() ) { Surface8u surf = (*cIt)->getSurface(); switch(i) { case 0: GLCubeDir = GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB; glTexImage2D(GLCubeDir, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, surf.getData()); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, surf.getData()); flipSurface(surf); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, surf.getData()); break; case 1: GLCubeDir = GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB; // Surface8u surf = (*cIt)->getSurface(); glTexImage2D(GLCubeDir, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, surf.getData()); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, surf.getData()); flipSurface(surf); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, surf.getData()); break; // case 2: // GLCubeDir = GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB; // break; default: GLCubeDir = GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB; break; } } i++; } }
void Sprite::init( SDL_Surface* image ) { putSize( image ); //new surface with optimized size SDL_Surface* desk = SDL_CreateRGBSurface( SDL_SWSURFACE, twidth, theight, image->format->BitsPerPixel, image->format->Rmask, image->format->Gmask, image->format->Bmask, image->format->Amask ); flipSurface( image, desk ); textureID = getGLuint( desk ); SDL_FreeSurface( image ); SDL_FreeSurface( desk ); }
int takeScreenshot(const char * filename) { GLint viewport[4]; Uint32 rmask, gmask, bmask, amask; SDL_Surface * picture, * finalpicture; glGetIntegerv(GL_VIEWPORT, viewport); #if SDL_BYTEORDER == SDL_BIG_ENDIAN rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; #else rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; #endif picture = SDL_CreateRGBSurface(SDL_SWSURFACE,viewport[2],viewport[3], 32, rmask, gmask, bmask, amask); SDL_LockSurface(picture); glReadPixels(viewport[0],viewport[1],viewport[2],viewport[3],GL_RGBA, GL_UNSIGNED_BYTE,picture->pixels); SDL_UnlockSurface(picture); finalpicture = flipSurface(picture); if (SDL_SaveBMP(finalpicture, filename)) { return -1; } SDL_FreeSurface(finalpicture); SDL_FreeSurface(picture); return 0; }
void iSprite::flipHorizontally() { this->surface = flipSurface(this->surface); this->setSurface(this->surface); this->mirrored = (this->mirrored == false); }
GLuint loadTexture(const char * filename,bool useMipMap) { GLuint glID; SDL_Surface * picture_surface = NULL; SDL_Surface *gl_surface = NULL; SDL_Surface * gl_fliped_surface = NULL; Uint32 rmask, gmask, bmask, amask; picture_surface = IMG_Load(filename); if (picture_surface == NULL) { printf("failed to load texture \"%s\"", filename); return 0; } #if SDL_BYTEORDER == SDL_BIG_ENDIAN rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; #else rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; #endif SDL_PixelFormat format = *(picture_surface->format); format.BitsPerPixel = 32; format.BytesPerPixel = 4; format.Rmask = rmask; format.Gmask = gmask; format.Bmask = bmask; format.Amask = amask; gl_surface = SDL_ConvertSurface(picture_surface,&format, SDL_SWSURFACE); if(!gl_surface) printf("could not convert surface (%s)", filename); gl_fliped_surface = flipSurface(gl_surface); if(!gl_fliped_surface) printf("could not flip surface (%s)", filename); glGenTextures(1, &glID); glBindTexture(GL_TEXTURE_2D, glID); printf("texture \"%s\" chargee (%d x %d)\n", filename, gl_surface->w, gl_surface->h); if (useMipMap) { gluBuild2DMipmaps(GL_TEXTURE_2D, 4, gl_fliped_surface->w, gl_fliped_surface->h, GL_RGBA,GL_UNSIGNED_BYTE, gl_fliped_surface->pixels); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); } else { glTexImage2D(GL_TEXTURE_2D, 0, 4, gl_fliped_surface->w, gl_fliped_surface->h, 0, GL_RGBA,GL_UNSIGNED_BYTE, gl_fliped_surface->pixels); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); SDL_FreeSurface(gl_fliped_surface); SDL_FreeSurface(gl_surface); SDL_FreeSurface(picture_surface); return glID; }
int loadTexture(Texture *texture) { GLuint glID; SDL_Surface * picture_surface = NULL; SDL_Surface *gl_surface = NULL; SDL_Surface * gl_fliped_surface = NULL; Uint32 rmask, gmask, bmask, amask; picture_surface = IMG_Load(texture->path); if (picture_surface == NULL) { printf("Error loading texture (%s)\n", texture->path); return 0; } #if SDL_BYTEORDER == SDL_BIG_ENDIAN rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; #else rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; #endif SDL_PixelFormat format = *(picture_surface->format); format.BitsPerPixel = 32; format.BytesPerPixel = 4; format.Rmask = rmask; format.Gmask = gmask; format.Bmask = bmask; format.Amask = amask; gl_surface = SDL_ConvertSurface(picture_surface,&format,SDL_SWSURFACE); gl_fliped_surface = flipSurface(gl_surface); glGenTextures(1, &glID); glBindTexture(GL_TEXTURE_2D, glID); glTexImage2D(GL_TEXTURE_2D, 0, 4, gl_fliped_surface->w, gl_fliped_surface->h, 0, GL_RGBA,GL_UNSIGNED_BYTE, gl_fliped_surface->pixels); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); if(glID != 0) { texture->IDtex = glID; texture->wMax = gl_fliped_surface->w; texture->hMax = gl_fliped_surface->h; } else { printf("Error loading texture (%s)\n", texture->path); return 0; } SDL_FreeSurface(gl_fliped_surface); SDL_FreeSurface(gl_surface); SDL_FreeSurface(picture_surface); texture->posTex[0].x = 0; texture->posTex[0].y = 1; texture->posTex[1].x = 1; texture->posTex[1].y = 0; return 1; }
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; }
GLuint loadtexture(char *filename, int use_mipmap) { GLuint gl_id; SDL_Surface *picture_surface = NULL; SDL_Surface *gl_surface = NULL; SDL_Surface *gl_fliped_surface = NULL; SDL_PixelFormat format; Uint32 rmask; Uint32 gmask; Uint32 bmask; Uint32 amask; picture_surface = IMG_Load(filename); if (picture_surface == NULL) return (0); if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; } else { rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; } format = *(picture_surface->format); format.BitsPerPixel = 32; format.BytesPerPixel = 4; format.Rmask = rmask; format.Gmask = gmask; format.Bmask = bmask; format.Amask = amask; gl_surface = SDL_ConvertSurface(picture_surface, &format, SDL_SWSURFACE); gl_fliped_surface = flipSurface(gl_surface); glGenTextures(1, &gl_id); glBindTexture(GL_TEXTURE_2D, gl_id); if (use_mipmap == 1) { gluBuild2DMipmaps(GL_TEXTURE_2D, 4, gl_fliped_surface->w, gl_fliped_surface->h, GL_RGBA, GL_UNSIGNED_BYTE, gl_fliped_surface->pixels); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); } else { glTexImage2D(GL_TEXTURE_2D, 0, 4, gl_fliped_surface->w, gl_fliped_surface->h, 0, GL_RGBA,GL_UNSIGNED_BYTE, gl_fliped_surface->pixels); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); SDL_FreeSurface(gl_fliped_surface); SDL_FreeSurface(gl_surface); SDL_FreeSurface(picture_surface); return (gl_id); }
pacman::pacman(SDL_Surface *parent) { this->parent = parent; status = 0; total = 4; me_right = new SDL_Surface*[total]; me_right[0] = load_image("images/pac1.bmp"); me_right[1] = load_image("images/pac2.bmp"); me_right[2] = load_image("images/pac3.bmp"); me_right[3] = load_image("images/pac2.bmp"); me_left = new SDL_Surface*[total]; me_left[0] = flipSurface(me_right[0],FLIP_HORIZONTAL); me_left[1] = flipSurface(me_right[1],FLIP_HORIZONTAL); me_left[2] = flipSurface(me_right[2],FLIP_HORIZONTAL); me_left[3] = flipSurface(me_right[3],FLIP_HORIZONTAL); me_up = new SDL_Surface*[total]; me_up[0] = flipSurface(me_right[0],FLIP_M90); me_up[1] = flipSurface(me_right[1],FLIP_M90); me_up[2] = flipSurface(me_right[2],FLIP_M90); me_up[3] = flipSurface(me_right[3],FLIP_M90); me_down = new SDL_Surface*[total]; me_down[0] = flipSurface(me_right[0],FLIP_P90); me_down[1] = flipSurface(me_right[1],FLIP_P90); me_down[2] = flipSurface(me_right[2],FLIP_P90); me_down[3] = flipSurface(me_right[3],FLIP_P90); me = new SDL_Surface*[total]; me[0] = me_right[0]; me[1] = me_right[1]; me[2] = me_right[2]; me[3] = me_right[3]; pos = me[0]->clip_rect; pos.x = 300; pos.y = 200; for(int i=0;i<total;i++) SDL_SetColorKey(me[i], SDL_SRCCOLORKEY, SDL_MapRGB(me[i]->format, 255, 255, 255)); }
void Sprite::flipSurface( SDL_Surface* src, SDL_Surface* desk ) { flipSurface( src, desk, Vec2i( 0, 0 ), Vec2i( src->w, src->h )); }