diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..36f0451 --- /dev/null +++ b/Makefile @@ -0,0 +1,45 @@ +# fxengine makefile +# fxengine needs gint (@Lephenixnoir) + +target ?= sh3eb-elf + +cflags := -m3 -mb -D FX9860G -ffreestanding -nostdlib -fstrict-volatile-bitfields -Wall \ + -Wextra -Os -I . + +lib := libfxengine.a +header := include/ + +prefix := $(shell $(target)-gcc -print-search-dirs | grep install \ + | sed 's/install: //') + +ifeq "$(prefix)" "" +$(error "Can't find install directory") +endif + +src := $(shell find src -name '*.c') + +obj := $(src:%=build/%.o) + + +all: $(lib) + +$(lib): $(obj) + $(target)-ar rcs $@ $^ + + +build/%.c.o: %.c + @ mkdir -p $(dir $@) + $(target)-gcc -c $< -o $@ $(cflags) + +clear: + @ rm -rf build + @ rm -f $(lib) + +%/: + mkdir -p $@ + + +install: + sh3eb-elf-ar -t $(lib) + cp $(lib) $(prefix) + cp -r $(header) $(prefix)include/fxengine \ No newline at end of file diff --git a/build/src/event/keyboard.c.o b/build/src/event/keyboard.c.o new file mode 100644 index 0000000..ad82dd2 Binary files /dev/null and b/build/src/event/keyboard.c.o differ diff --git a/build/src/render/translate.c.o b/build/src/render/translate.c.o new file mode 100644 index 0000000..ca6c293 Binary files /dev/null and b/build/src/render/translate.c.o differ diff --git a/build/src/render/zbuffer.c.o b/build/src/render/zbuffer.c.o new file mode 100644 index 0000000..cc01f0e Binary files /dev/null and b/build/src/render/zbuffer.c.o differ diff --git a/include/event/keyboard.h b/include/event/keyboard.h new file mode 100644 index 0000000..e910d92 --- /dev/null +++ b/include/event/keyboard.h @@ -0,0 +1,27 @@ +#ifndef FE_KEYBOARD +#define FE_KEYBOARD + +#include +#include +#include + +/* FE_keyboard: gestion evenementielle du clavier + on peut assigner des callbacks à certains evènements définis dans gint + les arguments envoyés sont le code de la touche en question (event.key) + le type d'evenement (event.type) + void (*callback)(void) + la fonction à exécuter en cas de pression sur une touche + la fonction reload est appelée à la demande de l'utilisateur et appelle tous les callbacks dans l'ordre */ + +typedef void (*callback)(void); + +void event_keyboard_set_key(uint32_t matrix_code, uint32_t ev_type, callback function); + +// reload all key events and call callbacks +void event_keyboard_reload(); + +//void event_keyboard_start(); + +//void event_keyboard_stop(); + +#endif \ No newline at end of file diff --git a/include/render/parameters.h b/include/render/parameters.h new file mode 100644 index 0000000..02cb5bd --- /dev/null +++ b/include/render/parameters.h @@ -0,0 +1,17 @@ +#ifndef RENDER_PARAM +#define RENDER_PARAM + + +// Render param + +#define render_width 128 +#define render_height 64 +#define render_x_mid ((render_width - 1) / 2) // depends on screen width +#define render_y_mid ((render_height - 1) / 2) + +#define render_max_dist 3000 +#define render_min_dist 1 + + + +#endif \ No newline at end of file diff --git a/include/render/translate.h b/include/render/translate.h new file mode 100644 index 0000000..bef0e4d --- /dev/null +++ b/include/render/translate.h @@ -0,0 +1,61 @@ +#ifndef RENDER_TRANSLATE_H +#define RENDER_TRANSLATE_H + +#include +#include + +/* FE_position: + notion de point dans l'espace simple */ +typedef struct FE_integer_position FE_integer_position; +struct FE_integer_position +{ + int32_t x, + y, + z; +}; + +typedef struct FE_floating_position FE_floating_position; +struct FE_floating_position +{ + double x, + y, + z; +}; + + + +/* FE_point: + notion de point dans l'espace destiné à etre utilisé dans l'affichage */ +typedef struct FE_integer_point FE_integer_point; +struct FE_integer_point +{ + FE_integer_position real, + translated; +}; + + + +// applique la matrice de rotation et les deltas sur les coordonnées d'un point +void render_translate(FE_integer_point * point); + + + +// change la matrice de rotation et les deltas pour le cycle à venir +void render_set(const double dh, const double dv, const double roulis, const FE_integer_position * camera); + + + +// constantes mathématiques + +extern const double pi, pi2, pi_sur_2; + + +// fonctions mathématiques + +double modulo_2pi(double a); + +double cos(double angle); + +double sin(const double angle); + +#endif \ No newline at end of file diff --git a/include/render/zbuffer.h b/include/render/zbuffer.h new file mode 100644 index 0000000..d637339 --- /dev/null +++ b/include/render/zbuffer.h @@ -0,0 +1,21 @@ +#ifndef RENDER_ZBUFFER +#define RENDER_ZBUFFER + +#include +#include + +/** FE_zbuffer_clear + * effacer le z buffer pour un nouveau cycle de dessin + * TODO : ajouter effacement avec le DMA Controller pour les modèles ayant un processeur SH4-A +**/ +void render_zbuffer_clear(); + +#include +/** FE_zbuffer_set_dist + * change la distance d'un pixel du zbuffer + * retourne true si il faut dessiner le pixel + * retourne false si le pixel est déjà existant +**/ +bool render_zbuffer_set_px(uint32_t x, uint32_t y, uint32_t dist); // if you are allowed to draw the pixel on vram + +#endif \ No newline at end of file diff --git a/libfxengine.a b/libfxengine.a new file mode 100644 index 0000000..bc44935 Binary files /dev/null and b/libfxengine.a differ diff --git a/src/event/keyboard.c b/src/event/keyboard.c new file mode 100644 index 0000000..b375d12 --- /dev/null +++ b/src/event/keyboard.c @@ -0,0 +1,33 @@ +#include + +static callback callbacks[3][6][10]={0}; + +static inline uint32_t get_x(const uint32_t matrix_code) +{ + return (matrix_code-1) % 0x10; +} + +static inline uint32_t get_y(const uint32_t matrix_code) +{ + return (matrix_code-1) / 0x10; +} + +void FE_keyboard_reload() +{ + key_event_t event; + while (1) + { + event=pollevent(); + event.type--; + if (event.type==-1) + break; + callback action = callbacks[event.type][get_x(event.key)][get_y(event.key)]; + if (action) + action(); + } +} + +void FE_keyboard_set_key(uint32_t matrix_code, uint32_t ev_type, callback function) +{ + callbacks[ev_type-1][get_x(matrix_code)][get_y(matrix_code)]=function; +} \ No newline at end of file diff --git a/src/render/translate.c b/src/render/translate.c new file mode 100644 index 0000000..b6b203f --- /dev/null +++ b/src/render/translate.c @@ -0,0 +1,123 @@ +#include + +#include +#include + +const double pi = 3.141592653589793238462643383279; +const double pi2 = pi * 2; +const double pi_sur_2 = pi / 2; + + +static double reducted_cos(const double a) +{ + double u= 1.0; + const double a2 = a * a; + for(int32_t p = 15; p>=1; p -= 2) + u = 1 - a2 / (p * p + p) * u; + return u; +} + +// return a with -pi<=api) + a -= pi2; + return a; +} + +static double cos_recursive(double angle) +{ + if (angle<0) + return cos_recursive(-angle); + if (angle>=pi_sur_2) + return -reducted_cos(angle - pi); + return reducted_cos(angle); // OK +} + +double cos(double angle) +{ + angle = modulo_2pi(angle); + return cos_recursive(angle); +} + +double sin(double angle) +{ + return cos(angle - pi_sur_2); +} + + +#define sgn(x) (x>=0?x:-x) + +static double matrice[3][3]= +{ + {0,0,0}, + {0,0,0}, + {0,0,0} +}; + +static FE_integer_position delta; + + +void render_translate(FE_integer_point * point) +{ + static FE_integer_position temp; + temp.x = point->real.x - delta.x; + temp.y = point->real.y - delta.y; + temp.z = point->real.z - delta.z; + + point->translated.x = (double)(matrice[0][0]*(double)temp.x + matrice[0][1]*(double)temp.y + matrice[0][2]*(double)temp.z); + point->translated.z = (double)(matrice[1][0]*(double)temp.x + matrice[1][1]*(double)temp.y + matrice[1][2]*(double)temp.z); + point->translated.y = (double)(matrice[2][0]*(double)temp.x + matrice[2][1]*(double)temp.y + matrice[2][2]*(double)temp.z); + + //point->translated.x*=10; + //point->translated.y*=10; + point->translated.x*=64; + point->translated.y*=64; + if (point->translated.z>0) + { + point->translated.x/=point->translated.z; + point->translated.y/=point->translated.z; + } + else + { + point->translated.x*=32768*sgn(point->translated.z); + point->translated.y*=32768*sgn(point->translated.z); + } + //(point->translated.x*1024)/point->translated.z; + //(point->translated.y*1024)/point->translated.z; + + point->translated.x+=render_x_mid; + point->translated.y+=render_y_mid; +} + +void render_set(const double dh, const double dv, const double roulis, const FE_integer_position * camera) +{ + const double A=cos(dv); + const double B=sin(dv); + + const double C=cos(roulis); + const double D=sin(roulis); + + const double E=cos(dh); + const double F=sin(dh); + + // raccourcis + const double AD=A*D, BD=B*D; + + matrice[0][0]=C*E; + matrice[0][1]=-C*F; + matrice[0][2]=D; + + matrice[1][0]=BD*E+A*F; + matrice[1][1]=-BD*F+A*E; + matrice[1][2]=-B*C; + + matrice[2][0]=-AD*E+B*F; + matrice[2][1]=AD*F+B*E; + matrice[2][2]=A*C; + + // assigner delta + memcpy(&delta, camera, sizeof(FE_integer_position)); +} \ No newline at end of file diff --git a/src/render/zbuffer.c b/src/render/zbuffer.c new file mode 100644 index 0000000..cfd6fab --- /dev/null +++ b/src/render/zbuffer.c @@ -0,0 +1,42 @@ +#include +#include + +#include +#include +#include +#include +#include +#include + + + +static const int size_uint32 = render_width * render_height; + +static const int size_char = size_uint32 * sizeof(uint32_t); + +// zbuffer et clear val sont 32B alignés pour ajouter éventuellement le DMA + +static int32_t *zbuffer = (void *)0x88080000 - ((size_char / 32) * 32 + 1); + +GALIGNED(32) GSECTION(".rodata") static const int32_t clearval[8]={render_max_dist,render_max_dist,render_max_dist,render_max_dist,render_max_dist,render_max_dist,render_max_dist,render_max_dist}; + +void render_zbuffer_clear() +{ + uint32_t indice = 0; + + for (indice = 0; indice < size_uint32; indice ++) + zbuffer[indice] = render_max_dist; +} + +bool render_zbuffer_set_px(uint32_t x, uint32_t y, uint32_t dist) +{ + const int indice = x * render_height + y; + + if (zbuffer[indice]>dist && dist>=render_min_dist && dist<=render_max_dist) + { + zbuffer[indice] = dist; + return true; + } + + return false; +} \ No newline at end of file diff --git a/unused temp.tar.xz b/unused temp.tar.xz new file mode 100644 index 0000000..da5c565 Binary files /dev/null and b/unused temp.tar.xz differ