summaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2021-12-23 20:52:49 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2021-12-23 20:52:49 +0100
commit35465f27ed87646a18cd9298eae861f14385300e (patch)
tree7f0256d28fa6fd5368b7a39500d080456e70be8e /src/main.cpp
First commit: moves a simple thing on the screen
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..422fdc3
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,132 @@
+//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 <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();
+ while( true )
+ {
+ // Handle events
+ SDL_Event e;
+ while( SDL_PollEvent( &e ) != 0 )
+ {
+ //User requests quit
+ window.handleEvent(e);
+ if( e.type == SDL_QUIT ) {
+ return 0;
+ } else if (e.type == SDL_KEYDOWN){
+ switch( e.key.keysym.sym ){
+ case 'q':
+ return 0;
+ }
+ }
+ }
+
+ if(window.closed()) return 0;
+ if(!window.focused()) goto next;
+
+
+ // GAME LOOP:
+ loop(renderer, gSpriteSheetTexture, gSpriteClips, timer.elapsed());
+
+ // Loop time calculation:
+ next:
+ timer.restart();
+ timer.waitUntil( LOOP_TIME );
+ }
+
+ SDL_Quit();
+ return 0;
+}