Jump to content
  • Advertisement
  • Remove ads and support GameDev.net for only $3. Learn more: The New GDNet+: No Ads!

  • 02/27/18 02:33 AM

    How to write a 2D UFO game using the Orx Portable Game Engine - Part 1

    General and Gameplay Programming
       (1 review)



    Welcome to the 2D UFO game guide using the Orx Portable Game Engine.

    My aim for this tutorial is to take you through all the steps to build a UFO game from scratch.

    The aim of our game is to allow the player to control a UFO by applying physical forces to move it around. The player must collect pickups to increase their score to win.

    I should openly acknowledge that this series is cheekily inspired by the 2D UFO tutorial written for Unity.

    It makes an excellent comparison of the approaches between Orx and Unity. It is also a perfect way to highlight one of the major parts that makes Orx unique among other game engines, its Data Driven Configuration System.

    You'll get very familiar with this system very soon. It's at the very heart of just about every game written using Orx.

    If you are very new to game development, don't worry. We'll take it nice and slow and try to explain everything in very simple terms. The only knowledge you will need is some simple C++.

    I'd like say a huge thank you to FullyBugged for providing the graphics for this series of articles.


    What are we making?

    Visit the video below to see the look and gameplay of the final game:

    Getting Orx

    The latest up to date version of Orx can be cloned from github and set up with:

    git clone https://github.com/orx/orx.git 

    Once cloning has completed, the setup script in the root of the files will start automatically for you. This script creates an $ORX environment variable for your system. The variable will point to the code subfolder where you cloned Orx.

    Why? I'll get to the in a moment, but it'll make your life easier.

    The setup script also creates several projects for various IDEs and operating system: Visual Studio, Codelite, Code::Blocks, and gmake. You can pick one of these projects to build the Orx library.

    Building the Orx Library

    While the Orx headers are provided, you need to compile the Orx library so that your own games can link to it. Because the setup script has already created a suitable a project for you (using premake), you can simply open one for your chosen OS/IDE and compile the Orx library yourself.

    There are three configurations to compile: Debug, Profile and Release. You will need to compile all three.

    For more details on compiling the Orx lbrary at: http://orx-project.org/wiki/en/tutorials/cloning_orx_from_github at the Orx learning wiki.

    The $ORX Environment Variable

    I promised I would explain what this is for. Once you have compiled all three orx library files, you will find them in the code/lib/dynamic folder:

    • orx.dll
    • orxd.dll
    • orxp.dll

    Also, link libraries will be available in the same folder:

    • orx.lib
    • orxd.lib
    • orxp.lib

    When it comes time to create our own game project, we would normally be forced to copy these library files and includes into every project.

    A better way is to have our projects point to the libraries and includes located at the folder that the $ORX environment variable points to (for example: C:\Dev\orx\code).

    This means that your projects will always know where to find the Orx library. And should you ever clone and re-compile a new version of Orx, your game projects can make immediate use of the newer version.

    Setting up a 2D UFO Project

    Now the you have the Orx libraries cloned and compiled, you will need a blank project for your game. Supported options are: Visual Studio, CodeLite, Code::Blocks, XCode or gmake, depending on your operating system.

    Once you have a game project, you can use it to work through the steps in this tutorial.

    Orx provides a very nice system for auto creating game projects for you. In the root of the Orx repo, you will find either the init.bat (for Windows) or init.sh (Mac/Linux) command.

    Create a project for our 2D game from the command line in the Orx folder and running:

    init c:\temp\ufo


    init.sh ~/ufo

    Orx will create a project for each IDE supported by your OS at the specified location. You can copy this folder anywhere, and your project will always compile and link due to the $ORX environment variable. It knows where the libraries and includes are for Orx.

    Open your project using your favourite IDE from within the ufo/build folder.

    When the blank template loads, there are two main folders to note in your solution:

    • config
    • src

    Firstly, the src folder contains a single source file, ufo.cpp. This is where we will add the c++ code for the game. The config folder contains configuration files for our game.


    What is config?

    Orx is a data driven 2D game engine. Many of the elements in your game, like objects, spawners, music etc, do not need to be defined in code. They can be defined (or configured) using config files.

    You can make a range of complex multi-part objects with special behaviours and effects in Orx, and bring them into your game with a single line of code. You'll see this in the following chapters of this guide.

    There are three ufo config files in the config folder but for this guide, only one will actually be used in our game. This is:


    All our game configuration will be done there.

    Over in the Orx library repo folder under orx/code/bin, there are two other config files:

    • CreationTemplate.ini
    • SettingsTemplate.ini

    These are example configs and they list all the properties and values that are available to you. We will mainly concentrate on referring to the CreationTemplate.ini, which is for objects, sounds, etc. It's good idea to include these two files into your project for easy reference.

    Alternatively you can view these online at https://github.com/orx/orx/blob/master/code/bin/CreationTemplate.ini and here: https://github.com/orx/orx/blob/master/code/bin/SettingsTemplate.ini


    The code template

    Now to take a look at the basic ufo.cpp and see what is contained there.

    The first function is the Init() function.

    This function will execute when the game starts up. Here you can create objects have been defined in the config, or perform other set up tasks like handlers. We'll do both of these soon.

    The Run() function is executed every main clock cycle. This is a good place to continually perform a task. Though there are better alternatives for this, and we will cover those later. This is mainly used to check for the quit key.

    The Exit() function is where memory is cleaned up when your game quits. Orx cleans up nicely after itself. We won't use this function as part of this guide.

    The Bootstrap() function is an optional function to use. This is used to tell Orx where to find the first config file for use in our game (ufo.ini). There is another way to do this, but for now, we'll use this function to inform Orx of the config.

    Then of course, the main() function. We do not need to use this function in this guide.

    Now that we have everything we need to get start, you should be able to compile successfully. Run the program and an Orx logo will appear slowly rotating.


    Great. So now you have everything you need to start building the UFO game.

    If you experience an issue compiling, check the troubleshooting article for Orx projects    for help.


    Setting up the game assets

    Our game will have a background, a UFO which the player will control, and some pickups that the player can collect.

    The UFO will be controlled by the player using the cursor keys.

    First you'll need the assets to make the game. You can download the file  assets-for-orx-ufo-game.zip which contains:

    The background file (background.png😞


    The UFO and Pickup sprite images (ufo.png and pickup.png😞


    And a pickup sound effect (pickup.ogg😞


    Copy the .png files into your data/texture folder

    Copy the .ogg file into your data/sound folder.

    Now these files can be accessed by your project and included in the game.


    Setting up the Playfield

    We will start by setting up the background object. This is done using config.

    Open the ufo.ini config file in your editor and add the following:


    Texture = background.png 
    Pivot   = center


    The BackgroundGraphic defined here is called a Graphic Section. It has two properties defined. The first is Texture which has been set as background.png.

    The Orx library knows where to find this image, due to the properties set in the Resource section:


    Texture = ../../data/texture


    So any texture files that are required (just like in our BackgroundGraphic section) will be located in the ../../data/texture folder.

    The second parameter is Pivot. A pivot is the handle (or sometimes “hotspot” in other frameworks). This is set to be center. The position is 0,0 by default, just like the camera. The effect is to ensure the background sits in the center of our game window.

    There are other values available for Pivot. To see the list of values, open the CreationTemplate.ini file in your editor. Scroll to the GraphicTemplate section and find Pivot in the list. There you can see all the possible values that could be used.

    top left is also a typical value.

    We need to define an object that will make use of this graphic. This will be the actual entity that is used in the game:


    Graphic = BackgroundGraphic 
    Position = (0, 0, 0)


    The Graphic property is the section BackgroundGraphic that we defined earlier. Our object will use that graphic.

    The second property is the Position. In our world, this object will be created at (0, 0, 0). In Orx, the coordinates are (x, y, z). It may seem strange that Orx, being a 2D game engine has a Z axis. Actually Orx is 2.5D. It respects the Z axis for objects, and can use this for layering above or below other objects in the game.

    To make the object appear in our game, we will add a line of code in our source file to create it.

    In the Init() function of ufo.cpp, remove the default line:


    and replace it with:


    Compile and run.

    The old spinning logo is now replaced with a nice tiled background object.


    Next, the ufo object is required. This is what the player will control. This will be covered in Part 2.

      Report Article

    User Feedback

    Create an account or sign in to leave a review

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

    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



    Great tutorial, I have been using ORX for a long time now for my hobby projects, it takes a little while to get used to the config files, but it is most definitely worth it.

    I remember the first time I used ORX for a project, after I got hold of the config files, I added particle effects (the game had four elemental arrows, each required different particles and each particle had some extra effect - rotating and scaling, for instance) and death effects (enemy blinking + disappearing) to my game in a couple of hours.

    ORX also has a very active community and any questions get answered pretty fast.

    Share this review

    Link to review

  • Advertisement
  • Advertisement
  • Latest Featured Articles

  • Featured Blogs

  • Popular Now

  • Similar Content

    • By phil67rpg
      well rutin has helped with this problem, but I wanted to get more input. I am still working on my rotation algorithm. I want my bullet sprite to rotate around my plane sprite and shoot in the same direction as my plane is facing.
      glTexCoord3f(0.0f, 0.0f, 0.0f); glVertex3f(cos(angle) - sin(angle), sin(angle) + cos(angle), 0.0f); glTexCoord3f(1.0f, 0.0f, 0.0f); glVertex3f(cos(angle) - sin(angle), sin(angle) + cos(angle), 0.0f); glTexCoord3f(1.0f, 1.0f, 0.0f); glVertex3f(cos(angle) - sin(angle), sin(angle) + cos(angle), 0.0f); glTexCoord3f(0.0f, 1.0f, 0.0f); glVertex3f(cos(angle) - sin(angle), sin(angle) + cos(angle), 0.0f); glEnd(); here is the code I am using.
    • By Chrispikula
      So I'm getting to the point in my engine design that I'm trying to implement some dynamic menus.  It's going well, until I've hit a recent conceptual stumbling block that I'm not how to sure to get around.  It has to do with drawing resizable, internally scrollable windows inside each monitor.
      Some background first.
      I'm creating a multi-monitor capable game, so I needed to roll my own GUI.  The window & menu system works, for fixed sized windows.  My menus are all made many sprites, utilizing DirectXTK SpriteBatch, and text via SpriteFont.  I load the images using WICTextureLoader. 
      For the case where I have activated elements in a window exceed the size of the window, that's okay.  They need to be visible, and there's only one active element at a time.  The problem comes with hiding passive elements as they are scrolled out of their parent window, and then drawn outside of it.  Also of note:  I have a *single* application drawing to all of the monitors, not multiple applications drawing to multiple monitors.  So solutions based around options that use the singleton pattern are not workable.  (This, sadly, omits most GUI frameworks).
      I've looked at using viewports, but I can't seem to get them to work properly when I'm dealing with multi-monitor displays, especially when the monitors have different resolutions, and are not aligned perfectly.  I'm not even sure where to begin if I have overlapping viewports, ie, overlapping windows.
      Should I keep struggling with viewports, or should I look into a way of dynamically clipping the images/text/sprites as they are edged out of the windows?  If so, what is such a technique called, and where can I read up more on it?
      Or am I missing a forest for the trees, and there's an easy solution to my problem that I've overlooked?
    • By Josheir
      I have rewritten this function a few times now and I am having trouble getting every other row to have the triangles face the reverse direction.  The image enclosed demonstrates the faulty behavior which includes only one row facing the reversed direction.  
       I am also confused by how many indexes I should have for the g_vertex_buffer_data_land variable, could someone show me a breakdown like : 18 vertices times 2 sets  times 8 columns times 8 depth.
      In another post fleabay mentioned I was not setting a VAO in core profile, however It seems to be.  
      here is the code:
      float* getVertices(void) { //using defines int incol = _colus; int depth = _depth; int i = 0; float scaleit = .5; float tempdepth = 0; int startindexat = 0; int counter = 0; int secondcounter = 0; //for (; (tempdepth+1) <= (depth);) //don't forget to change this back! for (int q=0;q<3;q++) { //odd rows for (int col = 0; (col+1) <= (incol ); col++) { GLfloat matrix1[3][3] = { {(col + 1),0,(tempdepth)},{ (col),0,(tempdepth)}, {(col),0,(tempdepth + 1) } }; // //vertex 1 g_vertex_buffer_data_land[startindexat + 0 + counter] = matrix1[0][0] * scaleit; g_vertex_buffer_data_land[startindexat + 1 + counter] = matrix1[0][1] * scaleit; g_vertex_buffer_data_land[startindexat + 2 + counter] = matrix1[0][2] * scaleit; //vertex 2 g_vertex_buffer_data_land[startindexat + 3 + counter] = matrix1[1][0] * scaleit; g_vertex_buffer_data_land[startindexat + 4 + counter] = matrix1[1][1] * scaleit; g_vertex_buffer_data_land[startindexat + 5 + counter] = matrix1[1][2] * scaleit; g_vertex_buffer_data_land[startindexat + 6 + counter] = matrix1[2][0] * scaleit; g_vertex_buffer_data_land[startindexat + 7 + counter] = matrix1[2][1] * scaleit; g_vertex_buffer_data_land[startindexat + 8 + counter] = matrix1[2][2] * scaleit; int matrix2[3][3] = { { (col + 1),0,(tempdepth + 1)},{ (col + 1),0,(tempdepth)}, {(col),0,(tempdepth + 1) } }; g_vertex_buffer_data_land[startindexat + 9 + counter] = matrix2[0][0] * scaleit; g_vertex_buffer_data_land[startindexat + 10 + counter] = matrix2[0][1] * scaleit; g_vertex_buffer_data_land[startindexat + 11 + counter] = matrix2[0][2] * scaleit; g_vertex_buffer_data_land[startindexat + 12 + counter] = matrix2[1][0] * scaleit; g_vertex_buffer_data_land[startindexat + 13 + counter] = matrix2[1][1] * scaleit; g_vertex_buffer_data_land[startindexat + 14 + counter] = matrix2[1][2] * scaleit; g_vertex_buffer_data_land[startindexat + 15 + counter] = matrix2[2][0] * scaleit; g_vertex_buffer_data_land[startindexat + 16 + counter] = matrix2[2][1] * scaleit; g_vertex_buffer_data_land[startindexat + 17 + counter] = matrix2[2][2] * scaleit; counter = counter + 18; }//end col startindexat = 17 + counter+ 1; for (int col2 = 0; (col2+1) <= (incol); col2++) { //first triangle : even rows GLfloat matrix3[3][3] = { {(col2 + 1) ,0,(tempdepth + 2)} , {(col2 + 1),0,(tempdepth + 1)}, {(col2),0,(tempdepth +1)} }; // //vertex 1 g_vertex_buffer_data_land[(startindexat + secondcounter)] = matrix3[0][0] * scaleit; g_vertex_buffer_data_land[(startindexat + 1 + secondcounter)] = matrix3[0][1] * scaleit; g_vertex_buffer_data_land[(startindexat + 2 + secondcounter)] = matrix3[0][2] * scaleit; //vertex 2 g_vertex_buffer_data_land[(startindexat + 3 + secondcounter)] = matrix3[1][0] * scaleit; g_vertex_buffer_data_land[(startindexat + 4 + secondcounter)] = matrix3[1][1] * scaleit; g_vertex_buffer_data_land[(startindexat + 5 + secondcounter)] = matrix3[1][2] * scaleit; g_vertex_buffer_data_land[(startindexat + 6 + secondcounter)] = matrix3[2][0] * scaleit; g_vertex_buffer_data_land[(startindexat + 7 + secondcounter)] = matrix3[2][1] * scaleit; g_vertex_buffer_data_land[(startindexat + 8 + secondcounter)] = matrix3[2][2] * scaleit; // even (2) int matrix4[3][3] = { {(col2 + 1),0,(tempdepth+ 2)},{ (col2),0,(tempdepth+ 1)}, {(col2),0,(tempdepth + 2) } }; g_vertex_buffer_data_land[(startindexat+9 + secondcounter)] = matrix4[0][0] * scaleit; g_vertex_buffer_data_land[(startindexat+10 + secondcounter)] = matrix4[0][1] * scaleit; g_vertex_buffer_data_land[(startindexat+11 + secondcounter)] = matrix4[0][2] * scaleit; g_vertex_buffer_data_land[(startindexat+12 + secondcounter)] = matrix4[1][0] * scaleit; g_vertex_buffer_data_land[(startindexat+13 + secondcounter)] = matrix4[1][1] * scaleit; g_vertex_buffer_data_land[(startindexat+14 + secondcounter)] = matrix4[1][2] * scaleit; g_vertex_buffer_data_land[(startindexat+15 + secondcounter)] = matrix4[2][0] * scaleit; g_vertex_buffer_data_land[(startindexat+16 + secondcounter)] = matrix4[2][1] * scaleit; g_vertex_buffer_data_land[(startindexat+17 + secondcounter)] = matrix4[2][2] * scaleit; //one column of 4 triangles //(three vetices per triangle) secondcounter = secondcounter + 18; } startindexat = 17 + secondcounter + 1; tempdepth = tempdepth - 1; } return gvertices; }  
      I am hoping someone might have the experience to help me solve this problem.  Or, what could I check and do I need to show more repo code? 
      Thank you,

    • By Purple Spelly
      Hiya! i'm Jason, And i want to reach everyone here who's excited about making games! And as the Title says, I'm looking for either GMS2 Programmers Or Godot Programmers.
      I want to invite you to my Game Creation Of Imagistory, A Beautiful 2D RPG game with Plot twists, Corky Characters and an amazing story.
      Here is some spoliers:
      Long ago, A mystical Comet Flew throughout the universe.
      Legends say that one day this comet will create a brand-new galaxy.
      A little scientist named Brown tried to fly to space to see the comet in action.
      His Friends, the Wizards, went with him to see the explosion.
      A Vortex Pulled them into a strange portal. They all scattered in different locations.
      What will Brown Do?
      And Here is Some Art:
      I'm Ciao Gelato #7986 on discord and my email is is imagistory2500@gmail.com, if you want to contact me there.
      I'm Looking For Free/Revshare Workers, Please!   Stay Creative!!!
    • By sevenfold1
      I'm looking for a open source PBR rendering engine that I can use.  Basic requirements are Windows (C/C++) and free to use type license.

      The first two hits I get on Google are:



      Does anybody have any experience using any of these, or do you recommend something else that's better?
      Pluses: Active development, easy to compile, zero dependencies.
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!