Better playing state's main loop handling
Cleaned a lot things to be more readable. Added some comments for more understandability. Better main loop structure in general.
This commit is contained in:
parent
6ad338c631
commit
1d7431ee83
|
@ -43,4 +43,6 @@ bin/
|
|||
obj/
|
||||
|
||||
### My files ###
|
||||
*.project.cpp
|
||||
*.project.cpp
|
||||
source/collisions.cpp
|
||||
source/collisions.h
|
||||
|
|
Binary file not shown.
|
@ -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(){
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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];
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -6,9 +6,7 @@
|
|||
Core core;
|
||||
|
||||
int main(int argc, char* args[]){
|
||||
//if(argc == 2) std::cout << "Initializing..." << std::endl;
|
||||
|
||||
core.coreInit();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -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){
|
||||
|
|
Loading…
Reference in New Issue