Merge remote-tracking branch 'origin/camera-follow'
This commit is contained in:
		
						commit
						a0ed188138
					
				
							
								
								
									
										11
									
								
								Makefile
								
								
								
								
							
							
						
						
									
										11
									
								
								Makefile
								
								
								
								
							|  | @ -1,12 +1,12 @@ | |||
| #-*- 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++ | ||||
| 
 | ||||
| COMPILER_FLAGS = -w | ||||
| 
 | ||||
| LINKER_FLAGS = -lSDL2 -std=gnu++11 | ||||
| LINKER_FLAGS = -lSDL2 -std=gnu++11 -lSDL2_image | ||||
| 
 | ||||
| OBJ_NAME = bin/main | ||||
| 
 | ||||
|  | @ -17,14 +17,17 @@ all: $(OBJS) | |||
| main.o: main.cpp | ||||
| 	$(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) | ||||
| 
 | ||||
| dt.o: dt.cpp dt.h | ||||
| 	$(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) | ||||
| 
 | ||||
| entity.o: entity.cpp entity.h | ||||
| 	$(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" | ||||
| 
 | ||||
| void Block::print(int x, int y, int w, int h,SDL_Renderer* renderer){ | ||||
| 	posX = x; | ||||
| 	posY = y; | ||||
| 	szW = w; | ||||
| 	szH = h; | ||||
| Block::Block(int x, int y, int w, int h,SDL_Renderer** render){ | ||||
| 	//set the rectangle dimensions
 | ||||
| 	rect = {x,y,w,h}; | ||||
| 
 | ||||
| 	rect = {posX, posY, szW, szH}; | ||||
| 
 | ||||
| 	SDL_SetRenderDrawColor(renderer,0,0xFF,0,0xFF); | ||||
| 	SDL_RenderFillRect(renderer,&rect);  | ||||
| 	//Set the renderer pointer
 | ||||
| 	renderer = render; | ||||
| } | ||||
| 
 | ||||
| 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{ | ||||
| 	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 | ||||
|  |  | |||
|  | @ -0,0 +1,25 @@ | |||
| //Camera class body
 | ||||
| 
 | ||||
| #include"camera.h" | ||||
| 
 | ||||
| Camera::Camera(int mapWidth,int screenWidth){ | ||||
| 	mW = mapWidth; | ||||
| 	scW = screenWidth; | ||||
| } | ||||
| 
 | ||||
| void Camera::update(int playerX, int playerY){ | ||||
| 	//Make the camera when the player hits the middle of the screem
 | ||||
| 	if(playerX >= scW/2) posX = playerX - scW/2; | ||||
| 	else posX = 0; | ||||
| 	if(playerX >= mW - scW/2) posX = mW - scW; | ||||
| 
 | ||||
| 	//posY = playerY;
 | ||||
| }; | ||||
| 
 | ||||
| int Camera::getPosX(){ | ||||
| 	return posX; | ||||
| }; | ||||
| 
 | ||||
| int Camera::getPosY(){ | ||||
| 	return posY; | ||||
| }; | ||||
|  | @ -0,0 +1,17 @@ | |||
| //Camera class header
 | ||||
| 
 | ||||
| #ifndef __CAMERA_H_INCLUDED__ | ||||
| #define __CAMERA_H_INCLUDED__ | ||||
| 
 | ||||
| class Camera{ | ||||
| 	public: | ||||
| 		Camera(int mapWidth,int screenWidth); | ||||
| 		void update(int playerX, int playerY); | ||||
| 		int getPosX(); | ||||
| 		int getPosY(); | ||||
| 	private: | ||||
| 		int posX, posY; | ||||
| 		int mW, scW; | ||||
| }; | ||||
| 
 | ||||
| #endif | ||||
							
								
								
									
										58
									
								
								main.cpp
								
								
								
								
							
							
						
						
									
										58
									
								
								main.cpp
								
								
								
								
							|  | @ -2,8 +2,10 @@ | |||
| 
 | ||||
| #include<iostream> | ||||
| #include<SDL2/SDL.h> | ||||
| //#include<SDL2/SDL_image.h>
 | ||||
| #include"player.h" | ||||
| #include"block.h" | ||||
| #include"camera.h" | ||||
| 
 | ||||
| const int SCREEN_WIDTH = 640; | ||||
| const int SCREEN_HEIGHT = 480; | ||||
|  | @ -17,10 +19,6 @@ const int sz = SCREEN_WIDTH/16; | |||
| void init(); | ||||
| bool loadMedia(); | ||||
| void close(); | ||||
| void gameLoop(); | ||||
| 
 | ||||
| Player posweg; | ||||
| Block ground; | ||||
| 
 | ||||
| bool loadMedia(){ | ||||
| 	bool success = true; | ||||
|  | @ -46,28 +44,6 @@ void close(){ | |||
| 	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(); | ||||
|  | @ -75,6 +51,15 @@ int main(int argc, char* args[]){ | |||
| 	bool quit = false; | ||||
| 	SDL_Event e; | ||||
| 
 | ||||
| 	Player posweg; | ||||
| 	Camera camera(36*sz,SCREEN_WIDTH); | ||||
| 	 | ||||
| 	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); | ||||
| 	Block ground2(SCREEN_WIDTH+sz*4,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer); | ||||
| 
 | ||||
| 	posweg.init(100,100,sz,sz,&gRenderer); | ||||
| 
 | ||||
| 	while(!quit){ | ||||
|  | @ -83,8 +68,27 @@ int main(int argc, char* args[]){ | |||
| 				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()); | ||||
| 		ground2.print(camera.getPosX()); | ||||
| 	 | ||||
| 		posweg.check(wallA.getRectangle());	 | ||||
| 		posweg.check(wallB.getRectangle()); | ||||
| 		posweg.check(wallC.getRectangle()); | ||||
| 		posweg.check(ground.getRectangle());	 | ||||
| 		posweg.check(ground2.getRectangle());	 | ||||
| 
 | ||||
| 		SDL_RenderPresent(gRenderer); | ||||
| 	} | ||||
| 	close(); | ||||
| 	return 0; | ||||
|  |  | |||
							
								
								
									
										10
									
								
								player.cpp
								
								
								
								
							
							
						
						
									
										10
									
								
								player.cpp
								
								
								
								
							|  | @ -2,11 +2,13 @@ | |||
| 
 | ||||
| #include"player.h" | ||||
| 
 | ||||
| void Player::print(){	 | ||||
| 	rect = {posX,posY,szW,szH}; | ||||
| void Player::print(int cameraX){	 | ||||
| 
 | ||||
| 	SDL_Rect cameraFix = rect; | ||||
| 	cameraFix.x -= cameraX; | ||||
| 
 | ||||
| 	SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF); | ||||
| 	SDL_RenderFillRect(*renderer, &rect); | ||||
| 	SDL_RenderFillRect(*renderer, &cameraFix); | ||||
| 
 | ||||
| 	oldPosX = posX; | ||||
| 	oldPosY = posY;		 | ||||
|  | @ -93,6 +95,8 @@ void Player::move(){ | |||
| 	//Convert and set new int position
 | ||||
| 	posX += static_cast<int>(x); | ||||
| 	posY -= static_cast<int>(y+0.5); | ||||
| 
 | ||||
| 	rect = {posX,posY,szW,szH}; | ||||
| }; | ||||
| 
 | ||||
| int Player::check(SDL_Rect rectA){ | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue