Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: MFC and SFML Window problem  (Read 29460 times)

0 Members and 2 Guests are viewing this topic.

xumepoc

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: MFC and SFML Window problem
« Reply #60 on: April 15, 2014, 11:09:13 pm »
I think this is exactly the same issue http://en.sfml-dev.org/forums/index.php?topic=12952.15. So how to change the videomode after the window has been created?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: MFC and SFML Window problem
« Reply #61 on: April 15, 2014, 11:17:12 pm »
I'm pretty sure you have to create() again to change the video mode.

There is a setSize() method, but I have no idea if that does what you need in this context.
« Last Edit: April 15, 2014, 11:22:40 pm by Ixrec »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: MFC and SFML Window problem
« Reply #62 on: April 15, 2014, 11:44:30 pm »
Can we please be absolutely clear about one point? You kept repeating that you got the right sf::Event::Resized events whenever you resized the widget. Is that really true? I really mean the sf::Event::Resized SFML event, not the QResizeEvent from Qt. If you get them, then it's absolutely impossible that your sf::RenderWindow size never changes...

By the way, you should write a complete and minimal example that reproduces the problem.
Laurent Gomila - SFML developer

xumepoc

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: MFC and SFML Window problem
« Reply #63 on: April 16, 2014, 05:18:21 pm »
Here is the code

sfmlcanvas.h
#ifndef QSFMLCANVAS_H
#define QSFMLCANVAS_H

#include <QWidget>
#include <SFML/Graphics.hpp>
#include <QTimer>

class QSFMLCanvas : public QWidget, public sf::RenderWindow
{
    //Q_OBJECT
public:
    explicit QSFMLCanvas(QWidget *parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime = 1);
    virtual void showEvent(QShowEvent*);
    virtual QPaintEngine* paintEngine() const;
    virtual void paintEvent(QPaintEvent*);

    virtual ~QSFMLCanvas();
    virtual void OnInit();
    virtual void OnUpdate();
private:
    QTimer myTimer;
    bool myInitialized;
    HWND _hWND;

protected:
    virtual void resizeEvent(QResizeEvent * event );
};

#endif // QSFMLCANVAS_H
 

sfmlcanvas.cpp
void QSFMLCanvas::showEvent(QShowEvent*)
{
    if (!myInitialized)
    {
        // Under X11, we need to flush the commands sent to the server to ensure that
        // SFML will get an updated view of the windows
#ifdef Q_WS_X11
        //XFlush(QX11Info::display());
#endif
        // Create the SFML window with the widget handle
        _hWND = sf::WindowHandle( winId());
        RenderWindow::create(_hWND);

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

        // Setup the timer to trigger a refresh at specified framerate
        connect(&myTimer, SIGNAL(timeout()), this, SLOT(repaint()));
        myTimer.start();
        myInitialized = true;
    }
}
QPaintEngine* QSFMLCanvas::paintEngine() const
{
    return 0;
}
void QSFMLCanvas::paintEvent(QPaintEvent*)
{
    // Let the derived class do its specific stuff
    OnUpdate();

    // Display on screen
    display();
}
void QSFMLCanvas::OnInit() {}
void QSFMLCanvas::OnUpdate() {}

void QSFMLCanvas::resizeEvent(QResizeEvent *event){}
 
mycanvas.h
#ifndef MYCANVAS_H
#define MYCANVAS_H

#include "qsfmlcanvas.h"
#include <SFML/Graphics.hpp>
class MyCanvas : public QSFMLCanvas
{
public :
    MyCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size);
    void OnInit();
    void OnUpdate();
    void resizeEvent(QResizeEvent *event);
private :
    sf::Clock myClock;
    sf::RectangleShape rect;
    sf::Font font;
    sf::Text text;
};

#endif // MYCANVAS_H

mycanvas.cpp
#include "mycanvas.h"
#include <iostream>
#include <string>
#include <QDir>
#include <QWheelEvent>
#include <qdebug.h>

MyCanvas::MyCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size) : QSFMLCanvas(Parent, Position, Size)
{
}
void MyCanvas::OnInit()
{
    myClock.restart();
}
void MyCanvas::OnUpdate()
{
    // Clear screen
    clear(sf::Color(69, 78, 83));

    sf::Font font;
    font.loadFromFile("arial.ttf");
    // Create a text
    sf::Text text("hello", font);
    text.setCharacterSize(30);
    text.setStyle(sf::Text::Bold);
    text.setColor(sf::Color::Red);
    text.setPosition(width() - 100,height()/2);

    // Draw it
    draw(text);
    myClock.restart();

}

void MyCanvas::resizeEvent(QResizeEvent *event)
{
    qDebug() << "RESIZED SFML";

   // setSize(sf::Vector2u(event->size().width(),event->size().height()));
    QWidget::resizeEvent(event);
}
 

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include "MyCanvas.h"


class MainWindow : public QWidget
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    MyCanvas Canvas;
private:

protected:
    void resizeEvent( QResizeEvent * event);
    void paintEvent(QPaintEvent *event);
};

#endif // MAINWINDOW_H
 

mainwindow.cpp
#include "mainwindow.h"
#include <QPainter>
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent),
    Canvas(this,QPoint(5,20),QSize(width() - 20,height() - 40))

{
    //setWindowFlags ( Qt :: FramelessWindowHint);
}

MainWindow::~MainWindow()
{

}
void MainWindow::resizeEvent(QResizeEvent *event)
{
    Canvas.move( 5, 22);
    Canvas.resize(width() - 10,height() - 44);
}
void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
}

 

main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
 

Just resize the window and you will see how the text disappear to the right.

xumepoc

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: MFC and SFML Window problem
« Reply #64 on: April 18, 2014, 08:47:24 am »
Bump ;D  ::)

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: MFC and SFML Window problem
« Reply #65 on: April 21, 2014, 11:55:22 am »
Can you make a complete and minimal code as described here?
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

xumepoc

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: MFC and SFML Window problem
« Reply #66 on: April 21, 2014, 06:28:42 pm »
I can't make more minimal code then this in order to reproduce the problem. ??? :-[

xumepoc

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: MFC and SFML Window problem
« Reply #67 on: May 01, 2014, 12:20:46 pm »
Well I overcome the problem for now, I delete the MyCanvas and recreate it again on end of resize. Not the best solution, but for now it works...

Now I have another problem. If I have a QDilaog or QWidget object in nonmodal on top of the SFML window I can't get back the focus of the mouse on that  object. The focus stays on the SFML. Interesting that the mouse wheel works at is should be, but the other functions (mouse move/click) are never send to the object. Always stay on the SFML.

Any solutions on that?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: MFC and SFML Window problem
« Reply #68 on: May 01, 2014, 08:03:06 pm »
This is probably another thing we can't even begin to help you with unless you provide a complete and minimal example, BUT it's known that SFML 2.1 had some window focus problems, so if that's the version you're using then you could try building from the latest Github source instead.

xumepoc

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: MFC and SFML Window problem
« Reply #69 on: May 01, 2014, 10:36:03 pm »
Thanks will try compile the latest.

xumepoc

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: MFC and SFML Window problem
« Reply #70 on: May 01, 2014, 11:08:54 pm »
Well what do you know, the latest beta worked :D Thanks Ixrec