/*
* Copyright 2023, Ali Arya
* Anyone Can Code: Algorithmic Thinking
* Example 5.1c
*/

#include "SDLX.h"

//simple animation


#define NUM_FRAMES	3

//global variables
SDLX_Bitmap* frames[NUM_FRAMES];
int currentFrame = 0;
int playerX, playerY;

//global functions
void LoadFrames()
{
	frames[0] = SDLX_LoadBitmap("Player0.bmp");
	frames[1] = SDLX_LoadBitmap("Player1.bmp");
	frames[2] = SDLX_LoadBitmap("Player2.bmp");
}
void AdvanceAnimation()
{
	currentFrame++;
 	if(currentFrame >= NUM_FRAMES)
 		currentFrame = 0;
}
void DrawAnimation()
{
	SDLX_DrawBitmap(frames[currentFrame], playerX, playerY);
}

//main function
int main(int argc, char *argv[])
{
	///////////////////////
	// Initialize
	///////////////////////

	//init SDL library and graphics window
	SDLX_Init("Simple 2D Game", 640, 480, true); 

	//game object
	playerX = 0;
	playerY = 0;
	LoadFrames();

	//loop variables
	SDLX_Event e;
	bool quit = false;

	// Main Loop
	while (!quit)
	{
		///////////////////////
		// Update
		///////////////////////

		//keyboard events
		if (SDLX_PollEvent(&e))
		{
			if (e.type == SDL_KEYDOWN)
			{
				//press escape to end the game
				if (e.keycode == SDLK_ESCAPE)
					quit = true;
				//update player  
				if (e.keycode == SDLK_RIGHT)
					playerX ++;
				if (e.keycode == SDLK_LEFT)
					playerX --;
				if (e.keycode == SDLK_UP)
					playerY --;
				if (e.keycode == SDLK_DOWN)
					playerY ++;
			}
		}

		AdvanceAnimation();

		///////////////////////
		// Draw
		///////////////////////

		//First clear the renderer/buffer
		SDLX_Clear();

		//Draw the objects (in order: background to foreground)
		DrawAnimation();

		//Update the screen
		SDLX_Render();

		//pause
		SDLX_Delay(1);
	}

	//clean up
	SDLX_End();

	return 0;
}
