File: Main.cpp

package info (click to toggle)
libsfml 1.6%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,352 kB
  • sloc: cpp: 13,164; ansic: 3,963; makefile: 483
file content (111 lines) | stat: -rw-r--r-- 3,525 bytes parent folder | download | duplicates (2)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

////////////////////////////////////////////////////////////
// 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.SetCenter(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.SetX(Event.GetX());
        mySprite.SetY(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);