~gustav-hartvigsson/+junk/invaders_vala

« back to all changes in this revision

Viewing changes to src/Game.vala

  • Committer: Gusatv Hartvigsson
  • Date: 2011-12-30 22:03:48 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20111230220348-w81my8a877oixugt
* added gee-1.0 to Makefile.
* Fixed Clean in Makefile.

* added getRandom(int i) to invaderGame.Game.
* moved SCREEN_* constnts to namespace from class Game.
* added process_keys.

* started work on GameObject super-class.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
using SDL;
2
2
using SDLGraphics;
 
3
using Gee;
3
4
 
4
5
namespace invadersGame{
5
6
  
 
7
  private const int SCREEN_HEIGHT = 640;
 
8
  private const int SCREEN_WIDTH = 480;
 
9
  private const int SCREEN_FPS = 60;
 
10
  private const int SCREEN_BPP = 32;
 
11
  
6
12
  class Game : Object{
7
 
    
8
 
    
9
 
    private const int SCREEN_HEIGHT = 640;
10
 
    private const int SCREEN_WIDTH = 480;
11
 
    private const int SCREEN_FPS = 60;
12
 
    private const int SCREEN_BPP = 32;
 
13
    private ArrayList GameObjectList = new ArrayList<GameObject>();
 
14
    /**GameObjectList
 
15
     * Stores the objects
 
16
     */
 
17
     
 
18
    private GameObject testObject;
13
19
    
14
20
    private uint32 fpsTimer;
15
21
    
16
22
    private unowned SDL.Screen screen;
17
 
    private GLib.Rand rand;
 
23
    //private GLib.Rand rand = new GLib.Rand();
18
24
    
19
25
    private bool eventBool[322];
20
26
    private bool isRun;
21
27
    
 
28
    public static int getRandom(int i){
 
29
      /**getRandom
 
30
       * Returns a value between 0 and i
 
31
       *
 
32
       */
 
33
      GLib.Rand rand = new GLib.Rand();
 
34
      return rand.int_range(0, i);
 
35
      //return (int) rand.next_int() % i;
 
36
    } //getRandom
 
37
    
22
38
    public Game(){
23
39
      SDL.init(SDL.InitFlag.EVERYTHING);
24
40
      stdout.printf("Running contructor...\n");
25
 
      this.isRun = true;
26
 
      this.rand = new GLib.Rand();
 
41
      this.isRun = true;/* Is the mainloop running? */
 
42
      //this.rand = new GLib.Rand();
 
43
      
 
44
      testObject = new GameObject();
 
45
      
27
46
      for(int i = 0; i < 322; i++) { // init them all to false
28
47
        eventBool[i] = false;
29
48
      }
30
 
    }
 
49
    } //Game
31
50
    
32
51
    public void mainLoop(){
33
52
      initScreen();
35
54
        fpsTimer = SDL.Timer.get_ticks();
36
55
        draw();
37
56
        process_events();
38
 
        
39
 
        if(eventBool[KeySymbol.ESCAPE]){
40
 
          stdout.printf("derp!\n");
41
 
          this.isRun = false;
42
 
        }
 
57
        process_keys();
43
58
        
44
59
        if(SDL.Timer.get_ticks() - fpsTimer < 1000/SCREEN_FPS){
45
60
          SDL.Timer.delay(1000/SCREEN_FPS - (SDL.Timer.get_ticks() - fpsTimer));
46
61
        }
47
62
      }
48
 
    }
 
63
    }//mainLoop
 
64
    
49
65
    public void initScreen(){
50
66
      uint32 VideoFlags = SurfaceFlag.DOUBLEBUF
51
67
                            | SurfaceFlag.HWACCEL
56
72
        stderr.printf("Could not initize video \n");
57
73
      }
58
74
      SDL.WindowManager.set_caption("Invaders_vala","");
59
 
    }
 
75
    } //initScreen
60
76
    
61
77
    private void draw(){
62
78
      //TODO
63
79
      this.screen.flip();
64
 
    }
 
80
    } // draw
65
81
    
66
82
    private void process_events(){
67
83
      Event event = Event();
80
96
          this.eventBool[event.key.keysym.sym] = false;
81
97
          break;
82
98
      }
83
 
    }
 
99
    } // process_events
 
100
    
 
101
    private void process_keys(){
 
102
      if(eventBool[KeySymbol.ESCAPE] || eventBool[KeySymbol.q]){
 
103
        stdout.printf("derp!\n");
 
104
        this.isRun = false;
 
105
      }
 
106
    } // process_keys
 
107
    /*----------------------------------------------------------------------*/
84
108
  }
85
109
}