extern int glimageLoadAndBind (const char * fname, GLuint *tex) { SDL_Surface * texSurface = NULL; SDL_RWops *rwop = NULL; char ShouldBeFlipped = 0; int flags= IMG_INIT_JPG | IMG_INIT_PNG; int initted= IMG_Init(flags); int sign = (SDL_BYTEORDER == SDL_BIG_ENDIAN) ? 1 : -1; printf("%s\n", fname); if((initted & flags) != flags) { printf("IMG_Init: Failed to init required jpg and png support!\n"); printf("IMG_Init: %s\n", IMG_GetError()); return 0; } if((texSurface = IMG_Load(fname)) == NULL ) { fprintf(stderr, "Impossible d'ouvrir le fichier : %s\n", IMG_GetError()); return 0; } rwop=SDL_RWFromFile(fname, "rb"); //Si l'image est en JPEG ou PNG, il faut inverser if (IMG_isJPG(rwop) || IMG_isBMP(rwop) || IMG_isPNG(rwop)) ShouldBeFlipped = 1; if (ShouldBeFlipped) //invert_surface_vertical(texSurface); FlipVertically(texSurface); IMG_Quit(); glGenTextures(1, tex); glBindTexture(GL_TEXTURE_2D, *tex); if (texSurface->format->BytesPerPixel == 3){ if (sign * texSurface->format->Rshift > sign * texSurface->format->Bshift) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texSurface->w, texSurface->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texSurface->pixels); else glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texSurface->w, texSurface->h, 0, GL_BGR, GL_UNSIGNED_BYTE, texSurface->pixels); } if (texSurface->format->BytesPerPixel == 4){ if (sign * texSurface->format->Rshift > sign * texSurface->format->Bshift) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texSurface->w, texSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texSurface->pixels); else glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texSurface->w, texSurface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, texSurface->pixels); } SDL_FreeSurface(texSurface); return 1; }
// ========================================================================= int main(int argc, char** argv) { const char * usage = "Usage: lmu2png map.lmu chipset.png output.png\n"; if(argc < 4 || argc > 5) { std::cout<<usage; exit(EXIT_FAILURE); } const char* map_path = argv[1]; const char* chipset_path = argv[2]; const char* output_path = argv[3]; std::unique_ptr<RPG::Map> map = LMU_Reader::Load(map_path, ""); if (map.get() == NULL) { std::cerr<<LcfReader::GetError()<<std::endl; exit(EXIT_FAILURE); } SDL_Surface* chipset = IMG_Load(chipset_path); if (chipset == NULL) { std::cerr<<IMG_GetError()<<std::endl; exit(EXIT_FAILURE); } if (IMG_isBMP(SDL_RWFromFile(chipset_path, "rb"))) { // Set as color key the first color in the palette SDL_Color ckey = chipset->format->palette->colors[0]; SDL_SetColorKey(chipset, SDL_TRUE, SDL_MapRGB(chipset->format, ckey.r, ckey.g, ckey.b)); } SDL_Surface* output = SDL_CreateRGBSurface(0, map->width * 16, map->height * 16, 8, 0, 0, 0, 0); output->format->palette = chipset->format->palette; stChipset gen; gen.GenerateFromSurface(chipset); for (int y = 0; y < map->height; ++y) for (int x = 0; x < map->width; ++x) { gen.RenderTile(output, x*16, y*16, map->lower_layer[x+y*map->width], 0); gen.RenderTile(output, x*16, y*16, map->upper_layer[x+y*map->width], 0); } std::vector<RPG::Event>::iterator ev; for (ev = map->events.begin(); ev != map->events.end(); ++ev) { RPG::EventPage evp = ev->pages[0]; if (evp.character_name.empty()) gen.RenderTile(output, (ev->x)*16, (ev->y)*16, 0x2710 + evp.character_index, 0); } if(IMG_SavePNG(output, output_path) < 0) { std::cerr<<IMG_GetError()<<std::endl; exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { Uint32 flags; SDL_Surface *screen, *image; int i, depth, done; SDL_Event event; #if 0 SDL_RWops* rw_ops; #endif /* Check command line usage */ if ( ! argv[1] ) { fprintf(stderr, "Usage: %s <image_file>\n", argv[0]); return(1); } /* Initialize the SDL library */ if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); return(255); } flags = SDL_SWSURFACE; for ( i=1; argv[i]; ++i ) { if ( strcmp(argv[i], "-fullscreen") == 0 ) { SDL_ShowCursor(0); flags |= SDL_FULLSCREEN; continue; } #if 0 rw_ops = SDL_RWFromFile(argv[1], "r"); fprintf(stderr, "BMP:\t%d\n", IMG_isBMP(rw_ops)); fprintf(stderr, "GIF:\t%d\n", IMG_isGIF(rw_ops)); fprintf(stderr, "JPG:\t%d\n", IMG_isJPG(rw_ops)); fprintf(stderr, "PNG:\t%d\n", IMG_isPNG(rw_ops)); fprintf(stderr, "TIF:\t%d\n", IMG_isTIF(rw_ops)); /* fprintf(stderr, "TGA:\t%d\n", IMG_isTGA(rw_ops)); */ fprintf(stderr, "PCX:\t%d\n", IMG_isPCX(rw_ops)); #endif /* Open the image file */ #ifdef XPM_INCLUDED image = IMG_ReadXPMFromArray(picture_xpm); #else image = IMG_Load(argv[i]); #endif if ( image == NULL ) { fprintf(stderr, "Couldn't load %s: %s\n", argv[i], SDL_GetError()); continue; } SDL_WM_SetCaption(argv[i], "showimage"); /* Create a display for the image */ depth = SDL_VideoModeOK(image->w, image->h, 32, flags); /* Use the deepest native mode, except that we emulate 32bpp for viewing non-indexed images on 8bpp screens */ if ( depth == 0 ) { if ( image->format->BytesPerPixel > 1 ) { depth = 32; } else { depth = 8; } } else if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) { depth = 32; } if(depth == 8) flags |= SDL_HWPALETTE; screen = SDL_SetVideoMode(image->w, image->h, depth, flags); if ( screen == NULL ) { fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n", image->w, image->h, depth, SDL_GetError()); continue; } /* Set the palette, if one exists */ if ( image->format->palette ) { SDL_SetColors(screen, image->format->palette->colors, 0, image->format->palette->ncolors); } /* Draw a background pattern if the surface has transparency */ if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY)) draw_background(screen); /* Display the image */ SDL_BlitSurface(image, NULL, screen, NULL); SDL_UpdateRect(screen, 0, 0, 0, 0); done = 0; while ( ! done ) { if ( SDL_PollEvent(&event) ) { switch (event.type) { case SDL_KEYUP: switch (event.key.keysym.sym) { case SDLK_LEFT: if ( i > 1 ) { i -= 2; done = 1; } break; case SDLK_RIGHT: if ( argv[i+1] ) { done = 1; } break; case SDLK_ESCAPE: case SDLK_q: argv[i+1] = NULL; /* Drop through to done */ case SDLK_SPACE: case SDLK_TAB: done = 1; break; default: break; } break; case SDL_MOUSEBUTTONDOWN: done = 1; break; case SDL_QUIT: argv[i+1] = NULL; done = 1; break; default: break; } } else { SDL_Delay(10); } } SDL_FreeSurface(image); } /* We're done! */ SDL_Quit(); return(0); }
bool Image::isBMP(SDL_RWops *src) { return IMG_isBMP(src); }