#include "maps.h" Maps::Maps(SDL_Renderer* render){ //Set renderer renderer = render; //set the window's (renderer) dimensions SDL_GetRendererOutputSize(renderer,&screenWidth,&screenHeight); //Set unit size sz = screenWidth/16; //Set keyboard input currentKeyStates = SDL_GetKeyboardState(NULL); //Set player default position playerPosX = 1*sz; playerPosY = 1*sz; blockTotal = 0; mapWidth = 0; mapHeight = 0; }; Maps::~Maps(){ //Deallocate rows for(int i = 0; i < blockTotal; ++i) delete blockRect[i]; //Deallocate delete blockRect; }; void Maps::loadMap(std::string path){ //Open the map to read it std::ifstream map(path.c_str()); //Check if the map is open if(map == NULL) std::cout << "Unable to load map" << std::endl; map >> playerPosX; map >> playerPosY; playerPosX *= sz; playerPosY *= sz; std::cout << playerPosX << " " << playerPosY << std::endl; //Get the total of blocks map >> blockTotal; std::cout << blockTotal << std::endl; blockRect = new int*[blockTotal]; //Build rows for(int i = 0; i < blockTotal; ++i) blockRect[i] = new int[4]; //Load the block information for(int i = 0; i < blockTotal; i++){ for(int j = 0; j < 4; j++){ map >> blockRect[i][j]; blockRect[i][j] *= 40; std::cout << blockRect[i][j] << " "; } if(blockRect[i][0]+blockRect[i][2] > mapWidth) mapWidth = blockRect[i][0]+blockRect[i][2]; if(blockRect[i][1]+blockRect[i][3] > mapHeight) mapHeight = blockRect[i][1]+blockRect[i][3]; std::cout << std::endl; } std::cout << mapWidth << " - " << mapHeight << std::endl; //Very descriptive by itself map.close(); }; int Maps::map(std::string mapName){ //Load map loadMap(mapName + ".map"); Texture level; level.setRenderer(renderer); SDL_Color textColor = {0xFF,0xFF,0xFF}; level.loadFromRendererText(mapName,textColor); SDL_Rect levelRect = {0,0,level.getWidth(),level.getHeight()}; //Initialize the player, set its position, pass the map's size and set the renderer Player player(playerPosX,playerPosY,mapHeight,renderer); //Set the camera and pass the map and screen's dimensions Camera camera(mapWidth,mapHeight,screenWidth,screenHeight); //Initialize the powerup, set his position, pass the renderer and the player Powerup powerup(13*sz,8*sz,renderer,&player); //Initialize the block class and set the player and the renderer Block ground(renderer,&player); bool quit = false; SDL_Event e; //Game loop while(quit == false){ while(SDL_PollEvent(&e)!=0){ if(e.type == SDL_QUIT){ quit = true; } } if(currentKeyStates[SDL_SCANCODE_ESCAPE]) return 0; //Clear the render and set the background color SDL_SetRenderDrawColor(renderer,0,0,100,0xFF); SDL_RenderClear(renderer); level.render(&levelRect); //Update the camera position camera.update(player.getRectangle().x,player.getRectangle().y); //Print player if(player.print(camera.getPosX()) == 1) return 1; //Print the blocks the corresponding dimensions and positions and check collisions for(int i = 0; i < blockTotal; i++) ground.printAndCheck(blockRect[i],camera.getPosX()); //Print the poweup and check collisions powerup.printAndCheck(camera.getPosX()); //Render SDL_RenderPresent(renderer); } return -1; };