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 1 commit
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
83 changes: 66 additions & 17 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,6 @@
#include "util/reader_mapping.hpp"
#include "util/writer.hpp"
#include "video/drawing_context.hpp"
#include "video/surface.hpp"

Background::Background() :
ExposedObject<Background, scripting::Background>(this),
Expand Down Expand Up @@ -192,9 +193,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 +300,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 +320,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 +329,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 +338,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 +347,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 +360,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 @@ -459,15 +469,25 @@ std::unordered_map<std::string, std::string> fallback_paths = {

} // namespace

SurfacePtr
SpritePtr
Vankata453 marked this conversation as resolved.
Show resolved Hide resolved
Background::load_background(const std::string& image_path)
{
SpritePtr sprite = load_background_sprite(image_path);
if (!sprite) return nullptr;

sprite->set_animation_enabled(!Editor::is_active());
return sprite;
}

SpritePtr
Background::load_background_sprite(const std::string& image_path)
{
if (image_path.empty())
return nullptr;

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

// Search for a fallback image in fallback_paths.
const std::string& default_dir = "images/background/";
Expand All @@ -477,13 +497,42 @@ Background::load_background(const std::string& image_path)
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);
// 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 Surface::from_file(new_path);
return SpriteManager::current()->create(new_path);
}

void
Background::set_top_image_action(const std::string& action)
{
if (m_image_top)
m_image_top->set_action(action);
}

void
Background::set_image_action(const std::string& action)
{
m_image->set_action(action);
}

void
Background::set_bottom_image_action(const std::string& action)
{
if (m_image_bottom)
m_image_bottom->set_action(action);
}

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: 11 additions & 5 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 @@ -78,7 +83,8 @@ class Background final : public GameObject,
};

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

private:
/** Backgrounds with NO_ALIGNMENT are repeated over the whole
Expand All @@ -99,9 +105,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
28 changes: 28 additions & 0 deletions src/scripting/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,34 @@ Background::fade_color(float red, float green, float blue, float alpha, float ti
object.fade_color(Color(red, green, blue, alpha), time);
}

void
Background::set_top_image_action(const std::string& action)
{
SCRIPT_GUARD_VOID;
object.set_top_image_action(action);
}

void
Background::set_image_action(const std::string& action)
{
SCRIPT_GUARD_VOID;
object.set_image_action(action);
}

void
Background::set_bottom_image_action(const std::string& action)
{
SCRIPT_GUARD_VOID;
object.set_bottom_image_action(action);
}

void
Background::set_image_actions(const std::string& action)
{
SCRIPT_GUARD_VOID;
object.set_image_actions(action);
}

} // namespace scripting

/* EOF */
21 changes: 21 additions & 0 deletions src/scripting/background.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ class Background final
* @param float $time
*/
void fade_color(float red, float green, float blue, float alpha, float time);

/**
* Sets the sprite action for the top image.
* @param string $action
*/
void set_top_image_action(const std::string& action);
/**
* Sets the sprite action for the main (middle) image.
* @param string $action
*/
void set_image_action(const std::string& action);
/**
* Sets the sprite action for the bottom image.
* @param string $action
*/
void set_bottom_image_action(const std::string& action);
/**
* Sets the sprite action for all images (top, middle and bottom).
* @param string $action
*/
void set_image_actions(const std::string& action);
};

} // namespace scripting
Expand Down
Loading
Loading