You can now set a sprite as a background in Background which will simplify a lot of games that just place sprites over a single image.

This commit is contained in:
Zed A. Shaw 2025-06-01 22:52:54 -04:00
parent 6fb20c5085
commit e1d61dc2c1
9 changed files with 79 additions and 22 deletions

View file

@ -11,27 +11,36 @@ namespace lel {
grid_x(x),
grid_y(y),
grid_w(width),
grid_h(height),
cur(0, 0)
grid_h(height)
{
}
Parser::Parser() : cur(0, 0) { }
void Parser::position(int x, int y, int width, int height) {
grid_x = x;
grid_y = y;
grid_w = width;
grid_h = height;
// used in GUIs for setting backgrounds
main = {
.x=grid_x,
.y=grid_y,
.w=grid_w,
.h=grid_h,
.mid_x=std::midpoint(grid_x, grid_x + grid_w),
.mid_y=std::midpoint(grid_y, grid_y + grid_h),
.max_w=grid_w,
.max_h=grid_h
};
}
void Parser::id(std::string name) {
void Parser::id(const std::string& name) {
if(name != "_") {
assert(!cells.contains(name) && "duplicate cell name");
cells.insert_or_assign(name, cur);
}
cur = {cur.col + 1, cur.row};
cur = {.col=cur.col + 1, .row=cur.row};
auto& row = grid.back();
row.push_back(name);
}
@ -87,15 +96,18 @@ namespace lel {
}
void Parser::reset() {
cur = {0, 0};
cur = {.col=0, .row=0};
main = {.col=0, .row=0};
grid.clear();
cells.clear();
}
std::optional<std::string> Parser::hit(int x, int y) {
for(auto& [name, cell] : cells) {
if((x >= cell.x && x <= cell.x + cell.w) &&
(y >= cell.y && y <= cell.y + cell.h)) {
if(name != "_MAIN" &&
(x >= cell.x && x <= cell.x + cell.w) &&
(y >= cell.y && y <= cell.y + cell.h))
{
return name;
}
}
@ -106,6 +118,7 @@ namespace lel {
Cell center(int width, int height, Cell &parent) {
Cell copy = parent;
// BUG: could I use std::midpoint here or am I even using this?
copy.x = parent.mid_x - width / 2;
copy.y = parent.mid_y - height / 2;
copy.w = width;

View file

@ -86,7 +86,7 @@ static const int Parser_en_main = 1;
#line 44 "src/guecs/lel_parser.rl"
bool Parser::parse(std::string input) {
bool Parser::parse(const std::string& input) {
reset();
int cs = 0;
const char *start = nullptr;

View file

@ -42,7 +42,7 @@ namespace lel {
%% write data;
bool Parser::parse(std::string input) {
bool Parser::parse(const std::string& input) {
reset();
int cs = 0;
const char *start = nullptr;

View file

@ -121,8 +121,36 @@ namespace guecs {
assert(shape != nullptr && "failed to make rectangle");
}
void Background::set_color(sf::Color c) {
color = c;
}
void Background::set_sprite(const std::string& name, bool stretch) {
auto sprite_texture = BACKEND->texture_get(name);
sprite = make_shared<sf::Sprite>(
*sprite_texture.texture,
sprite_texture.sprite->getTextureRect());
sprite->setPosition({float(x), float(y)});
if(stretch) {
auto bounds = sprite->getLocalBounds();
sprite->setScale({
float(w) / bounds.size.x,
float(h) / bounds.size.y});
}
}
void Background::render(sf::RenderWindow& window) {
window.draw(*shape);
if(shape != nullptr) {
window.draw(*shape);
}
if(sprite != nullptr) {
window.draw(*sprite);
}
}
void Effect::init(lel::Cell &cell) {

View file

@ -143,7 +143,7 @@ namespace guecs {
bool UI::mouse(float x, float y, bool hover) {
int action_count = 0;
// BUG: use lel::Parser.hit instead of this
// BUG: Is Parser::hit useful?
query<lel::Cell>([&](auto ent, auto& cell) {
if((x >= cell.x && x <= cell.x + cell.w) &&