From 79eb5aa36c41e9146c2301345359d55042635f27 Mon Sep 17 00:00:00 2001
From: Kalle Olavi Niemitalo <kon@iki.fi>
Date: Sun, 28 Feb 2010 22:15:44 +0200
Subject: [PATCH] SDL 1.3 SVN r5558 compatibility

Make Bos Wars mostly compatible with SDL 1.3 SVN r5558.
Because of <http://bugzilla.libsdl.org/show_bug.cgi?id=962>,
you must run it with -D 16 or -D 32 though; otherwise it crashes.

Three kinds of changes:

- In SDL 1.3, SDLK_LMETA, SDLK_RMETA, SDLK_LSUPER, and SDLK_RSUPER
  have been replaced with SDLK_LGUI and SDLK_RGUI.  In guichan,
  arbitrarily map those to Key::LEFT_SUPER and Key::RIGHT_SUPER,
  rather than to Key::LEFT_META and Key::RIGHT_META.

- In SDL 1.3, SDL_PixelFormat::colorkey has been replaced with
  SDL_GetColorKey.  (SDL 1.2 already had SDL_SetColorKey.)

- In SDL 1.3, SDL_PixelFormat::alpha has been replaced with
  SDL_GetSurfaceAlphaMod and SDL_SetSurfaceAlphaMod.
  SDL_GetSurfaceAlphaMod annoyingly doesn't return the alpha
  but rather stores it via a pointer parameter, so the
  corresponding variables in Bos Wars must be Uint8, not int.
---
 engine/guichan/sdl/sdlinput.cpp |    9 ++++++
 engine/ui/interface.cpp         |   20 +++++++++++++
 engine/video/font.cpp           |    5 +++
 engine/video/graphic.cpp        |   59 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 93 insertions(+), 0 deletions(-)

diff --git a/engine/guichan/sdl/sdlinput.cpp b/engine/guichan/sdl/sdlinput.cpp
index b15cc1f..6b160a1 100644
--- a/engine/guichan/sdl/sdlinput.cpp
+++ b/engine/guichan/sdl/sdlinput.cpp
@@ -353,6 +353,14 @@ namespace gcn
           case SDLK_SCROLLOCK:
               value = Key::SCROLL_LOCK;
               break;
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+          case SDLK_LGUI:
+              value = Key::LEFT_SUPER;
+	      break;
+          case SDLK_RGUI:
+              value = Key::RIGHT_SUPER;
+	      break;
+#else  // SDL < 1.3
           case SDLK_RMETA:
               value = Key::RIGHT_META;
               break;
@@ -365,6 +373,7 @@ namespace gcn
           case SDLK_RSUPER:
               value = Key::RIGHT_SUPER;
               break;
+#endif	// SDL < 1.3
           case SDLK_MODE:
               value = Key::ALT_GR;
               break;
diff --git a/engine/ui/interface.cpp b/engine/ui/interface.cpp
index 9d40365..052dc23 100644
--- a/engine/ui/interface.cpp
+++ b/engine/ui/interface.cpp
@@ -917,16 +917,29 @@ int HandleKeyModifiersDown(unsigned key, unsigned keychar)
 			return 1;
 		case SDLK_LALT:
 		case SDLK_RALT:
+#if !SDL_VERSION_ATLEAST(1, 3, 0)
+		// SDL 1.2 has SDLK_LALT, SDLK_LMETA, SDLK_LSUPER
+		// all with different values.  SDL 1.3 renamed
+		// SDLK_LMETA to SDLK_LGUI and made SDLK_LSUPER
+		// an alias for it; SDLK_LALT remains separate.
 		case SDLK_LMETA:
 		case SDLK_RMETA:
+#endif	// SDL < 1.3
 			KeyModifiers |= ModifierAlt;
 			// maxy: disabled
 			if (InterfaceState == IfaceStateNormal) {
 				SelectedUnitChanged(); // VLADI: to allow alt-buttons
 			}
 			return 1;
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+		// Cannot use the SDLK_LSUPER name until this is fixed:
+		// http://bugzilla.libsdl.org/show_bug.cgi?id=961
+		case SDLK_LGUI:
+		case SDLK_RGUI:
+#else  // SDL < 1.3
 		case SDLK_LSUPER:
 		case SDLK_RSUPER:
+#endif	// SDL < 1.3
 			KeyModifiers |= ModifierSuper;
 			return 1;
 		case SDLK_SYSREQ:
@@ -963,16 +976,23 @@ int HandleKeyModifiersUp(unsigned key, unsigned keychar)
 			return 1;
 		case SDLK_LALT:
 		case SDLK_RALT:
+#if !SDL_VERSION_ATLEAST(1, 3, 0) // See comments in HandleKeyModifiersDown
 		case SDLK_LMETA:
 		case SDLK_RMETA:
+#endif
 			KeyModifiers &= ~ModifierAlt;
 			// maxy: disabled
 			if (InterfaceState == IfaceStateNormal) {
 				SelectedUnitChanged(); // VLADI: to allow alt-buttons
 			}
 			return 1;
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+		case SDLK_LGUI:
+		case SDLK_RGUI:
+#else
 		case SDLK_LSUPER:
 		case SDLK_RSUPER:
+#endif
 			KeyModifiers &= ~ModifierSuper;
 			return 1;
 	}
diff --git a/engine/video/font.cpp b/engine/video/font.cpp
index 99bf5ef..2a71794 100644
--- a/engine/video/font.cpp
+++ b/engine/video/font.cpp
@@ -676,7 +676,12 @@ void CFont::MeasureWidths()
 	CharWidth = new char[maxy];
 	memset(CharWidth, 0, maxy);
 	CharWidth[0] = G->Width / 2;  // a reasonable value for SPACE
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+	ckey = 0;
+	SDL_GetColorKey(G->Surface, &ckey);
+#else  // SDL < 1.3
 	ckey = G->Surface->format->colorkey;
+#endif	// SDL < 1.3
 	ipr = G->Surface->w / G->Width;
 
 	SDL_LockSurface(G->Surface);
diff --git a/engine/video/graphic.cpp b/engine/video/graphic.cpp
index 8455e7b..ebf549e 100644
--- a/engine/video/graphic.cpp
+++ b/engine/video/graphic.cpp
@@ -133,10 +133,18 @@ void CGraphic::DrawSubTrans(int gx, int gy, int w, int h, int x, int y,
 	unsigned char alpha) const
 {
 	if (!UseOpenGL) {
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+		Uint8 oldalpha = 255;
+		SDL_GetSurfaceAlphaMod(Surface, &oldalpha);
+		SDL_SetSurfaceAlphaMod(Surface, alpha);
+		DrawSub(gx, gy, w, h, x, y);
+		SDL_SetSurfaceAlphaMod(Surface, oldalpha);
+#else  // SDL < 1.3
 		int oldalpha = Surface->format->alpha;
 		SDL_SetAlpha(Surface, SDL_SRCALPHA, alpha);
 		DrawSub(gx, gy, w, h, x, y);
 		SDL_SetAlpha(Surface, SDL_SRCALPHA, oldalpha);
+#endif	// SDL < 1.3
 	} else {
 		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 		glColor4ub(255, 255, 255, alpha);
@@ -390,7 +398,11 @@ void CGraphic::DrawFrameTransX(unsigned frame, int x, int y, int alpha) const
 	if (!UseOpenGL) {
 		SDL_Rect srect;
 		SDL_Rect drect;
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+		Uint8 oldalpha;
+#else
 		int oldalpha;
+#endif
 
 		srect.x = (SurfaceFlip->w - (frame % (SurfaceFlip->w /
 			Width)) * Width) - Width;
@@ -401,10 +413,17 @@ void CGraphic::DrawFrameTransX(unsigned frame, int x, int y, int alpha) const
 		drect.x = x;
 		drect.y = y;
 
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+		SDL_GetSurfaceAlphaMod(Surface, &oldalpha);
+		SDL_SetSurfaceAlphaMod(Surface, alpha);
+		SDL_BlitSurface(SurfaceFlip, &srect, TheScreen, &drect);
+		SDL_SetSurfaceAlphaMod(Surface, oldalpha);
+#else  // SDL < 1.3
 		oldalpha = Surface->format->alpha;
 		SDL_SetAlpha(Surface, SDL_SRCALPHA, alpha);
 		SDL_BlitSurface(SurfaceFlip, &srect, TheScreen, &drect);
 		SDL_SetAlpha(Surface, SDL_SRCALPHA, oldalpha);
+#endif	// SDL < 1.3
 	} else {
 		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 		glColor4ub(255, 255, 255, alpha);
@@ -420,7 +439,11 @@ void CGraphic::DrawFrameClipTransX(unsigned frame, int x, int y, int alpha) cons
 		SDL_Rect drect;
 		int oldx;
 		int oldy;
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+		Uint8 oldalpha;
+#else
 		int oldalpha;
+#endif
 
 		srect.x = (SurfaceFlip->w - (frame % (SurfaceFlip->w /
 			Width)) * Width) - Width;
@@ -437,10 +460,17 @@ void CGraphic::DrawFrameClipTransX(unsigned frame, int x, int y, int alpha) cons
 		drect.x = x;
 		drect.y = y;
 
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+		SDL_GetSurfaceAlphaMod(Surface, &oldalpha);
+		SDL_SetSurfaceAlphaMod(Surface, alpha);
+		SDL_BlitSurface(SurfaceFlip, &srect, TheScreen, &drect);
+		SDL_SetSurfaceAlphaMod(Surface, oldalpha);
+#else  // SDL < 1.3
 		oldalpha = Surface->format->alpha;
 		SDL_SetAlpha(Surface, SDL_SRCALPHA, alpha);
 		SDL_BlitSurface(SurfaceFlip, &srect, TheScreen, &drect);
 		SDL_SetAlpha(Surface, SDL_SRCALPHA, oldalpha);
+#endif	// SDL < 1.3
 	} else {
 		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 		glColor4ub(255, 255, 255, alpha);
@@ -791,8 +821,15 @@ void CGraphic::Flip()
 
 	s = SurfaceFlip = SDL_ConvertSurface(Surface, Surface->format, SDL_SWSURFACE);
 	if (Surface->flags & SDL_SRCCOLORKEY) {
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+		Uint32 ckey = 0;
+		SDL_GetColorKey(Surface, &ckey);
+		SDL_SetColorKey(SurfaceFlip, SDL_SRCCOLORKEY | SDL_RLEACCEL,
+				ckey);
+#else  // SDL < 1.3
 		SDL_SetColorKey(SurfaceFlip, SDL_SRCCOLORKEY | SDL_RLEACCEL,
 			Surface->format->colorkey);
+#endif	// SDL < 1.3
 	}
 
 	SDL_LockSurface(Surface);
@@ -900,7 +937,11 @@ static void MakeTextures2(CGraphic *g, GLuint texture, CUnitColors *colors,
 	useckey = g->Surface->flags & SDL_SRCCOLORKEY;
 	f = g->Surface->format;
 	bpp = f->BytesPerPixel;
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+	SDL_GetColorKey(g->Surface, &ckey);
+#else  // SDL < 1.3
 	ckey = f->colorkey;
+#endif	// SDL < 1.3
 
 	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 	maxw = std::min(g->GraphicWidth - ow, GLMaxTextureSize);
@@ -910,7 +951,11 @@ static void MakeTextures2(CGraphic *g, GLuint texture, CUnitColors *colors,
 	tex = new unsigned char[w * h * 4];
 	memset(tex, 0, w * h * 4);
 	if (g->Surface->flags & SDL_SRCALPHA) {
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+		SDL_GetSurfaceAlphaMod(g->Surface, &alpha);
+#else  // SDL < 1.3
 		alpha = f->alpha;
+#endif	// SDL < 1.3
 	} else {
 		alpha = 0xff;
 	}
@@ -1135,7 +1180,14 @@ void CGraphic::Resize(int w, int h)
 		SDL_UnlockSurface(Surface);
 		memcpy(pal, Surface->format->palette->colors, sizeof(SDL_Color) * 256);
 		useckey = Surface->flags & SDL_SRCCOLORKEY;
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+		ckey = 0;
+		if (useckey) {
+			SDL_GetColorKey(Surface, &ckey);
+		}
+#else  // SDL < 1.3
 		ckey = Surface->format->colorkey;
+#endif	// SDL < 1.3
 		SDL_FreeSurface(Surface);
 
 		Surface = SDL_CreateRGBSurfaceFrom(data, w, h, 8, w, 0, 0, 0, 0);
@@ -1243,9 +1295,16 @@ bool CGraphic::TransparentPixel(int x, int y)
 	SDL_LockSurface(Surface);
 	p = (unsigned char *)Surface->pixels + y * Surface->pitch + x * bpp;
 	if (bpp == 1) {
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+		Uint32 ckey;
+		if (SDL_GetColorKey(Surface, &ckey) == 0 && *p == ckey) {
+			ret = true;
+		}
+#else  // SDL < 1.3
 		if (*p == Surface->format->colorkey) {
 			ret = true;
 		}
+#endif	// SDL < 1.3
 	} else {
 		if (p[Surface->format->Ashift >> 3] == 255) {
 			ret = true;
-- 
1.6.6.1

