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