Collision detection (needs some improvement)
This commit is contained in:
		
							parent
							
								
									4f9d042877
								
							
						
					
					
						commit
						556ccd976e
					
				
							
								
								
									
										9
									
								
								Makefile
								
								
								
								
							
							
						
						
									
										9
									
								
								Makefile
								
								
								
								
							|  | @ -1,6 +1,6 @@ | ||||||
| #-*- Makefile -*-
 | #-*- Makefile -*-
 | ||||||
| 
 | 
 | ||||||
| OBJS = main.o player.o dt.o block.o entity.o | OBJS = main.o player.o dt.o block.o entity.o camera.o | ||||||
| 
 | 
 | ||||||
| CC = g++ | CC = g++ | ||||||
| 
 | 
 | ||||||
|  | @ -17,14 +17,17 @@ all: $(OBJS) | ||||||
| main.o: main.cpp | main.o: main.cpp | ||||||
| 	$(CC) -c main.cpp $(LINKER_FLAGS) | 	$(CC) -c main.cpp $(LINKER_FLAGS) | ||||||
| 
 | 
 | ||||||
| player.o: player.cpp player.h entity.h | player.o: player.cpp player.h entity.o | ||||||
| 	$(CC) -c player.cpp -lm $(LINKER_FLAGS) | 	$(CC) -c player.cpp -lm $(LINKER_FLAGS) | ||||||
| 
 | 
 | ||||||
| dt.o: dt.cpp dt.h | dt.o: dt.cpp dt.h | ||||||
| 	$(CC) -c dt.cpp $(LINKER_FLAGS) | 	$(CC) -c dt.cpp $(LINKER_FLAGS) | ||||||
| 
 | 
 | ||||||
| block.o: block.cpp block.h entity.h | block.o: block.cpp block.h entity.o | ||||||
| 	$(CC) -c block.cpp $(LINKER_FLAGS) | 	$(CC) -c block.cpp $(LINKER_FLAGS) | ||||||
| 
 | 
 | ||||||
| entity.o: entity.cpp entity.h | entity.o: entity.cpp entity.h | ||||||
| 	$(CC) -c entity.cpp $(LINKER_FLAGS) | 	$(CC) -c entity.cpp $(LINKER_FLAGS) | ||||||
|  | 
 | ||||||
|  | camera.o: camera.cpp camera.h | ||||||
|  | 	$(CC) -c camera.cpp | ||||||
|  |  | ||||||
							
								
								
									
										24
									
								
								block.cpp
								
								
								
								
							
							
						
						
									
										24
									
								
								block.cpp
								
								
								
								
							|  | @ -1,13 +1,19 @@ | ||||||
| #include"block.h" | #include"block.h" | ||||||
| 
 | 
 | ||||||
| void Block::print(int x, int y, int w, int h,SDL_Renderer* renderer){ | Block::Block(int x, int y, int w, int h,SDL_Renderer** render){ | ||||||
| 	posX = x; | 	//set the rectangle dimensions
 | ||||||
| 	posY = y; | 	rect = {x,y,w,h}; | ||||||
| 	szW = w; |  | ||||||
| 	szH = h; |  | ||||||
| 
 | 
 | ||||||
| 	rect = {posX, posY, szW, szH}; | 	//Set the renderer pointer
 | ||||||
| 
 | 	renderer = render; | ||||||
| 	SDL_SetRenderDrawColor(renderer,0,0xFF,0,0xFF); |  | ||||||
| 	SDL_RenderFillRect(renderer,&rect);  |  | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | void Block::print(int cameraX){ | ||||||
|  | 	//New SDL_Rect to make the objects follow the camera
 | ||||||
|  | 	SDL_Rect cameraFix = rect; | ||||||
|  | 	cameraFix.x -= cameraX; | ||||||
|  | 
 | ||||||
|  | 	//Set render color and render the rectangle
 | ||||||
|  | 	SDL_SetRenderDrawColor(*renderer,0,0xFF,0,0xFF); | ||||||
|  | 	SDL_RenderFillRect(*renderer,&cameraFix);  | ||||||
|  | }; | ||||||
|  |  | ||||||
							
								
								
									
										5
									
								
								block.h
								
								
								
								
							
							
						
						
									
										5
									
								
								block.h
								
								
								
								
							|  | @ -11,7 +11,10 @@ class Entity; | ||||||
| 
 | 
 | ||||||
| class Block: public Entity{ | class Block: public Entity{ | ||||||
| 	public: | 	public: | ||||||
| 		void print(int x,int y,int w,int h,SDL_Renderer* renderer); | 		Block(int x,int y,int w,int h,SDL_Renderer** render); | ||||||
|  | 		void print(int cameraX); | ||||||
|  | 	private: | ||||||
|  | 		SDL_Renderer** renderer; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | @ -0,0 +1,19 @@ | ||||||
|  | //Camera class body
 | ||||||
|  | 
 | ||||||
|  | #include"camera.h" | ||||||
|  | 
 | ||||||
|  | void Camera::update(int playerX, int playerY){ | ||||||
|  | 	//Make the camera when the player hits the middle of the screem
 | ||||||
|  | 	if(true) posX = playerX - 640/2; | ||||||
|  | 	//else posX = 0;
 | ||||||
|  | 
 | ||||||
|  | 	//posY = playerY;
 | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | int Camera::getPosX(){ | ||||||
|  | 	return posX; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | int Camera::getPosY(){ | ||||||
|  | 	return posY; | ||||||
|  | }; | ||||||
|  | @ -0,0 +1,15 @@ | ||||||
|  | //Camera class header
 | ||||||
|  | 
 | ||||||
|  | #ifndef __CAMERA_H_INCLUDED__ | ||||||
|  | #define __CAMERA_H_INCLUDED__ | ||||||
|  | 
 | ||||||
|  | class Camera{ | ||||||
|  | 	public: | ||||||
|  | 		void update(int playerX, int playerY); | ||||||
|  | 		int getPosX(); | ||||||
|  | 		int getPosY(); | ||||||
|  | 	private: | ||||||
|  | 		int posX, posY; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | #endif | ||||||
							
								
								
									
										50
									
								
								main.cpp
								
								
								
								
							
							
						
						
									
										50
									
								
								main.cpp
								
								
								
								
							|  | @ -4,6 +4,7 @@ | ||||||
| #include<SDL2/SDL.h> | #include<SDL2/SDL.h> | ||||||
| #include"player.h" | #include"player.h" | ||||||
| #include"block.h" | #include"block.h" | ||||||
|  | #include"camera.h" | ||||||
| 
 | 
 | ||||||
| const int SCREEN_WIDTH = 640; | const int SCREEN_WIDTH = 640; | ||||||
| const int SCREEN_HEIGHT = 480; | const int SCREEN_HEIGHT = 480; | ||||||
|  | @ -17,10 +18,9 @@ const int sz = SCREEN_WIDTH/16; | ||||||
| void init(); | void init(); | ||||||
| bool loadMedia(); | bool loadMedia(); | ||||||
| void close(); | void close(); | ||||||
| void gameLoop(); |  | ||||||
| 
 | 
 | ||||||
| Player posweg; | Player posweg; | ||||||
| Block ground; | Camera camera; | ||||||
| 
 | 
 | ||||||
| bool loadMedia(){ | bool loadMedia(){ | ||||||
| 	bool success = true; | 	bool success = true; | ||||||
|  | @ -46,28 +46,6 @@ void close(){ | ||||||
| 	SDL_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[]){ | int main(int argc, char* args[]){ | ||||||
| 	init(); | 	init(); | ||||||
| 	loadMedia(); | 	loadMedia(); | ||||||
|  | @ -75,6 +53,11 @@ int main(int argc, char* args[]){ | ||||||
| 	bool quit = false; | 	bool quit = false; | ||||||
| 	SDL_Event e; | 	SDL_Event e; | ||||||
| 	 | 	 | ||||||
|  | 	Block wallA(8*sz,SCREEN_HEIGHT-sz*5,sz*2,sz*2,&gRenderer); | ||||||
|  | 	Block wallB(4*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer); | ||||||
|  | 	Block wallC(6*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer); | ||||||
|  | 	Block ground(0,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer); | ||||||
|  | 
 | ||||||
| 	posweg.init(100,100,sz,sz,&gRenderer); | 	posweg.init(100,100,sz,sz,&gRenderer); | ||||||
| 
 | 
 | ||||||
| 	while(!quit){ | 	while(!quit){ | ||||||
|  | @ -83,8 +66,25 @@ int main(int argc, char* args[]){ | ||||||
| 				quit = true; | 				quit = true; | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  | 		SDL_SetRenderDrawColor(gRenderer,0,0,0,0xFF); | ||||||
|  | 		SDL_RenderClear(gRenderer); | ||||||
| 		 | 		 | ||||||
| 		gameLoop(); | 		camera.update(posweg.getRectangle().x,posweg.getRectangle().y); | ||||||
|  | 	 | ||||||
|  | 		posweg.print(camera.getPosX()); | ||||||
|  | 		std::cout << posweg.getRectangle().x << std::endl; | ||||||
|  | 		 | ||||||
|  | 		wallA.print(camera.getPosX()); | ||||||
|  | 		wallB.print(camera.getPosX()); | ||||||
|  | 		wallC.print(camera.getPosX()); | ||||||
|  | 		ground.print(camera.getPosX()); | ||||||
|  | 	 | ||||||
|  | 		posweg.check(wallA.getRectangle());	 | ||||||
|  | 		posweg.check(wallB.getRectangle()); | ||||||
|  | 		posweg.check(wallC.getRectangle()); | ||||||
|  | 		posweg.check(ground.getRectangle());	 | ||||||
|  | 	 | ||||||
|  | 		SDL_RenderPresent(gRenderer); | ||||||
| 	} | 	} | ||||||
| 	close(); | 	close(); | ||||||
| 	return 0; | 	return 0; | ||||||
|  |  | ||||||
|  | @ -2,11 +2,14 @@ | ||||||
| 
 | 
 | ||||||
| #include"player.h" | #include"player.h" | ||||||
| 
 | 
 | ||||||
| void Player::print(){	 | void Player::print(int cameraX){	 | ||||||
| 	rect = {posX,posY,szW,szH}; | 	rect = {posX,posY,szW,szH}; | ||||||
| 
 | 
 | ||||||
|  | 	SDL_Rect cameraFix = rect; | ||||||
|  | 	cameraFix.x -= cameraX; | ||||||
|  | 
 | ||||||
| 	SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF); | 	SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF); | ||||||
| 	SDL_RenderFillRect(*renderer, &rect); | 	SDL_RenderFillRect(*renderer, &cameraFix); | ||||||
| 
 | 
 | ||||||
| 	oldPosX = posX; | 	oldPosX = posX; | ||||||
| 	oldPosY = posY;		 | 	oldPosY = posY;		 | ||||||
|  |  | ||||||
							
								
								
									
										2
									
								
								player.h
								
								
								
								
							
							
						
						
									
										2
									
								
								player.h
								
								
								
								
							|  | @ -13,7 +13,7 @@ class Entity; | ||||||
| 
 | 
 | ||||||
| class Player: public Entity{ | class Player: public Entity{ | ||||||
| 	public: | 	public: | ||||||
| 		void print(); | 		void print(int cameraX); | ||||||
| 		int check(SDL_Rect rectB); | 		int check(SDL_Rect rectB); | ||||||
| 		void init(int x, int y,int w, int h,SDL_Renderer** render); | 		void init(int x, int y,int w, int h,SDL_Renderer** render); | ||||||
| 		//SDL_Rect getRectangle();
 | 		//SDL_Rect getRectangle();
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue