2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include "wxSFMLCanvas.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Custom SFML canvas
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
class MyCanvas : public wxSFMLCanvas
|
|
|
|
{
|
|
|
|
public :
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Construct the canvas
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-07-12 06:17:24 +08:00
|
|
|
MyCanvas(wxWindow* parent, wxWindowID id, const wxPoint& position, const wxSize& size, long style = 0) :
|
|
|
|
wxSFMLCanvas(parent, id, position, size, style)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Load an image and assign it to our sprite
|
|
|
|
myImage.LoadFromFile("datas/wxwidgets/sfml.png");
|
|
|
|
mySprite.SetImage(myImage);
|
2009-06-01 23:58:13 +08:00
|
|
|
mySprite.SetOrigin(mySprite.GetSize() / 2.f);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Catch the mouse move event
|
|
|
|
Connect(wxEVT_MOTION, wxMouseEventHandler(MyCanvas::OnMouseMove));
|
|
|
|
}
|
|
|
|
|
|
|
|
private :
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// /see wxSFMLCanvas::OnUpdate
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
virtual void OnUpdate()
|
|
|
|
{
|
|
|
|
// Rotate the sprite
|
|
|
|
if (GetInput().IsMouseButtonDown(sf::Mouse::Left)) mySprite.Rotate( GetFrameTime() * 50);
|
|
|
|
if (GetInput().IsMouseButtonDown(sf::Mouse::Right)) mySprite.Rotate(-GetFrameTime() * 50);
|
|
|
|
|
|
|
|
// Clear the view
|
|
|
|
Clear(sf::Color(0, 128, 128));
|
|
|
|
|
|
|
|
// Display the sprite in the view
|
|
|
|
Draw(mySprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Function called when the mouse cursor moves
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-07-12 06:17:24 +08:00
|
|
|
void OnMouseMove(wxMouseEvent& event)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Make the sprite follow the mouse cursor
|
2009-07-12 06:17:24 +08:00
|
|
|
mySprite.SetPosition(ConvertCoords(event.GetX(), event.GetY()));
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Member data
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
sf::Image myImage; ///< Some image to load...
|
|
|
|
sf::Sprite mySprite; ///< Something to draw...
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Our main window
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
class MyFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public :
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Default constructor : setup the window
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
MyFrame() :
|
2009-06-17 04:17:56 +08:00
|
|
|
wxFrame(NULL, wxID_ANY, wxT("SFML wxWidgets"), wxDefaultPosition, wxSize(440, 280))
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Let's create a SFML view
|
|
|
|
new MyCanvas(this, wxID_ANY, wxPoint(20, 20), wxSize(400, 200));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Our application class
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
class MyApplication : public wxApp
|
|
|
|
{
|
|
|
|
private :
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Initialize the application
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
virtual bool OnInit()
|
|
|
|
{
|
|
|
|
// Create the main window
|
2009-07-12 06:17:24 +08:00
|
|
|
MyFrame* mainFrame = new MyFrame;
|
|
|
|
mainFrame->Show();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_APP(MyApplication);
|