More organization and maps isolation
This commit is contained in:
parent
1d7431ee83
commit
b62718250a
Before Width: | Height: | Size: 361 KiB After Width: | Height: | Size: 361 KiB |
Binary file not shown.
|
@ -11,9 +11,12 @@ Camera::Camera(int mapWidth, int mapHeight,int screenWidth, int screenHeight){
|
|||
|
||||
void Camera::update(int playerX, int playerY){
|
||||
//Make the camera move when the player hits the middle of the screen
|
||||
if(playerX >= scW/2) posX = playerX - scW/2;
|
||||
if(mW != scW){
|
||||
if(playerX >= scW/2) posX = playerX - scW/2;
|
||||
else posX = 0;
|
||||
if(playerX >= mW - scW/2) posX = mW - scW;
|
||||
}
|
||||
else posX = 0;
|
||||
if(playerX >= mW - scW/2) posX = mW - scW;
|
||||
};
|
||||
|
||||
int Camera::getPosX(){
|
||||
|
|
|
@ -7,8 +7,6 @@ void Core::init(){
|
|||
gWindow = SDL_CreateWindow("Platform Test!",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
|
||||
gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
SDL_SetRenderDrawColor(gRenderer,0xFF,0xFF,0xFF,0xFF);
|
||||
|
||||
sz = SCREEN_WIDTH/16;
|
||||
}
|
||||
|
||||
void Core::close(){
|
||||
|
@ -23,79 +21,24 @@ void Core::close(){
|
|||
int Core::coreInit(){
|
||||
init();
|
||||
|
||||
Maps maps(gRenderer);
|
||||
|
||||
int gamestate = 0;
|
||||
|
||||
//-1 = quit, 0 = Menu, 1 = playing,
|
||||
while (gamestate != -1){
|
||||
if(gamestate == 0) gamestate = menu(gRenderer);
|
||||
if(gamestate == 1) gamestate = map1();
|
||||
while(gamestate != -1){
|
||||
switch(gamestate){
|
||||
case 0:
|
||||
gamestate = menu(gRenderer);
|
||||
break;
|
||||
case 1:
|
||||
gamestate = maps.map1();
|
||||
break;
|
||||
case 2:
|
||||
gamestate = maps.map2();
|
||||
}
|
||||
}
|
||||
|
||||
close();
|
||||
return 0;
|
||||
};
|
||||
|
||||
int Core::map1(){
|
||||
//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){
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
SDL_SetRenderDrawColor(gRenderer,0,0,100,0xFF);
|
||||
SDL_RenderClear(gRenderer);
|
||||
|
||||
camera.update(player.getRectangle().x,player.getRectangle().y);
|
||||
|
||||
if(player.print(camera.getPosX()) == 1) return 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -5,12 +5,8 @@
|
|||
|
||||
#include<iostream>
|
||||
#include<SDL.h>
|
||||
#include"entity/entity.h"
|
||||
#include"entity/player.h"
|
||||
#include"entity/block.h"
|
||||
#include"entity/powerup.h"
|
||||
#include"camera.h"
|
||||
#include"menu.h"
|
||||
#include"maps.h"
|
||||
|
||||
class Core{
|
||||
public:
|
||||
|
@ -24,7 +20,6 @@ class Core{
|
|||
|
||||
const int SCREEN_WIDTH = 640;
|
||||
const int SCREEN_HEIGHT = 480;
|
||||
int sz;
|
||||
|
||||
SDL_Window* gWindow = NULL;
|
||||
SDL_Renderer* gRenderer = NULL;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
//Delta time class header<
|
||||
//Delta time class header
|
||||
|
||||
#ifndef __DT_H_INCLUDED__
|
||||
#define __DT_H_INCLUDED__
|
||||
|
|
|
@ -2,6 +2,26 @@
|
|||
|
||||
#include"player.h"
|
||||
|
||||
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;
|
||||
power = 0;
|
||||
|
||||
renderer = render;
|
||||
|
||||
ply.setRenderer(renderer);
|
||||
loadMedia();
|
||||
|
||||
ground = false;
|
||||
};
|
||||
|
||||
int Player::print(int cameraX){
|
||||
oldPosX = posX;
|
||||
oldPosY = posY;
|
||||
|
@ -30,26 +50,6 @@ int Player::print(int cameraX){
|
|||
return 0;
|
||||
};
|
||||
|
||||
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);
|
||||
loadMedia();
|
||||
//first = true;
|
||||
|
||||
ground = false;
|
||||
};
|
||||
|
||||
void Player::loadMedia(){
|
||||
ply.loadTexture("textures/player.png");
|
||||
|
||||
|
|
|
@ -21,25 +21,24 @@ class Player: public Entity{
|
|||
void loadMedia();
|
||||
void move();
|
||||
int intVelX();
|
||||
|
||||
SDL_Rect plyFrame[3];
|
||||
SDL_Rect plyRun;
|
||||
SDL_Renderer* renderer;
|
||||
DeltaTime dTime;
|
||||
Texture ply;
|
||||
|
||||
bool ground;
|
||||
bool topCollision;
|
||||
bool ground, topCollision;
|
||||
bool isRunning = false;
|
||||
bool ifRunning = false;
|
||||
|
||||
//int posX, posY;
|
||||
float velocityX = 0;
|
||||
float velocityY = 0;
|
||||
|
||||
//int posX, posY;
|
||||
int oldPosX, oldPosY;
|
||||
int power = 0;
|
||||
int power;
|
||||
int levelHeight;
|
||||
|
||||
Texture ply;
|
||||
SDL_Rect plyFrame[3];
|
||||
SDL_Rect plyRun;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#ifndef RESOURCE_RC_INCLUDED
|
||||
#define RESOURCE_RC_INCLUDED
|
||||
|
||||
1 ICON "icon.ico"
|
||||
1 ICON "../assets/icon.ico"
|
||||
|
||||
#endif // RESOURCE_RC_INCLUDED
|
||||
|
|
|
@ -0,0 +1,118 @@
|
|||
#include "maps.h"
|
||||
|
||||
Maps::Maps(SDL_Renderer* render){
|
||||
//Set renderer
|
||||
renderer = render;
|
||||
|
||||
//set the window's (renderer) dimensions
|
||||
SDL_GetRendererOutputSize(renderer,&screenWidth,&screenHeight);
|
||||
|
||||
//Set unit size
|
||||
sz = screenWidth/16;
|
||||
|
||||
//Set keyboard input
|
||||
currentKeyStates = SDL_GetKeyboardState(NULL);
|
||||
};
|
||||
|
||||
int Maps::map1(){
|
||||
//Set the map dimensions
|
||||
int mapWidth = 36*sz;
|
||||
int mapHeight = screenHeight;
|
||||
|
||||
//Initialize the player, set its position, pass the map's size and set the renderer
|
||||
Player player(40,8*sz,mapHeight,renderer);
|
||||
|
||||
//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(renderer,&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,renderer,&player);
|
||||
|
||||
bool quit = false;
|
||||
SDL_Event e;
|
||||
|
||||
//Game loop
|
||||
while(quit == false){
|
||||
while(SDL_PollEvent(&e)!=0){
|
||||
if(e.type == SDL_QUIT){
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
if(currentKeyStates[SDL_SCANCODE_ESCAPE]) return 0;
|
||||
|
||||
//Clear the render and set the background color
|
||||
SDL_SetRenderDrawColor(renderer,0,0,100,0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
//Update the camera position
|
||||
camera.update(player.getRectangle().x,player.getRectangle().y);
|
||||
|
||||
//Print player
|
||||
if(player.print(camera.getPosX()) == 1) map1();
|
||||
|
||||
//Print the blocks the corresponding dimensions and positions and check collisions
|
||||
for(int i = 0; i < 5; i++) ground.printAndCheck(blockRect[i],camera.getPosX());
|
||||
|
||||
//Print the poweup and check collisions
|
||||
powerup.printAndCheck(camera.getPosX());
|
||||
|
||||
//Render
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
|
||||
int Maps::map2(){
|
||||
int mapWidth = screenWidth;
|
||||
int mapHeight = screenHeight;
|
||||
|
||||
Player player(40,8*sz,mapHeight,renderer);
|
||||
Camera camera(mapWidth,mapHeight,screenWidth,screenHeight);
|
||||
Block ground(renderer,&player);
|
||||
Powerup powerup(13*sz,8*sz,renderer,&player);
|
||||
|
||||
int blockRect[4];
|
||||
setRectSize(blockRect, 0 ,11*sz,16*sz, sz);
|
||||
|
||||
bool quit = false;
|
||||
SDL_Event e;
|
||||
|
||||
while(quit == false){
|
||||
while(SDL_PollEvent(&e)!=0){
|
||||
if(e.type == SDL_QUIT){
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
if(currentKeyStates[SDL_SCANCODE_ESCAPE]) return 0;
|
||||
|
||||
SDL_SetRenderDrawColor(renderer,0,0,100,0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
camera.update(player.getRectangle().x,player.getRectangle().y);
|
||||
|
||||
if(player.print(camera.getPosX()) == 1) map2();
|
||||
ground.printAndCheck(blockRect,camera.getPosX());
|
||||
powerup.printAndCheck(camera.getPosX());
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
|
||||
void Maps::setRectSize(int rect[],int x, int y, int w, int h){
|
||||
rect[0] = x;
|
||||
rect[1] = y;
|
||||
rect[2] = w;
|
||||
rect[3] = h;
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef MAPS_H
|
||||
#define MAPS_H
|
||||
|
||||
#include<iostream>
|
||||
#include<SDL.h>
|
||||
#include"entity/player.h"
|
||||
#include"entity/block.h"
|
||||
#include"entity/powerup.h"
|
||||
#include"camera.h"
|
||||
|
||||
class Maps{
|
||||
public:
|
||||
Maps(SDL_Renderer* render);
|
||||
int map1();
|
||||
int map2();
|
||||
private:
|
||||
void setRectSize(int rect[], int x, int y,int w,int h);
|
||||
|
||||
SDL_Renderer* renderer;
|
||||
|
||||
int sz;
|
||||
int screenWidth, screenHeight;
|
||||
const Uint8* currentKeyStates;
|
||||
};
|
||||
|
||||
#endif // MAPS_H
|
|
@ -8,7 +8,7 @@ int menu(SDL_Renderer* renderer){
|
|||
SDL_Event e;
|
||||
|
||||
Texture txLogo;
|
||||
txLogo.setRenderer(&renderer);
|
||||
txLogo.setRenderer(renderer);
|
||||
txLogo.loadTexture("textures/title.png");
|
||||
|
||||
int logoVmargin = 40;
|
||||
|
@ -68,6 +68,7 @@ int menu(SDL_Renderer* renderer){
|
|||
}
|
||||
if(currentKeyStates[SDL_SCANCODE_SPACE]){
|
||||
if(select == 0) return 1;
|
||||
else if(select == 1) return 2;
|
||||
else if(select == 2) break;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,17 +5,21 @@ Texture::Texture(){
|
|||
szW = 0;
|
||||
szH = 0;
|
||||
|
||||
//Set the SDL_Texture to null
|
||||
texture = NULL;
|
||||
|
||||
//Initialize the SDL_Image library
|
||||
IMG_Init(IMG_INIT_PNG);
|
||||
//Set renderer
|
||||
};
|
||||
|
||||
void Texture::setRenderer(SDL_Renderer** render){
|
||||
void Texture::setRenderer(SDL_Renderer* render){
|
||||
//Set renderer
|
||||
renderer = render;
|
||||
};
|
||||
|
||||
Texture::~Texture(){
|
||||
//Deallocate
|
||||
//free();
|
||||
//Deallocate the texture when the class closes
|
||||
free();
|
||||
};
|
||||
|
||||
void Texture::loadTexture(std::string path){
|
||||
|
@ -31,11 +35,10 @@ void Texture::loadTexture(std::string path){
|
|||
else{
|
||||
//Create texture from surface pixels
|
||||
texture = SDL_CreateTextureFromSurface
|
||||
(*renderer,loadedSurface);
|
||||
(renderer,loadedSurface);
|
||||
|
||||
if(texture == NULL){
|
||||
std::cout << "Couldn't to create texture from "
|
||||
<< path.c_str() << std::endl;
|
||||
std::cout << "Couldn't create texture from " << path.c_str() << std::endl;
|
||||
}
|
||||
else{
|
||||
//Get image dimensions
|
||||
|
@ -58,23 +61,15 @@ void Texture::free(){
|
|||
};
|
||||
|
||||
void Texture::render(SDL_Rect* quad,SDL_Rect* frame){
|
||||
//void Texture::render(SDL_Rect* quad){
|
||||
//Set clip rendering dimensions
|
||||
/*if(frame != NULL){
|
||||
quad.w = frame->w;
|
||||
quad.h = frame->h;
|
||||
}*/
|
||||
|
||||
//Render to screen
|
||||
SDL_RenderCopy(*renderer,texture,frame,quad);
|
||||
//SDL_RenderFillRect(*renderer,quad);
|
||||
//std::cout << "ye" << std::endl;
|
||||
//Render the texture
|
||||
SDL_RenderCopy(renderer,texture,frame,quad);
|
||||
};
|
||||
|
||||
void Texture::render(SDL_Rect* quad){
|
||||
//Make a frame of the size of the quad
|
||||
SDL_Rect frame = {0,0,quad->w,quad->h};
|
||||
|
||||
SDL_RenderCopy(*renderer,texture,&frame,quad);
|
||||
//Render the texture
|
||||
SDL_RenderCopy(renderer,texture,&frame,quad);
|
||||
};
|
||||
|
||||
int Texture::getWidth(){
|
||||
|
|
|
@ -16,14 +16,14 @@ class Texture{
|
|||
void free();
|
||||
void render(SDL_Rect* quad,SDL_Rect* frame);
|
||||
void render(SDL_Rect* quad);
|
||||
//void render(SDL_Rect* quad);
|
||||
int getWidth();
|
||||
int getHeight();
|
||||
void setRenderer (SDL_Renderer** render);
|
||||
void setRenderer (SDL_Renderer* render);
|
||||
private:
|
||||
SDL_Texture* texture = NULL;
|
||||
SDL_Texture* texture;
|
||||
SDL_Renderer* renderer;
|
||||
|
||||
int szW, szH;
|
||||
SDL_Renderer** renderer;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue