From c516a1313d368621392eef3c3b4a88087a763338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=B2sweg?= Date: Sat, 29 Apr 2017 20:43:17 +0200 Subject: [PATCH] test-1 --- block.cpp | 15 ++++ block.h | 17 +++++ collisions.cpp | 23 ++++++ collisions.h | 13 ++++ core.cpp | 17 +++++ core.h | 13 ++++ dt.cpp | 17 +++++ dt.h | 11 +++ entity.cpp | 8 +++ entity.h | 18 +++++ hello-world.cpp | 8 +++ main | Bin 0 -> 17724 bytes main.cpp | 122 ++++++++++++++++++++++++++++++++ player.cpp | 184 ++++++++++++++++++++++++++++++++++++++++++++++++ player.h | 39 ++++++++++ 15 files changed, 505 insertions(+) create mode 100644 block.cpp create mode 100644 block.h create mode 100644 collisions.cpp create mode 100644 collisions.h create mode 100644 core.cpp create mode 100644 core.h create mode 100644 dt.cpp create mode 100644 dt.h create mode 100644 entity.cpp create mode 100644 entity.h create mode 100644 hello-world.cpp create mode 100644 main create mode 100644 main.cpp create mode 100644 player.cpp create mode 100644 player.h 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 0000000000000000000000000000000000000000..f2406e4ba1144b2d658a198dbeebe780601f2d8b GIT binary patch literal 17724 zcmeHPeRx#WnLm?E=vdQa#I!|V>u7@wR>y#WhCMdJ8C{^6Y!?pb_eU_z*$K{v#bC@K$dmHB?K$wg;UXo-9ijM zZFjKV10jE&ko}U5OEC`mIB2YYD-HX+rTkYf6St$=7ZU~1q-VpX*9}cxAwz*>w_d-RZEa@v&HRD%}gF0QP zlr2G}??~@h7x2At`E!dNx}c4pJ#^^3op=7;N!n~wj-PV-+3OqL`E0{*q@HT+#gQjp zzi;P{Zhn2B^{#L4e4_82qST#h=f3sp-xbZ*UTgr{r1D7`WR#hJ_nQbH@l`qWq8xlY zhyQR6{plRKonNYY&_FY>@*0(eKzDjF-#6 zaXIMqeRc=!oGFULUcmI-ihriKR9rbp-)s?u{KJq38~BUG-M~Ar))kIl#BO74!@}zq zy2}~5$=cy>eN>UHiu&IK(r?8i^T#lq1QL9)ct{|(cHAMCLC!G zH2F4$1Bx~+1>NW?0&#C3x+xNE^R>4GY9fhts1)8})6iiG(Anxi%1sME=H|fmjS*ke-x!Cvl1W)UMQV8{99|u0i3@!bMoYKH z;{KM;ewI#OEcA7&8sn9rNKD`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210An-=2_)Sxcvh=$tZNMQ2ECF90$q-Dzoh%Blm z(U1tCcrha6h-EXq;KxvW`v*ub)+BYo!p%$@YeAOO=5T-+Rr>PPRVy`JTUE>KHaX?y zH4-s(tcS-0G>e0ihRq&HPnH)y&{37eWp zHcfill)SNQBHH8}HGSQZaKz_d8SsaE7>>%*l38FLkIc?OMizo_-6F(nEC7z2M6h+M zmedBqabHuYEg*Br=u^YOtZO$%k(!55ZtBp3TcIpFe=Wg4%NDIt%>%+tCX01MVp{{7 zEh%yn$!@wo7SV%a5KAy2{O1`wfvu2jg*aV%`K}1!zqxYs^TUE)UZ>my0mCS}rZ@KL`gSX>aOa zvPqYi4YfwHQ7}!0<%Dx3*Q9*F>~z*FWVcgO7%5_m$4D78{L&Ij$n}G*Gd3|`Ms3z6 zsq)X|nW~gUYLW?+5Vgd$l%AZ(x;Y!%GJW;WrKnC$>YQd^%*~uUFyW4EZ;Sgj0>+~X z2F)vkRv_9T-0hKgz+F{ce|_Ay*}yipC)^toAtaB7{K8Ev=!*q~+rPaX`V@>u6{Rx} zjbRI(gy^6}17Y&3i;i$yxVa338<+0Q5nLe06Ti|+?r22jb-DvVyyqDIcK{Ie2CIZg zWBD{H-^|G;bNoSY;o20c@bL_!8%D4nvzXU>wl($f zS-Ky5Gx@M)6zXI79;(U5cY=ozKT$l)Zix z*BmeT_>O%31NhE>ZpH@Rjro6NP>^jb)J`Q4)+|UJ^0}%yIzwI$+4B(6Ahp_Bm0xHmv%bIEJh +#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