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