Exemplo n.º 1
0
//////////////////////////////////////////
////
//// GW_PlatformSDL_Image
////
//////////////////////////////////////////
GW_PlatformSDL_Image::GW_PlatformSDL_Image(SDL_Renderer *renderer, const string &filename, GW_Platform_RGB *tcolor)
{
    SDL_Surface* _surface;
#ifndef GW_USE_ZDATA
    _surface = SDL_LoadBMP( filename.c_str() );
#else
    SDL_RWops *l = SDL_RWFromZZIP( filename.c_str(), "rb" );
    if (l)
        _surface = SDL_LoadBMP_RW( l, 1 );
    else
        _surface = NULL;
    //SDL_FreeRW(l);
#endif

    if (!_surface)
        throw GW_Exception(string("Unable to load image "+filename+": "+string(SDL_GetError())));
    if (tcolor)
        SDL_SetColorKey(_surface, SDL_TRUE, SDL_MapRGB(_surface->format, tcolor->r, tcolor->g, tcolor->b));

    texture_ = SDL_CreateTextureFromSurface(renderer, _surface);
    if (!texture_)
        throw GW_Exception(string("Unable to load image "+filename+": "+string(SDL_GetError())));
    SDL_QueryTexture(texture_, NULL, NULL, &w_, &h_);

    // Cleanup ...
    SDL_FreeSurface(_surface);
#ifdef GW_DEBUG
    GWDBG_FVOUTPUT("\tGW_PlatformSDL_Image(file %s, texture %p, width %i, height %i)\n", filename.c_str(), texture_, w_, h_);
#endif
}
Exemplo n.º 2
0
SDL_Surface *WadImageCache::image_from_desc(WadImageDescriptor& desc)
{
	SDL_Surface *surface = NULL;
	OpenedFile wad_file;
	if (open_wad_file_for_reading(desc.file, wad_file))
	{
		struct wad_header header;
		if (read_wad_header(wad_file, &header))
		{
			struct wad_data *wad;
			wad = read_indexed_wad_from_file(wad_file, &header, desc.index, true);
			if (wad)
			{
				void *data;
				size_t length;
				data = extract_type_from_wad(wad, desc.tag, &length);
				if (data && length)
				{
					SDL_RWops *rwops = SDL_RWFromConstMem(data, length);
#ifdef HAVE_SDL_IMAGE
					surface = IMG_Load_RW(rwops, 1);
#else
					surface = SDL_LoadBMP_RW(rwops, 1);
#endif
				}
				free_wad(wad);
			}
		}
		close_wad_file(wad_file);
	}
	clear_game_error();
	return surface;
}
Exemplo n.º 3
0
SDL_Surface *SDL_ImageResource::load(WORD resourceID) {
	HRSRC resource;
	HGLOBAL resourceGlobal;
	LPVOID resourcePointer;
	DWORD resourceSize;
	SDL_RWops *surfaceData;
	HMODULE moduleHandle;

	moduleHandle = GetModuleHandle(NULL);
	resource = FindResource(moduleHandle, MAKEINTRESOURCE(resourceID), TEXT("BINARY"));
	if (!resource)
		return NULL;
	resourceSize = SizeofResource(moduleHandle, resource);
	if (!resourceSize)
		return NULL;
	resourceGlobal = LoadResource(moduleHandle, resource);
	if (!resourceGlobal)
		return NULL;
	resourcePointer = LockResource(resourceGlobal);
	if (!resourcePointer)
		return NULL;

	surfaceData = SDL_RWFromMem(resourcePointer, resourceSize);
	if (!surfaceData)
		return NULL;
	_surface = SDL_LoadBMP_RW(surfaceData, 1);

	return _surface;
}
Exemplo n.º 4
0
    SDL_Texture* LoadTextureFromBMP(SDL_Renderer *renderer, std::string pathToFile, SDL_Color transColor)
    {
        SDL_RWops *io = SDL_RWFromFile(pathToFile.c_str(), "rb");
        if (!io)
        {
            printf("LoadTextureFromBMP ERROR: %s\n", SDL_GetError());
            return NULL;
        }

        SDL_Surface* img = SDL_LoadBMP_RW(io, 1);
        if (!img)
        {
            printf("LoadTextureFromBMP ERROR: %s\n", SDL_GetError());
            return NULL;
        }

        SDL_SetColorKey(img, SDL_TRUE, SDL_MapRGB(img->format, transColor.r, transColor.g, transColor.b));

        SDL_Texture* imgTex = SDL_CreateTextureFromSurface(renderer, img);

        if (!imgTex)
        {
            printf("LoadTextureFromBMP ERROR: %s\n", SDL_GetError());
        }

        SDL_FreeSurface(img);
        
        return imgTex;
    }
Exemplo n.º 5
0
_declspec (dllexport) bool ReSaveImageA(char* filename, char* buff, unsigned long len)
{
	if(!buff || !len)
		return false;
	
	SDL_RWops* fp = NULL;
	fp = SDL_RWFromMem(buff, len);
	if(fp == NULL)
	{
		printf("ImageEx: Failed to read memory\n");
		if(fp)
		{
			SDL_RWclose(fp);
		}
		return false;
	}
	SDL_Surface *back = nullptr;
	back = SDL_LoadBMP_RW(fp, 0);
	if(back == nullptr)
	{
		printf("ImageEx: Failed to load from memory\n");
		if(fp)
		{
			SDL_RWclose(fp);
		}
		return false;
	}

	BITMAPINFOHEADER *bmi = (BITMAPINFOHEADER*)(buff + sizeof(BITMAPFILEHEADER));


	Uint32 rmask, gmask, bmask, amask;

#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_Surface *out = SDL_CreateRGBSurface(0, back->w, back->h, bmi->biBitCount,rmask, gmask, bmask, amask);

	SDL_Rect  backRect;
	backRect.x = 0;
	backRect.y = 0;
	backRect.w = back->w;
	backRect.h = back->h;

	SDL_BlitSurface(back, &backRect, out, NULL);

	SDL_SaveBMP(out, filename);
	SDL_FreeSurface(back);
	SDL_RWclose(fp);
	return true;
}
Exemplo n.º 6
0
SDL_Surface* get_image(void* img, int size)
{
  SDL_Surface* pic;

  pic = SDL_LoadBMP_RW(SDL_RWFromConstMem(img, size), 1);
  
  return pic;
}
Exemplo n.º 7
0
bool
Bitmap::load(unsigned id)
{
  reset();

#ifdef ENABLE_SDL
  ResourceLoader::Data data = ResourceLoader::Load(id);
  if (data.first == NULL)
    return false;

#ifdef WIN32
  const BITMAPINFO *info = (const BITMAPINFO *)data.first;
  if (data.second < sizeof(*info))
    return false;

  int pitch = (((info->bmiHeader.biWidth * info->bmiHeader.biBitCount) / 8 - 1) | 3) + 1;
  int data_size = pitch * info->bmiHeader.biHeight;

  /* duplicate the BMP file and re-insert the BITMAPFILEHEADER which
     is not included in this .EXE file */
  BITMAPFILEHEADER *header = (BITMAPFILEHEADER *)malloc(sizeof(*header) + size);
  if (header == NULL)
    /* out of memory */
    return false;

  /* byte order?  this constant is correct according to MSDN */
  header->bfType = 0x4D42;
  header->bfSize = sizeof(*header) + size;
  header->bfReserved1 = 0;
  header->bfReserved2 = 0;
  header->bfOffBits = sizeof(BITMAPFILEHEADER) + size - data_size;
  memcpy(header + 1, data.first, data.second);

  const void *bmp_data = header;
  size_t bmp_size = sizeof(*header) + size;
#else
  const void *bmp_data = data.first;
  size_t bmp_size = data.second;
#endif

  SDL_RWops *rw = SDL_RWFromConstMem(bmp_data, bmp_size);
  surface = SDL_LoadBMP_RW(rw, 1);

#ifdef WIN32
  free(header);
#endif

  return true;
#else /* !ENABLE_SDL */
  bitmap = ResourceLoader::LoadBitmap2(id);
  return bitmap != NULL;
#endif /* !ENABLE_SDL */
}
Exemplo n.º 8
0
SDL_Surface * Resources::LoadImage(PString & imageName) {
    PTRACE(4, "load image '" << imageName << "' from resources");
    SDL_Surface * imageSurface = 0;
    ResourceRO * imageFile = RetrieveRead(imageName);
    if (!imageFile)
        return NULL;
    SDL_RWops * rw = imageFile->GetSDLRWOps();
    if (!rw)
        return NULL;
    imageSurface = SDL_LoadBMP_RW(rw, 0);
    PTRACE(4, "image width: " << imageSurface->w << ", image height: " << imageSurface->h);
    return imageSurface;
}
Exemplo n.º 9
0
bool AnimationLoad(
	Animation *a, const char *filename, const int w, const int h,
	const int frameRate)
{
	SDL_Surface *tmp;
#ifdef EMBED
	SDL_RWops *rw = NULL;
#endif

#ifdef EMBED
	if (strcmp(filename,IMAGE_SPARKS) == 0) rw = SDL_RWFromMem(IMG_SPARKS, SPARKS_SIZE);
	else if (strcmp(filename,IMAGE_SPARKS_RED) == 0) rw = SDL_RWFromMem(IMG_SPARKS_RED, SPARKS_RED_SIZE);
	else if (strcmp(filename,IMAGE_TAIL) == 0) rw = SDL_RWFromMem(IMG_TAIL, TAIL_SIZE);
	else if (strcmp(filename,IMAGE_ANIM) == 0) rw = SDL_RWFromMem(IMG_ANIM, ANIM_SIZE);
	else if (strcmp(filename,IMAGE_GAMEOVER) == 0) rw = SDL_RWFromMem(IMG_GAMEOVER, GAMEOVER_SIZE);
    tmp = SDL_LoadBMP_RW(rw, 0);
    if (rw) SDL_FreeRW(rw);
#else
	tmp = SDL_LoadBMP(filename);
#endif
	
	if (strcmp(filename,IMAGE_GAMEOVER) == 0) 
	{
		SDL_SetColorKey(tmp, (SDL_SRCCOLORKEY | SDL_ACCELERATION_RLE), SDL_MapRGB(tmp->format, 255, 0, 255));
	}
	else 
	{
		SDL_SetColorKey(tmp, (SDL_SRCCOLORKEY | SDL_ACCELERATION_RLE), 0);
	}
	
	a->image = SDL_DisplayFormat(tmp);
	if (tmp) SDL_FreeSurface(tmp);

	if (a->image == NULL) 
	{
		printf("Failure to load %s\n", filename);
		goto bail;
	}
	a->w = w;
	a->h = h;
	a->frame = 0;
	a->frameCounter = 0;
	a->frameRate = frameRate;

	return true;

bail:
	AnimationFree(a);
	return false;
}
Exemplo n.º 10
0
static int texture_from_file(lua_State * L) {
	const char * filename;
	SDL_Surface * surface;
	char adjusted_filename[MAX_ADJUSTED_FILENAME_LEN];
	SDL_RWops * file;
	
	filename = luaL_checkstring(L, 1);
	prepend_data_path(adjusted_filename, filename, MAX_ADJUSTED_FILENAME_LEN);
	file = SDL_RWFromFile(adjusted_filename, "rb");
	if (!file) {
		fatal(SDL_GetError());
	}
	surface = SDL_LoadBMP_RW(file, 1);
	if (!surface) fatal(SDL_GetError()); 
	return texture_from_surface(L, surface);
}
Exemplo n.º 11
0
SDL_Surface *WadImageCache::image_from_name(std::string& name) const
{
	FileSpecifier file;
	file.SetToImageCacheDir();
	file.AddPart(name);
	
	OpenedFile of;
	if (!file.Open(of))
		return NULL;
	
#ifdef HAVE_SDL_IMAGE
	SDL_Surface *img = IMG_Load_RW(of.GetRWops(), 0);
#else
	SDL_Surface *img = SDL_LoadBMP_RW(of.GetRWops(), 0);
#endif
	return img;
}
Exemplo n.º 12
0
/*static*/ KrFontResource* KrEncoder::CreateFixedFontResource(	const char* name,
																const U8* buffer,
																int bufferSize )
{
	SDL_RWops* rw = SDL_RWFromMem( (void*) buffer, bufferSize );
	SDL_Surface* surfaceBMP = SDL_LoadBMP_RW( rw, false );
	GLASSERT( surfaceBMP );
	SDL_FreeRW( rw );

	SDL_Surface* s32 = SDL_CreateRGBSurface(	SDL_SWSURFACE,
												surfaceBMP->w,
												surfaceBMP->h,
												32,
												0xff << ( KrRGBA::RED * 8 ),
												0xff << ( KrRGBA::GREEN * 8 ),
												0xff << ( KrRGBA::BLUE * 8 ),
												0xff << ( KrRGBA::ALPHA * 8 ) );
	GLASSERT( s32 );

	// Now copy one surface to the other, 
	// set transparency as needed afterwards.
	SDL_BlitSurface( surfaceBMP, 0, s32, 0 );

	KrPaintInfo info( s32 );

	KrRGBA* rgba = (KrRGBA*) s32->pixels;
	KrRGBA  tr = *rgba;

	int count = info.width * info.height;
	for( int i=0; i<count; ++i )
	{
		if (	rgba[i].c.red == tr.c.red 
			 && rgba[i].c.green == tr.c.green 
			 && rgba[i].c.blue == tr.c.blue )

			rgba[i].all = 0;
	}

	KrFontResource* fontRes = new KrFontResource( name, &info, 
												  0, 0, 
												  KrFontResource::FIXED, 256 );	

	SDL_FreeSurface( surfaceBMP );
	SDL_FreeSurface( s32 );
	return fontRes;
}
Exemplo n.º 13
0
SDL_Surface *BKLoadSurface(const char *filename)
{
    SDL_Surface *tempSurfaceP = NULL;
    SDL_RWops *ops;

#ifdef HAVE_SDL_ZZIP
    ops = SDL_RWFromZZIP(filename, "rb");
#else
    ops = SDL_RWFromFile(filename, "rb");
#endif
    if (ops != NULL)
    {
        if ( strstr(filename, ".tga") )
        {
            tempSurfaceP = BKLoadTGA_RW(ops, 0);
            if (!tempSurfaceP) fprintf(stderr, "SDLLoadTGA %s\n", SDL_GetError());
        }
#ifdef HAVE_SDL_IMAGE
        else
        {
            tempSurfaceP = IMG_Load_RW(ops,0);
            if (!tempSurfaceP) fprintf(stderr, "IMG_Load %s\n", SDL_GetError());
        }
#else
        else if ( strstr(filename, ".bmp") )
        {
            tempSurfaceP = SDL_LoadBMP_RW(ops,0);
            if (!tempSurfaceP) fprintf(stderr, "SDL_LoadBMP %s\n", SDL_GetError());
        }
        else
        {
            SDL_SetError("Unknown file type");
        }
#endif // HAVE_SDL_IMAGE

    /*
        if (tempSurfaceP) fprintf(stderr, "loaded \"%s\" %dx%dx%d%s%s\n", filename,
            tempSurfaceP->w, tempSurfaceP->h,
            tempSurfaceP->format->BitsPerPixel,
            (tempSurfaceP->flags & SDL_SRCCOLORKEY) ? " colorkey" : "",
            (tempSurfaceP->flags & SDL_SRCALPHA) ? " alpha" : "");
    */
    
        SDL_FreeRW(ops);
    }
Exemplo n.º 14
0
// image loading helper function
SDL_Surface *load_menu_bitmap(const u8 *buffer, int filesize) 
{
	//Load the buffer into a surface using RWops
	SDL_RWops *rw = SDL_RWFromMem((void*) buffer, filesize);
	SDL_Surface *temp = SDL_LoadBMP_RW(rw, 1);

	//make it transparent
	SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 255, 0, 255)); 
 
	//Convert the image to optimal display format
	SDL_Surface *image;
	image = SDL_DisplayFormat(temp);
 
	//Free the temporary surface
	SDL_FreeSurface(temp);
 
	//Return our loaded image
	return image;
}
Exemplo n.º 15
0
Galaxy::Galaxy() : GALAXY_RADIUS(50000.0), SOL_OFFSET_X(25000.0), SOL_OFFSET_Y(0.0), m_galaxybmp(nullptr), m_factions(this), m_customSystems(this)
{
    static const std::string filename("galaxy.bmp");

    RefCountedPtr<FileSystem::FileData> filedata = FileSystem::gameDataFiles.ReadFile(filename);
    if (!filedata) {
        Output("Galaxy: couldn't load '%s'\n", filename.c_str());
        Pi::Quit();
    }

    SDL_RWops *datastream = SDL_RWFromConstMem(filedata->GetData(), filedata->GetSize());
    m_galaxybmp = SDL_LoadBMP_RW(datastream, 1);
    if (!m_galaxybmp) {
        Output("Galaxy: couldn't load: %s (%s)\n", filename.c_str(), SDL_GetError());
        Pi::Quit();
    }

    m_starSystemCache = m_starSystemAttic.NewSlaveCache();
}
Exemplo n.º 16
0
/**
 * Generates a pre-defined Codepage 437 (MS-DOS terminal) font.
 */
void Font::loadTerminal()
{
	FontImage image;
	image.width = 9;
	image.height = 16;
	image.spacing = 0;
	_monospace = true;

	SDL_RWops *rw = SDL_RWFromConstMem(dosFont, DOSFONT_SIZE);
	SDL_Surface *s = SDL_LoadBMP_RW(rw, 0);
	SDL_FreeRW(rw);
	image.surface = new Surface(s->w, s->h);
	SDL_Color terminal[2] = {{0, 0, 0, 0}, {185, 185, 185, 255}};
	image.surface->setPalette(terminal, 0, 2);
	SDL_BlitSurface(s, 0, image.surface->getSurface(), 0);
	SDL_FreeSurface(s);
	_images.push_back(image);

	init(0, L" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");
}
Exemplo n.º 17
0
/**
 * Generates a pre-defined Codepage 437 (MS-DOS terminal) font.
 */
void Font::loadTerminal()
{
    _width = 9;
    _height = 16;
    _spacing = 0;
    _monospace = true;

    SDL_RWops *rw = SDL_RWFromConstMem(dosFont, DOSFONT_SIZE);
    SDL_Surface *s = SDL_LoadBMP_RW(rw, 0);
    SDL_FreeRW(rw);
    _surface = new Surface(s->w, s->h);
    SDL_Color terminal[2] = {{0, 0, 0, 0}, {185, 185, 185, 255}};
    _surface->setPalette(terminal, 0, 2);
    SDL_BlitSurface(s, 0, _surface->getSurface(), 0);
    SDL_FreeSurface(s);

    std::wstring temp = _index;
    _index = L" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
    init();
    _index = temp;
}
void WindowManager::setWindowIcon(const unsigned char *data, const int size) const
{
	// prepare data buffer structure
	SDL_RWops *buffer = SDL_RWFromMem((void*) data, size);

	if(buffer != NULL) {
		// load BMP from prepared data buffer
		SDL_Surface *surface = SDL_LoadBMP_RW(buffer, 1);

		if(surface != NULL) {
			// set window icon
			SDL_WM_SetIcon(surface, NULL);
			SDL_FreeSurface(surface);
		}
		else {
			cerr << "Could not create window icon surface: " << SDL_GetError() << endl;
		}
	}
	else {
		cerr << "Could not prepare window icon data: " << SDL_GetError() << endl;
	}
}
Exemplo n.º 19
0
void pong_main(void)
{
    Pong game;
    SDL_Window *window = NULL;

    //initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        fprintf(stderr, "SDL not initialized: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    //create the window
    window = SDL_CreateWindow("Pong", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
    if (!window)
    {
        fprintf(stderr, "SDL could not create window: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    //create a hardware accelerated renderer for the window
    game.renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);

    //create the font texture
    SDL_RWops *s = SDL_RWFromMem(assets_font, assets_font_size);
    if (!s)
    {
        fprintf(stderr, "Unable to load font data: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_Surface *font = SDL_LoadBMP_RW(s, 1); //we close the stream automatically
    game.font = SDL_CreateTextureFromSurface(game.renderer, font);
    if (!game.font)
    {
        fprintf(stderr, "Unable to create font texture: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_FreeSurface(font);

    //create the sprites texture
    s = SDL_RWFromMem(assets_sprites, assets_sprites_size);
    if (!s)
    {
        fprintf(stderr, "Unable to load sprite data: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_Surface *sprites = SDL_LoadBMP_RW(s, 1); //we close the stream automatically
    game.sprites = SDL_CreateTextureFromSurface(game.renderer, sprites);
    if (!game.sprites)
    {
        fprintf(stderr, "Unable to create sprites texture: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_FreeSurface(sprites);

    //initial game settings
    game.player1Score = 0;
    game.player2Score = 0;
    game.ball.x = WINDOW_WIDTH / 2;
    game.ball.y = WINDOW_HEIGHT / 2;
    game.ballDir = BALL_DEFAULT_DIR;
    paddle_init(&game.player1, &game.player2);

    //main game loop
    while(1)
    {
        if (!handle_events())
        {
            break;
        }

        //perform the game logic
        pong_tick(&game);

        //render the game
        pong_render(&game);
    }

    //destroy our window and associated resources
    SDL_DestroyTexture(game.font);
    SDL_DestroyRenderer(game.renderer);
    SDL_DestroyWindow(window);

    //all done!
    SDL_Quit();
    exit(EXIT_SUCCESS);
}
Exemplo n.º 20
0
int PDC_scr_open(int argc, char **argv)
{
    PDC_LOG(("PDC_scr_open() - called\n"));

    SP = calloc(1, sizeof(SCREEN));

    if (!SP)
        return ERR;

    pdc_own_screen = !pdc_screen;

    if (pdc_own_screen)
    {
        if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0)
        {
            fprintf(stderr, "Could not start SDL: %s\n", SDL_GetError());
            return ERR;
        }

        atexit(_clean);
    }

#ifdef PDC_WIDE
    if (!pdc_ttffont)
    {
        const char *ptsz, *fname;

        if (TTF_Init() == -1)
        {
            fprintf(stderr, "Could not start SDL_TTF: %s\n", SDL_GetError());
            return ERR;
        }

        ptsz = getenv("PDC_FONT_SIZE");
        if (ptsz != NULL)
           pdc_font_size = atoi(ptsz);
        if (pdc_font_size <= 0)
           pdc_font_size = 18;

        fname = getenv("PDC_FONT");
        pdc_ttffont = TTF_OpenFont(fname ? fname : PDC_FONT_PATH,
                                   pdc_font_size);
    }

    if (!pdc_ttffont)
    {
        fprintf(stderr, "Could not load font\n");
        return ERR;
    }

    TTF_SetFontKerning(pdc_ttffont, 0);
    TTF_SetFontHinting(pdc_ttffont, TTF_HINTING_MONO);

    SP->mono = FALSE;
#else
    if (!pdc_font)
    {
        const char *fname = getenv("PDC_FONT");
        pdc_font = SDL_LoadBMP(fname ? fname : "pdcfont.bmp");
    }

    if (!pdc_font)
        pdc_font = SDL_LoadBMP_RW(SDL_RWFromMem(font437, sizeof(font437)), 0);

    if (!pdc_font)
    {
        fprintf(stderr, "Could not load font\n");
        return ERR;
    }

    SP->mono = !pdc_font->format->palette;
#endif

    if (!SP->mono && !pdc_back)
    {
        const char *bname = getenv("PDC_BACKGROUND");
        pdc_back = SDL_LoadBMP(bname ? bname : "pdcback.bmp");
    }

    if (!SP->mono && (pdc_back || !pdc_own_screen))
    {
        SP->orig_attr = TRUE;
        SP->orig_fore = COLOR_WHITE;
        SP->orig_back = -1;
    }
    else
        SP->orig_attr = FALSE;

#ifdef PDC_WIDE
    TTF_SizeText(pdc_ttffont, "W", &pdc_fwidth, &pdc_fheight);
    pdc_fthick = pdc_font_size / 20 + 1;
#else
    pdc_fheight = pdc_font->h / 8;
    pdc_fwidth = pdc_font->w / 32;
    pdc_fthick = 1;

    if (!SP->mono)
        pdc_flastc = pdc_font->format->palette->ncolors - 1;
#endif

    if (pdc_own_screen && !pdc_icon)
    {
        const char *iname = getenv("PDC_ICON");
        pdc_icon = SDL_LoadBMP(iname ? iname : "pdcicon.bmp");

        if (!pdc_icon)
            pdc_icon = SDL_LoadBMP_RW(SDL_RWFromMem(iconbmp,
                                                    sizeof(iconbmp)), 0);

        if (pdc_icon)
            SDL_WM_SetIcon(pdc_icon, NULL);
    }

    if (pdc_own_screen)
    {
        const SDL_VideoInfo *info = SDL_GetVideoInfo();
        max_height = info->current_h;
        max_width = info->current_w;

        const char *env = getenv("PDC_LINES");
        pdc_sheight = (env ? atoi(env) : 25) * pdc_fheight;

        env = getenv("PDC_COLS");
        pdc_swidth = (env ? atoi(env) : 80) * pdc_fwidth;

        pdc_screen = SDL_SetVideoMode(pdc_swidth, pdc_sheight, 0,
            SDL_SWSURFACE|SDL_ANYFORMAT|SDL_RESIZABLE);
    }
    else
    {
        if (!pdc_sheight)
            pdc_sheight = pdc_screen->h - pdc_yoffset;

        if (!pdc_swidth)
            pdc_swidth = pdc_screen->w - pdc_xoffset;
    }

    if (!pdc_screen)
    {
        fprintf(stderr, "Couldn't create a surface: %s\n", SDL_GetError());
        return ERR;
    }

    if (SP->orig_attr)
        PDC_retile();

    _initialize_colors();

    SDL_EnableUNICODE(1);

    PDC_mouse_set();

    if (pdc_own_screen)
        PDC_set_title(argc ? argv[0] : "PDCurses");

    SP->lines = PDC_get_rows();
    SP->cols = PDC_get_columns();

    SP->mouse_wait = PDC_CLICK_PERIOD;
    SP->audible = FALSE;

    SP->termattrs = A_COLOR | A_UNDERLINE | A_LEFT | A_RIGHT | A_REVERSE;
#ifdef PDC_WIDE
    SP->termattrs |= A_ITALIC;
#endif

    PDC_reset_prog_mode();

    return OK;
}
Exemplo n.º 21
0
int PDC_scr_open(int argc, char **argv)
{
    int i;

    PDC_LOG(("PDC_scr_open() - called\n"));

    SP = calloc(1, sizeof(SCREEN));

    if (!SP)
        return ERR;

    pdc_own_screen = !pdc_screen;

    if (pdc_own_screen)
    {
        if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0)
        {
            fprintf(stderr, "Could not start SDL: %s\n", SDL_GetError());
            return ERR;
        }

        atexit(SDL_Quit);
    }

    if (!pdc_font)
    {
        const char *fname = getenv("PDC_FONT");
        pdc_font = SDL_LoadBMP(fname ? fname : "pdcfont.bmp");
    }

    if (!pdc_font)
        pdc_font = SDL_LoadBMP_RW(SDL_RWFromMem(deffont, sizeof(deffont)), 0);

    if (!pdc_font)
    {
        fprintf(stderr, "Could not load font\n");
        return ERR;
    }

    SP->mono = !pdc_font->format->palette;

    if (!SP->mono && !pdc_back)
    {
        const char *bname = getenv("PDC_BACKGROUND");
        pdc_back = SDL_LoadBMP(bname ? bname : "pdcback.bmp");
    }

    if (!SP->mono && (pdc_back || !pdc_own_screen))
    {
        SP->orig_attr = TRUE;
        SP->orig_fore = COLOR_WHITE;
        SP->orig_back = -1;
    }
    else
        SP->orig_attr = FALSE;

    pdc_fheight = pdc_font->h / 8;
    pdc_fwidth = pdc_font->w / 32;

    if (!SP->mono)
        pdc_flastc = pdc_font->format->palette->ncolors - 1;

    if (pdc_own_screen && !pdc_icon)
    {
        const char *iname = getenv("PDC_ICON");
        pdc_icon = SDL_LoadBMP(iname ? iname : "pdcicon.bmp");

        if (!pdc_icon)
            pdc_icon = SDL_LoadBMP_RW(SDL_RWFromMem(deficon,
                                                    sizeof(deficon)), 0);

        if (pdc_icon)
            SDL_WM_SetIcon(pdc_icon, NULL);
    }

    if (pdc_own_screen)
    {
        const char *env = getenv("PDC_LINES");
        pdc_sheight = (env ? atoi(env) : 25) * pdc_fheight;

        env = getenv("PDC_COLS");
        pdc_swidth = (env ? atoi(env) : 80) * pdc_fwidth;

        pdc_screen = SDL_SetVideoMode(pdc_swidth, pdc_sheight, 0,
            SDL_SWSURFACE|SDL_ANYFORMAT|SDL_RESIZABLE);
    }
    else
    {
        if (!pdc_sheight)
            pdc_sheight = pdc_screen->h - pdc_yoffset;

        if (!pdc_swidth)
            pdc_swidth = pdc_screen->w - pdc_xoffset;
    }

    if (!pdc_screen)
    {
        fprintf(stderr, "Couldn't create a surface: %s\n", SDL_GetError());
        return ERR;
    }

    if (SP->orig_attr)
        PDC_retile();

    for (i = 0; i < 8; i++)
    {
        pdc_color[i].r = (i & COLOR_RED) ? 0xc0 : 0;
        pdc_color[i].g = (i & COLOR_GREEN) ? 0xc0 : 0;
        pdc_color[i].b = (i & COLOR_BLUE) ? 0xc0 : 0;

        pdc_color[i + 8].r = (i & COLOR_RED) ? 0xff : 0x40;
        pdc_color[i + 8].g = (i & COLOR_GREEN) ? 0xff : 0x40;
        pdc_color[i + 8].b = (i & COLOR_BLUE) ? 0xff : 0x40;
    }

    for (i = 0; i < 16; i++)
        pdc_mapped[i] = SDL_MapRGB(pdc_screen->format, pdc_color[i].r,
                                   pdc_color[i].g, pdc_color[i].b);

    SDL_EnableUNICODE(1);

    PDC_mouse_set();

    if (pdc_own_screen)
        PDC_set_title(argc ? argv[0] : "PDCurses");

    SP->lines = PDC_get_rows();
    SP->cols = PDC_get_columns();

    SP->mouse_wait = PDC_CLICK_PERIOD;
    SP->audible = FALSE;

    PDC_reset_prog_mode();

    return OK;
}
Exemplo n.º 22
0
// load the surface from a .pbm or bitmap file
bool NXSurface::LoadImage(const char *pbm_name, bool use_colorkey, int use_display_format)
{
	stat("NXSurface::LoadImage name = %s, this = %p", pbm_name, this);

	Free();
	
	// if (use_display_format == -1)
	// {	// use value specified in settings
	// 	use_display_format = settings->displayformat;
	// }
	

	SDL_RWops* rwops = fileopen_SDL_RWops_RO(pbm_name);
	if (!rwops) { staterr("NXSurface::LoadImage: load failed of '%s'! %s", pbm_name, SDL_GetError()); return 1; }
	SDL_Surface *image = SDL_LoadBMP_RW(rwops, 1);
	if (!image) { staterr("NXSurface::LoadImage: load failed of '%s'! %s", pbm_name, SDL_GetError()); return 1; }
	
	if (use_colorkey)
	{
		SDL_SetColorKey(image, SDL_TRUE, SDL_MapRGB(image->format, 0, 0, 0));
	}

	SDL_Texture * tmptex = SDL_CreateTextureFromSurface(renderer, image);
	if (!tmptex)
	{
		staterr("NXSurface::LoadImage: SDL_CreateTextureFromSurface failed: %s", SDL_GetError());
		SDL_FreeSurface(image);
		return 1;
	}

	SDL_FreeSurface(image);

	{
		int wd, ht, access;
		Uint32 format;
		NXFormat nxformat;
		if (SDL_QueryTexture(tmptex, &format, &access, &wd, &ht)) goto error;
		nxformat.format = format;
		if (AllocNew(wd, ht, &nxformat)) goto error;
		if (SDL_SetTextureBlendMode(tmptex, SDL_BLENDMODE_NONE))	goto error;
		if (SDL_SetRenderTarget(renderer, fTexture)) goto error;
		if (SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255)) goto error;
		if (SDL_RenderClear(renderer)) goto error;
		if (SDL_RenderCopy(renderer, tmptex, NULL, NULL)) goto error;
		if (SDL_SetRenderTarget(renderer, NULL)) goto error;
		if (SDL_SetTextureBlendMode(fTexture, SDL_BLENDMODE_BLEND)) goto error;

		SDL_DestroyTexture(tmptex);

		goto done;
error:
		{
			staterr("NXSurface::LoadImage failed: %s", SDL_GetError());
			if (tmptex)  { SDL_DestroyTexture(tmptex); tmptex = NULL; }
			if (fTexture){ SDL_DestroyTexture(fTexture); fTexture = NULL; }
			SDL_SetRenderTarget(renderer, NULL);
		}
done:
		;
	}

	stat("NXSurface::LoadImage name = %s, this = %p done", pbm_name, this);

	return (fTexture == NULL);
}
Exemplo n.º 23
0
Resource* SDL_Surface_Image::LoadBMP(File* file) {
	return new SDL_Surface_Image(SDL_LoadBMP_RW(GetRWOps(file), 1));
}
Exemplo n.º 24
0
 *
 * Load a BMP image by RW operations, then return the surface.
 *
 * Params:
 *	rwops the RWOps object created with SDL.RWCreate
 *
 * Returns:
 *	The surface or nil
 *	The error message
 */
static int l_surface_loadBMP_RW(lua_State *L)
{
	SDL_RWops *ops = commonGetAs(L, 1, RWOpsName, SDL_RWops *);
	SDL_Surface *s;

	s = SDL_LoadBMP_RW(ops, 0);
	if (s == NULL)
		return commonPushSDLError(L, 1);

	return commonPush(L, "p", SurfaceName, s);
}

const luaL_Reg SurfaceFunctions[] = {
	{ "createRGBSurface",		l_surface_createRGB	},
	{ "loadBMP",			l_surface_loadBMP	},
	{ "loadBMP_RW",			l_surface_loadBMP_RW	},
#if 0
	{ "createRGBSurfaceFrom",	l_surface_createRGBFrom	},
#endif
	{ NULL,				NULL			}
};
Exemplo n.º 25
0
bool font_init(void)
{
bool error = false;

	// we'll be bypassing the NXSurface automatic scaling features
	// and drawing at the real resolution so we can get better-looking fonts.
//	sdl_screen = screen->GetSDLSurface();
	
	// at 320x240 switch to bitmap fonts for better appearance
	#ifdef CONFIG_ENABLE_TTF
	if (SCALE == 1)
	#endif
	{
		stat("fonts: using bitmapped from %s", bmpfontfile);
		
		SDL_RWops* rwops = fileopen_SDL_RWops_RO(bmpfontfile);
		if (!rwops) { staterr("Couldn't open bitmap font file %s: SDL_RWFromFP: %s", bmpfontfile, SDL_GetError()); return 1; }
		SDL_Surface *sheet = SDL_LoadBMP_RW(rwops, 1);
		if (!sheet) { staterr("Couldn't open bitmap font file: SDL_LoadBMP_RW: %s", SDL_GetError()); return 1; }
		
		uint32_t fgindex = SDL_MapRGB(sheet->format, 255, 255, 255);
		
		error = error || whitefont.InitBitmapChars(sheet, fgindex, 0xffffff);
		error = error || greenfont.InitBitmapChars(sheet, fgindex, 0x00ff80);
		error = error || bluefont.InitBitmapChars(sheet, fgindex, 0xa0b5de);
		error = error || shadowfont.InitBitmapCharsShadowed(sheet, fgindex, 0xffffff, 0x000000);
	}
	#ifdef CONFIG_ENABLE_TTF
	else
	{
		// It will get size 8, 17, 26, 35, ...
		// Hope ot will look nice on higher resolutions
		int pointsize = 8 + 9 * (SCALE - 1);
        
		stat("fonts: using truetype at %dpt", pointsize);

		// initilize normal TTF fonts
		if (TTF_Init() < 0)
		{
			staterr("Couldn't initialize SDL_ttf: %s", TTF_GetError());
			return 1;
		}
		
		TTF_Font *font = TTF_OpenFontRW(SDL_RWFromFP(fileopenRO(ttffontfile), SDL_TRUE), 1, pointsize);
		if (!font)
		{
			staterr("Couldn't open font: '%s'", ttffontfile);
			return 1;
		}
		
		error = error || whitefont.InitChars(font, 0xffffff);
		error = error || greenfont.InitChars(font, 0x00ff80);
		error = error || bluefont.InitChars(font, 0xa0b5de);
		error = error || shadowfont.InitCharsShadowed(font, 0xffffff, 0x000000);
		
		TTF_CloseFont(font);
	}
	#endif

	error = error || whitefont.InitTextures();
	error = error || greenfont.InitTextures();
	error = error || bluefont.InitTextures();
	error = error || shadowfont.InitTextures();
	
	error = error || create_shade_sfc();
	if (error) return 1;
	
	fontheight = (whitefont.letters['M']->h / SCALE);
	initilized = true;
	return 0;
}