Maelstrom: controls.cpp and dialog.cpp compile again

From bb075cffc893f19b8d168250e2cc51ba0a03c1de Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 20 Oct 2011 23:24:29 -0400
Subject: [PATCH] controls.cpp and dialog.cpp compile again

---
 controls.cpp | 42 +++++++++--------------
 dialog.cpp   | 40 ++++------------------
 dialog.h     | 94 +++++++++++++++++++++++++++++++---------------------
 3 files changed, 78 insertions(+), 98 deletions(-)

diff --git a/controls.cpp b/controls.cpp
index 9793e4b5..73ad84a9 100644
--- a/controls.cpp
+++ b/controls.cpp
@@ -178,34 +178,27 @@ static int Cancel_callback(void) {
 }
 static void BoxKeyPress(const SDL_Keysym &key, int *doneflag)
 {
-	SDL_Color black = { 0x00, 0x00, 0x00, 0 };
-	SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
 	int i;
+	SDL_Keycode sym = key.sym;
 	char keyname[128];
 
-	if ( key.sym == *checkboxes[currentbox].control )
+	if ( sym == *checkboxes[currentbox].control )
 		return;
 
 	/* Make sure the key isn't in use! */
 	for ( i=0; i<NUM_CTLS; ++i ) {
-		if ( key.sym == *checkboxes[i].control ) {
-			key.sym = (SDLKey)*checkboxes[currentbox].control;
+		if ( sym == *checkboxes[i].control ) {
+			sym = *checkboxes[currentbox].control;
 
 			/* Clear the current text */
-			fontserv->InvertText(keynames[currentbox]);
-			screen->QueueBlit(
-				X+96+(BOX_WIDTH-keynames[currentbox]->w)/2, 
-				Y+75+SP+checkboxes[currentbox].yoffset,
-						keynames[currentbox], NOCLIP);
-			screen->Update();
 			fontserv->FreeText(keynames[currentbox]);
 
 			/* Blit the new message */
 			strcpy(keyname, "That key is in use!");
 			keynames[currentbox] = fontserv->TextImage(keyname,
-					chicago, STYLE_NORM, black, white);
+					chicago, STYLE_NORM, 0x00, 0x00, 0x00);
 			screen->QueueBlit(
-				X+96+(BOX_WIDTH-keynames[currentbox]->w)/2, 
+				X+96+(BOX_WIDTH-screen->GetImageWidth(keynames[currentbox]))/2, 
 				Y+75+SP+checkboxes[currentbox].yoffset,
 					keynames[currentbox], NOCLIP);
 			screen->Update();
@@ -215,19 +208,14 @@ static void BoxKeyPress(const SDL_Keysym &key, int *doneflag)
 	}
 
 	/* Clear the current text */
-	fontserv->InvertText(keynames[currentbox]);
-	screen->QueueBlit(X+96+(BOX_WIDTH-keynames[currentbox]->w)/2, 
-				Y+75+SP+checkboxes[currentbox].yoffset,
-						keynames[currentbox], NOCLIP);
-	screen->Update();
 	fontserv->FreeText(keynames[currentbox]);
 
 	/* Display the new key */
-	*checkboxes[currentbox].control = key.sym;
+	*checkboxes[currentbox].control = sym;
 	KeyName(*checkboxes[currentbox].control, keyname);
-	keynames[currentbox] = fontserv->TextImage(keyname, chicago, STYLE_NORM,
-								black, white);
-	screen->QueueBlit(X+96+(BOX_WIDTH-keynames[currentbox]->w)/2, 
+	keynames[currentbox] = fontserv->TextImage(keyname,
+					chicago, STYLE_NORM, 0x00, 0x00, 0x00);
+	screen->QueueBlit(X+96+(BOX_WIDTH-screen->GetImageWidth(keynames[currentbox]))/2, 
 				Y+75+SP+checkboxes[currentbox].yoffset,
 						keynames[currentbox], NOCLIP);
 	screen->Update();
@@ -301,7 +289,7 @@ void ConfigureControls(void)
 			dialog->Add_Rectangle(92, 71+checkboxes[i].yoffset,
 						BOX_WIDTH, BOX_HEIGHT, black);
 			dialog->Add_Image(keynames[i],
-					92+(BOX_WIDTH-keynames[i]->w)/2, 
+					92+(BOX_WIDTH-screen->GetImageWidth(keynames[i]))/2, 
 						71+SP+checkboxes[i].yoffset);
 		}
 	}
@@ -333,7 +321,7 @@ void ConfigureControls(void)
 
 static void HandleEvent(SDL_Event *event)
 {
-	SDLKey key;
+	SDL_Keycode key;
 
 	switch (event->type) {
 #ifdef SDL_INIT_JOYSTICK
@@ -524,16 +512,16 @@ void ShowDawn(void)
 									screen);
 	x = y = 19;
 	dialog->Add_Image(splash, x, y);
-	x += (splash->w+26);
+	x += (screen->GetImageWidth(splash)+26);
 	text[0] = fontserv->TextImage(D_text[0], chicago, STYLE_NORM,
 							0x00, 0x00, 0x00);
 	dialog->Add_Image(text[0], x, y);
 	for ( i=1; i<6; ++i ) {
-		y += (text[i-1]->h+2);
+		y += (screen->GetImageHeight(text[i-1])+2);
 		text[i] = fontserv->TextImage(D_text[i], chicago, STYLE_NORM,
 							0x00, 0x00, 0x00);
 		dialog->Add_Image(text[i], x, y);
-		x += (text[i]->w+2);
+		x += (screen->GetImageWidth(text[i])+2);
 	}
 	OK = new Mac_DefaultButton(210, 160, 90, BUTTON_HEIGHT,
 						"OK", chicago, fontserv, NULL);
diff --git a/dialog.cpp b/dialog.cpp
index e4be64d6..2b50bb38 100644
--- a/dialog.cpp
+++ b/dialog.cpp
@@ -18,40 +18,14 @@ Mac_Button::Mac_Button(int x, int y, int width, int height,
 				const char *text, MFont *font, FontServ *fontserv, 
 				int (*callback)(void)) : Mac_Dialog(x, y)
 {
-	SDL_Surface *textb;
-	SDL_Rect dstrect;
-
 	/* Set private variables */
 	Width = width;
 	Height = height;
-
-	/* Build image of the button */
-	button = SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height,
-						8, 0, 0, 0, 0);
-	if ( button == NULL ) {
-		SetError("%s", SDL_GetError());
-		return;
-	}
-	button->format->palette->colors[0].r = 0xFF;
-	button->format->palette->colors[0].g = 0xFF;
-	button->format->palette->colors[0].b = 0xFF;
-	button->format->palette->colors[1].r = 0x00;
-	button->format->palette->colors[1].g = 0x00;
-	button->format->palette->colors[1].b = 0x00;
-	textb = fontserv->TextImage(text, font, STYLE_NORM,
-					0x00, 0x00, 0x00);
-	if ( textb != NULL ) {
-		if ( (textb->w <= button->w) && 
-					(textb->h <= button->h) ) {
-			dstrect.x = (button->w-textb->w)/2;
-			dstrect.y = (button->h-textb->h)/2;
-			dstrect.w = textb->w;
-			dstrect.h = textb->h;
-			SDL_BlitSurface(textb, NULL, button, &dstrect);
-		}
-		fontserv->FreeText(textb);
-	}
-	Bevel_Button(button);
+	Fontserv = fontserv;
+	label = fontserv->TextImage(text, font, STYLE_NORM, R_FG, G_FG, B_FG);
+	button[0] = NULL;
+	button[1] = NULL;
+	active = false;
 
 	/* Set the callback */
 	Callback = callback;
@@ -70,7 +44,7 @@ Mac_CheckBox::Mac_CheckBox(int *toggle, int x, int y, const char *text,
 {
 	/* Create the text label */
 	Fontserv = fontserv;
-	label = Fontserv->TextImage(text, font, STYLE_NORM, 0, 0, 0);
+	label = Fontserv->TextImage(text, font, STYLE_NORM, R_FG, G_FG, B_FG);
 
 	/* Set the checkbox variable */
 	checkval = toggle;
@@ -164,7 +138,7 @@ Maclike_Dialog:: Add_Rectangle(int x, int y, int w, int h, Uint32 color)
 }
 
 void
-Maclike_Dialog:: Add_Image(SDL_Surface *image, int x, int y)
+Maclike_Dialog:: Add_Image(SDL_Texture *image, int x, int y)
 {
 	struct image_elem *ielem;
 	
diff --git a/dialog.h b/dialog.h
index ca1f8ae0..38c6a7d4 100644
--- a/dialog.h
+++ b/dialog.h
@@ -119,13 +119,30 @@ class Mac_Button : public Mac_Dialog {
 		const char *text, MFont *font, FontServ *fontserv, 
 				int (*callback)(void));
 	virtual ~Mac_Button() {
-		SDL_FreeSurface(button);
+		if ( label ) {
+			Fontserv->FreeText(label);
+		}
+		if ( button[0] ) {
+			Screen->FreeImage(button[0]);
+		}
+		if ( button[1] ) {
+			Screen->FreeImage(button[1]);
+		}
 	}
 
 	virtual void Map(int Xoff, int Yoff, FrameBuf *screen) {
 		/* Do the normal dialog mapping */
 		Mac_Dialog::Map(Xoff, Yoff, screen);
 
+		/* Create the button images */
+		int pitch = (Width+3)&3;
+		Uint8 *image_bits = new Uint8[Height*pitch];
+		Bevel_Button(image_bits, pitch);
+		button[0] = Screen->LoadImage(Width, Height, image_bits);
+		Invert_Button(image_bits, pitch);
+		button[1] = Screen->LoadImage(Width, Height, image_bits);
+		delete[] image_bits;
+
 		/* Set up the button sensitivity */
 		sensitive.x = X;
 		sensitive.y  = Y;
@@ -133,7 +150,7 @@ class Mac_Button : public Mac_Dialog {
 		sensitive.h = Height;
 	}
 	virtual void Show(void) {
-		Screen->QueueBlit(X, Y, button, NOCLIP);
+		Screen->QueueBlit(X, Y, button[0], NOCLIP);
 	}
 
 	virtual void HandleButtonPress(int x, int y, int button, int *doneflag) {
@@ -143,58 +160,59 @@ class Mac_Button : public Mac_Dialog {
 
 protected:
 	int Width, Height;
-	SDL_Surface *button;
+	FontServ *Fontserv;
+	SDL_Texture *label;
+	SDL_Texture *button[2];
+	bool active;
 	SDL_Rect sensitive;
 	int (*Callback)(void);
 
-	virtual void Bevel_Button(SDL_Surface *image) {
-		Uint16 h;
-		Uint8 *image_bits;
+	void Bevel_Button(Uint8 *image_bits, int pitch) {
+		int h;
 
-		image_bits = (Uint8 *)image->pixels;
+		memset(image_bits, 0, Height*pitch);
 
 		/* Bevel upper corners */
-		memset(image_bits+3, 0x01, image->w-6);
-		image_bits += image->pitch;
-		memset(image_bits+1, 0x01, 2);
-		memset(image_bits+image->w-3, 0x01, 2);
-		image_bits += image->pitch;
-		memset(image_bits+1, 0x01, 1);
-		memset(image_bits+image->w-2, 0x01, 1);
-		image_bits += image->pitch;
+		memset(image_bits+3, 0xFF, Width-6);
+		image_bits += pitch;
+		memset(image_bits+1, 0xFF, 2);
+		memset(image_bits+Width-3, 0xFF, 2);
+		image_bits += pitch;
+		memset(image_bits+1, 0xFF, 1);
+		memset(image_bits+Width-2, 0xFF, 1);
+		image_bits += pitch;
 
 		/* Draw sides */
-		for ( h=3; h<(image->h-3); ++h ) {
-			image_bits[0] = 0x01;
-			image_bits[image->w-1] = 0x01;
-			image_bits += image->pitch;
+		for ( h=3; h<(Height-3); ++h ) {
+			image_bits[0] = 0xFF;
+			image_bits[Width-1] = 0xFF;
+			image_bits += pitch;
 		}
 
 		/* Bevel bottom corners */
-		memset(image_bits+1, 0x01, 1);
-		memset(image_bits+image->w-2, 0x01, 1);
-		image_bits += image->pitch;
-		memset(image_bits+1, 0x01, 2);
-		memset(image_bits+image->w-3, 0x01, 2);
-		image_bits += image->pitch;
-		memset(image_bits+3, 0x01, image->w-6);
-	}
-	virtual void InvertImage(void) {
+		memset(image_bits+1, 0xFF, 1);
+		memset(image_bits+Width-2, 0xFF, 1);
+		image_bits += pitch;
+		memset(image_bits+1, 0xFF, 2);
+		memset(image_bits+Width-3, 0xFF, 2);
+		image_bits += pitch;
+		memset(image_bits+3, 0xFF, Width-6);
+	}
+	void Invert_Button(Uint8 *image_bits, int pitch) {
 		int i;
 		Uint8 *buf;
 
-		for ( i=button->h*button->pitch, buf=(Uint8 *)button->pixels;
-							i > 0; --i, ++buf ) {
-			*buf = !*buf;
+		for ( i=Height*pitch, buf=image_bits; i > 0; --i, ++buf ) {
+			*buf = ~*buf;
 		}
 	}
 	virtual void ActivateButton(int *doneflag) {
 		/* Flash the button */
-		InvertImage();
+		active = true;
 		Show();
 		Screen->Update();
 		SDL_Delay(50);
-		InvertImage();
+		active = false;
 		Show();
 		Screen->Update();
 		/* Run the callback */
@@ -439,7 +457,7 @@ class Mac_RadioList : public Mac_Dialog {
 		Screen->DrawLine(x+8, y+10, x+9, y+10, Fg);
 		Screen->DrawLine(x+4, y+11, x+7, y+11, Fg);
 	}
-	void Spot(int x, int y);
+	void Spot(int x, int y)
 	{
 		x += 8;
 		y += 8;
@@ -625,11 +643,11 @@ class Mac_TextEntry : public Mac_Dialog {
 		if ( entry->hilite ) {
 			clear = Fg;
 			entry->text = Fontserv->TextImage(entry->variable,
-				Font, STYLE_NORM, background, foreground);
+				Font, STYLE_NORM, background);
 		} else {
 			clear = Bg;
 			entry->text = Fontserv->TextImage(entry->variable,
-				Font, STYLE_NORM, foreground, background);
+				Font, STYLE_NORM, foreground);
 		}
 		Screen->FillRect(entry->x, entry->y,
 					entry->width, entry->height, clear);
@@ -828,11 +846,11 @@ class Mac_NumericEntry : public Mac_Dialog {
 		if ( entry->hilite ) {
 			clear = Fg;
 			entry->text = Fontserv->TextImage(buf,
-				Font, STYLE_NORM, background, foreground);
+				Font, STYLE_NORM, background);
 		} else {
 			clear = Bg;
 			entry->text = Fontserv->TextImage(buf,
-				Font, STYLE_NORM, foreground, background);
+				Font, STYLE_NORM, foreground);
 		}
 		entry->end = Screen->GetImageWidth(entry->text);
 		Screen->FillRect(entry->x, entry->y,