簡體   English   中英

將 wxWidgets 與 SFML 一起使用時出錯

[英]Error with using wxWidgets alongside SFML

這個錯誤是 wxWidgets 3.1.5 和 SFML 2.5.1 我有以下代碼:

(main.cpp) :-

#include <SFML/Graphics.hpp>
#include <wx/wx.h>
#include "wxSfmlCanvas.h"
#include "main.h"


TestFrame::TestFrame() :
    wxFrame(NULL, wxID_ANY, "SFML 2.5 w/ wxWidgets 3.1", wxDefaultPosition, wxSize(650, 490))
{
    mCanvas = new TestSfmlCanvas(this, wxID_ANY, wxPoint(5, 25), wxSize(640, 480));

    // Also add a button.
     wxButton *button = new wxButton(
            this, 
            wxID_ANY, 
            wxT("Toggle Size"),
            wxPoint(5, 5)
        );
     button->Bind(wxEVT_BUTTON, [&](wxCommandEvent& arg) -> void {
         mCanvas->toggleSize();
     });

    

    wxBoxSizer* mainSizer =  new wxBoxSizer( wxVERTICAL );
    
    mainSizer->Add(mCanvas, 6, wxALIGN_TOP | wxEXPAND);
    mainSizer->Add(button, 0, wxALIGN_RIGHT | wxALIGN_BOTTOM);
    SetSizerAndFit(mainSizer);
}

TestSfmlCanvas::TestSfmlCanvas(
        wxWindow*  Parent,
        wxWindowID Id,
        wxPoint&   Position,
        wxSize&    Size,
        long       Style
    ) : wxSfmlCanvas(Parent, Id, Position, Size, Style),
        mLarge(false)
{
    // Load a texture and create a sprite.
    mTexture.loadFromFile("data/ball.png");
    mSprite = std::make_unique<sf::Sprite>(mTexture);
}

void 
TestSfmlCanvas::OnUpdate()
{
    clear(sf::Color(64, 196, 196));
    draw(*mSprite);
}

void
TestSfmlCanvas::toggleSize()
{
    if (mLarge) {
        mSprite->setScale(sf::Vector2f(1.2f, 1.2f));
    } 
    else {
        mSprite->setScale(sf::Vector2f(0.5f, 0.5f));
    }

    mLarge = !mLarge;
}

IMPLEMENT_APP(TestApplication);

(和 main.h):-

#pragma once

#include <memory>

#include <SFML/Graphics.hpp>
#include <wx/wx.h>
#include <string>
#include "wxSfmlCanvas.h"

// Our overridden class that does some SFML drawing.
class TestSfmlCanvas : public wxSfmlCanvas
{
public:

    TestSfmlCanvas(
            wxWindow*  Parent,
            wxWindowID Id,
            wxPoint&   Position,
            wxSize&    Size,
            long       Style = 0
    );

    void toggleSize();

protected:
    void OnUpdate() override;

private:
    sf::Texture mTexture;
    std::unique_ptr<sf::Sprite> mSprite;
    bool mLarge;
};


// wx Frame to contain the main canvas control.  Can have extra controls added to it as desired.
class TestFrame : public wxFrame
{
public :
    TestFrame();

protected:
    TestSfmlCanvas* mCanvas;

};

// Main wx Application instance.
class TestApplication : public wxApp
{
private :

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

        return true;
    }
};

(wxSFMLCanvas.h) :-

#pragma once

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

class wxSfmlCanvas : public wxControl, public sf::RenderWindow
{
public:
    wxSfmlCanvas(wxWindow* Parent = nullptr,
        wxWindowID Id = -1,
        //const wxPoint& Position = wxDefaultPosition,
        const wxSize& Size = wxDefaultSize,
        long Style = 0);

    virtual ~wxSfmlCanvas();

protected:

    virtual void OnUpdate();

    void OnIdle(wxIdleEvent&);

    void OnPaint(wxPaintEvent&);

    void OnEraseBackground(wxEraseEvent&);

    void OnSize(wxSizeEvent&);
    DECLARE_EVENT_TABLE()
};

(wxSFMLCanvas.cpp) :-

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


BEGIN_EVENT_TABLE(wxSfmlCanvas, wxControl)

EVT_PAINT(wxSfmlCanvas::OnPaint)
EVT_IDLE(wxSfmlCanvas::OnIdle)
EVT_ERASE_BACKGROUND(wxSfmlCanvas::OnEraseBackground)

EVT_SIZE(wxSfmlCanvas::OnSize)

END_EVENT_TABLE()

#ifdef __WXGTK__
#include <string>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <wx/gtk/win_gtk.h>
#endif

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

       

#else
       
        sf::RenderWindow::create(GetHandle());

#endif
    }

void wxSfmlCanvas::OnIdle(wxIdleEvent&)
{
    // Send a paint message when the control is idle, to ensure maximum framerate
    Refresh();
}

wxSfmlCanvas::~wxSfmlCanvas()
{
}

void wxSfmlCanvas::OnUpdate()
{
}

void wxSfmlCanvas::OnEraseBackground(wxEraseEvent&)
{

}

void wxSfmlCanvas::OnSize(wxSizeEvent& args)
{
    // Set the size of the sfml rendering window
    setSize(sf::Vector2u(args.GetSize().x, args.GetSize().y));

    // Also adjust the viewport so that a pixel stays 1-to-1.
    setView(sf::View(sf::FloatRect(0, 0, args.GetSize().x, args.GetSize().y)));
}

void wxSfmlCanvas::OnPaint(wxPaintEvent&)
{
    // Prepare the control to be repainted
    wxPaintDC Dc(this);

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

    // Display on screen
    display();
}

使用此代碼,我收到以下編譯錯誤:

Severity Code Description Project File Line Suppression State Error C4996 '_wgetenv': This function or variable may be unsafe. Consider using _wdupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. ae D:\\wxwidg\\include\\wx\\wxcrt.h 1050

和 100 個類似的其他人。 為什么? 我做錯了什么? wxWidgets 正確構建,SFML 和 wx 單獨工作正常,但是當組合時,由於某種原因會發生此錯誤。

您顯示的消息根本不是錯誤,它們是靜態分析器警告,可以安全地忽略,“不安全”功能在 wxWidgets 中不會以不安全的方式使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM