Follow the instructions of the README file, then read this.
Making a Helloworld program with HasseGameLib
I expect You already know some basics from C programming language.
So You have downloaded and installed (hopefully!) HasseGameLib. Thank You!
Let's start with an empty .c file:
int main(){
return 0;
}
So to use HasseGameLib, include HasseGameLib.h like this:
#include<HasseGameLib.h>
int main(){
return 0;
}
Then to actually do something, you have to add four more things, a call to "hseDrawText", another call to "hsePrintMatrix" and "sleep" to have the time to read something. Include stdlib.h for the "sleep" function.
#include<stdlib.h>
#include<HasseGameLib.h>
int main(){
hseDrawText(5,5,"Hello, world!");
hsePrintMatrix(1);
sleep(5);
return 0;
}
Compile it with:
gcc yoursourcefile.c -o yourprogramname -lHasseGame
The first two arguments of "hseDrawText" are the X and Y coordinates of the text and the third argument is the text itself. You can also use it a bit like printf; It supports the formats %d and %s.
int num=1234;
char *str="Hi!"
hseDrawText(0,10,"number:%d\nstring:%s",num,str);
To actually draw the stuff to the screen, You have to call "hsePrintMatrix". It's argument is whether to clear the old draw data or not, 0=no, 1=yes. e.g:
#include<stdlib.h>
#include<HasseGameLib.h>
int main(){
hseDrawText(5,5,"Hello, world!");
hsePrintMatrix(0);
sleep(5);
hseDrawText(5,6,"Hello, again!");
hsePrintMatrix(0);
sleep(5);
return 0;
}
The first "Hello, world!" text will stay there in the second call to "hsePrintMatrix". If You put 1 as the argument instead of zero, the "Hello, World!" text won't be shown anymore.
So now you know how to draw text. Not very cool. What about some rectangles?
#include<stdlib.h>
#include<HasseGameLib.h>
int main(){
hseDrawRect(5,5,10,10,"Rectangle");
hsePrintMatrix(1);
sleep(5);
return 0;
}
The first two coordinates it takes are the X and Y coordinates of the first corner, and the third and fourth are the X and Y coordinates of the other corner. If You didn't understand what that means, it's this:
|The first two coordinates
v
#------
| |
| |
| |
------#<- The second two coordinates
If You didn't get it now, just experiment.
Hmm... We've got some boring squares and text. What about lines? No problem. Just make a call to "hseDrawLine".
#include<stdlib.h>
#include<HasseGameLib.h>
int main(){
hseDrawLine(5,5,10,10,"HELLO!");
hsePrintMatrix(1);
sleep(5);
return 0;
}
This draws a line from 5,5 to 10,10 coordinates. Not too hard, was it? Again, if You didn't get it yet, here is an example:
#<-First coordinates
#
#
#
#<-Second coordinates
OK. Now we have some lines and rectangles in black and white. What about some colour? Just a call to "hseSetColour" and we're done!
#include<stdlib.h>
#include<HasseGameLib.h>
int main(){
hseSetColour(GREEN,MAGENTA);
hseDrawRect(5,5,10,10,"H");
hseSetColour(BLUE,RED);
hseDrawRect(11,11,20,20,"I");
hsePrintMatrix(1);
sleep(5);
return 0;
}
The first argument of "hseSetColour" is the background colour, second is the foreground colour.
List of available colours:
BLACK
RED
GREEN
YELLOW
BLUE
MAGENTA
CYAN
WHITE
Then we also have the almost useless function "hseDrawDot".
#include<stdlib.h>
#include<HasseGameLib.h>
int main(){
hseDrawDot(1,1,'A');
hsePrintMatrix(1);
sleep(5);
return 0;
}
It draws only one dot on the specified point. It doesn't take a string now for it's third argument, it takes a char, that's why the ' marks and not the " marks.
That's pretty much it for the graphical functions, let's move on to some other functions. Like "hseRandom". Why call "hseRandom" instead of just "rand"? Because "hseRandom" provides an easier interface for getting random numbers with microsecond seed.
#include<stdlib.h>
#include<HasseGameLib.h>
int main(){
hseDrawText(1,1,"The dice says: %d",hseRandom(6)+1);
hsePrintMatrix(1);
sleep(5);
return 0;
}
"hseRandom" takes only one argument; it's the maximum number of the random. Why "+1" You may ask. That's because it actually returns a number between 0-5 if the argument is 6. That's because it uses the modulus ('%') sign. Google it, if you didn't get it.
What if you're making a game and you want the title to be always at the center of the screen, not depending on the size of the terminal? There is a function called "hseAutoSize".
#include<stdlib.h>
#include<HasseGameLib.h>
int main(){
hseAutoSize(SCALED,100,100);
hseDrawText(50,50,"Awesome title for a game, eh?");
hsePrintMatrix(1);
sleep(5);
return 0;
}
But the text isn't in the center, it starts from there! That's a problem. And that's why there is "hseSetTextAlign".
#include<stdlib.h>
#include<HasseGameLib.h>
int main(){
hseAutoSize(SCALED,100,100);
hseSetTextAlign(ALIGN_CENTER);
hseDrawText(50,50,"Awesome title for a game, eh?");
hsePrintMatrix(1);
sleep(5);
return 0;
}
Now it works! If you don't define a how the text is aligned, it defaults to ALIGN_LEFT. There is also ALIGN_RIGHT, and it's pretty obvious.
Back to "hseAutoSize". That "100,100" after SCALED means that the screen is divided to 100 columns and 100 rows.
There are types other than SCALED for "hseAutoSize":
SIMPLE
SCALED
CENTERED
SIMPLE means that each coordinate is absolute so that 1,2 is two characters from the top of the terminal and one character from the left side of the terminal.
CENTERED means that 0,0 is the center of the terminal and 1,1 is one character right and down from the center.
But what are the two coordinates after the SIMPLE or CENTERED? They aren't :-).
"hseAutoSize" is declared "hseAutoSize(ast autoSizeType,...)", meaning that it takes varying amount of arguments.
Let's say you actually want to make a game with this library. Animation/movement? They have nothing to do with this library, but are easy to do. Include unistd.h for the "usleep" function.
#include<HasseGameLib.h>
#include<unistd.h>
int main(){
int x=0;
hseAutoSize(SCALED,100,100);
while(x<100){
x++;
hseDrawText(x,50,"Look me! I'm moving!");
hsePrintMatrix(1);
usleep(50000);
}
return 0;
}
Write "man usleep" in your terminal, it will tell you more about the "usleep" function. Please do not use sleep-time less than 50000, it may result flickering when drawing on some machines.
So that's the basic drawing loop.
A game also needs input. A keylistener might help :-). The keylistener that comes with HasseGameLib sadly can't detect more than one keypress at a time. If you have access to other keylisteners, please use them, they're probably a lot better than this.
So, if you've decided to use the keylistener that comes with HasseGameLib, here's how:
#include<HasseGameLib.h>
#include<unistd.h>
int main(){
char *c=hseStartKeylistener();
hseAutoSize(SCALED,100,100);
while(1){
hseDrawDot(50,50,*c);
hsePrintMatrix(1);
usleep(50000);
}
return 0;
}
"Oh no! Horrible pointer thingys!" If that was what you just said or thought, I'll explain a bit. You declare a pointer by adding an asterisk before it's name '*'. A pointer is the "ID" of a place in memory. That place is a variable. If you want to use the number the pointer "points" to, you add an asterisk before it's name.
The pointer is used by "hseStartKeylistener" for technical reasons.
TODO add more tuts