commit fdd1e361c0844ce38deca008b02924b00fff66a8 Author: Anthony Debucquoy Date: Sun Oct 30 23:44:30 2022 +0100 First Commit diff --git a/Entity.cpp b/Entity.cpp new file mode 100644 index 0000000..f336f27 --- /dev/null +++ b/Entity.cpp @@ -0,0 +1,19 @@ +#include "Entity.h" +#include + +Entity::Entity(Vec2 p, Vec2 s, Vec2 a): + pos(p), spe(s), acc(a) +{ + +} + +void Entity::Update(){ + pos = pos + spe + acc/2; + spe = spe + acc; +} + +void Entity::Debug() const{ + std::cout << "pos= (" << pos.x << ", " << pos.y << ")" + << "spe= (" << spe.x << ", " << spe.y << ")" + << "acc= (" << acc.x << ", " << acc.y << ")" << std::endl; +} diff --git a/Entity.h b/Entity.h new file mode 100644 index 0000000..8c3179f --- /dev/null +++ b/Entity.h @@ -0,0 +1,20 @@ +#ifndef ENTITY_H +#define ENTITY_H + +#include "Vector.h" + +class Entity +{ +private: + Vec2 pos, spe, acc; + +public: + Entity(); + Entity(Vec2, Vec2, Vec2); + + void Debug() const; + + void Update(); +}; + +#endif /* ENTITY_H */ diff --git a/ErrorHandler.cpp b/ErrorHandler.cpp new file mode 100644 index 0000000..434f339 --- /dev/null +++ b/ErrorHandler.cpp @@ -0,0 +1,15 @@ +#include "ErrorHandler.h" + +bool ErrorHandler::send(ErrorType e, std::string message){ + std::cout << "ERROR::(" << message << ")"; + switch (e) { + case ErrorType::SDL_ERROR: + std::cout << SDL_GetError() << std::endl; + return false; + break; + default: + std::cout << "UNKNOWN ERROR..." << std::endl; + return true; + } + +} diff --git a/ErrorHandler.h b/ErrorHandler.h new file mode 100644 index 0000000..7f11af7 --- /dev/null +++ b/ErrorHandler.h @@ -0,0 +1,22 @@ +#ifndef ERRORHANDLER_H +#define ERRORHANDLER_H + +#include +#include + + +enum class ErrorType{ + DEFAULT = -1, + SDL_ERROR +}; + +class ErrorHandler +{ +private: + + +public: + static bool send(ErrorType e, std::string message); +}; + +#endif /* ERRORHANDLER_H */ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9915845 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +CXX=g++ +LDFLAGS= -lSDL2 -lm +CXXFLAGS= -g -Wall + +SOURCES = $(wildcard *.cpp) + +all: sim + +sim: $(SOURCES) + $(CXX) $(CXXFLAGS) -o sim $(SOURCES) $(LDFLAGS) + +run: sim + ./sim + +clean: + rm sim *.o diff --git a/Vector.cpp b/Vector.cpp new file mode 100644 index 0000000..0903d9d --- /dev/null +++ b/Vector.cpp @@ -0,0 +1,27 @@ +#include "Vector.h" +#include + +Vec2 Vec2::operator+(const Vec2 right){ + + return {x + right.x, y + right.y}; +} + +Vec2 Vec2::operator*(const Vec2 right){ + + return {x * right.x, y * right.y}; +} + +Vec2 Vec2::operator*(const int right){ + + return {x * right, y * right}; +} + +Vec2 Vec2::operator/(const int right){ + + return {x / right, y / right}; +} + +int Vec2::norm(){ + return sqrt(pow(x, 2) + pow(y,2)); +} + diff --git a/Vector.h b/Vector.h new file mode 100644 index 0000000..802262c --- /dev/null +++ b/Vector.h @@ -0,0 +1,16 @@ +#ifndef VECTOR_H +#define VECTOR_H + +struct Vec2 +{ + int x, y = 0; + + Vec2 operator+ (const Vec2 left); + Vec2 operator* (const Vec2 left); + Vec2 operator* (const int left); + Vec2 operator/ (const int left); + + int norm(); +}; + +#endif /* VECTOR_H */ diff --git a/Window.cpp b/Window.cpp new file mode 100644 index 0000000..0464fc6 --- /dev/null +++ b/Window.cpp @@ -0,0 +1,61 @@ +#include "Window.h" +#include "ErrorHandler.h" +#include +#include + +void Window::Draw(std::function f){ + while(Running){ + + Running = f(this); + SDL_RenderClear(ren); + SDL_RenderPresent(ren); + + Events(); + } +} + +void Window::Events(){ + while(SDL_PollEvent(e)){ + if(e->type == SDL_QUIT){ + Running = false; + } + if(e->type == SDL_KEYDOWN){ + switch(e->key.keysym.sym){ + case SDLK_a: + Running = false; + break; + + } + } + + } +} + +Window::Window(std::string title, int width, int height) + : Running(true) +{ + + if(SDL_Init(SDL_INIT_VIDEO) != 0){ + ErrorHandler::send(ErrorType::SDL_ERROR, "SDL_Init"); + } + + win = SDL_CreateWindow("test", 0,0, width, height, SDL_WINDOW_SHOWN); + if(win == NULL){ + ErrorHandler::send(ErrorType::SDL_ERROR, "SDL_CreateWindow"); + } + + ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE); + if(ren == NULL){ + ErrorHandler::send(ErrorType::SDL_ERROR, "SDL_CreateRenderer"); + } +} + +Window::~Window(){ + SDL_DestroyWindow(win); + SDL_DestroyRenderer(ren); + SDL_Quit(); +} + +SDL_Renderer* Window::GetRenderer(){ + return ren; +} diff --git a/Window.h b/Window.h new file mode 100644 index 0000000..aa18d7e --- /dev/null +++ b/Window.h @@ -0,0 +1,29 @@ +#ifndef WINDOW_H +#define WINDOW_H + +#include +#include +#include + +class Window +{ +private: + SDL_Window* win; + SDL_Renderer* ren; + SDL_Event* e; + + bool Running; + +public: + + Window(std::string title, int width, int height); + virtual ~Window(); + + void Setup(); + void Draw(std::function f); + void Events(); + + SDL_Renderer* GetRenderer(); + +}; +#endif /* WINDOW_H */ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..5512f31 --- /dev/null +++ b/main.cpp @@ -0,0 +1,26 @@ +#include "Window.h" +#include "Entity.h" +#include +#include + +bool my_Draw(Window* win){ + SDL_Renderer* ren = win->GetRenderer(); + + SDL_SetRenderDrawColor(ren, 0xff, 0x00, 0x00, 0x00); + + return true; +} + +int main(int argc, char *argv[]) +{ + /* Window win("test", 100, 100); */ + /* win.Draw(my_Draw); */ + + Entity t({0, 0}, {1, 1}, {0,0}); + t.Debug(); + t.Update(); + t.Debug(); + + + return 0; +} diff --git a/sim b/sim new file mode 100755 index 0000000..a900278 Binary files /dev/null and b/sim differ