Music and textures

This commit is contained in:
Pòsweg 2017-07-26 05:01:29 +02:00
parent ccc5467d64
commit e40533d4ab
13 changed files with 119 additions and 6 deletions

1
.gitignore vendored
View File

@ -46,3 +46,4 @@ obj/
*.project.cpp
source/collisions.cpp
source/collisions.h
*.psd

BIN
assets/sand-castle.ogg Normal file

Binary file not shown.

View File

@ -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
1 7 1 0 11 32 1

Binary file not shown.

View File

@ -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);
};

View File

@ -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;
};

View File

@ -10,6 +10,7 @@
#include<fstream>
#include<algorithm>
#include"texture.h"
#include<SDL_mixer.h>
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

View File

@ -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;
};

View File

@ -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);

BIN
textures/castle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
textures/clouds.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
textures/stars.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB