Phase 1 - FOV Enum System: - Create PyFOV.h/cpp with mcrfpy.FOV IntEnum (BASIC, DIAMOND, SHADOW, etc.) - Add mcrfpy.default_fov module property initialized to FOV.BASIC - Add grid.fov and grid.fov_radius properties for per-grid defaults - Remove deprecated module-level FOV_* constants (breaking change) Phase 2 - Layer Operations: - Implement ColorLayer.fill_rect(pos, size, color) for rectangle fills - Implement TileLayer.fill_rect(pos, size, index) for tile rectangle fills - Implement ColorLayer.draw_fov(source, radius, fov, visible, discovered, unknown) to paint FOV-based visibility on color layers using parent grid's TCOD map The FOV enum uses Python's IntEnum for type safety while maintaining backward compatibility with integer values. Tests updated to use new API. Addresses #114 (FOV enum), #113 (layer operations) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
0545dd4861
commit
018e73590f
11 changed files with 1061 additions and 407 deletions
|
|
@ -2,6 +2,7 @@
|
|||
#include "UIGrid.h"
|
||||
#include "PyColor.h"
|
||||
#include "PyTexture.h"
|
||||
#include "PyFOV.h"
|
||||
#include <sstream>
|
||||
|
||||
// =============================================================================
|
||||
|
|
@ -11,47 +12,96 @@
|
|||
GridLayer::GridLayer(GridLayerType type, int z_index, int grid_x, int grid_y, UIGrid* parent)
|
||||
: type(type), z_index(z_index), grid_x(grid_x), grid_y(grid_y),
|
||||
parent_grid(parent), visible(true),
|
||||
dirty(true), texture_initialized(false),
|
||||
chunks_x(0), chunks_y(0),
|
||||
cached_cell_width(0), cached_cell_height(0)
|
||||
{}
|
||||
|
||||
void GridLayer::markDirty() {
|
||||
dirty = true;
|
||||
{
|
||||
initChunks();
|
||||
}
|
||||
|
||||
void GridLayer::ensureTextureSize(int cell_width, int cell_height) {
|
||||
// Check if we need to resize/create the texture
|
||||
unsigned int required_width = grid_x * cell_width;
|
||||
unsigned int required_height = grid_y * cell_height;
|
||||
void GridLayer::initChunks() {
|
||||
// Calculate chunk dimensions
|
||||
chunks_x = (grid_x + CHUNK_SIZE - 1) / CHUNK_SIZE;
|
||||
chunks_y = (grid_y + CHUNK_SIZE - 1) / CHUNK_SIZE;
|
||||
int total_chunks = chunks_x * chunks_y;
|
||||
|
||||
// Maximum texture size limit (prevent excessive memory usage)
|
||||
const unsigned int MAX_TEXTURE_SIZE = 4096;
|
||||
if (required_width > MAX_TEXTURE_SIZE) required_width = MAX_TEXTURE_SIZE;
|
||||
if (required_height > MAX_TEXTURE_SIZE) required_height = MAX_TEXTURE_SIZE;
|
||||
// Initialize per-chunk tracking
|
||||
chunk_dirty.assign(total_chunks, true); // All chunks start dirty
|
||||
chunk_texture_initialized.assign(total_chunks, false);
|
||||
chunk_textures.clear();
|
||||
chunk_textures.reserve(total_chunks);
|
||||
for (int i = 0; i < total_chunks; ++i) {
|
||||
chunk_textures.push_back(std::make_unique<sf::RenderTexture>());
|
||||
}
|
||||
}
|
||||
|
||||
// Skip if already properly sized
|
||||
if (texture_initialized &&
|
||||
cached_texture.getSize().x == required_width &&
|
||||
cached_texture.getSize().y == required_height &&
|
||||
void GridLayer::markDirty() {
|
||||
// Mark ALL chunks as dirty
|
||||
std::fill(chunk_dirty.begin(), chunk_dirty.end(), true);
|
||||
}
|
||||
|
||||
void GridLayer::markDirty(int cell_x, int cell_y) {
|
||||
// Mark only the specific chunk containing this cell
|
||||
if (cell_x < 0 || cell_x >= grid_x || cell_y < 0 || cell_y >= grid_y) return;
|
||||
|
||||
int chunk_idx = getChunkIndex(cell_x, cell_y);
|
||||
if (chunk_idx >= 0 && chunk_idx < static_cast<int>(chunk_dirty.size())) {
|
||||
chunk_dirty[chunk_idx] = true;
|
||||
}
|
||||
}
|
||||
|
||||
int GridLayer::getChunkIndex(int cell_x, int cell_y) const {
|
||||
int cx = cell_x / CHUNK_SIZE;
|
||||
int cy = cell_y / CHUNK_SIZE;
|
||||
return cy * chunks_x + cx;
|
||||
}
|
||||
|
||||
void GridLayer::getChunkCoords(int cell_x, int cell_y, int& chunk_x, int& chunk_y) const {
|
||||
chunk_x = cell_x / CHUNK_SIZE;
|
||||
chunk_y = cell_y / CHUNK_SIZE;
|
||||
}
|
||||
|
||||
void GridLayer::getChunkBounds(int chunk_x, int chunk_y, int& start_x, int& start_y, int& end_x, int& end_y) const {
|
||||
start_x = chunk_x * CHUNK_SIZE;
|
||||
start_y = chunk_y * CHUNK_SIZE;
|
||||
end_x = std::min(start_x + CHUNK_SIZE, grid_x);
|
||||
end_y = std::min(start_y + CHUNK_SIZE, grid_y);
|
||||
}
|
||||
|
||||
void GridLayer::ensureChunkTexture(int chunk_idx, int cell_width, int cell_height) {
|
||||
if (chunk_idx < 0 || chunk_idx >= static_cast<int>(chunk_textures.size())) return;
|
||||
if (!chunk_textures[chunk_idx]) return;
|
||||
|
||||
// Calculate chunk dimensions in cells
|
||||
int cx = chunk_idx % chunks_x;
|
||||
int cy = chunk_idx / chunks_x;
|
||||
int start_x, start_y, end_x, end_y;
|
||||
getChunkBounds(cx, cy, start_x, start_y, end_x, end_y);
|
||||
|
||||
int chunk_width_cells = end_x - start_x;
|
||||
int chunk_height_cells = end_y - start_y;
|
||||
|
||||
unsigned int required_width = chunk_width_cells * cell_width;
|
||||
unsigned int required_height = chunk_height_cells * cell_height;
|
||||
|
||||
// Check if texture needs (re)creation
|
||||
if (chunk_texture_initialized[chunk_idx] &&
|
||||
chunk_textures[chunk_idx]->getSize().x == required_width &&
|
||||
chunk_textures[chunk_idx]->getSize().y == required_height &&
|
||||
cached_cell_width == cell_width &&
|
||||
cached_cell_height == cell_height) {
|
||||
return; // Already properly sized
|
||||
}
|
||||
|
||||
// Create the texture for this chunk
|
||||
if (!chunk_textures[chunk_idx]->create(required_width, required_height)) {
|
||||
chunk_texture_initialized[chunk_idx] = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Create or resize the texture (SFML uses .create() not .resize())
|
||||
if (!cached_texture.create(required_width, required_height)) {
|
||||
// Creation failed - texture will remain uninitialized
|
||||
texture_initialized = false;
|
||||
return;
|
||||
}
|
||||
|
||||
chunk_texture_initialized[chunk_idx] = true;
|
||||
chunk_dirty[chunk_idx] = true; // Force re-render after resize
|
||||
cached_cell_width = cell_width;
|
||||
cached_cell_height = cell_height;
|
||||
texture_initialized = true;
|
||||
dirty = true; // Force re-render after resize
|
||||
|
||||
// Setup the sprite to use the texture
|
||||
cached_sprite.setTexture(cached_texture.getTexture());
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
|
@ -73,7 +123,76 @@ const sf::Color& ColorLayer::at(int x, int y) const {
|
|||
|
||||
void ColorLayer::fill(const sf::Color& color) {
|
||||
std::fill(colors.begin(), colors.end(), color);
|
||||
markDirty(); // #148 - Mark for re-render
|
||||
markDirty(); // Mark ALL chunks for re-render
|
||||
}
|
||||
|
||||
void ColorLayer::fillRect(int x, int y, int width, int height, const sf::Color& color) {
|
||||
// Clamp to valid bounds
|
||||
int x1 = std::max(0, x);
|
||||
int y1 = std::max(0, y);
|
||||
int x2 = std::min(grid_x, x + width);
|
||||
int y2 = std::min(grid_y, y + height);
|
||||
|
||||
// Fill the rectangle
|
||||
for (int fy = y1; fy < y2; ++fy) {
|
||||
for (int fx = x1; fx < x2; ++fx) {
|
||||
colors[fy * grid_x + fx] = color;
|
||||
}
|
||||
}
|
||||
|
||||
// Mark affected chunks dirty
|
||||
int chunk_x1 = x1 / CHUNK_SIZE;
|
||||
int chunk_y1 = y1 / CHUNK_SIZE;
|
||||
int chunk_x2 = (x2 - 1) / CHUNK_SIZE;
|
||||
int chunk_y2 = (y2 - 1) / CHUNK_SIZE;
|
||||
|
||||
for (int cy = chunk_y1; cy <= chunk_y2; ++cy) {
|
||||
for (int cx = chunk_x1; cx <= chunk_x2; ++cx) {
|
||||
int idx = cy * chunks_x + cx;
|
||||
if (idx >= 0 && idx < static_cast<int>(chunk_dirty.size())) {
|
||||
chunk_dirty[idx] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ColorLayer::drawFOV(int source_x, int source_y, int radius,
|
||||
TCOD_fov_algorithm_t algorithm,
|
||||
const sf::Color& visible_color,
|
||||
const sf::Color& discovered_color,
|
||||
const sf::Color& unknown_color) {
|
||||
// Need parent grid for TCOD map access
|
||||
if (!parent_grid) {
|
||||
return; // Cannot compute FOV without parent grid
|
||||
}
|
||||
|
||||
// Import UIGrid here to avoid circular dependency in header
|
||||
// parent_grid is already a UIGrid*, we can use its tcod_map directly
|
||||
// But we need to forward declare access to it...
|
||||
|
||||
// Compute FOV on the parent grid
|
||||
parent_grid->computeFOV(source_x, source_y, radius, true, algorithm);
|
||||
|
||||
// Paint cells based on visibility
|
||||
for (int cy = 0; cy < grid_y; ++cy) {
|
||||
for (int cx = 0; cx < grid_x; ++cx) {
|
||||
// Check if in FOV (visible right now)
|
||||
if (parent_grid->isInFOV(cx, cy)) {
|
||||
colors[cy * grid_x + cx] = visible_color;
|
||||
}
|
||||
// Check if previously discovered (current color != unknown)
|
||||
else if (colors[cy * grid_x + cx] != unknown_color) {
|
||||
colors[cy * grid_x + cx] = discovered_color;
|
||||
}
|
||||
// Otherwise leave as unknown (or set to unknown if first time)
|
||||
else {
|
||||
colors[cy * grid_x + cx] = unknown_color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mark entire layer dirty
|
||||
markDirty();
|
||||
}
|
||||
|
||||
void ColorLayer::resize(int new_grid_x, int new_grid_y) {
|
||||
|
|
@ -92,36 +211,53 @@ void ColorLayer::resize(int new_grid_x, int new_grid_y) {
|
|||
grid_x = new_grid_x;
|
||||
grid_y = new_grid_y;
|
||||
|
||||
// #148 - Invalidate cached texture (will be resized on next render)
|
||||
texture_initialized = false;
|
||||
markDirty();
|
||||
// Reinitialize chunks for new dimensions
|
||||
initChunks();
|
||||
}
|
||||
|
||||
// #148 - Render all cells to cached texture (called when dirty)
|
||||
void ColorLayer::renderToTexture(int cell_width, int cell_height) {
|
||||
ensureTextureSize(cell_width, cell_height);
|
||||
if (!texture_initialized) return;
|
||||
// Render a single chunk to its cached texture
|
||||
void ColorLayer::renderChunkToTexture(int chunk_x, int chunk_y, int cell_width, int cell_height) {
|
||||
int chunk_idx = chunk_y * chunks_x + chunk_x;
|
||||
if (chunk_idx < 0 || chunk_idx >= static_cast<int>(chunk_textures.size())) return;
|
||||
if (!chunk_textures[chunk_idx]) return;
|
||||
|
||||
cached_texture.clear(sf::Color::Transparent);
|
||||
ensureChunkTexture(chunk_idx, cell_width, cell_height);
|
||||
if (!chunk_texture_initialized[chunk_idx]) return;
|
||||
|
||||
// Get chunk bounds
|
||||
int start_x, start_y, end_x, end_y;
|
||||
getChunkBounds(chunk_x, chunk_y, start_x, start_y, end_x, end_y);
|
||||
|
||||
chunk_textures[chunk_idx]->clear(sf::Color::Transparent);
|
||||
|
||||
sf::RectangleShape rect;
|
||||
rect.setSize(sf::Vector2f(cell_width, cell_height));
|
||||
rect.setOutlineThickness(0);
|
||||
|
||||
// Render all cells to cached texture (no zoom - 1:1 pixel mapping)
|
||||
for (int x = 0; x < grid_x; ++x) {
|
||||
for (int y = 0; y < grid_y; ++y) {
|
||||
// Render only cells within this chunk (local coordinates in texture)
|
||||
for (int x = start_x; x < end_x; ++x) {
|
||||
for (int y = start_y; y < end_y; ++y) {
|
||||
const sf::Color& color = at(x, y);
|
||||
if (color.a == 0) continue; // Skip fully transparent
|
||||
|
||||
rect.setPosition(sf::Vector2f(x * cell_width, y * cell_height));
|
||||
// Position relative to chunk origin
|
||||
rect.setPosition(sf::Vector2f((x - start_x) * cell_width, (y - start_y) * cell_height));
|
||||
rect.setFillColor(color);
|
||||
cached_texture.draw(rect);
|
||||
chunk_textures[chunk_idx]->draw(rect);
|
||||
}
|
||||
}
|
||||
|
||||
cached_texture.display();
|
||||
dirty = false;
|
||||
chunk_textures[chunk_idx]->display();
|
||||
chunk_dirty[chunk_idx] = false;
|
||||
}
|
||||
|
||||
// Legacy: render all chunks (used by fill, resize, etc.)
|
||||
void ColorLayer::renderToTexture(int cell_width, int cell_height) {
|
||||
for (int cy = 0; cy < chunks_y; ++cy) {
|
||||
for (int cx = 0; cx < chunks_x; ++cx) {
|
||||
renderChunkToTexture(cx, cy, cell_width, cell_height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ColorLayer::render(sf::RenderTarget& target,
|
||||
|
|
@ -130,61 +266,67 @@ void ColorLayer::render(sf::RenderTarget& target,
|
|||
float zoom, int cell_width, int cell_height) {
|
||||
if (!visible) return;
|
||||
|
||||
// #148 - Use cached texture rendering
|
||||
// Re-render to texture only if dirty
|
||||
if (dirty || !texture_initialized) {
|
||||
renderToTexture(cell_width, cell_height);
|
||||
}
|
||||
// Calculate visible chunk range
|
||||
int chunk_left = std::max(0, left_edge / CHUNK_SIZE);
|
||||
int chunk_top = std::max(0, top_edge / CHUNK_SIZE);
|
||||
int chunk_right = std::min(chunks_x - 1, (x_limit + CHUNK_SIZE - 1) / CHUNK_SIZE);
|
||||
int chunk_bottom = std::min(chunks_y - 1, (y_limit + CHUNK_SIZE - 1) / CHUNK_SIZE);
|
||||
|
||||
if (!texture_initialized) {
|
||||
// Fallback to direct rendering if texture creation failed
|
||||
sf::RectangleShape rect;
|
||||
rect.setSize(sf::Vector2f(cell_width * zoom, cell_height * zoom));
|
||||
rect.setOutlineThickness(0);
|
||||
// Iterate only over visible chunks
|
||||
for (int cy = chunk_top; cy <= chunk_bottom; ++cy) {
|
||||
for (int cx = chunk_left; cx <= chunk_right; ++cx) {
|
||||
int chunk_idx = cy * chunks_x + cx;
|
||||
|
||||
for (int x = (left_edge - 1 >= 0 ? left_edge - 1 : 0); x < x_limit; ++x) {
|
||||
for (int y = (top_edge - 1 >= 0 ? top_edge - 1 : 0); y < y_limit; ++y) {
|
||||
if (x < 0 || x >= grid_x || y < 0 || y >= grid_y) continue;
|
||||
|
||||
const sf::Color& color = at(x, y);
|
||||
if (color.a == 0) continue;
|
||||
|
||||
auto pixel_pos = sf::Vector2f(
|
||||
(x * cell_width - left_spritepixels) * zoom,
|
||||
(y * cell_height - top_spritepixels) * zoom
|
||||
);
|
||||
|
||||
rect.setPosition(pixel_pos);
|
||||
rect.setFillColor(color);
|
||||
target.draw(rect);
|
||||
// Re-render chunk only if dirty AND visible
|
||||
if (chunk_dirty[chunk_idx] || !chunk_texture_initialized[chunk_idx]) {
|
||||
renderChunkToTexture(cx, cy, cell_width, cell_height);
|
||||
}
|
||||
|
||||
if (!chunk_texture_initialized[chunk_idx]) {
|
||||
// Fallback: direct rendering for this chunk
|
||||
int start_x, start_y, end_x, end_y;
|
||||
getChunkBounds(cx, cy, start_x, start_y, end_x, end_y);
|
||||
|
||||
sf::RectangleShape rect;
|
||||
rect.setSize(sf::Vector2f(cell_width * zoom, cell_height * zoom));
|
||||
rect.setOutlineThickness(0);
|
||||
|
||||
for (int x = start_x; x < end_x; ++x) {
|
||||
for (int y = start_y; y < end_y; ++y) {
|
||||
const sf::Color& color = at(x, y);
|
||||
if (color.a == 0) continue;
|
||||
|
||||
auto pixel_pos = sf::Vector2f(
|
||||
(x * cell_width - left_spritepixels) * zoom,
|
||||
(y * cell_height - top_spritepixels) * zoom
|
||||
);
|
||||
rect.setPosition(pixel_pos);
|
||||
rect.setFillColor(color);
|
||||
target.draw(rect);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Blit this chunk's texture to target
|
||||
int start_x, start_y, end_x, end_y;
|
||||
getChunkBounds(cx, cy, start_x, start_y, end_x, end_y);
|
||||
|
||||
// Chunk position in world pixel coordinates
|
||||
float chunk_world_x = start_x * cell_width;
|
||||
float chunk_world_y = start_y * cell_height;
|
||||
|
||||
// Position in target (accounting for viewport offset and zoom)
|
||||
float dest_x = (chunk_world_x - left_spritepixels) * zoom;
|
||||
float dest_y = (chunk_world_y - top_spritepixels) * zoom;
|
||||
|
||||
sf::Sprite chunk_sprite(chunk_textures[chunk_idx]->getTexture());
|
||||
chunk_sprite.setPosition(sf::Vector2f(dest_x, dest_y));
|
||||
chunk_sprite.setScale(sf::Vector2f(zoom, zoom));
|
||||
|
||||
target.draw(chunk_sprite);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Blit visible portion of cached texture with zoom applied
|
||||
// Calculate source rectangle (unzoomed pixel coordinates in cached texture)
|
||||
int src_left = std::max(0, (int)left_spritepixels);
|
||||
int src_top = std::max(0, (int)top_spritepixels);
|
||||
int src_width = std::min((int)cached_texture.getSize().x - src_left,
|
||||
(int)((x_limit - left_edge + 2) * cell_width));
|
||||
int src_height = std::min((int)cached_texture.getSize().y - src_top,
|
||||
(int)((y_limit - top_edge + 2) * cell_height));
|
||||
|
||||
if (src_width <= 0 || src_height <= 0) return;
|
||||
|
||||
// Set texture rect for visible portion
|
||||
cached_sprite.setTextureRect(sf::IntRect({src_left, src_top}, {src_width, src_height}));
|
||||
|
||||
// Position in target (offset for partial cell visibility)
|
||||
float dest_x = (src_left - left_spritepixels) * zoom;
|
||||
float dest_y = (src_top - top_spritepixels) * zoom;
|
||||
cached_sprite.setPosition(sf::Vector2f(dest_x, dest_y));
|
||||
|
||||
// Apply zoom via scale
|
||||
cached_sprite.setScale(sf::Vector2f(zoom, zoom));
|
||||
|
||||
target.draw(cached_sprite);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
|
@ -208,7 +350,37 @@ int TileLayer::at(int x, int y) const {
|
|||
|
||||
void TileLayer::fill(int tile_index) {
|
||||
std::fill(tiles.begin(), tiles.end(), tile_index);
|
||||
markDirty(); // #148 - Mark for re-render
|
||||
markDirty(); // Mark ALL chunks for re-render
|
||||
}
|
||||
|
||||
void TileLayer::fillRect(int x, int y, int width, int height, int tile_index) {
|
||||
// Clamp to valid bounds
|
||||
int x1 = std::max(0, x);
|
||||
int y1 = std::max(0, y);
|
||||
int x2 = std::min(grid_x, x + width);
|
||||
int y2 = std::min(grid_y, y + height);
|
||||
|
||||
// Fill the rectangle
|
||||
for (int fy = y1; fy < y2; ++fy) {
|
||||
for (int fx = x1; fx < x2; ++fx) {
|
||||
tiles[fy * grid_x + fx] = tile_index;
|
||||
}
|
||||
}
|
||||
|
||||
// Mark affected chunks dirty
|
||||
int chunk_x1 = x1 / CHUNK_SIZE;
|
||||
int chunk_y1 = y1 / CHUNK_SIZE;
|
||||
int chunk_x2 = (x2 - 1) / CHUNK_SIZE;
|
||||
int chunk_y2 = (y2 - 1) / CHUNK_SIZE;
|
||||
|
||||
for (int cy = chunk_y1; cy <= chunk_y2; ++cy) {
|
||||
for (int cx = chunk_x1; cx <= chunk_x2; ++cx) {
|
||||
int idx = cy * chunks_x + cx;
|
||||
if (idx >= 0 && idx < static_cast<int>(chunk_dirty.size())) {
|
||||
chunk_dirty[idx] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TileLayer::resize(int new_grid_x, int new_grid_y) {
|
||||
|
|
@ -227,32 +399,51 @@ void TileLayer::resize(int new_grid_x, int new_grid_y) {
|
|||
grid_x = new_grid_x;
|
||||
grid_y = new_grid_y;
|
||||
|
||||
// #148 - Invalidate cached texture (will be resized on next render)
|
||||
texture_initialized = false;
|
||||
markDirty();
|
||||
// Reinitialize chunks for new dimensions
|
||||
initChunks();
|
||||
}
|
||||
|
||||
// #148 - Render all cells to cached texture (called when dirty)
|
||||
void TileLayer::renderToTexture(int cell_width, int cell_height) {
|
||||
ensureTextureSize(cell_width, cell_height);
|
||||
if (!texture_initialized || !texture) return;
|
||||
// Render a single chunk to its cached texture
|
||||
void TileLayer::renderChunkToTexture(int chunk_x, int chunk_y, int cell_width, int cell_height) {
|
||||
if (!texture) return;
|
||||
|
||||
cached_texture.clear(sf::Color::Transparent);
|
||||
int chunk_idx = chunk_y * chunks_x + chunk_x;
|
||||
if (chunk_idx < 0 || chunk_idx >= static_cast<int>(chunk_textures.size())) return;
|
||||
if (!chunk_textures[chunk_idx]) return;
|
||||
|
||||
// Render all tiles to cached texture (no zoom - 1:1 pixel mapping)
|
||||
for (int x = 0; x < grid_x; ++x) {
|
||||
for (int y = 0; y < grid_y; ++y) {
|
||||
ensureChunkTexture(chunk_idx, cell_width, cell_height);
|
||||
if (!chunk_texture_initialized[chunk_idx]) return;
|
||||
|
||||
// Get chunk bounds
|
||||
int start_x, start_y, end_x, end_y;
|
||||
getChunkBounds(chunk_x, chunk_y, start_x, start_y, end_x, end_y);
|
||||
|
||||
chunk_textures[chunk_idx]->clear(sf::Color::Transparent);
|
||||
|
||||
// Render only tiles within this chunk (local coordinates in texture)
|
||||
for (int x = start_x; x < end_x; ++x) {
|
||||
for (int y = start_y; y < end_y; ++y) {
|
||||
int tile_index = at(x, y);
|
||||
if (tile_index < 0) continue; // No tile
|
||||
|
||||
auto pixel_pos = sf::Vector2f(x * cell_width, y * cell_height);
|
||||
// Position relative to chunk origin
|
||||
auto pixel_pos = sf::Vector2f((x - start_x) * cell_width, (y - start_y) * cell_height);
|
||||
sf::Sprite sprite = texture->sprite(tile_index, pixel_pos, sf::Vector2f(1.0f, 1.0f));
|
||||
cached_texture.draw(sprite);
|
||||
chunk_textures[chunk_idx]->draw(sprite);
|
||||
}
|
||||
}
|
||||
|
||||
cached_texture.display();
|
||||
dirty = false;
|
||||
chunk_textures[chunk_idx]->display();
|
||||
chunk_dirty[chunk_idx] = false;
|
||||
}
|
||||
|
||||
// Legacy: render all chunks (used by fill, resize, etc.)
|
||||
void TileLayer::renderToTexture(int cell_width, int cell_height) {
|
||||
for (int cy = 0; cy < chunks_y; ++cy) {
|
||||
for (int cx = 0; cx < chunks_x; ++cx) {
|
||||
renderChunkToTexture(cx, cy, cell_width, cell_height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TileLayer::render(sf::RenderTarget& target,
|
||||
|
|
@ -261,56 +452,62 @@ void TileLayer::render(sf::RenderTarget& target,
|
|||
float zoom, int cell_width, int cell_height) {
|
||||
if (!visible || !texture) return;
|
||||
|
||||
// #148 - Use cached texture rendering
|
||||
// Re-render to texture only if dirty
|
||||
if (dirty || !texture_initialized) {
|
||||
renderToTexture(cell_width, cell_height);
|
||||
}
|
||||
// Calculate visible chunk range
|
||||
int chunk_left = std::max(0, left_edge / CHUNK_SIZE);
|
||||
int chunk_top = std::max(0, top_edge / CHUNK_SIZE);
|
||||
int chunk_right = std::min(chunks_x - 1, (x_limit + CHUNK_SIZE - 1) / CHUNK_SIZE);
|
||||
int chunk_bottom = std::min(chunks_y - 1, (y_limit + CHUNK_SIZE - 1) / CHUNK_SIZE);
|
||||
|
||||
if (!texture_initialized) {
|
||||
// Fallback to direct rendering if texture creation failed
|
||||
for (int x = (left_edge - 1 >= 0 ? left_edge - 1 : 0); x < x_limit; ++x) {
|
||||
for (int y = (top_edge - 1 >= 0 ? top_edge - 1 : 0); y < y_limit; ++y) {
|
||||
if (x < 0 || x >= grid_x || y < 0 || y >= grid_y) continue;
|
||||
// Iterate only over visible chunks
|
||||
for (int cy = chunk_top; cy <= chunk_bottom; ++cy) {
|
||||
for (int cx = chunk_left; cx <= chunk_right; ++cx) {
|
||||
int chunk_idx = cy * chunks_x + cx;
|
||||
|
||||
int tile_index = at(x, y);
|
||||
if (tile_index < 0) continue;
|
||||
|
||||
auto pixel_pos = sf::Vector2f(
|
||||
(x * cell_width - left_spritepixels) * zoom,
|
||||
(y * cell_height - top_spritepixels) * zoom
|
||||
);
|
||||
|
||||
sf::Sprite sprite = texture->sprite(tile_index, pixel_pos, sf::Vector2f(zoom, zoom));
|
||||
target.draw(sprite);
|
||||
// Re-render chunk only if dirty AND visible
|
||||
if (chunk_dirty[chunk_idx] || !chunk_texture_initialized[chunk_idx]) {
|
||||
renderChunkToTexture(cx, cy, cell_width, cell_height);
|
||||
}
|
||||
|
||||
if (!chunk_texture_initialized[chunk_idx]) {
|
||||
// Fallback: direct rendering for this chunk
|
||||
int start_x, start_y, end_x, end_y;
|
||||
getChunkBounds(cx, cy, start_x, start_y, end_x, end_y);
|
||||
|
||||
for (int x = start_x; x < end_x; ++x) {
|
||||
for (int y = start_y; y < end_y; ++y) {
|
||||
int tile_index = at(x, y);
|
||||
if (tile_index < 0) continue;
|
||||
|
||||
auto pixel_pos = sf::Vector2f(
|
||||
(x * cell_width - left_spritepixels) * zoom,
|
||||
(y * cell_height - top_spritepixels) * zoom
|
||||
);
|
||||
sf::Sprite sprite = texture->sprite(tile_index, pixel_pos, sf::Vector2f(zoom, zoom));
|
||||
target.draw(sprite);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Blit this chunk's texture to target
|
||||
int start_x, start_y, end_x, end_y;
|
||||
getChunkBounds(cx, cy, start_x, start_y, end_x, end_y);
|
||||
|
||||
// Chunk position in world pixel coordinates
|
||||
float chunk_world_x = start_x * cell_width;
|
||||
float chunk_world_y = start_y * cell_height;
|
||||
|
||||
// Position in target (accounting for viewport offset and zoom)
|
||||
float dest_x = (chunk_world_x - left_spritepixels) * zoom;
|
||||
float dest_y = (chunk_world_y - top_spritepixels) * zoom;
|
||||
|
||||
sf::Sprite chunk_sprite(chunk_textures[chunk_idx]->getTexture());
|
||||
chunk_sprite.setPosition(sf::Vector2f(dest_x, dest_y));
|
||||
chunk_sprite.setScale(sf::Vector2f(zoom, zoom));
|
||||
|
||||
target.draw(chunk_sprite);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Blit visible portion of cached texture with zoom applied
|
||||
// Calculate source rectangle (unzoomed pixel coordinates in cached texture)
|
||||
int src_left = std::max(0, (int)left_spritepixels);
|
||||
int src_top = std::max(0, (int)top_spritepixels);
|
||||
int src_width = std::min((int)cached_texture.getSize().x - src_left,
|
||||
(int)((x_limit - left_edge + 2) * cell_width));
|
||||
int src_height = std::min((int)cached_texture.getSize().y - src_top,
|
||||
(int)((y_limit - top_edge + 2) * cell_height));
|
||||
|
||||
if (src_width <= 0 || src_height <= 0) return;
|
||||
|
||||
// Set texture rect for visible portion
|
||||
cached_sprite.setTextureRect(sf::IntRect({src_left, src_top}, {src_width, src_height}));
|
||||
|
||||
// Position in target (offset for partial cell visibility)
|
||||
float dest_x = (src_left - left_spritepixels) * zoom;
|
||||
float dest_y = (src_top - top_spritepixels) * zoom;
|
||||
cached_sprite.setPosition(sf::Vector2f(dest_x, dest_y));
|
||||
|
||||
// Apply zoom via scale
|
||||
cached_sprite.setScale(sf::Vector2f(zoom, zoom));
|
||||
|
||||
target.draw(cached_sprite);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
|
@ -324,6 +521,24 @@ PyMethodDef PyGridLayerAPI::ColorLayer_methods[] = {
|
|||
"set(x, y, color)\n\nSet the color at cell position (x, y)."},
|
||||
{"fill", (PyCFunction)PyGridLayerAPI::ColorLayer_fill, METH_VARARGS,
|
||||
"fill(color)\n\nFill the entire layer with the specified color."},
|
||||
{"fill_rect", (PyCFunction)PyGridLayerAPI::ColorLayer_fill_rect, METH_VARARGS | METH_KEYWORDS,
|
||||
"fill_rect(pos, size, color)\n\n"
|
||||
"Fill a rectangular region with a color.\n\n"
|
||||
"Args:\n"
|
||||
" pos (tuple): Top-left corner as (x, y)\n"
|
||||
" size (tuple): Dimensions as (width, height)\n"
|
||||
" color: Color object or (r, g, b[, a]) tuple"},
|
||||
{"draw_fov", (PyCFunction)PyGridLayerAPI::ColorLayer_draw_fov, METH_VARARGS | METH_KEYWORDS,
|
||||
"draw_fov(source, radius=None, fov=None, visible=None, discovered=None, unknown=None)\n\n"
|
||||
"Paint cells based on field-of-view visibility from source position.\n\n"
|
||||
"Args:\n"
|
||||
" source (tuple): FOV origin as (x, y)\n"
|
||||
" radius (int): FOV radius. Default: grid's fov_radius\n"
|
||||
" fov (FOV): FOV algorithm. Default: grid's fov setting\n"
|
||||
" visible (Color): Color for currently visible cells\n"
|
||||
" discovered (Color): Color for previously seen cells\n"
|
||||
" unknown (Color): Color for never-seen cells\n\n"
|
||||
"Note: Layer must be attached to a grid for FOV calculation."},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
|
|
@ -442,7 +657,7 @@ PyObject* PyGridLayerAPI::ColorLayer_set(PyColorLayerObject* self, PyObject* arg
|
|||
Py_DECREF(color_type);
|
||||
|
||||
self->data->at(x, y) = color;
|
||||
self->data->markDirty(); // #148 - Mark for re-render
|
||||
self->data->markDirty(x, y); // Mark only the affected chunk
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
|
|
@ -486,6 +701,170 @@ PyObject* PyGridLayerAPI::ColorLayer_fill(PyColorLayerObject* self, PyObject* ar
|
|||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyObject* PyGridLayerAPI::ColorLayer_fill_rect(PyColorLayerObject* self, PyObject* args, PyObject* kwds) {
|
||||
static const char* kwlist[] = {"pos", "size", "color", NULL};
|
||||
PyObject* pos_obj;
|
||||
PyObject* size_obj;
|
||||
PyObject* color_obj;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOO", const_cast<char**>(kwlist),
|
||||
&pos_obj, &size_obj, &color_obj)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!self->data) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "Layer has no data");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Parse pos
|
||||
int x, y;
|
||||
if (PyTuple_Check(pos_obj) && PyTuple_Size(pos_obj) == 2) {
|
||||
x = PyLong_AsLong(PyTuple_GetItem(pos_obj, 0));
|
||||
y = PyLong_AsLong(PyTuple_GetItem(pos_obj, 1));
|
||||
if (PyErr_Occurred()) return NULL;
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "pos must be a (x, y) tuple");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Parse size
|
||||
int width, height;
|
||||
if (PyTuple_Check(size_obj) && PyTuple_Size(size_obj) == 2) {
|
||||
width = PyLong_AsLong(PyTuple_GetItem(size_obj, 0));
|
||||
height = PyLong_AsLong(PyTuple_GetItem(size_obj, 1));
|
||||
if (PyErr_Occurred()) return NULL;
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "size must be a (width, height) tuple");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Parse color
|
||||
sf::Color color;
|
||||
auto* mcrfpy_module = PyImport_ImportModule("mcrfpy");
|
||||
if (!mcrfpy_module) return NULL;
|
||||
|
||||
auto* color_type = PyObject_GetAttrString(mcrfpy_module, "Color");
|
||||
Py_DECREF(mcrfpy_module);
|
||||
if (!color_type) return NULL;
|
||||
|
||||
if (PyObject_IsInstance(color_obj, color_type)) {
|
||||
color = ((PyColorObject*)color_obj)->data;
|
||||
} else if (PyTuple_Check(color_obj)) {
|
||||
int r, g, b, a = 255;
|
||||
if (!PyArg_ParseTuple(color_obj, "iii|i", &r, &g, &b, &a)) {
|
||||
Py_DECREF(color_type);
|
||||
return NULL;
|
||||
}
|
||||
color = sf::Color(r, g, b, a);
|
||||
} else {
|
||||
Py_DECREF(color_type);
|
||||
PyErr_SetString(PyExc_TypeError, "color must be a Color object or (r, g, b[, a]) tuple");
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(color_type);
|
||||
|
||||
self->data->fillRect(x, y, width, height, color);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyObject* PyGridLayerAPI::ColorLayer_draw_fov(PyColorLayerObject* self, PyObject* args, PyObject* kwds) {
|
||||
static const char* kwlist[] = {"source", "radius", "fov", "visible", "discovered", "unknown", NULL};
|
||||
PyObject* source_obj;
|
||||
int radius = -1; // -1 means use grid's default
|
||||
PyObject* fov_obj = Py_None;
|
||||
PyObject* visible_obj = nullptr;
|
||||
PyObject* discovered_obj = nullptr;
|
||||
PyObject* unknown_obj = nullptr;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|iOOOO", const_cast<char**>(kwlist),
|
||||
&source_obj, &radius, &fov_obj, &visible_obj, &discovered_obj, &unknown_obj)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!self->data) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "Layer has no data");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!self->grid) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "Layer is not attached to a grid");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Parse source position
|
||||
int source_x, source_y;
|
||||
if (PyTuple_Check(source_obj) && PyTuple_Size(source_obj) == 2) {
|
||||
source_x = PyLong_AsLong(PyTuple_GetItem(source_obj, 0));
|
||||
source_y = PyLong_AsLong(PyTuple_GetItem(source_obj, 1));
|
||||
if (PyErr_Occurred()) return NULL;
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "source must be a (x, y) tuple");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get radius from grid if not specified
|
||||
if (radius < 0) {
|
||||
radius = self->grid->fov_radius;
|
||||
}
|
||||
|
||||
// Get FOV algorithm
|
||||
TCOD_fov_algorithm_t algorithm;
|
||||
bool was_none = false;
|
||||
if (!PyFOV::from_arg(fov_obj, &algorithm, &was_none)) {
|
||||
return NULL;
|
||||
}
|
||||
if (was_none) {
|
||||
algorithm = self->grid->fov_algorithm;
|
||||
}
|
||||
|
||||
// Helper lambda to parse color
|
||||
auto parse_color = [](PyObject* obj, sf::Color& out, const sf::Color& default_val, const char* name) -> bool {
|
||||
if (!obj || obj == Py_None) {
|
||||
out = default_val;
|
||||
return true;
|
||||
}
|
||||
|
||||
auto* mcrfpy_module = PyImport_ImportModule("mcrfpy");
|
||||
if (!mcrfpy_module) return false;
|
||||
|
||||
auto* color_type = PyObject_GetAttrString(mcrfpy_module, "Color");
|
||||
Py_DECREF(mcrfpy_module);
|
||||
if (!color_type) return false;
|
||||
|
||||
if (PyObject_IsInstance(obj, color_type)) {
|
||||
out = ((PyColorObject*)obj)->data;
|
||||
Py_DECREF(color_type);
|
||||
return true;
|
||||
} else if (PyTuple_Check(obj)) {
|
||||
int r, g, b, a = 255;
|
||||
if (!PyArg_ParseTuple(obj, "iii|i", &r, &g, &b, &a)) {
|
||||
Py_DECREF(color_type);
|
||||
return false;
|
||||
}
|
||||
out = sf::Color(r, g, b, a);
|
||||
Py_DECREF(color_type);
|
||||
return true;
|
||||
}
|
||||
|
||||
Py_DECREF(color_type);
|
||||
PyErr_Format(PyExc_TypeError, "%s must be a Color object or (r, g, b[, a]) tuple", name);
|
||||
return false;
|
||||
};
|
||||
|
||||
// Default colors for FOV visualization
|
||||
sf::Color visible_color(255, 255, 200, 64); // Light yellow tint
|
||||
sf::Color discovered_color(128, 128, 128, 128); // Gray
|
||||
sf::Color unknown_color(0, 0, 0, 255); // Black
|
||||
|
||||
if (!parse_color(visible_obj, visible_color, visible_color, "visible")) return NULL;
|
||||
if (!parse_color(discovered_obj, discovered_color, discovered_color, "discovered")) return NULL;
|
||||
if (!parse_color(unknown_obj, unknown_color, unknown_color, "unknown")) return NULL;
|
||||
|
||||
self->data->drawFOV(source_x, source_y, radius, algorithm, visible_color, discovered_color, unknown_color);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyObject* PyGridLayerAPI::ColorLayer_get_z_index(PyColorLayerObject* self, void* closure) {
|
||||
if (!self->data) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "Layer has no data");
|
||||
|
|
@ -556,6 +935,13 @@ PyMethodDef PyGridLayerAPI::TileLayer_methods[] = {
|
|||
"set(x, y, index)\n\nSet the tile index at cell position (x, y). Use -1 for no tile."},
|
||||
{"fill", (PyCFunction)PyGridLayerAPI::TileLayer_fill, METH_VARARGS,
|
||||
"fill(index)\n\nFill the entire layer with the specified tile index."},
|
||||
{"fill_rect", (PyCFunction)PyGridLayerAPI::TileLayer_fill_rect, METH_VARARGS | METH_KEYWORDS,
|
||||
"fill_rect(pos, size, index)\n\n"
|
||||
"Fill a rectangular region with a tile index.\n\n"
|
||||
"Args:\n"
|
||||
" pos (tuple): Top-left corner as (x, y)\n"
|
||||
" size (tuple): Dimensions as (width, height)\n"
|
||||
" index (int): Tile index to fill with (-1 for no tile)"},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
|
|
@ -661,7 +1047,7 @@ PyObject* PyGridLayerAPI::TileLayer_set(PyTileLayerObject* self, PyObject* args)
|
|||
}
|
||||
|
||||
self->data->at(x, y) = index;
|
||||
self->data->markDirty(); // #148 - Mark for re-render
|
||||
self->data->markDirty(x, y); // Mark only the affected chunk
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
|
|
@ -680,6 +1066,48 @@ PyObject* PyGridLayerAPI::TileLayer_fill(PyTileLayerObject* self, PyObject* args
|
|||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyObject* PyGridLayerAPI::TileLayer_fill_rect(PyTileLayerObject* self, PyObject* args, PyObject* kwds) {
|
||||
static const char* kwlist[] = {"pos", "size", "index", NULL};
|
||||
PyObject* pos_obj;
|
||||
PyObject* size_obj;
|
||||
int tile_index;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOi", const_cast<char**>(kwlist),
|
||||
&pos_obj, &size_obj, &tile_index)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!self->data) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "Layer has no data");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Parse pos
|
||||
int x, y;
|
||||
if (PyTuple_Check(pos_obj) && PyTuple_Size(pos_obj) == 2) {
|
||||
x = PyLong_AsLong(PyTuple_GetItem(pos_obj, 0));
|
||||
y = PyLong_AsLong(PyTuple_GetItem(pos_obj, 1));
|
||||
if (PyErr_Occurred()) return NULL;
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "pos must be a (x, y) tuple");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Parse size
|
||||
int width, height;
|
||||
if (PyTuple_Check(size_obj) && PyTuple_Size(size_obj) == 2) {
|
||||
width = PyLong_AsLong(PyTuple_GetItem(size_obj, 0));
|
||||
height = PyLong_AsLong(PyTuple_GetItem(size_obj, 1));
|
||||
if (PyErr_Occurred()) return NULL;
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "size must be a (width, height) tuple");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
self->data->fillRect(x, y, width, height, tile_index);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyObject* PyGridLayerAPI::TileLayer_get_z_index(PyTileLayerObject* self, void* closure) {
|
||||
if (!self->data) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "Layer has no data");
|
||||
|
|
@ -749,7 +1177,7 @@ int PyGridLayerAPI::TileLayer_set_texture(PyTileLayerObject* self, PyObject* val
|
|||
|
||||
if (value == Py_None) {
|
||||
self->data->texture.reset();
|
||||
self->data->markDirty(); // #148 - Mark for re-render
|
||||
self->data->markDirty(); // Mark ALL chunks for re-render (texture change affects all)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -768,7 +1196,7 @@ int PyGridLayerAPI::TileLayer_set_texture(PyTileLayerObject* self, PyObject* val
|
|||
Py_DECREF(texture_type);
|
||||
|
||||
self->data->texture = ((PyTextureObject*)value)->data;
|
||||
self->data->markDirty(); // #148 - Mark for re-render
|
||||
self->data->markDirty(); // Mark ALL chunks for re-render (texture change affects all)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue