这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion copying.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ _the openage authors_ are:
| James Hagborg | blucoat | jameshagborg@gmail.com |
| Prashanth Jonnala | jprashanth | prashanth.neo@gmail.com |
| Jonathan Remnant | Jon0 | jono4728@gmail.com |
| Sam Schetterer | schets | samschet@gmail.com |
| Sam Schetterer | sams, schets | samschet@gmail.com |
| Georg Kilzer | leper | leper@wildfiregames.com |

If you're a first-time commiter, add yourself to the above list. This is not
Expand Down
14 changes: 12 additions & 2 deletions cpp/coord/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#ifndef OPENAGE_COORD_TILE_H_
#define OPENAGE_COORD_TILE_H_
#include <functional>

#include "../util/misc.h"
#include "decl.h"

#include "phys2.h"

#define MEMBERS ne, se
Expand Down Expand Up @@ -48,5 +49,14 @@ struct tile_delta {
#undef RELATIVE_TYPE
#undef ABSOLUTE_TYPE
#undef SCALAR_TYPE

namespace std{
template<>
struct hash<openage::coord::tile>{
size_t operator ()(const openage::coord::tile& pos) const{
size_t nehash = hash<openage::coord::tile_t>{}(pos.ne);
size_t sehash = hash<openage::coord::tile_t>{}(pos.se);
return openage::util::rol<size_t, 1>(nehash) ^ sehash;
}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please indent with tabs and place spaces before the block { (in this case after std {, const { and tile> {)

} //namespace std
#endif
2 changes: 1 addition & 1 deletion cpp/game_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ bool GameMain::on_input(SDL_Event *e) {
mousepos_tile.se);

TerrainChunk *chunk = terrain->get_create_chunk(mousepos_tile);
chunk->get_data(mousepos_tile)->terrain_id = editor_current_terrain;
chunk->set_id(mousepos_tile, editor_current_terrain);
}
else if (clicking_active and e->button.button == SDL_BUTTON_RIGHT and !construct_mode and selected_unit) {
TerrainChunk *chunk = terrain->get_chunk(mousepos_tile);
Expand Down
15 changes: 0 additions & 15 deletions cpp/pathfinding/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,6 @@ struct hash<openage::path::Node &> {
return openage::util::rol<size_t, 1>(nehash) ^ sehash;
}
};

/**
* Hash function for tiles
*
* TODO: relocate to coord/
*/
template <>
struct hash<openage::coord::tile> {
size_t operator ()(const openage::coord::tile &tile) const {
size_t nehash = std::hash<openage::coord::tile_t> {}(tile.ne);
size_t sehash = std::hash<openage::coord::tile_t> {}(tile.se);
return openage::util::rol<size_t, 1>(nehash) ^ sehash;
}
};

/**
* Hash function for phys3 coordinates.
*
Expand Down
60 changes: 50 additions & 10 deletions cpp/terrain/terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ bool Terrain::fill(const int *data, coord::tile_delta size) {
}
int terrain_id = data[pos.ne * size.ne + pos.se];
TerrainChunk *chunk = this->get_create_chunk(pos);
chunk->get_data(pos)->terrain_id = terrain_id;
chunk->set_id(pos, terrain_id);
}
}
return was_cut;
Expand All @@ -127,6 +127,11 @@ bool Terrain::fill(const int *data, coord::tile_delta size) {
void Terrain::attach_chunk(TerrainChunk *new_chunk,
coord::chunk position,
bool manually_created) {

//TODO only modify the relevant tile coordinates
this->blend_cache.clear();
this->unblend_cache.clear();

new_chunk->set_terrain(this);
new_chunk->manually_created = manually_created;
log::dbg("inserting new chunk at (%02d,%02d)", position.ne, position.se);
Expand Down Expand Up @@ -179,6 +184,13 @@ TerrainChunk *Terrain::get_create_chunk(coord::tile position) {
return this->get_create_chunk(position.to_chunk());
}

void Terrain::set_id(coord::tile position, int id){
this->remove_cache_pos(position);
if(auto* content = this->get_data(position)){
content->terrain_id = id;
}
}

TileContent *Terrain::get_data(coord::tile position) {
TerrainChunk *c = this->get_chunk(position.to_chunk());
if (c == nullptr) {
Expand Down Expand Up @@ -311,6 +323,18 @@ bool Terrain::check_tile_position(coord::tile pos) {

}

void Terrain::remove_cache_pos(coord::tile pos) {
auto rm_tile = [this](coord::tile tpos){
this->blend_cache.erase(tpos);
this->unblend_cache.erase(tpos);
};

rm_tile(pos);
for(auto offset : neigh_offsets){
rm_tile(pos + offset);
}
}

void Terrain::draw(Engine *engine) {
// TODO: move this draw invokation to a render manager.
// it can reorder the draw instructions and minimize texture switching.
Expand Down Expand Up @@ -414,13 +438,31 @@ struct terrain_render_data Terrain::create_draw_advice(coord::tile ab,
size_t tiles_count = std::abs(cf.ne - gb.ne) * std::abs(cf.se - gb.se);
tiles->reserve(tiles_count);

std::unordered_map<coord::tile, tile_draw_data>* cache;
if(this->blending_enabled){
cache = &blend_cache;
}
else{
cache = &unblend_cache;
}
// sweep the whole rhombus area
for (coord::tile tilepos = gb; tilepos.ne <= (ssize_t) cf.ne; tilepos.ne++) {
for (tilepos.se = gb.se; tilepos.se <= (ssize_t) cf.se; tilepos.se++) {

// get the terrain tile drawing data
auto tile = this->create_tile_advice(tilepos);
tiles->push_back(tile);
auto check = [cache, tilepos, this](){
auto tile_iter = cache->find(tilepos);
if(tile_iter != cache->end()){
return tile_iter->second;
}
else{
auto pair = cache->emplace(tilepos,
this->create_tile_advice(tilepos));
return pair.first->second;
}
};

tiles->push_back(check());

// get the object standing on the tile
// TODO: make the terrain independent of objects standing on it.
Expand Down Expand Up @@ -491,7 +533,6 @@ struct tile_draw_data Terrain::create_tile_advice(coord::tile position) {
// create the draw_masks from the calculated influences
this->calculate_masks(position, &tile, &influence_group);
}

return tile;
}

Expand All @@ -509,7 +550,6 @@ void Terrain::get_neighbors(coord::tile basepos,
// calculate the pos of the neighbor tile
coord::tile neigh_pos = basepos + neigh_offsets[neigh_id];

// get the neighbor data
TileContent *neigh_content = this->get_data(neigh_pos);

// chunk for neighbor or single tile is not existant
Expand Down Expand Up @@ -649,11 +689,11 @@ void Terrain::calculate_masks(coord::tile position,
int adjacent_mask_id = -1;

/* neighbor ids:
0
7 1 => 8 neighbors that can have influence on
6 @ 2 the mask id selection.
5 3
4
0
7 1 => 8 neighbors that can have influence on
6 @ 2 the mask id selection.
5 3
4
*/

// filter adjacent and diagonal influences neighbor_id: 76543210
Expand Down
14 changes: 13 additions & 1 deletion cpp/terrain/terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,17 @@ class Terrain {
bool is_infinite);
~Terrain();

bool blending_enabled; //!< is terrain blending active. increases memory accesses by factor ~8
bool blending_enabled; //!< is terrain blending active.
bool infinite; //!< chunks are automagically created as soon as they are referenced

coord::tile limit_positive, limit_negative; //!< for non-infinite terrains, this is the size limit.
//TODO: non-square shaped terrain bounds

/**
* Sets the id for the given tile and notifies the engine
*/
void set_id(coord::tile pos, int id);

/**
* fill the terrain with given terrain_id values.
* @returns whether the data filled on the terrain was cut because of
Expand Down Expand Up @@ -325,6 +330,11 @@ class Terrain {
*/
struct tile_draw_data create_tile_advice(coord::tile position);

/**
* Informs the terrain that the given tile has been modified
*/
void remove_cache_pos(coord::tile pos);

/**
* gather neighbors of a given base tile.
*
Expand Down Expand Up @@ -363,6 +373,8 @@ class Terrain {
* maps chunk coordinates to chunks.
*/
std::unordered_map<coord::chunk, TerrainChunk *, coord_chunk_hash> chunks;
std::unordered_map<coord::tile, tile_draw_data> blend_cache;
std::unordered_map<coord::tile, tile_draw_data> unblend_cache;

Texture **textures;
Texture **blending_masks;
Expand Down
7 changes: 7 additions & 0 deletions cpp/terrain/terrain_chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ TerrainChunk::~TerrainChunk() {
delete[] this->data;
}

void TerrainChunk::set_id(coord::tile pos, int id){
this->terrain->remove_cache_pos(pos);
if(auto* content = this->get_data(pos)){
content->terrain_id = id;
}
}

TileContent *TerrainChunk::get_data(coord::tile pos) {
return this->get_data(this->tile_position_neigh(pos));
}
Expand Down
5 changes: 5 additions & 0 deletions cpp/terrain/terrain_chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class TerrainChunk {
* the 8 neighbors this chunk has.
*/
chunk_neighbors neighbors;

/**
* Sets the id for the given tile and notifies the engine
*/
void set_id(coord::tile pos, int id);

/**
* draws the terrain chunk on screen.
Expand Down
2 changes: 1 addition & 1 deletion cpp/terrain/terrain_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void TerrainObject::set_ground(int id, int additional) {

size_t tile_pos = chunk->tile_position_neigh(temp_pos);
chunk->get_data(tile_pos)->terrain_id = id;
terrain->remove_cache_pos(temp_pos);
temp_pos.se++;
}
temp_pos.se = this->pos.start.se - additional;
Expand Down Expand Up @@ -203,7 +204,6 @@ tile_range SquareObject::get_range(const coord::phys3 &pos) const {

// TODO temporary hacky solution until openage::coord has been properly fixed.
coord::phys2 draw_pos = result.start.to_phys2();

draw_pos.ne += ((this->size.ne - 1) * coord::settings::phys_per_tile) / 2;
draw_pos.se += ((this->size.se - 1) * coord::settings::phys_per_tile) / 2;

Expand Down