First render of the physics
This commit is contained in:
		
							
								
								
									
										10
									
								
								Entity.cpp
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								Entity.cpp
									
									
									
									
									
								
							| @ -10,6 +10,12 @@ Entity::Entity(Vec2 p, Vec2 s, Vec2 a): | ||||
| void Entity::Update(){ | ||||
| 	pos = pos + spe + acc/2; | ||||
| 	spe = spe + acc; | ||||
| 	if(pos.y >= 1000){ | ||||
| 		spe.y = - spe.y + acc.y; | ||||
| 	} | ||||
| 	if(pos.x >= 1920){ | ||||
| 		pos.x = 0; | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void Entity::Debug() const{ | ||||
| @ -17,3 +23,7 @@ void Entity::Debug() const{ | ||||
| 		<< "spe= (" << spe.x << ", " << spe.y << ")"  | ||||
| 		<< "acc= (" << acc.x << ", " << acc.y << ")" << std::endl; | ||||
| } | ||||
|  | ||||
| Vec2 Entity::GetPos(){ | ||||
| 	return pos; | ||||
| } | ||||
|  | ||||
							
								
								
									
										2
									
								
								Entity.h
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Entity.h
									
									
									
									
									
								
							| @ -15,6 +15,8 @@ public: | ||||
| 	void Debug() const; | ||||
| 	 | ||||
| 	void Update(); | ||||
|  | ||||
| 	Vec2 GetPos(); | ||||
| }; | ||||
|  | ||||
| #endif /* ENTITY_H */ | ||||
|  | ||||
							
								
								
									
										4
									
								
								Vector.h
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Vector.h
									
									
									
									
									
								
							| @ -1,6 +1,7 @@ | ||||
| #ifndef VECTOR_H | ||||
| #define VECTOR_H | ||||
|  | ||||
|  | ||||
| struct Vec2  | ||||
| { | ||||
| 	int x, y = 0; | ||||
| @ -11,6 +12,9 @@ struct Vec2 | ||||
| 	Vec2 operator/ (const int left); | ||||
|  | ||||
| 	int norm(); | ||||
| 	 | ||||
| }; | ||||
|  | ||||
| const static Vec2 gravity = {0,10}; | ||||
|  | ||||
| #endif /* VECTOR_H */ | ||||
|  | ||||
							
								
								
									
										41
									
								
								Window.cpp
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								Window.cpp
									
									
									
									
									
								
							| @ -1,29 +1,38 @@ | ||||
| #include "Window.h" | ||||
| #include "ErrorHandler.h" | ||||
| #include <SDL2/SDL.h> | ||||
| #include <SDL2/SDL_rect.h> | ||||
| #include <SDL2/SDL_render.h> | ||||
| #include <SDL2/SDL_video.h> | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
|  | ||||
| void Window::Draw(std::function<bool(Window*)> f){ | ||||
| void Window::Draw(std::function<bool(Window*)> setup, std::function<bool(Window*)> draw){ | ||||
| 	setup(this); | ||||
| 	while(Running){ | ||||
|  | ||||
| 		Running = f(this); | ||||
| 		SDL_SetRenderDrawColor(ren, 0x00,0x00,0x00,0xff); | ||||
| 		SDL_RenderClear(ren); | ||||
| 		Events(draw); | ||||
| 		draw(this); | ||||
| 		SDL_RenderPresent(ren); | ||||
|  | ||||
| 		Events(); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void Window::Events(){ | ||||
| 	while(SDL_PollEvent(e)){ | ||||
| 		if(e->type == SDL_QUIT){ | ||||
| void Window::Events(std::function<bool(Window*)> draw){ | ||||
| 	while(SDL_PollEvent(&e)){ | ||||
| 		if(e.type == SDL_QUIT){ | ||||
| 			Running = false; | ||||
| 		} | ||||
| 		if(e->type == SDL_KEYDOWN){ | ||||
| 			switch(e->key.keysym.sym){ | ||||
| 		if(e.type == SDL_KEYDOWN){ | ||||
| 			switch(e.key.keysym.sym){ | ||||
| 				case SDLK_a: | ||||
| 					Running = false; | ||||
| 					break; | ||||
| 				case SDLK_s: | ||||
| 					step = true; | ||||
| 					break; | ||||
|  | ||||
| 			} | ||||
| 		} | ||||
| @ -39,7 +48,7 @@ Window::Window(std::string title, int width, int height) | ||||
| 		ErrorHandler::send(ErrorType::SDL_ERROR, "SDL_Init"); | ||||
| 	} | ||||
|  | ||||
| 	win = SDL_CreateWindow("test", 0,0, width, height, SDL_WINDOW_SHOWN); | ||||
| 	win = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN_DESKTOP); | ||||
| 	if(win == NULL){ | ||||
| 		ErrorHandler::send(ErrorType::SDL_ERROR, "SDL_CreateWindow"); | ||||
| 	} | ||||
| @ -59,3 +68,17 @@ Window::~Window(){ | ||||
| SDL_Renderer* Window::GetRenderer(){ | ||||
| 	return ren;	 | ||||
| } | ||||
|  | ||||
| void Window::DrawCircle(SDL_Renderer* ren, int x, int y, int r, bool filled){ | ||||
| 	std::vector<SDL_Point> circle; | ||||
| 	for(int _x = x - r; _x <= x + r; _x++){ | ||||
| 		for(int _y = y - r; _y <= y + r; _y++){ | ||||
| 			int dx = _x - x; | ||||
| 			int dy = _y - y; | ||||
| 			if (dx*dx + dy*dy <= r*r){ | ||||
| 				circle.push_back({_x, _y}); | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	SDL_RenderDrawPoints(ren, circle.data(), circle.size()); | ||||
| } | ||||
|  | ||||
							
								
								
									
										11
									
								
								Window.h
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								Window.h
									
									
									
									
									
								
							| @ -10,18 +10,21 @@ class Window | ||||
| private: | ||||
| 	SDL_Window* win; | ||||
| 	SDL_Renderer* ren; | ||||
| 	SDL_Event* e; | ||||
| 	SDL_Event e; | ||||
|  | ||||
| 	bool Running; | ||||
|  | ||||
| public: | ||||
|  | ||||
| 	bool step = false; | ||||
|  | ||||
| 	Window(std::string title, int width, int height); | ||||
| 	virtual ~Window(); | ||||
|  | ||||
| 	void Setup(); | ||||
| 	void Draw(std::function<bool(Window*)> f); | ||||
| 	void Events(); | ||||
| 	void Draw(std::function<bool(Window*)> setup, std::function<bool(Window*)> draw); | ||||
| 	void Events(std::function<bool(Window*)> draw); | ||||
|  | ||||
| 	void DrawCircle(SDL_Renderer*, int, int, int, bool); | ||||
|  | ||||
| 	SDL_Renderer* GetRenderer(); | ||||
|  | ||||
|  | ||||
							
								
								
									
										26
									
								
								main.cpp
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								main.cpp
									
									
									
									
									
								
							| @ -1,26 +1,38 @@ | ||||
| #include "Window.h" | ||||
| #include "Entity.h" | ||||
| #include <SDL2/SDL_render.h> | ||||
| #include <functional> | ||||
| #include <SDL2/SDL.h> | ||||
|  | ||||
| Entity* ent; | ||||
|  | ||||
| bool my_setup(Window* win){ | ||||
| 	ent = new Entity({10,10}, {5,0}, {0,2}); | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| bool my_Draw(Window* win){ | ||||
|  | ||||
| 	SDL_Renderer* ren = win->GetRenderer(); | ||||
|  | ||||
| 	/* if(win->step){ */ | ||||
| 		ent->Update(); | ||||
| 		win->step = false; | ||||
| 	/* } */ | ||||
| 	Vec2 ent_pos = ent->GetPos(); | ||||
|  | ||||
| 	SDL_SetRenderDrawColor(ren, 0xff, 0x00, 0x00, 0x00); | ||||
| 	win->DrawCircle(ren, ent_pos.x, ent_pos.y, 5, true); | ||||
|  | ||||
| 	return true;  | ||||
| } | ||||
|  | ||||
| int main(int argc, char *argv[]) | ||||
| { | ||||
| 	/* Window win("test", 100, 100); */ | ||||
| 	/* win.Draw(my_Draw); */ | ||||
| 	Window win("test", 100, 100); | ||||
| 	win.Draw(my_setup, my_Draw); | ||||
|  | ||||
| 	Entity t({0, 0}, {1, 1}, {0,0}); | ||||
| 	t.Debug(); | ||||
| 	t.Update(); | ||||
| 	t.Debug(); | ||||
| 	delete ent; | ||||
|  | ||||
| 	 | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user