Please refer to http://tieba.baidu.com/f?kz=1508964881
Follow the above URL tutorial, download the Three Kingdoms to kill the source, Swig tools, and download the latest QT4.8.2 for vs2008. I have installed vs2008 and QT4.7, so download QT4.8.2 and install it directly and click Qt in the Qt menu of vs2008 Options submenu, set the default Qt/win version to 4.8.2. Use vs2008 to open the Qsanguosha.pro project file and convert to QSanguosha.sln. The compiler cannot find the Fmodex.lib file at this time, This file is a library of DirectX sound files. Search the Three Kingdoms to kill the source directory, you can find, directly in the project properties set the Lib search path, add "./lib" to compile successfully.
Analysis of the source code behind the step.
First, start the interface
Start tracing from the main function and find the following code
MainWindow *main_window = new MainWindow;
Sanguosha->setparent (Main_window);
Main_window->show ();
In the constructor for the MainWindow class, create a Connection dialog box and a configuration dialog instance and associate its exec ()/show () slot with the action's triggered signal, show a dialog box when the action is triggered, and associate the signal of the dialog box with the corresponding processing slot function. A line of code is done, the code is simple and efficient.
Connection_dialog = new Connectiondialog (this);
Connect (Ui->actionstart_game, SIGNAL (triggered ()), Connection_dialog, SLOT (EXEC ()));
Connect (Connection_dialog, SIGNAL (Accepted ()), this, SLOT (Startconnection ()));
Config_dialog = new Configdialog (this);
Connect (Ui->actionconfigure, SIGNAL (triggered ()), Config_dialog, SLOT (Show ()));
Connect (Config_dialog, SIGNAL (bg_changed ()), this, SLOT (Changebackground ()));
Connect (UI->ACTIONABOUT_QT, SIGNAL (triggered ()), Qapp, SLOT (ABOUTQT ()));
Then create the boot scene (Start_scene) and create the 10 Start button on the splash screen, save 10 action objects in a qlist, each of which corresponds to the creation of a button (Button class, inheritance and Qgraphicsobject). and added to the boot scenario (start_scene).
Startscene *start_scene = new Startscene;
Qlist<qaction*> actions;
Actions << Ui->actionstart_game
<< Ui->actionstart_server
<< Ui->actionpc_console_start
<< Ui->actionreplay
<< ui->actionconfigure
<< Ui->actiongeneral_overview
<< Ui->actioncard_overview
<< Ui->actionscenario_overview
<< Ui->actionabout
<< ui->actionacknowledgement;
foreach (Qaction *action, actions)
Start_scene->addbutton (action);
Creates a Qgraphicview object and displays it in the center of the main form, setting the scene of the view as the start scene.
view = new Fitview (scene);
Setcentralwidget (view);
Restorefromconfig ();
Let view show Start_scene
Gotoscene (Start_scene);
Second, Button class
Start the interface button effect is cool, the mouse has animated effect, and has a sound, and large online game effect is very similar. Its implementation is very simple, the button class is inherited from Qgraphicobject, in its internal processing mouse events and self-painting. First Look at the button's constructor, An INIT member function is called directly inside, the INIT function sets the button to receive the focus, receives the mouse hover event, and creates a Qgraphicspixmapitem object based on the title parameter of the constructor, on which the caption text of the DrawText button is created, Displays the image above the position of the current object, noting that the image object is shown in the button's constructor, so it is always above the button instance, but it cannot accept focus and mouse events. Therefore, the button object is not affected by the handling of mouse events. The specified button image is then loaded and scaled to the target size, stored in the OUTIMG member.
SetFlags (itemisfocusable);
Setaccepthoverevents (TRUE);
Setacceptedmousebuttons (Qt::leftbutton);
title = new Qpixmap (Size.tosize ());
Title->fill (Qcolor (0,0,0,0));//fill in completely transparent black, so that only the drawn text is displayed, and the other parts do not overwrite the underlying elements
Qpainter PT (title);
Pt.setfont (font);
Pt.setpen (Config.texteditcolor);
Pt.setrenderhint (qpainter::textantialiasing);
Pt.drawtext (Boundingrect (), qt::aligncenter, label);
Title_item = new Qgraphicspixmapitem (this);
Title_item->setpixmap (*title);
Title_item->show ();
......
Qimage bgimg ("Image/system/button/button.png");
outimg = new Qimage (Size.tosize (), QIMAGE::FORMAT_ARGB32);
Qreal pad = 10;
int w = bgimg.width ();
int h = bgimg.height ();
int tw = Outimg->width ();
int th =outimg->height ();
Qreal XC = (w-2*pad)/(Tw-2*pad);
Qreal YC = (h-2*pad)/(Th-2*pad);
for (int i=0;i<tw;i++)
for (int j=0;j<th;j++)
{
int x = i;
int y = j;
if (X>=pad && x<= (tw-pad)) x = Pad + (x-pad) *xc;
else if (x>= (tw-pad)) x = W-(tw-x);
if (Y>=pad && y<= (th-pad)) y = pad + (y-pad) *yc;
else if (y>= (th-pad)) y = h-(th-y);
Qrgb RGB = Bgimg.pixel (x, y);
Outimg->setpixel (I,J,RGB);
}
The paint virtual method overload implementation of the Button class is simple, drawing the outimg image directly and drawing a white translucent rectangular area above the image based on the animation effect.
QRECTF rect = Boundingrect ();
Painter->drawimage (RECT,*OUTIMG);
Painter->fillrect (Rect,qcolor (255,255,255,glow*10));
In order to animate, the Hoverenterevent event triggered by the mouse stroke sets the button to have focus, plays the sound, and invokes the Qobject::starttimer function to start the timer, invoking the update function in the TimerEvent event to trigger repainting, and increment and decrement the glow variable, adjust the transparency of the rectangular area drawn above the button----when the button has focus, increase the visibility, the light white haze effect, lose focus decreases the visibility until the button picture is fully displayed.
void Button::hoverenterevent (Qgraphicsscenehoverevent *) {
SetFocus (Qt::mousefocusreason);
#ifdef Audio_support
if (!mute)
Sanguosha->playaudio ("Button-hover");
#endif
if (!timer_id) timer_id = Qobject::starttimer (40);
}
void Button::timerevent (Qtimerevent *)
{
Update ();
if (Hasfocus ())
{
if (glow<5) glow++;
}else
{
if (glow>0) glow--;
else if (timer_id)
{
Qobject::killtimer (timer_id);
timer_id = 0;
}
}
}
Third, the sound
The Sun god Three Kingdoms kill the sound is very smooth and beautiful. Implement the open source cross-platform game sound engine fmod, for more information, see: Http://baike.baidu.com/view/656662.htm. Internally encapsulates the fmod operation in the sound class, which is a simple class Just a few lines of code.
Class sound;
Static Fmod_system *system;
Static Fmod_sound *BGM;
Static Fmod_channel *bgmchannel;
Class sound{
Public
Sound (const QString &filename)
: Sound (NULL), channel (NULL)
{
Fmod_system_createsound (System, Filename.toascii (), Fmod_default, NULL, &sound);
}
~sound () {
if (sound)
Fmod_sound_release (sound);
}
void Play () {
if (sound) {
Fmod_result RESULT = Fmod_system_playsound (System, Fmod_channel_free, sound, False, &channel);
if (result = = FMOD_OK) {
Fmod_channel_setvolume (Channel, 1.000/*config.effectvolume*/);
Fmod_system_update (System);
}
}
}
BOOL IsPlaying () const{
if (channel = = NULL)
return false;
Fmod_bool is_playing = false;
Fmod_channel_isplaying (Channel, &is_playing);
return is_playing;
}
Private
Fmod_sound *sound;
Fmod_channel *channel;
};
Initialize the Fmod when the project starts:
Fmod_result RESULT = fmod_system_create (&system);
if (result = = FMOD_OK) {
Fmod_system_init (System, 0, NULL);
}
Release Fmod at the end of the project:
if (System) {
Soundcache.clear ();
Fmod_system_release (System);
System = NULL;
}
Note that Fmod requires 6 header files: fmod.h,fmod_codec.h,fmod_dsp.h,fmod_errors.h,fmod_memoryinfo.h,fmod_output.h, As well as a Lib file fmodex.lib, a DLL file Fmodex.dll. You can directly transfer the above class and 8 files to your own project to use, test pass. The only thing to note is that the sound object's destructor will end the audio playback, Therefore, if you declare a temporary variable, you need to wait for the sound to play before you can jump out of the scope of the sound object, otherwise the playback is over.
Iv. How to enter the Roomscene
After entering the game you need to first click on the Start Server button to establish the service side, then click the Start Game menu, restart a process, in the new process, click the Start Game button, pop up the connection form, enter the server IP address and user name can be added to the game, Go directly to the official game interface. Here you create two processes, the first one is the server, the second is the client. In order to track the second EXE process, you need to start an EXE process directly, after starting the second process, click on the vs2008 Debug menu-Attach to the process, find the second Three Kingdoms kill process, You can set the breakpoint tracking in the source code. Here is a description of the process by which the client builds the game.
When you click the Start Game button, a connection form pops up, the accepted signal of the window object is associated with the startconnection slot, and when you click the Connect button, the function is triggered, creating an instance of the client class, in its version_ Checked signal response function checkversion, determine whether the client and the server version number matches, if the match with the server to establish a connection, the client object server_connected signal trigger Enterroom function, into the game interface.
Connect (Connection_dialog, SIGNAL (Accepted ()), this, SLOT (Startconnection ())); The Connect form in the constructor returns the trigger Startconnection
The Startconnection function starts the client object and sets the connection of the signal to the slot
void Mainwindow::startconnection () {
Client *client = new Client (this);
Connect (client, SIGNAL (version_checked (qstring,qstring)), SLOT (Checkversion (qstring,qstring)));
Connect (client, SIGNAL (Error_message (QString)), SLOT (Networkerror (QString)));
}
Compare version numbers in Checkversion and go to the game interface
void Mainwindow::checkversion (const QString &server_version, const QString &server_mod) {
QString client_mod = Sanguosha->getmodname ();
if (client_mod! = server_mod) {
Qmessagebox::warning (This, tr ("Warning"), tr ("Client MOD name was not same as the server!");
Return
}
Client *client = qobject_cast<client *> (sender ());
QString client_version = Sanguosha->getversionnumber ();
if (server_version = = client_version) {
Client->signup ();
Connect (client, SIGNAL (server_connected ()), SLOT (Enterroom ()));
if (Qapp->arguments (). Contains ("-hall")) {
Halldialog *dialog = Halldialog::getinstance (this);
Connect (client, SIGNAL (server_connected ()), dialog, SLOT (Accept ()));
}
Return
}
......
Take a look at the core function enterroom. After setting up the server IP address and logging on successfully, this function is triggered. First, save the IP address in config. Sets the Enabled property of the associated action to invalidate the corresponding button and menu. Create a Roomscene object, Settings. Finally call Gotoscene (room_scene); switch to the game interface.
Five, the creation of the game interface
The elements of the game interface are completely created in the Roomscene scene class, and you can analyze how the corresponding interface is created by opening the game view and comparing the images in the code and the Image\system directory. The first one is based on the total number of players obtained from the game server, Generate icons that represent each offsite player.
Create avatars that represent other players without creating the current player
int i;
for (i = 0; i < player_count-1;i++) {
Photo *photo = new photo;
Photos << photo;
AddItem (photo);
Photo->setzvalue (-0.5);
}
The operator panel is then created, which includes the button area on the interface, as well as the current player's equipment area and hand area.
Add the operator panel and buttons at the bottom right
{
Createcontrolbuttons ();
Qgraphicsitem *button_widget = NULL;
if (clientinstance->getreplayer () = = NULL) {
QString Path = "Image/system/button/irregular/background.png";
Button_widget = new Qgraphicspixmapitem (Qpixmap (path));
Four irregular buttons
Ok_button->setparentitem (Button_widget);
Cancel_button->setparentitem (Button_widget);
Discard_button->setparentitem (Button_widget);
Trust_button->setparentitem (Button_widget);
}
The Create dashboard dashboard includes player equipment and hand areas
Dashboard = new Dashboard (button_widget);
Dashboard->setobjectname ("Dashboard");
Dashboard->setzvalue (0.8);
AddItem (dashboard);
Call Createstateitem (); The function creates two buttons that select the anti-Thief and the hero.
To create a chat area control:
Chat_box = new Qtextedit;
Qsize chat_box_size = room_layout->chat_box_size;
Chat_box_size.rwidth () + = Widen_width;
Chat_box->resize (chat_box_size);
Chat_box->setobjectname ("Chat_box");
Chat_box_widget = AddWidget (Chat_box);
Enter the TextEdit control for chat information:
Chat_edit = new Qlineedit;
Chat_edit->setfixedwidth (Chat_box->width ());
Chat_edit->setobjectname ("Chat_edit");
The System Information display box on the right:
Chat_widget = new Chatwidget ();
Chat_widget->setx (Chat_box_widget->x () +chat_edit->width ()-77);
Chat_widget->sety (Chat_box_widget->y () +chat_box->height () + 9);
Chat_widget->setzvalue (-0.2);
AddItem (Chat_widget);
At the bottom of the two ComboBox:
Sort_combobox = new Qcombobox;
Sort_combobox->additem (tr ("No sort"));
Sort_combobox->additem (TR ("Sort by Color"));
Sort_combobox->additem (TR ("Sort by Suit"));
Sort_combobox->additem (TR ("Sort by Type"));
Sort_combobox->additem (TR ("Sort by Availability"));
Connect (Sort_combobox, SIGNAL (currentindexchanged (int)), dashboard, SLOT (sortcards (int)));
}
Connect (self, SIGNAL (pile_changed (QString)), this, SLOT (Updatepilebutton (QString)));
Add role ComboBox
Role_combobox = new Qcombobox;
Role_combobox->additem (TR ("Your role"));
Role_combobox->additem (tr ("Unknown"));
Connect (self, SIGNAL (role_changed (QString)), this, SLOT (Updaterolecombobox (QString)));
Enter the game interface generated basically introduced, the following will be divided into several articles on the role of each class and implementation mechanism.
http://blog.csdn.net/henreash/article/details/7817792
VS2008 compiling QT Open source project three kills (five articles)