//Core class body

#include"core.h"

void Core::init(){
	SDL_Init(SDL_INIT_VIDEO);
	gWindow = SDL_CreateWindow("Platform Test!",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
	gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	SDL_SetRenderDrawColor(gRenderer,0xFF,0xFF,0xFF,0xFF);

	sz = SCREEN_WIDTH/16;
}

void Core::close(){
	SDL_DestroyRenderer(gRenderer);
	SDL_DestroyWindow(gWindow);
	gWindow = NULL;
	gRenderer = NULL;

	SDL_Quit();
}

int Core::coreInit(){
	init();

	int gamestate = 0;

	//-1 = quit, 0 = Menu, 1 = playing,
	while (gamestate != -1){
        if(gamestate == 0) gamestate = menu(gRenderer);
        if(gamestate == 1) gamestate = map1();
	}

	close();
	return 0;
};

int Core::map1(){
    //Set the map dimensions
    int mapWidth = 36*sz;
    int mapHeight = 12*sz;

    //set the window's (renderer) dimensions
    int screenWidth, screenHeight;
    SDL_GetRendererOutputSize(gRenderer,&screenWidth,&screenHeight);

    //Initialize the player, set its position, pass the map's size and set the renderer
	Player player(40,8*sz,mapHeight,gRenderer);

	//Set the camera and pass the map and screen's dimensions
	Camera camera(mapWidth,mapHeight,screenWidth,screenHeight);

	//Initialize the block class and set the player and the renderer
	Block ground(gRenderer,&player);

	//Set the quantity and size of the blocks
	int blockRect[5][4];
    setRectSize(blockRect[0], 8*sz, 7*sz, 2*sz,2*sz);
    setRectSize(blockRect[1], 4*sz, 9*sz,   sz,2*sz);
    setRectSize(blockRect[2], 6*sz, 9*sz,   sz,2*sz);
    setRectSize(blockRect[3], 0   ,11*sz,16*sz,  sz);
    setRectSize(blockRect[4],20*sz,11*sz,16*sz,  sz);

    //Initialize the powerup, set his position, pass the renderer and the player
    Powerup powerup(13*sz,8*sz,gRenderer,&player);

	bool quit = false;
	SDL_Event e;

	//Game loop
	while(quit == false){
		while(SDL_PollEvent(&e)!=0){
			if(e.type == SDL_QUIT){
				quit = true;
			}
		}
		SDL_SetRenderDrawColor(gRenderer,0,0,100,0xFF);
		SDL_RenderClear(gRenderer);

		camera.update(player.getRectangle().x,player.getRectangle().y);

		if(player.print(camera.getPosX()) == 1) return 0;

        for(int i = 0; i < 5; i++){
            ground.printAndCheck(blockRect[i],camera.getPosX());
        }

		powerup.printAndCheck(camera.getPosX());


		SDL_RenderPresent(gRenderer);
	}
    return -1;
};

void Core::setRectSize(int rect[],int x, int y, int w, int h){
    rect[0] = x;
    rect[1] = y;
    rect[2] = w;
    rect[3] = h;
}
