Archive

Archive for May, 2010

embedding Ogre 1.7 into wxWidgets 2.9 cross platform

May 17th, 2010 3 comments

well, its harder than you might think, but i finally found this to be working quite well:

wxutils.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * File:   wxutils.h
 * Author: thomas
 *
 * Created on May 12, 2010, 9:23 AM
 */


#ifndef _WXUTILS_H
#define _WXUTILS_H

#include <wx/wx.h>
#include <string.h>

std::string getWindowHandle(wxWindow *window);

#endif  /* _WXUTILS_H */

wxutils.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "wxutils.h"

#include <iostream>
#include <sstream>

// this is in a separate file to reduce the clutter in the global namespace and their conflicts

#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
# ifdef __WXGTK__
#include <gdk/gdk.h>
#include <gtk/gtk.h> // just this should suffice as it should include gdk.h itself
/* Seems to be needed under Linux */
//#include <wx/gtk/win_gtk.h>
#include "wx/gtk/private/win_gtk.h"
#include <gdk/gdkx.h>
#include <GL/glx.h>

#if  wxCHECK_VERSION(2, 9, 0)
#define piz(wxwin) WX_PIZZA((wxwin)->m_wxwindow)
#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
        GDK_WINDOW_XWINDOW(((GtkWidget*)piz(wxwin))->window) : \
        GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)

#else
#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
        GDK_WINDOW_XWINDOW(GTK_PIZZA((wxwin)->m_wxwindow)->bin_window) : \
        GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)

#endif

#ifdef __WXX11__
#include "wx/x11/privx.h"
#define GetXWindow(wxwin)   ((Window)(wxwin)->GetHandle())
#endif

# endif //__WXGTK__
#endif //OGRE_PLATFORM_LINUX

std::string getWindowHandle(wxWindow *window)
{
#if WIN32
    std::stringstream sstr;
    size_t hWnd = (size_t)window->GetHandle();
    sstr << hWnd;
    std::string result = sstr.str();
    return result;
#else

    // TODO: Someone test this. you might to use "parentWindowHandle" if this
    // does not work.  Ogre 1.2 + Linux + GLX platform wants a string of the
    // format display:screen:window, which has variable types ulong:uint:ulong.
    GtkWidget* widget = window->GetHandle();
    //gtk_widget_set_double_buffered (widget, FALSE);
    gtk_widget_realize( widget );   // Mandatory. Otherwise, a segfault happens.
    //XSync(GDK_WINDOW_XDISPLAY(widget->window), False);
   
    Display* display = GDK_WINDOW_XDISPLAY( widget->window );

   
    //Window wid = GetXWindow(window);
    Window wid = GDK_WINDOW_XWINDOW( widget->window );

    std::stringstream sstr;
    /* Get the right display (DisplayString() returns ":display.screen") */
    std::string displayStr = DisplayString( display );
    displayStr = displayStr.substr( 1, ( displayStr.find( ".", 0 ) - 1 ) );

    int screenNum = DefaultScreen( display );

    /* Put all together */
    printf("using display: %s\n", displayStr.c_str());
    printf("using screen: %d\n", screenNum);
    printf("using window: 0x%x\n", wid);

    // old format:
    sstr << displayStr << ':' << screenNum << ':' << wid;
    std::string result = sstr.str();

    printf("getWindowHandle() = %s\n", result.c_str());
    return result;
#endif
}

how to use (panel_viewport is the widget that should show the 3d content):

1
2
3
4
5
6
...
mWindow = mRoot->initialise(false);
Ogre::NameValuePairList params;
params["externalWindowHandle"] = getWindowHandle(panel_viewport);
mWindow = Ogre::Root::getSingleton().createRenderWindow("OgreRenderWindow", 640, 480, false, &params);
...

works great so far under windows 7 and ubuntu 9.10 with Ogre 1.7 and wxWidgets 2.9

Categories: coding, cpp Tags:

WxWorkspaceView patched

May 17th, 2010 No comments

so, i recently discovered this wxworkspaceview and the pixelization really bothered me, so i patched it away (issue link)

some before/after comparison (compare the borders):

Categories: coding Tags:

interactive graphs in wxWidgets

May 16th, 2010 No comments
Categories: coding Tags:

wxWidgets logging quickstart

May 2nd, 2010 No comments

so, its really easy if you know how ;)

This example inits a file log and logs to std::cout by default.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void MyApp::initLogging()
{
    // log everything
    wxLog::SetLogLevel(wxLOG_Max);
    wxLog::SetVerbose();

    // use stdout always
    wxLog *logger_cout = new wxLogStream(&std::cout);
    wxLog::SetActiveTarget(logger_cout);

    FILE *f = fopen("mylog.txt", "w");
    if(f)
    {
        // and a file log
        wxLog *logger_file = new wxLogStderr(f);
        wxLogChain *logChain = new wxLogChain(logger_file);
    }
    wxLogStatus(wxT("log started"));
}

in your application you can then use

1
wxLog{Status,Verbose,Info,Message,Warning,Error,FatalError}(...)
Categories: cpp Tags: