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
|
@ -44,3 +44,5 @@ obj/
|
||||||
|
|
||||||
### My files ###
|
### My files ###
|
||||||
*.project.cpp
|
*.project.cpp
|
||||||
|
source/collisions.cpp
|
||||||
|
source/collisions.h
|
||||||
|
|
Binary file not shown.
|
@ -2,18 +2,18 @@
|
||||||
|
|
||||||
#include"camera.h"
|
#include"camera.h"
|
||||||
|
|
||||||
Camera::Camera(int mapWidth,int screenWidth){
|
Camera::Camera(int mapWidth, int mapHeight,int screenWidth, int screenHeight){
|
||||||
mW = mapWidth;
|
mW = mapWidth;
|
||||||
|
mH = mapHeight;
|
||||||
scW = screenWidth;
|
scW = screenWidth;
|
||||||
|
scH = screenHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::update(int playerX, int playerY){
|
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;
|
if(playerX >= scW/2) posX = playerX - scW/2;
|
||||||
else posX = 0;
|
else posX = 0;
|
||||||
if(playerX >= mW - scW/2) posX = mW - scW;
|
if(playerX >= mW - scW/2) posX = mW - scW;
|
||||||
|
|
||||||
//posY = playerY;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int Camera::getPosX(){
|
int Camera::getPosX(){
|
||||||
|
|
|
@ -7,13 +7,14 @@
|
||||||
|
|
||||||
class Camera{
|
class Camera{
|
||||||
public:
|
public:
|
||||||
Camera(int mapWidth,int screenWidth);
|
Camera(int mapWidth, int mapHeight,int screenWidth, int screenHeight);
|
||||||
void update(int playerX, int playerY);
|
void update(int playerX, int playerY);
|
||||||
int getPosX();
|
int getPosX();
|
||||||
int getPosY();
|
int getPosY();
|
||||||
private:
|
private:
|
||||||
int posX, posY;
|
int posX, posY;
|
||||||
int mW, scW;
|
int mW, mH;
|
||||||
|
int scW, scH;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -26,7 +26,6 @@ int Core::coreInit(){
|
||||||
int gamestate = 0;
|
int gamestate = 0;
|
||||||
|
|
||||||
//-1 = quit, 0 = Menu, 1 = playing,
|
//-1 = quit, 0 = Menu, 1 = playing,
|
||||||
|
|
||||||
while (gamestate != -1){
|
while (gamestate != -1){
|
||||||
if(gamestate == 0) gamestate = menu(gRenderer);
|
if(gamestate == 0) gamestate = menu(gRenderer);
|
||||||
if(gamestate == 1) gamestate = map1();
|
if(gamestate == 1) gamestate = map1();
|
||||||
|
@ -37,11 +36,38 @@ int Core::coreInit(){
|
||||||
};
|
};
|
||||||
|
|
||||||
int Core::map1(){
|
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;
|
bool quit = false;
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
|
|
||||||
|
//Game loop
|
||||||
while(quit == false){
|
while(quit == false){
|
||||||
while(SDL_PollEvent(&e)!=0){
|
while(SDL_PollEvent(&e)!=0){
|
||||||
if(e.type == SDL_QUIT){
|
if(e.type == SDL_QUIT){
|
||||||
|
@ -51,20 +77,25 @@ int Core::map1(){
|
||||||
SDL_SetRenderDrawColor(gRenderer,0,0,100,0xFF);
|
SDL_SetRenderDrawColor(gRenderer,0,0,100,0xFF);
|
||||||
SDL_RenderClear(gRenderer);
|
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);
|
for(int i = 0; i < 5; i++){
|
||||||
Block wallA(8*sz,7*sz,sz*2,sz*2,&gRenderer,&posweg,camera.getPosX());
|
ground.printAndCheck(blockRect[i],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());
|
powerup.printAndCheck(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());
|
|
||||||
|
|
||||||
|
|
||||||
SDL_RenderPresent(gRenderer);
|
SDL_RenderPresent(gRenderer);
|
||||||
}
|
}
|
||||||
return -1;
|
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();
|
void close();
|
||||||
|
|
||||||
int map1();
|
int map1();
|
||||||
|
void setRectSize(int rect[],int x, int y, int w, int h);
|
||||||
|
|
||||||
const int SCREEN_WIDTH = 640;
|
const int SCREEN_WIDTH = 640;
|
||||||
const int SCREEN_HEIGHT = 480;
|
const int SCREEN_HEIGHT = 480;
|
||||||
|
@ -27,8 +28,6 @@ class Core{
|
||||||
|
|
||||||
SDL_Window* gWindow = NULL;
|
SDL_Window* gWindow = NULL;
|
||||||
SDL_Renderer* gRenderer = NULL;
|
SDL_Renderer* gRenderer = NULL;
|
||||||
|
|
||||||
//Player posweg(&gRenderer);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,21 +1,23 @@
|
||||||
#include"block.h"
|
#include"block.h"
|
||||||
|
|
||||||
Block::Block(int x, int y, int w, int h,SDL_Renderer** render, Player* player, int cameraX){
|
Block::Block(SDL_Renderer* render, Player* ply){
|
||||||
//set the rectangle dimensions
|
renderer = render; //Set the renderer
|
||||||
rect = {x,y,w,h};
|
player = ply; //Set the player class to check
|
||||||
|
|
||||||
//Set the renderer pointer
|
type = 1; //Set the entity type (1 = block)
|
||||||
renderer = render;
|
};
|
||||||
|
|
||||||
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
|
//Check if it collides with the player
|
||||||
SDL_Rect cameraFix = rect;
|
player->check(rect,type);
|
||||||
cameraFix.x -= cameraX;
|
|
||||||
|
//Adjust de SDL_Rect x to follow the camera
|
||||||
|
rect.x = dimensions[0] - cameraX;
|
||||||
|
|
||||||
//Set render color and render the rectangle
|
//Set render color and render the rectangle
|
||||||
SDL_SetRenderDrawColor(*renderer,0,0xFF,0,0xFF);
|
SDL_SetRenderDrawColor(renderer,0,0xFF,0,0xFF);
|
||||||
SDL_RenderFillRect(*renderer,&cameraFix);
|
SDL_RenderFillRect(renderer,&rect);
|
||||||
|
};
|
||||||
player->check(rect,type);
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,10 +12,11 @@ class Entity;
|
||||||
|
|
||||||
class Block: public Entity{
|
class Block: public Entity{
|
||||||
public:
|
public:
|
||||||
Block(int x,int y,int w,int h,SDL_Renderer** render, Player* player,int);
|
Block(SDL_Renderer* render, Player* ply);
|
||||||
void print(int cameraX);
|
void printAndCheck(int dimensions[], int cameraX);
|
||||||
protected:
|
protected:
|
||||||
Player* posu;
|
Player* player;
|
||||||
|
SDL_Renderer* renderer;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -2,13 +2,7 @@
|
||||||
|
|
||||||
#include"player.h"
|
#include"player.h"
|
||||||
|
|
||||||
int Player::print(int x,int y,int cameraX){
|
int Player::print(int cameraX){
|
||||||
if(first == true){
|
|
||||||
first = false;
|
|
||||||
posX = x;
|
|
||||||
posY = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
oldPosX = posX;
|
oldPosX = posX;
|
||||||
oldPosY = posY;
|
oldPosY = posY;
|
||||||
|
|
||||||
|
@ -18,7 +12,7 @@ int Player::print(int x,int y,int cameraX){
|
||||||
SDL_Rect cameraFix = rect;
|
SDL_Rect cameraFix = rect;
|
||||||
cameraFix.x -= cameraX;
|
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]);
|
if(power == 0)ply.render(&cameraFix,&plyFrame[0]);
|
||||||
else{
|
else{
|
||||||
ply.render(&cameraFix,&plyRun);
|
ply.render(&cameraFix,&plyRun);
|
||||||
|
@ -31,22 +25,25 @@ int Player::print(int x,int y,int cameraX){
|
||||||
topCollision = false;
|
topCollision = false;
|
||||||
|
|
||||||
//Check if the player has fell out of the world (not horizontally, sadly).
|
//Check if the player has fell out of the world (not horizontally, sadly).
|
||||||
int SCREEN_WIDTH, SCREEN_HEIGHT;
|
if(posY >= levelHeight) return 1;
|
||||||
SDL_GetRendererOutputSize(*renderer,&SCREEN_WIDTH,&SCREEN_HEIGHT);
|
|
||||||
|
|
||||||
if(posY >= SCREEN_HEIGHT) return 1;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
Player::Player(SDL_Renderer** render){
|
Player::Player(int x,int y, int lvlH, SDL_Renderer* render){
|
||||||
szW = 40;
|
levelHeight = lvlH;
|
||||||
|
|
||||||
|
posX = x;
|
||||||
|
posY = y;
|
||||||
|
|
||||||
|
szW = 40;
|
||||||
szH = 40;
|
szH = 40;
|
||||||
rect.w = szW;
|
rect.w = szW;
|
||||||
rect.h = szH;
|
rect.h = szH;
|
||||||
|
|
||||||
renderer = render;
|
renderer = render;
|
||||||
|
|
||||||
ply.setRenderer(renderer);
|
ply.setRenderer(&renderer);
|
||||||
loadMedia();
|
loadMedia();
|
||||||
//first = true;
|
//first = true;
|
||||||
|
|
||||||
|
|
|
@ -14,25 +14,28 @@ class Entity;
|
||||||
|
|
||||||
class Player: public Entity{
|
class Player: public Entity{
|
||||||
public:
|
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);
|
int check(SDL_Rect rectA,int type);
|
||||||
Player(SDL_Renderer** render);
|
|
||||||
private:
|
private:
|
||||||
void loadMedia();
|
void loadMedia();
|
||||||
void move();
|
void move();
|
||||||
int intVelX();
|
int intVelX();
|
||||||
SDL_Renderer** renderer;
|
SDL_Renderer* renderer;
|
||||||
DeltaTime dTime;
|
DeltaTime dTime;
|
||||||
|
|
||||||
bool ground;
|
bool ground;
|
||||||
bool topCollision;
|
bool topCollision;
|
||||||
|
bool isRunning = false;
|
||||||
|
bool ifRunning = false;
|
||||||
|
|
||||||
//int posX, posY;
|
//int posX, posY;
|
||||||
float velocityX = 0;
|
float velocityX = 0;
|
||||||
float velocityY = 0;
|
float velocityY = 0;
|
||||||
bool isRunning = false;
|
|
||||||
bool ifRunning = false;
|
|
||||||
int oldPosX, oldPosY;
|
int oldPosX, oldPosY;
|
||||||
bool first;
|
|
||||||
int power = 0;
|
int power = 0;
|
||||||
|
int levelHeight;
|
||||||
|
|
||||||
Texture ply;
|
Texture ply;
|
||||||
SDL_Rect plyFrame[3];
|
SDL_Rect plyFrame[3];
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#include"powerup.h"
|
#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};
|
rect = {x,y,20,20};
|
||||||
|
|
||||||
renderer = render;
|
renderer = render;
|
||||||
|
@ -19,4 +19,29 @@ Powerup::Powerup(int x, int y, SDL_Renderer** render,Player* player, int cameraX
|
||||||
SDL_RenderFillRect(*renderer,&cameraFix);
|
SDL_RenderFillRect(*renderer,&cameraFix);
|
||||||
}
|
}
|
||||||
//posu->check(rect, type);
|
//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{
|
class Powerup: public Entity{
|
||||||
public:
|
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:
|
protected:
|
||||||
Player* posu;
|
Player* player;
|
||||||
|
SDL_Renderer* renderer;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -6,8 +6,6 @@
|
||||||
Core core;
|
Core core;
|
||||||
|
|
||||||
int main(int argc, char* args[]){
|
int main(int argc, char* args[]){
|
||||||
//if(argc == 2) std::cout << "Initializing..." << std::endl;
|
|
||||||
|
|
||||||
core.coreInit();
|
core.coreInit();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -27,20 +27,12 @@ int menu(SDL_Renderer* renderer){
|
||||||
button[i].y = ((i+1)*spc+i*btnHeight)+SCREEN_HEIGHT-btnSpc;
|
button[i].y = ((i+1)*spc+i*btnHeight)+SCREEN_HEIGHT-btnSpc;
|
||||||
button[i].w = btnWidth;
|
button[i].w = btnWidth;
|
||||||
button[i].h = btnHeight;
|
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;
|
int select = 0;
|
||||||
bool wait = 0;
|
bool wait = 0;
|
||||||
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
|
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
std::cout << std::endl << "Entering main loop..." << std::endl;
|
|
||||||
|
|
||||||
while(!quit){
|
while(!quit){
|
||||||
while(SDL_PollEvent(&e)!=0){
|
while(SDL_PollEvent(&e)!=0){
|
||||||
if(e.type == SDL_QUIT){
|
if(e.type == SDL_QUIT){
|
||||||
|
|
Loading…
Reference in New Issue