Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Integrating with wxWidgets issues  (Read 3189 times)

0 Members and 1 Guest are viewing this topic.

zhinchliffe

  • Newbie
  • *
  • Posts: 18
    • View Profile
Integrating with wxWidgets issues
« on: March 08, 2009, 09:48:54 pm »
i'm attempting to compile a _slightly_ modified wxwidgets-SFML example, and it compiles, but the SFML canvas is invisible... I modified it to get the "quit" menu bar function working under OS X 10.5, but that's it. it was invisible before I modified it, as well, so that's not the issue.

main.cpp

Code: [Select]
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////

#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <sstream>
#include <cstring>
#include <stdio.h>
#include <unistd.h>
#include <fstream>
#include "wx/wx.h"
#include "wxSFMLCanvas.hpp"


////////////////////////////////////////////////////////////
/// Custom SFML canvas
////////////////////////////////////////////////////////////
class MyCanvas : public wxSFMLCanvas
{
public :

////////////////////////////////////////////////////////////
/// Construct the canvas
///
////////////////////////////////////////////////////////////
MyCanvas(wxWindow* Parent, wxWindowID Id, const wxPoint& Position, const wxSize& Size, long Style = 0) :
wxSFMLCanvas(Parent, Id, Position, Size, Style)
{
// Load an image and assign it to our sprite
myImage.LoadFromFile("grid.png");
mySprite.SetImage(myImage);
}

private :

////////////////////////////////////////////////////////////
/// /see wxSFMLCanvas::OnUpdate
///
////////////////////////////////////////////////////////////
virtual void OnUpdate()
{
Clear(sf::Color(255, 0, 0));
}

////////////////////////////////////////////////////////////
/// Member data
////////////////////////////////////////////////////////////
sf::Image  myImage;  ///< Some image to load...
sf::Sprite mySprite; ///< Something to draw...
};


////////////////////////////////////////////////////////////
/// Our main window
////////////////////////////////////////////////////////////
class MyFrame : public wxFrame
{
public :

void MyFrame::OnMenuFileQuit(wxCommandEvent &event);

void OnMenuFileOpen(wxCommandEvent &event);
void OnMenuFileSave(wxCommandEvent &event);

////////////////////////////////////////////////////////////
/// Default constructor : setup the window
///
////////////////////////////////////////////////////////////
MyFrame() :
wxFrame(NULL, wxNewId(), wxString::FromAscii("Yeah!Edit"), wxDefaultPosition, wxSize(800, 600))
{
ThisIsExitID = 4269;
// Let's create our SFML view

// status bar
CreateStatusBar(3);
SetStatusText(wxT("Ready"), 0);;

// menu bar
m_pMenuBar = new wxMenuBar();
// File Menu
m_pFileMenu = new wxMenu();
m_pFileMenu->Append(wxID_OPEN, _T("&Open"), _T("Opens an existing file"));
m_pFileMenu->Append(wxID_SAVE, _T("&Save"), _T("Save the content"));
m_pFileMenu->AppendSeparator();
m_pFileMenu->Append(ThisIsExitID, _T("&Quit"), _T("Quit the application"));
m_pMenuBar->Append(m_pFileMenu, _T("&File"));
// About menu
m_pHelpMenu = new wxMenu();
m_pHelpMenu->Append(wxID_ABOUT, _T("&About"), _T("Shows information about the application"));
m_pMenuBar->Append(m_pHelpMenu, _T("&Help"));

SetMenuBar(m_pMenuBar);

new MyCanvas(this, wxID_ANY, wxPoint(50, 50), wxSize(700, 500));

}

protected:

DECLARE_EVENT_TABLE()

private :
wxTextCtrl *m_pTextCtrl;
wxMenuBar *m_pMenuBar;
wxMenu *m_pFileMenu;
wxMenu *m_pHelpMenu;
int ThisIsExitID;
};

// If you're doing an application by inheriting from wxApp
// be sure to change wxFrame to wxApp (or whatever component
// you've inherited your class from).
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_OPEN, MyFrame::OnMenuFileOpen)
EVT_MENU(wxID_SAVE, MyFrame::OnMenuFileSave)
EVT_MENU(4269, MyFrame::OnMenuFileQuit)
END_EVENT_TABLE()

void MyFrame::OnMenuFileOpen(wxCommandEvent &event)
{
// ...
}

void MyFrame::OnMenuFileSave(wxCommandEvent &event)
{
// ...
}

void MyFrame::OnMenuFileQuit(wxCommandEvent &event)
{
Close(false);
}


////////////////////////////////////////////////////////////
/// Our application class
////////////////////////////////////////////////////////////
class MyApplication : public wxApp
{
private :

////////////////////////////////////////////////////////////
/// Initialize the application
///
////////////////////////////////////////////////////////////
virtual bool OnInit()
{
// Create the main window
MyFrame* MainFrame = new MyFrame;
MainFrame->Show();

return true;
}
};

IMPLEMENT_APP(MyApplication);


wxSFMLCanvas.hpp
Code: [Select]
#ifndef WXSFMLCANVAS_HPP
#define WXSFMLCANVAS_HPP

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <wx/wx.h>


////////////////////////////////////////////////////////////
/// wxSFMLCanvas allows to run SFML in a wxWidgets control
////////////////////////////////////////////////////////////
class wxSFMLCanvas : public wxControl, public sf::RenderWindow
{
public :

    ////////////////////////////////////////////////////////////
    /// Construct the wxSFMLCanvas
    ///
    /// \param Parent :   Parent of the control (NULL by default)
    /// \param Id :       Identifier of the control (-1 by default)
    /// \param Position : Position of the control (wxDefaultPosition by default)
    /// \param Size :     Size of the control (wxDefaultSize by default)
    /// \param Style :    Style of the control (0 by default)
    ///
    ////////////////////////////////////////////////////////////
    wxSFMLCanvas(wxWindow* Parent = NULL, wxWindowID Id = -1, const wxPoint& Position = wxDefaultPosition, const wxSize& Size = wxDefaultSize, long Style = 0);

    ////////////////////////////////////////////////////////////
    /// Destructor
    ///
    ////////////////////////////////////////////////////////////
    virtual ~wxSFMLCanvas();

private :

    DECLARE_EVENT_TABLE()

    ////////////////////////////////////////////////////////////
    /// Notification for the derived class that moment is good
    /// for doing its update and drawing stuff
    ///
    ////////////////////////////////////////////////////////////
    virtual void OnUpdate();

    ////////////////////////////////////////////////////////////
    /// Called when the window is idle - we can refresh our
    /// SFML window
    ///
    ////////////////////////////////////////////////////////////
    void OnIdle(wxIdleEvent&);

    ////////////////////////////////////////////////////////////
    /// Called when the window is repainted - we can display our
    /// SFML window
    ///
    ////////////////////////////////////////////////////////////
    void OnPaint(wxPaintEvent&);

    ////////////////////////////////////////////////////////////
    /// Called when the window needs to draw its background
    ///
    ////////////////////////////////////////////////////////////
    void OnEraseBackground(wxEraseEvent&);
};


#endif // WXSFMLCANVAS_HPP


wxSFMLCanvas.cpp
Code: [Select]
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "wxSFMLCanvas.hpp"

// Platform-specific includes
#ifdef __WXGTK__
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <wx/gtk/win_gtk.h>
#endif


////////////////////////////////////////////////////////////
// Event table
////////////////////////////////////////////////////////////
BEGIN_EVENT_TABLE(wxSFMLCanvas, wxControl)
EVT_IDLE(wxSFMLCanvas::OnIdle)
EVT_PAINT(wxSFMLCanvas::OnPaint)
EVT_ERASE_BACKGROUND(wxSFMLCanvas::OnEraseBackground)
END_EVENT_TABLE()


////////////////////////////////////////////////////////////
/// Construct the wxSFMLCanvas
////////////////////////////////////////////////////////////
wxSFMLCanvas::wxSFMLCanvas(wxWindow* Parent, wxWindowID Id, const wxPoint& Position, const wxSize& Size, long Style) :
wxControl(Parent, Id, Position, Size, Style)
{
#ifdef __WXGTK__

// GTK implementation requires to go deeper to find the low-level X11 identifier of the widget
gtk_widget_realize(m_wxwindow);
gtk_widget_set_double_buffered(m_wxwindow, false);
GdkWindow* Win = GTK_PIZZA(m_wxwindow)->bin_window;
XFlush(GDK_WINDOW_XDISPLAY(Win));
sf::RenderWindow::Create(GDK_WINDOW_XWINDOW(Win));

#else

// Tested under Windows XP only (should work with X11 and other Windows versions - no idea about MacOS)
sf::RenderWindow::Create(GetHandle());
   
#endif
}


////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
wxSFMLCanvas::~wxSFMLCanvas()
{
    // Nothing to do...
}


////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing its update and drawing stuff
////////////////////////////////////////////////////////////
void wxSFMLCanvas::OnUpdate()
{
    // Nothing to do by default...
}


////////////////////////////////////////////////////////////
/// Called when the control is idle - we can refresh our
/// SFML window
////////////////////////////////////////////////////////////
void wxSFMLCanvas::OnIdle(wxIdleEvent&)
{
    // Send a paint message when the control is idle, to ensure maximum framerate
    Refresh();
}


////////////////////////////////////////////////////////////
/// Called when the control is repainted - we can display our
/// SFML window
////////////////////////////////////////////////////////////
void wxSFMLCanvas::OnPaint(wxPaintEvent&)
{
    // Make sure the control is able to be repainted
    wxPaintDC Dc(this);

    // Let the derived class do its specific stuff
    OnUpdate();

    // Display on screen
    Display();
}


////////////////////////////////////////////////////////////
/// Called when the control needs to draw its background
////////////////////////////////////////////////////////////
void wxSFMLCanvas::OnEraseBackground(wxEraseEvent&)
{
    // Don't do anything. We intercept this event in order to prevent the
    // parent class to draw the background before repainting the window,
    // which would cause some flickering
}

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Integrating with wxWidgets issues
« Reply #1 on: March 09, 2009, 05:32:03 am »
Qt + SFML on Mac OS X won't work : the creation of a SFML window from a Cocoa one is not supported (working on it).

Quote from: "Release Notes"
What has not been implemented yet in the Mac OS X port :
    - the use of SFML windows in a Cocoa based application
Want to play movies in your SFML application? Check out sfeMovie!