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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
using SDL;
using SDLGraphics;
using Gee;
//using GameHelper;
namespace invadersGame{
private const int16 SCREEN_HEIGHT = 640;
private const int16 SCREEN_WIDTH = 480;
private const int SCREEN_FPS = 120;
private const int SCREEN_BPP = 32;
/**
* The main class for the game.
*/
public class Game : Object{
private SDL.Rect my_fill_rect;
private MovementDelta my_movement_delta;
/**GameObjectList
* Stores the objects
*/
private ArrayList<GameObject> GameObjectList = new ArrayList<GameObject>();
private uint32 fpsTimer;
private unowned SDL.Screen screen;
private bool eventBool[322];
private bool isRun;
private GameObjectPlayer player;
/**getRandom
* Returns a value between 0 and i.
* (This is probobly not the most efficient to do this).
*/
public static int getRandom(int i){
GLib.Rand rand = new GLib.Rand();
return rand.int_range(0, i);
//return (int) rand.next_int() % i;
} //getRandom
public Game(){
ResourceHandler.init();
player = new GameObjectPlayer();
for(int16 i = 0; i < 10; i++) {
stdout.printf("Adding object!\n");
GameObjectList.add(new GameObject(i*16, i*16, 0, 0,0, 0));
}
my_fill_rect.x = 0;
my_fill_rect.y = 0;
my_fill_rect.h = SCREEN_HEIGHT;
my_fill_rect.w = SCREEN_WIDTH;
SDL.init(SDL.InitFlag.EVERYTHING);
stdout.printf("Running contructor...\n");
this.isRun = true;/* Is the mainloop running? */
//this.rand = new GLib.Rand();
for(int i = 0; i < 322; i++) { // init them all to false
eventBool[i] = false;
}
} //Game
public void mainLoop(){
initScreen();
while(this.isRun){
player.move();
fpsTimer = SDL.Timer.get_ticks();
draw();
process_events();
process_keys();
player.set_movment_delta(my_movement_delta);
if(SDL.Timer.get_ticks() - fpsTimer < 1000/SCREEN_FPS){
SDL.Timer.delay(1000/SCREEN_FPS - (SDL.Timer.get_ticks() - fpsTimer));
}
}
}//mainLoop
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","");
} //initScreen
private void draw(){
/* Draw the player! */
player.draw(screen);
/* Draw all the items in the list! */
foreach(var g in GameObjectList) {
g.draw(screen);
}
this.screen.flip();
this.screen.fill(my_fill_rect, 0x0000000);
} // draw
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;
}
} // process_events
private void process_keys(){
my_movement_delta = {0,0};
if(eventBool[KeySymbol.ESCAPE] || eventBool[KeySymbol.q]){
stdout.printf("derp!\n");
this.isRun = false;
}
if (eventBool[KeySymbol.DOWN] && eventBool[KeySymbol.UP]) {
} else if (eventBool[KeySymbol.UP]){
my_movement_delta.y = -1;
} else if (eventBool[KeySymbol.DOWN]){
my_movement_delta.y = 1;
}
if (eventBool[KeySymbol.RIGHT] && eventBool[KeySymbol.LEFT]){
} else if (eventBool[KeySymbol.LEFT]){
my_movement_delta.x = -1;
} else if (eventBool[KeySymbol.RIGHT]){
my_movement_delta.x = 1;
}
} // process_keys
/*----------------------------------------------------------------------*/
}
}
|