- Ja carrega els patch, pero les textures es veuen mal. Arreglant mil coses del motor que tenia a foc pintar textures de 64x64

This commit is contained in:
2026-02-27 08:05:24 +01:00
parent 8dbcd4fcb7
commit 8e46fa85b2
4 changed files with 58 additions and 20 deletions

View File

@@ -99,6 +99,21 @@ namespace draw
return surf->pixels[x+y*surf->w]; return surf->pixels[x+y*surf->w];
} }
void putpsurf(surface_t *dest, int x, int y, uint8_t color)
{
if (x<0 || y<0 || x>=dest->w || y>=dest->h) return;
dest->pixels[x+y*dest->w] = color;
}
void drawsurf(int x, int y, surface_t *src, surface_t *dst)
{
int w = src->w; if (x+w > dst->w) w = dst->w-x;
int h = src->h; if (y+h > dst->h) h = dst->h-y;
for (int py=0;py<h;++py)
for (int px=0;px<w;++px)
dst->pixels[(px+x)+(py+y)*dst->w] = src->pixels[x+y*src->w];
}
void line(int x1, int y1, int x2, int y2, Uint8 color) void line(int x1, int y1, int x2, int y2, Uint8 color)
{ {
float dx = float(x2-x1); float dx = float(x2-x1);

4
draw.h
View File

@@ -20,6 +20,10 @@ namespace draw
void putpd(int x, int y, uint8_t color, float depth); void putpd(int x, int y, uint8_t color, float depth);
void putps(int x, int y, uint8_t color, float depth); void putps(int x, int y, uint8_t color, float depth);
uint8_t getp(surface_t *surf, int x, int y); uint8_t getp(surface_t *surf, int x, int y);
void putpsurf(surface_t *dest, int x, int y, uint8_t color);
void drawsurf(int x, int y, surface_t *src, surface_t *dst);
void line(int x1, int y1, int x2, int y2, uint8_t color); void line(int x1, int y1, int x2, int y2, uint8_t color);
void render(); void render();
void flip(); void flip();

View File

@@ -6,7 +6,7 @@
#include "draw.h" #include "draw.h"
#include "wad.h" #include "wad.h"
#define FOV 190 #define FOV 277
struct wall { struct wall {
Uint16 v1, v2; Uint16 v1, v2;
@@ -50,7 +50,7 @@ void createMap()
{ {
sector s; sector s;
s.floor_height = 32.0f; s.floor_height = 32.0f;
s.ceiling_height = 128.0f; s.ceiling_height = 96.0f;
s.verts.push_back({ 64.0f, 0.0f}); s.verts.push_back({ 64.0f, 0.0f});
s.verts.push_back({256.0f, 0.0f}); s.verts.push_back({256.0f, 0.0f});
@@ -75,7 +75,7 @@ void createMap()
{ {
sector s; sector s;
s.floor_height = 0.0f; s.floor_height = 0.0f;
s.ceiling_height = 96.0f; s.ceiling_height = 128.0f;
s.verts.push_back({256.0f, 0.0f}); s.verts.push_back({256.0f, 0.0f});
s.verts.push_back({ 64.0f, 0.0f}); s.verts.push_back({ 64.0f, 0.0f});
@@ -103,7 +103,8 @@ void createMap()
{ {
for (auto &w : s.walls ) for (auto &w : s.walls )
{ {
w.u2 = distance(s.verts[w.v1], s.verts[w.v2]) / 64.0f; w.u2 = distance(s.verts[w.v1], s.verts[w.v2]) / 8.0f;
//w.v2 = distance(s.verts[w.v1], s.verts[w.v2]) / 32.0f;
vec2 norm = { s.verts[w.v2].x - s.verts[w.v1].x, s.verts[w.v2].y - s.verts[w.v1].y}; vec2 norm = { s.verts[w.v2].x - s.verts[w.v1].x, s.verts[w.v2].y - s.verts[w.v1].y};
normalize(&norm); normalize(&norm);
const float tmp = norm.x; norm.x = -norm.y; norm.y = tmp; const float tmp = norm.x; norm.x = -norm.y; norm.y = tmp;
@@ -135,18 +136,18 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v
} }
if (w) if (w)
{ {
const int tex_height = 64; //const int tex_height = gif->h; //64;
const float sector_height = s.ceiling_height - s.floor_height; const float sector_height = s.ceiling_height - s.floor_height;
draw::map::putp(result.x, result.y, 6); draw::map::putp(result.x, result.y, 6);
float dist = stright_dist * SDL_cosf(a_inc*DEG_TO_RAD); float dist = stright_dist * SDL_cosf(a_inc*DEG_TO_RAD);
const vec2 AB = {s.verts[w->v2].x-s.verts[w->v1].x, s.verts[w->v2].y-s.verts[w->v1].y}; const vec2 AB = {s.verts[w->v2].x-s.verts[w->v1].x, s.verts[w->v2].y-s.verts[w->v1].y};
const vec2 AP = {result.x-s.verts[w->v1].x, result.y-s.verts[w->v1].y}; const vec2 AP = {result.x-s.verts[w->v1].x, result.y-s.verts[w->v1].y};
float v = dot(AP,AB) / dot(AB,AB); v *= w->u2; v = (v-int(v))*tex_height; float v = dot(AP,AB) / dot(AB,AB); v *= w->u2; v = v*gif->w; //(v-int(v))*gif->w;
float wall_height = (sector_height*FOV)/dist; // [64=altura sector] float wall_height = (sector_height*FOV)/dist; // [64=altura sector]
float wall_cut = 0.0f; float wall_cut = 0.0f;
float dpix = tex_height/wall_height; // [64=crec que altura sector] float dpix = sector_height/wall_height; // [64=crec que altura sector]
float cpix = 0; float cpix = 0;
float wall_start = 120-(wall_height/sector_height)*(sector_height-(height-s.floor_height)); // [64=els dos crec que altura sector] float wall_start = 120-(wall_height/sector_height)*(sector_height-(height-s.floor_height)); // [64=els dos crec que altura sector]
if (wall_start<start) { if (wall_start<start) {
@@ -160,9 +161,9 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v
for (int y=start; y<wall_start-1; y++) { for (int y=start; y<wall_start-1; y++) {
float straight_dist = (FOV * (sector_height-(height-s.floor_height))) / (y - (240 >> 1)); float straight_dist = (FOV * (sector_height-(height-s.floor_height))) / (y - (240 >> 1));
float actual_dist = straight_dist / SDL_cosf(a_inc*DEG_TO_RAD); float actual_dist = straight_dist / SDL_cosf(a_inc*DEG_TO_RAD);
int tx = SDL_abs(int(actual_dist * SDL_cosf(angle*DEG_TO_RAD) - position.x)) % tex_height; int tx = SDL_abs(int(actual_dist * SDL_cosf(angle*DEG_TO_RAD) - position.x)) % gif->w;
int ty = SDL_abs(int(actual_dist * SDL_sinf(angle*DEG_TO_RAD) - position.y)) % tex_height; int ty = SDL_abs(int(actual_dist * SDL_sinf(angle*DEG_TO_RAD) - position.y)) % gif->h;
draw::putpd(screen_column, y, gif->pixels[tx+ty*tex_height], -straight_dist); draw::putpd(screen_column, y, gif->pixels[tx+ty*gif->w], -straight_dist);
} }
@@ -171,7 +172,7 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v
// Pinta la pared // Pinta la pared
for (int i=0; i<wall_height; ++i) { for (int i=0; i<wall_height; ++i) {
if (wall_start+i>=end) break; if (wall_start+i>=end) break;
draw::putpd(screen_column, wall_start+i, gif->pixels[(int(v)%tex_height)+int(cpix)*tex_height], stright_dist); draw::putpd(screen_column, wall_start+i, gif->pixels[(int(v)%gif->w)+(int(cpix)%gif->h)*gif->w], stright_dist);
cpix += dpix; cpix += dpix;
} }
} else { } else {
@@ -185,7 +186,7 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v
// Pinta la pared // Pinta la pared
for (int i=0; i<upper_wall_height; ++i) { for (int i=0; i<upper_wall_height; ++i) {
if (wall_start+i>=end) break; if (wall_start+i>=end) break;
draw::putpd(screen_column, wall_start+i, gif->pixels[(int(v)%tex_height)+int(cpix)*tex_height], stright_dist); draw::putpd(screen_column, wall_start+i, gif->pixels[(int(v)%gif->w)+(int(cpix)%gif->h)*gif->w], stright_dist);
cpix += dpix; cpix += dpix;
} }
wall_start += upper_wall_height; wall_start += upper_wall_height;
@@ -206,7 +207,7 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v
// Pinta la pared // Pinta la pared
for (int i=0; i<lower_wall_height; ++i) { for (int i=0; i<lower_wall_height; ++i) {
if (wall_end+i>=end) break; if (wall_end+i>=end) break;
draw::putpd(screen_column, wall_end+i, gif->pixels[(int(v)%tex_height)+int(cpix)*tex_height], stright_dist); draw::putpd(screen_column, wall_end+i, gif->pixels[(int(v)%gif->w)+(int(cpix)%gif->h)*gif->w], stright_dist);
cpix += dpix; cpix += dpix;
} }
//wall_start += upper_wall_height; //wall_start += upper_wall_height;
@@ -220,9 +221,9 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v
for (int y=paint_end+1; y<end-1; y++) { for (int y=paint_end+1; y<end-1; y++) {
float straight_dist = (FOV * (height-s.floor_height)) / (y - (240 >> 1)); float straight_dist = (FOV * (height-s.floor_height)) / (y - (240 >> 1));
float actual_dist = straight_dist / SDL_cosf(a_inc*DEG_TO_RAD); float actual_dist = straight_dist / SDL_cosf(a_inc*DEG_TO_RAD);
int tx = SDL_abs(int(actual_dist * SDL_cosf(angle*DEG_TO_RAD) + position.x)) % tex_height; int tx = SDL_abs(int(actual_dist * SDL_cosf(angle*DEG_TO_RAD) + position.x)) % gif->w;
int ty = SDL_abs(int(actual_dist * SDL_sinf(angle*DEG_TO_RAD) + position.y)) % tex_height; int ty = SDL_abs(int(actual_dist * SDL_sinf(angle*DEG_TO_RAD) + position.y)) % gif->h;
draw::putpd(screen_column, y, gif->pixels[tx+ty*tex_height], straight_dist); draw::putpd(screen_column, y, gif->pixels[tx+ty*gif->w], straight_dist);
} }
//line(screen_column, 120-(wall_height), screen_column, 120+(wall_height), 5); //line(screen_column, 120-(wall_height), screen_column, 120+(wall_height), 5);
} }
@@ -279,7 +280,10 @@ int main(int argc, char *argv[])
draw::init(); draw::init();
gif = wad::loadFlat("FLOOR4_8"); //draw::loadgif("walls.gif"); //gif = draw::loadgif("walls.gif");
//gif = wad::loadFlat("FLOOR4_8");
//gif = wad::loadTexture("COMPSPAN");
gif = wad::loadPatch("TTALL1_2");
//palette = draw::loadpal("walls.gif"); //palette = draw::loadpal("walls.gif");
palette = wad::loadPalette(0); palette = wad::loadPalette(0);
draw::setpal(palette); draw::setpal(palette);

21
wad.cpp
View File

@@ -124,11 +124,12 @@ namespace wad {
uint16_t *patches = &((uint16_t*)texture_info)[5]; uint16_t *patches = &((uint16_t*)texture_info)[5];
for (int i=0; i<patch_count; ++i) { for (int i=0; i<patch_count; ++i) {
const int originx = patches[i*10+0]; const int originx = patches[i*5+0];
const int originy = patches[i*10+1]; const int originy = patches[i*5+1];
const int patch_index = patches[i*10+2]; const int patch_index = patches[i*5+2];
const char *patch_name = patch_names[patch_index].c_str(); const char *patch_name = patch_names[patch_index].c_str();
draw::surface_t *patch = loadPatch(patch_name); draw::surface_t *patch = loadPatch(patch_name);
draw::drawsurf(originx, originy, patch, surf);
} }
free(textures_lump); free(textures_lump);
@@ -143,8 +144,22 @@ namespace wad {
uint8_t *patch_lump = load(name); uint8_t *patch_lump = load(name);
const int patch_width = ((uint16_t*)patch_lump)[0]; const int patch_width = ((uint16_t*)patch_lump)[0];
const int patch_height = ((uint16_t*)patch_lump)[1]; const int patch_height = ((uint16_t*)patch_lump)[1];
printf("%ix%i\n", patch_width, patch_height);
draw::surface_t *surf = draw::newsurf(patch_width, patch_height); draw::surface_t *surf = draw::newsurf(patch_width, patch_height);
uint32_t *offsets = (uint32_t*)(patch_lump+8);
for (int i=0; i<patch_width; ++i) {
uint8_t *post = patch_lump+offsets[i];
while(true) {
if (post[0] == 0xff) break;
const int offset = post[0];
const int length = post[1];
for (int j=0;j<length;++j) {
draw::putpsurf(surf, i, offset+j, post[3+j]);
}
post = post+4+length;
}
}
//https://www.doomwiki.org/wiki/Picture_format //https://www.doomwiki.org/wiki/Picture_format
free(patch_lump); free(patch_lump);