• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
    114
  • comments
    273
  • views
    596718

Creating custom class templates for QtCreator

Sign in to follow this  
Followers 0
Servant of the Lord

4365 views

The QtCreator IDE provides a set of template projects and template classes, but occasionally the need to create your own arises.


templateuc.png

QtCreator stores its templates in this directory:
..\QtSDK\QtCreator\share\qtcreator\templates\wizards\

(In that directory is a bunch of pre-existing templates, which you can look at to learn how they work. At least one of them, 'listmodel', is purely for exampling how it's done. This is how I figured it out, a few hours age - however, there is a great lack of documentation about this online, hence this journal entry)



The first step is creating a new folder for your own template under the 'wizards' directory; the name of the folder doesn't matter.

In that folder you'll want to put your template's source and header files.
QtCreator scans the 'wizards' directory looking for XML files named 'wizard.xml'. The file must be called exactly that.

A wizard.xml file looks like this:
[xml]
AdventureFar.png
Creates an inheritted GameState class for AdventureFar.
GameState
Adventure Far





GameState parameters



Class name:


[/xml]

Let's walk through it:

[xml][/xml]
kind is the type of template you are creating. It is either a 'class' template or a 'project' template.

A 'project' template creates a whole new project, with templated default files and settings.
A 'class' template creates a set of templated files for the existing project.

[xml]AdventureFar.png[/xml]
This is an (optional) 32x32 pixel icon in the same directory as your custom wizard.xml file.
It appears alongside your template's name in the QtCreator GUI.

[xml]Creates an inheritted GameState class for AdventureFar.[/xml]
This is the description of your template, in the QtCreator GUI.

[xml]Game state[/xml]
The name of the template, in the GUI.

[xml]Adventure Far[/xml]
This is the category which your template appears under, in the QtCreator GUI. (In the image above, you can see all of these)

[xml]


[/xml]
These are the files that will be added to the project by your template.
source is the template file in your template's directory.
target is the new name of the file, as it appears in your project (Ignore the funky "[color=#800000]%ClassName%.%CppSourceSuffix%[/color]", it'll be explained a bit lower). This is optional. If the 'target' name isn't in the XML file, it'll keep the same name as the source file.
openeditor - I'm not 100% sure, but I believe this opens the newly created file in the editor when the template is used.


Now, QtCreator allows your template wizards to have fairly extensive GUIs, including multiple pages and widgets and customizations. I'm only familiar with a very small subset of it.

[xml]GameState parameters[/xml]
This is the title of the GUI wizard page we are on. The wizard.xml file we're using only has a single custom page.

[xml]
...
[/xml]
fields holds a list of fields; the template I created only has a need for a single field, but your templates can easily have a half dozen or more depending on what you are wanting to do.

(Within the 'fields' tag:)


[xml]

Class name:
[/xml]
Our field's name is specified by name. The one field I needed, was the name of the class I'm having the template create for me, so my field is named 'ClassName'.

is the Qt widget that accepts user input, which will set the value of our field
The widget is specified by class (in this case, QLineEdit), and we use a RegEx validator to make sure our classname is valid. We also give the QLineEdit the default text specified by defaulttext.

is just the text that appears in the GUI to inform the user what the purpose of the field is.

[xml]
[/xml]
And that's the end of the wizard.xml file.

Now, returning upwards a bit, we had the line:

[xml][/xml]

We now see that %ClassName% will actually be replaced by the value of our field, named 'ClassName'.
%CppSourceSuffix% is a built-in field specified elsewhere in QtCreator, that lets users determine if they want to use .cpp or .cxx or whatever else they fancy.

What's important to note is that %ClassName% wont just get replaced in our wizard.xml file; it'll also get replaced in any of the files the template is generating.

Our source.cpp has code that looks like this:
%ClassName%::%ClassName%()
{


}

%ClassName%::~%ClassName%()
{


}

...creating a constructor and destructor for a class named '%ClassName%'. (Asside from our special replacement variables, our source.cpp file is just a regular C++ file.
Our header.h file also uses our field variables to create inclusion guards, like this:
#ifndef %ClassName:u%_H
#define %ClassName:u%_H


Except here, because we love uppercase inclusion guards, we use %ClassName[color=#ff0000]:u[/color]% where the '[color=#FF0000]:u[/color]' specifies to use the uppercase version. Likewise, we could use '[color=#FF0000]:l[/color]' to specify lowercase.

I'm using my template to generate empty GameState classes for my project. My game has alot of GameStates, all looking roughly the same being inherited from the same abstract class with virtual functions that need to be defined in the subclasses.

Here's how my source and header templates look.

My templated header file:
#ifndef GAME_STATE_%ClassName:u%_H
#define GAME_STATE_%ClassName:u%_H

#include "Engine/Structure/GameState.h"

#include "Common/System/System.h"

namespace Game {
namespace State {

/////////////////////////////////////////////////////////////////////////////////////

class %ClassName% : public Engine::GameState
{
public:
%ClassName%();
~%ClassName%();

void Activated();
void Deactivated();

void React(PlayerCommand &command);
void Update(float deltaTime);
void Think();
void Draw(sf::RenderTarget &renderTarget);

std::string GetName() { return "%ClassName%"; }
};

}} //End of namespaces.


#endif // GAME_STATE_%ClassName:u%_H


My templated source file:
#include "%ClassName%.%CppHeaderSuffix%"

#include "Common/Logger/Log.h"
#include "Common/Types/ConfigFile.h"

#include "Engine/Structure/GameStructure.h"

namespace Game {
namespace State {

/////////////////////////////////////////////////////////////////////////////////////

%ClassName%::%ClassName%()
{

}

%ClassName%::~%ClassName%()
{

}

void %ClassName%::Activated()
{

}

void %ClassName%::Deactivated()
{

}

void %ClassName%::React(PlayerCommand &command)
{

}

void %ClassName%::Update(float deltaTime)
{

}

void %ClassName%::Think()
{

}

void %ClassName%::Draw(sf::RenderTarget &renderTarget)
{

}

void %ClassName%::DrawOver(sf::RenderTarget &renderTarget)
{

}

/////////////////////////////////////////////////////////////////////////////////////

}} //End of namespaces.


Hopefully you'll find this useful if you use QtCreator. I found a great lack of documentation on this otherwise simple feature which is common in most IDEs.

1
Sign in to follow this  
Followers 0


1 Comment


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now