mirror of
https://github.com/SFML/SFML.git
synced 2024-11-25 12:51:05 +08:00
5978d015eb
git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1151 4e206d99-4929-0410-ac5d-dfc041789085
111 lines
3.4 KiB
C++
111 lines
3.4 KiB
C++
|
|
////////////////////////////////////////////////////////////
|
|
// Headers
|
|
////////////////////////////////////////////////////////////
|
|
#include "wxSFMLCanvas.hpp"
|
|
#include <iostream>
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// 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("datas/wxwidgets/sfml.png");
|
|
mySprite.SetImage(myImage);
|
|
mySprite.SetOrigin(mySprite.GetSize() / 2.f);
|
|
|
|
// 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
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
void OnMouseMove(wxMouseEvent& Event)
|
|
{
|
|
// Make the sprite follow the mouse cursor
|
|
mySprite.SetPosition(ConvertCoords(Event.GetX(), Event.GetY()));
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// 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() :
|
|
wxFrame(NULL, wxID_ANY, wxT("SFML wxWidgets"), wxDefaultPosition, wxSize(440, 280))
|
|
{
|
|
// 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
|
|
MyFrame* MainFrame = new MyFrame;
|
|
MainFrame->Show();
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
IMPLEMENT_APP(MyApplication);
|