Daniele Bartolini 12 лет назад
Родитель
Сommit
dc666301da

+ 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)
 

+ 16 - 0
tools/editors/world-editor/CMakeLists.txt

@@ -0,0 +1,16 @@
+cmake_minimum_required(VERSION 2.8)
+
+find_package(PkgConfig)
+
+pkg_check_modules(GTKMM gtkmm-3.0)
+
+link_directories(${GTKMM_LIBRARY_DIRS} ${CROWN_BINARY_DIR})
+
+include_directories(${GTKMM_INCLUDE_DIRS})
+
+add_executable(world-editor world-editor.cpp CrownDrawingArea.h CrownDrawingArea.cpp)
+
+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)

+ 172 - 0
tools/editors/world-editor/CrownDrawingArea.cpp

@@ -0,0 +1,172 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+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 "CrownDrawingArea.h"
+#include <iostream>
+#include <gdk/gdkx.h>
+#include <cassert>
+#include <cstdio>
+#include <GL/gl.h>
+#include <gtkmm/window.h>
+#include <glibmm.h>
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+CrownDrawingArea::CrownDrawingArea(Device* engine) :
+	Gtk::DrawingArea(),
+	m_engine(engine)
+{
+	Gtk::Widget::set_double_buffered(false);
+
+	Display* dpy = gdk_x11_get_default_xdisplay();
+	int xscreen = DefaultScreen(dpy);
+	GdkScreen* screen = gdk_screen_get_default();
+
+	// Color index buffer not supported - deprecated
+	int32_t fbAttribs[] =
+	{
+		GLX_DOUBLEBUFFER,		static_cast<int32_t>(True),
+		GLX_RED_SIZE,			8,
+		GLX_GREEN_SIZE,			8,
+		GLX_BLUE_SIZE,			8,
+		GLX_ALPHA_SIZE,			8,
+		GLX_DEPTH_SIZE,			24,
+		GLX_STENCIL_SIZE,		0,
+		GLX_ACCUM_RED_SIZE,		0,
+		GLX_ACCUM_GREEN_SIZE,	0,
+		GLX_ACCUM_BLUE_SIZE,	0,
+		GLX_ACCUM_ALPHA_SIZE,	0,
+		GLX_RENDER_TYPE,		static_cast<int32_t>(GLX_RGBA_BIT),
+		GLX_DRAWABLE_TYPE,		static_cast<int32_t>(GLX_WINDOW_BIT),
+		GLX_X_RENDERABLE,		static_cast<int32_t>(True),
+		GLX_CONFIG_CAVEAT,		static_cast<int32_t>(GLX_DONT_CARE),
+		GLX_TRANSPARENT_TYPE,	static_cast<int32_t>(GLX_NONE),
+		static_cast<int32_t>(None)
+	};
+
+	int32_t fbCount;
+	GLXFBConfig* fbConfig = glXChooseFBConfig(dpy, xscreen, fbAttribs, &fbCount);
+
+	if (!fbConfig)
+	{
+		printf("Unable to find a matching FrameBuffer configuration.");
+		return;
+	}
+
+	XVisualInfo* visualInfo = glXGetVisualFromFBConfig(dpy, fbConfig[0]);
+
+	if (!visualInfo)
+	{
+		printf("Unable to find a matching Visual for the FrameBuffer configuration.");
+		XFree(fbConfig);
+		return;
+	}
+ 
+	XVisualInfo* xvisual = visualInfo;
+	GdkVisual* visual = gdk_x11_screen_lookup_visual(screen, xvisual->visualid);
+
+	
+	gtk_widget_set_visual(GTK_WIDGET(this->gobj()), visual);
+
+	m_context = glXCreateContext(dpy, xvisual, NULL, True);
+
+	XFree(visualInfo);
+	XFree(fbConfig);
+	XFlush(dpy);
+
+	m_display = dpy;
+}
+
+//-----------------------------------------------------------------------------
+CrownDrawingArea::~CrownDrawingArea()
+{
+	if (m_context)
+	{
+		if (m_context == glXGetCurrentContext())
+		{
+			glXMakeCurrent(m_display, None, NULL);
+		}
+
+		glXDestroyContext(m_display, m_context);
+	}
+}
+
+//-----------------------------------------------------------------------------
+bool CrownDrawingArea::make_current()
+{
+	Glib::RefPtr<Gdk::Window> gdk_win = get_window();
+
+	return (glXMakeContextCurrent(m_display,
+								  GDK_WINDOW_XID(gdk_win->gobj()),
+								  GDK_WINDOW_XID(gdk_win->gobj()),
+								  m_context) == True);
+}
+
+//-----------------------------------------------------------------------------
+void CrownDrawingArea::swap_buffers()
+{
+	Glib::RefPtr<Gdk::Window> gdk_win = get_window();
+
+	glXSwapBuffers(m_display, GDK_WINDOW_XID(gdk_win->gobj()));
+}
+
+//-----------------------------------------------------------------------------
+void CrownDrawingArea::on_realize()
+{
+	Gtk::DrawingArea::on_realize();
+
+	make_current();
+
+	char* argv[1];
+
+	m_engine->init(0, argv);
+
+	Glib::signal_idle().connect(sigc::mem_fun(*this, &CrownDrawingArea::on_idle));
+}
+
+//-----------------------------------------------------------------------------
+bool CrownDrawingArea::on_draw(const ::Cairo::RefPtr< ::Cairo::Context >& cr)
+{
+	(void)cr;
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+bool CrownDrawingArea::on_idle()
+{
+	make_current();
+
+	m_engine->frame();
+
+	swap_buffers();
+
+	return true;
+}
+
+} // namespace crown
+

+ 67 - 0
tools/editors/world-editor/CrownDrawingArea.h

@@ -0,0 +1,67 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+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 <gtkmm/drawingarea.h>
+#include <X11/Xutil.h>
+#include <X11/Xatom.h>
+#include <X11/Xlib.h>
+#include <GL/glx.h>
+
+#include "Device.h"
+
+namespace crown
+{
+
+// Custom widget to draw with OpenGL
+class CrownDrawingArea : public Gtk::DrawingArea
+{
+public:
+
+				CrownDrawingArea(Device* engine);
+	virtual		~CrownDrawingArea();
+
+	void		on_realize();
+	bool		on_draw(const ::Cairo::RefPtr< ::Cairo::Context >& cr);
+
+	bool		on_idle();
+
+protected:
+
+	Display*	m_display;
+	GLXContext	m_context;
+
+	Device*		m_engine;
+
+private:
+
+	bool		make_current();
+	void		swap_buffers();
+};
+
+} // namespace crown
+

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

@@ -0,0 +1,254 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.0 -->
+  <object class="GtkWindow" id="window1">
+    <property name="can_focus">False</property>
+    <child>
+      <object class="GtkBox" id="box1">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <child>
+          <object class="GtkMenuBar" id="menubar1">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <child>
+              <object class="GtkMenuItem" id="menuitem1">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="label" translatable="yes">_File</property>
+                <property name="use_underline">True</property>
+                <child type="submenu">
+                  <object class="GtkMenu" id="menu1">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <child>
+                      <object class="GtkImageMenuItem" id="imagemenuitem1">
+                        <property name="label">gtk-new</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkImageMenuItem" id="imagemenuitem2">
+                        <property name="label">gtk-open</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkImageMenuItem" id="imagemenuitem3">
+                        <property name="label">gtk-save</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkImageMenuItem" id="imagemenuitem4">
+                        <property name="label">gtk-save-as</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkSeparatorMenuItem" id="separatormenuitem1">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkImageMenuItem" id="imagemenuitem5">
+                        <property name="label">gtk-quit</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                        <signal name="activate" handler="on_quit" swapped="no"/>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkMenuItem" id="menuitem2">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="label" translatable="yes">_Edit</property>
+                <property name="use_underline">True</property>
+                <child type="submenu">
+                  <object class="GtkMenu" id="menu2">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <child>
+                      <object class="GtkImageMenuItem" id="imagemenuitem6">
+                        <property name="label">gtk-cut</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkImageMenuItem" id="imagemenuitem7">
+                        <property name="label">gtk-copy</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkImageMenuItem" id="imagemenuitem8">
+                        <property name="label">gtk-paste</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkImageMenuItem" id="imagemenuitem9">
+                        <property name="label">gtk-delete</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkSeparatorMenuItem" id="separatormenuitem2">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkMenuItem" id="menuitem5">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="label" translatable="yes">Grid size</property>
+                        <property name="use_underline">True</property>
+                        <child type="submenu">
+                          <object class="GtkMenu" id="menu4">
+                            <property name="visible">True</property>
+                            <property name="can_focus">False</property>
+                            <child>
+                              <object class="GtkRadioMenuItem" id="radiomenuitem2">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                                <property name="label" translatable="yes">0.25</property>
+                                <property name="use_underline">True</property>
+                                <property name="draw_as_radio">True</property>
+                              </object>
+                            </child>
+                            <child>
+                              <object class="GtkRadioMenuItem" id="radiomenuitem3">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                                <property name="label" translatable="yes">0.5</property>
+                                <property name="use_underline">True</property>
+                                <property name="draw_as_radio">True</property>
+                                <property name="group">radiomenuitem2</property>
+                              </object>
+                            </child>
+                            <child>
+                              <object class="GtkRadioMenuItem" id="radiomenuitem4">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                                <property name="label" translatable="yes">1</property>
+                                <property name="use_underline">True</property>
+                                <property name="active">True</property>
+                                <property name="draw_as_radio">True</property>
+                                <property name="group">radiomenuitem3</property>
+                              </object>
+                            </child>
+                            <child>
+                              <object class="GtkRadioMenuItem" id="radiomenuitem5">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                                <property name="label" translatable="yes">2</property>
+                                <property name="use_underline">True</property>
+                                <property name="draw_as_radio">True</property>
+                                <property name="group">radiomenuitem4</property>
+                              </object>
+                            </child>
+                          </object>
+                        </child>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkMenuItem" id="menuitem3">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="label" translatable="yes">_View</property>
+                <property name="use_underline">True</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkMenuItem" id="menuitem6">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="label" translatable="yes">_Tools</property>
+                <property name="use_underline">True</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkMenuItem" id="menuitem4">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="label" translatable="yes">_Help</property>
+                <property name="use_underline">True</property>
+                <child type="submenu">
+                  <object class="GtkMenu" id="menu3">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <child>
+                      <object class="GtkImageMenuItem" id="imagemenuitem10">
+                        <property name="label">gtk-about</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkToolbar" id="toolbar1">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>

+ 35 - 0
tools/editors/world-editor/world-editor.cpp

@@ -0,0 +1,35 @@
+#include <gtkmm.h>
+#include "CrownDrawingArea.h"
+#include "Crown.h"
+
+using namespace crown;
+
+int main(int argc, char *argv[])
+{
+	Gtk::Main kit(argc, argv);
+
+	Device* engine = device();
+
+	CrownDrawingArea crown_area(engine);
+	crown_area.set_size_request(800, 500);
+
+	Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("ui/world-editor.glade");
+
+	Gtk::Window* window = NULL;
+	builder->get_widget("window1", window);
+
+	window->set_title("World editor");
+
+	Gtk::Box* box = NULL;
+	builder->get_widget("box1", box);
+
+	box->pack_start(crown_area);
+
+	crown_area.show();
+
+	Gtk::Main::run(*window);
+
+	engine->shutdown();
+
+	return EXIT_SUCCESS;
+}