Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
A few fixes:
Browse files Browse the repository at this point in the history
Fixed creating render texture framebuffer.
Removed the need for palette watch, added surface format caching.
Added an SDL_DONTFREE flag so you can't free the window and 1.2 shadow surfaces.
  • Loading branch information
slouken committed Feb 13, 2011
1 parent 2f2fd0a commit 2510d6c
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 308 deletions.
43 changes: 19 additions & 24 deletions include/SDL_pixels.h
Expand Up @@ -253,24 +253,20 @@ typedef struct SDL_Color
} SDL_Color;
#define SDL_Colour SDL_Color

typedef struct SDL_Palette SDL_Palette;
typedef int (*SDL_PaletteChangedFunc) (void *userdata, SDL_Palette * palette);
typedef struct SDL_PaletteWatch SDL_PaletteWatch;

struct SDL_Palette
typedef struct SDL_Palette
{
int ncolors;
SDL_Color *colors;

Uint32 version;
int refcount;
SDL_PaletteWatch *watch;
};
} SDL_Palette;

/**
* \note Everything in the pixel format structure is read-only.
*/
typedef struct SDL_PixelFormat
{
Uint32 format;
SDL_Palette *palette;
Uint8 BitsPerPixel;
Uint8 BytesPerPixel;
Expand All @@ -286,6 +282,8 @@ typedef struct SDL_PixelFormat
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
int refcount;
struct SDL_PixelFormat *next;
} SDL_PixelFormat;

/**
Expand Down Expand Up @@ -321,6 +319,16 @@ extern DECLSPEC Uint32 SDLCALL SDL_MasksToPixelFormatEnum(int bpp,
Uint32 Bmask,
Uint32 Amask);

/**
* \brief Create an SDL_PixelFormat structure from a pixel format enum.
*/
extern DECLSPEC SDL_PixelFormat * SDLCALL SDL_AllocFormat(Uint32 pixel_format);

/**
* \brief Free an SDL_PixelFormat structure.
*/
extern DECLSPEC void SDLCALL SDL_FreeFormat(SDL_PixelFormat *format);

/**
* \brief Create a palette structure with the specified number of color
* entries.
Expand All @@ -334,23 +342,10 @@ extern DECLSPEC Uint32 SDLCALL SDL_MasksToPixelFormatEnum(int bpp,
extern DECLSPEC SDL_Palette *SDLCALL SDL_AllocPalette(int ncolors);

/**
* \brief Add a callback function which is called when the palette changes.
*
* \sa SDL_DelPaletteWatch()
*/
extern DECLSPEC int SDLCALL SDL_AddPaletteWatch(SDL_Palette * palette,
SDL_PaletteChangedFunc
callback, void *userdata);

/**
* \brief Remove a callback function previously added with
* SDL_AddPaletteWatch().
*
* \sa SDL_AddPaletteWatch()
* \brief Set the palette for a pixel format structure.
*/
extern DECLSPEC void SDLCALL SDL_DelPaletteWatch(SDL_Palette * palette,
SDL_PaletteChangedFunc
callback, void *userdata);
extern DECLSPEC int SDLCALL SDL_SetPixelFormatPalette(SDL_PixelFormat * format,
SDL_Palette *palette);

/**
* \brief Set a range of colors in a palette.
Expand Down
1 change: 1 addition & 0 deletions include/SDL_surface.h
Expand Up @@ -54,6 +54,7 @@ extern "C" {
/*@{*/
#define SDL_PREALLOC 0x00000001 /**< Surface uses preallocated memory */
#define SDL_RLEACCEL 0x00000002 /**< Surface is RLE encoded */
#define SDL_DONTFREE 0x00000004 /**< Surface is referenced internally */
/*@}*//*Surface flags*/

/**
Expand Down
22 changes: 14 additions & 8 deletions src/SDL_compat.c
Expand Up @@ -93,12 +93,7 @@ SDL_GetVideoInfo(void)

/* Memory leak, compatibility code, who cares? */
if (!info.vfmt && SDL_GetDesktopDisplayMode(GetVideoDisplay(), &mode) == 0) {
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;

SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask, &Bmask,
&Amask);
info.vfmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
info.vfmt = SDL_AllocFormat(mode.format);
info.current_w = mode.w;
info.current_h = mode.h;
}
Expand Down Expand Up @@ -383,7 +378,7 @@ SDL_ResizeVideoMode(int width, int height, int bpp, Uint32 flags)
int w, h;

/* We can't resize something we don't have... */
if (!SDL_VideoWindow) {
if (!SDL_VideoSurface) {
return -1;
}

Expand All @@ -396,6 +391,9 @@ SDL_ResizeVideoMode(int width, int height, int bpp, Uint32 flags)
if (flags != SDL_VideoFlags) {
return -1;
}
if (bpp != SDL_VideoSurface->format->BitsPerPixel) {
return -1;
}

/* Resize the window */
SDL_GetWindowSize(SDL_VideoWindow, &w, &h);
Expand Down Expand Up @@ -469,6 +467,7 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
/* Destroy existing window */
SDL_PublicSurface = NULL;
if (SDL_ShadowSurface) {
SDL_ShadowSurface->flags &= ~SDL_DONTFREE;
SDL_FreeSurface(SDL_ShadowSurface);
SDL_ShadowSurface = NULL;
}
Expand Down Expand Up @@ -564,6 +563,7 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
return NULL;
}
SDL_ShadowSurface->flags |= surface_flags;
SDL_ShadowSurface->flags |= SDL_DONTFREE;

/* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
if (SDL_ShadowSurface->format->palette) {
Expand Down Expand Up @@ -670,7 +670,13 @@ SDL_DisplayFormatAlpha(SDL_Surface * surface)
optimised alpha format is written, add the converter here */
break;
}
format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
format = SDL_AllocFormat(SDL_MasksToPixelFormatEnum(32, rmask,
gmask,
bmask,
amask));
if (!format) {
return NULL;
}
converted = SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
SDL_FreeFormat(format);
return converted;
Expand Down
14 changes: 2 additions & 12 deletions src/render/SDL_render.c
Expand Up @@ -27,7 +27,6 @@
#include "SDL_log.h"
#include "SDL_render.h"
#include "SDL_sysrender.h"
#include "../video/SDL_pixels_c.h"
#include "software/SDL_render_sw_c.h"


Expand Down Expand Up @@ -306,8 +305,6 @@ SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
SDL_bool needAlpha;
Uint32 i;
Uint32 format;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
SDL_Texture *texture;

CHECK_RENDERER_MAGIC(renderer, NULL);
Expand All @@ -333,20 +330,13 @@ SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
}
}

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

texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
surface->w, surface->h);
if (!texture) {
return NULL;
}

if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
&& Bmask == fmt->Bmask && Amask == fmt->Amask) {
if (format == surface->format->format) {
if (SDL_MUSTLOCK(surface)) {
SDL_LockSurface(surface);
SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
Expand All @@ -359,7 +349,7 @@ SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
SDL_Surface *temp = NULL;

/* Set up a destination surface for the texture update */
SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
SDL_InitFormat(&dst_fmt, format);
temp = SDL_ConvertSurface(surface, &dst_fmt, 0);
if (temp) {
SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
Expand Down
1 change: 0 additions & 1 deletion src/render/software/SDL_render_sw.c
Expand Up @@ -24,7 +24,6 @@
#if !SDL_RENDER_DISABLED

#include "../SDL_sysrender.h"
#include "../../video/SDL_pixels_c.h"

#include "SDL_draw.h"
#include "SDL_blendfillrect.h"
Expand Down
14 changes: 2 additions & 12 deletions src/video/SDL_blit.c
Expand Up @@ -221,18 +221,8 @@ SDL_CalculateBlit(SDL_Surface * surface)
blit = SDL_CalculateBlitN(surface);
}
if (blit == NULL) {
Uint32 src_format =
SDL_MasksToPixelFormatEnum(surface->format->BitsPerPixel,
surface->format->Rmask,
surface->format->Gmask,
surface->format->Bmask,
surface->format->Amask);
Uint32 dst_format =
SDL_MasksToPixelFormatEnum(dst->format->BitsPerPixel,
dst->format->Rmask,
dst->format->Gmask,
dst->format->Bmask,
dst->format->Amask);
Uint32 src_format = surface->format->format;
Uint32 dst_format = dst->format->format;

blit =
SDL_ChooseBlitFunc(src_format, dst_format, map->info.flags,
Expand Down
6 changes: 1 addition & 5 deletions src/video/SDL_blit.h
Expand Up @@ -105,7 +105,7 @@ typedef struct SDL_BlitMap

/* the version count matches the destination; mismatch indicates
an invalid mapping */
unsigned int format_version;
Uint32 palette_version;
} SDL_BlitMap;

/* Functions found in SDL_blit.c */
Expand All @@ -129,10 +129,6 @@ extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface * surface);
#define DECLARE_ALIGNED(t,v,a) t v
#endif

#define FORMAT_EQUAL(A, B) \
((A)->BitsPerPixel == (B)->BitsPerPixel \
&& ((A)->Rmask == (B)->Rmask) && ((A)->Amask == (B)->Amask))

/* Load pixel of the specified format from a buffer and get its R-G-B values */
/* FIXME: rescale values to 0..255 here? */
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b) \
Expand Down
14 changes: 6 additions & 8 deletions src/video/SDL_bmp.c
Expand Up @@ -437,17 +437,15 @@ SDL_SaveBMP_RW(SDL_Surface * saveme, SDL_RWops * dst, int freedst)
/* If the surface has a colorkey or alpha channel we'll save a
32-bit BMP with alpha channel, otherwise save a 24-bit BMP. */
if (save32bit) {
SDL_InitFormat(&format, 32,
0x00FF0000, 0x0000FF00, 0x000000FF,
0xFF000000);
} else {
SDL_InitFormat(&format, 24,
SDL_InitFormat(&format,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x00FF0000, 0x0000FF00, 0x000000FF,
SDL_PIXELFORMAT_ARGB8888
#else
0x000000FF, 0x0000FF00, 0x00FF0000,
SDL_PIXELFORMAT_BGRA8888
#endif
0);
);
} else {
SDL_InitFormat(&format, SDL_PIXELFORMAT_BGR24);
}
surface = SDL_ConvertSurface(saveme, &format, 0);
if (!surface) {
Expand Down

0 comments on commit 2510d6c

Please sign in to comment.