Esempio n. 1
0
 /// operator() handles an event.
 virtual void operator()(Event event) {
     const auto shifted = static_cast<int32_t>(event.y) + _shift;
     if (shifted >= 0 && shifted < _height) {
         event.y = shifted;
         _handle_event(event);
     }
 }
Esempio n. 2
0
void start_game(game *g) {
    _world_vertices(g);
    _setup_world(g);

    _reset_camera(g);
    _setup_camera(g);
    _update_camera(g);

    // Start game
    Uint32 start_loop = SDL_GetTicks();
    Uint32 last_ticks = SDL_GetTicks();
    SDL_Delay(1);
    Uint32 cur_ticks = SDL_GetTicks();

    SDL_Event e;
    size_t count = 0;

    while (g->state != ENDED) {
        _render_world(g);

        // Get time since last frame (ms)
        last_ticks = cur_ticks;
        cur_ticks = SDL_GetTicks();

        if (g->o.enabled) {
            // Calculate average and instant FPS
            if (g->o.fps_upd & 1) {
                g->avg_fps = count / ((cur_ticks - start_loop) / 1000.f);
                g->fps = 1000.0 / (cur_ticks - last_ticks);
                g->o.fps_upd = 0;
            }

            // Check if we should render FPS (only 2 times per second)
            if ((cur_ticks / 100) % 5 == 0) {
                g->o.fps_upd = 2;
            } else {
                g->o.fps_upd >>= 1;
            }

            _render_overlay(g);
        }

        SDL_GL_SwapWindow(g->win);

        // Events
        while (SDL_PollEvent(&e)) {
            _handle_event(g, e);
        }

        // Update camera
        _update_camera(g);

        // Update the world
        ++count;
        if (g->state == RUNNING) {
            switch (g->step) {
                case(WHOLE): world_step(g->w); break;
                case(HALF): world_half_step(g->w); break;
            }
        }

        _update_world_buffer(g);
    }
}
Esempio n. 3
0
 /// operator() handles an event.
 virtual void operator()(Event event) {
     event.y = _height - 1 - event.y;
     _handle_event(event);
 }
 /// operator() handles an event.
 virtual void operator()(Event event) {
     if (event.x >= _left && event.x < _right && event.y >= _bottom && event.y < _top) {
         _handle_event(event);
     }
 }