- Ja carrega els nivells i funciona un poc, pero com els vertices no estàn ordenats peta. TBD: ordenar clockwise els vertices de cada sector

This commit is contained in:
2026-02-27 19:02:39 +01:00
parent 8e46fa85b2
commit 943b79d9bb
5 changed files with 255 additions and 77 deletions

51
wad.cpp
View File

@@ -5,6 +5,7 @@
#include <unordered_map>
#include <string>
#include <vector>
#include <SDL3/SDL.h>
namespace wad {
@@ -37,26 +38,28 @@ namespace wad {
fread(&directory[i].filepos, 4, 1, f);
fread(&directory[i].size, 4, 1, f);
fread(directory[i].name, 8, 1, f);
if (strncmp("TEXTURE1", directory[i].name, 8)==0)
printf("num lumps: %8.10s % 6i % 6i\n", directory[i].name,directory[i].filepos, directory[i].size);
//if (strncmp("TEXTURE1", directory[i].name, 8)==0)
// printf("num lumps: %8.10s % 6i % 6i\n", directory[i].name,directory[i].filepos, directory[i].size);
}
fclose(f);
uint8_t *pnames = load("PNAMES");
int num_pnames = *(uint32_t*)pnames;
//printf("num pnames: %i\n", num_pnames);
//patch_names = (char (*)[8]) malloc(num_pnames * sizeof *patch_names);
char *names = (char*)(pnames+4);
for (int i=0;i<num_pnames;++i) {
char name[9]; name[8]=0;
for (int j=0;j<8;++j) name[j] = names[8*i+j];
for (int j=0;j<8;++j) name[j] = toupper(names[8*i+j]);
patch_names.push_back(name);
}
free(pnames);
for (int i=0;i<num_pnames;++i) printf("%s\n", patch_names[i].c_str());
//for (int i=0;i<num_pnames;++i) printf("%s\n", patch_names[i].c_str());
}
uint8_t *load(const char *name) {
int i=-1;
for (i=0; i<numlumps; ++i) if (strncmp(name, directory[i].name,8)==0) break;
if (i==-1) return nullptr;
@@ -70,6 +73,24 @@ namespace wad {
return buffer;
}
uint8_t *load(const char *prev, const char *name, int *size)
{
int i=-1;
for (i=0; i<numlumps; ++i) if (strncmp(prev, directory[i].name,8)==0) break;
if (i==-1) return nullptr;
for (; i<numlumps; ++i) if (strncmp(name, directory[i].name,8)==0) break;
if (i==-1) return nullptr;
FILE *f = fopen(file, "rb");
fseek(f, directory[i].filepos, SEEK_SET);
uint8_t *buffer = (uint8_t*)malloc(directory[i].size);
fread(buffer, directory[i].size, 1, f);
fclose(f);
if (size) *size = directory[i].size;
return buffer;
}
uint32_t *loadPalette(int index)
{
uint8_t *palettes = wad::load("PLAYPAL");
@@ -113,7 +134,8 @@ namespace wad {
if (strncmp(name, texture_name, 8)==0) break;
i++;
}
if (i==num_textures_in_lump) return nullptr;
SDL_assert(i<num_textures_in_lump);
//if (i==num_textures_in_lump) { printf("TEXTURA '%s' NO TROBADA!\n", name); return nullptr; }
uint8_t *texture_info = textures_lump+offsets[i]+12;
const int texture_width = ((uint16_t*)texture_info)[0];
@@ -121,13 +143,15 @@ namespace wad {
draw::surface_t *surf = draw::newsurf(texture_width, texture_height);
const int patch_count = ((uint16_t*)texture_info)[4];
//printf("Texture: %s (%ix%i)\n Patch count: %i\n", name, texture_width, texture_height, patch_count);
uint16_t *patches = &((uint16_t*)texture_info)[5];
for (int i=0; i<patch_count; ++i) {
const int originx = patches[i*5+0];
const int originy = patches[i*5+1];
const int originx = int16_t(patches[i*5+0]);
const int originy = int16_t(patches[i*5+1]);
const int patch_index = patches[i*5+2];
const char *patch_name = patch_names[patch_index].c_str();
//printf("Patch. Index:%i, Name:%s, ox:%i, oy:%i\n", patch_index, patch_name, originx, originy);
draw::surface_t *patch = loadPatch(patch_name);
draw::drawsurf(originx, originy, patch, surf);
}
@@ -144,20 +168,21 @@ namespace wad {
uint8_t *patch_lump = load(name);
const int patch_width = ((uint16_t*)patch_lump)[0];
const int patch_height = ((uint16_t*)patch_lump)[1];
printf("%ix%i\n", patch_width, patch_height);
//printf("Patch:%s, %ix%i\n", name, 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];
if (*post == 0xff) break;
const int offset = *(post++);
const int length = *(post++);
post++;
for (int j=0;j<length;++j) {
draw::putpsurf(surf, i, offset+j, post[3+j]);
draw::putpsurf(surf, i, offset+j, *(post++));
}
post = post+4+length;
post++;
}
}
//https://www.doomwiki.org/wiki/Picture_format