Jump to content

  • Log In with Google      Sign In   
  • Create Account

Pixel → Tile → World

Creating custom class templates for QtCreator

Posted by , in Article, Tool 07 March 2012 - - - - - - · 3,576 views

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


Posted Image

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:
<wizard kind="class">
<icon>AdventureFar.png</icon>
<description>Creates an inheritted GameState class for AdventureFar.</description>
<displayname>GameState</displayname>
<displaycategory>Adventure Far</displaycategory>
<files>
<file source="source.cpp" target="%ClassName%.%CppSourceSuffix%" openeditor="true"/>
<file source="header.h" target="%ClassName%.%CppHeaderSuffix%" openeditor="true"/>
</files>
<!-- Create parameter wizard page -->
<fieldpagetitle>GameState parameters</fieldpagetitle>
<fields>
<field name="ClassName">
<fieldcontrol class="QLineEdit" validator="^[a-zA-Z0-9_]+$" defaulttext="MyGameState" />
<fielddescription>Class name:</fielddescription>
</field>
</fields>
</wizard>


Let's walk through it:

<wizard kind="class">

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.

<icon>AdventureFar.png</icon>

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.

<description>Creates an inheritted GameState class for AdventureFar.</description>

This is the description of your template, in the QtCreator GUI.

<displayname>Game state</displayname>

The name of the template, in the GUI.

<displaycategory>Adventure Far</displaycategory>

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

<files>
<file source="source.cpp" target="%ClassName%.%CppSourceSuffix%" openeditor="true"/>
<file source="header.h" target="%ClassName%.%CppHeaderSuffix%" openeditor="true"/>
</files>

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 "%ClassName%.%CppSourceSuffix%", 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.

<fieldpagetitle>GameState parameters</fieldpagetitle>

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.

<fields>
...
</fields>

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:)
<field name="ClassName">
<fieldcontrol class="QLineEdit" validator="^[a-zA-Z0-9_]+$" defaulttext="MyGameState" />
<fielddescription>Class name:</fielddescription>
</field>

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'.

<fieldcontrol> 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.

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

</wizard>

And that's the end of the wizard.xml file.

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

<file source="source.cpp" target="%ClassName%.%CppSourceSuffix%"  openeditor="true"/>


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:u% where the ':u' specifies to use the uppercase version. Likewise, we could use ':l' 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.


NomenRationarium - A tool for organizing character and area names

Posted by , in Uncategorized, Tool 16 February 2012 - - - - - - · 1,101 views
free, tools, writing
NomenRationarium is a tool I've made for my own usages, and the usage of my siblings. Despite us all working on different projects in different mediums (One writing a book, another writing a manga, and myself a video game), we found we had a common need: A way to organize potential character or area names.

NomenRationarium, meaning "Name list" in butchered latin, provides a common place for you to store all your name ideas.

My siblings and I use the same tool, and sync our master list of names via DropBox. For each of our individual projects, we have a NomenRationarium file with the names we are using in that project (whether from the master list, or by creating unique project-only names).

NomenRationarium allows you to individually tag each name with mutliple tags, and search for names by filtering tags and usage.

Posted Image

(This is a picture of a clean usage. When I run the program on my computer, the main list contains almost 300 names already - shared via DropBox)

The tool is definitely functional; however, if I recoded it from scratch I'd do it alot differently. Maybe someday in the future! But at the moment, I'm too busy with my AdventureFar game project (which is making good progress, thank you for asking!) to let myself be distracted any longer by working this tool.

Download NomenRationarium

Or, if plot-related tools don't interest you:
Download Cheesecake


ViewTiled - A small tool for artists

Posted by , in Uncategorized, Tool 09 February 2012 - - - - - - · 1,996 views
tool, free, art, tools

When I work on pixel art, I use MS Paint and Paint Shop Pro 11. It works fine for me making simple game graphics, though I'm sure professionals would prefer something else.

When making pixel art (primarily tiles), I need to view the image tiled side by side to see if it's seamless or not. If I'm currently working in MS Paint and don't have Paint Shop Pro 11 open, I need to:

Open Paint Shop Pro -> wait for it to load -> Paste the image in -> (And in the menu:) "Effects -> Image Effects -> Seamless tiling..." -> Check 'Show preview' -> In the preview window, check 'Show original'


Not cool. So I made a tool that I made that lets you right-click on a image file in Windows Explorer, and select "View image tiled", and wallah!

Posted Image


Note: This does not change any file ascosiations; your files will open using the same preffered default program they always do. All it does is add a single simple 'View image tiled' context menu command, and only on the file extensions you select (Supports: PNG, Gif, Jpg, BMP, and Tiff). You don't even have to enable the context menu integration at all, if you don't want to. You can just open the tool and drag-and-drop or copy-and-paste your images in for viewing.

Posted Image

You can download it for free from GD.net's marketplace (Windows only). If you find it useful, donate $5 and buy me a pizza*.

*Warning: Money will not actually be spent on pizza. Chances are that it'll go to further my raw cookie-dough or Dr Pepper addictions, or perhaps blown on a indie game.





Recent Entries

Recent Comments

Latest Visitors