summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: d059b753e4e747ca26b3cd7ced4d44376ff2d2ca (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
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
//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 "keyHandler.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;

void loop( Renderer &renderer, Texture &gSpriteSheetTexture, SDL_Rect* gSpriteClips, Uint32 dt){
    static int frame;

    renderer.clear();
    SDL_Rect* currentClip = &gSpriteClips[ frame / WALKING_ANIMATION_FRAMES ];


    // Txanpona mugitu:
    float vx = 100; // px/s
    static float xpos0;
    float xpos = xpos0 + dt/1000.f * vx;
    xpos0 = xpos;

    float g = 75; // px/(s^2)
    static float vy0 = 100; // px/s
    static float ypos0;
    float vy = vy0 -(dt/1000.f * g) ;
    float ypos = ypos0 + dt/1000.f * vy;
    ypos0 = ypos;
    vy0 = vy;
    if ( ypos0 < 0 ) { ypos0 = 0; }
    if ( ypos < 0 ) { ypos = 0; }

    gSpriteSheetTexture.render(xpos, (SCREEN_HEIGHT/2 -20) - ypos, currentClip);

    //Update screen
    renderer.update();
    ++frame;

    //Cycle animation
    if( frame / WALKING_ANIMATION_FRAMES >= WALKING_ANIMATION_FRAMES )
    {
        frame = 0;
    }
    return;

}

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());
    renderer.setScale(2,2);


    //The window we'll be rendering to
    SDL_Rect gSpriteClips[ WALKING_ANIMATION_FRAMES ];
    Texture gSpriteSheetTexture {renderer.renderer()};


    //Load sprite sheet texture
    if(-1 ==gSpriteSheetTexture.loadFromFile( "assets/money.png" ) )
    {
        return -1;
    }
    else
    {
        for( int i =0; i < WALKING_ANIMATION_FRAMES; i++){
            //Set sprite clips
            gSpriteClips[ i ].x = i*16;
            gSpriteClips[ i ].y =    0;
            gSpriteClips[ i ].w =   16;
            gSpriteClips[ i ].h =   16;
        }
    }


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

    GameState game;
    KeyHandler keys;

    while( true )
    {
        // Handle events
        SDL_Event e;
        while( SDL_PollEvent( &e ) != 0 )
        {
            // Maybe add commands in a queue or a better defined stream?
            window.handleEvent(e)->execute(game);
            keys.handleEvent(e)->execute(game);
        }

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


        // GAME LOOP:
        loop(renderer, gSpriteSheetTexture, gSpriteClips, timer.elapsed());

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

    SDL_Quit();
    return 0;
}