commit c516a1313d368621392eef3c3b4a88087a763338 Author: Pòsweg Date: Sat Apr 29 20:43:17 2017 +0200 test-1 diff --git a/block.cpp b/block.cpp new file mode 100644 index 0000000..573105a --- /dev/null +++ b/block.cpp @@ -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< +#include + +class Entity; + +class Block: public Entity{ + public: + void print(int x,int y,int w,int h,SDL_Renderer* renderer); +}; + +#endif diff --git a/collisions.cpp b/collisions.cpp new file mode 100644 index 0000000..fd53d9c --- /dev/null +++ b/collisions.cpp @@ -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; +}; diff --git a/collisions.h b/collisions.h new file mode 100644 index 0000000..f5ea866 --- /dev/null +++ b/collisions.h @@ -0,0 +1,13 @@ +//Collision checker class + +#ifndef __COLLISIONS_H_INCLUDED__ +#define __COLLISIONS_H_INCLUDED__ + +#include + +class checkCollisions{ + public: + char check(SDL_Rect rectA, SDL_Rect rectB); +} + +#endif diff --git a/core.cpp b/core.cpp new file mode 100644 index 0000000..bddb6f1 --- /dev/null +++ b/core.cpp @@ -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; +}; diff --git a/core.h b/core.h new file mode 100644 index 0000000..92e7456 --- /dev/null +++ b/core.h @@ -0,0 +1,13 @@ +//Delta time class header + +#include + +class CUU{ + public: + CUU(); + float getDt(); + int rendererWidth(); + int rendererHeight(); + private: + float now, last, dt; +}; diff --git a/dt.cpp b/dt.cpp new file mode 100644 index 0000000..bddb6f1 --- /dev/null +++ b/dt.cpp @@ -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; +}; diff --git a/dt.h b/dt.h new file mode 100644 index 0000000..f91419b --- /dev/null +++ b/dt.h @@ -0,0 +1,11 @@ +//Delta time class header + +#include + +class DeltaTime{ + public: + DeltaTime(); + float getDt(); + private: + float now, last, dt; +}; diff --git a/entity.cpp b/entity.cpp new file mode 100644 index 0000000..0d102af --- /dev/null +++ b/entity.cpp @@ -0,0 +1,8 @@ +//Virtual base class for entities + +#include"entity.h" + +SDL_Rect Entity::getRectangle(){ + //std::cout << rect.w << std::endl; + return rect; +}; diff --git a/entity.h b/entity.h new file mode 100644 index 0000000..feb6c57 --- /dev/null +++ b/entity.h @@ -0,0 +1,18 @@ +//Virutal base class for entities + +#ifndef __ENTITY_H_INCLUDED__ +#define __ENTITY_H_INCLUDED__ + +#include +#include + +class Entity{ + public: + SDL_Rect getRectangle(); + protected: + SDL_Rect rect; + int posX, posY, szW, szH; + +}; + +#endif diff --git a/hello-world.cpp b/hello-world.cpp new file mode 100644 index 0000000..9ddc49c --- /dev/null +++ b/hello-world.cpp @@ -0,0 +1,8 @@ +#include + +using namespace std; + +int main(){ + cout << "Hello world!" << endl; + return 0; +} diff --git a/main b/main new file mode 100644 index 0000000..f2406e4 Binary files /dev/null and b/main differ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..4b9d71e --- /dev/null +++ b/main.cpp @@ -0,0 +1,122 @@ +//main file + +#include +#include +#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<(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(x); + posY -= static_cast(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(movX); + posY = static_cast(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(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(velocityX); + } + //Right collision + else if(posX == b2X){ + collision = 5; + if(velocityX < 0) + velocityX -= static_cast(velocityX); + } + } +}; diff --git a/player.h b/player.h new file mode 100644 index 0000000..738a67a --- /dev/null +++ b/player.h @@ -0,0 +1,39 @@ +//Player class header + +#ifndef __PLAYER_H_INCLUDED__ +#define __PLAYER_H_INLCUDED__ + +#include +#include +#include"dt.h" +#include"entity.h" +#include + +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