Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background ".sprite" support #2748

Merged
merged 7 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 143 additions & 96 deletions src/object/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <utility>

#include "editor/editor.hpp"
#include "sprite/sprite.hpp"
#include "sprite/sprite_manager.hpp"
#include "supertux/d_scope.hpp"
#include "supertux/flip_level_transformer.hpp"
#include "supertux/gameconfig.hpp"
Expand All @@ -28,7 +30,105 @@
#include "util/reader_mapping.hpp"
#include "util/writer.hpp"
#include "video/drawing_context.hpp"
#include "video/surface.hpp"

static const std::unordered_map<std::string, std::string> fallback_paths = {
{"arctis2.png", "antarctic/arctis2.png"},
{"misty_snowhills_small.png", "antarctic/misty_snowhills_small.png"},
{"semi_arctic.jpg", "antarctic/semi_arctic.jpg"},
{"bridgecloud-dark.png", "arctic_bridge/bridgecloud-dark.png"},
{"bridgecloud-light.png", "arctic_bridge/bridgecloud-light.png"},
{"bridgeocean-fade.png", "arctic_bridge/bridgeocean-fade.png"},
{"bridgeocean-nofade.png", "arctic_bridge/bridgeocean-nofade.png"},
{"bridgeocean-original.png", "arctic_bridge/bridgeocean-original.png"},
{"arcticskies1.png", "arcticskies/arcticskies1.png"},
{"arcticskies2.png", "arcticskies/arcticskies2.png"},
{"arcticskies3.png", "arcticskies/arcticskies3.png"},
{"arcticskies35.png", "arcticskies/arcticskies35.png"},
{"arcticskies4.png", "arcticskies/arcticskies4.png"},
{"block-snow-background.png", "block_snow/block-snow-background.png"},
{"block-snow-midground.png", "block_snow/block-snow-midground.png"},
{"block-snow-top.png", "block_snow/block-snow-top.png"},
{"bluemountain-bottom.png", "bluemountain/bluemountain-bottom.png"},
{"bluemountain-middle.png", "bluemountain/bluemountain-middle.png"},
{"bluemountain-top.png", "bluemountain/bluemountain-top.png"},
{"bluemountain2.png", "bluemountain/bluemountain2.png"},
{"castle_foreground.png", "castle/castle_foreground.png"},
{"snowcastle.png", "castle/snowcastle.png"},
{"cloud-mountains-background.png", "cloud_mountains/cloud-mountains-background.png"},
{"cloud-mountains-bottom.png", "cloud_mountains/cloud-mountains-bottom.png"},
{"cloud-mountains-forground.png", "cloud_mountains/cloud-mountains-forground.png"},
{"cloud-mountains-midground.png", "cloud_mountains/cloud-mountains-midground.png"},
{"dawn_hill_para_blur.png", "forest/dawn_hill_para_blur.png"},
{"forest2_para.png", "forest/forest2_para.png"},
{"forest_para2.png", "forest/forest_para2.png"},
{"forest_para3.png", "forest/forest_para3.png"},
{"forest_para3_bottom.png", "forest/forest_para3_bottom.png"},
{"nighthills.png", "forest/nighthills.png"},
{"ghostforest.jpg", "ghostforest/ghostforest.jpg"},
{"ghostforest_grave.png", "ghostforest/ghostforest_grave.png"},
{"ghostforest_para.png", "ghostforest/ghostforest_para.png"},
{"cave2.jpg", "ice_cave/cave2.jpg"},
{"darkcave-background.png", "ice_cave/darkcave-background.png"},
{"darkcave-middle.png", "ice_cave/darkcave-middle.png"},
{"darkcave-preview.png", "ice_cave/darkcave-preview.png"},
{"darkcave-top_and_bottom.png", "ice_cave/darkcave-top_and_bottom.png"},
{"darkcavemidground-middle.png", "ice_cave/darkcavemidground-middle.png"},
{"darkcavemidground-top_and_bottom.png", "ice_cave/darkcavemidground-top_and_bottom.png"},
{"black_800px.png", "misc/black_800px.png"},
{"fog.png", "misc/fog.png"},
{"grid.png", "misc/grid.png"},
{"grid.surface", "misc/grid.surface"},
{"heatshimmer-displacement.png", "misc/heatshimmer-displacement.png"},
{"heatshimmer.png", "misc/heatshimmer.png"},
{"heatshimmer.surface", "misc/heatshimmer.surface"},
{"leaves.png", "misc/leaves.png"},
{"oiltux.jpg", "misc/oiltux.jpg"},
{"transparent_up.png", "misc/transparent_up.png"},
{"nightsky.png", "nightsky/nightsky.png"},
{"nightsky_bottom.png", "nightsky/nightsky_bottom.png"},
{"nightsky_middle.png", "nightsky/nightsky_middle.png"},
{"nightsky_para.png", "nightsky/nightsky_para.png"},
{"nightsky_top.png", "nightsky/nightsky_top.png"},
};

static SpritePtr load_background_sprite(const std::string& image_path)
MatusGuy marked this conversation as resolved.
Show resolved Hide resolved
{
if (image_path.empty())
return nullptr;

if (PHYSFS_exists(image_path.c_str()))
// No need to search fallback paths.
return SpriteManager::current()->create(image_path);

// Search for a fallback image in fallback_paths.
const std::string& default_dir = "images/background/";
const std::string& default_dir2 = "/images/background/";
Vankata453 marked this conversation as resolved.
Show resolved Hide resolved
std::string new_path = image_path;
if (image_path.substr(0, default_dir.length()) == default_dir)
new_path.erase(0, default_dir.length());
else if (image_path.substr(0, default_dir2.length()) == default_dir2)
new_path.erase(0, default_dir2.length());

auto it = fallback_paths.find(new_path);
if (it == fallback_paths.end())
// Unknown image, try checking for a ".deprecated" version, or use the dummy texture.
return SpriteManager::current()->create(image_path);

new_path = default_dir + it->second;
return SpriteManager::current()->create(new_path);
}

static SpritePtr load_background(const std::string& image_path)
{
SpritePtr sprite = load_background_sprite(image_path);
if (!sprite) return nullptr;

if (Editor::is_active())
sprite->pause_animation();

return sprite;
}


Background::Background() :
ExposedObject<Background, scripting::Background>(this),
Expand Down Expand Up @@ -192,9 +292,9 @@ Background::get_settings()
result.add_float(_("Scroll speed y"), &m_scroll_speed.y, "scroll-speed-y", 0.0f);
result.add_float(_("Parallax Speed x"), &m_parallax_speed.x, "speed", std::nullopt);
result.add_float(_("Parallax Speed y"), &m_parallax_speed.y, "speed-y", m_parallax_speed.x);
result.add_surface(_("Top image"), &m_imagefile_top, "image-top", "");
result.add_surface(_("Image"), &m_imagefile, "image");
result.add_surface(_("Bottom image"), &m_imagefile_bottom, "image-bottom", "");
result.add_sprite(_("Top image"), &m_imagefile_top, "image-top", "");
result.add_sprite(_("Image"), &m_imagefile, "image");
result.add_sprite(_("Bottom image"), &m_imagefile_bottom, "image-bottom", "");
result.add_rgba(_("Colour"), &m_color, "color");
result.add_enum(_("Draw target"), reinterpret_cast<int*>(&m_target),
{_("Normal"), _("Lightmap")},
Expand Down Expand Up @@ -299,13 +399,16 @@ Background::draw_image(DrawingContext& context, const Vector& pos_)
Canvas& canvas = context.get_canvas(m_target);
context.set_flip(context.get_flip() ^ m_flip);

m_image->set_color(m_color);
m_image->set_blend(m_blend);

if (m_fill)
{
Rectf dstrect(Vector(pos_.x - context.get_width() / 2.0f,
pos_.y - context.get_height() / 2.0f),
Sizef(context.get_width(),
context.get_height()));
canvas.draw_surface_scaled(m_image, dstrect, m_layer);
m_image->draw_scaled(canvas, dstrect, m_layer);
}
else
{
Expand All @@ -316,7 +419,7 @@ Background::draw_image(DrawingContext& context, const Vector& pos_)
{
Vector p(pos_.x - parallax_image_size.width / 2.0f,
pos_.y + static_cast<float>(y) * img_h - img_h_2);
canvas.draw_surface(m_image, p, 0.f, m_color, m_blend, m_layer);
m_image->draw(canvas, p, m_layer);
}
break;

Expand All @@ -325,7 +428,7 @@ Background::draw_image(DrawingContext& context, const Vector& pos_)
{
Vector p(pos_.x + parallax_image_size.width / 2.0f - img_w,
pos_.y + static_cast<float>(y) * img_h - img_h_2);
canvas.draw_surface(m_image, p, 0.f, m_color, m_blend, m_layer);
m_image->draw(canvas, p, m_layer);
}
break;

Expand All @@ -334,7 +437,7 @@ Background::draw_image(DrawingContext& context, const Vector& pos_)
{
Vector p(pos_.x + static_cast<float>(x) * img_w - img_w_2,
pos_.y - parallax_image_size.height / 2.0f);
canvas.draw_surface(m_image, p, 0.f, m_color, m_blend, m_layer);
m_image->draw(canvas, p, m_layer);
}
break;

Expand All @@ -343,7 +446,7 @@ Background::draw_image(DrawingContext& context, const Vector& pos_)
{
Vector p(pos_.x + static_cast<float>(x) * img_w - img_w_2,
pos_.y - img_h + parallax_image_size.height / 2.0f);
canvas.draw_surface(m_image, p, 0.f, m_color, m_blend, m_layer);
m_image->draw(canvas, p, m_layer);
}
break;

Expand All @@ -356,15 +459,21 @@ Background::draw_image(DrawingContext& context, const Vector& pos_)

if (m_image_top && (y < 0))
{
canvas.draw_surface(m_image_top, p, 0.f, m_color, m_blend, m_layer);
m_image_top->set_color(m_color);
m_image_top->set_blend(m_blend);

m_image_top->draw(canvas, p, m_layer);
}
else if (m_image_bottom && (y > 0))
{
canvas.draw_surface(m_image_bottom, p, 0.f, m_color, m_blend, m_layer);
m_image_bottom->set_color(m_color);
m_image_bottom->set_blend(m_blend);

m_image_bottom->draw(canvas, p, m_layer);
}
else
{
canvas.draw_surface(m_image, p, 0.f, m_color, m_blend, m_layer);
m_image->draw(canvas, p, m_layer);
mrkubax10 marked this conversation as resolved.
Show resolved Hide resolved
}
}
break;
Expand Down Expand Up @@ -396,94 +505,32 @@ Background::draw(DrawingContext& context)
center_offset.y * (1.0f - m_parallax_speed.y)));
}

namespace {
std::unordered_map<std::string, std::string> fallback_paths = {
{"arctis2.png", "antarctic/arctis2.png"},
{"misty_snowhills_small.png", "antarctic/misty_snowhills_small.png"},
{"semi_arctic.jpg", "antarctic/semi_arctic.jpg"},
{"bridgecloud-dark.png", "arctic_bridge/bridgecloud-dark.png"},
{"bridgecloud-light.png", "arctic_bridge/bridgecloud-light.png"},
{"bridgeocean-fade.png", "arctic_bridge/bridgeocean-fade.png"},
{"bridgeocean-nofade.png", "arctic_bridge/bridgeocean-nofade.png"},
{"bridgeocean-original.png", "arctic_bridge/bridgeocean-original.png"},
{"arcticskies1.png", "arcticskies/arcticskies1.png"},
{"arcticskies2.png", "arcticskies/arcticskies2.png"},
{"arcticskies3.png", "arcticskies/arcticskies3.png"},
{"arcticskies35.png", "arcticskies/arcticskies35.png"},
{"arcticskies4.png", "arcticskies/arcticskies4.png"},
{"block-snow-background.png", "block_snow/block-snow-background.png"},
{"block-snow-midground.png", "block_snow/block-snow-midground.png"},
{"block-snow-top.png", "block_snow/block-snow-top.png"},
{"bluemountain-bottom.png", "bluemountain/bluemountain-bottom.png"},
{"bluemountain-middle.png", "bluemountain/bluemountain-middle.png"},
{"bluemountain-top.png", "bluemountain/bluemountain-top.png"},
{"bluemountain2.png", "bluemountain/bluemountain2.png"},
{"castle_foreground.png", "castle/castle_foreground.png"},
{"snowcastle.png", "castle/snowcastle.png"},
{"cloud-mountains-background.png", "cloud_mountains/cloud-mountains-background.png"},
{"cloud-mountains-bottom.png", "cloud_mountains/cloud-mountains-bottom.png"},
{"cloud-mountains-forground.png", "cloud_mountains/cloud-mountains-forground.png"},
{"cloud-mountains-midground.png", "cloud_mountains/cloud-mountains-midground.png"},
{"dawn_hill_para_blur.png", "forest/dawn_hill_para_blur.png"},
{"forest2_para.png", "forest/forest2_para.png"},
{"forest_para2.png", "forest/forest_para2.png"},
{"forest_para3.png", "forest/forest_para3.png"},
{"forest_para3_bottom.png", "forest/forest_para3_bottom.png"},
{"nighthills.png", "forest/nighthills.png"},
{"ghostforest.jpg", "ghostforest/ghostforest.jpg"},
{"ghostforest_grave.png", "ghostforest/ghostforest_grave.png"},
{"ghostforest_para.png", "ghostforest/ghostforest_para.png"},
{"cave2.jpg", "ice_cave/cave2.jpg"},
{"darkcave-background.png", "ice_cave/darkcave-background.png"},
{"darkcave-middle.png", "ice_cave/darkcave-middle.png"},
{"darkcave-preview.png", "ice_cave/darkcave-preview.png"},
{"darkcave-top_and_bottom.png", "ice_cave/darkcave-top_and_bottom.png"},
{"darkcavemidground-middle.png", "ice_cave/darkcavemidground-middle.png"},
{"darkcavemidground-top_and_bottom.png", "ice_cave/darkcavemidground-top_and_bottom.png"},
{"black_800px.png", "misc/black_800px.png"},
{"fog.png", "misc/fog.png"},
{"grid.png", "misc/grid.png"},
{"grid.surface", "misc/grid.surface"},
{"heatshimmer-displacement.png", "misc/heatshimmer-displacement.png"},
{"heatshimmer.png", "misc/heatshimmer.png"},
{"heatshimmer.surface", "misc/heatshimmer.surface"},
{"leaves.png", "misc/leaves.png"},
{"oiltux.jpg", "misc/oiltux.jpg"},
{"transparent_up.png", "misc/transparent_up.png"},
{"nightsky.png", "nightsky/nightsky.png"},
{"nightsky_bottom.png", "nightsky/nightsky_bottom.png"},
{"nightsky_middle.png", "nightsky/nightsky_middle.png"},
{"nightsky_para.png", "nightsky/nightsky_para.png"},
{"nightsky_top.png", "nightsky/nightsky_top.png"},
};

} // namespace

SurfacePtr
Background::load_background(const std::string& image_path)
void
Background::set_top_image_action(const std::string& action)
{
if (image_path.empty())
return nullptr;
if (m_image_top)
m_image_top->set_action(action);
}

if (PHYSFS_exists(image_path.c_str()))
// No need to search fallback paths.
return Surface::from_file(image_path);
void
Background::set_image_action(const std::string& action)
{
m_image->set_action(action);
}

// Search for a fallback image in fallback_paths.
const std::string& default_dir = "images/background/";
const std::string& default_dir2 = "/images/background/";
std::string new_path = image_path;
if (image_path.substr(0, default_dir.length()) == default_dir)
new_path.erase(0, default_dir.length());
else if (image_path.substr(0, default_dir2.length()) == default_dir2)
new_path.erase(0, default_dir2.length());
auto it = fallback_paths.find(new_path);
if (it == fallback_paths.end())
// Unknown image, let the texture manager select the dummy texture.
return Surface::from_file(image_path);
void
Background::set_bottom_image_action(const std::string& action)
{
if (m_image_bottom)
m_image_bottom->set_action(action);
}

new_path = default_dir + it->second;
return Surface::from_file(new_path);
void
Background::set_image_actions(const std::string& action)
Vankata453 marked this conversation as resolved.
Show resolved Hide resolved
{
set_top_image_action(action);
set_image_action(action);
set_bottom_image_action(action);
}

void
Expand Down
16 changes: 9 additions & 7 deletions src/object/background.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

#include "math/vector.hpp"
#include "scripting/background.hpp"
#include "sprite/sprite_ptr.hpp"
#include "squirrel/exposed_object.hpp"
#include "supertux/game_object.hpp"
#include "supertux/timer.hpp"
#include "video/blend.hpp"
#include "video/drawing_context.hpp"
#include "video/flip.hpp"
#include "video/surface_ptr.hpp"

class ReaderMapping;

Expand Down Expand Up @@ -68,6 +68,11 @@ class Background final : public GameObject,
void set_color(Color color) { m_color = color; }
void fade_color(Color color, float time);

void set_top_image_action(const std::string& action);
void set_image_action(const std::string& action);
void set_bottom_image_action(const std::string& action);
void set_image_actions(const std::string& action);

private:
enum Alignment {
NO_ALIGNMENT,
Expand All @@ -77,9 +82,6 @@ class Background final : public GameObject,
BOTTOM_ALIGNMENT
};

private:
SurfacePtr load_background(const std::string& image_path);

private:
/** Backgrounds with NO_ALIGNMENT are repeated over the whole
screen, backgrounds with left, right, top, bottom alignment are
Expand All @@ -99,9 +101,9 @@ class Background final : public GameObject,
Vector m_parallax_speed;
Vector m_scroll_speed;
Vector m_scroll_offset;
SurfacePtr m_image_top; /**< image to draw above pos */
SurfacePtr m_image; /**< image to draw, anchored at pos */
SurfacePtr m_image_bottom; /**< image to draw below pos+screenheight */
SpritePtr m_image_top; /**< image to draw above pos */
SpritePtr m_image; /**< image to draw, anchored at pos */
SpritePtr m_image_bottom; /**< image to draw below pos+screenheight */

Blend m_blend;
Color m_color;
Expand Down
Loading