platform-test/src/maps.cpp

143 lines
3.8 KiB
C++
Raw Normal View History

2017-06-17 17:12:59 +00:00
#include "maps.h"
Maps::Maps(SDL_Renderer* render){
if(false) Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 );
2017-07-26 03:01:29 +00:00
//Set renderer
renderer = render;
2017-06-17 17:12:59 +00:00
//set the window's (renderer) dimensions
SDL_GetRendererOutputSize(renderer,&screenWidth,&screenHeight);
2017-06-17 17:12:59 +00:00
//Set unit size
2017-06-17 17:12:59 +00:00
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
Mix_HaltMusic();
2017-07-26 03:01:29 +00:00
//Free the music
2017-07-26 03:01:29 +00:00
Mix_FreeMusic( gMusic );
gMusic = NULL;
}
2017-06-17 17:12:59 +00:00
2017-07-27 17:57:08 +00:00
int Maps::map(std::string mapName){
//Load map
playerPosX = 1*sz;
2017-07-27 17:57:08 +00:00
playerPosY = 4*sz;
mapWidth = sz*32;
mapHeight = screenHeight;
/*
2017-07-26 14:18:24 +00:00
gMusic = Mix_LoadMUS( "assets/sapce-odyssey.ogg" );
2017-07-26 03:01:29 +00:00
if( gMusic == NULL ){
std::cout << "Failed to load beat music! SDL_mixer Error: " << Mix_GetError() << std::endl;
}
*/
2017-07-26 03:01:29 +00:00
2017-06-21 20:03:11 +00:00
Texture level;
level.setRenderer(renderer);
SDL_Color textColor = {0xFF,0xFF,0xFF,0xFF};
level.loadFromRendererText("level: " + mapName,textColor);
2017-06-21 20:03:11 +00:00
Texture sandCastle;
sandCastle.setRenderer(renderer);
sandCastle.loadTexture("textures/foreground.png");
SDL_Rect castleQuad = {0,0,sandCastle.getWidth(),sandCastle.getHeight()};
2017-07-26 03:01:29 +00:00
Texture sandMiddle;
sandMiddle.setRenderer(renderer);
sandMiddle.loadTexture("textures/sand-middleground.png");
SDL_Rect middleQuad = {0,0,sandMiddle.getWidth(),sandMiddle.getHeight()};
float sandParallax = 0.5;
2017-07-26 03:01:29 +00:00
Texture stars;
stars.setRenderer(renderer);
stars.loadTexture("textures/stars.png");
SDL_Rect starsQuad = {0,0,stars.getWidth(),stars.getHeight()};
float starsParallax = 0.2;
2017-07-26 03:01:29 +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);
2017-06-17 17:12:59 +00:00
//Initialize the block class and set the player and the renderer
2017-07-27 17:57:08 +00:00
Block ground(0,sz*11,screenWidth*2,sz*1,renderer,&player);
Block ground2(sz*21,sz*10,sz*11,sz*1,renderer,&player);
Block ground3(sz*22,sz*9,sz*10,sz*1,renderer,&player);
Block ground4(sz*30,sz*8,sz*2,sz*1,renderer,&player);
Block ground5(sz*31,0,sz*1,sz*8,renderer,&player);
//Block ground(renderer,&player);
//Mix_PlayMusic( gMusic, -1 );
2017-07-26 03:01:29 +00:00
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]){
return 0;
2017-07-26 03:01:29 +00:00
}
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);
sandMiddle.render(&middleQuad,camera.getPosX(),sandParallax);
sandCastle.render(&castleQuad,camera.getPosX());
//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());
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
2017-06-17 17:12:59 +00:00
powerup.printAndCheck(camera.getPosX());
//Render
SDL_RenderPresent(renderer);
}
return -1;
}