dos-3D-Engine/src/part3D/defines.h
2026-05-11 17:53:07 +02:00

139 lines
4.9 KiB
C

#ifndef DEFINE_H
#define DEFINE_H
// #define MINIMAL_PLAYER
#include <stdint.h>
#include <stdlib.h>
#ifndef MINIMAL_PLAYER
#include <math.h> // Uniquement nécessaire pour la génération
#endif
#include <string.h>
#include <stdio.h>
#include <conio.h>
#define MODE_WIRE 0
#define MODE_HIDDEN 1
#define MODE_SOLID 2
#define SCREEN_W 320
#define SCREEN_H 200
#define SCREEN_PIXELS (SCREEN_W * SCREEN_H)
#define MAX_VERTEX 5000
#define MAX_FACES 8000
#define MAX_OBJECTS 64
typedef struct { uint8_t r, g, b; } RGB;
#define COLOR0 0 // 0: Noir
#define COLOR1 1 // 1: Rouge vif
#define COLOR2 2 // 2: Vert vif
#define COLOR3 3 // 3: Bleu vif
#define COLOR4 4 // 4: Jaune
#define COLOR5 5 // 5: Magenta
#define COLOR6 6 // 6: Cyan
#define COLOR7 7 // 7: Blanc
#define COLOR8 8 // 8: Rouge sombre
#define COLOR9 9 // 9: Vert sombre
#define COLOR10 10 // 10: Bleu sombre
#define COLOR11 11 // 11: Gris
#define COLOR12 12 // 12: Orange
#define COLOR13 13 // 13: Citron vert
#define COLOR14 14 // 14: Marron
#define COLOR15 15 // 15: Bleu nuit
typedef int32_t fixed;
#define f_to_int(a) ((a) >> 16)
#define int_to_f(a) ((a) << 16)
static __inline fixed f_mul(fixed a, fixed b) { return (fixed)(((int64_t)a * b) >> 16); }
static __inline fixed f_div(fixed a, fixed b) { if (b == 0) return 0; return (fixed)(((int64_t)a << 16) / b); }
typedef struct { fixed m[3][3]; } Matrix3;
typedef struct { fixed x, y, z; } Vector3;
typedef struct { int a, b, c; uint8_t force_wire; } Face;
typedef struct { int x, y; fixed z; fixed intensity; } Point2D;
/* Un objet dans l'univers : tranche du pool partagé + transform */
typedef struct {
int vert_start, num_verts; /* indices dans Universe.verts/v_normals */
int face_start, num_faces; /* indices dans Universe.faces */
Vector3 pos; /* position monde courante (mise à jour par orbite) */
Vector3 base_pos; /* offset par rapport au centre d'orbite */
Matrix3 rot;
uint8_t color;
} Object3D;
/* Pool unique pour toute la scène */
typedef struct {
Vector3 verts[MAX_VERTEX];
Vector3 v_normals[MAX_VERTEX];
Face faces[MAX_FACES];
int num_verts, num_faces;
Object3D objects[MAX_OBJECTS];
int num_objects;
} Universe;
typedef struct {
Vector3 pos;
} Camera;
/* Mesh dynamique conservé pour save/load uniquement */
typedef struct {
Vector3 *verts;
Vector3 *v_normals;
Face *faces;
int num_verts, num_faces;
} Mesh;
/* Globales */
extern fixed sintab[360], costab[360];
extern Point2D v_cache[MAX_VERTEX];
extern uint8_t *backbuffer;
extern uint16_t *zbuffer;
extern uint8_t *vga;
/* Prototypes */
/* Type pour les fonctions de génération de surface : reçoit x, z et renvoie y */
typedef fixed (*SurfaceFunc)(fixed x, fixed z);
void init_engine_math(void);
void mat3_identity(Matrix3 *mat);
void mat3_rotate_x(Matrix3 *m, int angle);
void mat3_rotate_y(Matrix3 *m, int angle);
void mat3_mul(Matrix3 *res, Matrix3 *a, Matrix3 *b);
void set_object_pos (Object3D *obj, fixed x, fixed y, fixed z);
void move_object (Object3D *obj, fixed dx, fixed dy, fixed dz);
void set_object_color (Object3D *obj, uint8_t col);
void bake_object_rotation(Universe *uni, Object3D *obj, int ax, int ay, int az);
/* Retournent un pointeur sur le nouvel Object3D, NULL si pool saturé */
Object3D* add_box (Universe *uni, fixed x, fixed y, fixed z, fixed dx, fixed dy, fixed dz, uint8_t col, uint8_t wire);
Object3D* add_sphere (Universe *uni, fixed x, fixed y, fixed z, fixed r, int det, uint8_t col, uint8_t wire);
Object3D* add_cylinder (Universe *uni, fixed x, fixed y, fixed z, fixed r, fixed h, int det, uint8_t col, uint8_t wire);
Object3D* add_cone (Universe *uni, fixed x, fixed y, fixed z, fixed r, fixed h, int det, uint8_t col, uint8_t wire);
Object3D* add_plane (Universe *uni, fixed x, fixed y, fixed z, fixed w, fixed d, int seg_x, int seg_z, uint8_t col, uint8_t wire);
Object3D* add_torus (Universe *uni, fixed x, fixed y, fixed z, fixed r_main, fixed r_tube, int seg_main, int seg_tube, uint8_t col, uint8_t wire);
Object3D* add_math_surface(Universe *uni, fixed x, fixed y, fixed z,
fixed w, fixed d, int seg_x, int seg_z,
uint8_t col, uint8_t wire, SurfaceFunc func);
void render_universe(Universe *uni, Camera *cam, int mode);
int init_vga(void);
void clear_buffers(uint8_t color);
void flip(void);
void close_vga(void);
void fill_triangle_gouraud(Point2D *p1, Point2D *p2, Point2D *p3, uint8_t base_color);
void draw_line_z(Point2D *p1, Point2D *p2, uint8_t col, int z_test);
void save_mesh(Mesh *m, const char *fn);
Mesh *load_mesh(const char *fname);
void free_mesh(Mesh *m);
#endif