home download get support documentation weaver games

Understanding Weaver Empty Game

When you create a new directory to work in any game project, Weaver creates automaticly for you an initial empty game. A game that consists in a black screen and that exits if any key is pressed.

Try:

weaver dummy

All the content of this game is in src/game.c. Let's study now how this empty game works. Create an empty game rarely is our objective. Because this, the first thing we do after creating a Weaver Project is change src/game.c. But to accomplish this, we must first understend how an empty game works.

Part 1: The License

/* 
Copyright (C) 2009 Thiago Leucz Astrizi

This file is part of dummy.  

dummy is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your
option) any later version.
                                                                                                        
dummy is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
                                                                                                        
You should have received a copy of the GNU General Public License
along with dummy.  If not, see <http://www.gnu.org/licenses/>.  
*/

This is a comment. But is not an ordinary comment. It's the place where a copyright note is added giving to you the recognisement for the work that's to be done. Your name is obtained from /etc/passwd and the current year by "date" command.

After the copyright info, there's the license note. As Weaver is under GNU General Public License v. 3, all the derived works made with Weaver shall be free software too. The deafult license for your game made with Weaver is GNU General Public License v. 3. But if you wish, you can use another license compatible with GNU GPL 3. See http://www.gnu.org/licenses/license-list.html for more information.

Part 2: Headers

#include "weaver/weaver.h"
#include "game.h"

This includes some headers. The "weaver/weaver.h" declares all the Weaver functions that will help us to build complex games. The "game.h" is empty for now. You can use it to declare functions defined in "game.c" or some similar things in the future.

Part 3: Initialization

int main(int argc, char **argv){
  awake_the_weaver(); //Initializing Weaver API

All the Weaver projects must start calling the function "awake_the_weaver()" in the main function. This is the function that initializes a lot of internal data structure used by Weaver and also some usefull variables that you can use in your game.

Part 4: The Main Loop

  // Main loop                                                                                           
  for(;;){
    get_input();
    if(keyboard[ANY]){
      break;
    }

    weaver_rest(10000000);
  }

All the games must have a main loop. An endless loop where all the action happens. The first thing to do in the main loop is get the player input. This is done with the function "get_input()". With it, we can detec things like pressed keys or mouse moves.

Then, if the player presses any key, we break the loop and end the game. If instead we want to finish the game only if the player presses <Esc>, replace "ANY" by "ESC" (without quotes) . Putting "A" (without quotes) instead of "ANY", the game will end if the player presses "A". Other options are SHIFT, CTRL, LEFT_CTRL, RIGHT_CTRL, ENTER, PLUS, MINUS, LEFT, RIGHT, TOP, DOWN, and much more keys.

The main loop ends with a calling to the function "weaver_rest(n)". This function spends n nanosseconds idle, without doing nothing. In our example, it's 10 millions of nanosseconds (or 0.1 seconds). We need this because it's not fair that our game uses 100% of CPU time because of an endless loop. This functions also updates the global variable fps that stores an integer that informs at how many frames per second our game is running.

Part 5: Finalization

  may_the_weaver_sleep();
  return 0;
}

This is the end of the initial code. It calls a function to finish the Weaver API. It also returns the code 0 to the Operating System (meaning sucess) and closes the game.


XHTML 1.1 válido! CSS válido!