Split off SDL-specific colormap bits into SDL domain; fixed minor SDL_Palette access bug; some colormap processing cleanups

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3433 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
avolkov
2009-12-17 01:24:02 +00:00
parent bcb20a36c0
commit 40d81864d4
15 changed files with 264 additions and 120 deletions
+1
View File
@@ -1,4 +1,5 @@
Changes towards version 0.7:
- Split off SDL-specific colormap bits into SDL domain - Alex
- Fixed planet blinking when exiting scan (bug #799) - Alex
- Restore menu sounds after editing a control set name (bug #1066) - Alex
- Use an own 'UniChar' rather than 'wchar_t', which may not be large
+77 -69
View File
@@ -16,7 +16,8 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "gfx_common.h"
#include "libs/graphics/cmap.h"
#include "libs/threadlib.h"
#include "libs/timelib.h"
#include "libs/inplib.h"
#include "libs/log.h"
@@ -48,10 +49,11 @@ static TimeCount fadeStartTime;
static sint32 fadeInterval;
#define SPARE_COLORMAPS 20
#define MAP_POOL_SIZE (MAX_COLORMAPS + SPARE_COLORMAPS)
static TFB_ColorMap mappool[MAP_POOL_SIZE];
// Colormaps are rapidly replaced in some parts of the game, so
// it pays to have some spares on hand
static TFB_ColorMap *poolhead;
static int poolcount;
static TFB_ColorMap * colormaps[MAX_COLORMAPS];
static int mapcount;
@@ -65,11 +67,6 @@ InitColorMaps (void)
// init colormaps
maplock = CreateMutex ("Colormaps Lock", SYNC_CLASS_TOPLEVEL | SYNC_CLASS_VIDEO);
// init static pool
for (i = 0; i < MAP_POOL_SIZE - 1; ++i)
mappool[i].next = mappool + i + 1;
mappool[i].next = NULL;
poolhead = mappool;
// init xform control
XFormControl.Highest = -1;
@@ -81,6 +78,15 @@ InitColorMaps (void)
void
UninitColorMaps (void)
{
TFB_ColorMap *next;
// free spares
for ( ; poolhead; poolhead = next)
{
next = poolhead->next;
HFree (poolhead);
}
// uninit xform control
DestroyMutex (XFormControl.Lock);
@@ -94,12 +100,22 @@ alloc_colormap (void)
{
TFB_ColorMap *map;
if (!poolhead)
return NULL;
map = poolhead;
poolhead = map->next;
if (poolhead)
{ // have some spares
map = poolhead;
poolhead = map->next;
--poolcount;
}
else
{ // no spares, need a new one
map = HMalloc (sizeof (*map));
map->palette = AllocNativePalette ();
if (!map->palette)
{
HFree (map);
return NULL;
}
}
map->next = NULL;
map->index = -1;
map->refcount = 1;
@@ -117,26 +133,9 @@ clone_colormap (TFB_ColorMap *from, int index)
map = alloc_colormap ();
if (!map)
{
static DWORD NextTime = 0;
DWORD Now;
if (!from)
{
log_add (log_Warning, "FATAL: clone_colormap(): "
"no maps available");
exit (EXIT_FAILURE);
}
Now = GetTimeCounter ();
if (Now >= NextTime)
{
log_add (log_Warning, "clone_colormap(): static pool exhausted");
NextTime = Now + ONE_SECOND;
}
// just overwrite the current one -- better than aborting
map = from;
from->refcount++;
log_add (log_Warning, "FATAL: clone_colormap(): "
"could not allocate a map");
exit (EXIT_FAILURE);
}
else
{ // fresh new map
@@ -158,8 +157,17 @@ free_colormap (TFB_ColorMap *map)
return;
}
map->next = poolhead;
poolhead = map;
if (poolcount < SPARE_COLORMAPS)
{ // return to the spare pool
map->next = poolhead;
poolhead = map;
++poolcount;
}
else
{ // don't need any more spares
FreeNativePalette (map->palette);
HFree (map);
}
}
static inline TFB_ColorMap *
@@ -216,21 +224,15 @@ TFB_GetColorMap (int index)
}
void
TFB_ColorMapToRGB (Color *pal, int index)
GetColorMapColors (Color *colors, TFB_ColorMap *map)
{
TFB_ColorMap *map = NULL;
if (index < mapcount)
map = colormaps[index];
int i;
if (!map)
{
log_add (log_Warning, "TFB_ColorMapToRGB(): "
"requested non-present colormap %d", index);
return;
}
memcpy (pal, map->colors, sizeof map->colors);
for (i = 0; i < NUMBER_OF_PLUTVALS; ++i)
colors[i] = GetNativePaletteColor (map->palette, i);
}
BOOLEAN
@@ -279,18 +281,21 @@ SetColorMap (COLORMAPPTR map)
for (mpp = colormaps + start; start <= end; ++start, ++mpp)
{
int i;
Color *pal;
TFB_ColorMap *newmap;
TFB_ColorMap *oldmap;
oldmap = *mpp;
newmap = clone_colormap (oldmap, start);
for (i = 0, pal = newmap->colors; i < NUMBER_OF_PLUTVALS; ++i, ++pal)
for (i = 0; i < NUMBER_OF_PLUTVALS; ++i, colors += PLUTVAL_BYTE_SIZE)
{
pal->r = *colors++;
pal->g = *colors++;
pal->b = *colors++;
Color color;
color.a = 0xff;
color.r = colors[PLUTVAL_RED];
color.g = colors[PLUTVAL_GREEN];
color.b = colors[PLUTVAL_BLUE];
SetNativePaletteColor (newmap->palette, i, color);
}
*mpp = newmap;
@@ -418,6 +423,11 @@ finish_colormap_xform (int which)
}
}
static inline BYTE
blendChan (BYTE c1, BYTE c2, int weight, int scale)
{
return c1 + ((int)c2 - c1) * weight / scale;
}
/* This gives the XFormColorMap task a timeslice to do its thing
* Only one thread should ever be allowed to be calling this at any time
@@ -456,34 +466,32 @@ XFormColorMap_step (void)
{
#define XFORM_SCALE 0x10000
TFB_ColorMap *newmap = NULL;
UBYTE *pNewCMap;
Color *pCurCMap, *pOldCMap;
UBYTE *newClr;
Color *oldClr;
int frac;
int i;
newmap = clone_colormap (curmap, index);
pCurCMap = newmap->colors;
pOldCMap = control->OldCMap;
pNewCMap = (UBYTE*)control->CMapPtr + 2;
oldClr = control->OldCMap;
newClr = (UBYTE*)control->CMapPtr + 2;
frac = (int)(control->Ticks - TicksLeft) * XFORM_SCALE
/ control->Ticks;
for (i = 0; i < NUMBER_OF_PLUTVALS; i++, ++pCurCMap, ++pOldCMap)
for (i = 0; i < NUMBER_OF_PLUTVALS; ++i, ++oldClr,
newClr += PLUTVAL_BYTE_SIZE)
{
pCurCMap->r = (UBYTE)(pOldCMap->r +
((int)*pNewCMap - pOldCMap->r) * frac / XFORM_SCALE);
pNewCMap++;
Color color;
pCurCMap->g = (UBYTE)(pOldCMap->g +
((int)*pNewCMap - pOldCMap->g) * frac / XFORM_SCALE);
pNewCMap++;
pCurCMap->b = (UBYTE)(pOldCMap->b +
((int)*pNewCMap - pOldCMap->b) * frac / XFORM_SCALE);
pNewCMap++;
color.a = 0xff;
color.r = blendChan (oldClr->r, newClr[PLUTVAL_RED],
frac, XFORM_SCALE);
color.g = blendChan (oldClr->g, newClr[PLUTVAL_GREEN],
frac, XFORM_SCALE);
color.b = blendChan (oldClr->b, newClr[PLUTVAL_BLUE],
frac, XFORM_SCALE);
SetNativePaletteColor (newmap->palette, i, color);
}
colormaps[index] = newmap;
@@ -575,7 +583,7 @@ XFormPLUT (COLORMAPPTR ColorMapPtr, SIZE TimeInterval)
log_add (log_Warning, "BUG: XFormPLUT(): no current map");
return (0);
}
memcpy (control->OldCMap, map->colors, sizeof (map->colors));
GetColorMapColors (control->OldCMap, map);
UnlockMutex (maplock);
control->CMapIndex = index;
+28 -5
View File
@@ -16,27 +16,44 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "tfb_draw.h"
#ifndef CMAP_H
#define CMAP_H
#include "libs/gfxlib.h"
#define MAX_COLORMAPS 250
// These are pertinent to colortable file format
// We load colormaps as binary and parse them when needed
#define PLUTVAL_BYTE_SIZE 3
// Channel order in colormap tables
#define PLUTVAL_RED 0
#define PLUTVAL_GREEN 1
#define PLUTVAL_BLUE 2
#define NUMBER_OF_PLUTVALS 256
// Size of the colormap in a colortable file
#define PLUT_BYTE_SIZE (PLUTVAL_BYTE_SIZE * NUMBER_OF_PLUTVALS)
#define FADE_NO_INTENSITY 0
#define FADE_NORMAL_INTENSITY 255
#define FADE_FULL_INTENSITY 510
typedef struct NativePalette NativePalette;
typedef struct tfb_colormap
{
Color colors[NUMBER_OF_PLUTVALS];
int index;
// Colormap index as the game sees it
int version;
// Version goes up every time the colormap changes. This may
// be due to SetColorMap() or at every transformation step
// of XFormColorMap(). Paletted TFB_Images track the last
// colormap version they were drawn with for optimization.
int refcount;
struct tfb_colormap *next;
// for spares linking
NativePalette *palette;
} TFB_ColorMap;
extern int GetFadeAmount (void);
@@ -44,11 +61,17 @@ extern int GetFadeAmount (void);
extern void InitColorMaps (void);
extern void UninitColorMaps (void);
extern void TFB_ColorMapToRGB (Color *pal, int colormap_index);
extern void GetColorMapColors (Color *colors, TFB_ColorMap *);
extern TFB_ColorMap * TFB_GetColorMap (int index);
extern void TFB_ReturnColorMap (TFB_ColorMap *map);
extern BOOLEAN XFormColorMap_step (void);
// Native
NativePalette* AllocNativePalette (void);
void FreeNativePalette (NativePalette *);
void SetNativePaletteColor (NativePalette *, int index, Color);
Color GetNativePaletteColor (NativePalette *, int index);
#endif
#endif /* CMAP_H */
-2
View File
@@ -99,6 +99,4 @@ extern int ScreenHeightActual;
extern int ScreenColorDepth;
extern int GraphicsDriver;
#include "cmap.h"
#endif
+1
View File
@@ -1,4 +1,5 @@
uqm_CFILES="3do_blt.c 3do_funcs.c 3do_getbody.c dcqueue.c opengl.c
palette.c
primitives.c pure.c rndzoom.c sdl_common.c
scalers.c 2xscalers.c
2xscalers_mmx.c 2xscalers_sse.c 2xscalers_3dnow.c
+48 -38
View File
@@ -18,10 +18,12 @@
#include SDL_INCLUDE(SDL.h)
#include "sdl_common.h"
#include "libs/graphics/gfx_common.h"
#include "libs/graphics/sdl/primitives.h"
#include "libs/graphics/tfb_draw.h"
#include "libs/graphics/cmap.h"
#include "libs/log.h"
#include "libs/memlib.h"
#include "primitives.h"
#include "palette.h"
#include "rotozoom.h"
#include "options.h"
#include "types.h"
@@ -80,13 +82,15 @@ TFB_DrawCanvas_Rect (RECT *rect, Color color, TFB_Canvas target)
SDL_FillRect (dst, &sr, sdlColor);
}
// XXX: If a colormap is passed in, it has to have been acquired via
// TFB_GetColorMap(). We release the colormap at the end.
void
TFB_DrawCanvas_Image (TFB_Image *img, int x, int y, int scale,
TFB_ColorMap *cmap, TFB_Canvas target)
{
SDL_Rect srcRect, targetRect, *pSrcRect;
SDL_Surface *surf;
Color *palette;
SDL_Palette *NormalPal;
if (img == 0)
{
@@ -97,15 +101,10 @@ TFB_DrawCanvas_Image (TFB_Image *img, int x, int y, int scale,
LockMutex (img->mutex);
if (cmap)
palette = cmap->colors;
else
palette = img->Palette;
NormalPal = ((SDL_Surface *)img->NormalImg)->format->palette;
// only set the new palette if it changed
if (((SDL_Surface *)img->NormalImg)->format->palette
&& cmap && img->colormap_version != cmap->version)
SDL_SetColors (img->NormalImg, (SDL_Color*)palette, 0, 256);
if (NormalPal && cmap && img->colormap_version != cmap->version)
SDL_SetColors (img->NormalImg, cmap->palette->colors, 0, 256);
if (scale != 0 && scale != GSCALE_IDENTITY)
{
@@ -114,9 +113,9 @@ TFB_DrawCanvas_Image (TFB_Image *img, int x, int y, int scale,
if (type == TFB_SCALE_TRILINEAR && img->MipmapImg)
{
// only set the new palette if it changed
if (((SDL_Surface *)img->MipmapImg)->format->palette
if (TFB_DrawCanvas_IsPaletted (img->MipmapImg)
&& cmap && img->colormap_version != cmap->version)
SDL_SetColors (img->MipmapImg, (SDL_Color*)palette, 0, 256);
SDL_SetColors (img->MipmapImg, cmap->palette->colors, 0, 256);
}
else if (type == TFB_SCALE_TRILINEAR && !img->MipmapImg)
{
@@ -125,8 +124,13 @@ TFB_DrawCanvas_Image (TFB_Image *img, int x, int y, int scale,
TFB_DrawImage_FixScaling (img, scale, type);
surf = img->ScaledImg;
if (surf->format->palette)
SDL_SetColors (surf, (SDL_Color*)palette, 0, 256);
if (TFB_DrawCanvas_IsPaletted (surf))
{
// We may only get a paletted scaled image if the source is
// paletted. Currently, all scaling targets are truecolor.
assert (NormalPal && NormalPal->colors);
SDL_SetColors (surf, NormalPal->colors, 0, NormalPal->ncolors);
}
srcRect.x = 0;
srcRect.y = 0;
@@ -149,6 +153,9 @@ TFB_DrawCanvas_Image (TFB_Image *img, int x, int y, int scale,
if (cmap)
{
img->colormap_version = cmap->version;
// TODO: Technically, this is not a proper place to release a
// colormap. As it stands now, the colormap must have been
// addrefed when passed to us.
TFB_ReturnColorMap (cmap);
}
@@ -248,6 +255,7 @@ TFB_DrawCanvas_FilledImage (TFB_Image *img, int x, int y, int scale,
{
SDL_Rect srcRect, targetRect, *pSrcRect;
SDL_Surface *surf;
SDL_Palette *palette;
int i;
bool force_fill = false;
@@ -299,7 +307,8 @@ TFB_DrawCanvas_FilledImage (TFB_Image *img, int x, int y, int scale,
targetRect.y = y - img->NormalHs.y;
}
if (surf->format->palette)
palette = surf->format->palette;
if (palette)
{ // set palette for fill-stamp
// Calling SDL_SetColors() results in an expensive src -> dst
// color-mapping operation for an SDL blit, following the call.
@@ -307,15 +316,13 @@ TFB_DrawCanvas_FilledImage (TFB_Image *img, int x, int y, int scale,
// TODO: generate a 32bpp filled image?
SDL_Color pal[256];
SDL_Color colors[256];
for (i = 0; i < 256; i++)
{
pal[i].r = color.r;
pal[i].g = color.g;
pal[i].b = color.b;
}
SDL_SetColors (surf, pal, 0, 256);
colors[0] = ColorToNative (color);
for (i = 1; i < palette->ncolors; i++)
colors[i] = colors[0];
SDL_SetColors (surf, colors, 0, palette->ncolors);
// reflect the change in *actual* image palette
img->colormap_version--;
}
@@ -488,7 +495,7 @@ TFB_DrawCanvas_New_ForScreen (int w, int h, BOOLEAN withalpha)
}
TFB_Canvas
TFB_DrawCanvas_New_Paletted (int w, int h, Color *palette,
TFB_DrawCanvas_New_Paletted (int w, int h, Color palette[256],
int transparent_index)
{
SDL_Surface *new_surf;
@@ -501,7 +508,7 @@ TFB_DrawCanvas_New_Paletted (int w, int h, Color *palette,
}
if (palette != NULL)
{
SDL_SetColors(new_surf, (SDL_Color *)palette, 0, 256);
TFB_DrawCanvas_SetPalette (new_surf, palette);
}
if (transparent_index >= 0)
{
@@ -608,21 +615,18 @@ TFB_DrawCanvas_ExtractPalette (TFB_Canvas canvas)
{
int i;
Color *result;
SDL_Surface *surf = (SDL_Surface *)canvas;
SDL_Palette *palette = surf->format->palette;
if (!surf->format->palette)
{
if (!palette)
return NULL;
}
result = (Color *) HMalloc (sizeof (Color) * 256);
for (i = 0; i < 256; ++i)
{
result[i].r = surf->format->palette->colors[i].r;
result[i].g = surf->format->palette->colors[i].g;
result[i].b = surf->format->palette->colors[i].b;
}
// There may be less colors in the surface than 256. Init to 0 first.
result = HCalloc (sizeof (Color) * 256);
assert (palette->ncolors <= 256);
for (i = 0; i < palette->ncolors; ++i)
result[i] = NativeToColor (palette->colors[i]);
return result;
}
@@ -656,9 +660,15 @@ TFB_DrawCanvas_IsPaletted (TFB_Canvas canvas)
}
void
TFB_DrawCanvas_SetPalette (TFB_Canvas target, Color *palette)
TFB_DrawCanvas_SetPalette (TFB_Canvas target, Color palette[256])
{
SDL_SetColors ((SDL_Surface *)target, (SDL_Color *)palette, 0, 256);
SDL_Color colors[256];
int i;
for (i = 0; i < 256; ++i)
colors[i] = ColorToNative (palette[i]);
SDL_SetColors ((SDL_Surface *)target, colors, 0, 256);
}
int
+47
View File
@@ -0,0 +1,47 @@
/*
* Copyright 2009 Alex Volkov <codepro@usa.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "palette.h"
#include "libs/memlib.h"
#include "libs/log.h"
NativePalette *
AllocNativePalette (void)
{
return HCalloc (sizeof (NativePalette));
}
void
FreeNativePalette (NativePalette *palette)
{
HFree (palette);
}
void
SetNativePaletteColor (NativePalette *palette, int index, Color color)
{
assert (index < NUMBER_OF_PLUTVALS);
palette->colors[index] = ColorToNative (color);
}
Color
GetNativePaletteColor (NativePalette *palette, int index)
{
assert (index < NUMBER_OF_PLUTVALS);
return NativeToColor (palette->colors[index]);
}
+53
View File
@@ -0,0 +1,53 @@
/*
* Copyright 2009 Alex Volkov <codepro@usa.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef PALETTE_H_INCL__
#define PALETTE_H_INCL__
#include "port.h"
#include SDL_INCLUDE(SDL.h)
#include "libs/graphics/cmap.h"
struct NativePalette
{
SDL_Color colors[NUMBER_OF_PLUTVALS];
};
static inline Color
NativeToColor (SDL_Color native)
{
Color color;
color.r = native.r;
color.g = native.g;
color.b = native.b;
color.a = 0xff; // fully opaque
return color;
}
static inline SDL_Color
ColorToNative (Color color)
{
SDL_Color native;
native.r = color.r;
native.g = color.g;
native.b = color.b;
native.unused = 0;
return native;
}
#endif /* PALETTE_H_INCL__ */
+1
View File
@@ -26,6 +26,7 @@
#include "options.h"
#include "uqmversion.h"
#include "libs/graphics/drawcmd.h"
#include "libs/graphics/cmap.h"
#include "libs/input/sdl/input.h"
// for ProcessInputEvent()
#include "bbox.h"
-2
View File
@@ -38,8 +38,6 @@ typedef struct _tfb_graphics_backend {
extern TFB_GRAPHICS_BACKEND *graphics_backend;
// constants for TFB_InitGraphics
extern SDL_Surface *SDL_Video;
extern SDL_Surface *SDL_Screen;
extern SDL_Surface *TransitionScreen;
+3 -3
View File
@@ -33,7 +33,7 @@ typedef enum {
} SCREEN;
#include "libs/graphics/gfx_common.h"
#include "cmap.h"
#include "libs/graphics/cmap.h"
typedef struct tfb_image
{
@@ -119,7 +119,7 @@ void TFB_DrawImage_FontChar (TFB_Char *, TFB_Image *backing, int x, int y,
TFB_Canvas TFB_DrawCanvas_New_TrueColor (int w, int h, BOOLEAN hasalpha);
TFB_Canvas TFB_DrawCanvas_New_ForScreen (int w, int h, BOOLEAN withalpha);
TFB_Canvas TFB_DrawCanvas_New_Paletted (int w, int h, Color *palette,
TFB_Canvas TFB_DrawCanvas_New_Paletted (int w, int h, Color palette[256],
int transparent_index);
TFB_Canvas TFB_DrawCanvas_New_ScaleTarget (TFB_Canvas canvas,
TFB_Canvas oldcanvas, int type, int last_type);
@@ -154,7 +154,7 @@ void TFB_DrawCanvas_FontChar (TFB_Char *, TFB_Image *backing, int x, int y,
TFB_Canvas target);
Color *TFB_DrawCanvas_ExtractPalette (TFB_Canvas canvas);
void TFB_DrawCanvas_SetPalette (TFB_Canvas target, Color *palette);
void TFB_DrawCanvas_SetPalette (TFB_Canvas target, Color palette[256]);
int TFB_DrawCanvas_GetTransparentIndex (TFB_Canvas canvas);
void TFB_DrawCanvas_SetTransparentIndex (TFB_Canvas canvas, int i,
BOOLEAN rleaccel);
+1
View File
@@ -28,6 +28,7 @@
#include <stdarg.h>
#include "libs/graphics/gfx_common.h"
#include "libs/graphics/cmap.h"
#include "libs/sound/sound.h"
#include "libs/input/input_common.h"
#include "libs/inplib.h"
+1 -1
View File
@@ -23,7 +23,7 @@
#include "element.h"
#include "setup.h"
#include "libs/compiler.h"
#include "libs/graphics/gfx_common.h"
#include "libs/graphics/cmap.h"
#include "libs/mathlib.h"
+2
View File
@@ -41,6 +41,8 @@ InitOscilloscope (DWORD x, DWORD y, DWORD width, DWORD height, FRAME f)
if (!scope_init)
{
TFB_Canvas scope_bg_canvas, scope_surf_canvas;
// TODO: this scope image copying does not properly account for
// the loaded colormaps. This should use regular primitives.
if (TFB_DrawCanvas_IsPaletted (scope_frame->image->NormalImg))
{
scope_bg_canvas = TFB_DrawCanvas_New_Paletted (width, height,
+1
View File
@@ -43,6 +43,7 @@
#include "libs/log.h"
#include "libs/gfxlib.h"
#include "libs/graphics/gfx_common.h"
#include "libs/graphics/tfb_draw.h"
#include "libs/misc.h"
#include "uqmversion.h"