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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
module br.mainloop;
private import opengl;
private import SDL;
private import br.ship;
private import util.key;
private import util.mouse;
private import std.string;
private import br.screen;
private import br.gamemanager;
private import util.parts;
private import std.math;
private import util.vector;
private import util.matrix;
private import util.log;
public class Mainloop{
static int count;
private:
Parts ship;
//Parts[10] append;
public:
const int INTERVAL = 16;
const int MAX_SKIP_FRAME = 5;
GameManager gm;
bool accframe;
Key key;
Mouse mouse;
bool active;
uint interval;
uint maxSkipFrame;
public this(Key key ,Mouse mouse){
this.key = key;
this.mouse = mouse;
}
public void loop(){
gm = new GameManager(key ,mouse);
// prefManager = new PrefManager;
// prefManager.load();
long prvTickCount = 0;
long nowTick;
int frame;
//partsManager = new PartsManager(128);
gm.start();
active = true;
accframe = false;
interval = INTERVAL;
maxSkipFrame = MAX_SKIP_FRAME;
SDL_Event event;
SDLKey *sdlkey;
GLubyte mask[128];
Uint32 prevTime;
//count = 0;
bool done = false;
//for(int iCount = 0 ; iCount < 128 ; iCount++) mask[iCount] = 0xF0;
while(!done){
//prevTime = SDL_GetTicks();
//SDL_UpdateRect(gScreenSurface,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
while(SDL_PollEvent(&event)){
//event.type = SDL_USEREVENT;
key.handleEvent(&event);
switch(event.type){
case SDL_QUIT:
done = true;
break;
case SDL_ACTIVEEVENT:
switch(event.active.state){
case SDL_APPACTIVE:
if(event.active.gain){
//active = true;
}else active = false;
break;
case SDL_APPINPUTFOCUS:
if(event.active.gain){
//active = true;
}else active = false;
break;
case SDL_APPMOUSEFOCUS:
if(event.active.gain)active = true;
else active = false;
break;
default:break;
}
break;
//case SDL_APPINPUTFOCUS:
case SDL_MOUSEBUTTONDOWN:
active = true;
break;
case SDL_KEYDOWN:
sdlkey=&(event.key.keysym.sym);
if(*sdlkey==SDLK_ESCAPE){
done = true;
}else if(*sdlkey==SDLK_F9)screen.toggleFullScreen();
// else if(*sdlkey==SDLK_F12)screen.saveBMP("screenshot.bmp");
else if(*sdlkey==SDLK_F5)SDL_WM_IconifyWindow();
else if(*sdlkey==SDLK_RETURN){
gm.returnPushed();
}
break;
/*case SDL_VIDEORESIZE:
{
SDL_ResizeEvent re = event.resize;
screen_width = re.w;
screen_height = re.h;
}
*/
default:break;
}
}
nowTick = SDL_GetTicks();
frame = cast(int) (nowTick-prvTickCount) / interval;
// Log_write(d);
if(frame <= 0){
frame = 1;
SDL_Delay(cast(uint)(prvTickCount+interval-nowTick));
if (accframe) {
prvTickCount = SDL_GetTicks();
} else {
prvTickCount += interval;
}
}else if (frame > maxSkipFrame) {
frame = maxSkipFrame;
prvTickCount = nowTick;
} else {
prvTickCount += frame * interval;
}
if(active){
for (int i = 0; i < frame; i++) {
gm.move();
}
}
//active code
//glLoadIdentity();
// screen.clear();
//partsManager.move();
//partsManager.draw();
gm.draw();
screen.flip();
}
// prefManager.save();
gm.close();
SDL_Quit();
}
}
|