diff --git a/sc2/src/sc2code/comm.c b/sc2/src/sc2code/comm.c index fafc55f4b..b89d8cb06 100644 --- a/sc2/src/sc2code/comm.c +++ b/sc2/src/sc2code/comm.c @@ -481,6 +481,8 @@ XFormPLUT (COLORMAPPTR ColorMapPtr, SIZE TimeInterval) { DWORD TimeOut; + TimeInterval = 0; // added by Mika + FlushPLUTXForms (); TaskControl.CMapPtr = ColorMapPtr; @@ -497,7 +499,7 @@ XFormPLUT (COLORMAPPTR ColorMapPtr, SIZE TimeInterval) ) == 0 ) { - SetColorMap (ColorMapPtr); + SetColorMap (ColorMapPtr, TFB_COLORMAP_COMM); ColorChange = TRUE; TimeOut = GetTimeCounter (); } @@ -1154,7 +1156,7 @@ AlienTalkSegue (COUNT wait_track) if (!pCurInputState->Initialized) { - SetColorMap (GetColorMapAddress (CommData.AlienColorMap)); + SetColorMap (GetColorMapAddress (CommData.AlienColorMap), TFB_COLORMAP_COMM); DrawAlienFrame (CommData.AlienFrame, NULL_PTR); UpdateSpeechGraphics (TRUE); @@ -1537,6 +1539,7 @@ BatchGraphics (); DestroyStringTable (ReleaseStringTable (CommData.ConversationPhrases)); DestroyMusic ((MUSIC_REF)CommData.AlienSong); + TFB_ReleaseColorMap (); DestroyColorMap (ReleaseColorMap (CommData.AlienColorMap)); DestroyFont (ReleaseFont (CommData.AlienFont)); DestroyDrawable (ReleaseDrawable (CommData.AlienFrame)); diff --git a/sc2/src/sc2code/comm/comandr/comandr.c b/sc2/src/sc2code/comm/comandr/comandr.c index 28b3c1cd6..e51c139d1 100644 --- a/sc2/src/sc2code/comm/comandr/comandr.c +++ b/sc2/src/sc2code/comm/comandr/comandr.c @@ -561,6 +561,7 @@ GiveRadios (RESPONSE_REF R) XFormPLUT (GetColorMapAddress ( SetAbsColorMapIndex (CommData.AlienColorMap, 0) ), 0); + TFB_ReleaseColorMap (); ClearSemaphore (&GraphicsSem); AlienTalkSegue ((COUNT)~0); diff --git a/sc2/src/sc2code/hyper.c b/sc2/src/sc2code/hyper.c index 58365ae56..3d36b4702 100644 --- a/sc2/src/sc2code/hyper.c +++ b/sc2/src/sc2code/hyper.c @@ -17,6 +17,7 @@ */ #include "starcon.h" +#include "libs/graphics/gfx_common.h" //Added by Chris @@ -335,12 +336,12 @@ LoadHyperspace (void) if (GET_GAME_STATE (ARILOU_SPACE_SIDE) <= 1) { SetContextBackGroundColor (BUILD_COLOR (MAKE_RGB15 (0x7, 0x00, 0x00), 0x2F)); - SetColorMap (GetColorMapAddress (hypercmaps[0])); + SetColorMap (GetColorMapAddress (hypercmaps[0]), TFB_COLORMAP_HYPERSPACE); } else { SetContextBackGroundColor (BUILD_COLOR (MAKE_RGB15 (0x00, 0x1A, 0x00), 0x2F)); - SetColorMap (GetColorMapAddress (hypercmaps[1])); + SetColorMap (GetColorMapAddress (hypercmaps[1]), TFB_COLORMAP_QUASISPACE); SET_GAME_STATE (USED_BROADCASTER, 0); SET_GAME_STATE (BROADCASTER_RESPONSE, 0); } diff --git a/sc2/src/sc2code/libs/gfxlib.h b/sc2/src/sc2code/libs/gfxlib.h index 982be8894..6595d490d 100644 --- a/sc2/src/sc2code/libs/gfxlib.h +++ b/sc2/src/sc2code/libs/gfxlib.h @@ -366,7 +366,7 @@ extern BOOLEAN DestroyOverlayWindow (FRAME Display, DRAWABLE Drawable); extern void ScrollDisplay (FRAME Display, COORD x, COORD y); extern BOOLEAN ReadColorMap (COLORMAPPTR ColorMapPtr); -extern BOOLEAN SetColorMap (COLORMAPPTR ColorMapPtr); +extern BOOLEAN SetColorMap (COLORMAPPTR ColorMapPtr, int type); extern BOOLEAN BatchColorMap (COLORMAPPTR ColorMapPtr); extern CYCLE_REF CycleColorMap (COLORMAPPTR ColorMapPtr, COUNT Cycles, SIZE TimeInterval); diff --git a/sc2/src/sc2code/libs/graphics/cmap.c b/sc2/src/sc2code/libs/graphics/cmap.c index eecbf99ad..c1b80d304 100644 --- a/sc2/src/sc2code/libs/graphics/cmap.c +++ b/sc2/src/sc2code/libs/graphics/cmap.c @@ -16,40 +16,124 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include "libs/graphics/sdl/sdl_common.h" #include "gfx_common.h" - static struct { - COLORMAPPTR CMapPtr; - SIZE Ticks; + COLORMAPPTR CMapPtr; + SIZE Ticks; union { - COUNT NumCycles; - TASK XFormTask; + COUNT NumCycles; + TASK XFormTask; } tc; } TaskControl; - +UBYTE _batch_flags; DWORD* _varPLUTs; static unsigned int varPLUTsize = VARPLUTS_SIZE; -UBYTE _batch_flags; + +static BOOLEAN has_colormap = FALSE; +static TFB_Palette cmap_rgb[256]; +static int cmap_type = 0; -UBYTE *current_colormap; -int current_colormap_size; +BOOLEAN TFB_HasColorMap () +{ + return has_colormap; +} +int TFB_GetColorMapType () +{ + return cmap_type; +} + +void TFB_ReleaseColorMap () +{ + //printf("TFB_ReleaseColorMap()\n"); + has_colormap = FALSE; +} + +void TFB_ColorMapToRGB (UBYTE *colors) +{ + DWORD i, k, cval; + UBYTE j, r, g, b; + + for (i = 0; i < 32;) + { + cval = MAKE_DWORD (MAKE_WORD (colors[3], colors[2]), MAKE_WORD (colors[1], colors[0])); + colors += 4; + + r = (UBYTE)((cval >> 26) & 0x1f); + g = (UBYTE)((cval >> 21) & 0x1f); + b = (UBYTE)((cval >> 16) & 0x1f); + + for (j = 0; j < 8; ++j) + { + k = ((j << 5) + i); + + cmap_rgb[k].r = r * (j + 1); + cmap_rgb[k].g = g * (j + 1); + cmap_rgb[k].b = b * (j + 1); + } + + ++i; + + r = (UBYTE)((cval >> 10) & 0x1f); + g = (UBYTE)((cval >> 5) & 0x1f); + b = (UBYTE)(cval & 0x1f); + + for (j = 0; j < 8; ++j) + { + k = ((j << 5) + i); + cmap_rgb[k].r = r * (j + 1); + cmap_rgb[k].g = g * (j + 1); + cmap_rgb[k].b = b * (j + 1); + } + + ++i; + } +} + +BOOLEAN TFB_CopyRGBColorMap (TFB_Palette *dst) +{ + if (cmap_type == TFB_COLORMAP_HYPERSPACE || + cmap_type == TFB_COLORMAP_QUASISPACE) + { + // TODO: some special colormaps still not implemented + return FALSE; + + } + else if (cmap_type == TFB_COLORMAP_PLANET) + { + int i; + + for (i = 0; i < 32; ++i) + { + dst[251-i].r = cmap_rgb[255-i].r; + dst[251-i].g = cmap_rgb[255-i].g; + dst[251-i].b = cmap_rgb[255-i].b; + } + } + else + { + memcpy(dst, cmap_rgb, sizeof(TFB_Palette) * 256); + } + + return TRUE; +} // Status: Implemented BOOLEAN BatchColorMap (COLORMAPPTR ColorMapPtr) { - return (SetColorMap (ColorMapPtr)); + return (SetColorMap (ColorMapPtr, TFB_COLORMAP_NONE)); } -// Status: Partially implemented +// Status: Implemented BOOLEAN -SetColorMap (COLORMAPPTR map) +SetColorMap (COLORMAPPTR map, int type) { int start, end, bytes; UBYTE *colors = (UBYTE*)map; @@ -75,19 +159,19 @@ SetColorMap (COLORMAPPTR map) vp = (UBYTE*)_varPLUTs + (start * PLUT_BYTE_SIZE); memcpy (vp, colors, bytes); + cmap_type = type; + TFB_ColorMapToRGB (vp); + has_colormap = TRUE; - current_colormap = vp; - current_colormap_size = bytes; - - //printf("SetColorMap(): vp %x map %x bytes %d\n",vp, map, bytes); + //printf("SetColorMap(): vp %x map %x bytes %d, start %d end %d\n",vp, map, bytes, start, end); return TRUE; } void _threedo_set_colors (UBYTE *colors, unsigned int indices) { - int i, start, end; - //unsigned int *ce; + int i, start, end; + //unsigned int *ce; start = (int)LOBYTE (indices); end = (int)HIBYTE (indices); @@ -95,16 +179,16 @@ _threedo_set_colors (UBYTE *colors, unsigned int indices) //ce = colorEntries; for (i = start; i <= end; i++) { - UBYTE r, g, b; + UBYTE r, g, b; - r = (*colors << 2) | (*colors >> 4); - ++colors; - g = (*colors << 2) | (*colors >> 4); - ++colors; - b = (*colors << 2) | (*colors >> 4); - ++colors; + r = (*colors << 2) | (*colors >> 4); + ++colors; + g = (*colors << 2) | (*colors >> 4); + ++colors; + b = (*colors << 2) | (*colors >> 4); + ++colors; - //*ce++ = MakeCLUTColorEntry (i, r, g, b); + //*ce++ = MakeCLUTColorEntry (i, r, g, b); } //ceCt = end - start + 1; @@ -253,7 +337,6 @@ XFormColorMap (COLORMAPPTR ColorMapPtr, SIZE TimeInterval) //printf ("Partially implemented function activated: XFormColorMap()\n"); - FlushColorXForms (); if (ColorMapPtr == (COLORMAPPTR)0) @@ -278,7 +361,7 @@ XFormColorMap (COLORMAPPTR ColorMapPtr, SIZE TimeInterval) } //if (what == FadeAllToBlack || what == FadeAllToWhite || what == FadeAllToColor) - //_threedo_enable_fade (); + //_threedo_enable_fade (); if ((TaskControl.Ticks = TimeInterval) <= 0 || (TaskControl.tc.XFormTask = AddTask (xform_clut_task, 1024)) == 0) @@ -289,7 +372,7 @@ XFormColorMap (COLORMAPPTR ColorMapPtr, SIZE TimeInterval) else { do - TaskSwitch (); + TaskSwitch (); while (TaskControl.tc.XFormTask); TimeOut = GetTimeCounter () + TimeInterval + 1; @@ -302,11 +385,11 @@ XFormColorMap (COLORMAPPTR ColorMapPtr, SIZE TimeInterval) void FlushColorXForms() { - if (XForming) + //printf ("Partially implemented function activated: FlushColorXForms()\n"); + + if (XForming) { XForming = FALSE; TaskSwitch (); } - - //printf ("Partially implemented function activated: FlushColorXForms()\n"); } diff --git a/sc2/src/sc2code/libs/graphics/cmap.h b/sc2/src/sc2code/libs/graphics/cmap.h index e5c9cfc07..482df3f81 100644 --- a/sc2/src/sc2code/libs/graphics/cmap.h +++ b/sc2/src/sc2code/libs/graphics/cmap.h @@ -35,19 +35,21 @@ #define CYCLE_PENDING (1 << 5) #define ENABLE_CYCLE (1 << 6) -/*enum +enum { - FadeAllToWhite = 250, - FadeSomeToWhite, - FadeAllToBlack, - FadeAllToColor, - FadeSomeToBlack, - FadeSomeToColor -};*/ + TFB_COLORMAP_NONE, + TFB_COLORMAP_HYPERSPACE, + TFB_COLORMAP_QUASISPACE, + TFB_COLORMAP_PLANET, + TFB_COLORMAP_COMM +}; +extern DWORD* _varPLUTs; extern UBYTE _batch_flags; -extern UBYTE *current_colormap; -extern int current_colormap_size; +BOOLEAN TFB_HasColorMap (); +int TFB_GetColorMapType (); +void TFB_ReleaseColorMap (); +BOOLEAN TFB_CopyRGBColorMap (TFB_Palette *dst); #endif diff --git a/sc2/src/sc2code/libs/graphics/gfx_common.h b/sc2/src/sc2code/libs/graphics/gfx_common.h index 2437ed61b..3690b806b 100644 --- a/sc2/src/sc2code/libs/graphics/gfx_common.h +++ b/sc2/src/sc2code/libs/graphics/gfx_common.h @@ -27,7 +27,6 @@ #include "libs/gfxlib.h" #include "libs/vidlib.h" #include "libs/misc.h" -#include "cmap.h" #define TFB_WINDOW_CAPTION "The Ur-Quan Masters" @@ -240,4 +239,6 @@ extern int GraphicsDriver; int Starcon2Main (void *); +#include "cmap.h" + #endif diff --git a/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c b/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c index 935434f9d..422d250c3 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c +++ b/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c @@ -120,7 +120,7 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) } } - DrawCommand->image = (TFB_ImageStruct*) img; //TFB_Image + DrawCommand->image = (TFB_ImageStruct*) img; DrawCommand->UsePalette = FALSE; if (GetPrimType (PrimPtr) == STAMPFILL_PRIM) @@ -144,35 +144,21 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) } else { - // NOTE: following code for colormaps is experimental - if (img->NormalImg->format->BytesPerPixel == 1) + if (TFB_HasColorMap() && img->NormalImg->format->BytesPerPixel == 1) { - if (current_colormap && current_colormap_size <= 512) + // TODO: this currently uses a hack to avoid corrupting sun colors.. + // why sun won't work properly with colormaps, should be traced later + + int type = TFB_GetColorMapType(); + + if (type != TFB_COLORMAP_PLANET || + (type == TFB_COLORMAP_PLANET && (img->Palette[255].r != 248 || + img->Palette[255].g != 248 || img->Palette[255].b != 248))) { - int i, n; - SDL_Color *imgpal; - SDL_Color cmap_rgb[256]; - - n = TFB_ColorMapToRGB (cmap_rgb); - - //printf("draw cmap %x, size %d, n %d, imgw %d imgh %d\n",current_colormap, current_colormap_size, n, img->NormalImg->w, img->NormalImg->h); - - imgpal = img->NormalImg->format->palette->colors; - for (i = 0; i < 256; ++i) + if (TFB_CopyRGBColorMap(DrawCommand->Palette)) { - DrawCommand->Palette[i].r = imgpal[i].r; - DrawCommand->Palette[i].g = imgpal[i].g; - DrawCommand->Palette[i].b = imgpal[i].b; + DrawCommand->UsePalette = TRUE; } - - for (i = 0; i < n; ++i) - { - DrawCommand->Palette[252 - i].r = cmap_rgb[n - i - 1].r; - DrawCommand->Palette[252 - i].g = cmap_rgb[n - i - 1].g; - DrawCommand->Palette[252 - i].b = cmap_rgb[n - i - 1].b; - } - - DrawCommand->UsePalette = TRUE; } } } @@ -209,8 +195,6 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) SDL_mutexV (dst_img->mutex); } - current_colormap = 0; - SDL_mutexV (img->mutex); } @@ -220,8 +204,6 @@ fillrect_blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) int r, g, b; DWORD c32k; - current_colormap = 0; - c32k = GetPrimColor (PrimPtr) >> 8; //shift out color index r = (c32k >> (10 - (8 - 5))) & 0xF8; g = (c32k >> (5 - (8 - 5))) & 0xF8; @@ -295,8 +277,6 @@ line_blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) int r, g, b; DWORD c32k; - current_colormap = 0; - c32k = GetPrimColor (PrimPtr) >> 8; // shift out color index r = (c32k >> (10 - (8 - 5))) & 0xF8; g = (c32k >> (5 - (8 - 5))) & 0xF8; @@ -332,8 +312,6 @@ line_blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) static void read_screen (PRECT lpRect, FRAMEPTR DstFramePtr) { - current_colormap = 0; - if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) != SCREEN_DRAWABLE || TYPE_GET (DstFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE || !(TYPE_GET (GetFrameParentDrawable (DstFramePtr) diff --git a/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c b/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c index 1bee094f6..429f2ef8e 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c +++ b/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c @@ -243,34 +243,6 @@ TFB_FreeImage (TFB_Image *img) } } -int TFB_ColorMapToRGB (SDL_Color *cmap_rgb) -{ - int i, n; - UBYTE *colors; - DWORD c; - - colors = current_colormap; - n = current_colormap_size / 2; - if (n > 256) - n = 256; - - for (i = 0; i < n;) - { - c = MAKE_DWORD (MAKE_WORD (colors[3], colors[2]), MAKE_WORD (colors[1], colors[0])); - colors += 4; - - cmap_rgb[i].r = (Uint8)(((c >> 26) & 0x1f) << 3); - cmap_rgb[i].g = (Uint8)(((c >> 21) & 0x1f) << 3); - cmap_rgb[i++].b = (Uint8)(((c >> 16) & 0x1f) << 3); - - cmap_rgb[i].r = (Uint8)(((c >> 10) & 0x1f) << 3); - cmap_rgb[i].g = (Uint8)(((c >> 5 ) & 0x1f) << 3); - cmap_rgb[i++].b = (Uint8)(((c >> 0 ) & 0x1f) << 3); - } - - return n; -} - void TFB_SwapBuffers () { @@ -347,7 +319,6 @@ TFB_FlushGraphics () // Only call from main thread!! if (ShowFPS) TFB_ComputeFPS (); - while (DrawCommandQueue->Size > 0) { TFB_DrawCommand DC_real; diff --git a/sc2/src/sc2code/libs/graphics/sdl/sdl_common.h b/sc2/src/sc2code/libs/graphics/sdl/sdl_common.h index c7f21b863..725b1494a 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/sdl_common.h +++ b/sc2/src/sc2code/libs/graphics/sdl/sdl_common.h @@ -53,7 +53,6 @@ typedef struct tfb_image TFB_Image *TFB_LoadImage (SDL_Surface *img); void TFB_FreeImage (TFB_Image *img); -int TFB_ColorMapToRGB (SDL_Color *cmap_rgb); void TFB_SwapBuffers (); SDL_Surface* TFB_DisplayFormatAlpha (SDL_Surface *surface); diff --git a/sc2/src/sc2code/oscill.c b/sc2/src/sc2code/oscill.c index 48ace3cc3..80b5459f3 100644 --- a/sc2/src/sc2code/oscill.c +++ b/sc2/src/sc2code/oscill.c @@ -52,7 +52,7 @@ typedef signed int sint32; #define NUMBER_OF_PLUT_UINT32s (NUMBER_OF_PLUTVALS >> 1) #define GET_VAR_PLUT(i) (_varPLUTs + (i) * NUMBER_OF_PLUT_UINT32s) -extern uint32 *_varPLUTs; +//extern uint32 *_varPLUTs; #define NUM_THINGS 256 diff --git a/sc2/src/sc2code/planets/planets.c b/sc2/src/sc2code/planets/planets.c index a088a2793..63004b959 100644 --- a/sc2/src/sc2code/planets/planets.c +++ b/sc2/src/sc2code/planets/planets.c @@ -210,6 +210,7 @@ FreePlanet (void) pSolarSysState->hTopoData = 0; DestroyDrawable (ReleaseDrawable (pSolarSysState->TopoFrame)); pSolarSysState->TopoFrame = 0; + TFB_ReleaseColorMap (); DestroyColorMap (ReleaseColorMap (pSolarSysState->OrbitalCMap)); pSolarSysState->OrbitalCMap = 0; diff --git a/sc2/src/sc2code/planets/solarsys.c b/sc2/src/sc2code/planets/solarsys.c index 59c7c11a1..0307750fd 100644 --- a/sc2/src/sc2code/planets/solarsys.c +++ b/sc2/src/sc2code/planets/solarsys.c @@ -115,6 +115,7 @@ FreeIPData (void) SISIPFrame = 0; DestroyDrawable (ReleaseDrawable (SunFrame)); SunFrame = 0; + TFB_ReleaseColorMap (); DestroyColorMap (ReleaseColorMap (OrbitalCMap)); OrbitalCMap = 0; DestroyDrawable (ReleaseDrawable (OrbitalFrame)); @@ -575,7 +576,7 @@ if (!(draw_sys_flags & DRAW_PLANETS)) SetColorMap (GetColorMapAddress ( SetAbsColorMapIndex (OrbitalCMap, PLANCOLOR (Type)) - )); + ), TFB_COLORMAP_PLANET); Size = PLANSIZE (Type); if (denom == 2 || xnumer > (COUNT)DISPLAY_FACTOR) @@ -625,10 +626,13 @@ if (!(draw_sys_flags & DRAW_PLANETS)) if (index < NUMBER_OF_PLANET_TYPES && (pPlanetDesc->data_index & PLANET_SHIELDED)) { + TFB_ReleaseColorMap(); s.frame = SetAbsFrameIndex (SpaceJunkFrame, 17); DrawStamp (&s); } } + + TFB_ReleaseColorMap(); } static void @@ -1678,8 +1682,11 @@ DrawSystem (SIZE radius, BOOLEAN IsInnerSystem) SetColorMap (GetColorMapAddress ( SetAbsColorMapIndex (OrbitalCMap, PLANCOLOR (PlanData[pCurDesc->data_index].Type)) - )); + ), TFB_COLORMAP_PLANET); DrawStamp (&pCurDesc->image); + + TFB_ReleaseColorMap(); + if (index == pSolarSysState->LastPlanetIndex) break; index = pCurDesc->NextIndex; diff --git a/sc2/src/sc2code/setup.c b/sc2/src/sc2code/setup.c index 257b0bc52..ac072125a 100644 --- a/sc2/src/sc2code/setup.c +++ b/sc2/src/sc2code/setup.c @@ -18,6 +18,7 @@ #include "starcon.h" #include "coderes.h" +#include "libs/graphics/gfx_common.h" //Added by Chris @@ -155,7 +156,7 @@ LoadKernel (int argc, char *argv[]) COLORMAP ColorMapTab; ColorMapTab = CaptureColorMap (LoadColorMap (STARCON_COLOR_MAP)); - SetColorMap (GetColorMapAddress (ColorMapTab)); + SetColorMap (GetColorMapAddress (ColorMapTab), TFB_COLORMAP_NONE); DestroyColorMap (ReleaseColorMap (ColorMapTab)); }