Welcome to part 5 of my Love2d tutorial. In this part we are going to add tiles to the game.
Adding tiles
The first thing we need to do is to create three tile pieces. For this i used GIMP and made three simple squares.
Next create a directory called images and place the three tile pieces there.
The next thing to do is to create a file Tiles.lua.
Inside this file, the first thing to do is to create a tile map.
This is a 2d array using numbers to represent location of the tiles on the map
0 represents empty space
1 represents tile 1
2 is tile 2
3 is tile 3
For the next part, the steps are:
Create an Icon object to hold icons. it should store:
icon for the tile created with love.graphics.newImage()
width of icon using getWidth()
height of icon. Use the function getHeight()
Create Icon list.This is a list to hold the Icon objects.
Create a function which populates the icon list with the three icons tile1, tile2, and tile3.
A function to loop through the tile map and print each tile.
We use a nested for loop to loop through the tile map.
If the tile isn't zero then we print the corresponding icon from icon_list using the number in Tile_Map.
Modifying main.lua
We now need to make a few changes to main. We need to call our new code and we clean up a few things we previously added.
New stuff to add
Icon_List:populate we call the populate function in our love.load() function.
drawTileMap() We add this function to love.draw() in order to draw the tiles on each update.
Cleaning up older stuff
We now clean up some of our older code to improve our main.lua file.
we change our player construct function to use an image icon using love.graphics.newImage().
we change our enemy construct function to use an image icon directly.
we change the values for WIDTH and HEIGHT to accommodate our new tile map.
Updating Characters.lua
We now make a few changes to our Characters.lua file.
o.icon = icon we set the objects icon to the icon we pass to it. No more calling love.graphics.newImage()
o.y = y - o.oy we adjust the y position to account for the y offset.
Final remarks
If all went well you should now have tiles printed to the map when you start the game.You can try adjusting the numbers in tile_map to change the map.
In the next part we will further develop the game to include collision with the tiles.
As always, you can get the complete code from github.
Welcome to my quick introduction to the Wren Programming language.
In this post i will be covering the basics of this interesting little language.
I hope by the end you have not only learned a little about Wren, but you have had your interested piqued by it like mine has been.
What is Wren
Wren bills itself as a "Small,fast,classed-based concurrent scripting language."
It is a small embeddable scripting language written entirely in C99.
It has syntax which is familiar to C.
Wren is based abound classes. OOP here we come.
Variables
Variables are one of the most basic parts of a programming language.
In wren, unlike C, you don't need to declare the type of the variable.
Variables are declared using the var keyword.
var name = valuedeclares a variable name containing value
var can hold int,floats,bools,strings,lists,objects,functions,etc.
vars can then be reassigned from one type to another.
Lists
Creating and using lists in wren is very simple.
List are dynamic, meaning they have no fixed size.
They can also hold a mixed of different types at once.
Lists can also be used as a map or dictionary.
lists start at 0.
lists can hold many different types at one.
negative index values start at the end of the list and go backwards.
Lists can also be used as a map/dictionary.
Keys can be almost anything:strings,variables,numbers,objects,etc.
values can also be almost anything.
Control flow
Control flow in wren is very similar to C. if statements are near identical.
wren uses the same set of operators such as == and <
The only difference with C you need to watch out for is 0 is evaluated as true.
while loops in wren look identical to C while loops. Nothing to explain here
for loops are a bit different. You loops over a range or over a list.
Lists can be created in the loop or you can use a list which was created prior to the loop.
To loop through a map you use mapname.keys or mapname.values.
Classes and Objects
Wren is based around classes. But luckily wren makes classes very easy.
Anyone familiar with most OOP languages will get this stuff very easily.
Even without an OOP background it is fairly easy to get.
class name {}
make a new class called name.
construct new(args)
constructor for the class.
_fieldname
declare a field for the class named fieldname
methodName=(value) {})
declare methodName a setter for the class.
methodName {_fieldname}
declare methodName a getter for class.
class childclass in parentclass
declare childclass to be a child class of parentclass.
super
calls the method or field in the parent class.
Final remarks
Thank you for checking out my introduction to Wren. I hope you find the language as interesting as I do.
I will be covering embedding wren into C in a separate blog poster later on. Hope to see you there.
For those looking to make games, there is a Game engine using wren.
I also suggest you check out the wren wiki for more info,libraries,projects,and applications.
Welcome to my blog post exploring the basics of the C preprocessor.
The C preprocessor is a macro processor which is used as part of the process of compiling C source code.
Macros are widely used when doing C programming, and a knowledge of how to use the C preprocessor will help you be a better C programmer.
All example code was testing used both GCC and Clang running on manjaro linux. Results may vary on other compilers and/or O.S.
From here on the C preprocessors will be abbreviated as 'CPP'
Basics
This first example will illustrate the very basics of macros.
This example has the preprocessor include a file, and also define two macros.
#include <filename>
this has CPP include the contents of filename into the current file.
#define macrobody
The CPP replaces all occurrences of macro with body
In the follow example, we include stdio.h header into our file so that we have access to printf().
We also define a macro for PI which expands out into 3.14159 and one called str.
The code then shows a few examples of these macros being used. Nothing fancy or complicated but i think you get the idea.
Function like macros
We now move on to function like macros.
These are macros that we can use like functions.
#define name(args) body defining a function like macro which can accept arguments.
To call a function like macro is very similar to calling a normal C function.
name(args) calls a function like macro name with args.
In the example code, we create a function like macro which takes in a number and returns the square of it.
External files
This example we cover defining macros in another file and then using them in our current file.
We first define them in a file macros.h then include that in our file.
We can then use those macros in the current file just as before.
#define "file" search for file in same directory as current file then include it into our current file.
Conditional processing
We can use if like statements to conditionally control the CPP.
Depending on whether the conditions are true or false we can control how certain macros are handled or even if they exist in the file at all.
If you already understand if statements as they are regularly used in programming then this should be easy to understand.
#if simple if statement. Checks for true/false
#elifsame as an else if.
#elsean else statement
#ifdefif defined. Checks if a macro is defined, and if is, returns true,otherwise returns false.
#ifndefif not defined. Checks to see if a macro is not defined.
#endifEnds a if block.
In the example code we first create two macros to use.
We then use conditionals to conditionally define other macros which we then use in main()
In our code we also introduce #undef which undefines a macro.
try commenting out #undef X and also try defining a macro 'A' to see how this affects the output.
Predefined macros
In this example I cover some common predefined macros.
These are macros which the complier already defines.
Some Caution, not every single one of these may be included in every single C compiler.
__FILE__ returns the name of the file.
__LINE__ returns the line number.
__COUNTER__ a counter which starts at zero and increments by 1 on each call.
__func__ returns the name of the function it is inside of.
__DATE__ returns the current date.
__TIME__ returns the time at which the CPP started running.
Debugging using the CPP
This example will show a simple way to turn on/off debugging messages using conditionals and the CPP.
When DEBUG is defined as 1 and/or DEBUG_ALT is defined, debug messages get printed to STDERR.
Also shown is how to use a conditional to turn off a block of code without needing to delete it all or comment it all out.
Macros with variable arguments
In this example we improve on our debugging by introducing variadic macros
These are macros which can take in a variable number of arguments.
We use them to handle the multiple arguments to a call to frpintf()
#define macro(...) body
(...) this indicates that the macro can take variable arguments.
__VA_ARGS__ This represent the variable arguments.
this will expand out into each argument in the order that it was passed to the macro.
Using VA macro we can now call DEBUG_MSSG() with as many arguments as we need to handle our fprintf call.
Using the CPP by itself
The CPP can be used as a standalone macro processor.
It is very limited in its functionality in this respect compared to a dedicated general purpose macro processor like m4.
But still, it can be put to some use.
Some reasons to use it as a standalone preprocessor:
Debug your macros
you can view how your macros expand out so you can check for errors.
learn more about macros
viewing how the CPP expands macros can help you learn about macro expansion.
general macro processor
Use it in place of a dedicated general purpose macro processor.
There are a few ways to call the CPP by itself.
On a GNU/Linux system i know of three ways:
CPP file
process file and prints result to stdout.
gcc -E file
tells gcc to compile file but to stop after it runs the preprocessor.
clang -E file
tell clang to compile file but to stop after running the preprocessor.
When using the CPP by itself, I recommend using the flags
-P
This will stop the CPP from generating linemarkers.
-C
this will keep comments. Normally CPP removes comments from files.
Comparing .c files to output of CPP
Below you can see the output of example1.c after running it through CPP.
You can see how the CPP expands the macros PI and str.
Compare this to the file example1.c to see how the CPP alters it.
Using CPP as genreal purpose macro expander
In this example I use the CPP to exand macros into html tags.
This illustrates an example of how it can be used as a general purpose macro expander.
It also illustrates the weakness of using the CPP as such.
The CPP is designed for C code, when you try to use non standard C code with it, you can run into issues.
Run the CPP on this file and saving it as an .html file.
Inspecting the file reveals it to generate a valid html file. all macros expand properly.
You can then open the saved .html file in a browser to verify that it works.
So you see, it can be done, it's just not the best tool for the job.
Final remarks
The CPP provides some neat functionality to C programming.
In this post I have show the basics of its use when doing C programming, but also how it can be by itself.
To go further, I recommend you read the guide on the GCC preprocessor here.
All example code featured here can be found on github.