126 lines
3.3 KiB
C++
126 lines
3.3 KiB
C++
#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;
|
|
};
|
|
|
|
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
|
|
blockTotal = std::count(std::istreambuf_iterator<char>(map), std::istreambuf_iterator<char>(), '\n');
|
|
std::cout << blockTotal << std::endl;
|
|
|
|
map.clear();
|
|
map.seekg(0,map.beg);
|
|
map.ignore(std::numeric_limits<std::streamsize>::max(),'-');
|
|
|
|
// dynamically allocate memory using new
|
|
blockRect = new int*[blockTotal];
|
|
//Build rows
|
|
for(int i = 0; i < blockTotal; ++i) blockRect[i] = new int[4];
|
|
|
|
//Initialize size of the map variables
|
|
int mH = 0,mW = 0;
|
|
|
|
//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] > mW) mW = blockRect[i][0]+blockRect[i][2];
|
|
if(blockRect[i][1]+blockRect[i][3] > mH) mH = blockRect[i][1]+blockRect[i][3];
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
//Very descriptive by itself
|
|
map.close();
|
|
|
|
//Initialize the player, set its position, pass the map's size and set the renderer
|
|
player.set(playerPosX,playerPosY,mH,renderer);
|
|
};
|
|
|
|
int Maps::map(std::string mapName){
|
|
//Load map
|
|
loadMap(mapName + ".map");
|
|
|
|
int mapHeight = 12*sz;
|
|
int mapWidth = 36*sz;
|
|
|
|
//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);
|
|
|
|
//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;
|
|
};
|