https://github.com/libsdl-org/Maelstrom/commit/6754e9df4e0f4a0cc2f6f05d8265380d3bb335b5
From 6754e9df4e0f4a0cc2f6f05d8265380d3bb335b5 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 31 Oct 2011 19:24:45 -0400
Subject: [PATCH] Added a static image element to the UI system.
---
MaelstromUI.cpp | 3 ++
screenlib/Makefile.am | 2 ++
screenlib/Makefile.in | 12 ++++---
screenlib/SDL_FrameBuf.cpp | 6 ++++
screenlib/SDL_FrameBuf.h | 1 +
screenlib/UIElementImage.cpp | 68 ++++++++++++++++++++++++++++++++++++
screenlib/UIElementImage.h | 38 ++++++++++++++++++++
7 files changed, 126 insertions(+), 4 deletions(-)
create mode 100644 screenlib/UIElementImage.cpp
create mode 100644 screenlib/UIElementImage.h
diff --git a/MaelstromUI.cpp b/MaelstromUI.cpp
index 50383d0a..8cbe4803 100644
--- a/MaelstromUI.cpp
+++ b/MaelstromUI.cpp
@@ -37,6 +37,7 @@
#include "UIElementSprite.h"
#include "UIElementTitle.h"
#include "screenlib/UIElementButton.h"
+#include "screenlib/UIElementImage.h"
#include "screenlib/UIElementLabel.h"
#include "screenlib/UIElementLine.h"
#include "screenlib/UIElementRadio.h"
@@ -199,6 +200,8 @@ MaelstromUI::CreateElement(UIBaseElement *parent, const char *type, const char *
return new UIElementRect(parent, name);
} else if (strcasecmp(type, "Label") == 0) {
return new UIElementLabel(parent, name);
+ } else if (strcasecmp(type, "Image") == 0) {
+ return new UIElementImage(parent, name);
} else if (strcasecmp(type, "Button") == 0) {
return new UIElementButton(parent, name);
} else if (strcasecmp(type, "DialogLabel") == 0) {
diff --git a/screenlib/Makefile.am b/screenlib/Makefile.am
index 1a21e5f8..29b0ce0c 100644
--- a/screenlib/Makefile.am
+++ b/screenlib/Makefile.am
@@ -21,6 +21,8 @@ libSDLscreen_a_SOURCES = \
UIElementCheckbox.h \
UIElementEditbox.cpp \
UIElementEditbox.h \
+ UIElementImage.cpp \
+ UIElementImage.h \
UIElementLabel.cpp \
UIElementLabel.h \
UIElementLine.cpp \
diff --git a/screenlib/Makefile.in b/screenlib/Makefile.in
index 174fa1e0..0e0c2dce 100644
--- a/screenlib/Makefile.in
+++ b/screenlib/Makefile.in
@@ -52,10 +52,11 @@ am_libSDLscreen_a_OBJECTS = SDL_FrameBuf.$(OBJEXT) UIArea.$(OBJEXT) \
UIBaseElement.$(OBJEXT) UIDialog.$(OBJEXT) \
UIDialogButton.$(OBJEXT) UIElement.$(OBJEXT) \
UIElementButton.$(OBJEXT) UIElementCheckbox.$(OBJEXT) \
- UIElementEditbox.$(OBJEXT) UIElementLabel.$(OBJEXT) \
- UIElementLine.$(OBJEXT) UIElementRadio.$(OBJEXT) \
- UIElementRect.$(OBJEXT) UIElementTexture.$(OBJEXT) \
- UIManager.$(OBJEXT) UIPanel.$(OBJEXT) UITemplates.$(OBJEXT)
+ UIElementEditbox.$(OBJEXT) UIElementImage.$(OBJEXT) \
+ UIElementLabel.$(OBJEXT) UIElementLine.$(OBJEXT) \
+ UIElementRadio.$(OBJEXT) UIElementRect.$(OBJEXT) \
+ UIElementTexture.$(OBJEXT) UIManager.$(OBJEXT) \
+ UIPanel.$(OBJEXT) UITemplates.$(OBJEXT)
libSDLscreen_a_OBJECTS = $(am_libSDLscreen_a_OBJECTS)
DEFAULT_INCLUDES = -I.@am__isrc@
depcomp = $(SHELL) $(top_srcdir)/depcomp
@@ -200,6 +201,8 @@ libSDLscreen_a_SOURCES = \
UIElementCheckbox.h \
UIElementEditbox.cpp \
UIElementEditbox.h \
+ UIElementImage.cpp \
+ UIElementImage.h \
UIElementLabel.cpp \
UIElementLabel.h \
UIElementLine.cpp \
@@ -275,6 +278,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementButton.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementCheckbox.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementEditbox.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementImage.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementLabel.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementLine.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementRadio.Po@am__quote@
diff --git a/screenlib/SDL_FrameBuf.cpp b/screenlib/SDL_FrameBuf.cpp
index e02ee068..74b4e523 100644
--- a/screenlib/SDL_FrameBuf.cpp
+++ b/screenlib/SDL_FrameBuf.cpp
@@ -282,6 +282,12 @@ FrameBuf:: LoadImage(int w, int h, Uint32 *pixels)
return(texture);
}
+SDL_Texture *
+FrameBuf:: LoadImage(SDL_Surface *surface)
+{
+ return SDL_CreateTextureFromSurface(renderer, surface);
+}
+
void
FrameBuf:: FreeImage(SDL_Texture *image)
{
diff --git a/screenlib/SDL_FrameBuf.h b/screenlib/SDL_FrameBuf.h
index 482a252e..98dfe71a 100644
--- a/screenlib/SDL_FrameBuf.h
+++ b/screenlib/SDL_FrameBuf.h
@@ -152,6 +152,7 @@ class FrameBuf : public ErrorBase {
/* Load and convert an 8-bit image with the given mask */
SDL_Texture *LoadImage(int w, int h, Uint8 *pixels, Uint8 *mask = NULL);
SDL_Texture *LoadImage(int w, int h, Uint32 *pixels);
+ SDL_Texture *LoadImage(SDL_Surface *surface);
int GetImageWidth(SDL_Texture *image) {
int w, h;
SDL_QueryTexture(image, NULL, NULL, &w, &h);
diff --git a/screenlib/UIElementImage.cpp b/screenlib/UIElementImage.cpp
new file mode 100644
index 00000000..f6c53820
--- /dev/null
+++ b/screenlib/UIElementImage.cpp
@@ -0,0 +1,68 @@
+/*
+ Maelstrom: Open Source version of the classic game by Ambrosia Software
+ Copyright (C) 1997-2011 Sam Lantinga
+
+ 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
+
+ Sam Lantinga
+ slouken@libsdl.org
+*/
+
+#include "SDL_FrameBuf.h"
+#include "../utils/physfsrwops.h"
+#include "UIElementImage.h"
+
+UIElementType UIElementImage::s_elementType;
+
+
+UIElementImage::UIElementImage(UIBaseElement *parent, const char *name) :
+ UIElementTexture(parent, name)
+{
+}
+
+bool
+UIElementImage::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
+{
+ rapidxml::xml_attribute<> *attr;
+ SDL_Surface *bmp;
+ SDL_Texture *texture;
+
+
+ if (!UIElementTexture::Load(node, templates)) {
+ return false;
+ }
+
+ attr = node->first_attribute("file", 0, false);
+ if (!attr) {
+ SetError("Element '%s' missing attribute 'file'", node->name());
+ return false;
+ }
+
+ bmp = SDL_LoadBMP_RW(PHYSFSRWOPS_openRead(attr->value()), 1);
+ if (!bmp) {
+ SetError("Element '%s' Couldn't open file '%s'", node->name(), attr->value());
+ return false;
+ }
+
+ texture = m_screen->LoadImage(bmp);
+ SDL_FreeSurface(bmp);
+ if (!texture) {
+ SetError("Element '%s' Couldn't load image from '%s'", node->name(), attr->value());
+ return false;
+ }
+ SetTexture(texture);
+
+ return true;
+}
diff --git a/screenlib/UIElementImage.h b/screenlib/UIElementImage.h
new file mode 100644
index 00000000..2f8fe38e
--- /dev/null
+++ b/screenlib/UIElementImage.h
@@ -0,0 +1,38 @@
+/*
+ Maelstrom: Open Source version of the classic game by Ambrosia Software
+ Copyright (C) 1997-2011 Sam Lantinga
+
+ 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
+
+ Sam Lantinga
+ slouken@libsdl.org
+*/
+
+#ifndef _UIElementImage_h
+#define _UIElementImage_h
+
+#include "UIElementTexture.h"
+
+
+class UIElementImage : public UIElementTexture
+{
+DECLARE_TYPESAFE_CLASS(UIElementTexture)
+public:
+ UIElementImage(UIBaseElement *parent, const char *name = "");
+
+ override bool Load(rapidxml::xml_node<> *node, const UITemplates *templates);
+};
+
+#endif // _UIElementImage_h