Exemple #1
0
static void
SW_DestroyRenderer(SDL_Renderer * renderer)
{
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
    int i;

    if (data) {
        for (i = 0; i < SDL_arraysize(data->texture); ++i) {
            if (data->texture[i]) {
                DestroyTexture(data->renderer, data->texture[i]);
            }
        }
        if (data->surface.format) {
            SDL_SetSurfacePalette(&data->surface, NULL);
            SDL_FreeFormat(data->surface.format);
        }
        if (display->palette) {
            SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
                                data);
        }
        if (data->renderer) {
            data->renderer->DestroyRenderer(data->renderer);
        }
        SDL_FreeDirtyRects(&data->dirty);
        SDL_free(data);
    }
    SDL_free(renderer);
}
Exemple #2
0
SDL_Surface*
Blitter::create_surface_from_format(SDL_Surface* surface, int w, int h)
{
  SDL_Surface* new_surface = SDL_CreateRGBSurface(0, w, h,
                                                  surface->format->BitsPerPixel, 
                                                  surface->format->Rmask,
                                                  surface->format->Gmask,
                                                  surface->format->Bmask,
                                                  surface->format->Amask);

  Uint8 alpha;
  if (SDL_GetSurfaceAlphaMod(surface, &alpha) == 0)
    SDL_SetSurfaceAlphaMod(new_surface, alpha);

  SDL_BlendMode blend_mode;
  if (SDL_GetSurfaceBlendMode(surface, &blend_mode) == 0)
    SDL_SetSurfaceBlendMode(new_surface, blend_mode);

  Uint8 r, g, b;
  if (SDL_GetSurfaceColorMod(surface, &r, &g, &b) == 0)
    SDL_SetSurfaceColorMod(new_surface, r, g, b);

  if (surface->format->palette)
    SDL_SetSurfacePalette(new_surface, surface->format->palette);

  Uint32 colorkey;
  if (SDL_GetColorKey(surface, &colorkey) == 0)
    SDL_SetColorKey(new_surface, SDL_TRUE, colorkey);

  return new_surface;
}
Exemple #3
0
/*
 * Free a surface created by the above function.
 */
void
SDL_FreeSurface(SDL_Surface * surface)
{
    if (surface == NULL) {
        return;
    }
    if (surface->flags & SDL_DONTFREE) {
        return;
    }
    if (--surface->refcount > 0) {
        return;
    }
    while (surface->locked > 0) {
        SDL_UnlockSurface(surface);
    }
    if (surface->flags & SDL_RLEACCEL) {
        SDL_UnRLESurface(surface, 0);
    }
    if (surface->format) {
        SDL_SetSurfacePalette(surface, NULL);
        SDL_FreeFormat(surface->format);
        surface->format = NULL;
    }
    if (surface->map != NULL) {
        SDL_FreeBlitMap(surface->map);
        surface->map = NULL;
    }
    if (!(surface->flags & SDL_PREALLOC)) {
        SDL_free(surface->pixels);
    }
    SDL_free(surface);
}
Exemple #4
0
/*++
 Load the screen background picture of the battle.
 --*/
static VOID PAL_LoadBattleBackground(void)
{
    PAL_LARGE BYTE           buf[320 * 200];
    
    //
    // Create the surface
    //
    g_Battle.lpBackground =
    SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
                         gpScreen->format->Rmask, gpScreen->format->Gmask,
                         gpScreen->format->Bmask, gpScreen->format->Amask);
    
    if (g_Battle.lpBackground == NULL)
    {
        TerminateOnError("PAL_LoadBattleBackground(): failed to create surface!");
    }
#if SDL_VERSION_ATLEAST(2, 0, 0)
    SDL_SetSurfacePalette(g_Battle.lpBackground, gpScreen->format->palette);
#else
    SDL_SetPalette(g_Battle.lpBackground, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
#endif
    //
    // Load the picture
    //
    PAL_MKFDecompressChunk(buf, 320 * 200, gpGlobals->wNumBattleField, gpGlobals->f.fpFBP);
    
    //
    // Draw the picture to the surface.
    //
    PAL_FBPBlitToSurface(buf, g_Battle.lpBackground);
}
Exemple #5
0
int display_init() {
	memset(&display, 0, sizeof(display));

	if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
		return 0;

	display.window = SDL_CreateWindow("Gamebuino",
			SDL_WINDOWPOS_UNDEFINED,
			SDL_WINDOWPOS_UNDEFINED,
			84*DISPLAY_SCALE_X, 48*DISPLAY_SCALE_Y, 0);
	if (display.window == NULL)
		return 0;

	display.renderer = SDL_CreateRenderer(display.window, -1,
			SDL_RENDERER_PRESENTVSYNC);

	display.texture = SDL_CreateTexture(display.renderer,
			SDL_PIXELFORMAT_RGB332,
			SDL_TEXTUREACCESS_STREAMING, 84, 48);
	if (display.texture == NULL)
		return 0;

	SDL_Surface* numbers_surface = SDL_CreateRGBSurfaceFrom((void*) numbers_bitmap,
			numbers_bitmap_width, numbers_bitmap_height, 1, numbers_bitmap_width / 8,
			0x80000000, 0x80000000, 0x80000000, 0xFFFFFFFF);
	SDL_Palette* palette = SDL_AllocPalette(2);
	SDL_SetPaletteColors(palette, numbers_palette_colors, 0, 2);
	SDL_SetSurfacePalette(numbers_surface, palette);
	display.numbers_texture = SDL_CreateTextureFromSurface(display.renderer, numbers_surface);
	if (display.numbers_texture == 0)
		return 0;
	SDL_FreeSurface(numbers_surface);

	return 1;
}
TexturePtr
TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
{
  SDL_Surface *image = nullptr;

  Surfaces::iterator i = m_surfaces.find(filename);
  if (i != m_surfaces.end())
  {
    image = i->second;
  }
  else
  {
    image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
    if (!image)
    {
      std::ostringstream msg;
      msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
      throw std::runtime_error(msg.str());
    }

    m_surfaces[filename] = image;
  }

  SDL_PixelFormat* format = image->format;
  if(format->Rmask == 0 && format->Gmask == 0 && format->Bmask == 0 && format->Amask == 0) {
    log_debug << "Wrong surface format for image " << filename << ". Compensating." << std::endl;
    image = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_RGBA8888, 0);
  }

  SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
                                                  rect.top * image->pitch +
                                                  rect.left * image->format->BytesPerPixel,
                                                  rect.get_width(), rect.get_height(),
                                                  image->format->BitsPerPixel,
                                                  image->pitch,
                                                  image->format->Rmask,
                                                  image->format->Gmask,
                                                  image->format->Bmask,
                                                  image->format->Amask));
  if (!subimage)
  {
    throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
  }

#ifdef OLD_SDL
  if (image->format->palette)
  { // copy the image palette to subimage if present
    SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors);
  }
#endif

  return VideoSystem::current()->new_texture(subimage.get());
}
Exemple #7
0
static void osinterface_resize(int width, int height)
{
	rct_drawpixelinfo *screenDPI;
	int newScreenBufferSize;
	void *newScreenBuffer;

	if (_surface != NULL)
		SDL_FreeSurface(_surface);
	if (_palette != NULL)
		SDL_FreePalette(_palette);

	_surface = SDL_CreateRGBSurface(0, width, height, 8, 0, 0, 0, 0);
	_palette = SDL_AllocPalette(256);

	SDL_SetSurfacePalette(_surface, _palette);

	newScreenBufferSize = _surface->pitch * _surface->h;
	newScreenBuffer = malloc(newScreenBufferSize);
	if (_screenBuffer == NULL) {
		memset(newScreenBuffer, 0, newScreenBufferSize);
	} else {
		memcpy(newScreenBuffer, _screenBuffer, min(_screenBufferSize, newScreenBufferSize));
		if (newScreenBufferSize - _screenBufferSize > 0)
			memset((uint8*)newScreenBuffer + _screenBufferSize, 0, newScreenBufferSize - _screenBufferSize);
		free(_screenBuffer);
	}

	_screenBuffer = newScreenBuffer;
	_screenBufferSize = newScreenBufferSize;

	RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_WIDTH, sint16) = width;
	RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_HEIGHT, sint16) = height;

	screenDPI = RCT2_ADDRESS(RCT2_ADDRESS_SCREEN_DPI, rct_drawpixelinfo);
	screenDPI->bits = _screenBuffer;
	screenDPI->x = 0;
	screenDPI->y = 0;
	screenDPI->width = width;
	screenDPI->height = height;
	screenDPI->pitch = _surface->pitch - _surface->w;

	RCT2_GLOBAL(0x009ABDF0, uint8) = 6;
	RCT2_GLOBAL(0x009ABDF1, uint8) = 3;
	RCT2_GLOBAL(0x009ABDF2, uint8) = 1;
	RCT2_GLOBAL(RCT2_ADDRESS_DIRTY_BLOCK_WIDTH, sint16) = 64;
	RCT2_GLOBAL(RCT2_ADDRESS_DIRTY_BLOCK_HEIGHT, sint16) = 8;
	RCT2_GLOBAL(RCT2_ADDRESS_DIRTY_BLOCK_COLUMNS, sint32) = (width >> 6) + 1;
	RCT2_GLOBAL(RCT2_ADDRESS_DIRTY_BLOCK_ROWS, sint32) = (height >> 3) + 1;

	RCT2_CALLPROC_EBPSAFE(0x0066B905); // resize_gui()
	gfx_invalidate_screen();
}
Exemple #8
0
void EmuinoSDL::renderTiles()
{
    uint8_t *addr;
    for (int i = 0; i < NUM_TILEMAPS; i++) {
        TileLayerInfo layer_info;
        
        SharedRAM.read(TILE_LAYER_REG(i, TILE_CTRL_0) | 0x8000, &layer_info.ctrl0, 2);
        SharedRAM.read(TILE_LAYER_REG(i, TILE_EMPTY_VALUE) | 0x8000, &layer_info.empty, 2);
        SharedRAM.read(TILE_LAYER_REG(i, TILE_COLOR_KEY) | 0x8000, &layer_info.color_key, 2);
        SharedRAM.read(TILE_LAYER_REG(i, TILE_DATA_OFFSET) | 0x8000, &layer_info.offset, 2);
        
        // clear map surface
        memset(tileMapSurfaces[i]->pixels, 0, tileMapSurfaces[i]->pitch * tileMapSurfaces[i]->h);
        
        if (layer_info.ctrl0 & (1 << TILE_LAYER_ENABLED)) {
            addr = SharedRAM[(TILEMAP_BANK * (0x4000) + 0x8000) + (0x800 * i) - 1];
            for (int y = 0; y < 32; y++) {
                for (int x = 0; x < 32; x++) {
                    uint16_t entry;
                    SharedRAM.read((TILEMAP_BANK * (0x4000) + 0x8000) + (0x800 * i) + (2*(y*32+x)), &entry, 2);
                    uint16_t tile = entry & ~(1 << 15 | 1 << 14 | 1 << 13);
                    
                    //printf("\t0x%x 0x%x\n", entry, tile);
                    
                    if ( tile != layer_info.empty ) {
                    
                        int paletteIndex = (layer_info.ctrl0 >> TILE_PALETTE_START) & 0xFFFF;
                        
                        char tileCopy[16*16];
                        memcpy(tileCopy, SharedRAM[(VRAM_BANK_BEGIN * (0x4000) + layer_info.offset + 0x8000) + (tile * 16*16)], 16*16);
                        
                        SDL_Surface *tileSurface = SDL_CreateRGBSurfaceFrom(tileCopy, 16, 16, 8, 16, 0, 0, 0, 0);
                        
                        flip(tileSurface, entry & (1 << 13), entry & (1 << 14), entry & (1 << 15));
                        
                        SDL_SetSurfacePalette(tileSurface, palettes[paletteIndex]);
                        SDL_SetColorKey(tileSurface, SDL_TRUE, layer_info.color_key);
                        SDL_Rect dst = {x * 16, y * 16, 0, 0};
                        SDL_BlitSurface(tileSurface, NULL, tileMapSurfaces[i], &dst);
                        SDL_FreeSurface(tileSurface);
                        
                    }

                    /*if (entry & (1 << 14))
                        std::cout << "flip y" << std::endl;
                    if (entry & (1 << 15))
                        std::cout << "flip diag" << std::endl;*/
                }
            }
            SDL_UpdateTexture(tileMapTextures[i], NULL, tileMapSurfaces[i]->pixels, tileMapSurfaces[i]->pitch);
        }
Exemple #9
0
static void VL_SDL2_RefreshPaletteAndBorderColor(void *screen)
{
	SDL_Surface *surf = (SDL_Surface *)screen;
	static SDL_Color sdl2_palette[16];

	for (int i = 0; i < 16; i++)
	{
		sdl2_palette[i].r = VL_EGARGBColorTable[vl_emuegavgaadapter.palette[i]][0];
		sdl2_palette[i].g = VL_EGARGBColorTable[vl_emuegavgaadapter.palette[i]][1];
		sdl2_palette[i].b = VL_EGARGBColorTable[vl_emuegavgaadapter.palette[i]][2];
	}
	SDL_SetPaletteColors(vl_sdl2_palette, sdl2_palette, 0, 16);
	SDL_SetSurfacePalette(surf, vl_sdl2_palette);
}
Exemple #10
0
void SDLImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal)
{
	//Init image
	image->surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SpriteSize.x, SpriteSize.y, 8, 0, 0, 0, 0);
	image->margins  = Margins;
	image->fullSize = FullSize;

	//Prepare surface
	SDL_Palette * p = SDL_AllocPalette(256);
	SDL_SetPaletteColors(p, pal, 0, 256);
	SDL_SetSurfacePalette(image->surf, p);
	SDL_FreePalette(p);

	SDL_LockSurface(image->surf);
	lineStart = position = (ui8*)image->surf->pixels;
}
s_videomodes setupPreBlitProcessing(s_videomodes videomodes)
{
	bytes_per_pixel = videomodes.pixel;
	
	if(screen) { SDL_FreeSurface(screen); screen=NULL; }
	if(bscreen) { SDL_FreeSurface(bscreen); bscreen=NULL; }
	if(bscreen2) { SDL_FreeSurface(bscreen2); bscreen2=NULL; }
	
	// set scale factors to 1 by default
	videomodes.hScale = savedata.glscale;
	videomodes.vScale = savedata.glscale;
	
	// set up indexed to RGB conversion
	if(videomodes.pixel == 1)
	{
		bscreen = SDL_CreateRGBSurface(0, videomodes.hRes, videomodes.vRes, 8, 0,0,0,0);
		screen = SDL_CreateRGBSurface(0, videomodes.hRes, videomodes.vRes, 32, masks[3][0], masks[3][1], masks[3][2], masks[3][3]);
		if(!screenPalette) screenPalette = SDL_AllocPalette(256);
		SDL_SetSurfacePalette(bscreen, screenPalette);
		videomodes.pixel = 4;
	}
	
	// set up software scaling
	if(savedata.swfilter && (savedata.glscale >= 2.0 || savedata.fullscreen))
	{
		if (screen) SDL_FreeSurface(screen);
		screen = SDL_CreateRGBSurface(0, videomodes.hRes*2, videomodes.vRes*2, 16, masks[1][0], masks[1][1], masks[1][2], masks[1][3]);
		if (!bscreen) bscreen = SDL_CreateRGBSurface(0, videomodes.hRes, videomodes.vRes, 8*bytes_per_pixel, masks[bytes_per_pixel-1][0], masks[bytes_per_pixel-1][1], masks[bytes_per_pixel-1][2], masks[bytes_per_pixel-1][3]); // 24bit mask
		bscreen2 = SDL_CreateRGBSurface(0, videomodes.hRes+4, videomodes.vRes+8, 16, masks[1][0], masks[1][1], masks[1][2], masks[1][3]);
		Init_Gfx(565, 16);
		memset(pDeltaBuffer, 0x00, 1244160);
		
		assert(bscreen);
		assert(bscreen2);

		videomodes.hRes *= 2;
		videomodes.vRes *= 2;
		videomodes.hScale /= 2;
		videomodes.vScale /= 2;
		videomodes.pixel = 2;
	}
	
	return videomodes;
}
Exemple #12
0
SDL_Surface*
Blitter::scale_surface(SDL_Surface* surface, int width, int height)
{
  int i;
  int j;
  unsigned char *pixels;
  unsigned char *new_pixels;
  int x;
  int bpp;
  int new_pitch;
  SDL_Surface* new_surface;

  bpp = surface->format->BytesPerPixel;
  if (bpp == 1) {
    SDL_Palette* pal = SDL_AllocPalette(256);

    Uint32 ckey;
    int useckey = 0;

    useckey = SDL_GetColorKey(surface, &ckey) == 0;

    new_surface = SDL_CreateRGBSurface(0, width, height, 8, 0, 0, 0, 0);

    SDL_LockSurface(surface);
    SDL_LockSurface(new_surface);

    pixels     = static_cast<unsigned char*>(surface->pixels);
    new_pixels = static_cast<unsigned char*>(new_surface->pixels);
    new_pitch  = new_surface->pitch;

    memcpy(pal->colors, surface->format->palette->colors, sizeof(SDL_Color) * 256);

    for (i = 0; i < height; ++i) {
      x = i * new_pitch;
      for (j = 0; j < width; ++j) {
        new_pixels[x] = pixels[(i * surface->h / height) * surface->pitch + j * surface->w / width];
        ++x;
      }
    }

    SDL_UnlockSurface(surface);
    SDL_UnlockSurface(new_surface);

    SDL_SetSurfacePalette(new_surface, pal);
    if (useckey) 
    {
      SDL_SetColorKey(new_surface, SDL_TRUE, ckey);
      SDL_SetSurfaceRLE(new_surface, SDL_TRUE);
    }
  } else {
    int ix, iy;
    float fx, fy, fz;
    unsigned char *p1, *p2, *p3, *p4;

    new_surface = SDL_CreateRGBSurface(0, width, height, surface->format->BitsPerPixel,
                                       surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);

    SDL_LockSurface(surface);
    SDL_LockSurface(new_surface);

    pixels     = static_cast<unsigned char*>(surface->pixels);
    new_pixels = static_cast<unsigned char*>(new_surface->pixels);
    new_pitch = new_surface->pitch;

    for (i = 0; i < height; ++i) {
      x = i * new_pitch;
      fy = static_cast<float>(i) * static_cast<float>(surface->h) / static_cast<float>(height);
      iy = static_cast<int>(fy);
      fy -= static_cast<float>(iy);
      for (j = 0; j < width; ++j) {
        fx = static_cast<float>(j) * static_cast<float>(surface->w) / static_cast<float>(width);
        ix = static_cast<int>(fx);
        fx -= static_cast<float>(ix);
        fz = (fx + fy) / 2;

        p1 = &pixels[iy * surface->pitch + ix * bpp];
        p2 = (iy != surface->h - 1) ?
          &pixels[(iy + 1) * surface->pitch + ix * bpp] : p1;
        p3 = (ix != surface->w - 1) ?
          &pixels[iy * surface->pitch + (ix + 1) * bpp] : p1;
        p4 = (iy != surface->h - 1 && ix != surface->w - 1) ?
          &pixels[(iy + 1) * surface->pitch + (ix + 1) * bpp] : p1;

        new_pixels[x + 0] = static_cast<unsigned char>(
          (static_cast<float>(p1[0]) * (1 - fy) + static_cast<float>(p2[0]) * fy +
           static_cast<float>(p1[0]) * (1 - fx) + static_cast<float>(p3[0]) * fx +
           static_cast<float>(p1[0]) * (1 - fz) + static_cast<float>(p4[0]) * fz) / 3.0 + .5);
        new_pixels[x + 1] = static_cast<unsigned char>(
          (static_cast<float>(p1[1]) * (1 - fy) + static_cast<float>(p2[1]) * fy +
           static_cast<float>(p1[1]) * (1 - fx) + static_cast<float>(p3[1]) * fx +
           static_cast<float>(p1[1]) * (1 - fz) + static_cast<float>(p4[1]) * fz) / 3.0 + .5);
        new_pixels[x + 2] = static_cast<unsigned char>(
          (static_cast<float>(p1[2]) * (1 - fy) + static_cast<float>(p2[2]) * fy +
           static_cast<float>(p1[2]) * (1 - fx) + static_cast<float>(p3[2]) * fx +
           static_cast<float>(p1[2]) * (1 - fz) + static_cast<float>(p4[2]) * fz) / 3.0 + .5);
        if (bpp == 4) {
          new_pixels[x + 3] = static_cast<unsigned char>(
            (static_cast<float>(p1[3]) * (1 - fy) + static_cast<float>(p2[3]) * fy +
             static_cast<float>(p1[3]) * (1 - fx) + static_cast<float>(p3[3]) * fx +
             static_cast<float>(p1[3]) * (1 - fz) + static_cast<float>(p4[3]) * fz) / 3.0 + .5);
        }
        x += bpp;
      }
    }

    SDL_UnlockSurface(surface);
    SDL_UnlockSurface(new_surface);
  }

  return new_surface;
}
Exemple #13
0
static void sdl2_init_font(sdl2_video_t *vid, const char *font_path,
                          unsigned font_size)
{
   int i, r, g, b;
   SDL_Color colors[256];
   SDL_Surface *tmp = NULL;
   SDL_Palette *pal = NULL;
   const struct font_atlas *atlas = NULL;
   settings_t *settings = config_get_ptr();

   if (!settings->video.font_enable)
      return;

   if (!font_renderer_create_default(&vid->font_driver, &vid->font_data,
                                    *font_path ? font_path : NULL, font_size))
   {
      RARCH_WARN("[SDL]: Could not initialize fonts.\n");
      return;
   }

   r = settings->video.msg_color_r * 255;
   g = settings->video.msg_color_g * 255;
   b = settings->video.msg_color_b * 255;

   r = (r < 0) ? 0 : (r > 255 ? 255 : r);
   g = (g < 0) ? 0 : (g > 255 ? 255 : g);
   b = (b < 0) ? 0 : (b > 255 ? 255 : b);

   vid->font_r = r;
   vid->font_g = g;
   vid->font_b = b;

   atlas = vid->font_driver->get_atlas(vid->font_data);

   tmp = SDL_CreateRGBSurfaceFrom(atlas->buffer, atlas->width,
         atlas->height, 8, atlas->width,
         0, 0, 0, 0);

   for (i = 0; i < 256; ++i)
   {
      colors[i].r = colors[i].g = colors[i].b = i;
      colors[i].a = 255;
   }

   pal = SDL_AllocPalette(256);
   SDL_SetPaletteColors(pal, colors, 0, 256);
   SDL_SetSurfacePalette(tmp, pal);
   SDL_SetColorKey(tmp, SDL_TRUE, 0);

   vid->font.tex  = SDL_CreateTextureFromSurface(vid->renderer, tmp);

   if (vid->font.tex)
   {
      vid->font.w      = atlas->width;
      vid->font.h      = atlas->height;
      vid->font.active = true;

      SDL_SetTextureBlendMode(vid->font.tex, SDL_BLENDMODE_ADD);
   }
   else
      RARCH_WARN("[SDL]: Failed to initialize font texture: %s\n", SDL_GetError());

   SDL_FreePalette(pal);
   SDL_FreeSurface(tmp);
}
Exemple #14
0
/*
 * Create an empty RGB surface of the appropriate depth using the given
 * enum SDL_PIXELFORMAT_* format
 */
SDL_Surface *
SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth,
                               Uint32 format)
{
    SDL_Surface *surface;

    /* The flags are no longer used, make the compiler happy */
    (void)flags;

    /* Allocate the surface */
    surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
    if (surface == NULL) {
        SDL_OutOfMemory();
        return NULL;
    }

    surface->format = SDL_AllocFormat(format);
    if (!surface->format) {
        SDL_FreeSurface(surface);
        return NULL;
    }
    surface->w = width;
    surface->h = height;
    surface->pitch = SDL_CalculatePitch(format, width);
    SDL_SetClipRect(surface, NULL);

    if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
        SDL_Palette *palette =
            SDL_AllocPalette((1 << surface->format->BitsPerPixel));
        if (!palette) {
            SDL_FreeSurface(surface);
            return NULL;
        }
        if (palette->ncolors == 2) {
            /* Create a black and white bitmap palette */
            palette->colors[0].r = 0xFF;
            palette->colors[0].g = 0xFF;
            palette->colors[0].b = 0xFF;
            palette->colors[1].r = 0x00;
            palette->colors[1].g = 0x00;
            palette->colors[1].b = 0x00;
        }
        SDL_SetSurfacePalette(surface, palette);
        SDL_FreePalette(palette);
    }

    /* Get the pixels */
    if (surface->w && surface->h) {
        /* Assumptions checked in surface_size_assumptions assert above */
        Sint64 size = ((Sint64)surface->h * surface->pitch);
        if (size < 0 || size > SDL_MAX_SINT32) {
            /* Overflow... */
            SDL_FreeSurface(surface);
            SDL_OutOfMemory();
            return NULL;
        }

        surface->pixels = SDL_malloc((size_t)size);
        if (!surface->pixels) {
            SDL_FreeSurface(surface);
            SDL_OutOfMemory();
            return NULL;
        }
        /* This is important for bitmaps */
        SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
    }

    /* Allocate an empty mapping */
    surface->map = SDL_AllocBlitMap();
    if (!surface->map) {
        SDL_FreeSurface(surface);
        return NULL;
    }

    /* By default surface with an alpha mask are set up for blending */
    if (surface->format->Amask) {
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
    }

    /* The surface is ready to go */
    surface->refcount = 1;
    return surface;
}
Exemple #15
0
/*++
 Start a battle.
 
 Parameters:
 [IN]  wEnemyTeam - the number of the enemy team.
 [IN]  fIsBoss - TRUE for boss fight (not allowed to flee).
 
 Return value:
 The result of the battle.
 --*/
BATTLERESULT PAL_StartBattle(WORD wEnemyTeam, BOOL fIsBoss)
{
    int            i;
    WORD           w, wPrevWaveLevel;
    SHORT          sPrevWaveProgression;
    
    //
    // Set the screen waving effects
    //
    wPrevWaveLevel = gpGlobals->wScreenWave;
    sPrevWaveProgression = gpGlobals->sWaveProgression;
    
    gpGlobals->sWaveProgression = 0;
    gpGlobals->wScreenWave = gpGlobals->g.lprgBattleField[gpGlobals->wNumBattleField].wScreenWave;
    
    //
    // Make sure everyone in the party is alive, also clear all hidden
    // EXP count records
    //
    for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
    {
        w = gpGlobals->rgParty[i].wPlayerRole;
        
        if (gpGlobals->g.PlayerRoles.rgwHP[w] == 0)
        {
            gpGlobals->g.PlayerRoles.rgwHP[w] = 1;
            gpGlobals->rgPlayerStatus[w][kStatusPuppet] = 0;
        }
        
        gpGlobals->Exp.rgHealthExp[w].wCount = 0;
        gpGlobals->Exp.rgMagicExp[w].wCount = 0;
        gpGlobals->Exp.rgAttackExp[w].wCount = 0;
        gpGlobals->Exp.rgMagicPowerExp[w].wCount = 0;
        gpGlobals->Exp.rgDefenseExp[w].wCount = 0;
        gpGlobals->Exp.rgDexterityExp[w].wCount = 0;
        gpGlobals->Exp.rgFleeExp[w].wCount = 0;
    }
    
    //
    // Clear all item-using records
    //
    for (i = 0; i < MAX_INVENTORY; i++)
    {
        gpGlobals->rgInventory[i].nAmountInUse = 0;
    }
    
    //
    // Store all enemies
    //
    for (i = 0; i < MAX_ENEMIES_IN_TEAM; i++)
    {
        memset(&(g_Battle.rgEnemy[i]), 0, sizeof(BATTLEENEMY));
        w = gpGlobals->g.lprgEnemyTeam[wEnemyTeam].rgwEnemy[i];
        
        if (w == 0xFFFF)
        {
            break;
        }
        
        if (w != 0)
        {
            g_Battle.rgEnemy[i].e = gpGlobals->g.lprgEnemy[gpGlobals->g.rgObject[w].enemy.wEnemyID];
            g_Battle.rgEnemy[i].wObjectID = w;
            g_Battle.rgEnemy[i].state = kFighterWait;
            g_Battle.rgEnemy[i].wScriptOnTurnStart = gpGlobals->g.rgObject[w].enemy.wScriptOnTurnStart;
            g_Battle.rgEnemy[i].wScriptOnBattleEnd = gpGlobals->g.rgObject[w].enemy.wScriptOnBattleEnd;
            g_Battle.rgEnemy[i].wScriptOnReady = gpGlobals->g.rgObject[w].enemy.wScriptOnReady;
            g_Battle.rgEnemy[i].iColorShift = 0;
            g_Battle.rgEnemy[i].dwMaxHealth = g_Battle.rgEnemy[i].e.wHealth;
            
#ifndef PAL_CLASSIC
            g_Battle.rgEnemy[i].flTimeMeter = 50;
            
            //
            // HACK: Otherwise the black thief lady will be too hard to beat
            //
            if (g_Battle.rgEnemy[i].e.wDexterity == 164)
            {
                g_Battle.rgEnemy[i].e.wDexterity /= ((gpGlobals->wMaxPartyMemberIndex == 0) ? 6 : 3);
            }
            
            //
            // HACK: Heal up automatically for final boss
            //
            if (g_Battle.rgEnemy[i].e.wHealth == 32760)
            {
                for (w = 0; w < MAX_PLAYER_ROLES; w++)
                {
                    gpGlobals->g.PlayerRoles.rgwHP[w] = gpGlobals->g.PlayerRoles.rgwMaxHP[w];
                    gpGlobals->g.PlayerRoles.rgwMP[w] = gpGlobals->g.PlayerRoles.rgwMaxMP[w];
                }
            }
            
            //
            // Yet another HACKs
            //
            if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -32)
            {
                g_Battle.rgEnemy[i].e.wDexterity = 0; // for Grandma Knife
            }
            else if (g_Battle.rgEnemy[i].e.wDexterity == 20)
            {
                //
                // for Fox Demon
                //
                if (gpGlobals->g.PlayerRoles.rgwLevel[0] < 15)
                {
                    g_Battle.rgEnemy[i].e.wDexterity = 8;
                }
                else if (gpGlobals->g.PlayerRoles.rgwLevel[4] > 28 ||
                         gpGlobals->Exp.rgPrimaryExp[4].wExp > 0)
                {
                    g_Battle.rgEnemy[i].e.wDexterity = 60;
                }
            }
            else if (g_Battle.rgEnemy[i].e.wExp == 250 &&
                     g_Battle.rgEnemy[i].e.wCash == 1100)
            {
                g_Battle.rgEnemy[i].e.wDexterity += 12; // for Snake Demon
            }
            else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -60)
            {
                g_Battle.rgEnemy[i].e.wDexterity = 15; // for Spider
            }
            else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -30)
            {
                g_Battle.rgEnemy[i].e.wDexterity = (WORD)-10; // for Stone Head
            }
            else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -16)
            {
                g_Battle.rgEnemy[i].e.wDexterity = 0; // for Zombie
            }
            else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -20)
            {
                g_Battle.rgEnemy[i].e.wDexterity = -8; // for Flower Demon
            }
            else if (g_Battle.rgEnemy[i].e.wLevel < 20 &&
                     gpGlobals->wNumScene >= 0xD8 && gpGlobals->wNumScene <= 0xE2)
            {
                //
                // for low-level monsters in the Cave of Trial
                //
                g_Battle.rgEnemy[i].e.wLevel += 15;
                g_Battle.rgEnemy[i].e.wDexterity += 25;
            }
            else if (gpGlobals->wNumScene == 0x90)
            {
                g_Battle.rgEnemy[i].e.wDexterity += 25; // for Tower Dragons
            }
            else if (g_Battle.rgEnemy[i].e.wLevel == 2 &&
                     g_Battle.rgEnemy[i].e.wCash == 48)
            {
                g_Battle.rgEnemy[i].e.wDexterity += 8; // for Miao Fists
            }
            else if (g_Battle.rgEnemy[i].e.wLevel == 4 &&
                     g_Battle.rgEnemy[i].e.wCash == 240)
            {
                g_Battle.rgEnemy[i].e.wDexterity += 18; // for Fat Miao
            }
            else if (g_Battle.rgEnemy[i].e.wLevel == 16 &&
                     g_Battle.rgEnemy[i].e.wMagicRate == 4 &&
                     g_Battle.rgEnemy[i].e.wAttackEquivItemRate == 4)
            {
                g_Battle.rgEnemy[i].e.wDexterity += 50; // for Black Spider
            }
#endif
        }
    }
    
    g_Battle.wMaxEnemyIndex = i - 1;
    
    //
    // Store all players
    //
    for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
    {
        g_Battle.rgPlayer[i].flTimeMeter = 15.0f;
#ifndef PAL_CLASSIC
        g_Battle.rgPlayer[i].flTimeSpeedModifier = 2.0f;
        g_Battle.rgPlayer[i].sTurnOrder = -1;
#endif
        g_Battle.rgPlayer[i].wHidingTime = 0;
        g_Battle.rgPlayer[i].state = kFighterWait;
        g_Battle.rgPlayer[i].action.sTarget = -1;
        g_Battle.rgPlayer[i].fDefending = FALSE;
        g_Battle.rgPlayer[i].wCurrentFrame = 0;
        g_Battle.rgPlayer[i].iColorShift = FALSE;
    }
    
    //
    // Load sprites and background
    //
    PAL_LoadBattleSprites();
    PAL_LoadBattleBackground();
    
    //
    // Create the surface for scene buffer
    //
    g_Battle.lpSceneBuf =
    SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
                         gpScreen->format->Rmask, gpScreen->format->Gmask,
                         gpScreen->format->Bmask, gpScreen->format->Amask);
    
    if (g_Battle.lpSceneBuf == NULL)
    {
        TerminateOnError("PAL_StartBattle(): creating surface for scene buffer failed!");
    }
    
#if SDL_VERSION_ATLEAST(2, 0, 0)
    SDL_SetSurfacePalette(g_Battle.lpSceneBuf, gpScreen->format->palette);
#else
    SDL_SetPalette(g_Battle.lpSceneBuf, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
#endif
    
    PAL_UpdateEquipments();
    
    g_Battle.iExpGained = 0;
    g_Battle.iCashGained = 0;
    
    g_Battle.fIsBoss = fIsBoss;
    g_Battle.fEnemyCleared = FALSE;
    g_Battle.fEnemyMoving = FALSE;
    g_Battle.iHidingTime = 0;
    g_Battle.wMovingPlayerIndex = 0;
    
    g_Battle.UI.szMsg[0] = '\0';
    g_Battle.UI.szNextMsg[0] = '\0';
    g_Battle.UI.dwMsgShowTime = 0;
    g_Battle.UI.state = kBattleUIWait;
    g_Battle.UI.fAutoAttack = FALSE;
    g_Battle.UI.wSelectedIndex = 0;
    g_Battle.UI.wPrevEnemyTarget = 0;
    
    memset(g_Battle.UI.rgShowNum, 0, sizeof(g_Battle.UI.rgShowNum));
    
    g_Battle.lpSummonSprite = NULL;
    g_Battle.sBackgroundColorShift = 0;
    
    gpGlobals->fInBattle = TRUE;
    g_Battle.BattleResult = kBattleResultPreBattle;
    
    PAL_BattleUpdateFighters();
    
    //
    // Load the battle effect sprite.
    //
    i = PAL_MKFGetChunkSize(10, gpGlobals->f.fpDATA);
    g_Battle.lpEffectSprite = UTIL_malloc(i);
    
    PAL_MKFReadChunk(g_Battle.lpEffectSprite, i, 10, gpGlobals->f.fpDATA);
    
#ifdef PAL_CLASSIC
    g_Battle.Phase = kBattlePhaseSelectAction;
    g_Battle.fRepeat = FALSE;
    g_Battle.fForce = FALSE;
    g_Battle.fFlee = FALSE;
#endif
    
#ifdef PAL_ALLOW_KEYREPEAT
    SDL_EnableKeyRepeat(120, 75);
#endif
    
    //
    // Run the main battle routine.
    //
    i = PAL_BattleMain();
    
#ifdef PAL_ALLOW_KEYREPEAT
    SDL_EnableKeyRepeat(0, 0);
    PAL_ClearKeyState();
    g_InputState.prevdir = kDirUnknown;
#endif
    
    if (i == kBattleResultWon)
    {
        //
        // Player won the battle. Add the Experience points.
        //
        PAL_BattleWon();
    }
    
    //
    // Clear all item-using records
    //
    for (w = 0; w < MAX_INVENTORY; w++)
    {
        gpGlobals->rgInventory[w].nAmountInUse = 0;
    }
    
    //
    // Clear all player status, poisons and temporary effects
    //
    PAL_ClearAllPlayerStatus();
    for (w = 0; w < MAX_PLAYER_ROLES; w++)
    {
        PAL_CurePoisonByLevel(w, 3);
        PAL_RemoveEquipmentEffect(w, kBodyPartExtra);
    }
    
    //
    // Free all the battle sprites
    //
    PAL_FreeBattleSprites();
    free(g_Battle.lpEffectSprite);
    
    //
    // Free the surfaces for the background picture and scene buffer
    //
    SDL_FreeSurface(g_Battle.lpBackground);
    SDL_FreeSurface(g_Battle.lpSceneBuf);
    
    g_Battle.lpBackground = NULL;
    g_Battle.lpSceneBuf = NULL;
    
    gpGlobals->fInBattle = FALSE;
    
    PAL_PlayMUS(gpGlobals->wNumMusic, TRUE, 1);
    
    //
    // Restore the screen waving effects
    //
    gpGlobals->sWaveProgression = sPrevWaveProgression;
    gpGlobals->wScreenWave = wPrevWaveLevel;
    
    return i;
}
Exemple #16
0
/*
 * Create an empty RGB surface of the appropriate depth
 */
SDL_Surface *
SDL_CreateRGBSurface(Uint32 flags,
                     int width, int height, int depth,
                     Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
{
    SDL_Surface *surface;
    Uint32 format;

    /* The flags are no longer used, make the compiler happy */
    (void)flags;

    /* Get the pixel format */
    format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
    if (format == SDL_PIXELFORMAT_UNKNOWN) {
        SDL_SetError("Unknown pixel format");
        return NULL;
    }

    /* Allocate the surface */
    surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
    if (surface == NULL) {
        SDL_OutOfMemory();
        return NULL;
    }

    surface->format = SDL_AllocFormat(format);
    if (!surface->format) {
        SDL_FreeSurface(surface);
        return NULL;
    }
    surface->w = width;
    surface->h = height;
    surface->pitch = SDL_CalculatePitch(surface);
    SDL_SetClipRect(surface, NULL);

    if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
        SDL_Palette *palette =
            SDL_AllocPalette((1 << surface->format->BitsPerPixel));
        if (!palette) {
            SDL_FreeSurface(surface);
            return NULL;
        }
        if (palette->ncolors == 2) {
            /* Create a black and white bitmap palette */
            palette->colors[0].r = 0xFF;
            palette->colors[0].g = 0xFF;
            palette->colors[0].b = 0xFF;
            palette->colors[1].r = 0x00;
            palette->colors[1].g = 0x00;
            palette->colors[1].b = 0x00;
        }
        SDL_SetSurfacePalette(surface, palette);
        SDL_FreePalette(palette);
    }

    /* Get the pixels */
    if (surface->w && surface->h) {
        surface->pixels = SDL_malloc(surface->h * surface->pitch);
        if (!surface->pixels) {
            SDL_FreeSurface(surface);
            SDL_OutOfMemory();
            return NULL;
        }
        /* This is important for bitmaps */
        SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
    }

    /* Allocate an empty mapping */
    surface->map = SDL_AllocBlitMap();
    if (!surface->map) {
        SDL_FreeSurface(surface);
        return NULL;
    }

    /* By default surface with an alpha mask are set up for blending */
    if (Amask) {
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
    }

    /* The surface is ready to go */
    surface->refcount = 1;
    return surface;
}
Exemple #17
0
/*
 * Convert a surface into the specified pixel format.
 */
SDL_Surface *
SDL_ConvertSurface(SDL_Surface * surface, const SDL_PixelFormat * format,
                   Uint32 flags)
{
    SDL_Surface *convert;
    Uint32 copy_flags;
    SDL_Color copy_color;
    SDL_Rect bounds;

    if (!surface) {
        SDL_InvalidParamError("surface");
        return NULL;
    }
    if (!format) {
        SDL_InvalidParamError("format");
        return NULL;
    }

    /* Check for empty destination palette! (results in empty image) */
    if (format->palette != NULL) {
        int i;
        for (i = 0; i < format->palette->ncolors; ++i) {
            if ((format->palette->colors[i].r != 0xFF) ||
                (format->palette->colors[i].g != 0xFF) ||
                (format->palette->colors[i].b != 0xFF))
                break;
        }
        if (i == format->palette->ncolors) {
            SDL_SetError("Empty destination palette");
            return (NULL);
        }
    }

    /* Create a new surface with the desired format */
    convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
                                   format->BitsPerPixel, format->Rmask,
                                   format->Gmask, format->Bmask,
                                   format->Amask);
    if (convert == NULL) {
        return (NULL);
    }

    /* Copy the palette if any */
    if (format->palette && convert->format->palette) {
        SDL_memcpy(convert->format->palette->colors,
                   format->palette->colors,
                   format->palette->ncolors * sizeof(SDL_Color));
        convert->format->palette->ncolors = format->palette->ncolors;
    }

    /* Save the original copy flags */
    copy_flags = surface->map->info.flags;
    copy_color.r = surface->map->info.r;
    copy_color.g = surface->map->info.g;
    copy_color.b = surface->map->info.b;
    copy_color.a = surface->map->info.a;
    surface->map->info.r = 0xFF;
    surface->map->info.g = 0xFF;
    surface->map->info.b = 0xFF;
    surface->map->info.a = 0xFF;
    surface->map->info.flags = 0;
    SDL_InvalidateMap(surface->map);

    /* Copy over the image data */
    bounds.x = 0;
    bounds.y = 0;
    bounds.w = surface->w;
    bounds.h = surface->h;
    SDL_LowerBlit(surface, &bounds, convert, &bounds);

    /* Clean up the original surface, and update converted surface */
    convert->map->info.r = copy_color.r;
    convert->map->info.g = copy_color.g;
    convert->map->info.b = copy_color.b;
    convert->map->info.a = copy_color.a;
    convert->map->info.flags =
        (copy_flags &
         ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
           | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
           SDL_COPY_RLE_ALPHAKEY));
    surface->map->info.r = copy_color.r;
    surface->map->info.g = copy_color.g;
    surface->map->info.b = copy_color.b;
    surface->map->info.a = copy_color.a;
    surface->map->info.flags = copy_flags;
    SDL_InvalidateMap(surface->map);
    if (copy_flags & SDL_COPY_COLORKEY) {
        SDL_bool set_colorkey_by_color = SDL_FALSE;

        if (surface->format->palette) {
            if (format->palette &&
                surface->format->palette->ncolors <= format->palette->ncolors &&
                (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
                  surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
                /* The palette is identical, just set the same colorkey */
                SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
            } else if (format->Amask) {
                /* The alpha was set in the destination from the palette */
            } else {
                set_colorkey_by_color = SDL_TRUE;
            }
        } else {
            set_colorkey_by_color = SDL_TRUE;
        }

        if (set_colorkey_by_color) {
            SDL_Surface *tmp;
            SDL_Surface *tmp2;
            int converted_colorkey = 0;

            /* Create a dummy surface to get the colorkey converted */
            tmp = SDL_CreateRGBSurface(0, 1, 1,
                                   surface->format->BitsPerPixel, surface->format->Rmask,
                                   surface->format->Gmask, surface->format->Bmask,
                                   surface->format->Amask);

            /* Share the palette, if any */
            if (surface->format->palette) {
                SDL_SetSurfacePalette(tmp, surface->format->palette);
            }
            
            SDL_FillRect(tmp, NULL, surface->map->info.colorkey);

            tmp->map->info.flags &= ~SDL_COPY_COLORKEY;

            /* Convertion of the colorkey */
            tmp2 = SDL_ConvertSurface(tmp, format, 0);

            /* Get the converted colorkey */
            SDL_memcpy(&converted_colorkey, tmp2->pixels, tmp2->format->BytesPerPixel);

            SDL_FreeSurface(tmp);
            SDL_FreeSurface(tmp2);

            /* Set the converted colorkey on the new surface */
            SDL_SetColorKey(convert, 1, converted_colorkey);

            /* This is needed when converting for 3D texture upload */
            SDL_ConvertColorkeyToAlpha(convert);
        }
    }
    SDL_SetClipRect(convert, &surface->clip_rect);

    /* Enable alpha blending by default if the new surface has an
     * alpha channel or alpha modulation */
    if ((surface->format->Amask && format->Amask) ||
        (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
        SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
    }
    if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
        SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
    }

    /* We're ready to go! */
    return (convert);
}
Exemple #18
0
VOID
PAL_ShowFBP(
WORD         wChunkNum,
WORD         wFade
)
/*++
  Purpose:

  Draw an FBP picture to the screen.

  Parameters:

  [IN]  wChunkNum - number of chunk in fbp.mkf file.

  [IN]  wFade - fading speed of showing the picture.

  Return value:

  None.

  --*/
{
	PAL_LARGE BYTE            buf[320 * 200];
	PAL_LARGE BYTE            bufSprite[320 * 200];
	const int                 rgIndex[6] = {0, 3, 1, 5, 2, 4};
	SDL_Surface              *p;
	int                       i, j, k;
	BYTE                      a, b;

	if (PAL_MKFDecompressChunk(buf, 320 * 200, wChunkNum, gpGlobals->f.fpFBP) <= 0)
	{
		memset(buf, 0, sizeof(buf));
	}

	if (g_wCurEffectSprite != 0)
	{
		PAL_MKFDecompressChunk(bufSprite, 320 * 200, g_wCurEffectSprite, gpGlobals->f.fpMGO);
	}

	if (wFade)
	{
		wFade++;
		wFade *= 10;

		p = SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
			gpScreen->format->Rmask, gpScreen->format->Gmask,
			gpScreen->format->Bmask, gpScreen->format->Amask);
#if SDL_VERSION_ATLEAST(2, 0, 0)
		SDL_SetSurfacePalette(p, gpScreen->format->palette);
#else
		SDL_SetPalette(p, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
#endif
		PAL_FBPBlitToSurface(buf, p);
		VIDEO_BackupScreen();

		for (i = 0; i < 16; i++)
		{
			for (j = 0; j < 6; j++)
			{
				//
				// Blend the pixels in the 2 buffers, and put the result into the
				// backup buffer
				//
				for (k = rgIndex[j]; k < gpScreen->pitch * gpScreen->h; k += 6)
				{
					a = ((LPBYTE)(p->pixels))[k];
					b = ((LPBYTE)(gpScreenBak->pixels))[k];

					if (i > 0)
					{
						if ((a & 0x0F) > (b & 0x0F))
						{
							b++;
						}
						else if ((a & 0x0F) < (b & 0x0F))
						{
							b--;
						}
					}

					((LPBYTE)(gpScreenBak->pixels))[k] = ((a & 0xF0) | (b & 0x0F));
				}

				SDL_BlitSurface(gpScreenBak, NULL, gpScreen, NULL);

				if (g_wCurEffectSprite != 0)
				{
					int f = SDL_GetTicks() / 150;
					PAL_RLEBlitToSurface(PAL_SpriteGetFrame(bufSprite, f % PAL_SpriteGetNumFrames(bufSprite)),
						gpScreen, PAL_XY(0, 0));
				}

				VIDEO_UpdateScreen(NULL);
				UTIL_Delay(wFade);
			}
		}

		SDL_FreeSurface(p);
	}

	//
	// HACKHACK: to make the ending show correctly
	//
#ifdef PAL_WIN95
	if (wChunkNum != 68)
#else
	if (wChunkNum != 49)
#endif
	{
		PAL_FBPBlitToSurface(buf, gpScreen);
	}

	VIDEO_UpdateScreen(NULL);
}
Exemple #19
0
VOID
PAL_EndingAnimation(
VOID
)
/*++
  Purpose:

  Show the ending animation.//就是灵儿独自面对合体水魔兽的动画

  Parameters:

  None.

  Return value:

  None.

  --*/
{
	LPBYTE            buf;
	LPBYTE            bufGirl;
	SDL_Surface      *pUpper;
	SDL_Surface      *pLower;
	SDL_Rect          srcrect, dstrect;

	int               yPosGirl = 180;
	int               i;

	buf = (LPBYTE)UTIL_calloc(1, 64000);
	bufGirl = (LPBYTE)UTIL_calloc(1, 6000);

	pUpper = SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
		gpScreen->format->Rmask, gpScreen->format->Gmask,
		gpScreen->format->Bmask, gpScreen->format->Amask);

	pLower = SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
		gpScreen->format->Rmask, gpScreen->format->Gmask,
		gpScreen->format->Bmask, gpScreen->format->Amask);

#if SDL_VERSION_ATLEAST(2, 0, 0)
	SDL_SetSurfacePalette(pUpper, gpScreen->format->palette);
	SDL_SetSurfacePalette(pLower, gpScreen->format->palette);
#else
	SDL_SetPalette(pUpper, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
	SDL_SetPalette(pLower, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
#endif

#ifdef PAL_WIN95
	PAL_MKFDecompressChunk(buf, 64000, 69, gpGlobals->f.fpFBP);
	PAL_FBPBlitToSurface(buf, pUpper);

	PAL_MKFDecompressChunk(buf, 64000, 70, gpGlobals->f.fpFBP);
	PAL_FBPBlitToSurface(buf, pLower);
#else
	PAL_MKFDecompressChunk(buf, 64000, 61, gpGlobals->f.fpFBP);
	PAL_FBPBlitToSurface(buf, pUpper);

	PAL_MKFDecompressChunk(buf, 64000, 62, gpGlobals->f.fpFBP);
	PAL_FBPBlitToSurface(buf, pLower);
#endif

	PAL_MKFDecompressChunk(buf, 64000, 571, gpGlobals->f.fpMGO);
	PAL_MKFDecompressChunk(bufGirl, 6000, 572, gpGlobals->f.fpMGO);

	srcrect.x = 0;
	dstrect.x = 0;
	srcrect.w = 320;
	dstrect.w = 320;

	gpGlobals->wScreenWave = 2;

	for (i = 0; i < 400; i++)
	{
		//
		// Draw the background
		//
		srcrect.y = 0;
		srcrect.h = 200 - i / 2;

		dstrect.y = i / 2;
		dstrect.h = 200 - i / 2;

		SDL_BlitSurface(pLower, &srcrect, gpScreen, &dstrect);

		srcrect.y = 200 - i / 2;
		srcrect.h = i / 2;

		dstrect.y = 0;
		dstrect.h = i / 2;

		SDL_BlitSurface(pUpper, &srcrect, gpScreen, &dstrect);

		PAL_ApplyWave(gpScreen);

		//
		// Draw the beast
		//
		PAL_RLEBlitToSurface(PAL_SpriteGetFrame(buf, 0), gpScreen, PAL_XY(0, -400 + i));
		PAL_RLEBlitToSurface(PAL_SpriteGetFrame(buf, 1), gpScreen, PAL_XY(0, -200 + i));

#ifdef PAL_WIN95
		PAL_RLEBlitToSurface(buf + 0x8444, gpScreen, PAL_XY(0, -200 + i));
#else
		PAL_RLEBlitToSurface(PAL_SpriteGetFrame(buf, 1), gpScreen, PAL_XY(0, -200 + i));
#endif

		//
		// Draw the girl
		//
		yPosGirl -= i & 1;
		if (yPosGirl < 80)
		{
			yPosGirl = 80;
		}

		PAL_RLEBlitToSurface(PAL_SpriteGetFrame(bufGirl, (SDL_GetTicks() / 50) % 4),
			gpScreen, PAL_XY(220, yPosGirl));

		//
		// Update the screen
		//
		VIDEO_UpdateScreen(NULL);
		if (gpGlobals->fNeedToFadeIn)
		{
			PAL_FadeIn(gpGlobals->wNumPalette, gpGlobals->fNightPalette, 1);
			gpGlobals->fNeedToFadeIn = FALSE;
#if SDL_VERSION_ATLEAST(2, 0, 0)
			SDL_SetSurfacePalette(pUpper, gpScreen->format->palette);
			SDL_SetSurfacePalette(pLower, gpScreen->format->palette);
#else
			SDL_SetPalette(pUpper, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
			SDL_SetPalette(pLower, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
#endif
		}

		UTIL_Delay(50);
	}

	gpGlobals->wScreenWave = 0;

	SDL_FreeSurface(pUpper);
	SDL_FreeSurface(pLower);

	free(buf);
	free(bufGirl);
}
Exemple #20
0
VOID
PAL_ScrollFBP(
WORD         wChunkNum,
WORD         wScrollSpeed,
BOOL         fScrollDown
)
/*++
  Purpose:

  Scroll up an FBP picture to the screen.

  Parameters:

  [IN]  wChunkNum - number of chunk in fbp.mkf file.

  [IN]  wScrollSpeed - scrolling speed of showing the picture.

  [IN]  fScrollDown - TRUE if scroll down, FALSE if scroll up.

  Return value:

  None.

  --*/
{
	SDL_Surface          *p;
	PAL_LARGE BYTE        buf[320 * 200];
	PAL_LARGE BYTE        bufSprite[320 * 200];
	int                   i, l;
	SDL_Rect              rect, dstrect;

	if (PAL_MKFDecompressChunk(buf, 320 * 200, wChunkNum, gpGlobals->f.fpFBP) <= 0)
	{
		return;
	}

	if (g_wCurEffectSprite != 0)
	{
		PAL_MKFDecompressChunk(bufSprite, 320 * 200, g_wCurEffectSprite, gpGlobals->f.fpMGO);
	}

	p = SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
		gpScreen->format->Rmask, gpScreen->format->Gmask,
		gpScreen->format->Bmask, gpScreen->format->Amask);

	if (p == NULL)
	{
		return;
	}

#if SDL_VERSION_ATLEAST(2, 0, 0)
	SDL_SetSurfacePalette(p, gpScreen->format->palette);
#else
	SDL_SetPalette(p, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
#endif

	VIDEO_BackupScreen();
	PAL_FBPBlitToSurface(buf, p);

	if (wScrollSpeed == 0)
	{
		wScrollSpeed = 1;
	}

	rect.x = 0;
	rect.w = 320;
	dstrect.x = 0;
	dstrect.w = 320;

	for (l = 0; l < 220; l++)
	{
		i = l;
		if (i > 200)
		{
			i = 200;
		}

		if (fScrollDown)
		{
			rect.y = 0;
			dstrect.y = i;
			rect.h = 200 - i;
			dstrect.h = 200 - i;
		}
		else
		{
			rect.y = i;
			dstrect.y = 0;
			rect.h = 200 - i;
			dstrect.h = 200 - i;
		}

		SDL_BlitSurface(gpScreenBak, &rect, gpScreen, &dstrect);

		if (fScrollDown)
		{
			rect.y = 200 - i;
			dstrect.y = 0;
			rect.h = i;
			dstrect.h = i;
		}
		else
		{
			rect.y = 0;
			dstrect.y = 200 - i;
			rect.h = i;
			dstrect.h = i;
		}

		SDL_BlitSurface(p, &rect, gpScreen, &dstrect);

		PAL_ApplyWave(gpScreen);

		if (g_wCurEffectSprite != 0)
		{
			int f = SDL_GetTicks() / 150;
			PAL_RLEBlitToSurface(PAL_SpriteGetFrame(bufSprite, f % PAL_SpriteGetNumFrames(bufSprite)),
				gpScreen, PAL_XY(0, 0));
		}

		VIDEO_UpdateScreen(NULL);

		if (gpGlobals->fNeedToFadeIn)
		{
			PAL_FadeIn(gpGlobals->wNumPalette, gpGlobals->fNightPalette, 1);
			gpGlobals->fNeedToFadeIn = FALSE;
#if SDL_VERSION_ATLEAST(2, 0, 0)
			SDL_SetSurfacePalette(p, gpScreen->format->palette);
#else
			SDL_SetPalette(p, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
#endif
		}

		UTIL_Delay(800 / wScrollSpeed);
	}

	SDL_BlitSurface(p, NULL, gpScreen, NULL);
	SDL_FreeSurface(p);
	VIDEO_UpdateScreen(NULL);
}
SDL_Renderer *
SDL_DUMMY_CreateRenderer(SDL_Window * window, Uint32 flags)
{
    SDL_VideoDisplay *display = window->display;
    SDL_DisplayMode *displayMode = &display->current_mode;
    SDL_Renderer *renderer;
    SDL_DUMMY_RenderData *data;
    int i, n;
    int bpp;
    Uint32 Rmask, Gmask, Bmask, Amask;

    if (!SDL_PixelFormatEnumToMasks
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
        SDL_SetError("Unknown display format");
        return NULL;
    }

    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
    if (!renderer) {
        SDL_OutOfMemory();
        return NULL;
    }

    data = (SDL_DUMMY_RenderData *) SDL_malloc(sizeof(*data));
    if (!data) {
        SDL_DUMMY_DestroyRenderer(renderer);
        SDL_OutOfMemory();
        return NULL;
    }
    SDL_zerop(data);

    renderer->RenderDrawPoints = SDL_DUMMY_RenderDrawPoints;
    renderer->RenderDrawLines = SDL_DUMMY_RenderDrawLines;
    renderer->RenderDrawRects = SDL_DUMMY_RenderDrawRects;
    renderer->RenderFillRects = SDL_DUMMY_RenderFillRects;
    renderer->RenderCopy = SDL_DUMMY_RenderCopy;
    renderer->RenderReadPixels = SDL_DUMMY_RenderReadPixels;
    renderer->RenderWritePixels = SDL_DUMMY_RenderWritePixels;
    renderer->RenderPresent = SDL_DUMMY_RenderPresent;
    renderer->DestroyRenderer = SDL_DUMMY_DestroyRenderer;
    renderer->info.name = SDL_DUMMY_RenderDriver.info.name;
    renderer->info.flags = 0;
    renderer->window = window;
    renderer->driverdata = data;
    Setup_SoftwareRenderer(renderer);

    if (flags & SDL_RENDERER_PRESENTFLIP2) {
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
        n = 2;
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
        n = 3;
    } else {
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
        n = 1;
    }
    for (i = 0; i < n; ++i) {
        data->screens[i] =
            SDL_CreateRGBSurface(0, window->w, window->h, bpp, Rmask, Gmask,
                                 Bmask, Amask);
        if (!data->screens[i]) {
            SDL_DUMMY_DestroyRenderer(renderer);
            return NULL;
        }
        SDL_SetSurfacePalette(data->screens[i], display->palette);
    }
    data->current_screen = 0;

    return renderer;
}
Exemple #22
0
SDL_Renderer *
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
{
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
    SDL_DisplayMode *displayMode = &display->current_mode;
    SDL_Renderer *renderer;
    SW_RenderData *data;
    int i, n;
    int bpp;
    Uint32 Rmask, Gmask, Bmask, Amask;
    Uint32 renderer_flags;
    const char *desired_driver;

    if (!SDL_PixelFormatEnumToMasks
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
        SDL_SetError("Unknown display format");
        return NULL;
    }

    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
    if (!renderer) {
        SDL_OutOfMemory();
        return NULL;
    }

    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
    if (!data) {
        SW_DestroyRenderer(renderer);
        SDL_OutOfMemory();
        return NULL;
    }
    renderer->ActivateRenderer = SW_ActivateRenderer;
    renderer->DisplayModeChanged = SW_DisplayModeChanged;

    renderer->RenderPoint = SW_RenderPoint;
    renderer->RenderLine = SW_RenderLine;
    renderer->RenderFill = SW_RenderFill;
    renderer->RenderCopy = SW_RenderCopy;
    renderer->RenderPresent = SW_RenderPresent;
    renderer->DestroyRenderer = SW_DestroyRenderer;
    renderer->info.name = SW_RenderDriver.info.name;
    renderer->info.flags = 0;
    renderer->window = window->id;
    renderer->driverdata = data;
    Setup_SoftwareRenderer(renderer);

    if (flags & SDL_RENDERER_PRESENTFLIP2) {
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
        n = 2;
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
        n = 3;
    } else {
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
        n = 1;
    }
    data->format = displayMode->format;

    /* Find a render driver that we can use to display data */
    renderer_flags = (SDL_RENDERER_SINGLEBUFFER |
                      SDL_RENDERER_PRESENTDISCARD);
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
        renderer_flags |= SDL_RENDERER_PRESENTVSYNC;
    }
    desired_driver = SDL_getenv("SDL_VIDEO_RENDERER_SWDRIVER");
    for (i = 0; i < display->num_render_drivers; ++i) {
        SDL_RenderDriver *driver = &display->render_drivers[i];
        if (driver->info.name == SW_RenderDriver.info.name) {
            continue;
        }
        if (desired_driver
            && SDL_strcasecmp(desired_driver, driver->info.name) != 0) {
            continue;
        }
        data->renderer = driver->CreateRenderer(window, renderer_flags);
        if (data->renderer) {
            break;
        }
    }
    if (i == display->num_render_drivers) {
        SW_DestroyRenderer(renderer);
        SDL_SetError("Couldn't find display render driver");
        return NULL;
    }
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
    }

    /* Create the textures we'll use for display */
    for (i = 0; i < n; ++i) {
        data->texture[i] =
            CreateTexture(data->renderer, data->format, window->w, window->h);
        if (!data->texture[i]) {
            SW_DestroyRenderer(renderer);
            return NULL;
        }
    }
    data->current_texture = 0;

    /* Create a surface we'll use for rendering */
    data->surface.flags = SDL_PREALLOC;
    data->surface.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
    if (!data->surface.format) {
        SW_DestroyRenderer(renderer);
        return NULL;
    }
    SDL_SetSurfacePalette(&data->surface, display->palette);

    /* Set up a palette watch on the display palette */
    if (display->palette) {
        SDL_AddPaletteWatch(display->palette, DisplayPaletteChanged, data);
    }

    return renderer;
}
Exemple #23
0
SDL_Surface *
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
{
    SDL_DisplayMode desktop_mode;
    SDL_DisplayMode mode;
    int window_x = SDL_WINDOWPOS_UNDEFINED;
    int window_y = SDL_WINDOWPOS_UNDEFINED;
    Uint32 window_flags;
    Uint32 desktop_format;
    Uint32 desired_format;
    Uint32 surface_flags;

    if (!SDL_GetVideoDevice()) {
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
            return NULL;
        }
    }
    
    SDL_GetDesktopDisplayMode(&desktop_mode);

    if (width == 0) {
        width = desktop_mode.w;
    }
    if (height == 0) {
        height = desktop_mode.h;
    }

    /* See if we can simply resize the existing window and surface */
    if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) {
        return SDL_PublicSurface;
    }

    /* Destroy existing window */
    SDL_PublicSurface = NULL;
    if (SDL_ShadowSurface) {
        SDL_FreeSurface(SDL_ShadowSurface);
        SDL_ShadowSurface = NULL;
    }
    if (SDL_VideoSurface) {
        SDL_DelPaletteWatch(SDL_VideoSurface->format->palette,
                            SDL_VideoPaletteChanged, NULL);
        SDL_FreeSurface(SDL_VideoSurface);
        SDL_VideoSurface = NULL;
    }
    if (SDL_VideoContext) {
        /* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */
        SDL_GL_DeleteContext(SDL_VideoContext);
        SDL_VideoContext = NULL;
    }
    if (SDL_VideoWindow) {
        SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y);
        SDL_DestroyWindow(SDL_VideoWindow);
    }

    /* Set up the event filter */
    if (!SDL_GetEventFilter(NULL, NULL)) {
        SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
    }

    /* Create a new window */
    window_flags = SDL_WINDOW_SHOWN;
    if (flags & SDL_FULLSCREEN) {
        window_flags |= SDL_WINDOW_FULLSCREEN;
    }
    if (flags & SDL_OPENGL) {
        window_flags |= SDL_WINDOW_OPENGL;
    }
    if (flags & SDL_RESIZABLE) {
        window_flags |= SDL_WINDOW_RESIZABLE;
    }
    if (flags & SDL_NOFRAME) {
        window_flags |= SDL_WINDOW_BORDERLESS;
    }
    GetEnvironmentWindowPosition(width, height, &window_x, &window_y);
    SDL_SetFullscreenDisplayMode(NULL);
    SDL_VideoWindow =
        SDL_CreateWindow(wm_title, window_x, window_y, width, height,
                         window_flags);
    if (!SDL_VideoWindow) {
        return NULL;
    }
    SDL_SetWindowIcon(SDL_VideoWindow, SDL_VideoIcon);

    window_flags = SDL_GetWindowFlags(SDL_VideoWindow);
    surface_flags = 0;
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
        surface_flags |= SDL_FULLSCREEN;
    }
    if (window_flags & SDL_WINDOW_OPENGL) {
        surface_flags |= SDL_OPENGL;
    }
    if (window_flags & SDL_WINDOW_RESIZABLE) {
        surface_flags |= SDL_RESIZABLE;
    }
    if (window_flags & SDL_WINDOW_BORDERLESS) {
        surface_flags |= SDL_NOFRAME;
    }

    /* Set up the desired display mode */
    desktop_format = desktop_mode.format;
    if (desktop_format && ((flags & SDL_ANYFORMAT)
                           || (bpp == SDL_BITSPERPIXEL(desktop_format)))) {
        desired_format = desktop_format;
    } else {
        switch (bpp) {
        case 0:
            if (desktop_format) {
                desired_format = desktop_format;
            } else {
                desired_format = SDL_PIXELFORMAT_RGB888;
            }
            bpp = SDL_BITSPERPIXEL(desired_format);
            break;
        case 8:
            desired_format = SDL_PIXELFORMAT_INDEX8;
            break;
        case 15:
            desired_format = SDL_PIXELFORMAT_RGB555;
            break;
        case 16:
            desired_format = SDL_PIXELFORMAT_RGB565;
            break;
        case 24:
            desired_format = SDL_PIXELFORMAT_RGB24;
            break;
        case 32:
            desired_format = SDL_PIXELFORMAT_RGB888;
            break;
        default:
            SDL_SetError("Unsupported bpp in SDL_SetVideoMode()");
            return NULL;
        }
    }
    mode.format = desired_format;
    mode.w = width;
    mode.h = height;
    mode.refresh_rate = 0;

    /* Set the desired display mode */
    if (flags & SDL_FULLSCREEN) {
        if (SDL_SetFullscreenDisplayMode(&mode) < 0) {
            return NULL;
        }
    }

    /* If we're in OpenGL mode, just create a stub surface and we're done! */
    if (flags & SDL_OPENGL) {
        SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow);
        if (!SDL_VideoContext) {
            return NULL;
        }
        if (SDL_GL_MakeCurrent(SDL_VideoWindow, SDL_VideoContext) < 0) {
            return NULL;
        }
        SDL_VideoSurface =
            SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
        if (!SDL_VideoSurface) {
            return NULL;
        }
        SDL_VideoSurface->flags |= surface_flags;
        SDL_PublicSurface = SDL_VideoSurface;
        return SDL_PublicSurface;
    }

    /* Create a renderer for the window */
    if (SDL_CreateRenderer
        (SDL_VideoWindow, -1,
         SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD) < 0) {
        return NULL;
    }
    SDL_GetRendererInfo(&SDL_VideoRendererInfo);

    /* Create a texture for the screen surface */
    SDL_VideoTexture =
        SDL_CreateTexture(desired_format, SDL_TEXTUREACCESS_STREAMING, width,
                          height);

    if (!SDL_VideoTexture) {
        SDL_VideoTexture =
            SDL_CreateTexture(desktop_format,
                              SDL_TEXTUREACCESS_STREAMING, width, height);
    }
    if (!SDL_VideoTexture) {
        return NULL;
    }

    /* Create the screen surface */
    SDL_VideoSurface = CreateVideoSurface(SDL_VideoTexture);
    if (!SDL_VideoSurface) {
        return NULL;
    }
    SDL_VideoSurface->flags |= surface_flags;

    /* Set a default screen palette */
    if (SDL_VideoSurface->format->palette) {
        SDL_VideoSurface->flags |= SDL_HWPALETTE;
        SDL_DitherColors(SDL_VideoSurface->format->palette->colors,
                         SDL_VideoSurface->format->BitsPerPixel);
        SDL_AddPaletteWatch(SDL_VideoSurface->format->palette,
                            SDL_VideoPaletteChanged, SDL_VideoSurface);
        SDL_SetPaletteColors(SDL_VideoSurface->format->palette,
                             SDL_VideoSurface->format->palette->colors, 0,
                             SDL_VideoSurface->format->palette->ncolors);
    }

    /* Create a shadow surface if necessary */
    if ((bpp != SDL_VideoSurface->format->BitsPerPixel)
        && !(flags & SDL_ANYFORMAT)) {
        SDL_ShadowSurface =
            SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
        if (!SDL_ShadowSurface) {
            return NULL;
        }
        SDL_ShadowSurface->flags |= surface_flags;

        /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
        if (SDL_ShadowSurface->format->palette) {
            SDL_ShadowSurface->flags |= SDL_HWPALETTE;
            if (SDL_VideoSurface->format->palette) {
                SDL_SetSurfacePalette(SDL_ShadowSurface,
                                      SDL_VideoSurface->format->palette);
            } else {
                SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
                                 SDL_ShadowSurface->format->BitsPerPixel);
            }
            SDL_AddPaletteWatch(SDL_ShadowSurface->format->palette,
                                SDL_VideoPaletteChanged, SDL_ShadowSurface);
        }
    }
    SDL_PublicSurface =
        (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);

    SDL_VideoFlags = flags;

    ClearVideoSurface();

    SetupScreenSaver(flags);

    /* We're finally done! */
    return SDL_PublicSurface;
}
Exemple #24
0
/*
 * Sets the window icon
 */
static void
SetSDLIcon()
{
	SDL_Surface *icon;
	SDL_Color transColor, solidColor;
	Uint8 *ptr;
	int i;
	int mask;

	icon = SDL_CreateRGBSurface(SDL_SWSURFACE,
			q2icon_width, q2icon_height, 8,
			0, 0, 0, 0);

	if (icon == NULL)
	{
		return;
	}

	SDL_SetColorKey(icon, SDL_SRCCOLORKEY, 0);

	transColor.r = 255;
	transColor.g = 255;
	transColor.b = 255;

	solidColor.r = 0;
	solidColor.g = 16;
	solidColor.b = 0;

#if SDL_VERSION_ATLEAST(2, 0, 0)
	// only SDL2 has alphas there and they must be set apparently
	transColor.a = 0;
	solidColor.a = 255;

	SDL_Palette* palette = SDL_AllocPalette(256);
	SDL_SetPaletteColors(palette, &transColor, 0, 1);
	SDL_SetPaletteColors(palette, &solidColor, 1, 1);

	SDL_SetSurfacePalette(icon, palette);
#else
	SDL_SetColors(icon, &transColor, 0, 1);
	SDL_SetColors(icon, &solidColor, 1, 1);
#endif

	ptr = (Uint8 *)icon->pixels;

	for (i = 0; i < sizeof(q2icon_bits); i++)
	{
		for (mask = 1; mask != 0x100; mask <<= 1)
		{
			*ptr = (q2icon_bits[i] & mask) ? 1 : 0;
			ptr++;
		}
	}
#if SDL_VERSION_ATLEAST(2, 0, 0)
	SDL_SetWindowIcon(window, icon);
	SDL_FreePalette(palette);
#else
	SDL_WM_SetIcon(icon, NULL);
#endif

	SDL_FreeSurface(icon);
}