start keeping track of inputs and ticks
This commit is contained in:
parent
3d05584a7e
commit
c9fde558e7
73
src/game.d
73
src/game.d
@ -5,12 +5,23 @@ import derelict.opengl.glu;
|
||||
|
||||
enum int WIDTH = 800;
|
||||
enum int HEIGHT = 600;
|
||||
enum uint SDL_TICKS_PER_GAME_TICK = 1000u / 20u;
|
||||
|
||||
class Player
|
||||
{
|
||||
public:
|
||||
int x;
|
||||
int y;
|
||||
uint tick;
|
||||
};
|
||||
|
||||
class Inputs
|
||||
{
|
||||
public:
|
||||
bool left;
|
||||
bool right;
|
||||
bool up;
|
||||
bool down;
|
||||
};
|
||||
|
||||
void init()
|
||||
@ -63,6 +74,8 @@ int main(char[][] args)
|
||||
p1.y = HEIGHT / 2;
|
||||
p2.x = WIDTH / 2 + 100;
|
||||
p2.y = HEIGHT / 2;
|
||||
int pcontrol = 0;
|
||||
Player players[] = [p1, p2];
|
||||
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING))
|
||||
{
|
||||
@ -80,20 +93,72 @@ int main(char[][] args)
|
||||
return 2;
|
||||
}
|
||||
|
||||
uint last_sdl_tick;
|
||||
init();
|
||||
SDL_Event event;
|
||||
Inputs current_inputs = new Inputs();
|
||||
bool exit = false;
|
||||
for (;;)
|
||||
{
|
||||
if (SDL_GetTicks() >= (last_sdl_tick + SDL_TICKS_PER_GAME_TICK))
|
||||
{
|
||||
players[pcontrol].tick++;
|
||||
last_sdl_tick += SDL_TICKS_PER_GAME_TICK;
|
||||
}
|
||||
if (SDL_PollEvent(&event))
|
||||
{
|
||||
if (event.type == SDL_QUIT)
|
||||
break;
|
||||
else if (event.type == SDL_KEYDOWN)
|
||||
switch (event.type)
|
||||
{
|
||||
if (event.key.keysym.sym == SDLK_ESCAPE)
|
||||
case SDL_QUIT:
|
||||
exit = true;
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
switch (event.key.keysym.sym)
|
||||
{
|
||||
case SDLK_ESCAPE:
|
||||
exit = true;
|
||||
break;
|
||||
case SDLK_a:
|
||||
current_inputs.left = true;
|
||||
break;
|
||||
case SDLK_d:
|
||||
current_inputs.right = true;
|
||||
break;
|
||||
case SDLK_w:
|
||||
current_inputs.up = true;
|
||||
break;
|
||||
case SDLK_s:
|
||||
current_inputs.down = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
switch (event.key.keysym.sym)
|
||||
{
|
||||
case SDLK_a:
|
||||
current_inputs.left = false;
|
||||
break;
|
||||
case SDLK_d:
|
||||
current_inputs.right = false;
|
||||
break;
|
||||
case SDLK_w:
|
||||
current_inputs.up = false;
|
||||
break;
|
||||
case SDLK_s:
|
||||
current_inputs.down = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (exit)
|
||||
break;
|
||||
display(p1, p2);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user