Files
wolf/draw.cpp
Raimon Zamora 8dbcd4fcb7 - Reestructuració masiva del codi
- afegit el WAD del Doom 1
- Ja llegim FLATS i TEXTURES del WAD, falten els PATCH
2026-02-26 17:15:52 +01:00

159 lines
4.0 KiB
C++

#include "draw.h"
#include <SDL3/SDL.h>
#include "gif.h"
#include "jdebug.h"
#include <stdio.h>
#include <stdlib.h>
// Funcions de pintat
namespace draw
{
SDL_Window *sdl_window;
SDL_Renderer *sdl_renderer;
SDL_Texture *sdl_texture;
Uint32 *palette;
Uint8 screen[320*240];
float depth_buffer[320*240];
void init()
{
SDL_Init(SDL_INIT_VIDEO);
sdl_window = SDL_CreateWindow("WOLF", 640, 480, 0);
SDL_SetWindowPosition(sdl_window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
sdl_renderer = SDL_CreateRenderer(sdl_window, NULL);
sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240);
SDL_SetTextureScaleMode(sdl_texture, SDL_SCALEMODE_NEAREST);
debug::init(sdl_renderer);
}
surface_t *newsurf(int w, int h)
{
surface_t *surf = (surface_t*)malloc(sizeof(surface_t));
surf->w = w;
surf->h = h;
surf->pixels = (uint8_t*)malloc(w*h);
return surf;
}
surface_t *loadgif(const char* filename)
{
FILE *f = fopen(filename, "rb");
fseek(f, 0, SEEK_END);
int filesize = ftell(f);
fseek(f, 0, SEEK_SET);
Uint8 *buffer = (Uint8*)malloc(filesize);
fread(buffer, filesize, 1, f);
fclose(f);
surface_t *surf = (surface_t*)malloc(sizeof(surface_t));
surf->pixels = LoadGif(buffer, &surf->w, &surf->h);
free(buffer);
return surf;
}
Uint32 *loadpal(const char* filename)
{
FILE *f = fopen(filename, "rb");
fseek(f, 0, SEEK_END);
int filesize = ftell(f);
fseek(f, 0, SEEK_SET);
Uint8 *buffer = (Uint8*)malloc(filesize);
fread(buffer, filesize, 1, f);
fclose(f);
Uint32 *pal = LoadPalette(buffer);
free(buffer);
return pal;
}
void setpal(Uint32* pal)
{
palette = pal;
}
void putp(int x, int y, Uint8 color)
{
if (x<0 || y<0 || x>=320 || y>=240) return;
screen[x+y*320]=color;
}
void putpd(int x, int y, Uint8 color, float depth)
{
if (x<0 || y<0 || x>=320 || y>=240) return;
screen[x+y*320]=color;
depth_buffer[x+y*320]=depth;
}
void putps(int x, int y, Uint8 color, float depth)
{
if (x<0 || y<0 || x>=320 || y>=240 || color==0) return;
if (depth_buffer[x+y*320]>=depth)
{
screen[x+y*320]=color;
depth_buffer[x+y*320]=depth;
}
}
Uint8 getp(surface_t *surf, int x, int y)
{
return surf->pixels[x+y*surf->w];
}
void line(int x1, int y1, int x2, int y2, Uint8 color)
{
float dx = float(x2-x1);
float dy = float(y2-y1);
float steps = SDL_max(SDL_fabsf(dx), SDL_fabsf(dy));
if (steps==0) { putp(x1,y1,color); return; }
float incx = dx / steps;
float incy = dy / steps;
float x = (float)x1;
float y = (float)y1;
for (int i=0; i<=int(steps); ++i)
{
putp(int(x+0.5f),int(y+0.5f),color);
x += incx;
y += incy;
}
}
void render()
{
// Send to texture and render
Uint32 *pixels;
int pitch;
SDL_LockTexture(sdl_texture, NULL, (void**)&pixels, &pitch);
for (int i=0; i<(320*240); ++i)
{
pixels[i] = 0xFF000000 | palette[screen[i]];
// [DEBUG] Visualització del depth buffer
//pixels[i] = 0xFF000000 | palette[SDL_min(int(depth_buffer[i]),255)];
}
SDL_UnlockTexture(sdl_texture);
SDL_RenderTexture(sdl_renderer, sdl_texture, NULL, NULL);
}
void flip()
{
SDL_RenderPresent(sdl_renderer);
}
namespace map
{
void putp(int x, int y, Uint8 color)
{
draw::putp(160+(x/8), 120+(y/8), color);
}
void line(int x1, int y1, int x2, int y2, Uint8 color)
{
draw::line(160+(x1/8), 120+(y1/8), 160+(x2/8), 120+(y2/8), color);
}
}
}