• Advertisement
Sign in to follow this  
  • entries
    3
  • comments
    2
  • views
    4629

About this blog

Promoting good software implementation architectures.

Entries in this blog

It's a pain to create levels, unless you have a level editor that is.
Luckily, for 2D games, there are free editors out there like Tiled ( http://www.mapeditor.org/ ).

I needed to have a tool to help me create levels, so I wrote a parser for Tmx files exported in json.


I chose json because of 3 reasons.

1. The json parser is lighter than an xml one (e.g. tinyxml2).

2. The plain xml file (with base64/zlib disabled) is bigger than the json export.

3. The Zlib dependency is eliminated (since the derbh system can compress all assets in marmalade anyways)


Grab the source code from github

Usage is really simple:
//-- Load the map:
tmxparser::Map_t *map = new tmxparser::Map_t;
tmxparser::parseTmxFromJSON_file("testLevel.json", map);

//-- Create a renderer (for marmalade):
tmxparser::TmxRenderer *renderer = new tmxparser::TmxRenderer(map);

//-- In the main loop call the renderer to draw the map (passing a given offset location)
renderer->Render(offset);

The renderer clips the tiles that are outside the display. Look at the IsTileVisible() method if you need to change the cliping area.


Have a good weekend!
I would like to release this is a Drag and Drop implementation in case it might be useful to others.
It's in C++ and is fairly portable. Located in 2 files (DragDrop.cpp, DragDrop.h).

The example project is for the marmalade game engine.

You can get it from here:
https://github.com/GameCy/MarmaladeDragDrop

If you are a visual type of learner maybe you can understand it faster by looking at some diagrams in the documentation here:
https://github.com/GameCy/MarmaladeDragDrop/blob/master/DragDropArchitectureDoc.pdf
In the old days, C++ didn't have any concept of events and delegates. One had to implement it by using function pointers and other gimicks.
This is an attempt of mine at implementing them, including a dispatcher for cross thread execution.

The Concept

Maybe you have already seen a OnMouseClick() function somewhere. This function is called whenever the user clicks the mouse button, and, the programmer has a chance to perform any commands he wants inside this function.
Similarly, you can probably guess what might happen inside a OnNetworkConnectionLost() function.

It's like this:

  • Mouth Shouting to Ear (real world analogy)
  • Publisher Notifies Subscriber (software pattern)
  • Signal raised triggers Slot (QT framework)
  • Observable Updates Observer (Java)
  • Event Notifies EventHandler(via Delegate) (CSharp)

    It is of course possible to attach many Ears to the same mouth (or many EventHandler functions to the same event).

    Event parameters

    The shouting signal can be empty or it can contain a message (extra parameters). These parameters are usually called event arguments.
    For example on a mouse click it may be desirable to pass the mouse possition and button state. So there would be a MouseEventArgs class containing the X and Y position. Each time the OnClick event is raised, it contains a MouseEventArgs object with the actual mouse position at the current time.

    Event Dispatcher

    It is sometimes the case that the event happens on one thread but we need to execute the handler on another thread.
    There are three clasic cases for this:

    1. The event happens on another thread but we need to update the user interface which runs on the main thread. E.g a connection is closed and we need to change the connection status in the window via the main thread.
    2. EventHandler A can under some condition call EventHandler B, and EventHandler B sometimes calls EventHandler A. it is clear that this will result in an infinite circular invocation of both handlers, until the program stack is exhausted and our program will crash. Therefore we need to put the calls in queue, and execute them later on, or on another thread.
    3. When we wish to execute an EventHandler with some delay, for example we wish to remove a user popup message after 5 seconds have passed. We cannot pause the whole program, so the solution is to queue the event to be raised at a later time by the dispatcher in another thread.

    Implementation

    The events implementation is based on http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4119/Delegate-in-Standard-C.htm with some fixes for my convenience.
    Later on the EventDispatcher was implemented. It uses threads and locks for the https://www.madewithmarmalade.com/ game engine, so you will need to replace them with equivalent calls in your engine.

    Code is attached.
Sign in to follow this  
  • Advertisement