afegit surface.h
This commit is contained in:
165
source/common/surface.cpp
Normal file
165
source/common/surface.cpp
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
#include "jUnit.h"
|
||||||
|
#include "gif.c"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
struct jSurface_s
|
||||||
|
{
|
||||||
|
Uint8 *data;
|
||||||
|
Uint16 w, h;
|
||||||
|
};
|
||||||
|
|
||||||
|
static SDL_Window *jWin = NULL;
|
||||||
|
static SDL_Renderer *jRen = NULL;
|
||||||
|
static SDL_Texture *jTex = NULL;
|
||||||
|
static jSurface jScreen;
|
||||||
|
static jSurface jDestSurf;
|
||||||
|
static jSurface jSourceSurf = NULL;
|
||||||
|
static Uint32 paleta[256];
|
||||||
|
static int jWidth = 320;
|
||||||
|
static int jHeight = 240;
|
||||||
|
static int jZoom = 2;
|
||||||
|
static int transparentColor = 0;
|
||||||
|
|
||||||
|
jSurface jNewSurface(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 jDeleteSurface(jSurface surf)
|
||||||
|
{
|
||||||
|
if (surf == NULL)
|
||||||
|
return;
|
||||||
|
if (surf->data != NULL)
|
||||||
|
free(surf->data);
|
||||||
|
free(surf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jSetDest(jSurface surf)
|
||||||
|
{
|
||||||
|
if (surf == NULL)
|
||||||
|
jDestSurf = jScreen;
|
||||||
|
else
|
||||||
|
jDestSurf = surf;
|
||||||
|
}
|
||||||
|
|
||||||
|
void jSetSource(jSurface surf)
|
||||||
|
{
|
||||||
|
jSourceSurf = surf;
|
||||||
|
}
|
||||||
|
|
||||||
|
void jBlit(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)
|
||||||
|
jPutPixel(dx + ix, dy + iy, jGetPixel(sx + ix, sy + iy));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
jSurface jLoadSurface(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 jLoadPal(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 jInit(const char *titol, int w, int h, int z)
|
||||||
|
{
|
||||||
|
SDL_Init(SDL_INIT_EVERYTHING);
|
||||||
|
jWidth = w;
|
||||||
|
jHeight = h;
|
||||||
|
jZoom = z;
|
||||||
|
jWin = SDL_CreateWindow(titol, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w * z, h * z, SDL_WINDOW_SHOWN);
|
||||||
|
jRen = SDL_CreateRenderer(jWin, -1, 0);
|
||||||
|
SDL_RenderSetLogicalSize(jRen, w, h);
|
||||||
|
jTex = SDL_CreateTexture(jRen, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, w, h);
|
||||||
|
jScreen = jNewSurface(w, h);
|
||||||
|
jDestSurf = jScreen;
|
||||||
|
}
|
||||||
|
|
||||||
|
void jSetPal(int index, Uint32 color)
|
||||||
|
{
|
||||||
|
paleta[index] = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
void jCls(Uint8 color)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < jDestSurf->w * jDestSurf->h; ++i)
|
||||||
|
jDestSurf->data[i] = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
void jFlip()
|
||||||
|
{
|
||||||
|
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_RenderCopy(jRen, jTex, NULL, NULL);
|
||||||
|
SDL_RenderPresent(jRen);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jPutPixel(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 jGetPixel(int x, int y)
|
||||||
|
{
|
||||||
|
return jSourceSurf->data[x + y * jSourceSurf->w];
|
||||||
|
}
|
||||||
28
source/common/surface.h
Normal file
28
source/common/surface.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
typedef struct jSurface_s *jSurface;
|
||||||
|
|
||||||
|
void init();
|
||||||
|
void update();
|
||||||
|
|
||||||
|
jSurface jNewSurface(int w, int h);
|
||||||
|
void jDeleteSurface(jSurface surf);
|
||||||
|
void jSetDest(jSurface surf);
|
||||||
|
void jSetSource(jSurface surf);
|
||||||
|
|
||||||
|
jSurface jLoadSurface(const char* filename);
|
||||||
|
|
||||||
|
void jPutPixel(int x, int y, Uint8 color);
|
||||||
|
Uint8 jGetPixel(int x, int y);
|
||||||
|
|
||||||
|
void jBlit(int dx, int dy, int sx, int sy, int w, int h);
|
||||||
|
|
||||||
|
void jInit(const char *titol, int w, int h, int z);
|
||||||
|
|
||||||
|
void jSetPal(int index, Uint32 color);
|
||||||
|
void jLoadPal(const char *filename);
|
||||||
|
|
||||||
|
void jCls(Uint8 color);
|
||||||
|
|
||||||
|
void jFlip();
|
||||||
@@ -363,11 +363,11 @@ bool Director::setFileList()
|
|||||||
asset->add(prefix + "/data/gfx/title_dust.png", t_bitmap);
|
asset->add(prefix + "/data/gfx/title_dust.png", t_bitmap);
|
||||||
asset->add(prefix + "/data/gfx/title_dust.ani", t_data);
|
asset->add(prefix + "/data/gfx/title_dust.ani", t_data);
|
||||||
|
|
||||||
asset->add(prefix + "/data/gfx/player1.png", t_bitmap);
|
asset->add(prefix + "/data/gfx/player1.gif", t_bitmap);
|
||||||
asset->add(prefix + "/data/gfx/player2.png", t_bitmap);
|
asset->add(prefix + "/data/gfx/player2.gif", t_bitmap);
|
||||||
asset->add(prefix + "/data/gfx/player.ani", t_data);
|
asset->add(prefix + "/data/gfx/player.ani", t_data);
|
||||||
asset->add(prefix + "/data/gfx/player1_power.png", t_bitmap);
|
asset->add(prefix + "/data/gfx/player1_power.gif", t_bitmap);
|
||||||
asset->add(prefix + "/data/gfx/player2_power.png", t_bitmap);
|
asset->add(prefix + "/data/gfx/player2_power.gif", t_bitmap);
|
||||||
asset->add(prefix + "/data/gfx/player_power.ani", t_data);
|
asset->add(prefix + "/data/gfx/player_power.ani", t_data);
|
||||||
|
|
||||||
// Fuentes de texto
|
// Fuentes de texto
|
||||||
|
|||||||
Reference in New Issue
Block a user