summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 1cc27d30074935a27c4e174179c29ea086153752 (plain)
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
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
#include "texture.h"
#include "renderer.h"
#include "window.h"
#include "timer.h"
#include "keyboard.h"

#include <chrono>
#include <thread>
//void millisleep(Uint32 t){
//    SDL_Delay(t);
//}

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int WALKING_ANIMATION_FRAMES = 7;

int
main( int argc, char* args[] ) {

    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize: %s\n", SDL_GetError() );
    }

    Window window {"This is the title", SCREEN_WIDTH, SCREEN_HEIGHT};
    Renderer renderer;
    renderer.init(window.window());

    const Uint32 LOOP_TIME = 20L; // milliseconds
    Timer timer;
    timer.start();

    GameState game {&renderer, 2.0};
    Keyboard keys;

    while( true )
    {
        // Handle events
        SDL_Event e;
        while( SDL_PollEvent( &e ) != 0 )
        {
            window.handleEvent(e)->execute(game);
            keys.handleEvent(e)->execute(game);
        }

        if(game.isClosed()) return 0;
        if(game.isPaused()) goto next;


        // GAME LOOP:
        game.update( timer.elapsed() );
        game.render();

    next:
        // Loop time calculation:
        timer.restart();
        timer.waitUntil( LOOP_TIME );
    }

    SDL_Quit();
    return 0;
}