# Tutorial

### A basic gameloop

So lets directly go for our first gameloop. Detailed description follows later:

    #include <sge.h>

    void game() {
    	int quit=0;
    	SDL_Event event;
    	sgeGameLoop(event,quit) {
    		switch (event.type) {
    			case SDL_USEREVENT:
    				if (event.user.code==SGEREDRAW) {
    					sgeClearScreen();
    					sgeDrawRect(screen,10,20,30,40,2,sgeMakeColor(0xff,0,0,-1));
    					sgeFlip();
    				}
    				break;
    			case SDL_KEYUP:
    				if (event.key.keysym.sym==SDLK_ESCAPE) {
    					quit=1;
    				}
    				break;
    			case SDL_QUIT:
    				quit=1;
    				break;
    		}
    	}
    }

    int run(int argc, char *argv[]) {
    	sgeInit(NOAUDIO,JOYSTICK);
    	sgeOpenScreen("mygame",640,480,32,NOFULLSCREEN);
    	sgeStartGame(game, 25);
    	sgeCloseScreen();
    	return 0;
    }

First thing every SGE game has to do is include the sge header file sge.h:

    #include <sge.h>

As C programmer you usually start your applications with a basic function called main(). You do not do this with SGE. You define a function called run() which takes the same params as main(), as entry function names may be named other than main() on several platforms.

    int run(int argc, char *argv[]) {
    }

SGE has to be initialized. This should be your first step in any game you make. SGE does take away some unnecessary typing from the programmer, so you do not have to check if the init failed. SGE will print out some error message and quit by itself.

    sgeInit(NOAUDIO,JOYSTICK);

The three params used by sgeInit() tell SGE which subsystems should be initialized and should be: NOAUDIO/AUDIO, NOJOYSTICK/JOYSTICK

Then we need a screen to draw on. Again there is no error checking necesarry.

    sgeOpenScreen("mygame",640,480,32,NOFULLSCREEN);

The first string sets the string displayed in the window border. Next two values (640,480) is the resolution the display should be opened, followed by the needed bitdepth (32) and a param which selects if the screen opens fullscreen (FULLSCREEN) or not (NOFULLSCREEN)

From now on you can access the screen through the global variable screen which is a SDL_Surface.

Next we start the game loop. The function starts a redraw timer with 25 frames per second and runs the function game().

    sgeStartGame(game, 25);

In our game() function, we define two variables and start our main gameloop.

    void game() {
    	int quit=0;
    	SDL_Event event;
    	sgeGameLoop(event,quit) {
    	}

The sgeGameLoop() function takes a SDL_Event structure and a integer variable which tells it to quit.

In the following switch() there are three event blocks captured:

    			case SDL_USEREVENT:
    				if (event.user.code==SGEREDRAW) {
    					sgeClearScreen();
    					sgeDrawRect(screen,10,20,30,40,2,sgeMakeColor(0xff,0,0,-1));
    					sgeFlip();
    				}
    				break;

Every time a frame has to be redrawn, a userevent is triggered, giving usercode SGEREDRAW

sgeClearScreen() clears the screen (surprise, eh?)

sgeDrawRect() draws a rectangle, which is not filled. There are a bunch of params to it. First you need it to tell where to draw (screen), then the x and y coords where to draw (10,20) and the width and height of the rectangle (30,40). The next param is the linewidth (2) followed by a color to draw with.

The color is generated by a call to sgeMakeColor() which takes the rgb values of the color - a full red in this example, plus a alpha value. A alpha value of -1 means: no alpha used at all, whereas 0-255 would mean, fully transprarent to fully opaque.

Every time you have finished drawing a frame, you have to use sgeFlip() to show your newly drawn stuff. You will not see anything until you use sgeFlip()

The next two blocks handle possible user requests to quit the game loop:

    			case SDL_KEYUP:
    				if (event.key.keysym.sym==SDLK_ESCAPE) {
    					quit=1;
    				}
    				break;
    			case SDL_QUIT:
    				quit=1;
    				break;

On the first case, we check if the user pressed escape, the second case checks for the user having closed the window.