Permalink
Browse files

Restructure a load of stuff to do with layers and tiles. Pass around …

…IDs rather than pointers. Implement layer lowering and fix bugs with layer reorganization
  • Loading branch information...
1 parent cb076bd commit 8e5d5cc1d9893e4215196a9b15b9adc5491c6726 @Kazade committed May 5, 2012
View
@@ -9,6 +9,8 @@ find_package(Boost COMPONENTS system filesystem thread date_time regex REQUIRED)
pkg_check_modules(CURL REQUIRED libcurl)
+pkg_check_modules(GTHREAD REQUIRED "gthread-2.0 >= 2.14.0")
+
pkg_check_modules(GTKMM REQUIRED gtkmm-2.4)
IF(GTKMM_FOUND)
link_directories(${GTKMM_LIBRARY_DIRS})
@@ -34,6 +36,12 @@ include_directories(${CURL_INCLUDE_DIRS})
FILE(GLOB_RECURSE sources platformation/*.cpp)
FILE(GLOB_RECURSE kazbase kazbase/*.cpp)
+FOREACH(item ${kazbase})
+ IF(${item} MATCHES "kazbase/tests.cpp")
+ LIST(REMOVE_ITEM kazbase ${item})
+ ENDIF(${item} MATCHES "kazbase/tests.cpp")
+ENDFOREACH(item)
+
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
set(headers
@@ -55,6 +63,7 @@ add_executable(platformation ${sources} ${kazbase})
target_link_libraries(
platformation
${GTKMM_LIBRARIES}
+ ${GTHREAD_LIBRARIES}
${Boost_FILESYSTEM_LIBRARY}
${Boost_REGEX_LIBRARY}
${Boost_THREAD_LIBRARY}
@@ -8,17 +8,13 @@ LayerRenameAction::LayerRenameAction(Level* level, const std::string& old_name,
}
void LayerRenameAction::do_action() {
- Layer* layer = level_->get_layer_by_name(old_name_);
- assert(layer && "Layer not found");
-
- layer->set_name(new_name_);
+ LayerID layer = level_->layer_by_name(old_name_);
+ level_->set_layer_name(layer, new_name_);
}
void LayerRenameAction::undo_action() {
- Layer* layer = level_->get_layer_by_name(new_name_);
- assert(layer && "Layer not found");
-
- layer->set_name(old_name_);
+ LayerID layer = level_->layer_by_name(new_name_);
+ level_->set_layer_name(layer, old_name_);
}
std::string LayerRenameAction::get_description() {
@@ -14,34 +14,39 @@ SpawnTileInstanceAction::SpawnTileInstanceAction(Level* level, OpenGLTileSelecto
void SpawnTileInstanceAction::do_action() {
//If this is not a redo, get the active layer
- Layer* layer = level_->get_active_layer();
+ LayerID layer = level_->active_layer_id();
//If this is a redo, we will have stored the layer name, so get that
if(!layer_name_.empty()) {
- layer = level_->get_layer_by_name(layer_name_);
+ layer = level_->layer_by_name(layer_name_);
+ //Make sure it's the active layer
+ level_->set_active_layer(layer);
}
- assert(layer);
-
//Store the layer name (on a redo this will be a no-op)
- layer_name_ = layer->get_name(); //For this to work, layer names must be unique, and undo of a layer delete should restore the name
+ layer_name_ = level_->layer_name(layer); //For this to work, layer names must be unique, and undo of a layer delete should restore the name
if(tile_id_ == -1) {
//This is not a redo (because there is no stored id) so use the active one
tile_id_ = selector_->get_active_tile_id();
}
if(tile_id_ != -1) {
+ Tile::ptr tile = selector_->library().tile(tile_id_);
//Finally, spawn the instance
- spawned_instance_ = layer->spawn_tile_instance(tile_id_);
+ spawned_instance_ = level_->spawn_tile_instance(tile);
spawned_instance_->set_position(x_, y_);
}
}
void SpawnTileInstanceAction::undo_action() {
- Layer* layer = level_->get_layer_by_name(layer_name_);
- assert(layer);
- layer->delete_tile_instance(spawned_instance_);
+ /*
+ * Get the layer we are supposed to be deleting an instance from
+ * then make sure it's active and delete the instance
+ */
+ LayerID layer = level_->layer_by_name(layer_name_);
+ level_->set_active_layer(layer);
+ level_->delete_tile_instance(spawned_instance_);
//Clear the spawned instance
spawned_instance_ = NULL;
@@ -70,22 +70,16 @@ void EditorView::do_render()
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
- for(uint32_t i = 0; i < level_->get_layer_count(); ++i) {
- Layer* layer = level_->get_layer_at(i);
+ for(uint32_t i = 0; i < level_->layer_count(); ++i) {
+ LayerID layer = level_->layer_by_index(i);
- if(layer != level_->get_active_layer()) {
+ if(layer != level_->active_layer_id()) {
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
} else {
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
- Layer::TileListIteratorPair iters = layer->get_iterators();
-
- for(; iters.first != iters.second; iters.first++) {
- Object* obj = (*iters.first).get();
- glBindTexture(GL_TEXTURE_2D, get_texture_for_object(obj));
- obj->render_geometry();
- }
+ level_->render_layer(*this, layer);
}
if(active_object_) {
@@ -219,9 +213,8 @@ void EditorView::do_button_press(GdkEventButton* event)
gfloat x = event->x;
gfloat y = event->y;
- Layer* layer = level_->get_active_layer();
-
- Layer::TileListIteratorPair iterators = layer->get_iterators();
+ Layer& layer = level_->active_layer();
+ Layer::TileListIteratorPair iterators = layer.get_iterators();
Object::ptr t = picker_->pick(x, y, iterators.first, iterators.second);
if(t) {
@@ -258,15 +251,15 @@ void EditorView::do_button_press(GdkEventButton* event)
gfloat x = event->x;
gfloat y = event->y;
- Layer* layer = level_->get_active_layer();
- Level::TileListIteratorPair iterators = layer->get_iterators();
+ Layer& layer = level_->active_layer();
+ Layer::TileListIteratorPair iterators = layer.get_iterators();
Object::ptr t = picker_->pick(x, y, iterators.first, iterators.second);
if(t) {
TileInstance* ti = dynamic_cast<TileInstance*>(t.get());
if(ti) {
- layer->delete_tile_instance(ti);
+ level_->delete_tile_instance(ti);
active_object_ = NULL;
}
@@ -308,8 +301,8 @@ GLuint EditorView::get_texture_for_object(Object* obj)
}
if(TileInstance* ti = dynamic_cast<TileInstance*>(obj)) {
- Tile* t = ti->get_tile();
- assert(t);
+ TileID tile_id = ti->tile_id();
+ Tile* t = &level_->tile(tile_id);
GLuint new_tex;
glGenTextures(1, &new_tex);
@@ -32,6 +32,7 @@
class Level;
class Object;
class MainWindow;
+class OpenGLTileSelector;
class EditorView : public OpenGLWidget {
public:
@@ -41,6 +42,9 @@ class EditorView : public OpenGLWidget {
void set_level(Level* level);
void set_tile_selector(OpenGLTileSelector* selector);
+
+ GLuint get_texture_for_object(Object* obj);
+
private:
MainWindow* parent_;
@@ -57,9 +61,6 @@ class EditorView : public OpenGLWidget {
OpenGLPicker<Object::ptr>::ptr picker_;
Object* active_object_;
Glib::Timer active_timer_;
-
- GLuint get_texture_for_object(Object* obj);
-
std::map<Object*, GLuint> texture_ids_;
OpenGLTileSelector* tile_selector_;
@@ -17,29 +17,31 @@
#include "layer.h"
+#include "level.h"
using namespace std;
-Layer::Layer(Tileset::ptr tileset):
-tileset_(tileset),
-name_("Untitled") {
+Layer::Layer(Level* parent, const std::string& name):
+id_(Layer::generate_layer_id()),
+level_(parent),
+name_(name) {
}
-TileInstance* Layer::spawn_tile_instance(int tile_id, bool select) {
- TileInstance::ptr new_tile_instance(new TileInstance(tileset_, tile_id));
+TileInstance* Layer::spawn_tile_instance(TileID tile_id, bool select) {
+ TileInstance::ptr new_tile_instance(new TileInstance(this, tile_id));
tile_instances_.push_back(new_tile_instance);
signal_changed_();
return new_tile_instance.get();
}
-int Layer::get_tile_instance_count() const {
+uint32_t Layer::get_tile_instance_count() const {
return tile_instances_.size();
}
-TileInstance* Layer::get_tile_instance_at(int i) const {
+TileInstance* Layer::get_tile_instance_at(uint32_t i) const {
assert(0);
return NULL;
}
@@ -57,7 +59,6 @@ void Layer::delete_tile_instance(TileInstance* instance) {
if(iters.first == iters.second) {
return;
}
-
tile_instances_.erase(iters.first);
signal_changed_();
@@ -68,6 +69,6 @@ void Layer::set_name(const std::string& name) {
signal_changed_();
}
-string Layer::get_name() const {
+string Layer::name() const {
return name_;
}
View
@@ -31,6 +31,8 @@
#include "tileset.h"
#include "tile_instance.h"
+
+class Level;
class Layer {
public:
@@ -39,27 +41,36 @@ class Layer {
typedef std::list<TileInstance::ptr> TileList;
typedef std::pair<TileList::iterator, TileList::iterator> TileListIteratorPair;
- Layer(Tileset::ptr tileset);
- TileInstance* spawn_tile_instance(int tile_id, bool select=true);
+ Layer(Level* parent, const std::string& name);
+ TileInstance* spawn_tile_instance(TileID tile_id, bool select=true);
- int get_tile_instance_count() const;
- TileInstance* get_tile_instance_at(int i) const;
+ uint32_t get_tile_instance_count() const;
+ TileInstance* get_tile_instance_at(uint32_t i) const;
void delete_tile_instance(TileInstance* instance);
TileListIteratorPair get_iterators() {
return std::make_pair(tile_instances_.begin(), tile_instances_.end());
}
void set_name(const std::string& name);
- std::string get_name() const;
+ std::string name() const;
sigc::signal<void>& signal_changed() { return signal_changed_; }
+ Level& level() { return *level_; }
+
+ static LayerID generate_layer_id() {
+ static LayerID counter = 0;
+ return ++counter;
+ }
+
+ LayerID id() const { return id_; }
private:
- TileList tile_instances_;
- Tileset::ptr tileset_;
+ LayerID id_;
+ Level* level_;
std::string name_;
-
+
+ TileList tile_instances_;
sigc::signal<void> signal_changed_;
};
@@ -59,6 +59,7 @@ LayerManager::LayerManager(MainWindow* parent, Gtk::TreeView* view, Gtk::Button*
gtk_rename_menu_item_->signal_activate().connect(sigc::mem_fun(this, &LayerManager::on_layer_rename));
gtk_move_up_menu_item_->signal_activate().connect(sigc::mem_fun(this, &LayerManager::on_layer_raise));
+ gtk_move_down_menu_item_->signal_activate().connect(sigc::mem_fun(this, &LayerManager::on_layer_lower));
//TODO: Connect the other menu signals
gtk_layer_menu_->add(*gtk_rename_menu_item_);
@@ -77,8 +78,13 @@ bool LayerManager::on_layer_popup(GdkEventButton* event) {
return false;
}
+void LayerManager::on_layer_lower() {
+ level_->lower_layer(level_->active_layer_id());
+ update_list_view();
+}
+
void LayerManager::on_layer_raise() {
- level_->raise_layer(level_->get_active_layer());
+ level_->raise_layer(level_->active_layer_id());
update_list_view();
}
@@ -94,7 +100,7 @@ void LayerManager::on_layer_rename() {
Level* level = get_main_window()->get_level();
assert(level);
- std::string old_name = level->get_active_layer()->get_name();
+ std::string old_name = level->layer_name(level_->active_layer_id());
Action::ptr rename_action(new LayerRenameAction(level, old_name, new_name));
rename_action->do_action();
@@ -119,29 +125,28 @@ void LayerManager::set_level(Level* level) {
level_->layer_created().connect(bind(&LayerManager::on_layer_created, this, _1));
level_->layer_destroyed().connect(bind(&LayerManager::on_layer_destroyed, this, _1));
- //Call the callback manually on any already existing layers
- for(uint32_t i = 0; i < level_->get_layer_count(); ++i) {
- on_layer_created(level_->get_layer_at(i));
- }
+ level_->trigger_layer_created_on_all_layers();
}
}
void LayerManager::update_list_view() {
+ L_DEBUG("Updating layer list view");
+
if(!level_) {
return;
}
tree_model_->clear();
- for(int32_t i = (int32_t) level_->get_layer_count() - 1; i >= 0; --i) {
- Layer* l = level_->get_layer_at(i);
+ for(int32_t i = (int32_t) level_->layer_count() - 1; i >= 0; --i) {
+ LayerID layer_id = level_->layer_by_index(i);
Gtk::TreeModel::Row row = *(tree_model_->append());
row[columns_.column_id_] = i;
- row[columns_.column_name_] = l->get_name();
+ row[columns_.column_name_] = level_->layer_name(layer_id);
//If this is the active layer, display a tick next to it
- if(l == level_->get_active_layer()) {
+ if(layer_id == level_->active_layer_id()) {
row[columns_.checked_] = view_->render_icon(Gtk::Stock::YES, Gtk::ICON_SIZE_MENU);
} else {
row[columns_.checked_] = Glib::RefPtr<Gdk::Pixbuf>();
@@ -160,7 +165,7 @@ void LayerManager::on_layer_manager_row_activate() {
if(iter) {
Gtk::TreeModel::Row row = *iter;
uint32_t active_layer = row[columns_.column_id_];
- level_->set_active_layer(active_layer);
+ level_->set_active_layer_by_index(active_layer);
}
/*
@@ -185,7 +190,7 @@ void LayerManager::on_add_layer_clicked() {
void LayerManager::on_delete_layer_clicked() {
if(level_) {
- level_->destroy_layer(level_->get_active_layer());
+ level_->destroy_layer(level_->active_layer_id());
}
}
Oops, something went wrong. Retry.

0 comments on commit 8e5d5cc

Please sign in to comment.