Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon

8. November 2017


In the first tutorial we looked at creating our initial application, while in the second tutorial we looked at events and a cleaner game loop.  Today we get to the fun stuff… drawing bitmaps to the screen.  If you are from another game engine, the term bitmap in Allegro might be a bit misleading as it generally represents an image on disk.  In Allegro terms a bitmap is much more, basically its an all in one term for images that can be load and displayed on screen, taking the role often reserved for sprites in other game libraries.  Let’s just jump into the example:

#include "stdafx.h"
#include <allegro5\allegro.h>
#include <allegro5\allegro_image.h>

int main()
{
	ALLEGRO_DISPLAY * display;
	ALLEGRO_EVENT_QUEUE *queue;
	ALLEGRO_BITMAP * bitmap = NULL;

	al_init();
	display = al_create_display(640, 480);
	queue = al_create_event_queue();

	al_install_keyboard();
	al_register_event_source(queue, al_get_keyboard_event_source());
	al_register_event_source(queue, al_get_display_event_source(display));

	al_init_image_addon();
	bitmap = al_load_bitmap("image.jpg");
	assert(bitmap != NULL);

	bool running = true;
	float x = 0;

	int width = al_get_display_width(display);
	while (running) {
		al_clear_to_color(al_map_rgba_f(1, 1, 1, 1));
		al_draw_bitmap(bitmap, x += 0.01, 0, 0);
		al_flip_display();


		if (x > width) x = -al_get_bitmap_width(bitmap);

		ALLEGRO_EVENT event;

		if (!al_is_event_queue_empty(queue)) {
			al_wait_for_event(queue, &event);
			if (event.type == ALLEGRO_EVENT_KEY_UP || event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
				running = false;
		}
	}

	al_destroy_display(display);
	al_uninstall_keyboard();
	al_destroy_bitmap(bitmap);

	return 0;
}


Most of the code has already been explained in the previous tutorials, so lets jump right into the new code.  You will notice we are using another add-on in this example, the image add on.  This is the module that enables us to load images from file and needs to be enabled in the same way we enabled the font and ttf add-ons earlier.

image


As always there is an init function al_init_image_addon() that needs to be called before any image code can be called.  One initialized we can load a bitmap from disk calling al_load_bitmap() passing in the name of the image file.  Be sure to place this file in debug directory, just like we did earlier with the font file.  The next critical call is the function al_draw_bitmap() which is used to draw our bitmap to the backbuffer at the given coordinates.  In this simple example we are simply incrementing the X coordinate every pass through the game loop.  We then check that the bitmap hasnt moved off screen, if it is, we move it back to the beginning, minus the width of our image and start the process again.  This will cause the image to smoothly scroll back on screen. 


Here is our application in action:

results


There are a few things that are important to note.  First off, notice the call to al_destroy_bitmap(), this is important to call when you are done with your bitmap or resources will leak.  Next notice how drawing coordinates work.  When drawing, the origin (0,0) is the top left corner of the window as well as the top left corner of the bitmap.


It’s also important to realize in addition to al_draw_bitmap() there are several other draw functions available such as al_draw_rotated_bitmap(), al_draw_scaled_bitmap() etc.


Back to Table Of Contents

Programming , ,

7. November 2017


With the upcoming Godot 3.0 release, there is a pretty major change to the physics system within the engine.  They have decided to replace the in-house physics engine with the open source Bullet physics engine.  Bullet is a well established open source project and has been used in such games as Rocket League, Grand Theft Auto 4 and DiRT as well as powering applications such as Blender and Cinema4D.  This change will not affect 2D physics in the Godot engine.


From the Godot announcement:

Introducing Bullet

Godot always supported an abstract physics interface, so Andrea Catania (Odino) volunteered to add Bullet support as a backend. I initially though it would not be possible to replicate Godot's API in Bullet faithfully, but Andrea proved me wrong and did a fantastic job. He also finished before the Beta deadline, so his work was just merged and will be present in Godot 3.0.

Physics should work just like before, and no code should change, except Bullet is being used internally. Godot's old physics engine is provided for compatibility and can be selected in the project settings, but will likely be removed by the time 3.1 is out.


In an unrelated but similarly timed announcement VR support was also added to Godot in the recently released Alpha 2.  You can read more about VR support here.  I did a hands-on video about both physics and VR in Godot available here or embedded below.

GameDev News ,

6. November 2017


In the first tutorial we created a simple Hello World application using Allegro.  However, that application was very limited.  In fact, it couldn’t even be closed!  It simply looped forever.  In this example we are going to change that up slightly, by allowing the application to exit if the user hits a key or clicks the close icon on the app window.


In order to accomplish this we are going to cover the basics of handling events and keyboards.  To handle events of any kind we need to first create an event queue.  As we saw earlier with addons, in Allegro you enable the functionality you need, this includes the keyboard.  Without further ado, lets jump into our new example.

#include "stdafx.h"
#include <allegro5\allegro.h>

int main()
{
	ALLEGRO_DISPLAY * display;
	ALLEGRO_EVENT_QUEUE *queue;

	al_init();
	display = al_create_display(640, 480);
	queue = al_create_event_queue();

	al_install_keyboard();
	al_register_event_source(queue, al_get_keyboard_event_source());
	al_register_event_source(queue, al_get_display_event_source(display));


	bool running = true;
	while (running) {
		al_flip_display();

		ALLEGRO_EVENT event;
		al_wait_for_event(queue, &event);
		if (event.type == ALLEGRO_EVENT_KEY_UP || event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
			running = false;
	}

	al_destroy_display(display);
	al_uninstall_keyboard();

	return 0;
}


When run this example will simply display an empty window.  The big difference is now it can be closed using the X icon or by pressing (and releasing) any key on the keyboard.  You will also notice in this example that since we can now exit our main loop, after the main game loop we do some cleanup work.  Generally every create function has a corresponding destroy function to clean up resources used.  This is also true with install and uninstall functions.


It’s possible that you want your game loop to run at a fixed rate, for example 30 or 60 frames per second.  This is also dont using ALLEGRO_EVENT, in this case a timer.  Here is a simple tweak to the above example that causes the graphics to be updated 60 times per second.

#include "stdafx.h"
#include <allegro5\allegro.h>

int main()
{
	ALLEGRO_DISPLAY * display;
	ALLEGRO_EVENT_QUEUE *queue;
	ALLEGRO_TIMER *timer;

	al_init();
	display = al_create_display(640, 480);
	queue = al_create_event_queue();
	timer = al_create_timer(1.0 / 60.0);

	al_install_keyboard();
	al_register_event_source(queue, al_get_keyboard_event_source());
	al_register_event_source(queue, al_get_display_event_source(display));
	al_register_event_source(queue, al_get_timer_event_source(timer));

	bool running = true;
	while (running) {
		

		ALLEGRO_EVENT event;
		al_wait_for_event(queue, &event);
		if (event.type == ALLEGRO_EVENT_KEY_UP || event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
			running = false;

		if (event.type == ALLEGRO_EVENT_TIMER) {
			al_flip_display();
		}
	}

	al_destroy_display(display);
	al_uninstall_keyboard();
	al_destroy_timer(timer);

	return 0;
}


Back to Table Of Contents

Programming , ,

3. November 2017


Welcome to part one in our tutorial series covering Allegro, an open source C game library/media framework.  For a bit more of an overview and a video version of this tutorial be sure to check out this video.  In this tutorial series we are going to cover the basics of getting started with Allegro, from creating your basic app, to drawing graphics and playing audio.


In this part we are going to illustrate creating a project using Visual Studio 2017.  Allegro is also available on Linux and Mac, but we are not going to cover the getting started process on these platforms.  All of the code in subsequent tutorials will be applicable for all platforms.


Allegro supports NuGet, which makes setting up a new project in Visual Studio incredibly easy.  Let’s start off by creating a Hello World example.  Let’s get started.


Start off by creating a new Windows application in Visual Studio.  Select File->New->Project…

image


Next select Visual C++, then Windows Console Application, name it and give it a location, then hit the OK button.

image


In the Solution Explorer, for your newly created project, right click References and select Manage NuGet Packages…

image


Next click Browse, enter Allegro in the text field, then click the download icon.  Click OK if prompted with an additional dialog.

image


And done!  You have now configured Allegro for use.  In this example however we are going to need two additional libraries.  Allegro is modular in nature and you only enable the functionality you need.  Our application is going to display Hello World on screen and this is going to require a pair of libraries for text and font support.  Don’t worry, it’s easy.


In the Solution Explorer, select your project, like so:

image

Now either right click and select Properties or press Alt + Enter.  This will bring up the Properties Window for your project.  On the left hand side, located and select Allegro 5, then locate and enable Truetype Font Addon and Font Addon, like so:

image


Click Apply then Ok to close the dialog.  We are now ready to code.


You should have a file created for you with the same name as your project, in my case it’s HelloWorld.cpp.  Double click it in the Project Explorer window and replace it with the following code:

#include "stdafx.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_font.h>

int main()
{
	al_init();
	al_init_font_addon();
	al_init_ttf_addon();

	ALLEGRO_DISPLAY * display = al_create_display(640, 480);
	ALLEGRO_FONT * font = al_load_ttf_font("YARDSALE.ttf", 64, 0);

	while (true) {
		al_clear_to_color(al_map_rgb(255, 255, 255));
		al_draw_text(font, al_map_rgb(0, 0, 0), 0, 0, 0, "Hello World");
		al_flip_display();
	}
}


This code follows a pretty common pattern in Allegro.  We always start off by initializing the Allegro libraries with a call to al_init().  You will notice in this example we also need to init the two addons we included.  This is true for all addons use in Allegro as you will see shortly.

This code then creates a window 640x480 in size, loads a font named YARDSALE.ttf with a size of 64pt.  Next we run an infinite loop clearing the background to white ( in RBG format, each number from 0 – 255 is the amount of that could, Red, Green and Blue respectively ).  We then draw the text using our font and this time the color black.  All this time we have been drawing to an offscreen buffer.  Call al_flip_display() to make this offscreen buffer visible to the user.


There are a few things to note about this example… first it can’t be exited.  Don’t worry, we will cover that in a future tutorial.  It also leaks resources like a sieve.  Each load, create or init call has a corresponding call to free those resources up when we are done with them.  We will also cover this in a future tutorial.


The final thing you might notice is we used a font called YARDSALE.ttf…  guess we need that font don’t we?  That font is available for download here, simply download the zip file and copy the file YARDSALE.ttf.  Of course if you have your own TTF file, you can feel free to use it, just be sure to change the file name in the code.  Now the question remains… where do you copy it?  This is actually an area that Visual Studio C++ developers often get tripped up… I certainly do.  You want this file to be copied into the Working Directory of your application.  You can determine or set this directory in the Properties panel of your project. 

image


Copy the TTF file to this location.


Now you can run your code by hitting F5 or clicking Local Windows Debugger in the toolbar.  If everything went according to plan, you should see:

image


Congratulations, you’ve just created your first Allegro application!  On thing you might notice is a DOS window in the background, like so:

image


Don’t worry, getting rid of this window is pretty simple.  Once again, open up the Properties window of your project.  Now locate Linker->System on the left hand side.  Select SubSystem, then Windows instead of Console.

image


Voila, no more console window!  One other thing you may notice… due to the fact this application is an infinite loop, it never actually shuts down and you can’t close it using the X icon.  For now you can exit the application in Visual Studio using this button:

image


Or by hitting Shift F5.  Well that’s it for Part One, congratulations, you’ve created your very first Allegro application.  Next we will look a bit closer at our game loop and making exiting our game a lot easier.


Back to Table Of Contents

Programming , ,

1. November 2017


Google have just launched Poly, a giant repository of mostly free 3D models, aimed at AR and VR content creation.  The site is available at https://poly.google.com/ with most models downloadable in OBJ format, a file format compatible with the vast majority of 3D applications including Max, Maya and Blender.  This service is very similar to Microsoft’s Remix3D, launched along side the Windows 10 Creator Update.  The majority of models in Poly are licensed under the CC-BY open source license, a very game developer friendly license simply requiring attribution. 


You can see Poly in action (and a bit of Remix3D) in this video.

GameDev News

Month List

Popular Comments