SDL2 / wxWidgets SDL_CreateWindowFrom - c++

I have spent the last few days trying to embed an SDL2 display into wxWidgets but failing miserably. OK, here is what I have so far...
First a little detail:
wxWidgets version: 3.1 | SDL version 2.0.4 | OS: Fedora 23 | g++ 5.3.1
I have searched Google but cannot find much up-to-date information. What I have found suggests that I should use SDL_CreateWindowFrom() and then link it to a wxPanel or wxStaticBitmap.
I have written test code which implements a simple wxFrame and initialises SDL2, the code compiles fine but hangs when run.I am new to SDL2 and so would be grateful if somebody would take a look at my code and see what I am doing wrong. I'm obviously misunderstanding something here so any help would be appreciated.
The following code is a simplified block which highlights my problem.
main.h
#include <wx/wx.h>
#include <SDL2/SDL.h>
class TestApp : public wxApp
{
public:
virtual bool OnInit();
};
main.cpp
wxIMPLEMENT_APP_NO_MAIN(TestApp);
// Initialise SDL before WX-Widgets and GTK3
int main(int argc, char** argv)
{
// Initialise SDL and Error Check
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL Initialisation Error\n\n";
}
return wxEntry(argc, argv);
}
// WX-WIDGETS ENTRY POINT: Handover control to WX-Widgets
bool TestApp::OnInit()
{
UI_Frame *frame = new UI_Frame("SDL Playing with wxWidgets");
frame->Show(true);
return true;
}
TestApp.h
class UI_Frame : public wxFrame
{
public:
UI_Frame(const wxString& title);
private:
wxDECLARE_EVENT_TABLE();
};
And finally, the method which handles SDL and wxFrame
TestApp.cpp
wxBEGIN_EVENT_TABLE(UI_Frame, wxFrame)
wxEND_EVENT_TABLE()
UI_Frame::UI_Frame(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
{
this->SetSize(1024, 768);
wxBoxSizer* topSizer;
topSizer = new wxBoxSizer(wxVERTICAL);
wxPanel *panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
topSizer->Add(panel, 1, wxEXPAND | wxALL, 5);
/*******************************************************
*
* SDL Specific Stuff
*
******************************************************/
SDL_Window *sdl_window = nullptr;
SDL_Renderer *renderer = nullptr;
// SDL: Create Embedded Window in wxWidgets
sdl_window = SDL_CreateWindowFrom((void *) panel->GetHandle());
if (sdl_window == NULL) {
std::cerr << "SDL NULL Pointer ERROR";
return;
}
// *** This line causes the application to hang ***
renderer = SDL_CreateRenderer(sdl_window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
fprintf(stderr, "SDL: failed to create renderer: %s\n", SDL_GetError());
}
}

Related

Impossible to convert 'WXWidget' in 'GtkWidget *

Here is my code :
/// Get the Gtk2+ window ID of wxPanel pnlview for GStreamer output
GtkWidget* video_window = pnlView->GetHandle();
// Without this line, GStreamer will create its own new window for the video stream.
gtk_widget_realize(video_window);
GdkWindow *videoareaXwindow = gtk_widget_get_window(video_window);
data.xid = GDK_WINDOW_XID(videoareaXwindow) // Get xid for setting overlay later
were pnlView is define as wxPanel* pnlView;
But the consol give me this error : Impossible to convert 'WXWidget' in 'GtkWidget * at the line where I initialize video_window
Can someone has any idea how to fix it ?
I just want to add my gstreamer window in my wxWindow
Thank you
I've never used gstreamer, but I do sometimes use libvlc which is probably very similar. When using libvlc to render into a wxWindow, I need to wait until the wxWindow is fully created before I can set vlc to use it. This is done by adding an event handler for the window creation event.
The process of declaring and binding the event handler looks like this:
class MainWindow : public wxFrame
{
...
// Event handlers
void OnRendererWinCreated(wxWindowCreateEvent& event);
}
MainWindow::MainWindow(...)
{
...
#ifdef __WXGTK__
// On GTK+, we have to wait until the window is actually created before we
// can tell VLC to use it for output. So wait for the window create event.
pnlView->Bind(wxEVT_CREATE, &MainWindow::OnRendererWinCreated, this);
#elif defined(__WXMSW__)
m_player.setHwnd(pnlView->GetHandle());
#endif
...
}
For libvlc, my window creation event handler looks like this:
void MainWindow::OnRendererWinCreated(wxWindowCreateEvent& event)
{
#ifdef __WXGTK__
m_player.setXwindow(
gdk_x11_window_get_xid(gtk_widget_get_window(pnlView->GetHandle()))
);
pnlView->Unbind(wxEVT_CREATE,&MainWindow::OnRendererWinCreated,this);
#endif
}
Based on the code you posted, I think the body of an event handler that will work for you should look something like this:
void MainWindow::OnRendererWinCreated(wxWindowCreateEvent& event)
{
#ifdef __WXGTK__
/// Get the Gtk2+ window ID of wxPanel pnlview for GStreamer output
GtkWidget* video_window = pnlView->GetHandle();
GdkWindow *videoareaXwindow = gtk_widget_get_window(video_window);
data.xid = GDK_WINDOW_XID(videoareaXwindow) // Get xid for setting overlay later
pnlView->Unbind(wxEVT_CREATE,&MainWindow::OnRendererWinCreated,this);
#endif
}
Edit:
Here's a simple example of using GStreamer to draw onto a wxWindow on GTK. This shows how to use the wxEVT_CREATE to get the XID for a window and how to use GStreamer's bus sync handler callback to pass that XID to GStreamer at the correct time.
This is basically a mashup of the 2nd tutorial and the code snippet from the GstVideoOverlay page adjusted for wxWidgets. Since this is based on the 2nd tutorial, it just shows a test pattern. The source variable can be changed to show other videos.
Obviously this assumes GTK is using X11. Some adjustments would be needed if Wayland is used instead, but I don't have a running distro that uses Wayland to test on, so I don't know what changes are needed there.
#include "wx/wx.h"
#ifdef __WXGTK__
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#endif
#include <gst/gst.h>
#include <gst/video/videooverlay.h>
class MainWindow : public wxFrame
{
public:
MainWindow(const wxString& title);
~MainWindow();
private:
// Event handlers
void OnRendererWinCreated(wxWindowCreateEvent&);
void OnPlay(wxCommandEvent&);
void OnStop(wxCommandEvent&);
// Helper function
void LoadVideo();
void PlayHelper();
// wx controls
wxWindow* m_renderWindow;
wxButton* m_playButton;
wxButton* m_stopButton;
// GStreamer data
GstElement* m_pipeline;
guintptr m_xid;
};
MainWindow::MainWindow(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
{
// Create the UI widgets.
wxPanel* bg = new wxPanel(this,wxID_ANY);
m_renderWindow = new wxWindow(bg,wxID_ANY);
m_playButton = new wxButton(bg,wxID_ANY,"Play");
m_stopButton = new wxButton(bg,wxID_ANY,"Stop");
m_renderWindow->SetBackgroundColour(*wxBLACK);
m_playButton->Enable(true);
m_stopButton->Enable(false);
// Layout the UI.
wxBoxSizer* szr1 = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* szr2 = new wxBoxSizer(wxHORIZONTAL);
szr2->Add(m_playButton, wxSizerFlags(0).Border(wxLEFT|wxRIGHT|wxBOTTOM));
szr2->Add(m_stopButton, wxSizerFlags(0).Border(wxRIGHT|wxBOTTOM));
szr1->Add(m_renderWindow, wxSizerFlags(1).Expand().Border(wxBOTTOM));
szr1->Add(szr2, wxSizerFlags(0));
bg->SetSizer(szr1);
Layout();
// Set up the event handlers.
#ifdef __WXGTK__
m_renderWindow->Bind(wxEVT_CREATE, &MainWindow::OnRendererWinCreated, this);
m_playButton->Enable(false);
#endif
m_playButton->Bind(wxEVT_BUTTON, &MainWindow::OnPlay, this);
m_stopButton->Bind(wxEVT_BUTTON, &MainWindow::OnStop, this);
// Initialize GStreamer.
m_xid = 0;
m_pipeline = NULL;
gst_init(NULL, NULL);
}
MainWindow::~MainWindow()
{
if ( m_pipeline )
{
gst_element_set_state(m_pipeline, GST_STATE_NULL);
gst_object_unref(m_pipeline);
}
}
void MainWindow::OnRendererWinCreated(wxWindowCreateEvent&)
{
#ifdef __WXGTK__
// This event is no longer needed.
m_renderWindow->Unbind(wxEVT_CREATE,&MainWindow::OnRendererWinCreated,this);
// Get the XID for this window.
m_xid = GDK_WINDOW_XID(gtk_widget_get_window(m_renderWindow->GetHandle()));
// We can now load and play the video, so enable the play button.
m_playButton->Enable(true);
#endif
}
void MainWindow::OnPlay(wxCommandEvent&)
{
if ( m_pipeline )
{
PlayHelper();
}
else
{
LoadVideo();
}
}
void MainWindow::OnStop(wxCommandEvent&)
{
if ( m_pipeline )
{
GstStateChangeReturn ret =
gst_element_set_state(m_pipeline, GST_STATE_PAUSED);
if ( ret == GST_STATE_CHANGE_FAILURE )
{
wxLogWarning("Unable to set the pipeline to the paused state.");
gst_object_unref(m_pipeline);
m_pipeline = NULL;
m_playButton->Enable(true);
m_stopButton->Enable(false);
}
else
{
m_playButton->Enable(true);
m_stopButton->Enable(false);
}
}
}
void MainWindow::LoadVideo()
{
// Create the elements
GstElement *source = gst_element_factory_make("videotestsrc", "source");
#ifdef __WXGTK__
GstElement *sink = gst_element_factory_make("xvimagesink", "sink");
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(sink), m_xid);
#elif defined __WXMSW__
GstElement *sink = gst_element_factory_make("d3dvideosink", "sink");
WXWidget hwnd = m_renderWindow->GetHandle();
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(sink),
reinterpret_cast<guintptr>(hwnd));
#endif
//Create the empty pipeline
m_pipeline = gst_pipeline_new ("test-pipeline");
if ( !m_pipeline || !source || !sink )
{
wxLogError("Not all elements could be created.");
return;
}
// Build the pipeline
gst_bin_add_many(GST_BIN(m_pipeline), source, sink, NULL);
if ( gst_element_link(source, sink) != TRUE )
{
wxLogWarning("Elements could not be linked.");
gst_object_unref(m_pipeline);
m_pipeline = NULL;
return;
}
// Modify the source's properties
g_object_set(source, "pattern", 0, NULL);
PlayHelper();
}
void MainWindow::PlayHelper()
{
GstStateChangeReturn ret =
gst_element_set_state(m_pipeline, GST_STATE_PLAYING);
if ( ret == GST_STATE_CHANGE_FAILURE )
{
wxLogWarning("Unable to set the pipeline to the playing state.");
gst_object_unref(m_pipeline);
m_pipeline = NULL;
m_playButton->Enable(true);
m_stopButton->Enable(false);
}
else
{
m_playButton->Enable(false);
m_stopButton->Enable(true);
}
}
class MyApp : public wxApp
{
public:
bool OnInit() override
{
MainWindow* mainWindow = new MainWindow("wxWidgets GStreamer demo");
mainWindow->Show();
return true;
}
};
wxIMPLEMENT_APP(MyApp);
On mint it looks like this:
On windows it looks like this:

wxWidgets menu bar doesn't show up

I am trying to create a simple program with a menu using wxWidgets. However, the menu doesn't seem to be appearing properly.
This is my code:
helloworld.hpp:
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "TextFrame.hpp"
class HelloWorldApp : public wxApp {
public:
virtual bool OnInit();
};
DECLARE_APP(HelloWorldApp)
helloworld.cpp:
#include "helloworld.hpp"
IMPLEMENT_APP(HelloWorldApp)
bool HelloWorldApp::OnInit() {
TextFrame *frame = new TextFrame(_T("Hi"), 200, 200, 800, 600);
frame->CreateStatusBar();
frame->SetStatusText(_T("Hello, World!"));
frame->Show(true);
SetTopWindow(frame);
return true;
}
TextFrame.hpp:
#pragma once
#include <wx/wxprec.h>
#ifndef WX_PREC
#include <wx/wx.h>
#endif
class TextFrame : public wxFrame {
public:
/** Constructor. Creates a new TextFrame */
TextFrame(const wxChar *title, int xpos, int ypos, int width, int height);
private:
wxTextCtrl *m_pTextCtrl;
wxMenuBar *m_pMenuBar;
wxMenu *m_pFileMenu;
wxMenu *m_pHelpMenu;
};
TextFrame.cpp:
#include "TextFrame.hpp"
TextFrame::TextFrame(const wxChar *title, int xpos, int ypos, int width, int height)
: wxFrame((wxFrame *) NULL, -1, title, wxPoint(xpos, ypos), wxSize(width, height))
{
m_pTextCtrl = new wxTextCtrl(this, -1, _T("Type some text..."),
wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
m_pMenuBar = new wxMenuBar();
// File Menu
m_pFileMenu = new wxMenu();
m_pFileMenu->Append(wxID_OPEN, _T("&Open"));
m_pFileMenu->Append(wxID_SAVE, _T("&Save"));
m_pFileMenu->AppendSeparator();
m_pFileMenu->Append(wxID_EXIT, _T("&Quit"));
m_pMenuBar->Append(m_pFileMenu, _T("&File"));
// About menu
m_pHelpMenu = new wxMenu();
m_pHelpMenu->Append(wxID_ABOUT, _T("&About"));
m_pMenuBar->Append(m_pHelpMenu, _T("&Help"));
SetMenuBar(m_pMenuBar);
}
This code comes (for the most part) directly from here.
It compiles successfully (using g++ TextFrame.cpp helloworld.cpp `wx-config -cxxflags --libs` -o helloworld), but when I run it I see the default menu, and not the custom one I tried to add.
I tried out the "menu" sample and that works fine, so I think I'm doing something wrong here.
Thank you for your help.

C++ SDL texture Error Microsoft Visual Studio 2013

I'm trying to run this code but it keeps giving me an error.
I copyed SDL2_image.lib in the debug folder but in vain.
I'm at the beggining of programming so please be patient.
Errors:
Error 1 error C3861: 'IMG_LoadTexture': identifier not found
Error 2 IntelliSense: identifier "IMG_LoadTexture" is undefined
#include<SDL/SDL.h>
#include<iostream>
using namespace std;
int main(int argc, char** argv)
{
bool quit = false;
//*Initializing Window;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = NULL;
window = SDL_CreateWindow("Game Test", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
//*If game Crushes;
if (window == NULL)
{
cout << "The game window is not working";
}
//*Creating Update Function
SDL_Renderer* render = NULL;
render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event* mainEvent = new SDL_Event();
//*End Update Function
//*Adding Textures;
SDL_Texture* grass_image = NULL;
grass_image = IMG_LoadTexture(render, "grass.bmp");
//*Creating a Sprite;
SDL_Rect grass_rect;
grass_rect.x = 10;
grass_rect.y = 50;
grass_rect.w = 250;
grass_rect.h = 250;
//*Content Of the Window;
while (!quit && mainEvent->type!=SDL_QUIT)
{
SDL_PollEvent(mainEvent);
SDL_RenderClear(render);
SDL_RenderCopy(render, grass_image, NULL, &grass_rect);
SDL_RenderPresent(render);
}
//*End Window Content
//*Memory Cleaning
SDL_DestroyWindow(window);
SDL_DestroyRenderer(render);
delete mainEvent;
//*End Memory Cleaning
return 0;
}
You are missing to include the header that contains the declaration of IMG_LoadTexture():
#include <SDL/SDL_image.h>
It's a separate extension library for SDL, and besides including that header, you'll also need to link that library with your project.

Is there any way to get desktop picture bitmap from MS RDP component (AxMSTSCLib)?

I'm writing an app using Qt Framework and MS RDP component. What I need is to post-process remote computer's desktop image before picturing it on the screen.
So, the main question is: is there any way to grab remote desktop picture bitmap from MsRdpClientNotSafeForScripting instance? In other words, I need direct access to memory containing the remote computer's desktop image data.
I'm using ActiveQt to work with the RDP component. I've tried to get an OLE object from AxWidget and paint it on the HBITMAP (code in OnMakeScreenShotSlot()). But, in the first place, it's wrong way to get the screen bitmap, and, well, this method gives the wrong result after all: (attached code) when we press "Screenshot" button the file "screen.bmp" appears which contains white rectangle with text "I'm alive!" in the centre (but not the image of remote computer's desktop). "I'm alive!" is the value of the ConnectedStatusText property of IMsRdpClient instance.
Code is attached. Work environment: Windows 8, MSVC 2012, Qt 4.8.5, x86.
Main.cpp:
#include <QApplication>
#include "containerwidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ContainerWidget *w = new ContainerWidget();
w->show();
return a.exec();
}
containerwidget.h:
#ifndef CONTAINERWIDGET_H
#define CONTAINERWIDGET_H
#include <QWidget>
#include <QAxWidget>
#include <QAxObject>
#include <QPushButton>
class ContainerWidget : public QWidget
{
Q_OBJECT
QAxWidget *m_rdpWidget;
QPushButton *m_screenshotButton;
void initRdpWidget();
public:
ContainerWidget(QWidget *parent = 0);
~ContainerWidget();
public slots:
void OnMakeScreenshotSlot();
};
#endif // CONTAINERWIDGET_H
containerwidget.cpp:
#include "containerwidget.h"
#include <QBoxLayout>
#include <QDebug>
#include <QUuid>
#include <comdef.h>
ContainerWidget::ContainerWidget(QWidget *parent) :
QWidget(parent)
{
initRdpWidget();
m_screenshotButton = new QPushButton("Make screenshot", this);
connect(m_screenshotButton, SIGNAL(clicked()), this, SLOT(OnMakeScreenshotSlot()));
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->addWidget(m_rdpWidget);
mainLayout->addWidget(m_screenshotButton);
}
ContainerWidget::~ContainerWidget()
{
if (m_rdpWidget) {
m_rdpWidget->dynamicCall("Disconnect()");
}
}
void ContainerWidget::initRdpWidget()
{
m_rdpWidget = new QAxWidget();
m_rdpWidget->setControl("{7cacbd7b-0d99-468f-ac33-22e495c0afe5}");
m_rdpWidget->dynamicCall("SetDesktopWidth(int)", 800);
m_rdpWidget->dynamicCall("SetDesktopHeight(int)", 600);
m_rdpWidget->dynamicCall("SetServer(QString)", "ip");
m_rdpWidget->dynamicCall("SetUserName(QString)", "user");
m_rdpWidget->dynamicCall("SetConnectedStatusText(QString)", "I'm alive!");
QAxObject *advancedSettings2 = m_rdpWidget->querySubObject("AdvancedSettings2");
if (advancedSettings2) {
advancedSettings2->dynamicCall("SetClearTextPassword(QString)", "password");
advancedSettings2->dynamicCall("SetAuthenticationLevel(int)", 2);
}
m_rdpWidget->dynamicCall("Connect()");
m_rdpWidget->setFixedSize(800, 600);
m_rdpWidget->setVisible(true);
}
void ContainerWidget::OnMakeScreenshotSlot()
{
if (m_rdpWidget != NULL) {
IOleObject *oleObj = NULL;
m_rdpWidget->queryInterface((QUuid)IID_IOleObject, (void **)&oleObj);
if (oleObj == NULL) {
qDebug() << "bad ole obj.";
return;
}
IViewObject2 *iviewObj2 = NULL;
HRESULT hres = oleObj->QueryInterface(IID_IViewObject2, (void **)&iviewObj2);
if (SUCCEEDED(hres)) {
SIZE picSize;
hres = iviewObj2->GetExtent(DVASPECT_CONTENT, -1, NULL, &picSize);
if (SUCCEEDED(hres)) {
HDC dc = GetDC(0);
SIZE adjustedSize;
adjustedSize.cx = MulDiv(picSize.cx, GetDeviceCaps(dc, LOGPIXELSX), 2540);
adjustedSize.cy = MulDiv(picSize.cy, GetDeviceCaps(dc, LOGPIXELSY), 2540);
ReleaseDC(0, dc);
RECT r;
SetRect(&r, 0, 0, adjustedSize.cx, adjustedSize.cy);
HDC tmpDC = GetDC(0);
HDC memDC = CreateCompatibleDC(tmpDC);
HBITMAP hBmp = CreateCompatibleBitmap(memDC, adjustedSize.cx, adjustedSize.cy);
HBITMAP oldBmp = (HBITMAP)SelectObject(memDC, hBmp);
OleDraw(oleObj, DVASPECT_CONTENT, memDC, &r);
QPixmap p = QPixmap::fromWinHBITMAP(hBmp);
p.save(QString("screen.bmp"));
SelectObject(memDC, oldBmp);
DeleteDC(memDC);
ReleaseDC(0, tmpDC);
} else {
qDebug() << "bad picSize.";
}
} else {
qDebug() << "bad iviewobj2.";
}
}
}
Well, it seems like there is no way to get raw image bytes from MsTsc component.

Adding an OpenGL window into a GTK builder

I am writing a C++ application using GTK and OpenGL. I have a GTK builder which reads an XML file and builds itself. I need to add an "OpenGL window area" into it. How do I do this?
The way I do it (gtkmm / C++ code) is like this:
#include <gtkmm.h>
#include <gtkglmm.h>
class GLWidget : public Gtk::GL::DrawingArea {
public:
GLWidget(Glib::RefPtr<Gdk::GL::Config> glconfig)
: Gtk::GL::DrawingArea(glconfig) {}
~GLWidget() {}
virtual bool on_expose_event(GdkEventExpose* event);
};
bool GLWidget::on_expose_event(GdkEventExpose* event)
{
Glib::RefPtr<Gdk::GL::Drawable> d = get_gl_drawable();
d->gl_begin(get_gl_context());
// make this as complex as you need
glClear(GL_COLOR_BUFFER_BIT);
d->swap_buffers();
d->gl_end();
return true;
}
int main(int argc, char **argv)
{
Gtk::Main kit(argc, argv);
Gtk::GL::init(argc, argv);
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("ui.glade");
Gtk::Window* mainWindow;
Gtk::Alignment* container;
builder->get_widget("mainWindow", mainWindow);
builder->get_widget("Box", container);
if (mainWindow == NULL || container == NULL) {
g_critical("Gtk Builder failed to load mainWindow and/or container !\n");
return -1;
}
Glib::RefPtr<Gdk::GL::Config> glconfig;
glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGBA | Gdk::GL::MODE_DOUBLE);
if (!glconfig)
glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB);
if (!glconfig) {
g_critical("Cannot create OpenGL-capable config\n");
return -1;
}
GLWidget drawingArea(glconfig);
drawingArea.set_size_request(640, 480);
drawingArea.show();
container->add(drawingArea);
kit.run(*mainWindow);
return 0;
I.e. I'm simply loading a UI that contains an empty container widget, get the handle to that by name, then create the GL-enabled drawing area in code and add it to the empty container. Supposedly, it's possible to "load-time" substitute an ordinary Gtk DrawingArea for a GtkGL one (see this posting) but that method didn't work for me; the above, programmatically creating it, always did.
You need gtkglext / gtkglextmm for the GL Drawingarea widget.