From f657c49c7987f49d941a0645c7816865bee7463c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=B2sweg=20Blumenthal?= Date: Mon, 2 Dec 2019 11:48:01 +0100 Subject: [PATCH] Fix core dump & clearer music deallocation --- src/maps.cpp | 101 +++++++++++++++++++++++---------------------------- src/maps.h | 1 - 2 files changed, 45 insertions(+), 57 deletions(-) diff --git a/src/maps.cpp b/src/maps.cpp index 06c4e48..26c9345 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -1,18 +1,15 @@ #include "maps.h" Maps::Maps(SDL_Renderer* render){ - int sound = false; + if(false) Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ); - if(sound == true){ - Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ); - } - //Set renderer - renderer = render; + //Set renderer + renderer = render; - //set the window's (renderer) dimensions - SDL_GetRendererOutputSize(renderer,&screenWidth,&screenHeight); + //set the window's (renderer) dimensions + SDL_GetRendererOutputSize(renderer,&screenWidth,&screenHeight); - //Set unit size + //Set unit size sz = screenWidth/16; //Set keyboard input @@ -28,20 +25,21 @@ Maps::Maps(SDL_Renderer* render){ } Maps::~Maps(){ - //Deallocate rows - for(int i = 0; i < blockTotal; ++i) delete blockRect[i]; - //Deallocate - delete blockRect; + //Deallocate rows + for(int i = 0; i < blockTotal; ++i) delete blockRect[i]; + //Deallocate + //delete blockRect; + Mix_HaltMusic(); - //Free the music + //Free the music Mix_FreeMusic( gMusic ); gMusic = NULL; } int Maps::map(std::string mapName){ - //Load map - playerPosX = 1*sz; + //Load map + playerPosX = 1*sz; playerPosY = 4*sz; mapWidth = sz*32; @@ -54,37 +52,37 @@ int Maps::map(std::string mapName){ */ Texture level; - level.setRenderer(renderer); - SDL_Color textColor = {0xFF,0xFF,0xFF,0xFF}; - level.loadFromRendererText("level: " + mapName,textColor); + level.setRenderer(renderer); + SDL_Color textColor = {0xFF,0xFF,0xFF,0xFF}; + level.loadFromRendererText("level: " + mapName,textColor); - Texture sandCastle; - sandCastle.setRenderer(renderer); - sandCastle.loadTexture("textures/foreground.png"); - SDL_Rect castleQuad = {0,0,sandCastle.getWidth(),sandCastle.getHeight()}; + Texture sandCastle; + sandCastle.setRenderer(renderer); + sandCastle.loadTexture("textures/foreground.png"); + SDL_Rect castleQuad = {0,0,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 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 stars; + stars.setRenderer(renderer); + stars.loadTexture("textures/stars.png"); + SDL_Rect starsQuad = {0,0,stars.getWidth(),stars.getHeight()}; + 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 - Player player(playerPosX,playerPosY,mapHeight,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 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(0,sz*11,screenWidth*2,sz*1,renderer,&player); @@ -107,8 +105,7 @@ int Maps::map(std::string mapName){ } } if(currentKeyStates[SDL_SCANCODE_ESCAPE]){ - musicQuit(); - return 0; + return 0; } //Clear the render and set the background color @@ -124,30 +121,22 @@ int Maps::map(std::string mapName){ sandMiddle.render(&middleQuad,camera.getPosX(),sandParallax); sandCastle.render(&castleQuad,camera.getPosX()); - //Print player + //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()); - ground.printAndCheck(camera.getPosX()); - ground2.printAndCheck(camera.getPosX()); - ground3.printAndCheck(camera.getPosX()); - ground4.printAndCheck(camera.getPosX()); - ground5.printAndCheck(camera.getPosX()); + //for(int i = 0; i < blockTotal; i++) ground.printAndCheck(blockRect[i],camera.getPosX()); + ground.printAndCheck(camera.getPosX()); + ground2.printAndCheck(camera.getPosX()); + ground3.printAndCheck(camera.getPosX()); + ground4.printAndCheck(camera.getPosX()); + ground5.printAndCheck(camera.getPosX()); - //Print the poweup and check collisions + //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; + return -1; } diff --git a/src/maps.h b/src/maps.h index 4a6d494..76c12a3 100644 --- a/src/maps.h +++ b/src/maps.h @@ -20,7 +20,6 @@ class Maps{ int map(std::string mapName); private: void loadMap(std::string path); - void musicQuit(); SDL_Renderer* renderer;