179 lines
4.7 KiB
C++
179 lines
4.7 KiB
C++
#include "maps.h"
|
|
|
|
Maps::Maps(SDL_Renderer* render){
|
|
int sound = 1;
|
|
|
|
if(sound == true){
|
|
Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 );
|
|
}
|
|
//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;
|
|
|
|
|
|
//Free the music
|
|
Mix_FreeMusic( gMusic );
|
|
gMusic = NULL;
|
|
};
|
|
|
|
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;
|
|
|
|
//Get the total of blocks
|
|
map >> blockTotal;
|
|
|
|
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;
|
|
}
|
|
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];
|
|
}
|
|
|
|
gMusic = Mix_LoadMUS( "assets/sand-castle.ogg" );
|
|
if( gMusic == NULL ){
|
|
std::cout << "Failed to load beat music! SDL_mixer Error: " << Mix_GetError() << std::endl;
|
|
}
|
|
|
|
//Close the opened file
|
|
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("level: " + mapName,textColor);
|
|
|
|
Texture sandCastle;
|
|
sandCastle.setRenderer(renderer);
|
|
sandCastle.loadTexture("textures/castle.png");
|
|
SDL_Rect castleQuad = {6*sz,2*sz,sandCastle.getWidth(),sandCastle.getHeight()};
|
|
|
|
Texture sandMiddle;
|
|
sandMiddle.setRenderer(renderer);
|
|
sandMiddle.loadTexture("textures/sand-middleground.png");
|
|
SDL_Rect middleQuad = {0,0,sandMiddle.getWidth(),sandMiddle.getHeight()};
|
|
float sandParallax = 0.5;
|
|
|
|
Texture stars;
|
|
stars.setRenderer(renderer);
|
|
stars.loadTexture("textures/stars.png");
|
|
SDL_Rect starsQuad = {0,0,stars.getWidth(),stars.getHeight()};
|
|
float starsParallax = 0.2;
|
|
|
|
/*Texture clouds;
|
|
clouds.setRenderer(renderer);
|
|
clouds.loadTexture("textures/clouds.png");
|
|
SDL_Rect cloudsQuad = {sz,sz/2,clouds.getWidth(),clouds.getHeight()};
|
|
int cloudsParallax = 3;*/
|
|
|
|
SDL_Rect levelRect = {10,10,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);
|
|
|
|
Mix_PlayMusic( gMusic, -1 );
|
|
|
|
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]){
|
|
musicQuit();
|
|
return 0;
|
|
}
|
|
|
|
//Clear the render and set the background color
|
|
SDL_SetRenderDrawColor(renderer,10,20,73,0xFF);
|
|
SDL_RenderClear(renderer);
|
|
|
|
level.render(&levelRect);
|
|
|
|
//Update the camera position
|
|
camera.update(player.getRectangle().x,player.getRectangle().y);
|
|
|
|
stars.render(&starsQuad,camera.getPosX(),starsParallax);
|
|
//clouds.render(&cloudsQuad,camera.getPosX(),cloudsParallax,1);
|
|
sandMiddle.render(&middleQuad,camera.getPosX(),sandParallax);
|
|
sandCastle.render(&castleQuad,camera.getPosX());
|
|
|
|
//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);
|
|
}
|
|
musicQuit();
|
|
return -1;
|
|
};
|
|
|
|
void Maps::musicQuit(){
|
|
Mix_HaltMusic();
|
|
//Free the music
|
|
Mix_FreeMusic( gMusic );
|
|
gMusic = NULL;
|
|
};
|