Learn to Create Games with Allegro C/C++ on Windows and Linux
Allegro is a cross-platform library primarily aimed at game development, but is also widely used in multimedia programming.
Allegro is a cross-platform library primarily aimed at game development, but is also widely used in multimedia programming.
In this article we will see how to install the Allegro library on Windows and compile our code with Allegro in two ways: with Visual Studio and with GCC/G++ with MinGW, in case you want to use it with a different code editor or IDE.
Let’s see through this step by step how to install on Windows and compile with Visual Studio first.
1. Open Visual Studio and click on Create a New Project
2. Choose C++ and Empty Project and click Next
3. Enter the name of your project and choose the location where you will save it and click Create
4. Right-click your project and select the option: Manage NuGet Packages…
5. Select the tab: Search and type the word Allegro
6. Select the first package that has the name:** Allegro** and in the right corner click on Install . In the window that will open, just click OK and wait for the installation when there is the word Finished at the bottom
7. Close the two NuGet windows on the right side, right-click again on your project name, go to Add and New Item
8. Choose your file name. Remembering that in addition to being able to create files with a .cpp extension and other related ones, you can also create a .c file, in this case I will create a file with the .c extension (for C language) and name: main.c and click Add
9. Now click again with the right mouse button on top of your project name, go to Properties
10. Note that in the tree on the right side there is the option: Allegro 5, click on it to expand and then select Library Type, then click on it again, but on the option on the right side and select the item : Dynamic Debug - Dynamic runtime
11. Now in the tree on the left side choose the Allegro 5 Add-ons sub-item and once selected, click on each of the options on the right side and mark as Yes and click on Apply and Ok after that
12. Copy this test file and paste it inside the file you created:
13. Then compile the code and show the window below, that means everything is ok and you can use Visual Studio this way to start creating your Games with Allegro on Windows!
Compiling Allegro with GCC/G++ and MinGW on Windows
3. Open CMD or PowerShell (which should already be properly configured with MinGW for GCC/G++) and enter your project where there is the file(s) with the Allegro code, example: MyProject/main.c
4. Also copy the files from the C:\allegro folder into your project:
The liballegro_monolith.dll.a library:
And allegro_monolith-5.2.dll library:
5. Run this command:
If you have a problem, increment the command with -L, example: gcc -I C:\allegro\include -L C:\allegro\lib -c main.c, note that after -L the path is from \lib
6. After that run the command::
If you have a problem, increment the command with -L, for example: gcc -I C:\allegro\include -L C:\allegro\lib main.o -o program.exe liballegro_monolith.dll.a .
7. And now run the binary
Utilizando make com Makefile
If you want to automate the compilation, use a Makefile. Create a file named Makefile (Do not use Notepad, it adds a .txt to the end of the file name, and this file cannot have an extension)
Copy the code below and paste it inside your Makefile, and save the file inside your project
Rename the mingw32-make.exe file in the C:\mingw64\bin\mingw32-make folder to just make.exe.
Now when you are inside your project via CMD or PowerShell, just run the make command, it will compile and run in an easier way