/+junk/invaders_vala

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/invaders_vala

« back to all changes in this revision

Viewing changes to src/Game.vala

  • Committer: Gusatv Hartvigsson
  • Date: 2011-12-29 20:10:21 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20111229201021-sp1csy430cb7icv2
* initial code of a Vala port of invaders...
*  BUG: event handlingn not working.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
namespace invadersGame{
 
3
  using SDL;
 
4
  using SDLGraphics;
 
5
  
 
6
  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
    
 
14
    private uint32 fpsTimer;
 
15
    
 
16
    private unowned SDL.Screen screen;
 
17
    private GLib.Rand rand;
 
18
    
 
19
    private bool eventBool[322];
 
20
    private bool isRun;
 
21
    
 
22
    public Game(){
 
23
      stdout.printf("Running contructor...\n");
 
24
      this.isRun = true;
 
25
      this.rand = new GLib.Rand();
 
26
      for(int i = 0; i < 322; i++) { // init them all to false
 
27
        eventBool[i] = false;
 
28
      }
 
29
    }
 
30
    
 
31
    public void mainLoop(){
 
32
      initScreen();
 
33
      while(this.isRun){
 
34
        fpsTimer = SDL.Timer.get_ticks();
 
35
        draw();
 
36
        process_events();
 
37
        if(SDL.Timer.get_ticks() - fpsTimer < 1000/SCREEN_FPS){
 
38
          SDL.Timer.delay(1000/SCREEN_FPS - (SDL.Timer.get_ticks() - fpsTimer));
 
39
        }
 
40
      }
 
41
    }
 
42
    public void initScreen(){
 
43
      uint32 VideoFlags = SurfaceFlag.DOUBLEBUF
 
44
                            | SurfaceFlag.HWACCEL
 
45
                            | SurfaceFlag.HWSURFACE;
 
46
      this.screen = Screen.set_video_mode (SCREEN_WIDTH, SCREEN_HEIGHT,
 
47
                                            SCREEN_BPP, VideoFlags);
 
48
      if(this.screen == null){
 
49
        stderr.printf("Could not initize video \n");
 
50
      }
 
51
      SDL.WindowManager.set_caption("Invaders_vala","");
 
52
    }
 
53
    
 
54
    private void draw(){
 
55
      //TODO
 
56
      this.screen.flip();
 
57
    }
 
58
    
 
59
    private void process_events(){
 
60
      Event event = Event();
 
61
      while(Event.poll(event) != 0){
 
62
        switch(event.type){
 
63
          case EventType.QUIT:
 
64
            stdout.printf("quiting...\n");
 
65
            this.isRun = false;
 
66
            break;
 
67
          case EventType.KEYDOWN:
 
68
            stdout.printf("keydown\n");
 
69
            this.eventBool[event.key.keysym.sym] = true;
 
70
            break;
 
71
          case EventType.KEYUP:
 
72
            stdout.printf("keyup\n");
 
73
            this.eventBool[event.key.keysym.sym] = false;
 
74
            break;
 
75
        }
 
76
      }
 
77
    }
 
78
  }
 
79
}