platform-test/source/maps.cpp

179 lines
4.7 KiB
C++
Raw Normal View History

2017-06-17 17:12:59 +00:00
#include "maps.h"
Maps::Maps(SDL_Renderer* render){
2017-07-26 03:01:29 +00:00
int sound = 1;
if(sound == true){
Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 );
}
2017-06-17 17:12:59 +00:00
//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;
2017-06-21 20:03:11 +00:00
blockTotal = 0;
mapWidth = 0;
mapHeight = 0;
2017-06-17 17:12:59 +00:00
};
Maps::~Maps(){
//Deallocate rows
for(int i = 0; i < blockTotal; ++i) delete blockRect[i];
//Deallocate
delete blockRect;
2017-07-26 03:01:29 +00:00
//Free the music
Mix_FreeMusic( gMusic );
gMusic = NULL;
};
2017-06-17 17:12:59 +00:00
void Maps::loadMap(std::string path){
//Open the map to read it
2017-06-21 20:03:11 +00:00
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
2017-06-21 20:03:11 +00:00
map >> blockTotal;
2017-06-21 20:03:11 +00:00
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;
}
2017-06-21 20:03:11 +00:00
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];
}
2017-06-17 17:12:59 +00:00
2017-07-26 03:01:29 +00:00
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();
};
2017-06-17 17:12:59 +00:00
int Maps::map(std::string mapName){
//Load map
loadMap(mapName + ".map");
2017-06-21 20:03:11 +00:00
Texture level;
level.setRenderer(renderer);
SDL_Color textColor = {0xFF,0xFF,0xFF};
2017-06-22 17:36:18 +00:00
level.loadFromRendererText("level: " + mapName,textColor);
2017-06-21 20:03:11 +00:00
2017-07-26 03:01:29 +00:00
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;*/
2017-06-22 17:36:18 +00:00
SDL_Rect levelRect = {10,10,level.getWidth(),level.getHeight()};
2017-06-21 20:03:11 +00:00
//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);
2017-06-17 17:12:59 +00:00
//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);
2017-07-26 03:01:29 +00:00
Mix_PlayMusic( gMusic, -1 );
2017-06-17 17:12:59 +00:00
bool quit = false;
SDL_Event e;
//Game loop
while(quit == false){
while(SDL_PollEvent(&e)!=0){
if(e.type == SDL_QUIT){
quit = true;
}
}
2017-07-26 03:01:29 +00:00
if(currentKeyStates[SDL_SCANCODE_ESCAPE]){
musicQuit();
return 0;
}
2017-06-17 17:12:59 +00:00
//Clear the render and set the background color
2017-07-26 03:01:29 +00:00
SDL_SetRenderDrawColor(renderer,10,20,73,0xFF);
2017-06-17 17:12:59 +00:00
SDL_RenderClear(renderer);
2017-06-21 20:03:11 +00:00
level.render(&levelRect);
2017-06-17 17:12:59 +00:00
//Update the camera position
camera.update(player.getRectangle().x,player.getRectangle().y);
2017-07-26 03:01:29 +00:00
stars.render(&starsQuad,camera.getPosX(),starsParallax);
//clouds.render(&cloudsQuad,camera.getPosX(),cloudsParallax,1);
sandMiddle.render(&middleQuad,camera.getPosX(),sandParallax);
sandCastle.render(&castleQuad,camera.getPosX());
2017-06-17 17:12:59 +00:00
//Print player
if(player.print(camera.getPosX()) == 1) return 1;
2017-06-17 17:12:59 +00:00
//Print the blocks the corresponding dimensions and positions and check collisions
for(int i = 0; i < blockTotal; i++) ground.printAndCheck(blockRect[i],camera.getPosX());
2017-06-17 17:12:59 +00:00
//Print the poweup and check collisions
powerup.printAndCheck(camera.getPosX());
//Render
SDL_RenderPresent(renderer);
}
2017-07-26 03:01:29 +00:00
musicQuit();
2017-06-17 17:12:59 +00:00
return -1;
};
2017-07-26 03:01:29 +00:00
void Maps::musicQuit(){
Mix_HaltMusic();
//Free the music
Mix_FreeMusic( gMusic );
gMusic = NULL;
};