diff --git a/Entity.cpp b/Entity.cpp index f336f27..60edd84 100644 --- a/Entity.cpp +++ b/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; +} diff --git a/Entity.h b/Entity.h index 8c3179f..bc67513 100644 --- a/Entity.h +++ b/Entity.h @@ -15,6 +15,8 @@ public: void Debug() const; void Update(); + + Vec2 GetPos(); }; #endif /* ENTITY_H */ diff --git a/Vector.h b/Vector.h index 802262c..8de9b79 100644 --- a/Vector.h +++ b/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 */ diff --git a/Window.cpp b/Window.cpp index 0464fc6..29f7d86 100644 --- a/Window.cpp +++ b/Window.cpp @@ -1,29 +1,38 @@ #include "Window.h" #include "ErrorHandler.h" #include +#include +#include +#include #include +#include -void Window::Draw(std::function f){ +void Window::Draw(std::function setup, std::function 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 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 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()); +} diff --git a/Window.h b/Window.h index aa18d7e..b0b6937 100644 --- a/Window.h +++ b/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 f); - void Events(); + void Draw(std::function setup, std::function draw); + void Events(std::function draw); + + void DrawCircle(SDL_Renderer*, int, int, int, bool); SDL_Renderer* GetRenderer(); diff --git a/main.cpp b/main.cpp index 5512f31..a958614 100644 --- a/main.cpp +++ b/main.cpp @@ -1,26 +1,38 @@ #include "Window.h" #include "Entity.h" +#include #include #include +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; } diff --git a/main.h b/main.h new file mode 100644 index 0000000..e69de29 diff --git a/sim b/sim deleted file mode 100755 index a900278..0000000 Binary files a/sim and /dev/null differ