Copy3DEngine/eng/moteur.c

397 lines
12 KiB
C
Raw Permalink Normal View History

2024-03-09 21:21:17 +01:00
// Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3
2023-07-20 12:31:08 +02:00
#include <math.h>
#include <stdlib.h>
2024-09-28 22:11:47 +02:00
#include <stdint.h>
2023-07-20 12:31:08 +02:00
#include <string.h>
#include <time.h>
#include <gint/display.h>
2024-10-08 13:16:05 +02:00
#include <gint/display-fx.h>
2023-07-20 12:31:08 +02:00
#include <gint/keyboard.h>
2024-09-07 11:42:56 +02:00
#include <gint/gray.h>
#include <libprof.h>
2023-07-20 12:31:08 +02:00
2024-10-22 12:27:16 +02:00
#include "C3D/fixed.h"
#include "C3D/moteur.h"
#include "C3D/map.h"
#include "C3D/game.h"
#include "C3D/config.h"
2023-07-20 12:31:08 +02:00
// moteur.c :
// ici se trouvent tout ce qui concerne les graphismes, mouvement et collisions
//
2024-10-05 21:48:42 +02:00
//1D zbuf for sprites
fixed_t zbuf[viewport_w];
2024-10-05 00:56:13 +02:00
void move(RcGame *game) {
2023-07-20 12:31:08 +02:00
extern int frame_time;
2024-10-25 23:05:09 +02:00
fixed_t moveSpeed = fmul(fix(frame_time/1000), 0x148); //frame_time * fix(carrés/seconde/1000) là carrés/seconde = 5
fixed_t rotSpeed = fmul(fix(frame_time/1000), 0x83); //frame_time * fix(radians/seconde/1000) là radians/seconde = 2
2023-07-20 12:31:08 +02:00
fixed_t c_rotSpeed = fix(cos(f2float(rotSpeed)));
fixed_t s_rotSpeed = fix(sin(f2float(rotSpeed)));
2024-10-10 17:53:09 +02:00
uint8_t *map_test = game->current_map->dat;
2024-10-05 00:56:13 +02:00
V2d *plane = &game->player.plane;
V2d *dir = &game->player.dir;
V2d *pos = &game->player.pos;
2024-10-25 23:05:09 +02:00
int map_w = game->current_map->w;
int map_h = game->current_map->h;
2023-07-20 12:31:08 +02:00
fixed_t oldDirX;
fixed_t oldPlaneX;
int xtemp1;
int ytemp1;
int xtemp2;
int ytemp2;
2024-03-09 21:21:17 +01:00
if (keydown(KEY_UP)) {
2024-10-05 00:56:13 +02:00
xtemp1 = ffloor(pos->x + fmul(dir->x, moveSpeed));
ytemp1 = ffloor(pos->y);
xtemp2 = ffloor(pos->x);
ytemp2 = ffloor(pos->y + fmul(dir->y, moveSpeed));
if(!map_test[xtemp1*map_w+ytemp1])
pos->x += fmul(dir->x, moveSpeed);
if(!map_test[xtemp2*map_w+ytemp2])
pos->y += fmul(dir->y, moveSpeed);
2023-07-20 12:31:08 +02:00
}
//move backwards if no wall behind you
if (keydown(KEY_DOWN)) {
2024-10-05 00:56:13 +02:00
xtemp1 = ffloor(pos->x - fmul(dir->x, moveSpeed));
ytemp1 = ffloor(pos->y);
xtemp2 = ffloor(pos->x);
ytemp2 = ffloor(pos->y - fmul(dir->y, moveSpeed));
if(!map_test[xtemp1*map_w+ytemp1])
pos->x -= fmul(dir->x, moveSpeed);
if(!map_test[xtemp2*map_w+ytemp2])
pos->y -= fmul(dir->y, moveSpeed);
2023-07-20 12:31:08 +02:00
}
2024-10-05 00:56:13 +02:00
//rotate to the right
2023-07-20 12:31:08 +02:00
if (keydown(KEY_RIGHT)) {
//both camera direction and camera plane must be rotated
2024-10-05 00:56:13 +02:00
oldDirX = dir->x;
dir->x = (fmul(dir->x, c_rotSpeed)+1) - (fmul(dir->y, -s_rotSpeed)+1);
dir->y = (fmul(oldDirX, -s_rotSpeed)+1) + (fmul(dir->y, c_rotSpeed)+1);
oldPlaneX = plane->x;
plane->x = (fmul(plane->x, c_rotSpeed)+1) - (fmul(plane->y, -s_rotSpeed)+1);
plane->y = (fmul(oldPlaneX, -s_rotSpeed)+1) + (fmul(plane->y, c_rotSpeed)+1);
2023-07-20 12:31:08 +02:00
}
//rotate to the left
if (keydown(KEY_LEFT)) {
//both camera direction and camera plane must be rotated
2024-10-05 00:56:13 +02:00
oldDirX = dir->x;
dir->x = (fmul(dir->x, c_rotSpeed)-1) - (fmul(dir->y, s_rotSpeed)-1);
dir->y = (fmul(oldDirX, s_rotSpeed)+1) + (fmul(dir->y, c_rotSpeed)+1);
oldPlaneX = plane->x;
plane->x = (fmul(plane->x, c_rotSpeed)-1) - (fmul(plane->y, s_rotSpeed) - 1);
plane->y = (fmul(oldPlaneX, s_rotSpeed)+1) + (fmul(plane->y, c_rotSpeed) + 1);
2023-07-20 12:31:08 +02:00
}
2024-10-05 00:56:13 +02:00
if (dir->x > 0xFFFF) dir->x = 0xFFFF;
if (dir->y > 0xFFFF) dir->y = 0xFFFF;
if (dir->x < -0xFFFF) dir->x = -0xFFFF;
if (dir->y < -0xFFFF) dir->y = -0xFFFF;
2023-07-20 12:31:08 +02:00
}
void load_map(){
2024-10-05 00:56:13 +02:00
2023-07-20 12:31:08 +02:00
}
2024-09-28 22:11:47 +02:00
//Fonction fournie par le grand Lephé :D
2024-09-26 19:09:33 +02:00
inline int __attribute__((always_inline))
get_pixel(bopti_image_t const *img, int u, int v)
2024-10-05 00:56:13 +02:00
{
int layers = image_layer_count(img->profile);
int columns = (img->width + 31) >> 5;
uint32_t const *layer_ptr = (void *)img->data;
layer_ptr += (v * columns + (u >> 5)) * layers;
uint32_t mask = 0x80000000u >> (u & 31);
int light = (layer_ptr[0] & mask) != 0;
int dark = (layer_ptr[1] & mask) != 0;
2024-10-08 13:16:05 +02:00
int alpha = 0;
if(layers > 2){
alpha = !(layer_ptr[2] & mask);
}
2024-10-25 23:05:09 +02:00
return (alpha << 2) | (dark << 1) | light;
2024-10-05 00:56:13 +02:00
}
2024-09-26 19:09:33 +02:00
uint32_t *light, *dark;
2024-09-07 11:42:56 +02:00
2024-09-28 22:11:47 +02:00
//Version simplifiée de gpixel dans gint/src/gray/gpixel.c
//Tout le crédit revient à Lephé
inline void __attribute__((always_inline))
cust_dpixel(int x, int y, color_t color){
2024-10-05 21:48:42 +02:00
if(y >= viewport_h || y < 0)
2024-09-28 22:11:47 +02:00
return;
int offset = (y << 2) + (x >> 5);
uint32_t mask = 1 << (~x & 31);
switch(color){
case C_WHITE:
light[offset] &= ~mask;
dark [offset] &= ~mask;
break;
case C_LIGHT:
light[offset] |= mask;
dark [offset] &= ~mask;
break;
case C_DARK:
light[offset] &= ~mask;
dark [offset] |= mask;
break;
case C_BLACK:
light[offset] |= mask;
dark [offset] |= mask;
break;
2024-10-25 23:05:09 +02:00
2024-10-08 13:16:05 +02:00
case C_NONE:
break;
2024-09-28 22:11:47 +02:00
default: break;
}
}
void dscale_bopti(bopti_image_t *tex, fixed_t scale_x, fixed_t scale_y, int x, int y){
2024-09-28 22:11:47 +02:00
dgray_getvram(&light,&dark);
fixed_t screen_y = fix(y);
fixed_t tex_y = 0;
do{
fixed_t oldy = screen_y;
2024-09-28 22:11:47 +02:00
if(screen_y < 0 || screen_y+scale_x > viewport_h)
return;
do{
fixed_t screen_x = fix(x);
fixed_t tex_x = 0;
do{
fixed_t oldx = screen_x;
int tpix = get_pixel(tex, tex_x, tex_y);
2024-09-28 22:11:47 +02:00
if(screen_x < 0 || screen_x+scale_x > viewport_w)
return;
do{
2024-10-05 21:48:42 +02:00
cust_dpixel(ffloor(oldx), ffloor(oldy), tpix);
2024-09-28 22:11:47 +02:00
oldx += 0xFFFF;
} while(oldx < screen_x+scale_x);
screen_x += scale_x;
tex_x++;
} while(tex_x < tex->width);
oldy += 0xFFFF;
} while(oldy < screen_y+scale_y);
screen_y += scale_y;
tex_y++;
} while(tex_y < tex->height);
}
2024-09-26 19:09:33 +02:00
inline void __attribute__((always_inline))
draw_stripe(bopti_image_t *tex, int texSampleY, int linePos, fixed_t texSize,
int texX, int x){
2024-09-07 11:42:56 +02:00
fixed_t screenPos = fix(linePos);
2024-09-07 11:42:56 +02:00
for(int texPos = texSampleY; texPos < TSIZE; ++texPos){
if(screenPos >= 0){
2024-09-01 16:38:23 +02:00
fixed_t oldPos = screenPos;
2024-09-26 19:09:33 +02:00
int tpix = get_pixel(tex, texX, texPos);
2024-09-01 16:38:23 +02:00
do{
2024-09-07 11:42:56 +02:00
oldPos += 0xFFFF;
2024-09-28 22:11:47 +02:00
cust_dpixel(x, fround(oldPos), tpix);
} while(oldPos < screenPos+texSize);
}
screenPos += texSize;
}
}
2024-09-01 16:38:23 +02:00
void draw_walls(
#if debug
2024-10-05 00:56:13 +02:00
EngineTimers *timers,
2024-09-01 16:38:23 +02:00
#endif
2024-10-05 00:56:13 +02:00
RcGame *game
2024-09-01 16:38:23 +02:00
){
2024-10-05 00:56:13 +02:00
fixed_t posX = game->player.pos.x;
fixed_t posY = game->player.pos.y;
fixed_t dirX = game->player.dir.x;
fixed_t dirY = game->player.dir.y;
fixed_t planeX = game->player.plane.x;
fixed_t planeY = game->player.plane.y;
2024-09-07 11:42:56 +02:00
extern bopti_image_t *tex_index[TINDEX_S];
2024-10-10 17:53:09 +02:00
uint8_t *map_test = game->current_map->dat;
2024-10-25 23:05:09 +02:00
int map_w = game->current_map->w;
int map_h = game->current_map->h;
2023-07-20 12:31:08 +02:00
fixed_t cameraX;
fixed_t rayDirX;
fixed_t rayDirY;
fixed_t sideDistX;//length of ray from current position to next x or y-side
fixed_t sideDistY;
fixed_t deltaDistX;
fixed_t deltaDistY;
fixed_t perpWallDist;
int x;
int mapX;
int mapY;
int stepX; //what direction to step in x or y-direction (either +1 or -1)
int stepY;
2024-10-09 16:43:10 +02:00
int side = 0; //was a NS or a EW wall hit?
2023-07-20 12:31:08 +02:00
int lineHeight;
int texX;
int texSample;
int texSampleY;
2024-09-26 19:09:33 +02:00
dgray_getvram(&light,&dark);
2024-10-09 16:43:10 +02:00
//int v_offset = 0;
2024-09-28 22:11:47 +02:00
fixed_t h_offset = 0;
2023-07-20 12:31:08 +02:00
2024-09-07 11:42:56 +02:00
//struct image_linear_map temp;
2023-07-20 12:31:08 +02:00
for(x = 0; x < viewport_w; x++) {
2024-09-01 16:38:23 +02:00
#if debug
prof_enter(timers->raycast_time);
#endif
2023-07-20 12:31:08 +02:00
//calculate ray position and direction
cameraX = fdiv(fix(x*2), fix(viewport_w)) - 0xFFFF + h_offset; //x-coordinate in camera space
rayDirX = dirX + fmul(planeX, cameraX);
rayDirY = dirY + fmul(planeY, cameraX);
//which box of the map we're in
2024-10-05 00:56:13 +02:00
mapX = ffloor(posX);
mapY = ffloor(posY);
2023-07-20 12:31:08 +02:00
// length of ray from one x or y-side to next x or y-side
// these are derived as:
// deltaDistX = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX))
// deltaDistY = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY))
// which can be simplified to abs(|rayDir| / rayDirX) and abs(|rayDir| / rayDirY)
// where |rayDir| is the length of the vector (rayDirX, rayDirY). Its length,
// unlike (dirX, dirY) is not 1, however this does not matter, only the
// ratio between deltaDistX and deltaDistY matters, due to the way the DDA
// stepping further below works. So the values can be computed as below.
// Division through zero is prevented, even though technically that's not
// needed in C++ with IEEE 754 floating point values.
2024-09-01 16:38:23 +02:00
//Fcalva : It is with fp32s !
rayDirX = rayDirX == 0 ? 1 : rayDirX;
rayDirY = rayDirY == 0 ? 1 : rayDirY;
2023-07-20 12:31:08 +02:00
deltaDistX = abs(fdiv(0xFFFF, rayDirX));
deltaDistY = abs(fdiv(0xFFFF, rayDirY));
2024-10-05 21:48:42 +02:00
if(deltaDistX > max_dist) deltaDistX = max_dist;
if(deltaDistY > max_dist) deltaDistY = max_dist;
2023-07-20 12:31:08 +02:00
//calculate step and initial sideDist
2023-10-19 17:15:52 +02:00
if (rayDirX < 0) {
stepX = -1;
2023-07-20 12:31:08 +02:00
sideDistX = fmul(posX - fix(mapX), deltaDistX);
}
else {
stepX = 1;
sideDistX = fmul( fix(mapX + 1) - posX, deltaDistX);
}
2023-08-20 15:45:57 +02:00
if (rayDirY == 0) {
stepY = 0;
2024-03-09 21:21:17 +01:00
sideDistY = 0;
2023-08-20 15:45:57 +02:00
}
else if (rayDirY < 0) {
2023-10-19 17:15:52 +02:00
stepY = -1;
2023-07-20 12:31:08 +02:00
sideDistY = fmul(posY - fix(mapY), deltaDistY);
}
else {
stepY = 1;
sideDistY = fmul( fix(mapY + 1) - posY, deltaDistY);
}
//perform DDA
while(true) {
//Check if the ray is out of range/bounds
2024-10-05 00:56:13 +02:00
if (sideDistX >= max_dist || sideDistY >= max_dist || mapX < 0 || mapY < 0 || mapY >= map_w || mapX >= map_h) {
2023-07-20 12:31:08 +02:00
break;
}
//Otherwise check if ray has hit a wall
2024-10-05 00:56:13 +02:00
if (map_test[mapX*map_w+mapY] > 0) {
2023-07-20 12:31:08 +02:00
break;
}
//jump to next map square, either in x-direction, or in y-direction
if (sideDistX < sideDistY) {
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
}
else {
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
}
//Calculate distance projected on camera direction. This is the shortest distance from the point where the wall is
//hit to the camera plane. Euclidean to center camera point would give fisheye effect!
//This can be computed as (mapX - posX + (1 - stepX) / 2) / rayDirX for side == 0, or same formula with Y
//for size == 1, but can be simplified to the code below thanks to how sideDist and deltaDist are computed:
//because they were left scaled to |rayDir|. sideDist is the entire length of the ray above after the multiple
//steps, but we subtract deltaDist once because one step more into the wall was taken above.
if (side == 0) perpWallDist = (sideDistX - deltaDistX);
else perpWallDist = (sideDistY - deltaDistY);
2024-10-05 21:48:42 +02:00
zbuf[x] = perpWallDist;
2024-09-01 16:38:23 +02:00
#if debug
prof_leave(timers->raycast_time);
prof_enter(timers->draw_time);
#endif
2023-07-20 12:31:08 +02:00
//texturing calculations
//calculate value of wallX
fixed_t wallX; //where exactly the wall was hit
if (side == 0) wallX = posY + fmul(perpWallDist, rayDirY);
else wallX = posX + fmul(perpWallDist, rayDirX);
2024-10-05 00:56:13 +02:00
wallX -= fix(ffloor(wallX));
2023-07-20 12:31:08 +02:00
//x coordinate on the texture
2024-09-07 11:42:56 +02:00
texX = fmul(wallX, TSIZE);
texX %= TSIZE;
2024-03-09 21:21:17 +01:00
2023-07-20 12:31:08 +02:00
if(side == 0 && rayDirX > 0) texX = 64 - texX - 1;
if(side == 1 && rayDirY < 0) texX = 64 - texX - 1;
2024-10-05 00:56:13 +02:00
lineHeight = ffloor(fdiv(fix(viewport_h), perpWallDist)); //Taille en px de la ligne
2023-07-20 12:31:08 +02:00
if (lineHeight < 1) lineHeight = 1;
2024-09-26 19:09:33 +02:00
//if (lineHeight > viewport_h) lineHeight = viewport_h - 1;
2023-07-20 12:31:08 +02:00
2024-09-07 11:42:56 +02:00
fixed_t texSize = fix(lineHeight) / TSIZE; //taille proportionelle de la ligne a la tex
2024-09-26 19:09:33 +02:00
if (texSize < fix(1.0/(float)TSIZE)) texSize = fix(1.0/(float)TSIZE);
if (texSize > fix((float)viewport_h/(float)TSIZE)) {
texSample = fceil(fdiv(fix(viewport_h), texSize));
2024-09-26 19:09:33 +02:00
texSampleY = TSIZE/2 - texSample/2;
2023-07-20 12:31:08 +02:00
}
else {
2024-09-07 11:42:56 +02:00
texSample = TSIZE;
2023-07-20 12:31:08 +02:00
texSampleY = 0;
}
2024-10-27 23:52:11 +01:00
int linePos = viewport_h/2 - lineHeight/2 + game->flags.v_offset;
2024-10-05 00:56:13 +02:00
bopti_image_t *tex = tex_index[map_test[mapX*map_w+mapY]];
2024-09-07 11:42:56 +02:00
2024-10-05 21:48:42 +02:00
//dline(x, linePos, x, linePos + lineHeight, 1);
2024-09-28 22:11:47 +02:00
draw_stripe(tex, texSampleY, linePos,
2024-09-07 11:42:56 +02:00
texSize, texX, x);
2024-09-01 16:38:23 +02:00
#if debug
prof_leave(timers->draw_time);
#endif
}
2023-07-20 12:31:08 +02:00
}