Arduboy Magazine Vol.1

Page 6

LEARN:

In this topic we would like to present you some tips & tricks to help you code your games for Arduboy. What we present doesn’t intend to be the absolute truth, it’s just a way we at TEAM a.r.g. code. So always remember, these are not golden rules.

s k c i r T Tips &

by TEAM A.R.G

#1 - Using multiple files

by @jo3ri

In this first edition I would like to start with the very first thing I taught myself, when I started coding on Arduino (the platform Arduboy is based on): use multiple files. When you start coding a game/app you will always start small, with a few lines, but that will grow fast while adding code into one big file. I found it unpleasant to have to scroll through hundreds of lines of code, so I started looking for a solution, which I found in “using multiple files” Assuming you’re using Arduino IDE for writing your code, but the idea is the same for any other program you use, you can add a new tab (file) to your game/app by clicking on the little arrow on the right side of the IDE and choose “New Tab”. Let’s do this and call this tab: globals.h Now if you save your game/app and check the folder you saved it in, you’ll see an extra file called globals.h In the IDE you can switch tabs (files), by clicking on them. We will use the globals.h to put all global includes, defines, variables and sometimes functions.

Before we write anything into the file, we always put this 2 lines of code at the very beginning of our file: #ifndef GLOBALS_H #define GLOBALS_H

end at the end we put: #endif

Always use capitals and the exact same name as you used for the file itself and end that with _H We will also have to tell our compiler to include this global.h file when compiling. You do this by adding the next line to the top of the main file (the .ino file), the tab (file) with the name of your game/ app: #include “globals.h”

Before we continue, let’s explain this header file. A header file is a file with extension .h which contains C/C++ function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler. to be continued next page >> Page 6 of 11


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.