July 12, 2009

C++ Reflection

Posted in Computer Graphics tagged , , , , , , , , , , , at 10:48 pm by sagito

Today I faced a different problem while trying to build an external interface for the Conspiracy Engine… As it was built on C++, which is a compiled (not interpreted) language, the compiler does not have the concept of reflection. In the first place, what is reflection? Reflection is a technique that makes a language able to know, observe and modify its own structure and behaviour. For example, if I have a class A that wants to know which methods and variables are in class B, I just ask the compiler to tell me. This happens, for example in C#, where we can ask the compiler for class methods, variables, and even execute the methods if we want.

Problem is: There is no direct support for this in C++… However, I need that, because my intention is to build an external editor that can interact directly with the engine like the CryEditor, for example. So, I’m trying to find a solution! There are several ways of attempting such a thing. The first one is to read through the debug information… This is not viable because different compilers use different approaches for debug and the program would have to be always built with debug information (which is huge).

Second solution would be to create a compiler that supported reflection for C++… I have already create one (not for the C++ language!) and I guarantee that it is not something neither pretty to see nor easy to implement. Third solution: Create a specialized pre-processor that could build over the class descriptions… This solution could be viable… However, parsing the C++ language is not trivial and would require a two-pass-process which would introduce a huge overhead.

So, there is no solution for this problem? What if the engine had a specialized class that would store the data for reflection? Every class that derived from this base would register their variables and methods in a global storage that could be accessed from any other class. This is not the real reflection that we are to, but it seems like a nice approach. And it contemplates the cases in which a variable (or class) is sealed, i.e., not accessible through reflection. In that case, the class simply does not register its variables or methods! This approach seems to be the best one so far and I have already implemented it on Conspiracy.

So far it is working fine! πŸ˜€ I should give you some news very soon about the new editor for Conspiracy! πŸ˜‰

July 8, 2009

Sky is not the limit!

Posted in Computer Graphics tagged , , , , , , , , , , , at 9:40 pm by sagito

After a short break due to WordPress maintenaince, I’m back to post about the new stuff that I implemented in the Conspiracy Engine! It now features skybox support, collision detection and a very complete particle engine!

Implementing a skybox was pretty easy. For those of you that do not know what a skybox is… Well, it is pretty much that, a box that represents the sky. So, what about that, why can’t it be just a cube with some texture mapped like every other object? Because unlike other objects, the skybox must move with the viewer… I.e., the user cannot get out of the box as the skybox itself must move with the camera. Just like a person can’t reach the horizon! So, how can one implement a skybox? Pretty easy, you get the view matrix (through IDirect3DDevice9::GetTransform(D3DTS_VIEW, &view);), reset the last row to 0.0 except for the last value and set the new view matrix. After that, the world matrix must be set as an identity too in order to remove any unwanted transformation. Then we can render the skybox! After rendering, we put back the old view and world matrix.

For collision detection, DirectX already provides some very useful functions that automatically calculate the bounding box and sphere for a given object! After that, I just took some optimization measures and converted the bounding box to an axis-aligned oriented bounding box (see http://www.toymaker.info/Games/html/collisions.html for more info). Then, the objects are tested for collision… If a collision happened, it is propagated through a chain of responsability to some entity that handles the event or discard if no one does…

Finally, the particle system was based on a tutorial from CodeSampler (http://www.codesampler.com/dx9src/dx9src_7.htm). The system described in this tutorial is awsome and very complete! Also, the code is very well structured and commented, so it was pretty easy to implement this too.

To end this (already long) post, here is an image of the engine with the skybox and the textured cube:

skybox

June 27, 2009

Conspiracy Engine is born!

Posted in Computer Graphics tagged , , , , , , , , , , , , at 12:41 am by sagito

Hi again! My new engine is already moving… And in quite different directions at the same time! I have implemented a window manager which basically is responsible for creating a window and saving its internal state and a video driver which fires up DirectX and provides basic window painting features – i.e.: paints the window background with a chosen colour! That is very simple indeed, but from my experience with DirectX is a nice way to see if everything is working fine…

However, I’m also evolving it in some different components as I have already built an Observer Pattern Architecture. So basically, any object can now be an observer of any other object which declares itself as a subject! Events are then handled by a Mediator which notifies every one it should. How does this work? Pretty simple, the Mediator registers every subject and every observer which wants to listen from that particular subject. Then, when the subject has some new information, it informs the Mediator, which informs every observer interested in that subject! I believe that this will be a great way of passing events and notifying entities. Also, the Observer, Subject and even the Mediator classes can be easily extended in order to create or provide further specifications for this information flow.

Besides this, I have also implemented an utility class, which follows the Singleton pattern, and a custom exception handler which has already proven to be one of the greatest additions to the engine. Finally, in order to have a better memory management, every class inherits from an IObject interface, which forces its children to implement a drop method. This method is used for clearing everything from memory before leaving the program. This way, the engine becomes much more organized and clean I hope!

I will be having more news, very soon, I hope you are liking! πŸ˜€

June 22, 2009

Simplification of the puzzle prototype

Posted in Web tagged , , , , , , , at 11:36 pm by sagito

As mentioned before, I have been looking for a way of simplifying the puzzle architecture and encapsulate its functionality inside the Piece class. So now, in order to use this architecture, only two things are needed:

  1. Inherit the Piece class
  2. Call the Piece constructor with the Piece name. For example (from within a constructor): public _Default() : base(”Main Page Piece”) {}

Only two steps are required now! The data is automagically filled in by calling the update() function, which searches the whole page for the sections of code, whose name matches the one on the file. For example, if I have an entry “news” in the configuration file and a div called “news” in the .aspx file, then the information will be automatically filled in. The code was largely reduced by using this new technique!

Prototype Puzzle Site

Posted in Uncategorized tagged , , , , , , , at 11:18 pm by sagito

After some (somehow simple) work, I managed to get my first prototype working. The site can be easily parameterized through the new piece’s configuration file. To ease my work, I prefered to avoid using XML and created a rather simple syntax, somewhat similar to a scene graph which describes the data to be transfered to the page.

On to the results, the basic page looks like this:

Basic site

This page’s class inherits directly from the Piece class, which searches for configuration files on every page_load and saves the new data internally. So, I created a small configuration file, as follows:

{news}
<ul>
<li><a href=”#”>Puzzle system is now working!</a></li>
</ul>
{/news}

When I refresh the page, the content is automatically updated, and it is now as follows:

news

So, I decided to try out something different and also customize the top menu, and so, I fed the piece a new configuration file with the following:

{menu}
| <a href=”http://www.google.com”>Google!</a&gt; |
<a href=”https://sagito.wordpress.com”>Tiago’s Blog</a>
{/menu}
{news}
<ul>
<li><a href=”#”>Puzzle system is now working!</a></li>
</ul>
{/news}

Ready, we have a different menu on-the-fly, as shown below:

menu

Thought this could be useful for when a piece wants it’s place in the top menu! πŸ˜‰ So, what must a normal page do in order to implement this architecture, so far? Very simple:

  1. Inherit the Piece class
  2. Call the Piece constructor with the Piece name. For example (from within a constructor): public _Default() : base(“Main Page Piece”) {}
  3. Get the desired information through getInfoNodeByName(string node). For example: InfoNode news = getInfoNodeByName(“news”);
  4. Fill the desired fields with this information. πŸ™‚

And it is ready to use! However, many os these steps are very complicated and should be automatic. Also, a piece must also generate automagically the configuration/data files, which are still created manually.

So, expect further updates very soon! πŸ˜€

June 21, 2009

ASP.NET Unit Testing

Posted in Web tagged , , , , , , , , , at 3:54 pm by sagito

Considering that the project that I mentioned in the previous post was developed for a real-world company (MDevices) and that it was developed for a discipline at the College of Engineering of the Oporto University (FEUP – Faculdade de Engenharia da Universidade do Porto), we had some rules that we had to obey. One of these rules, established by the professors requirements was to produce a test report for the application, which had to include Unit Tests.

However, we have faced a major problem. As we had used Server, Session, Request and Response objects, the Visual Studio 2008 unit testing platform just wouldn’t run. Why? Because apparently, although it was developed to perform unit testing on every kind of .NET projects, it didn’t run in a server context. What does that mean? We wouldn’t have access to server-specific stuff like Server, Session, Request, Response, Cookies and so on… I have searched throughout Google and MSDN just to find out that it just wasn’t possible. Some workarounds were available, which involved editing the web.config file, and I tried each one of them. Nothing worked. So, as the time disappeared, a differente solution had to be found. I tried using some other unit testing platforms, such as NUnit (which also did not contemplate web-based software) and NUnit-ASP which is now a dead project. Resharper looked like a good option at the start, but the very same problems appeared not long after the installation of this addin.

So, to solve this problem, I started studying the Visual Studio Expansibility API and developed a small Unit Testing Add-In for Visual Studio 2008. Based on some examples which I was able to find with the help of Google, this Add-In was quickly integrated with Visual Studio 2008. The big problem was to access the project and its contents. Google did not know too much about this, and it looked quite clearly that MSDN did not want us to know anything about that either. With some effort and a lot of time spent with trial-and-error, I discovered a way to access every solution item and through Reflection access its contents.

Ok, so I had the whole code available. What would I do with that? How could I test that in a server context? The idea was quite simple… My Add-In generated automated unit tests Β for each of the code files that the user chose and saved them in a new class file that was added to a new folder inside the solution. After that, it generated an .aspx and its code-behind files, which has a simple task: Call every test-class function, perform the tests and present the results.

So, when the project is launched, the Visual Studio’s Web Development Server would launch and we would have the server context that we needed. After that, it was just a matter of navigating to the generated folder and open the .aspx file contained within. It would show up in a new webpage the result of running all of the tests, because now it would run within server context! Of course that these automatically generated files could (and should) be edited, in order to suit the programmer and testers needs. But nevertheless, with this project, my team was able to save a large number of hours of work!

I will make this Unit Testing Add-In available, as soon as I make it prettier. It looks quite rough now, although it is totally functional! News about this project will become available in this blog very soon (I hope!), so keep visiting! πŸ˜€

June 20, 2009

The Puzzle Architecture

Posted in Web tagged , , , , at 11:42 am by sagito

Recently I had to develop a large and multidisciplinary web-based software system using ASP.NET and C# for an university course, which consisted in a Decision Support System. The development of this application was carefully planned in the very beginning of the project and was to be developed by a team of 12 students, in which I was included.

However, the application was composed of a large number of small parts which had to be integrated to work as one. As the project’s team leader I knew that the integration would not be as trivial as it should and we decided to start integrating some of the components right away. Truth be told that the integration was very positive because we put everything together from the very beginning. However, some of the features that should have been independent from the others turned out to be mixed in the end, in order to ease the integration of these parts. Although that did not seem to be a problem at the time, this now looks like a big mistake to me while doing the project’s post-mortem.

After reading the “Gang of Four” design pattern books and after having this experience, I decided to do a bit of investigation and try to develop a somehow different type of architecture. A large number of applications today use the concept of “plugins” as a way of extending themselves. Maybe the best example of this would be the Eclipse IDE or even the Mozilla Firefox Browser, which are largely extended by a huge ammount of plugins! However, to apply a plugin to the Eclipse IDE, I need to actually have the base Eclipse application. Same can be applied for Firefox. Plugins, as the name say, just fit into a bigger application and extend their functionality.

So, I intend to build an application that does not have a base. That is, it is composed of completely independent pieces which can be removed or added at any moment. This idea reminds me of a puzzle where a piece fits into some other pieces, but there is not a main piece! Every piece is just like every other piece with the difference that it adds a little bit of colour to the whole thing. Also, it can be removed or added at any moment and when looking at the big picture it won’t affect the rest of the whole puzzle.

Considering that each puzzle piece is now a small piece of software, how would each of the pieces know of the existence of the others? I consider this to be the main problem in this architecture style. My current idea is to implement this communication by using some kind of “hand-shake” XML configuration file that each piece must have and deploy when it is installed. Then, every other piece will attempt to read the file and try to see if it fits within its features and configurations. For example, the main page for a site could integrate a “News” section. This section would be implemented as another piece of the puzzle. So, the “News” piece would give the main page some configuration file, indicating how and where the data could be obtained from in the database (or some other data source, like an external file or some other site).

But now you could think: “Wouldn’t that imply that the main page would already have a space for a news spot?”. Although we could try to make the creation of this space dynamic, my answer is still “Yes, it would”. Because in a puzzle you still need to have a hole in one piece, so that the dent of some other piece can fit in, right? ;P

I will try to develop a system like this in ASP.NET, using C# as the code-behind language. Keep you posted… πŸ˜‰