【问题标题】:Error with using wxWidgets alongside SFML将 wxWidgets 与 SFML 一起使用时出错
【发布时间】:2021-12-06 16:28:04
【问题描述】:

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 各自工作正常,但是当组合时,由于某种原因会发生此错误。

【问题讨论】:

  • 你使用什么编译器?什么版本?你在用treat warning as error开关吗?
  • 这些消息非常简单明了,而且非常具有描述性。您是否尝试过简单地按照他们的提示自行解决这些问题?
  • treat warning as error 开关关闭,消息难以理解和混淆

标签: c++ user-interface wxwidgets sfml


【解决方案1】:

您显示的消息根本不是错误,它们是静态分析器警告,可以安全地忽略,“不安全”功能不会在 wxWidgets 中以不安全的方式使用。

【讨论】:

  • 不,这些是构建错误,我无法绕过它们
  • 我不知道如何礼貌地说,所以我就直接说:不,你错了,这些不是构建错误。也许还有其他您没有显示的构建错误,但您显示的是(无害的)分析器警告。
猜你喜欢
  • 2019-03-28
  • 2014-09-10
  • 2014-08-10
  • 2022-09-13
  • 2021-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多