forked from jaildesigner-jailgames/jaildoctors_dilemma
176 lines
3.5 KiB
C++
176 lines
3.5 KiB
C++
#include "paleta.h"
|
|
#include "gif.c"
|
|
#include <stdio.h>
|
|
|
|
struct jSurface_s
|
|
{
|
|
Uint8 *data;
|
|
Uint16 w, h;
|
|
};
|
|
|
|
static SDL_Texture *jTex = NULL;
|
|
static jSurface jScreen;
|
|
static jSurface jDestSurf;
|
|
static jSurface jSourceSurf = NULL;
|
|
static Uint32 paleta[256];
|
|
static int jWidth = 256;
|
|
static int jHeight = 128;
|
|
static int transparentColor = 255;
|
|
|
|
void pInit(SDL_Renderer *renderer, int w, int h)
|
|
{
|
|
jWidth = w;
|
|
jHeight = h;
|
|
jTex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, w, h);
|
|
jScreen = pNewSurface(w, h);
|
|
jDestSurf = jScreen;
|
|
}
|
|
|
|
jSurface pNewSurface(int w, int h)
|
|
{
|
|
jSurface surf = (jSurface)malloc(sizeof(jSurface_s));
|
|
surf->w = w;
|
|
surf->h = h;
|
|
surf->data = (Uint8 *)malloc(w * h);
|
|
return surf;
|
|
}
|
|
|
|
void pDeleteSurface(jSurface surf)
|
|
{
|
|
if (surf == NULL)
|
|
return;
|
|
if (surf->data != NULL)
|
|
free(surf->data);
|
|
free(surf);
|
|
}
|
|
|
|
void pSetDest(jSurface surf)
|
|
{
|
|
if (surf == NULL)
|
|
jDestSurf = jScreen;
|
|
else
|
|
jDestSurf = surf;
|
|
}
|
|
|
|
void pSetSource(jSurface surf)
|
|
{
|
|
jSourceSurf = surf;
|
|
}
|
|
|
|
void pBlit(int dx, int dy, int sx, int sy, int w, int h)
|
|
{
|
|
if (jSourceSurf == NULL)
|
|
return;
|
|
|
|
for (int iy = 0; iy < h; ++iy)
|
|
{
|
|
for (int ix = 0; ix < w; ++ix)
|
|
pPutPixel(dx + ix, dy + iy, pGetPixel(sx + ix, sy + iy));
|
|
}
|
|
}
|
|
|
|
jSurface pLoadSurface(const char *filename)
|
|
{
|
|
FILE *f = fopen(filename, "rb");
|
|
if (!f)
|
|
return NULL;
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
long size = ftell(f);
|
|
fseek(f, 0, SEEK_SET);
|
|
Uint8 *buffer = (Uint8 *)malloc(size);
|
|
fread(buffer, size, 1, f);
|
|
fclose(f);
|
|
|
|
Uint16 w, h;
|
|
Uint8 *pixels = LoadGif(buffer, &w, &h);
|
|
if (pixels == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
jSurface surf = (jSurface)malloc(sizeof(jSurface_s));
|
|
surf->w = w;
|
|
surf->h = h;
|
|
surf->data = pixels;
|
|
free(buffer);
|
|
return surf;
|
|
}
|
|
|
|
void pLoadPal(const char *filename)
|
|
{
|
|
FILE *f = fopen(filename, "rb");
|
|
if (!f)
|
|
return;
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
long size = ftell(f);
|
|
fseek(f, 0, SEEK_SET);
|
|
Uint8 *buffer = (Uint8 *)malloc(size);
|
|
fread(buffer, size, 1, f);
|
|
fclose(f);
|
|
|
|
Uint32 *pal = LoadPalette(buffer);
|
|
if (pal == NULL)
|
|
{
|
|
return;
|
|
}
|
|
free(buffer);
|
|
for (int i = 0; i < 256; ++i)
|
|
{
|
|
paleta[i] = pal[i];
|
|
}
|
|
}
|
|
|
|
void pSetPal(int index, Uint32 color)
|
|
{
|
|
paleta[index] = color;
|
|
}
|
|
|
|
void pCls(Uint8 color)
|
|
{
|
|
for (int i = 0; i < jDestSurf->w * jDestSurf->h; ++i)
|
|
jDestSurf->data[i] = color;
|
|
}
|
|
|
|
void pFlip(SDL_Renderer *renderer)
|
|
{
|
|
Uint32 *pixels;
|
|
int pitch;
|
|
SDL_LockTexture(jTex, NULL, (void **)&pixels, &pitch);
|
|
for (int i = 0; i < jWidth * jHeight; ++i)
|
|
pixels[i] = paleta[jScreen->data[i]];
|
|
SDL_UnlockTexture(jTex);
|
|
SDL_Rect rect = {0, 64, 256, 128};
|
|
SDL_RenderCopy(renderer, jTex, NULL, &rect);
|
|
}
|
|
|
|
void pPutPixel(int x, int y, Uint8 color)
|
|
{
|
|
if (x < 0 || y < 0 || x >= jDestSurf->w || y >= jDestSurf->h || color == transparentColor)
|
|
return;
|
|
jDestSurf->data[x + y * jDestSurf->w] = color;
|
|
}
|
|
|
|
Uint8 pGetPixel(int x, int y)
|
|
{
|
|
return jSourceSurf->data[x + y * jSourceSurf->w];
|
|
}
|
|
|
|
bool pFadePal()
|
|
{
|
|
// Colores pares
|
|
for (int i = 18; i > 0; i = i - 2)
|
|
{
|
|
paleta[i] = paleta[i - 2];
|
|
}
|
|
|
|
// Colores impares
|
|
for (int i = 17; i > 1; i = i - 2)
|
|
{
|
|
paleta[i] = paleta[i - 2];
|
|
}
|
|
|
|
paleta[1] = paleta[0];
|
|
|
|
return paleta[15] == paleta[0] ? true : false;
|
|
} |