diff --git a/.gitignore b/.gitignore index 6eb33cc..0f018bf 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,4 @@ obj/ *.project.cpp source/collisions.cpp source/collisions.h +*.psd diff --git a/assets/sand-castle.ogg b/assets/sand-castle.ogg new file mode 100644 index 0000000..670976a Binary files /dev/null and b/assets/sand-castle.ogg differ diff --git a/overworld.map b/overworld.map index 87fa7a4..7af1670 100644 --- a/overworld.map +++ b/overworld.map @@ -1 +1 @@ -1 7 5 8 7 2 2 4 9 1 2 6 9 1 2 0 11 16 1 20 11 16 1 \ No newline at end of file +1 7 1 0 11 32 1 \ No newline at end of file diff --git a/platform-test.exe b/platform-test.exe index 0ac96b2..0bf93dc 100644 Binary files a/platform-test.exe and b/platform-test.exe differ diff --git a/source/entity/block.cpp b/source/entity/block.cpp index ad24395..9a0bdf2 100644 --- a/source/entity/block.cpp +++ b/source/entity/block.cpp @@ -18,6 +18,6 @@ void Block::printAndCheck(int dimensions[], int cameraX){ rect.x = dimensions[0] - cameraX; //Set render color and render the rectangle - SDL_SetRenderDrawColor(renderer,0,0xFF,0,0xFF); + SDL_SetRenderDrawColor(renderer,169,145,73,0xFF); SDL_RenderFillRect(renderer,&rect); }; diff --git a/source/maps.cpp b/source/maps.cpp index 937d0cf..a229e06 100644 --- a/source/maps.cpp +++ b/source/maps.cpp @@ -1,6 +1,11 @@ #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; @@ -27,6 +32,11 @@ Maps::~Maps(){ 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){ @@ -58,7 +68,12 @@ void Maps::loadMap(std::string path){ if(blockRect[i][1]+blockRect[i][3] > mapHeight) mapHeight = blockRect[i][1]+blockRect[i][3]; } - //Very descriptive by itself + 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(); }; @@ -71,6 +86,29 @@ int Maps::map(std::string mapName){ 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 @@ -85,6 +123,8 @@ int Maps::map(std::string mapName){ //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; @@ -95,10 +135,13 @@ int Maps::map(std::string mapName){ quit = true; } } - if(currentKeyStates[SDL_SCANCODE_ESCAPE]) return 0; + if(currentKeyStates[SDL_SCANCODE_ESCAPE]){ + musicQuit(); + return 0; + } //Clear the render and set the background color - SDL_SetRenderDrawColor(renderer,0,0,100,0xFF); + SDL_SetRenderDrawColor(renderer,10,20,73,0xFF); SDL_RenderClear(renderer); level.render(&levelRect); @@ -106,6 +149,11 @@ int Maps::map(std::string mapName){ //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; @@ -118,6 +166,13 @@ int Maps::map(std::string mapName){ //Render SDL_RenderPresent(renderer); } - + musicQuit(); return -1; }; + +void Maps::musicQuit(){ + Mix_HaltMusic(); + //Free the music + Mix_FreeMusic( gMusic ); + gMusic = NULL; +}; diff --git a/source/maps.h b/source/maps.h index 461bf7a..01030d2 100644 --- a/source/maps.h +++ b/source/maps.h @@ -10,6 +10,7 @@ #include #include #include"texture.h" +#include class Maps{ public: @@ -18,6 +19,7 @@ class Maps{ int map(std::string mapName); private: void loadMap(std::string path); + void musicQuit(); SDL_Renderer* renderer; @@ -28,6 +30,8 @@ class Maps{ int mapWidth, mapHeight; int playerPosX, playerPosY; const Uint8* currentKeyStates; + + Mix_Music *gMusic = NULL; }; #endif // MAPS_H diff --git a/source/texture.cpp b/source/texture.cpp index 15343d1..a469537 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -111,6 +111,54 @@ void Texture::render(SDL_Rect* quad){ SDL_RenderCopy(renderer,texture,&frame,quad); }; +void Texture::render(SDL_Rect* quad, int cameraX){ + //Make a frame of the size of the quad + SDL_Rect frame = {0,0,quad->w,quad->h}; + + quad->x = quad->x - cameraX; + + //Render the texture + SDL_RenderCopy(renderer,texture,&frame,quad); + + quad->x = quad->x + cameraX; +}; + +void Texture::render(SDL_Rect* quad, int cameraX,float parallax){ + //Make a frame of the size of the quad + SDL_Rect frame = {0,0,quad->w,quad->h}; + + quad->x = quad->x - cameraX * parallax; + + //Render the texture + SDL_RenderCopy(renderer,texture,&frame,quad); + + quad->x = quad->x + cameraX * parallax; +}; + +/*void Texture::render(SDL_Rect* quad, int cameraX,int parallax, int vel){ + //Make a frame of the size of the quad + SDL_Rect frame = {0,0,quad->w,quad->h}; + + quad->x -= cameraX / parallax; + + quad->x += vel; + + if(quad->x >= 160){ + quad->x -= 160 + szW; + if(quad->x >= 0){ + quad->x += 160 + szW; + } + else SDL_RenderCopy(renderer,texture,&frame,quad); + + quad->x += 160 + szW; + } + + //Render the texture + SDL_RenderCopy(renderer,texture,&frame,quad); + + quad->x += cameraX / parallax; +};*/ + int Texture::getWidth(){ return szW; }; diff --git a/source/texture.h b/source/texture.h index b599a06..6c1d957 100644 --- a/source/texture.h +++ b/source/texture.h @@ -15,9 +15,14 @@ class Texture{ ~Texture(); void loadTexture(std::string path); bool loadFromRendererText(std::string textureText, SDL_Color textColor); + void free(); void render(SDL_Rect* quad,SDL_Rect* frame); void render(SDL_Rect* quad); + void render(SDL_Rect* quad, int cameraX,float parallax); + void render(SDL_Rect* quad, int cameraX); + //void render(SDL_Rect* quad, int cameraX,int parallax, int vel); + int getWidth(); int getHeight(); void setRenderer (SDL_Renderer* render); diff --git a/textures/castle.png b/textures/castle.png new file mode 100644 index 0000000..1e5da7b Binary files /dev/null and b/textures/castle.png differ diff --git a/textures/clouds.png b/textures/clouds.png new file mode 100644 index 0000000..59a1e12 Binary files /dev/null and b/textures/clouds.png differ diff --git a/textures/sand-middleground.png b/textures/sand-middleground.png new file mode 100644 index 0000000..32db947 Binary files /dev/null and b/textures/sand-middleground.png differ diff --git a/textures/stars.png b/textures/stars.png new file mode 100644 index 0000000..27dcbf2 Binary files /dev/null and b/textures/stars.png differ