Ver Fonte

Add heightfield to world-editor

Daniele Bartolini há 12 anos atrás
pai
commit
ae75dccf63

+ 1 - 1
tools/CMakeLists.txt

@@ -10,7 +10,7 @@ link_directories(${CROWN_BINARY_DIR} ${GTKMM_LIBRARY_DIRS})
 include_directories(${INCLUDES} ${GTKMM_INCLUDE_DIRS})
 
 add_subdirectory(compilers)
-#add_subdirectory(editors/world-editor)
+add_subdirectory(editors/world-editor)
 add_subdirectory(editors/resource-browser)
 add_subdirectory(pycrown)
 

+ 37 - 4
tools/editors/world-editor/CMakeLists.txt

@@ -6,11 +6,44 @@ pkg_check_modules(GTKMM gtkmm-3.0)
 
 link_directories(${GTKMM_LIBRARY_DIRS} ${CROWN_BINARY_DIR})
 
-include_directories(${GTKMM_INCLUDE_DIRS})
+set (INCLUDES
+	terrain
+)
 
-add_executable(world-editor world-editor.cpp CrownDrawingArea.h CrownDrawingArea.cpp)
+set (SRC
+	world-editor.cpp
+	CrownDrawingArea.cpp
+)
+
+set (HEADERS
+	CrownDrawingArea.h
+)
+
+set (TERRAIN_SRC
+	terrain/Heightfield.cpp
+)
+
+set (TERRAIN_HEADERS
+	terrain/Heightfield.h
+)
+
+set (WORLD_EDITOR_SRC
+	${SRC}
+
+	${TERRAIN_SRC}
+)
+
+set (WORLD_EDITOR_HEADERS
+	${HEADERS}
+
+	${TERRAIN_HEADERS}
+)
+
+include_directories(${GTKMM_INCLUDE_DIRS} ${INCLUDES})
+
+add_executable(world-editor ${WORLD_EDITOR_SRC} ${WORLD_EDITOR_HEADERS})
 
 target_link_libraries(world-editor ${GTKMM_LIBRARIES} X11 GL crown)
 
-install (TARGETS world-editor DESTINATION bin)
-install (FILES ui/world-editor.glade DESTINATION ${CMAKE_INSTALL_PREFIX}/bin/ui)
+install (TARGETS world-editor DESTINATION bin/world-editor)
+install (FILES ui/world-editor.glade DESTINATION ${CMAKE_INSTALL_PREFIX}/bin/world-editor/ui)

+ 2 - 2
tools/editors/world-editor/CrownDrawingArea.cpp

@@ -142,9 +142,9 @@ void CrownDrawingArea::on_realize()
 
 	make_current();
 
-	char* argv[1];
+	char* argv[] = {"crown", "--dev" };
 
-	m_engine->init(0, argv);
+	m_engine->init(2, argv);
 
 	Glib::signal_idle().connect(sigc::mem_fun(*this, &CrownDrawingArea::on_idle));
 }

+ 175 - 0
tools/editors/world-editor/terrain/Heightfield.cpp

@@ -0,0 +1,175 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "Heightfield.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+Heightfield::Heightfield() :
+	m_initial_width(0),
+	m_initial_height(0),
+	m_initial_meters_per_tile(0),
+	m_initial_altitude(0.0f),
+	m_initial_min_altitude(0.0f),
+	m_initial_max_altitude(0.0f),
+
+	m_width(0),
+	m_height(0),
+	m_meters_per_tile(0),
+
+	m_min_altitude(0.0f),
+	m_max_altitude(0.0f),
+
+	m_altitudes(NULL)
+{
+}
+
+//-----------------------------------------------------------------------------
+Heightfield::~Heightfield()
+{
+	if (m_altitudes != NULL)
+	{
+		m_allocator.deallocate(m_altitudes);
+	}
+}
+
+//-----------------------------------------------------------------------------
+void Heightfield::recreate(uint32_t width, uint32_t height, uint32_t meters_per_tile, float initial_altitude, float min_altitude, float max_altitude)
+{
+	// Recreate the heightfield if already existent
+	if (m_altitudes != NULL)
+	{
+		m_allocator.deallocate(m_altitudes);
+		m_altitudes = NULL;
+	}
+
+	m_initial_width = width;
+	m_initial_height = height;
+	m_initial_meters_per_tile = meters_per_tile;
+	m_initial_altitude = initial_altitude;
+	m_initial_min_altitude = min_altitude;
+	m_initial_max_altitude = max_altitude;
+
+	m_width = width;
+	m_height = height;
+	m_meters_per_tile = meters_per_tile;
+	m_min_altitude = min_altitude;
+	m_max_altitude = max_altitude;
+
+	m_altitudes = (float*)m_allocator.allocate(width * height * sizeof(float));
+
+	set_altitudes(m_initial_altitude);
+}
+
+//-----------------------------------------------------------------------------
+void Heightfield::clear()
+{
+	m_width = m_initial_width;
+	m_height = m_initial_height;
+	m_meters_per_tile = m_initial_meters_per_tile;
+	m_min_altitude = m_initial_min_altitude;
+	m_max_altitude = m_initial_max_altitude;
+
+	set_altitudes(m_initial_altitude);
+}
+
+//-----------------------------------------------------------------------------
+uint32_t Heightfield::width() const
+{
+	return m_width;
+}
+
+//-----------------------------------------------------------------------------
+uint32_t Heightfield::height() const
+{
+	return m_height;
+}
+
+//-----------------------------------------------------------------------------
+float Heightfield::min_altitude() const
+{
+	return m_min_altitude;
+}
+
+//-----------------------------------------------------------------------------
+float Heightfield::max_altitude() const
+{
+	return m_max_altitude;
+}
+
+//-----------------------------------------------------------------------------
+void Heightfield::set_min_altitude(float min)
+{
+	m_min_altitude = min;
+}
+
+//-----------------------------------------------------------------------------
+void Heightfield::set_max_altitude(float max)
+{
+	m_max_altitude = max;
+}
+
+//-----------------------------------------------------------------------------
+float Heightfield::altitude(uint32_t x, uint32_t y) const
+{
+	assert(x < m_width);
+	assert(y < m_height);
+
+	return m_altitudes[coords_to_index(x, y)];
+}
+
+//-----------------------------------------------------------------------------
+void Heightfield::set_altitude(uint32_t x, uint32_t y, float altitude)
+{
+	const uint32_t adjusted_altitude = (altitude < m_min_altitude) ? m_min_altitude : (altitude > m_max_altitude) ? m_max_altitude : altitude;
+
+	m_altitudes[coords_to_index(x, y)] = adjusted_altitude;
+}
+
+//-----------------------------------------------------------------------------
+void Heightfield::set_altitudes(float altitude)
+{
+	for (uint32_t w = 0; w < m_width; w++)
+	{
+		for (uint32_t h = 0; h < m_height; h++)
+		{
+			set_altitude(w, h, altitude);
+		}
+	}
+}
+
+//-----------------------------------------------------------------------------
+uint32_t Heightfield::coords_to_index(uint32_t x, uint32_t y) const
+{
+	assert(x < m_width);
+	assert(y < m_height);
+
+	return m_width * y + x;
+}
+
+} // namespace crown
+

+ 94 - 0
tools/editors/world-editor/terrain/Heightfield.h

@@ -0,0 +1,94 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "MallocAllocator.h"
+
+namespace crown
+{
+
+/// Represents a heightfiled.
+class Heightfield
+{
+public:
+
+				Heightfield();
+				~Heightfield();
+
+	/// (Re)Creates the heightfield of sizes @width by @height.
+	/// The param @meters_per_tile indicates how many meters should be mapped per tile unit. (Higher values
+	///	means better quality at same sizes. You can also specify an @initial_height for all the tiles of the heightmap.
+	void		recreate(uint32_t width, uint32_t height, uint32_t meters_per_tile, float initial_altitude, float min_altitude, float max_altitude);
+
+	/// Clears the content of the heightfield, switching it to the initial parameters passed to @recreate().
+	void		clear();
+
+	uint32_t	width() const;
+	uint32_t	height() const;
+
+	float		min_altitude() const;
+	float		max_altitude() const;
+
+	void		set_min_altitude(float min);
+	void		set_max_altitude(float max);
+
+	/// Returns the altitude value for the tile at @x and @y coordinates.
+	float		altitude(uint32_t x, uint32_t y) const;
+
+	/// Sets the @height value for the tile @x and @y coordinates.
+	void		set_altitude(uint32_t x, uint32_t y, float altitude);
+
+	/// Sets the @height value for all the tiles.
+	void		set_altitudes(float altitude);
+
+private:
+
+	uint32_t	coords_to_index(uint32_t x, uint32_t y) const;
+
+private:
+
+	MallocAllocator	m_allocator;
+
+	uint32_t		m_initial_width;
+	uint32_t		m_initial_height;
+	uint32_t		m_initial_meters_per_tile;
+	float			m_initial_altitude;
+	float			m_initial_min_altitude;
+	float			m_initial_max_altitude;
+
+	uint32_t		m_width;
+	uint32_t		m_height;
+	uint32_t		m_meters_per_tile;
+	float			m_min_altitude;
+	float			m_max_altitude;
+
+	// A buffer of heights of width by height.
+	float*			m_altitudes;
+};
+
+} // namespace crown
+

+ 22 - 0
tools/editors/world-editor/ui/world-editor.glade

@@ -202,6 +202,28 @@
                 <property name="can_focus">False</property>
                 <property name="label" translatable="yes">_Tools</property>
                 <property name="use_underline">True</property>
+                <child type="submenu">
+                  <object class="GtkMenu" id="menu5">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <child>
+                      <object class="GtkMenuItem" id="menuitem7">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="label" translatable="yes">Resource browser</property>
+                        <property name="use_underline">True</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkMenuItem" id="menuitem8">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="label" translatable="yes">Terrain</property>
+                        <property name="use_underline">True</property>
+                      </object>
+                    </child>
+                  </object>
+                </child>
               </object>
             </child>
             <child>