From 1d6a7bb467d28898da5eb1270a6e8dabf958fa42 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Tue, 28 Dec 2021 12:39:47 +0100 Subject: Huge rewrite, this is not how I'm supposed to use git but whatever --- src/main.cpp | 72 ++++-------------------------------------------------------- 1 file changed, 4 insertions(+), 68 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index fb4a6b5..1cc27d3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,44 +18,6 @@ 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[] ) { @@ -67,38 +29,12 @@ main( int argc, char* args[] ) { 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; + GameState game {&renderer, 2.0}; Keyboard keys; while( true ) @@ -107,7 +43,6 @@ main( int argc, char* args[] ) { 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); } @@ -117,10 +52,11 @@ main( int argc, char* args[] ) { // GAME LOOP: - loop(renderer, gSpriteSheetTexture, gSpriteClips, timer.elapsed()); + game.update( timer.elapsed() ); + game.render(); - // Loop time calculation: next: + // Loop time calculation: timer.restart(); timer.waitUntil( LOOP_TIME ); } -- cgit v1.2.3