This commit is contained in:
Pòsweg 2017-04-29 20:43:17 +02:00
commit c516a1313d
15 changed files with 505 additions and 0 deletions

15
block.cpp Normal file
View File

@ -0,0 +1,15 @@
#include"block.h"
void Block::print(int x, int y, int w, int h,SDL_Renderer* renderer){
posX = x;
posY = y;
szW = w;
szH = h;
//std::cout<<x<<" "<<y<<" "<<w<<" "<<h<<" / ";
rect = {posX, posY, szW, szH};
SDL_SetRenderDrawColor(renderer,0,0xFF,0,0xFF);
SDL_RenderFillRect(renderer,&rect);
}

17
block.h Normal file
View File

@ -0,0 +1,17 @@
//Block class header
#ifndef __BLOCK_H_INLCUDED__
#define __BLOCK_H_INCLUDED__
#include"entity.h"
#include<SDL2/SDL.h>
#include<iostream>
class Entity;
class Block: public Entity{
public:
void print(int x,int y,int w,int h,SDL_Renderer* renderer);
};
#endif

23
collisions.cpp Normal file
View File

@ -0,0 +1,23 @@
//Collision checker class body
#include"collisions.h"
char check(SDL_Rect rectA, SDL_Rect rect2){
int collision;
int aX = rectA.x;
int aY = rectA.y;
int a2X = rectA.x + rectA.w;
int a2Y = rectA.y + rectA.h;
int bX = rectB.x;
int bY = rectB.y;
int b2X = rectB.x + rectB.w;
int b2Y = rectB.y + rectB.h;
if(aX >= bX and aX <= b2X or a2X >= bX and a2X <= b2X) collision += 1;
if(aY >= bY and aY <= b2Y or a2Y >= bY and a2Y <= b2Y) collision += 2;
return collision;
};

13
collisions.h Normal file
View File

@ -0,0 +1,13 @@
//Collision checker class
#ifndef __COLLISIONS_H_INCLUDED__
#define __COLLISIONS_H_INCLUDED__
#include<SDL2/SDL.h>
class checkCollisions{
public:
char check(SDL_Rect rectA, SDL_Rect rectB);
}
#endif

17
core.cpp Normal file
View File

@ -0,0 +1,17 @@
//Delta time class body
#include"dt.h"
DeltaTime::DeltaTime(){
now = SDL_GetPerformanceCounter();
};
float DeltaTime::getDt(){
last = now;
now = SDL_GetPerformanceCounter();
dt = (now - last)/SDL_GetPerformanceFrequency();
if(dt > 0.15f) dt = 0.15f;
return dt;
};

13
core.h Normal file
View File

@ -0,0 +1,13 @@
//Delta time class header
#include<SDL2/SDL.h>
class CUU{
public:
CUU();
float getDt();
int rendererWidth();
int rendererHeight();
private:
float now, last, dt;
};

17
dt.cpp Normal file
View File

@ -0,0 +1,17 @@
//Delta time class body
#include"dt.h"
DeltaTime::DeltaTime(){
now = SDL_GetPerformanceCounter();
};
float DeltaTime::getDt(){
last = now;
now = SDL_GetPerformanceCounter();
dt = (now - last)/SDL_GetPerformanceFrequency();
if(dt > 0.15f) dt = 0.15f;
return dt;
};

11
dt.h Normal file
View File

@ -0,0 +1,11 @@
//Delta time class header
#include<SDL2/SDL.h>
class DeltaTime{
public:
DeltaTime();
float getDt();
private:
float now, last, dt;
};

8
entity.cpp Normal file
View File

@ -0,0 +1,8 @@
//Virtual base class for entities
#include"entity.h"
SDL_Rect Entity::getRectangle(){
//std::cout << rect.w << std::endl;
return rect;
};

18
entity.h Normal file
View File

@ -0,0 +1,18 @@
//Virutal base class for entities
#ifndef __ENTITY_H_INCLUDED__
#define __ENTITY_H_INCLUDED__
#include<SDL2/SDL.h>
#include<iostream>
class Entity{
public:
SDL_Rect getRectangle();
protected:
SDL_Rect rect;
int posX, posY, szW, szH;
};
#endif

8
hello-world.cpp Normal file
View File

@ -0,0 +1,8 @@
#include<iostream>
using namespace std;
int main(){
cout << "Hello world!" << endl;
return 0;
}

BIN
main Normal file

Binary file not shown.

122
main.cpp Normal file
View File

@ -0,0 +1,122 @@
//main file
#include<iostream>
#include<SDL2/SDL.h>
#include"player.h"
#include"block.h"
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
//12 sz of height and 16 of width
const int sz = SCREEN_WIDTH/16;
void init();
bool loadMedia();
void close();
void gameLoop();
int check(SDL_Rect rectA, SDL_Rect rectB);
Player posweg;
Block ground;
bool loadMedia(){
bool success = true;
return success;
}
void init(){
SDL_Init(SDL_INIT_VIDEO);
//IMG_Init(IMG_INIT_JPG);
gWindow = SDL_CreateWindow("Plataform 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);
}
void close(){
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
gRenderer = NULL;
//IMG_Quit();
SDL_Quit();
}
void gameLoop(){
SDL_SetRenderDrawColor(gRenderer,0,0,0,0xFF);
SDL_RenderClear(gRenderer);
Block wallA;
Block wallB;
Block wallC;
wallA.print(8*sz,SCREEN_HEIGHT-sz*5,sz*2,sz*2,gRenderer);
wallB.print(4*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,gRenderer);
wallC.print(6*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,gRenderer);
ground.print(0,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,gRenderer);
posweg.print();
posweg.check(wallA.getRectangle());
posweg.check(wallB.getRectangle());
posweg.check(wallC.getRectangle());
posweg.check(ground.getRectangle());
SDL_RenderPresent(gRenderer);
}
int main(int argc, char* args[]){
init();
loadMedia();
bool quit = false;
SDL_Event e;
posweg.init(100,100,sz,sz,&gRenderer);
while(!quit){
while(SDL_PollEvent(&e)!=0){
if(e.type == SDL_QUIT){
quit = true;
}
}
gameLoop();
}
close();
return 0;
}
int check(SDL_Rect rectA, SDL_Rect rectB){
int collision = 0;
int aX = rectA.x;
int aY = rectA.y;
int a2X = rectA.x + rectA.w;
int a2Y = rectA.y + rectA.h;
int bX = rectB.x;
int bY = rectB.y;
int b2X = rectB.x + rectB.w;
int b2Y = rectB.y + rectB.h;
if(aX > bX and aX < b2X){
if(aY > bY and aY < b2Y) collision += 1;
if(a2Y > bY and a2Y < b2Y) collision += 2;
}
if(a2X > bX and a2X < b2X){
if(aY > bY and aY < b2Y) collision += 4;
if(a2Y > bY and a2Y < b2Y) collision += 8;
}
//std::cout<<aX<<" "<<a2X<<" "<<aY<<" "<<a2Y<<" / ";
//std::cout<<bX<<" "<<b2X<<" "<<bY<<" "<<b2Y;
return collision;
};

184
player.cpp Normal file
View File

@ -0,0 +1,184 @@
//Player class cpp
#include"player.h"
void Player::print(){
rect = {posX,posY,szW,szH};
SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF);
SDL_RenderFillRect(*renderer, &rect);
oldPosX = posX;
oldPosY = posY;
move();
ground = false;
topCollision = false;
};
void Player::init(int x,int y, int w, int h, SDL_Renderer** render){
posX = x;
posY = y;
szW = w;
szH = h;
renderer = render;
oldPosX = posX;
oldPosY = posY; };
int Player::intVelX(){
return static_cast<int>(velocityX);
};
void Player::move(){
//float dt = dTime.getDt();
float dt = 0.016;
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
//Intializing variables
int direction = 0; //1 = right, 0 = idle/both, -1 = left
int dur = 4; //Divide transition time
int speed = 200; //Horizontal movement speed
float run = 1.5; //Running speed multiplication
//static bool isRunning = false;
int gravity = 800; //Gravity force
int jump = 500; //Jump force
if(currentKeyStates[SDL_SCANCODE_LEFT]) direction += -1;
if(currentKeyStates[SDL_SCANCODE_RIGHT]) direction += 1;
if(!currentKeyStates[SDL_SCANCODE_LSHIFT] and !isRunning) run = 1;
if(run!=1) dur /= 1;
if(direction == 1 and intVelX() < speed){
velocityX += speed * dt * dur;
if(intVelX() > speed) velocityX = speed;
}
if(direction == -1 and intVelX() > -speed){
velocityX -= speed * dt * dur;
if(intVelX() < -speed) velocityX = -speed;
}
if(direction == 0 and velocityX != 0)
if(intVelX() > 0){
velocityX -= speed * dt * dur;
if(intVelX() < 0) velocityX = 0;
}
else if(intVelX() < 0){
velocityX += speed * dt * dur;
if(intVelX() > 0) velocityX = 0;
}
else if(velocityX <= 1 or velocityX >= -1) velocityX = 0;
//Jump and gravity key logic
if(ground){
isRunning = false;
if(currentKeyStates[SDL_SCANCODE_SPACE] and !topCollision){
velocityY = jump;
if(run!=1){
isRunning = true;
}
}
}
else{
if(isRunning == false) run = 1;
}
velocityY -= gravity * dt;
float x = velocityX * dt * run;
float y = velocityY * dt;
//Convert and set new int position
posX += static_cast<int>(x);
posY -= static_cast<int>(y+0.5);
};
int Player::check(SDL_Rect rectA){
//Initialize and reset collision type
int collision = 0;
//Set B rectangle variables
int bX = rectA.x;
int bY = rectA.y;
int b2X = rectA.x + rectA.w;
int b2Y = rectA.y + rectA.h;
//Float-ize the position
float movX = posX, movY = posY;
//Clacule the opposite od the player direction angle
float angle = atan2(oldPosY - posY, oldPosX - posX);
//Move the player out of the rectangle
while(((movX >= bX and movX < b2X)
or (movX + szW > bX and movX + szW <= b2X))
and ((movY >= bY and movY < b2Y)
or (movY + szH > bY and movY + szH <= b2Y))){
movX += cos(angle) / 2;
movY += sin(angle) / 2;
collision = 1;
}
//Correct possible position issues
if(collision == 1){
//Vertical adjustement
if((movX > bX and movX < b2X)
or (movX + szW > bX and movX + szW < b2X)){
while(movY + szH < bY){
movY++;
velocityY = 0;
}
while(movY - 1 > b2Y){
movY--;
}
}
//Horizontal adjustement
if((movY > bY and movY < b2Y)
or (movY + szH > bY and movY + szH < b2Y)){
while(movX + szW < bX){
movX++;
}
while(posX > b2X){
movX--;
}
}
}
//Set and int-ize the position
posX = static_cast<int>(movX);
posY = static_cast<int>(movY);
//Check collsion type and reset velocities
//Vertical collisions
if((posX >= bX and posX < b2X)
or (posX + szW > bX and posX + szW <= b2X)){
//Top collision
if(posY + szH == bY){
ground = true;
collision = 2;
velocityY = 0;
}
//Bottom collision
else if(posY == b2Y){
collision = 3;
topCollision = true;
if(velocityY > 0)
velocityY -= static_cast<int>(velocityY);
}
}
//Horizontal collisions
if((posY >= bY and posY < b2Y)
or (posY + szH > bY and posY + szH <= b2Y)){
//Left collision
if(posX + szW == bX){
collision = 4;
if(velocityX > 0)
velocityX -= static_cast<int>(velocityX);
}
//Right collision
else if(posX == b2X){
collision = 5;
if(velocityX < 0)
velocityX -= static_cast<int>(velocityX);
}
}
};

39
player.h Normal file
View File

@ -0,0 +1,39 @@
//Player class header
#ifndef __PLAYER_H_INCLUDED__
#define __PLAYER_H_INLCUDED__
#include<SDL2/SDL.h>
#include<iostream>
#include"dt.h"
#include"entity.h"
#include<cmath>
class Entity;
class Player: public Entity{
public:
//Player(/*int* collision*/);
void print();
int check(SDL_Rect rectB);
void init(int x, int y,int w, int h,SDL_Renderer** render);
//SDL_Rect getRectangle();
private:
int* coll;
void move();
int intVelX();
SDL_Renderer** renderer;
DeltaTime dTime;
bool ground;
bool topCollision;
//int posX, posY;
//int cVelocity, oldVelX, oldVelY, newVelX, newVelY;
float velocityX = 0;
float velocityY = 0;
bool isRunning = false;
int oldPosX, oldPosY;
//int szW = 40;
//int szH = 40;
};
#endif