First Commit

This commit is contained in:
Debucquoy Anthony 2022-10-30 23:44:30 +01:00
commit fdd1e361c0
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
11 changed files with 251 additions and 0 deletions

19
Entity.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "Entity.h"
#include <iostream>
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;
}

20
Entity.h Normal file
View File

@ -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 */

15
ErrorHandler.cpp Normal file
View File

@ -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;
}
}

22
ErrorHandler.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef ERRORHANDLER_H
#define ERRORHANDLER_H
#include <iostream>
#include <SDL2/SDL.h>
enum class ErrorType{
DEFAULT = -1,
SDL_ERROR
};
class ErrorHandler
{
private:
public:
static bool send(ErrorType e, std::string message);
};
#endif /* ERRORHANDLER_H */

16
Makefile Normal file
View File

@ -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

27
Vector.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "Vector.h"
#include <math.h>
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));
}

16
Vector.h Normal file
View File

@ -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 */

61
Window.cpp Normal file
View File

@ -0,0 +1,61 @@
#include "Window.h"
#include "ErrorHandler.h"
#include <SDL2/SDL.h>
#include <iostream>
void Window::Draw(std::function<bool(Window*)> 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;
}

29
Window.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef WINDOW_H
#define WINDOW_H
#include <SDL2/SDL.h>
#include <string>
#include <functional>
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<bool(Window*)> f);
void Events();
SDL_Renderer* GetRenderer();
};
#endif /* WINDOW_H */

26
main.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "Window.h"
#include "Entity.h"
#include <functional>
#include <SDL2/SDL.h>
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;
}

BIN
sim Executable file

Binary file not shown.