How to be notified on focus? - qml

This question has been edited:
I am working with QML.
I have a custom type called polygon, which is a subcalss of QDeclarativeItem.
I want to be notified when a mouse clicked on a polygon(has focus).
I know QDeclarativeItem has a function: focusInEvent.
I override it in Polygon.cpp, here is polygon.cpp
#include "polygon.h"
#include "point.h"
#include <QPainter>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <QGraphicsSceneDragDropEvent>
#include <QFocusEvent>
#include "DeclarativeDragDropEvent.h"
using namespace std;
using namespace Qt;
Polygon::Polygon(QDeclarativeItem *parent)
: QDeclarativeItem(parent)
{
// need to disable this flag to draw inside a QDeclarativeItem
setFlag(QDeclarativeItem::ItemHasNoContents, false);
setFlags(ItemIsSelectable|ItemIsMovable|ItemIsFocusable);
setAcceptDrops(true);
}
QVariant Polygon::itemChange(GraphicsItemChange change, const QVariant &value)
{
return QGraphicsItem::itemChange(change, value);
}
void Polygon::focusInEvent ( QFocusEvent * event ){
cout<<"focusin"<<endl;
}
QRectF Polygon::boundingRect() const{
QVector<QPointF> vPnt=listToVector(m_vertices);
return QPolygonF(vPnt).boundingRect();
}
QPainterPath Polygon::shape () const
{
QPainterPath path;
QVector<QPointF> vPnt=listToVector(m_vertices);
path.addPolygon(QPolygonF(vPnt));
return path;
}
QString Polygon::name() const
{
return m_name;
}
void Polygon::setName(const QString &name)
{
m_name = name;
}
QColor Polygon::color() const
{
return m_color;
}
void Polygon::setColor(const QColor &color)
{
m_color = color;
}
void Polygon::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
QPen pen(m_color, 2);
painter->setPen(pen);
painter->setRenderHints(QPainter::Antialiasing, true);
QVector<QPointF> vPnt=listToVector(m_vertices);
painter->setBrush(QBrush(m_color,Qt::SolidPattern));
painter-> drawPolygon(QPolygonF(vPnt),Qt::OddEvenFill);
}
QVector<QPointF> Polygon:: listToVector(QList<Point *> lpnt) const{
QVector<QPointF> vPnt;
for(int i=0;i<lpnt.length();i++){
vPnt.append(QPointF(lpnt.at(i)->x(),lpnt.at(i)->y()));
}
return vPnt;
}
QDeclarativeListProperty<Point> Polygon::vertices()
{
return QDeclarativeListProperty<Point>(this, 0, &Polygon::append_vertex);
}
void Polygon::append_vertex(QDeclarativeListProperty<Point> *list, Point *vertex)
{
Polygon *polygon = qobject_cast<Polygon *>(list->object);
if (polygon) {
vertex->setParentItem(polygon);
polygon->m_vertices.append(vertex);
}
}
And here is my qml file:
import MyTypes 1.0
import QtQuick 1.0
import Qt 4.7
Item {
id:container
width: 300; height: 200
Polygon {
id: aPolygon
anchors.centerIn: parent
width: 100; height: 100
name: "A simple polygon"
color: "blue"
vertices:[
Point{x:20.0; y:40.0},
Point{x:40.0; y:40.0},
Point{x:40.0; y:20.0},
Point{x:20.0; y:20.0}
]
}
}
i override shape, boundingRect as you can see to define Polygon object and to succeed to be notified when a click, a focus appeared in ti
But i can not see cout output in focusInEvent function on screen.
Should i add something on main.cpp of project?
What to do to C++ code know that the object has focus?
Some connection?
Thanks for any idea.

I tried and it seems the problem is with the output "cout", try the "qDebug" and see that you have good code.
All you did is correct.

Related

C++ Builder - program end error and localization error

I have a problem with a program I have done with Embarcadero C++ Builder.
Part one:
I want to make stand-alone .exe file, and according to that i have disabled "link with dynamic RTL" and disabled "link with runtime packages". But, every time i end my program, I get an error "Abnormal program termination". I've invstigated that and found a partial solution. When I enable link with runtime packages, that error does not appear, but .exe file won't run on PC which don't have C++ builder installed, because some .bpl files missing. So, for now, I have two possibilities: to have "Abnormal program termination" error, or not to have, functional .exe file for all PCs. Except for that error, my program works perfectly.
Part two:
I made fully functional program with 3 units, and I want to translate it on english language using resource DLL wizard. I can preview translated form, but I can't build it, because it say Error but I can't see any more details about error. I really don't know how is it possible to have working program with no errors, and localized version to have an error. I got the advice to do "clean all" and then "build all" but I can't even clean all because of that error.
I really don't know what to do anymore, I am struggling with those problems for a week now and I really hope that someone can help me. Thanks :)
Localization
Not sure if your localization tool is suited for Borland/Embarcadero apps (it might be just for MSVC++ apps which might be the problem).
I do localization on my own (as I needed custom stuff like internal messages and tables translation. So I coded a ~55 KByte parser that takes *.h,*.cpp,*.dfm of all windows as input and creates *.ini file and *.h that will read the selected language ini and translate all forms to it (Caption,Hint,...) then just for each new language I copy the ini and translate the strings. This way I can switch languages on the run without any DLL or restart needed and customers can add new languages on their own without the need for source code or programming skills.
Can not share the code however as it uses some non sharable (corporate) libs of mine for strings,dynamic lists and fast ini file access (they are included in the 55 KByte estimate) but I can share the executable (win32 standalone compiled in BDS2006 and for VCL BDS2006 formatting of code):
Form decode tool v2.70
Use the slow download (it is free without registration). I even add one window in the formulars for testing. You just run the exe and when done use the files from output. However the outputed source files use the fast ini file access (I can not share) so you would need to encode that (or replace with your style access). Here slow winapi based workaround for my ini.h that shoudl work:
#ifndef _ini_h
#define _ini_h
class inifile
{
public:
AnsiString section,filename;
// inline
inifile() {close();}
inifile(inifile& a) { *this=a; }
~inifile() {close();}
inifile* operator = (const inifile *a) { *this=*a; return this; }
//inifile* operator = (const inifile &a) { ...copy... return this; }
int open(AnsiString name) // open ini file
{
close();
if (FileExists(name)) { filename=name; return 1; }
return 0;
}
void close() // close ini file
{
filename="";
section="";
}
int sec_getid(const AnsiString &sec, bool _add) // select section to work with ignoring the returned ID
{
section=sec;
return 0;
}
AnsiString key_get(int section_id,const AnsiString &key,const AnsiString &def) // read key from section of ini with default value
{
if (filename=="") return def;
char dest[1024]; AnsiString val;
GetPrivateProfileString(section.c_str(),key.c_str(),def.c_str(),dest,1024,filename.c_str());
val=dest; return val;
}
};
#endif
So the app has 3 folders:
formulars
put all the *.h,*.cpp;*.dfm files of your forms here. The parsing of *.h is stopping on public: (skip user declarations) to prevent collisions with user code. You can also add comments to end of component line:
// LANGUAGE_SKIP_CAPT
// LANGUAGE_SKIP_TEXT
// LANGUAGE_SKIP_HINT
or any combination to prevent translation is messing with it.
messages
Ignore this it is for *.cpp source files with internal messages _messages[]to translate must be properly encoded using markers in comments like this:
AnsiString _messages[_msg_enum_end+1]=
{
// LANGUAGE_MSG000_TEXT_BEG
"Error: message id out of range !!!",
"OpenGL FBO support is missing !!! ... switching to simplified graphics",
"OpenGL VBO support is missing !!! ... switching to simplified graphics",
"Sorry: ATI graphic card detected !!! ... switching to simplified graphics",
"Sorry: ATI Radeon HD 5450 graphic card detected !!! ... disabling preview features",
"Done.",
// LANGUAGE_MSG000_TEXT_END
"",
""
};
output
the app here outputs 4 files
init.h can ignore this it is just init/exit load/stroing of window state
init.ini ini file for the init can ignore this
into app ini.
language.h this is what you want it is the translation routine
english.ini translation ini file using current window texts
Here example of generated language.h:
#include <math.h>
#include "ini.h" // use the ini.h above
void language_init(AnsiString name)
{
int sec;
inifile ini;
ini.open(name);
sec=ini.sec_getid("Twin_EditorSetup",true);
win_EditorSetup->ck_specchar->Caption=ini.key_get(sec,"C_ck_specchar","Special chars.");
win_EditorSetup->ck_specchar->Hint =ini.key_get(sec,"H_ck_specchar","View special characters on/off");
win_EditorSetup->cb_colors->Hint =ini.key_get(sec,"H_cb_colors","Select color for editing");
win_EditorSetup->ed_tabs->Text =ini.key_get(sec,"T_ed_tabs","1");
win_EditorSetup->ed_tabs->Hint =ini.key_get(sec,"H_ed_tabs","Tabulator size");
win_EditorSetup->ck_smart->Caption=ini.key_get(sec,"C_ck_smart","Smart eol");
win_EditorSetup->ck_smart->Hint =ini.key_get(sec,"H_ck_smart","Delete empty space at end of edited line");
win_EditorSetup->RadioGroup1->Caption=ini.key_get(sec,"C_RadioGroup1","");
win_EditorSetup->RadioGroup1->Hint =ini.key_get(sec,"H_RadioGroup1","");
win_EditorSetup->rb_eol1310->Caption=ini.key_get(sec,"C_rb_eol1310","CR LF");
win_EditorSetup->rb_eol1310->Hint =ini.key_get(sec,"H_rb_eol1310","EOL CR LF 13 10");
win_EditorSetup->rb_eol13->Caption=ini.key_get(sec,"C_rb_eol13","CR");
win_EditorSetup->rb_eol13->Hint =ini.key_get(sec,"H_rb_eol13","EOL CR 13");
win_EditorSetup->rb_eol10->Caption=ini.key_get(sec,"C_rb_eol10","LF");
win_EditorSetup->rb_eol10->Hint =ini.key_get(sec,"H_rb_eol10","EOL LF 10");
win_EditorSetup->txt_tabs->Caption=ini.key_get(sec,"C_txt_tabs","TAB:");
win_EditorSetup->txt_tabs->Hint =ini.key_get(sec,"H_txt_tabs","");
win_EditorSetup->pan_color->Caption=ini.key_get(sec,"C_pan_color","");
win_EditorSetup->pan_color->Hint =ini.key_get(sec,"H_pan_color","Pick color");
sec=ini.sec_getid("Global",true);
ini.close();
}
so in your main app form cpp include language.h but after its pointer is declared (after line like TForm1 *Form1; ) otherwise you will get access violation erros. Now call language_init with selected language ini file on any language change or Apps init like this:
if (Form1->Visible) language_init(ExtractFilePath(Application->ExeName)+"english.ini");
you need to specify full path if winapi is used for the ini.h !!!
Packages
Hard to say without MCVE. You most likely use non standard 3th party package and or have a bug in the code. My bet is you got memory leaks which tend to make a such errors time to time (especially on exit of app) try to use CodeGuard in the Project options which will show you runtime errors and allow you to debug them much better but it will not work for huge projects. Also if you are on older compiler or need to debug the memory leaks see these:
traceback a pointer in c++ code
BDS 2006 C++ hidden memory manager conflicts
They might solve your issues (even those you did not know of yet)
P.S.
If you're interested here is the main source for this tool (but without the supporting *.h files it will not work but you can see how it work in case you want to encode this on your own:
//--- Formular decoder storage class ver 2.40 -------------------------------
#ifndef _frm_decode_h
#define _frm_decode_h
//---------------------------------------------------------------------------
const int _frm_cmp_prop_none =0x00000000;
// language properties
const int _frm_cmp_prop_capt =0x00000001;
const int _frm_cmp_prop_text =0x00000002;
const int _frm_cmp_prop_hint =0x00000004;
// ini properties
const int _frm_cmp_prop_align =0x00010000;
const int _frm_cmp_prop_pos =0x00020000;
const int _frm_cmp_prop_check =0x00040000;
const int _frm_cmp_prop_down =0x00080000;
const int _frm_cmp_prop_page =0x00100000;
class _frm_component
{
public:
AnsiString name; // name in C++
int prop; // component class enum
int action; // if true do not save/load by language
};
_frm_component frm_component[]=
{
// standard
"TMenuItem" ,_frm_cmp_prop_capt|_frm_cmp_prop_hint|_frm_cmp_prop_align|_frm_cmp_prop_check,0,
"TLabel" ,_frm_cmp_prop_capt|_frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TEdit" ,_frm_cmp_prop_text|_frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TMemo" , _frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TButton" ,_frm_cmp_prop_capt|_frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TCheckBox" ,_frm_cmp_prop_capt|_frm_cmp_prop_hint|_frm_cmp_prop_align|_frm_cmp_prop_check,0,
"TRadioButton" ,_frm_cmp_prop_capt|_frm_cmp_prop_hint|_frm_cmp_prop_align|_frm_cmp_prop_check,0,
"TListBox" , _frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TComboBox" , _frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TScrollBar" , _frm_cmp_prop_hint|_frm_cmp_prop_align|_frm_cmp_prop_pos ,0,
"TGroupBox" ,_frm_cmp_prop_capt|_frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TRadioGroup" ,_frm_cmp_prop_capt|_frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TPanel" ,_frm_cmp_prop_capt|_frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TAction" ,_frm_cmp_prop_capt|_frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
// additional
"TBitBtn" ,_frm_cmp_prop_capt|_frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TSpeedButton" ,_frm_cmp_prop_capt|_frm_cmp_prop_hint|_frm_cmp_prop_align|_frm_cmp_prop_down ,0,
"TStringGrid" , _frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TCategoryButtons" , _frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
// win32
"TProgressBar" , _frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
// system
"TPaintBox" , _frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
// win3.1
"TTabbedNotebook" , _frm_cmp_prop_hint|_frm_cmp_prop_align|_frm_cmp_prop_page ,0,
"TFileListBox" , _frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TDirectoryListBox" , _frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"TDriveComboBox" , _frm_cmp_prop_hint|_frm_cmp_prop_align ,0,
"",0,0
};
//---------------------------------------------------------------------------
class frm_decode
{
public:
AnsiString form; // formular class name in C++
struct _component:_frm_component
{
// language properties
AnsiString text,hint;
// init properties
AnsiString align; // -only for internal init purposes
AnsiString checked,down,position,left,top,width,height;
int skip; // znacky
void reset()
{
text="";
hint="";
align="alNone";
checked="False";
down="False";
position="1";
left="0";
top="0";
width="1";
height="1";
skip=0;
}
};
struct _message { AnsiString id,txt; };
List<_component> cmp;
List<_message> msg;
frm_decode(){ reset(); }
void reset() { form=""; cmp.reset(); msg.reset(); }
void load_form(AnsiString file); // load formular (file.h, file.dfm)
void load_msg(AnsiString file); // load messages (file.h, file.cpp)
void save_lang_ini(inifile &ini); // save language to ini
void save_init_ini(inifile &ini); // save initialisation to ini
void save_lang_cpp(AnsiString &txt,bool end=true); // save language loader (C++) to txt
void save_init_cpp(AnsiString &txt,bool end=true); // save init loader (C++) to txt
void save_exit_cpp(AnsiString &txt,bool end=true); // save exit saver (C++) to txt
};
//---------------------------------------------------------------------------
void frm_decode::load_form(AnsiString file)
{
// nepodporuje "/* ... */"
int hnd,adr,siz,i,j,l,e,_skip;
AnsiString lin,s,s0,ss;
_component a;
BYTE *txt;
reset();
// load list of components from: file.h
hnd=FileOpen(file+".h",fmOpenRead);
if (hnd<0) return;
siz=FileSeek(hnd,0,2);
FileSeek(hnd,0,0);
txt=new BYTE[siz];
if (txt==NULL) { FileClose(hnd); return; }
FileRead(hnd,txt,siz);
FileClose(hnd);
for (adr=0;adr<siz;)
{
lin=txt_load_lin(txt,siz,adr,true);
l=lin.Length();
for (_skip=0;i<=l;) // osetri zacky este pred odrezanim pokecu
{
s=str_load_str(lin,i,true);
if (s=="LANGUAGE_SKIP_CAPT") _skip|=_frm_cmp_prop_capt;
if (s=="LANGUAGE_SKIP_TEXT") _skip|=_frm_cmp_prop_text;
if (s=="LANGUAGE_SKIP_HINT") _skip|=_frm_cmp_prop_hint;
}
if (str_is_mask(lin,"*//*")) { s=lin; lin=""; for (i=1;i<l;i++) if ((s[i]=='/')&&(s[i+1]=='/')) break; else lin+=char(s[i]); l=lin.Length(); }
s=""; s0=""; e=0;
for (i=1;i<=l;)
{
s0=s;
s=str_load_str(lin,i,true);
if (s=="private:") // stop processing for non formular symbols
{
adr=siz; break;
}
if (!e) { e=1; continue; }
if (s0=="class")
{
form=s;
continue;
}
if (s!="")
for (j=0;;j++)
{
if (frm_component[j].name=="") break;
if (frm_component[j].name==s0)
{
a.reset();
a.name="";
if (s[1]!='*') s=" "+s;
for (int q=2;q<=s.Length();q++) a.name+=s[q];
a.prop=frm_component[j].prop;
a.action=0;
a.skip=_skip;
cmp.add(a);
break;
}
}
}
}
delete txt;
// load component properties from: file.dfm
int cmpid=-1;
hnd=FileOpen(file+".dfm",fmOpenRead);
if (hnd<0) return;
siz=FileSeek(hnd,0,2);
FileSeek(hnd,0,0);
txt=new BYTE[siz];
if (txt==NULL) { FileClose(hnd); return; }
FileRead(hnd,txt,siz);
FileClose(hnd);
for (adr=0;adr<siz;)
{
lin=txt_load_lin(txt,siz,adr,true);
l=lin.Length();
for (i=1;i<=l;i++) // if cutted '' line then continue
if (lin[i]>32)
{
if (lin[i]!='\'') i=-1;
break;
}
if (i<0) { s=""; s0=""; e=0; } // else new line
for (i=1;i<=l;)
{
int q=1;
if (s!="")
if (s[1]=='\'')
{
if (s[s.Length()]!='\'')
{
s+=" "+str_load_str(lin,i,true);
if (s[s.Length()]!='\'') continue;
}
q=0;
}
if (q)
{
int qqq=0; // handle cutted '' line
ss=str_load_str(lin,i,true);
if ((s0!="")&&((ss!="")&&(ss[1]=='\''))) { qqq=1; s+=ss; }
if ((s0!="")&&(ss=="+")) { qqq=1; }
if (!qqq) { s0=s; s=ss; }
if (s!="")
if (s[1]=='\'')
if (s[s.Length()]!='\'')
e=0;
}
/* // odstran ''
if (s!="")
if (s[1]=='\'')
if (s[s.Length()]=='\'')
{ ss=""; for (q=2;q<s.Length();q++) ss+=s[q]; s=ss; }
*/
if (!e) { e=1; continue; }
if (s!="")
if (s0=="object")
{
cmpid=-1;
if (s[s.Length()]==':')
{
AnsiString t=s;
s=""; for (j=1;j<t.Length();j++) s+=char(t[j]);
}
for (j=0;j<cmp.num;j++)
if (cmp[j].name==s)
{
cmpid=j;
break;
}
continue;
}
if (cmpid<0) continue;
char aa;
bool tt;
int ii,cc,ll=s.Length();
if (ll>0)
{
ss="";
tt=false;
for (ii=1;ii<=ll;ii++)
{
aa=s[ii];
if (aa=='\'') { tt=!tt; continue; }
if (tt) { ss+=aa; continue; }
if (aa!='#'){ ss+=aa; continue; }
cc=0;
for (ii++;ii<=ll;ii++)
{
aa=s[ii];
if (aa=='\'') { ii--; break; }
if (aa< '0') { ii--; break; }
if (aa> '9') { ii--; break; }
cc*=10; cc+=aa-'0';
}
ss+=char(cc);
}
s=ss;
}
if (s0=="Action" ) cmp[cmpid].action=1;
if (s0=="Caption" ) cmp[cmpid].text=s;
if (s0=="Text" ) cmp[cmpid].text=s;
if (s0=="Hint" ) cmp[cmpid].hint=s;
if (s0=="Align" ) cmp[cmpid].align=s;
if (s0=="Checked" ) cmp[cmpid].checked=s;
if (s0=="Down" ) cmp[cmpid].down=s;
if (s0=="Position") cmp[cmpid].position=s;
if (s0=="Left" ) cmp[cmpid].left=s;
if (s0=="Top" ) cmp[cmpid].top=s;
if (s0=="Width" ) cmp[cmpid].width=s;
if (s0=="Height" ) cmp[cmpid].height=s;
}
}
delete txt;
// load list of messages from: file.cpp
load_msg(file+".cpp");
}
//---------------------------------------------------------------------------
void frm_decode::load_msg(AnsiString file)
{
// nepodporuje "/* ... */"
int hnd,adr,siz,i,l,e;
AnsiString lin,s;
BYTE *txt;
hnd=FileOpen(file,fmOpenRead);
if (hnd<0) return;
siz=FileSeek(hnd,0,2);
FileSeek(hnd,0,0);
txt=new BYTE[siz];
if (txt==NULL) { FileClose(hnd); return; }
FileRead(hnd,txt,siz);
FileClose(hnd);
e=msg.num;
for (adr=0;adr<siz;)
{
lin=txt_load_lin(txt,siz,adr,true);
l=lin.Length();
for (i=1;i<=l;)
{
s=str_load_str(lin,i,true);
if (s=="_msg_enum_beg")
for (;adr<siz;)
{
lin=txt_load_lin(txt,siz,adr,true);
l=lin.Length();
for (i=1;i<=l;)
{
s=str_load_str(lin,i,true);
if (s=="_msg_enum_end") { adr=siz; i=l+1; break; }
if (str_is_mask(s,"_msg*"))
{
_message m;
m.id=s;
m.txt="";
msg.add(m);
}
}
}
}
}
for (adr=0;adr<siz;)
{
lin=txt_load_lin(txt,siz,adr,true);
l=lin.Length();
for (i=1;i<=l;)
{
s=str_load_str(lin,i,true);
if (s=="_messages[_msg_enum_end+1]")
for (;adr<siz;)
{
if (e>=msg.num) break;
s="";
for (;adr<siz;adr++)
if (txt[adr]=='"')
{
adr++;
for (;adr<siz;adr++)
if (txt[adr]=='"')
{
adr++;
msg[e].txt=s;
e++;
break;
}
else s+=char(txt[adr]);
break;
}
}
}
}
delete txt;
}
//---------------------------------------------------------------------------
void frm_decode::save_lang_ini(inifile &ini)
{
int i,p,s,sec;
AnsiString key,val;
sec=ini.sec_getid(form,true);
for (i=0;i<msg.num;i++) ini.key_set(sec,msg[i].id,msg[i].txt);
for (i=0;i<cmp.num;i++)
{
if (cmp[i].action) continue;
key=cmp[i].name;
p=cmp[i].prop;
s=cmp[i].skip;
if ((int(p&_frm_cmp_prop_capt)!=0)&&(int(s&_frm_cmp_prop_capt)==0)) ini.key_set(sec,"C_"+key,cmp[i].text);
if ((int(p&_frm_cmp_prop_text)!=0)&&(int(s&_frm_cmp_prop_text)==0)) ini.key_set(sec,"T_"+key,cmp[i].text);
if ((int(p&_frm_cmp_prop_hint)!=0)&&(int(s&_frm_cmp_prop_hint)==0)) ini.key_set(sec,"H_"+key,cmp[i].hint);
}
}
//---------------------------------------------------------------------------
void frm_decode::save_init_ini(inifile &ini)
{
int i,p,sec;
AnsiString key,val;
sec=ini.sec_getid(form,true);
for (i=0;i<cmp.num;i++)
{
if (cmp[i].action) continue;
key=cmp[i].name;
p=cmp[i].prop;
if (cmp[i].align=="alLeft") ini.key_set(sec,"XS_"+key,cmp[i].width);
if (cmp[i].align=="alRight") ini.key_set(sec,"XS_"+key,cmp[i].width);
if (cmp[i].align=="alTop") ini.key_set(sec,"YS_"+key,cmp[i].height);
if (cmp[i].align=="alBottom") ini.key_set(sec,"YS_"+key,cmp[i].height);
if (cmp[i].align=="alClient")
{
}
if (cmp[i].align=="alNone")
{
/*
ini.key_set(sec,"X0_"+key,cmp[i].left);
ini.key_set(sec,"Y0_"+key,cmp[i].top);
ini.key_set(sec,"XS_"+key,cmp[i].width);
ini.key_set(sec,"YS_"+key,cmp[i].height);
*/
}
if (int(p&_frm_cmp_prop_check )!=0) ini.key_set(sec,"CHK_"+key,(cmp[i].checked=="True")?"1":"0");
if (int(p&_frm_cmp_prop_down )!=0) ini.key_set(sec,"DWN_"+key,(cmp[i].down =="True")?"1":"0");
if (int(p&_frm_cmp_prop_pos )!=0) ini.key_set(sec,"POS_"+key, cmp[i].position );
}
}
//---------------------------------------------------------------------------
void frm_decode::save_lang_cpp(AnsiString &txt,bool end)
{
int i,p,s;
AnsiString cpp,lin,key,frm,tabl,endl;
tabl=char(9); endl=char(13); endl+=char(10);
cpp="";
if (txt=="")
{
lin="";
lin+="#include <math.h>"+endl;
lin+="#include \"ini.h\""+endl+endl;
lin+="void language_init(AnsiString name)"+endl;
lin+=tabl+"{"+endl;
lin+=tabl+"int sec;"+endl;
lin+=tabl+"inifile ini;"+endl;
lin+=tabl+"ini.open(name);"+endl;
cpp+=lin;
}
frm=""; for (i=2;i<=form.Length();i++) frm+=form[i];
cpp+=endl;
cpp+=tabl+"sec=ini.sec_getid(\""+form+"\",true);"+endl;
for (i=0;i<msg.num;i++)
{
lin=tabl+"_messages[";
if (i<100) lin+=" ";
if (i<10) lin+=" ";
lin+=i;
lin+="]=ini.key_get(sec,\""+msg[i].id+"\",\""+msg[i].txt+"\");"+endl;
cpp+=lin;
}
for (i=0;i<cmp.num;i++)
{
if (cmp[i].action) continue;
key=cmp[i].name;
p=cmp[i].prop;
s=cmp[i].skip;
lin="";
if ((int(p&_frm_cmp_prop_capt)!=0)&&(int(s&_frm_cmp_prop_capt)==0)) lin+=tabl+frm+"->"+key+"->Caption=ini.key_get(sec,\"C_"+key+"\",\""+cmp[i].text+"\");"+endl;
if ((int(p&_frm_cmp_prop_text)!=0)&&(int(s&_frm_cmp_prop_text)==0)) lin+=tabl+frm+"->"+key+"->Text =ini.key_get(sec,\"T_"+key+"\",\""+cmp[i].text+"\");"+endl;
if ((int(p&_frm_cmp_prop_hint)!=0)&&(int(s&_frm_cmp_prop_hint)==0)) lin+=tabl+frm+"->"+key+"->Hint =ini.key_get(sec,\"H_"+key+"\",\""+cmp[i].hint+"\");"+endl;
cpp+=lin;
}
if (end)
{
lin=endl;
lin+=tabl+"ini.close();"+endl;
lin+=tabl+"}"+endl;
cpp+=lin;
}
txt+=cpp;
}
//---------------------------------------------------------------------------
void frm_decode::save_init_cpp(AnsiString &txt,bool end)
{
int i,p;
AnsiString cpp,lin,key,frm,tabl,endl,endlx;
tabl=char(9); endl=char(13); endl+=char(10); endlx="\\"+endl;
cpp="";
if (txt=="")
{
lin="";
lin+="//---------------------------------------------------------------------------"+endl;
lin+="#include <math.h>"+endl;
lin+="#include \"ini.h\""+endl;
lin+="//---------------------------------------------------------------------------"+endl;
lin+="#define _def_program_init(name) for(;;)"+endlx;
lin+=tabl+"{"+endlx;
lin+=tabl+"int sec;"+endlx;
lin+=tabl+"inifile ini;"+endlx;
lin+=tabl+"ini.open(name);"+endlx;
// lin+=tabl+"if (!ini.open(name)) break;"+endlx;
cpp+=lin;
}
frm=""; for (i=2;i<=form.Length();i++) frm+=form[i];
cpp+=tabl+"sec=ini.sec_getid(\""+form+"\",true);"+endlx;
for (i=0;i<cmp.num;i++)
{
if (cmp[i].action) continue;
key=cmp[i].name;
p=cmp[i].prop;
lin="";
if (cmp[i].align=="alLeft") lin+=tabl+frm+"->"+key+"->Width =ini.key_geti(sec,\"XS_"+key+"\","+cmp[i].width +");"+endlx;
if (cmp[i].align=="alRight") lin+=tabl+frm+"->"+key+"->Width =ini.key_geti(sec,\"XS_"+key+"\","+cmp[i].width +");"+endlx;
if (cmp[i].align=="alTop") lin+=tabl+frm+"->"+key+"->Height=ini.key_geti(sec,\"YS_"+key+"\","+cmp[i].height+");"+endlx;
if (cmp[i].align=="alBottom") lin+=tabl+frm+"->"+key+"->Height=ini.key_geti(sec,\"YS_"+key+"\","+cmp[i].height+");"+endlx;
if (cmp[i].align=="alClient")
{
}
if (cmp[i].align=="alNone")
{
/*
lin+=tabl+frm+"->"+key+"->Left =ini.key_geti(sec,\"X0_"+key+"\","+cmp[i].left +");"+endlx;
lin+=tabl+frm+"->"+key+"->Top =ini.key_geti(sec,\"Y0_"+key+"\","+cmp[i].top +");"+endlx;
lin+=tabl+frm+"->"+key+"->Width =ini.key_geti(sec,\"XS_"+key+"\","+cmp[i].width +");"+endlx;
lin+=tabl+frm+"->"+key+"->Height=ini.key_geti(sec,\"YS_"+key+"\","+cmp[i].height+");"+endlx;
*/
}
if (int(p&_frm_cmp_prop_check )!=0) lin+=tabl+frm+"->"+key+"->Checked =ini.key_geti(sec,\"CHK_"+key+"\","+cmp[i].checked +");"+endlx;
if (int(p&_frm_cmp_prop_down )!=0) lin+=tabl+frm+"->"+key+"->Down =ini.key_geti(sec,\"DWN_"+key+"\","+cmp[i].down +");"+endlx;
if (int(p&_frm_cmp_prop_pos )!=0) lin+=tabl+frm+"->"+key+"->Position=ini.key_geti(sec,\"POS_"+key+"\","+cmp[i].position+");"+endlx;
cpp+=lin;
}
if (end)
{
lin+=tabl+"ini.close();"+endlx;
lin+=tabl+"break;"+endlx;
lin+=tabl+"}"+endl;
cpp+=lin;
}
txt+=cpp;
}
//---------------------------------------------------------------------------
void frm_decode::save_exit_cpp(AnsiString &txt,bool end)
{
int i,p;
AnsiString cpp,lin,key,frm,tabl,endl,endlx;
tabl=char(9); endl=char(13); endl+=char(10); endlx="\\"+endl;
cpp="";
if (txt=="")
{
lin+="//---------------------------------------------------------------------------"+endl;
lin+="#define _def_program_exit(name) for(;;)"+endlx;
lin+=tabl+"{"+endlx;
lin+=tabl+"int sec;"+endlx;
lin+=tabl+"inifile ini;"+endlx;
lin+=tabl+"ini.open(name);"+endlx;
cpp+=lin;
}
frm=""; for (i=2;i<=form.Length();i++) frm+=form[i];
cpp+=tabl+"sec=ini.sec_getid(\""+form+"\",true);"+endlx;
for (i=0;i<cmp.num;i++)
{
if (cmp[i].action) continue;
key=cmp[i].name;
p=cmp[i].prop;
lin="";
if (cmp[i].align=="alLeft") lin+=tabl+"ini.key_set(sec,\"XS_"+key+"\","+frm+"->"+key+"->Width );"+endlx;
if (cmp[i].align=="alRight") lin+=tabl+"ini.key_set(sec,\"XS_"+key+"\","+frm+"->"+key+"->Width );"+endlx;
if (cmp[i].align=="alTop") lin+=tabl+"ini.key_set(sec,\"YS_"+key+"\","+frm+"->"+key+"->Height);"+endlx;
if (cmp[i].align=="alBottom") lin+=tabl+"ini.key_set(sec,\"YS_"+key+"\","+frm+"->"+key+"->Height);"+endlx;
if (cmp[i].align=="alClient")
{
}
if (cmp[i].align=="alNone")
{
/*
lin+=tabl+"ini.key_set(sec,\"X0_"+key+"\","+frm+"->"+key+"->Left );"+endlx;
lin+=tabl+"ini.key_set(sec,\"Y0_"+key+"\","+frm+"->"+key+"->Top );"+endlx;
lin+=tabl+"ini.key_set(sec,\"XS_"+key+"\","+frm+"->"+key+"->Width );"+endlx;
lin+=tabl+"ini.key_set(sec,\"YS_"+key+"\","+frm+"->"+key+"->Height);"+endlx;
*/
}
if (int(p&_frm_cmp_prop_check )!=0) lin+=tabl+"ini.key_set(sec,\"CHK_"+key+"\",int("+frm+"->"+key+"->Checked));"+endlx;
if (int(p&_frm_cmp_prop_down )!=0) lin+=tabl+"ini.key_set(sec,\"DWN_"+key+"\",int("+frm+"->"+key+"->Down ));"+endlx;
if (int(p&_frm_cmp_prop_pos )!=0) lin+=tabl+"ini.key_set(sec,\"POS_"+key+"\", "+frm+"->"+key+"->Position);"+endlx;
cpp+=lin;
}
if (end)
{
lin+=tabl+"ini.save(name);"+endlx;
lin+=tabl+"ini.close();"+endlx;
lin+=tabl+"break;"+endlx;
lin+=tabl+"}"+endl;
lin+="//---------------------------------------------------------------------------"+endl;
cpp+=lin;
}
txt+=cpp;
}
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------

How to notify ListView that DataModel has changed

I created a ListView and I want to use it with a custom DataModel. However, I have a problem: at the moment the view gets created, I don't have the data loaded into the model. The model data is set after the view is created and when I set the data onto the model, the view doesn't update and doesn't read again the model data. This is my ListView:
ListViewCountainer.qml
Container {
// countryModelData is set after ListViewCountainer gets created
// when countryModelData gets set, the model is populated with data
property variant countryModelData
leftPadding: 20.0
rightPadding: 20.0
topPadding: 20.0
bottomPadding: 20.0
CountryDetailsListView {
id: countryDetailsListView
dataModel: CountryDataModel {
countryData: countryModelData
}
}
}
And here is my model:
countrydatamodel.h
#ifndef COUNTRYDATAMODEL_H_
#define COUNTRYDATAMODEL_H_
#include <QtCore/QAbstractListModel>
#include <QtCore/QList>
#include <QObject>
#include <QtCore/QVariant>
#include <bb/cascades/DataModel>
#include <bb/data/JsonDataAccess>
class CountryDataModel : public bb::cascades::DataModel
{
Q_OBJECT
Q_PROPERTY(QVariant countryData READ getCountryData WRITE setCountryData)
public:
CountryDataModel(QObject* parent = 0);
virtual ~CountryDataModel();
Q_INVOKABLE int childCount(const QVariantList& indexPath);
Q_INVOKABLE QVariant data(const QVariantList& indexPath);
Q_INVOKABLE bool hasChildren(const QVariantList& indexPath);
Q_INVOKABLE QString itemType(const QVariantList& indexPath);
Q_INVOKABLE void removeItem(const QVariantList& indexPath);
Q_INVOKABLE QVariant getCountryData();
Q_INVOKABLE void setCountryData(QVariant data);
private:
QVariantList m_elements;
};
#endif /* COUNTRYDATAMODEL_H_ */
countrydatamodel.cpp
#include <src/countrydatamodel.h>
#include <QtCore/QtAlgorithms>
#include <QtCore/QDebug>
#include <bb/cascades/DataModel>
#include <bb/data/JsonDataAccess>
CountryDataModel::CountryDataModel(QObject* parent) : bb::cascades::DataModel(parent)
{
}
CountryDataModel::~CountryDataModel()
{
}
bool CountryDataModel::hasChildren(const QVariantList &indexPath)
{
qDebug() << "==== CountryDataModel::hasChildren" << indexPath;
if ((indexPath.size() == 0))
{
return true;
}
else
{
return false;
}
}
int CountryDataModel::childCount(const QVariantList &indexPath)
{
qDebug() << "==== CountryDataModel::childCount" << indexPath;
if (indexPath.size() == 0)
{
qDebug() << "CountryDataModel::childCount" << m_elements.size();
return m_elements.size();
}
qDebug() << "==== CountryDataModel::childCount" << 0;
return 0;
}
QVariant CountryDataModel::data(const QVariantList &indexPath)
{
qDebug() << "==== CountryDataModel::data" << indexPath;
if (indexPath.size() == 1) {
return m_elements.at(indexPath.at(0).toInt());
}
QVariant v;
return v;
}
QString CountryDataModel::itemType(const QVariantList &indexPath)
{
Q_UNUSED(indexPath);
return "";
}
void CountryDataModel::removeItem(const QVariantList& indexPath)
{
if(indexPath.size() == 1) {
m_elements.removeAt(indexPath.at(0).toInt());
}
emit itemRemoved(indexPath);
}
QVariant CountryDataModel::getCountryData()
{
return QVariant(m_elements);
}
void CountryDataModel::setCountryData(QVariant data)
{
m_elements = data.toList();
qDebug() << "================== CountryDataModel: " << m_elements;
}
I put some debug messages in the childCount function for example and it gets called only once, which means that the ListView asks the model for the data just once, when the model is created.
Is it possible to force ListView to read again the data from the model after the model gets populated with data? Or how could I make this approach work and load the data in the view?
Thanks!
In order for the model to be updated, the setCountryData member function needs to be updated like so:
void CountryDataModel::setCountryData(QVariant data)
{
m_elements = data.toList();
emit itemsChanged(bb::cascades::DataModelChangeType::AddRemove, QSharedPointer< bb::cascades::DataModel::IndexMapper >(0));
}
FML...
You need to declare a signal for the property you want to update in backend.
Q_PROPERTY(QVariant countryData READ getCountryData WRITE setCountryData NOTIFY contryDataChanged)
add its declaration as well.
Then you say -
emit contryDataChanged();
wherever you feel like list should re-read contents. (normally setter methods).

Qml define custom property group [duplicate]

This question already has answers here:
How to create grouped/nested properties?
(3 answers)
Closed 7 years ago.
How can I define custom property groups in qml, equal to the anchors property?
Example usage:
Item {
anchors {
leftMargin: 5
rightMargin: 5
}
}
I'm afraid this is not as simple as you are thinking it might be.
As far as I know, you have two options:
1.- Follow the recommendation you will see in the link provided by #BaCaRoZzo and implement your own object type.
2.- Write a more complex QML type in C++ and use it in your QML file. More or less the idea pointed out by #folibis. Example:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "customitem.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<CustomItem>("CustomItem", 1,0, "CustomItem");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
customitem.h
#ifndef CUSTOMITEM_H
#define CUSTOMITEM_H
#include <QObject>
class CustomItem: public QObject
{
Q_OBJECT
/*
* Any property that is writable should have an associated NOTIFY signal.
* Ref: http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#exposing-properties
*/
Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged)
Q_PROPERTY(int y READ y WRITE setY NOTIFY yChanged)
public:
CustomItem(QObject *parent = 0);
int x() const;
void setX(int);
int y() const;
void setY(int);
private:
int m_x;
int m_y;
signals:
void xChanged();
void yChanged();
public slots:
};
#endif // CUSTOMITEM_H
customitem.cpp
#include "customitem.h"
CustomItem::CustomItem(QObject *parent)
: QObject(parent), m_x(0), m_y(0)
{
}
int CustomItem::x() const
{
return m_x;
}
void CustomItem::setX(int x)
{
m_x = x;
}
int CustomItem::y() const
{
return m_y;
}
void CustomItem::setY(int y)
{
m_y = y;
}
main.qml
import QtQuick 2.5
import QtQuick.Window 2.2
import CustomItem 1.0
Window {
visible: true
CustomItem {
id: customItem
x: 50
y: 50
}
Rectangle {
id: rect
x: customItem.x
y: customItem.y
width: 100; height: 100
color: "red"
}
}

D3D D2D Interop on Windows 7

I'm trying to use DWrite to draw text in my dx11 app but I'm having lot of problems, I shared resources beetween D3D10 Device and D3D11 Device because dx10 is capable to connect D3D with D2D, that's the code of my GraphicsDevice:
// File: GraphicsDevice.h
#pragma once
#ifndef _GRAPHICSDEVICE_H_
#define _GRAPHICSDEVICE_H_
#ifndef _DEFINES_H_
#include "Defines.h"
#endif
#ifndef _COLOR_H_
#include "Color.h"
#endif
#ifndef _UTILITIES_H_
#include "Utilities.h"
#endif
#ifndef _DWRITE_H_
#include "DWrite.h"
#endif
// Forward declaration
namespace BSGameFramework { ref class Game; }
using namespace BSGameFramework;
using namespace System;
namespace BSGameFramework
{
namespace Graphics
{
public ref class GraphicsDevice
{
public:
/// <summary>GraphicsDevice constructor.</summary>
/// <param name="game">The game wich the device has to work.</param>
GraphicsDevice(Game^ game);
virtual ~GraphicsDevice();
/// <summary>Clear the screen.</summary>
/// <param name="color">The color that the screen background will assume after clearing.</param>
void Clear(Color color);
/// <summary>Render on the screen.</summary>
void Render();
/// <summary>Set the full screen state.</summary>
void FullScreen(bool isFullScreen);
property Color BlendFactor
{
Color get()
{
return blendFactor_;
}
void set(Color blendFactor_)
{
blendFactor_ = BlendFactor;
}
}
property D3D_DRIVER_TYPE DriverType
{
D3D_DRIVER_TYPE get()
{
return driverType_;
}
}
property D3D_FEATURE_LEVEL FeatureLevel
{
D3D_FEATURE_LEVEL get()
{
return featureLevel_;
}
}
property ID3D11Device* D3DDevice
{
ID3D11Device* get()
{
return d3dDevice_;
}
}
property ID3D11DeviceContext* D3DContext
{
ID3D11DeviceContext* get()
{
return d3dContext_;
}
}
property ID3D10Device1* D3DDevice10_1
{
ID3D10Device1* get()
{
return d3dDevice10_1_;
}
}
property ID2D1Factory* D2DFactory
{
ID2D1Factory* get()
{
return d2dFactory_;
}
}
property ID2D1RenderTarget* D2DRenderTarget
{
ID2D1RenderTarget* get()
{
return d2dRenderTarget_;
}
}
property ID2D1SolidColorBrush* D2DSolidColorBrush
{
ID2D1SolidColorBrush* get()
{
return d2dSolidColorBrush_;
}
}
property IDWriteFactory* DWriteFactory
{
IDWriteFactory* get()
{
return dWriteFactory_;
}
}
property int WindowWidth
{
int get()
{
return GetWindowWidth();
}
}
property int WindowHeight
{
int get()
{
return GetWindowHeight();
}
}
property HWND Window
{
HWND get()
{
return GetWindow();
}
}
property int SafeTitleArea
{
int get()
{
return safeTitleArea_;
}
void set(int safeTitleArea)
{
safeTitleArea_ = safeTitleArea;
}
}
private:
void CreateD3D11Resources();
void CreateD3D10Resources(ID3D11Texture2D* d3d11Texture);
void CreateD2D1Resources(ID3D10Texture2D* d3d10Texture);
Game^ game_;
Color blendFactor_;
D3D_DRIVER_TYPE driverType_;
D3D_FEATURE_LEVEL featureLevel_;
int safeTitleArea_;
int GetWindowWidth();
int GetWindowHeight();
HWND GetWindow();
// Direct3D 11
ID3D11Device* d3dDevice_;
ID3D11DeviceContext* d3dContext_;
// Direct3D 10
ID3D10Device1* d3dDevice10_1_;
// Direct2D
ID2D1Factory* d2dFactory_;
ID2D1RenderTarget* d2dRenderTarget_;
ID2D1SolidColorBrush* d2dSolidColorBrush_;
// DirectWrite
IDWriteFactory* dWriteFactory_;
IDXGISwapChain* swapChain_;
ID3D11RenderTargetView* backBufferTarget_;
};
}
}
#endif
// FILE: GraphicsDevice.cpp
#include "GraphicsDevice.h"
#include "Game.h"
#include "GraphicsDeviceNativeWrapper.h"
using namespace BSGameFramework::Graphics;
using namespace BSGameFramework;
inline GraphicsDevice::GraphicsDevice(Game^ game) : driverType_( D3D_DRIVER_TYPE_NULL ), featureLevel_( D3D_FEATURE_LEVEL_11_0 ),
d3dDevice_( 0 ), d3dContext_( 0 ), swapChain_( 0 ), backBufferTarget_( 0 )
{
game_ = game;
BlendFactor = Color::White;
CreateD3D11Resources();
}
inline GraphicsDevice::~GraphicsDevice()
{
if (backBufferTarget_)
{
backBufferTarget_->Release();
}
if (swapChain_)
{
swapChain_->Release();
}
if (d3dContext_)
{
d3dContext_->Release();
}
if (d3dDevice_)
{
d3dDevice_->Release();
}
backBufferTarget_ = 0;
swapChain_ = 0;
d3dContext_ = 0;
d3dDevice_ = 0;
}
inline void GraphicsDevice::Clear(Color color)
{
if (d3dContext_ == 0)
{
return;
}
float clearColor[4];
Vec4 convertedColor = Utilities::ColorToVec4(color);
clearColor[0] = convertedColor.values[0];
clearColor[1] = convertedColor.values[1];
clearColor[2] = convertedColor.values[2];
clearColor[3] = convertedColor.values[3];
d3dContext_->ClearRenderTargetView(backBufferTarget_, clearColor);
}
inline void GraphicsDevice::Render()
{
swapChain_->Present(0, 0);
}
inline void GraphicsDevice::FullScreen(bool isFullScreen)
{
swapChain_->SetFullscreenState(isFullScreen, NULL);
}
inline int GraphicsDevice::GetWindowWidth()
{
return game_->WindowWidth;
}
inline int GraphicsDevice::GetWindowHeight()
{
return game_->WindowHeight;
}
inline HWND GraphicsDevice::GetWindow()
{
return game_->Window;
}
#pragma region CreateD3D11Resources
inline void GraphicsDevice::CreateD3D11Resources()
{
HRESULT result;
RECT dimensions;
GetClientRect(Window, &dimensions);
unsigned int width = dimensions.right - dimensions.left;
unsigned int height = dimensions.bottom - dimensions.top;
D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
D3D_DRIVER_TYPE_SOFTWARE
};
unsigned int totalDriverTypes = ARRAYSIZE(driverTypes);
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
};
unsigned int totalFeatureLevels = ARRAYSIZE(featureLevels);
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
swapChainDesc.BufferCount = 2;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = game_->Window;
swapChainDesc.Windowed = true;
swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
unsigned int creationFlags = 0;
#ifdef _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
unsigned int driver = 0;
pin_ptr<IDXGISwapChain*> swapChainPointer;
swapChainPointer = &swapChain_;
pin_ptr<ID3D11Device*> d3dDevicePointer;
d3dDevicePointer = &d3dDevice_;
pin_ptr<D3D_FEATURE_LEVEL> featureLevelPointer;
featureLevelPointer = &featureLevel_;
pin_ptr<ID3D11DeviceContext*> d3dContextPointer;
d3dContextPointer = &d3dContext_;
for (driver = 0; driver < totalDriverTypes; ++driver)
{
result = D3D11CreateDeviceAndSwapChain(0, driverTypes[driver], 0, creationFlags, featureLevels, totalFeatureLevels,
D3D11_SDK_VERSION, &swapChainDesc, swapChainPointer,
d3dDevicePointer, featureLevelPointer, d3dContextPointer);
if (SUCCEEDED(result))
{
driverType_ = driverTypes[driver];
break;
}
}
if (FAILED(result))
{
DXTRACE_MSG("Failed to create the Direct3D device!");
return;
}
ID3D11Texture2D* backBufferTexture;
result = swapChain_->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferTexture);
if (FAILED(result))
{
DXTRACE_MSG("Failed to get the swap chain back buffer!");
return;
}
pin_ptr<ID3D11RenderTargetView*> backBufferTargetPointer;
backBufferTargetPointer = &backBufferTarget_;
result = d3dDevice_->CreateRenderTargetView(backBufferTexture, 0, backBufferTargetPointer);
if (FAILED(result))
{
DXTRACE_MSG("Failed to create the render target view!");
return;
}
d3dContext_->OMSetRenderTargets(1, backBufferTargetPointer, 0);
D3D11_VIEWPORT viewport;
viewport.Width = static_cast<float>(width);
viewport.Height = static_cast<float>(height);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;
d3dContext_->RSSetViewports(1, &viewport);
CreateD3D10Resources(backBufferTexture);
}
#pragma endregion
#pragma region CreateD3D10Resources
inline void GraphicsDevice::CreateD3D10Resources(ID3D11Texture2D* d3d11Texture)
{
//Load D3D10.DLL
HMODULE d3d10_1 = LoadLibrary("D3D10_1.dll");
// Get adapter of the current D3D11 device. Our D3D10 will run on the same adapter.
IDXGIDevice* dxgiDevice;
IDXGIAdapter* dxgiAdapter;
d3dDevice_->QueryInterface<IDXGIDevice>(&dxgiDevice);
dxgiDevice->GetAdapter(&dxgiAdapter);
SafeRelease<IDXGIDevice>(&dxgiDevice);
//Get address of the function D3D10CreateDevice1 dynamically.
typedef HRESULT (WINAPI* FN_D3D10CreateDevice1)(
IDXGIAdapter *pAdapter, D3D10_DRIVER_TYPE DriverType, HMODULE Software,
UINT Flags, D3D10_FEATURE_LEVEL1 HardwareLevel, UINT SDKVersion, ID3D10Device1 **ppDevice );
FN_D3D10CreateDevice1 fnCreate = (FN_D3D10CreateDevice1)GetProcAddress(d3d10_1, "D3D10CreateDevice1");
//Call D3D10CreateDevice1 dynamically.
pin_ptr<ID3D10Device1*> d3dDevice10_1Ptr = &d3dDevice10_1_;
fnCreate(dxgiAdapter, D3D10_DRIVER_TYPE_HARDWARE, NULL, D3D10_CREATE_DEVICE_BGRA_SUPPORT | D3D10_CREATE_DEVICE_DEBUG, D3D10_FEATURE_LEVEL_10_1, D3D10_1_SDK_VERSION, d3dDevice10_1Ptr);
//Create a D3D10.1 render target texture and share it with our D3D11.
D3D10_TEXTURE2D_DESC tDesc;
tDesc.Width = game_->WindowWidth;
tDesc.Height = game_->WindowHeight;
tDesc.MipLevels = 1;
tDesc.ArraySize = 1;
tDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
tDesc.SampleDesc.Count = 1;
tDesc.SampleDesc.Quality = 0;
tDesc.Usage = D3D10_USAGE_DEFAULT;
//EVEN IF YOU WON'T USE AS SHADER RESOURCE, SET THIS BIND FLAGS:
tDesc.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE;
tDesc.CPUAccessFlags = 0;
tDesc.MiscFlags = D3D10_RESOURCE_MISC_SHARED;
//Create the RT texture on D3D10
ID3D10Texture2D* texture;
d3dDevice10_1_->CreateTexture2D(&tDesc, NULL, &texture);
//Get DXGI Resource and retrieve the sharing handle.
IDXGISurface* dxgiSurface;
IDXGIResource* dxgiResource;
HANDLE shareHandle;
texture->QueryInterface<IDXGISurface>(&dxgiSurface);
dxgiSurface->QueryInterface<IDXGIResource>(&dxgiResource);
dxgiResource->GetSharedHandle(&shareHandle);
SafeRelease(&dxgiResource);
SafeRelease(&dxgiSurface);
//Call D3D 11 to open shared resource.
ID3D11Resource* d3d11Resource;
d3dDevice_->OpenSharedResource(shareHandle, __uuidof(ID3D11Resource), (void**)&d3d11Resource);
d3d11Resource->QueryInterface<ID3D11Texture2D>(&d3d11Texture);
SafeRelease(&d3d11Resource);
if (d3d11Texture)
{
d3d11Texture->Release();
}
CreateD2D1Resources(texture);
}
#pragma endregion
#pragma region CreateD2D1Resources
inline void GraphicsDevice::CreateD2D1Resources(ID3D10Texture2D* d3d10Texture)
{
pin_ptr<ID2D1Factory*> d2dFactoryPtr = &d2dFactory_;
pin_ptr<IDWriteFactory*> dWriteFactoryPtr = &dWriteFactory_;
//pin_ptr<ID2D1HwndRenderTarget*> d2dRenderTargetPtr = &d2dRenderTarget_;
//pin_ptr<ID2D1SolidColorBrush*> D2DSolidColorBrushPtr = &d2dSolidColorBrush_;
GraphicsDeviceNativeWrapper::CreateFactories(Window, d2dFactoryPtr, dWriteFactoryPtr);
//Get DXGI Surface from the created render target.
IDXGISurface1* pRT10;
d3d10Texture->QueryInterface<IDXGISurface1>(&pRT10);
FLOAT dpiX;
FLOAT dpiY;
d2dFactory_->GetDesktopDpi(&dpiX, &dpiY);
// Create a DC render target.
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_IGNORE),
static_cast<float>(dpiX),
static_cast<float>(dpiY)
);
// Create a Direct2D render target.
// Assuming m_pD2DFactory was previously created with:
//D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory), NULL,
// (void**)(&m_pD2DFactory));
pin_ptr<ID2D1RenderTarget*> renderTargetPtr = &d2dRenderTarget_;
d2dFactory_->CreateDxgiSurfaceRenderTarget(pRT10, (const D2D1_RENDER_TARGET_PROPERTIES *)&props, renderTargetPtr);
pin_ptr<ID2D1SolidColorBrush*> solidColorBrushPtr = &d2dSolidColorBrush_;
d2dRenderTarget_->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), solidColorBrushPtr);
}
#pragma endregion
// File GraphicsDeviceNativeWrapper.h
#pragma once
#ifndef _GRAPHICSDEVICENATIVEWRAPPER_H_
#define _GRAPHICSDEVICENATIVEWRAPPER_H_
#ifndef _DWRITE_H_
#include "DWrite.h"
#endif
#pragma managed(push, false)
namespace BSGameFramework
{
namespace Graphics
{
class GraphicsDeviceNativeWrapper abstract sealed
{
public:
static void CreateFactories(HWND window, ID2D1Factory** d2dFactory, IDWriteFactory** dWriteFactory/*,ID2D1RenderTarget** d2dRenderTarget, ID2D1SolidColorBrush** d2dSolidColorBrush*/)
{
HRESULT result;
result = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,__uuidof(ID2D1Factory), NULL, (void**)d2dFactory);
if (SUCCEEDED(result))
{
result = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown**>(dWriteFactory));
}
RECT rc;
GetClientRect(window, &rc);
D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);
}
static void CreateTextFormat(const wchar_t* font, IDWriteFactory* factory, IDWriteTextFormat** format)
{
factory->CreateTextFormat(font, NULL, DWRITE_FONT_WEIGHT_REGULAR, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 72.0f, L"en-us", format);
(*format)->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
(*format)->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
}
};
}
}
#pragma managed(pop)
#endif
And this is my SpriteBatch:
// File: SpriteBatch.h
#pragma once
#ifndef _SPRITEBATCH_H_
#define _SPRITEBATCH_H_
#ifndef _DEFINES_H_
#include "Defines.h"
#endif
#ifndef _GRAPHICRESOURCE_H_
#include "GraphicResource.h"
#endif
#ifndef _TEXTURE2D_H_
#include "Texture2D.h"
#endif
#ifndef _GRAPHICSDEVICE_H_
#include "GraphicsDevice.h"
#endif
#ifndef _SPRITESORTMODE_H_
#include "SpriteSortMode.h"
#endif
#ifndef _BLENDSTATE_H_
#include "BlendState.h"
#endif
#ifndef _NATIVESPRITEBATCH_H_
#include "NativeSpritebatch.h"
#endif
#ifndef _SPRITEEFFECT_H_
#include "SpriteEffect.h"
#endif
#ifndef _IDRAWABLECOMPONENT_H_
#include "IDrawableComponent.h"
#endif
#ifndef _SPRITEFONT_H_
#include "SpriteFont.h"
#endif
using namespace BSGameFramework::GameBase;
namespace BSGameFramework
{
namespace Graphics
{
public ref class SpriteBatch : GraphicResource
{
public:
SpriteBatch(GraphicsDevice^ graphicsDevice);
~SpriteBatch();
void Begin();
void Begin(SpriteSortMode sortMode, BlendState^ blendState);
void Draw(IDrawableComponent^ component);
void DrawString(SpriteFont^ font, System::String^ text, Vector2 position);
void End();
private:
bool CompileD3DShader(char* filePath, char* entry, char* shaderModel, ID3DBlob** buffer);
void SortByDepth();
SpriteSortMode sortMode_;
BlendState ^blendState_;
System::Collections::Generic::List<IDrawableComponent^>^ componentList_;
bool beginInvoked_;
ID3D11VertexShader* solidColorVS_;
ID3D11PixelShader* solidColorPS_;
ID3D11InputLayout* inputLayout_;
ID3D11Buffer* vertexBuffer_;
ID3D11BlendState* alphaBlendState_;
NativeSpritebatch* spriteBatch;
};
}
}
#endif
// File: SpriteBatch.cpp
#include "SpriteBatch.h"
#ifndef _SPRITEBATCHBEGINENDEXCEPTION_H_
#include "SpriteBatchBeginEndException.h"
#endif
using namespace BSGameFramework::Graphics;
using namespace BSGameFramework::Exception;
inline SpriteBatch::SpriteBatch(GraphicsDevice^ graphicsDevice) : alphaBlendState_( 0 )
{
graphicDevice_ = graphicsDevice;
sortMode_ = SpriteSortMode::Deferred;
blendState_ = BlendState::AlphaBlend;
// ID3DBlob contiene un puntatore ad un dato di lunghezza qualsiasi, GetBufferPointer restituisce il puntatore e GetBufferSize la grandezza
ID3DBlob* vsBuffer = 0;
// Compila lo shader e salva il risultato nel buffer
bool compileResult = CompileD3DShader("TextureMap.fx", "VS_Main", "vs_4_0", &vsBuffer);
if (compileResult == false)
{
DXTRACE_MSG("Error compiling the vertex shader!");
return;
}
HRESULT d3dResult;
pin_ptr<ID3D11VertexShader*> solidColorVSPointer;
solidColorVSPointer = &solidColorVS_;
// Crea il vertex shader e lo salva in solidColorVS_ di tipo ID3D11VertexShader*
d3dResult = Device->D3DDevice->CreateVertexShader(vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), 0, solidColorVSPointer);
if (FAILED(d3dResult))
{
DXTRACE_MSG("Error creating the vertex shader!");
if (vsBuffer)
{
vsBuffer->Release();
}
return;
}
D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
unsigned int totalLayoutElements = ARRAYSIZE(solidColorLayout);
pin_ptr<ID3D11InputLayout*> inputLayoutPointer;
inputLayoutPointer = &inputLayout_;
// Crea l'input layout e lo salva in inputLayout di tipo ID3D11InputLayout*
d3dResult = Device->D3DDevice->CreateInputLayout(solidColorLayout, totalLayoutElements, vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), inputLayoutPointer);
vsBuffer->Release();
if (FAILED(d3dResult))
{
DXTRACE_MSG("Error creating the input layout!");
return;
}
ID3DBlob* psBuffer = 0;
// Compila il pixel shader e salva il risultato in psBuffer
compileResult = CompileD3DShader("TextureMap.fx", "PS_Main", "ps_4_0", &psBuffer);
if (compileResult == false)
{
DXTRACE_MSG("Error compiling pixel shader!");
return;
}
pin_ptr<ID3D11PixelShader*> solidColorPSPointer;
solidColorPSPointer = &solidColorPS_;
// Crea il pixel shader e lo salva in solidColorPS_ di tipo ID3D11PixelShader*
d3dResult = Device->D3DDevice->CreatePixelShader(psBuffer->GetBufferPointer(), psBuffer->GetBufferSize(), 0, solidColorPSPointer);;
psBuffer->Release();
if (FAILED(d3dResult))
{
DXTRACE_MSG("Error creating pixel shader!");
return;
}
spriteBatch = new NativeSpritebatch(Device->D3DDevice);
// Spostare nel Begin successivamente
D3D11_BLEND_DESC blendDesc;
ZeroMemory(&blendDesc, sizeof(D3D11_BLEND_DESC));
blendDesc.AlphaToCoverageEnable = FALSE;
blendDesc.IndependentBlendEnable = FALSE;
blendDesc.RenderTarget[0].BlendEnable = TRUE;
blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_DEST_ALPHA;
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
pin_ptr<ID3D11BlendState*> alphaBlendStatePointer;
alphaBlendStatePointer = &alphaBlendState_;
Device->D3DDevice->CreateBlendState(&blendDesc, alphaBlendStatePointer);
Device->D3DContext->OMSetBlendState(alphaBlendState_, blendFactor, 0xFFFFFFFF);
}
inline SpriteBatch::~SpriteBatch()
{
}
inline void SpriteBatch::Begin()
{
if (beginInvoked_)
{
throw gcnew SpriteBatchBeginEndException("Beetween two SpriteBatch begin methods you must call SpriteBacth End method!");
}
beginInvoked_ = true;
Device->D2DRenderTarget->BeginDraw();
Device->D2DRenderTarget->SetTransform(D2D1::IdentityMatrix());
if (componentList_ == nullptr)
{
componentList_ = gcnew System::Collections::Generic::List<IDrawableComponent^>();
}
}
inline void SpriteBatch::Begin(SpriteSortMode sortMode, BlendState^ blendState)
{
Begin();
sortMode_ = sortMode;
}
inline void SpriteBatch::Draw(IDrawableComponent^ component)
{
if (component == nullptr)
{
throw gcnew ArgumentNullException("Component argument is null, please ensure to initialize all components correctly!");
}
else
{
componentList_->Add(component);
}
}
inline void SpriteBatch::DrawString(SpriteFont^ font, System::String^ text, Vector2 position)
{
RECT rc;
GetClientRect(Device->Window, &rc);
// Create a D2D rect that is the same size as the window.
D2D1_RECT_F layoutRect = D2D1::RectF(
static_cast<FLOAT>(rc.left) / font->DpiScaleX,
static_cast<FLOAT>(rc.top) / font->DpiScaleY,
static_cast<FLOAT>(rc.right - rc.left) / font->DpiScaleX,
static_cast<FLOAT>(rc.bottom - rc.top) / font->DpiScaleY
);
// Use the DrawText method of the D2D render target interface to draw.
WCHAR textUnicode = Utilities::StringToWCHAR(text);
UINT32 cTextLength_ = (UINT32) wcslen(&textUnicode);
Device->D2DSolidColorBrush->SetColor(D2D1::ColorF(0,0,0,1));
Device->D2DSolidColorBrush->SetColor(D2D1::ColorF(255, 255, 255, 255));
Device->D2DRenderTarget->DrawText(&textUnicode, cTextLength_, font->DWriteTextFormat, layoutRect, Device->D2DSolidColorBrush);
}
inline void SpriteBatch::End()
{
if (componentList_->Count)
{
if (sortMode_ == SpriteSortMode::BackToFront)
{
SortByDepth();
}
for (int i = 0; i < componentList_->Count; i++)
{
Texture* text = componentList_[i]->Texture->TextureInfo;
unsigned int stride = sizeof(VertexPos);
unsigned int offset = 0;
Device->D3DContext->IASetInputLayout(inputLayout_);
if (componentList_[i]->Effect != SpriteEffect::None)
{
ID3D11Buffer* vertexBuffer;
float width = (float)text->textureDesc_.Width;
float height = (float)text->textureDesc_.Height;
D3D11_BUFFER_DESC vertexDesc;
ZeroMemory(&vertexDesc, sizeof(vertexDesc));
vertexDesc.Usage = D3D11_USAGE_DYNAMIC;
vertexDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexDesc.ByteWidth = sizeof(VertexPos) * 6;
D3D11_SUBRESOURCE_DATA resourceData;
ZeroMemory(&resourceData, sizeof(resourceData));
pin_ptr<ID3D11Buffer*> vertexBufferPointer;
vertexBufferPointer = &vertexBuffer;
switch (componentList_[i]->Effect)
{
case BSGameFramework::Graphics::SpriteEffect::FlipHorizontally:
{
VertexPos verticesOne[] =
{
{ XMFLOAT3(width, height, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(width, 0.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(0.0f, 0.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(0.0f, 0.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(0.0f, height, 1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(width, height, 1.0f), XMFLOAT2(0.0f, 0.0f) },
};
resourceData.pSysMem = verticesOne;
Device->D3DDevice->CreateBuffer(&vertexDesc, &resourceData, vertexBufferPointer);
Device->D3DContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
break;
}
case BSGameFramework::Graphics::SpriteEffect::FlipVertically:
{
VertexPos verticesTwo[] =
{
{ XMFLOAT3(width, height, 1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(width, 0.0f, 1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(0.0f, 0.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(0.0f, 0.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(0.0f, height, 1.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(width, height, 1.0f), XMFLOAT2(1.0f, 1.0f) },
};
resourceData.pSysMem = verticesTwo;
Device->D3DDevice->CreateBuffer(&vertexDesc, &resourceData, vertexBufferPointer);
Device->D3DContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
break;
}
}
}
else
{
Device->D3DContext->IASetVertexBuffers(0, 1, &text->vertexBuffer_, &stride, &offset);
}
Device->D3DContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
Device->D3DContext->VSSetShader(solidColorVS_, 0, 0);
Device->D3DContext->PSSetShader(solidColorPS_, 0, 0);
Device->D3DContext->PSSetShaderResources(0, 1, &text->colorMap_);
Device->D3DContext->PSSetSamplers(0, 1, &text->colorMapSampler_);
spriteBatch->SetTranspose(Device->D3DContext, text, Device->WindowWidth, Device->WindowHeight, componentList_[i]->Position.X, componentList_[i]->Position.Y,
componentList_[i]->Scale.X, componentList_[i]->Scale.Y, componentList_[i]->Rotation);
Device->D3DContext->Draw(6, 0);
}
}
Device->D2DRenderTarget->EndDraw();
componentList_->Clear();
beginInvoked_ = false;
sortMode_ = SpriteSortMode::Deferred;
}
inline bool SpriteBatch::CompileD3DShader(char* filePath, char* entry, char* shaderModel, ID3DBlob** buffer)
{
DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined(DEBUG) || defined(_DEBUG)
shaderFlags |= D3DCOMPILE_DEBUG;
#endif
ID3DBlob* errorBuffer = 0;
HRESULT result;
result = D3DX11CompileFromFile(filePath, 0, 0, entry, shaderModel, shaderFlags, 0, 0, buffer, &errorBuffer, 0);
if (FAILED(result))
{
if (errorBuffer != 0)
{
OutputDebugStringA((char*)errorBuffer->GetBufferPointer());
errorBuffer->Release();
}
return false;
}
if (errorBuffer != 0)
{
errorBuffer->Release();
}
return true;
}
inline void SpriteBatch::SortByDepth()
{
for (int i = 0; i < componentList_->Count - 1; i++)
{
for (int j = 1; j < componentList_->Count; j++)
{
if (componentList_[i]->ZIndex < componentList_[j]->ZIndex)
{
IDrawableComponent^ component = componentList_[i];
componentList_[i] = componentList_[j];
componentList_[j] = component;
}
}
}
}
When I call SpriteBatch.DrawString(...) from my C# app I'm not obtaining nothing written on my screen, can please somebody explaining me what's I'm missing? I'm new on DirectX programming so please be quiet with me xD Thanks!
There's a lot of code you posted and I didn't read it all, but I was trying to do the same thing (successfully) a few days ago and I might have some suggestions.
Overall, it looks like you do this more or less the same way I do. What I did different it that I created the shared texture using D3D11 device, but this should make no difference. The second thing, which I think could be the problem, is that you're not synchronizing the texture resource between D3D10 and D3D11.
When I tried to use such resources without synchronization, D2D was only rendering stuff once in like 50 calls, the rest just had no effect. I think I was continously blocking the resource when rendering it with D3D11.
You should create the texture using
tDesc.MiscFlags = D3D10_RESOURCE_MISC_SHARED_KEYEDMUTEX;
instead. Here are the details about this flag: MSDN. Basically, you query two IDXGIKeyedMutex interfaces from the shared texture objects on both D3D10 and D3D11 devices. You lock the D3D10 mutex when you draw your D2D stuff to the texture. You lock the D3D11 mutex when you want to use this texture in D3D11. You do this using IDXGIKeyedMutex::AcquireSync and IDXGIKeyedMutex::ReleaseSync. Just be sure to pass the same Key value to each consecutive D3D10 Release -> D3D11 Acquire and D3D11 Release -> D3D10 Acquire calls.
Oh, one more little, not important thing, I noticed this in your code:
texture->QueryInterface<IDXGISurface>(&dxgiSurface);
dxgiSurface->QueryInterface<IDXGIResource>(&dxgiResource);
I'm not 100% sure, but I think you can get the IDXGIResource interface directly from your texture.

What to do to make "onEntered" working?

I have a custom QDeclarativeItem subclass named Polygon.
I add a MouseArea in it but onEntered or onPressed does not working, or am i expect wrong thing to happen? I can see my polygons on the window but nothing is writing on console onPressed or onEntered.
Here is QML file:
import MyTypes 1.0
import QtQuick 1.0
import Qt 4.7
Item {
id: container
width: 350; height: 250
Polygon {
id: aPolygon
width: 20; height: 20
name: "A simple polygon"
color: "blue"
vertices:[
Point{x:20.0; y:40.0},
Point{x:40.0; y:40.0},
Point{x:40.0; y:20.0},
Point{x:20.0; y:20.0}
]
MouseArea{
anchors.fill: parent
drag.target: aPolygon
drag.axis: Drag.XandYAxis
drag.minimumX: 0
drag.maximumX: container.width - parent.width
drag.minimumY: 0
drag.maximumY: container.height - parent.width
onPressed:console.log("============== ==onPressed")
}
}
Polygon {
id: bPolygon
//anchors.centerIn: parent
width: 20; height: 20
name: "A simple polygon"
color: "blue"
vertices:[
Point{x:60.0; y:80.0},
Point{x:80.0; y:80.0},
Point{x:80.0; y:60.0},
Point{x:60.0; y:60.0}
]
MouseArea{
//hoverEnabled: false
enabled: visible
hoverEnabled: visible
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
onEntered: {
console.log("============== ==onEntered")
}
}
}
}
Thanks for any idea.
Edit:
polygon.cpp
#include "polygon.h"
#include "point.h"
#include <QPainter>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <QGraphicsSceneDragDropEvent>
#include <QFocusEvent>
#include "DeclarativeDragDropEvent.h"
using namespace std;
using namespace Qt;
Polygon::Polygon(QDeclarativeItem *parent)
: QDeclarativeItem(parent)
{
// need to disable this flag to draw inside a QDeclarativeItem
//setFlag(QDeclarativeItem::ItemHasNoContents, false);
setFlags(ItemIsSelectable|ItemIsMovable|ItemIsFocusable);
setAcceptDrops(true);
setAcceptedMouseButtons( Qt::LeftButton );
}
/*void Polygon::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
forceActiveFocus();
}
void Polygon::focusOutEvent(QFocusEvent *event)
{
cout<<"focusout"<<endl;
this->setSelected( false );
}*/
/*QVariant Polygon::itemChange(GraphicsItemChange change, const QVariant &value)
{
return QGraphicsItem::itemChange(change, value);
}*/
/*void Polygon::focusInEvent ( QFocusEvent * event ){
cout<<"focusin"<<endl;
}*/
QRectF Polygon::boundingRect() const{
QVector<QPointF> vPnt=listToVector(m_vertices);
return QPolygonF(vPnt).boundingRect();
}
QPainterPath Polygon::shape () const
{
QPainterPath path;
QVector<QPointF> vPnt=listToVector(m_vertices);
path.addPolygon(QPolygonF(vPnt));
return path;
}
QString Polygon::name() const
{
return m_name;
}
void Polygon::setName(const QString &name)
{
m_name = name;
}
QColor Polygon::color() const
{
return m_color;
}
void Polygon::setColor(const QColor &color)
{
m_color = color;
}
void Polygon::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
QPen pen(m_color, 2);
painter->setPen(pen);
painter->setRenderHints(QPainter::Antialiasing, true);
QVector<QPointF> vPnt=listToVector(m_vertices);
painter->setBrush(QBrush(m_color,Qt::SolidPattern));
painter-> drawPolygon(QPolygonF(vPnt),Qt::OddEvenFill);
}
QVector<QPointF> Polygon:: listToVector(QList<Point *> lpnt) const{
QVector<QPointF> vPnt;
for(int i=0;i<lpnt.length();i++){
vPnt.append(QPointF(lpnt.at(i)->x(),lpnt.at(i)->y()));
}
return vPnt;
}
QDeclarativeListProperty<Point> Polygon::vertices()
{
return QDeclarativeListProperty<Point>(this, 0, &Polygon::append_vertex);
}
void Polygon::append_vertex(QDeclarativeListProperty<Point> *list, Point *vertex)
{
Polygon *polygon = qobject_cast<Polygon *>(list->object);
if (polygon) {
vertex->setParentItem(polygon);
polygon->m_vertices.append(vertex);
}
}
I believe you should set hoverEnabled to true, or is this intentionally set to visible? visible is a boolean property which might be false for a MouseArea.
MouseArea {
hoverEnabled: true
}
The same goes for enabled: visible