Fix core dump & clearer music deallocation

This commit is contained in:
Pòsweg Blumenthal 2019-12-02 11:48:01 +01:00
parent c15e4e48a5
commit f657c49c79
2 changed files with 45 additions and 57 deletions

View File

@ -1,18 +1,15 @@
#include "maps.h" #include "maps.h"
Maps::Maps(SDL_Renderer* render){ Maps::Maps(SDL_Renderer* render){
int sound = false; if(false) Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 );
if(sound == true){ //Set renderer
Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ); renderer = render;
}
//Set renderer
renderer = render;
//set the window's (renderer) dimensions //set the window's (renderer) dimensions
SDL_GetRendererOutputSize(renderer,&screenWidth,&screenHeight); SDL_GetRendererOutputSize(renderer,&screenWidth,&screenHeight);
//Set unit size //Set unit size
sz = screenWidth/16; sz = screenWidth/16;
//Set keyboard input //Set keyboard input
@ -28,20 +25,21 @@ Maps::Maps(SDL_Renderer* render){
} }
Maps::~Maps(){ Maps::~Maps(){
//Deallocate rows //Deallocate rows
for(int i = 0; i < blockTotal; ++i) delete blockRect[i]; for(int i = 0; i < blockTotal; ++i) delete blockRect[i];
//Deallocate //Deallocate
delete blockRect; //delete blockRect;
Mix_HaltMusic();
//Free the music //Free the music
Mix_FreeMusic( gMusic ); Mix_FreeMusic( gMusic );
gMusic = NULL; gMusic = NULL;
} }
int Maps::map(std::string mapName){ int Maps::map(std::string mapName){
//Load map //Load map
playerPosX = 1*sz; playerPosX = 1*sz;
playerPosY = 4*sz; playerPosY = 4*sz;
mapWidth = sz*32; mapWidth = sz*32;
@ -54,37 +52,37 @@ int Maps::map(std::string mapName){
*/ */
Texture level; Texture level;
level.setRenderer(renderer); level.setRenderer(renderer);
SDL_Color textColor = {0xFF,0xFF,0xFF,0xFF}; SDL_Color textColor = {0xFF,0xFF,0xFF,0xFF};
level.loadFromRendererText("level: " + mapName,textColor); level.loadFromRendererText("level: " + mapName,textColor);
Texture sandCastle; Texture sandCastle;
sandCastle.setRenderer(renderer); sandCastle.setRenderer(renderer);
sandCastle.loadTexture("textures/foreground.png"); sandCastle.loadTexture("textures/foreground.png");
SDL_Rect castleQuad = {0,0,sandCastle.getWidth(),sandCastle.getHeight()}; SDL_Rect castleQuad = {0,0,sandCastle.getWidth(),sandCastle.getHeight()};
Texture sandMiddle; Texture sandMiddle;
sandMiddle.setRenderer(renderer); sandMiddle.setRenderer(renderer);
sandMiddle.loadTexture("textures/sand-middleground.png"); sandMiddle.loadTexture("textures/sand-middleground.png");
SDL_Rect middleQuad = {0,0,sandMiddle.getWidth(),sandMiddle.getHeight()}; SDL_Rect middleQuad = {0,0,sandMiddle.getWidth(),sandMiddle.getHeight()};
float sandParallax = 0.5; float sandParallax = 0.5;
Texture stars; Texture stars;
stars.setRenderer(renderer); stars.setRenderer(renderer);
stars.loadTexture("textures/stars.png"); stars.loadTexture("textures/stars.png");
SDL_Rect starsQuad = {0,0,stars.getWidth(),stars.getHeight()}; SDL_Rect starsQuad = {0,0,stars.getWidth(),stars.getHeight()};
float starsParallax = 0.2; float starsParallax = 0.2;
SDL_Rect levelRect = {10,10,level.getWidth(),level.getHeight()}; SDL_Rect levelRect = {10,10,level.getWidth(),level.getHeight()};
//Initialize the player, set its position, pass the map's size and set the renderer //Initialize the player, set its position, pass the map's size and set the renderer
Player player(playerPosX,playerPosY,mapHeight,renderer); Player player(playerPosX,playerPosY,mapHeight,renderer);
//Set the camera and pass the map and screen's dimensions //Set the camera and pass the map and screen's dimensions
Camera camera(mapWidth,mapHeight,screenWidth,screenHeight); Camera camera(mapWidth,mapHeight,screenWidth,screenHeight);
//Initialize the powerup, set his position, pass the renderer and the player //Initialize the powerup, set his position, pass the renderer and the player
Powerup powerup(13*sz,8*sz,renderer,&player); Powerup powerup(13*sz,8*sz,renderer,&player);
//Initialize the block class and set the player and the renderer //Initialize the block class and set the player and the renderer
Block ground(0,sz*11,screenWidth*2,sz*1,renderer,&player); Block ground(0,sz*11,screenWidth*2,sz*1,renderer,&player);
@ -107,8 +105,7 @@ int Maps::map(std::string mapName){
} }
} }
if(currentKeyStates[SDL_SCANCODE_ESCAPE]){ if(currentKeyStates[SDL_SCANCODE_ESCAPE]){
musicQuit(); return 0;
return 0;
} }
//Clear the render and set the background color //Clear the render and set the background color
@ -124,30 +121,22 @@ int Maps::map(std::string mapName){
sandMiddle.render(&middleQuad,camera.getPosX(),sandParallax); sandMiddle.render(&middleQuad,camera.getPosX(),sandParallax);
sandCastle.render(&castleQuad,camera.getPosX()); sandCastle.render(&castleQuad,camera.getPosX());
//Print player //Print player
if(player.print(camera.getPosX()) == 1) return 1; if(player.print(camera.getPosX()) == 1) return 1;
//Print the blocks the corresponding dimensions and positions and check collisions //Print the blocks the corresponding dimensions and positions and check collisions
//for(int i = 0; i < blockTotal; i++) ground.printAndCheck(blockRect[i],camera.getPosX()); //for(int i = 0; i < blockTotal; i++) ground.printAndCheck(blockRect[i],camera.getPosX());
ground.printAndCheck(camera.getPosX()); ground.printAndCheck(camera.getPosX());
ground2.printAndCheck(camera.getPosX()); ground2.printAndCheck(camera.getPosX());
ground3.printAndCheck(camera.getPosX()); ground3.printAndCheck(camera.getPosX());
ground4.printAndCheck(camera.getPosX()); ground4.printAndCheck(camera.getPosX());
ground5.printAndCheck(camera.getPosX()); ground5.printAndCheck(camera.getPosX());
//Print the poweup and check collisions //Print the poweup and check collisions
powerup.printAndCheck(camera.getPosX()); powerup.printAndCheck(camera.getPosX());
//Render //Render
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }
musicQuit(); return -1;
return -1;
}
void Maps::musicQuit(){
Mix_HaltMusic();
//Free the music
Mix_FreeMusic( gMusic );
gMusic = NULL;
} }

View File

@ -20,7 +20,6 @@ class Maps{
int map(std::string mapName); int map(std::string mapName);
private: private:
void loadMap(std::string path); void loadMap(std::string path);
void musicQuit();
SDL_Renderer* renderer; SDL_Renderer* renderer;