diff --git a/.gitignore b/.gitignore index d54a7ec..6eb33cc 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,6 @@ bin/ obj/ ### My files ### -*.project.cpp \ No newline at end of file +*.project.cpp +source/collisions.cpp +source/collisions.h diff --git a/platform-test.exe b/platform-test.exe index 0939c22..bda9abf 100644 Binary files a/platform-test.exe and b/platform-test.exe differ diff --git a/source/camera.cpp b/source/camera.cpp index 8a583f3..3a240e4 100644 --- a/source/camera.cpp +++ b/source/camera.cpp @@ -2,18 +2,18 @@ #include"camera.h" -Camera::Camera(int mapWidth,int screenWidth){ - mW = mapWidth; +Camera::Camera(int mapWidth, int mapHeight,int screenWidth, int screenHeight){ + mW = mapWidth; + mH = mapHeight; scW = screenWidth; + scH = screenHeight; } void Camera::update(int playerX, int playerY){ - //Make the camera when the player hits the middle of the screem + //Make the camera move when the player hits the middle of the screen if(playerX >= scW/2) posX = playerX - scW/2; else posX = 0; if(playerX >= mW - scW/2) posX = mW - scW; - - //posY = playerY; }; int Camera::getPosX(){ diff --git a/source/camera.h b/source/camera.h index a88dc73..149b613 100644 --- a/source/camera.h +++ b/source/camera.h @@ -7,13 +7,14 @@ class Camera{ public: - Camera(int mapWidth,int screenWidth); + Camera(int mapWidth, int mapHeight,int screenWidth, int screenHeight); void update(int playerX, int playerY); int getPosX(); int getPosY(); private: int posX, posY; - int mW, scW; + int mW, mH; + int scW, scH; }; #endif diff --git a/source/core.cpp b/source/core.cpp index dad4029..8c5168f 100644 --- a/source/core.cpp +++ b/source/core.cpp @@ -26,7 +26,6 @@ int Core::coreInit(){ int gamestate = 0; //-1 = quit, 0 = Menu, 1 = playing, - while (gamestate != -1){ if(gamestate == 0) gamestate = menu(gRenderer); if(gamestate == 1) gamestate = map1(); @@ -37,11 +36,38 @@ int Core::coreInit(){ }; int Core::map1(){ - Player posweg(&gRenderer); + //Set the map dimensions + int mapWidth = 36*sz; + int mapHeight = 12*sz; + + //set the window's (renderer) dimensions + int screenWidth, screenHeight; + SDL_GetRendererOutputSize(gRenderer,&screenWidth,&screenHeight); + + //Initialize the player, set its position, pass the map's size and set the renderer + Player player(40,8*sz,mapHeight,gRenderer); + + //Set the camera and pass the map and screen's dimensions + Camera camera(mapWidth,mapHeight,screenWidth,screenHeight); + + //Initialize the block class and set the player and the renderer + Block ground(gRenderer,&player); + + //Set the quantity and size of the blocks + int blockRect[5][4]; + setRectSize(blockRect[0], 8*sz, 7*sz, 2*sz,2*sz); + setRectSize(blockRect[1], 4*sz, 9*sz, sz,2*sz); + setRectSize(blockRect[2], 6*sz, 9*sz, sz,2*sz); + setRectSize(blockRect[3], 0 ,11*sz,16*sz, sz); + setRectSize(blockRect[4],20*sz,11*sz,16*sz, sz); + + //Initialize the powerup, set his position, pass the renderer and the player + Powerup powerup(13*sz,8*sz,gRenderer,&player); bool quit = false; SDL_Event e; + //Game loop while(quit == false){ while(SDL_PollEvent(&e)!=0){ if(e.type == SDL_QUIT){ @@ -51,20 +77,25 @@ int Core::map1(){ SDL_SetRenderDrawColor(gRenderer,0,0,100,0xFF); SDL_RenderClear(gRenderer); - Camera camera(36*sz,SCREEN_WIDTH); + camera.update(player.getRectangle().x,player.getRectangle().y); - if(posweg.print(40,8*sz ,camera.getPosX()) == 1) return 0; + if(player.print(camera.getPosX()) == 1) return 0; - camera.update(posweg.getRectangle().x,posweg.getRectangle().y); - Block wallA(8*sz,7*sz,sz*2,sz*2,&gRenderer,&posweg,camera.getPosX()); - Block wallB(4*sz,9*sz,sz,sz*2,&gRenderer,&posweg,camera.getPosX()); - Block wallC(6*sz,9*sz,sz,sz*2,&gRenderer,&posweg,camera.getPosX()); - Block ground(0,11*sz,16*sz,sz,&gRenderer,&posweg,camera.getPosX()); - Block ground2(sz*20,sz*11,sz*16,sz,&gRenderer,&posweg,camera.getPosX()); - Powerup powerup(13*sz,8*sz,&gRenderer,&posweg,camera.getPosX()); + for(int i = 0; i < 5; i++){ + ground.printAndCheck(blockRect[i],camera.getPosX()); + } + + powerup.printAndCheck(camera.getPosX()); SDL_RenderPresent(gRenderer); } return -1; }; + +void Core::setRectSize(int rect[],int x, int y, int w, int h){ + rect[0] = x; + rect[1] = y; + rect[2] = w; + rect[3] = h; +} diff --git a/source/core.h b/source/core.h index f8bda69..bf38d94 100644 --- a/source/core.h +++ b/source/core.h @@ -20,6 +20,7 @@ class Core{ void close(); int map1(); + void setRectSize(int rect[],int x, int y, int w, int h); const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; @@ -27,8 +28,6 @@ class Core{ SDL_Window* gWindow = NULL; SDL_Renderer* gRenderer = NULL; - - //Player posweg(&gRenderer); }; #endif diff --git a/source/entity/block.cpp b/source/entity/block.cpp index a3fa7cd..ad24395 100644 --- a/source/entity/block.cpp +++ b/source/entity/block.cpp @@ -1,21 +1,23 @@ #include"block.h" -Block::Block(int x, int y, int w, int h,SDL_Renderer** render, Player* player, int cameraX){ - //set the rectangle dimensions - rect = {x,y,w,h}; +Block::Block(SDL_Renderer* render, Player* ply){ + renderer = render; //Set the renderer + player = ply; //Set the player class to check - //Set the renderer pointer - renderer = render; + type = 1; //Set the entity type (1 = block) +}; - type = 1; +void Block::printAndCheck(int dimensions[], int cameraX){ + //Set the block's rect's dimensions and position + rect = {dimensions[0],dimensions[1],dimensions[2],dimensions[3]}; - //New SDL_Rect to make the objects follow the camera - SDL_Rect cameraFix = rect; - cameraFix.x -= cameraX; + //Check if it collides with the player + player->check(rect,type); + + //Adjust de SDL_Rect x to follow the camera + rect.x = dimensions[0] - cameraX; //Set render color and render the rectangle - SDL_SetRenderDrawColor(*renderer,0,0xFF,0,0xFF); - SDL_RenderFillRect(*renderer,&cameraFix); - - player->check(rect,type); -} + SDL_SetRenderDrawColor(renderer,0,0xFF,0,0xFF); + SDL_RenderFillRect(renderer,&rect); +}; diff --git a/source/entity/block.h b/source/entity/block.h index 1afed18..58cc245 100644 --- a/source/entity/block.h +++ b/source/entity/block.h @@ -12,10 +12,11 @@ class Entity; class Block: public Entity{ public: - Block(int x,int y,int w,int h,SDL_Renderer** render, Player* player,int); - void print(int cameraX); + Block(SDL_Renderer* render, Player* ply); + void printAndCheck(int dimensions[], int cameraX); protected: - Player* posu; + Player* player; + SDL_Renderer* renderer; }; #endif diff --git a/source/entity/player.cpp b/source/entity/player.cpp index 5b0f0fe..6fcaa05 100644 --- a/source/entity/player.cpp +++ b/source/entity/player.cpp @@ -2,13 +2,7 @@ #include"player.h" -int Player::print(int x,int y,int cameraX){ - if(first == true){ - first = false; - posX = x; - posY = y; - } - +int Player::print(int cameraX){ oldPosX = posX; oldPosY = posY; @@ -18,7 +12,7 @@ int Player::print(int x,int y,int cameraX){ SDL_Rect cameraFix = rect; cameraFix.x -= cameraX; - SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF); + SDL_SetRenderDrawColor(renderer,0xFF,0,0,0xFF); if(power == 0)ply.render(&cameraFix,&plyFrame[0]); else{ ply.render(&cameraFix,&plyRun); @@ -31,22 +25,25 @@ int Player::print(int x,int y,int cameraX){ topCollision = false; //Check if the player has fell out of the world (not horizontally, sadly). - int SCREEN_WIDTH, SCREEN_HEIGHT; - SDL_GetRendererOutputSize(*renderer,&SCREEN_WIDTH,&SCREEN_HEIGHT); - - if(posY >= SCREEN_HEIGHT) return 1; + if(posY >= levelHeight) return 1; return 0; }; -Player::Player(SDL_Renderer** render){ - szW = 40; +Player::Player(int x,int y, int lvlH, SDL_Renderer* render){ + levelHeight = lvlH; + + posX = x; + posY = y; + + szW = 40; szH = 40; rect.w = szW; rect.h = szH; + renderer = render; - ply.setRenderer(renderer); + ply.setRenderer(&renderer); loadMedia(); //first = true; diff --git a/source/entity/player.h b/source/entity/player.h index 9b6bcbc..c984a5c 100644 --- a/source/entity/player.h +++ b/source/entity/player.h @@ -14,25 +14,28 @@ class Entity; class Player: public Entity{ public: - int print(int x, int y,int cameraX); + Player(int x,int y,int lvlH,SDL_Renderer* render); + int print(int cameraX); int check(SDL_Rect rectA,int type); - Player(SDL_Renderer** render); private: void loadMedia(); void move(); int intVelX(); - SDL_Renderer** renderer; + SDL_Renderer* renderer; DeltaTime dTime; + bool ground; bool topCollision; + bool isRunning = false; + bool ifRunning = false; + //int posX, posY; float velocityX = 0; float velocityY = 0; - bool isRunning = false; - bool ifRunning = false; + int oldPosX, oldPosY; - bool first; int power = 0; + int levelHeight; Texture ply; SDL_Rect plyFrame[3]; diff --git a/source/entity/powerup.cpp b/source/entity/powerup.cpp index d0b6b9b..619f31b 100644 --- a/source/entity/powerup.cpp +++ b/source/entity/powerup.cpp @@ -2,7 +2,7 @@ #include"powerup.h" -Powerup::Powerup(int x, int y, SDL_Renderer** render,Player* player, int cameraX){ +/*Powerup::Powerup(int x, int y, SDL_Renderer** render,Player* player, int cameraX){ rect = {x,y,20,20}; renderer = render; @@ -12,11 +12,36 @@ Powerup::Powerup(int x, int y, SDL_Renderer** render,Player* player, int cameraX //New SDL_Rect to make the objects follow the camera SDL_Rect cameraFix = rect; cameraFix.x -= cameraX; - + //Set render color and render the rectangle if(player->check(rect,type)==0){ SDL_SetRenderDrawColor(*renderer,0xFF,0xFF,0,0xFF); SDL_RenderFillRect(*renderer,&cameraFix); } //posu->check(rect, type); +};*/ + +Powerup::Powerup(int x,int y,SDL_Renderer* render, Player* ply){ + renderer = render; //Set the renderer + player = ply; //Set the player class to check + + type = 2; //Set the entity type (2 = powerup) + + rect = {x,y,20,20};//Set the powerup's position and dimensions +}; + +void Powerup::printAndCheck(int cameraX){ + //Check if it collides with the player + if(player->check(rect,type) == 0){ + + //Adjust de SDL_Rect x to follow the camera + rect.x -= cameraX; + + //Set render color and render the rectangle + SDL_SetRenderDrawColor(renderer,0xFF,0xFF,0,0xFF); + SDL_RenderFillRect(renderer,&rect); + + //Change again the x position of rect + rect.x += cameraX; + } }; diff --git a/source/entity/powerup.h b/source/entity/powerup.h index 88a718d..c365d71 100644 --- a/source/entity/powerup.h +++ b/source/entity/powerup.h @@ -12,9 +12,11 @@ class Entity; class Powerup: public Entity{ public: - Powerup(int x, int y, SDL_Renderer** render, Player* player,int); + Powerup(int x, int y,SDL_Renderer* render, Player* ply); + void printAndCheck(int cameraX); protected: - Player* posu; + Player* player; + SDL_Renderer* renderer; }; #endif diff --git a/source/main.cpp b/source/main.cpp index b41f14a..9380213 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -6,9 +6,7 @@ Core core; int main(int argc, char* args[]){ - //if(argc == 2) std::cout << "Initializing..." << std::endl; - core.coreInit(); - + return 0; } diff --git a/source/menu.cpp b/source/menu.cpp index af6cfdc..2efd75f 100644 --- a/source/menu.cpp +++ b/source/menu.cpp @@ -27,20 +27,12 @@ int menu(SDL_Renderer* renderer){ button[i].y = ((i+1)*spc+i*btnHeight)+SCREEN_HEIGHT-btnSpc; button[i].w = btnWidth; button[i].h = btnHeight; - std::cout << "Button " << i+1 - << " set as -> x: " << button[i].x - << " y: " << button[i].y - << " w: " << button[i].w - << " h: " << button[i].h - << std::endl; } int select = 0; bool wait = 0; const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL); - std::cout << std::endl << "Entering main loop..." << std::endl; - while(!quit){ while(SDL_PollEvent(&e)!=0){ if(e.type == SDL_QUIT){