Agrego prueba de pingüino y movimiento.

master
Félix Arreola Rodríguez 2018-05-25 10:34:11 -05:00
parent e81cb8338f
commit 331fe2424f
42 changed files with 1071 additions and 16 deletions

View File

@ -1,6 +1,42 @@
gamedatadir = $(pkgdatadir)/data
nobase_dist_gamedata_DATA =
nobase_dist_gamedata_DATA = images/background.png \
images/penguin_1_back.png \
images/penguin_1_color.png \
images/penguin_1_front.png \
images/penguin_2_back.png \
images/penguin_2_color.png \
images/penguin_2_front.png \
images/penguin_3_back.png \
images/penguin_3_color.png \
images/penguin_3_front.png \
images/penguin_4_back.png \
images/penguin_4_color.png \
images/penguin_4_front.png \
images/penguin_5_1_front.png \
images/penguin_5_2_front.png \
images/penguin_5_3_front.png \
images/penguin_5_back.png \
images/penguin_5_color.png \
images/penguin_6_1_back.png \
images/penguin_6_1_color.png \
images/penguin_6_1_front.png \
images/penguin_6_2_back.png \
images/penguin_6_2_color.png \
images/penguin_6_2_front.png \
images/penguin_6_3_front.png \
images/penguin_6_4_front.png \
images/penguin_6_5_front.png \
images/penguin_6_6_front.png \
images/penguin_7_back.png \
images/penguin_7_color.png \
images/penguin_7_front.png \
images/penguin_8_1_front.png \
images/penguin_8_2_front.png \
images/penguin_8_3_front.png \
images/penguin_8_back.png \
images/penguin_8_color.png \
images/plataform.png
# Instalar los archivos .desktop e iconos
applicationsdir = $(datadir)/applications

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 883 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

View File

@ -3,7 +3,9 @@
gamedatadir = $(pkgdatadir)/data
bin_PROGRAMS = bean-counters-classic
bean_counters_classic_SOURCES = beans.c path.c path.h
bean_counters_classic_SOURCES = beans.c \
gfx_blit_func.c gfx_blit_func.h \
path.c path.h
if MACOSX
bean_counters_classic_SOURCES += SDLMain.m SDLMain.h

View File

@ -38,18 +38,35 @@
#include "path.h"
#include "gfx_blit_func.h"
#define FPS (1000/24)
#define RANDOM(x) ((int) (x ## .0 * rand () / (RAND_MAX + 1.0)))
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define RMASK 0xff000000
#define GMASK 0x00ff0000
#define BMASK 0x0000ff00
#define AMASK 0x000000ff
#else
#define RMASK 0x000000ff
#define GMASK 0x0000ff00
#define BMASK 0x00ff0000
#define AMASK 0xff000000
#endif
/* Enumerar las imágenes */
enum {
IMG_NONE,
IMG_BACKGROUND,
IMG_PLATAFORM,
NUM_IMAGES
};
/* Los nombres de archivos */
const char *images_names[NUM_IMAGES] = {
"images/none.png",
"images/background.png",
"images/plataform.png"
};
enum {
@ -69,18 +86,160 @@ enum {
GAME_QUIT
};
/* Imágenes de los pingüinos */
enum {
IMG_PENGUIN_1_BACK,
IMG_PENGUIN_1_COLOR,
IMG_PENGUIN_1_FRONT,
IMG_PENGUIN_2_BACK,
IMG_PENGUIN_2_COLOR,
IMG_PENGUIN_2_FRONT,
IMG_PENGUIN_3_BACK,
IMG_PENGUIN_3_COLOR,
IMG_PENGUIN_3_FRONT,
IMG_PENGUIN_4_BACK,
IMG_PENGUIN_4_COLOR,
IMG_PENGUIN_4_FRONT,
IMG_PENGUIN_5_BACK,
IMG_PENGUIN_5_COLOR,
IMG_PENGUIN_5_1_FRONT,
IMG_PENGUIN_5_2_FRONT,
IMG_PENGUIN_5_3_FRONT,
IMG_PENGUIN_6_1_BACK,
IMG_PENGUIN_6_2_BACK,
IMG_PENGUIN_6_1_COLOR,
IMG_PENGUIN_6_2_COLOR,
IMG_PENGUIN_6_1_FRONT,
IMG_PENGUIN_6_2_FRONT,
IMG_PENGUIN_6_3_FRONT,
IMG_PENGUIN_6_4_FRONT,
IMG_PENGUIN_6_5_FRONT,
IMG_PENGUIN_6_6_FRONT,
IMG_PENGUIN_7_BACK,
IMG_PENGUIN_7_COLOR,
IMG_PENGUIN_7_FRONT,
IMG_PENGUIN_8_BACK,
IMG_PENGUIN_8_COLOR,
IMG_PENGUIN_8_1_FRONT,
IMG_PENGUIN_8_2_FRONT,
IMG_PENGUIN_8_3_FRONT,
NUM_PENGUIN_IMGS
};
const char *penguin_images_names[NUM_PENGUIN_IMGS] = {
"images/penguin_1_back.png",
"images/penguin_1_color.png",
"images/penguin_1_front.png",
"images/penguin_2_back.png",
"images/penguin_2_color.png",
"images/penguin_2_front.png",
"images/penguin_3_back.png",
"images/penguin_3_color.png",
"images/penguin_3_front.png",
"images/penguin_4_back.png",
"images/penguin_4_color.png",
"images/penguin_4_front.png",
"images/penguin_5_back.png",
"images/penguin_5_color.png",
"images/penguin_5_1_front.png",
"images/penguin_5_2_front.png",
"images/penguin_5_3_front.png",
"images/penguin_6_1_back.png",
"images/penguin_6_2_back.png",
"images/penguin_6_1_color.png",
"images/penguin_6_2_color.png",
"images/penguin_6_1_front.png",
"images/penguin_6_2_front.png",
"images/penguin_6_3_front.png",
"images/penguin_6_4_front.png",
"images/penguin_6_5_front.png",
"images/penguin_6_6_front.png",
"images/penguin_7_back.png",
"images/penguin_7_color.png",
"images/penguin_7_front.png",
"images/penguin_8_back.png",
"images/penguin_8_color.png",
"images/penguin_8_1_front.png",
"images/penguin_8_2_front.png",
"images/penguin_8_3_front.png"
};
enum {
PENGUIN_FRAME_1,
PENGUIN_FRAME_2,
PENGUIN_FRAME_3,
PENGUIN_FRAME_4,
PENGUIN_FRAME_5_1,
PENGUIN_FRAME_5_2,
PENGUIN_FRAME_5_3,
PENGUIN_FRAME_6_1,
PENGUIN_FRAME_6_2,
PENGUIN_FRAME_6_3,
PENGUIN_FRAME_6_4,
PENGUIN_FRAME_6_5,
PENGUIN_FRAME_6_6,
PENGUIN_FRAME_7,
PENGUIN_FRAME_8,
PENGUIN_FRAME_9,
PENGUIN_FRAME_10,
NUM_PENGUIN_FRAMES
};
const SDL_Color penguin_colors[18] = {
{0, 51, 102},
{51, 51, 51},
{206, 0, 0},
{255, 204, 0},
{0, 153, 0},
{153, 102, 0},
{255, 49, 156},
{99, 0, 156},
{0, 156, 204},
{255, 102, 0},
{0, 102, 0},
{255, 99, 99},
{139, 227, 3},
{28, 150, 163},
{240, 240, 216},
{174, 159, 200},
{128, 33, 75},
{46, 71, 170}
};
/* Prototipos de función */
int game_intro (void);
int game_loop (void);
int game_finish (void);
void setup (void);
SDL_Surface * set_video_mode(unsigned flags);
void setup_and_color_penguin (void);
/* Variables globales */
SDL_Surface * screen;
SDL_Surface * images[NUM_IMAGES];
SDL_Surface * penguin_images[NUM_PENGUIN_FRAMES];
int use_sound;
int color_penguin = 0;
Mix_Chunk * sounds[NUM_SOUNDS];
Mix_Music * mus_carnie;
@ -97,15 +256,16 @@ int main (int argc, char *argv[]) {
setup ();
bind_textdomain_codeset (PACKAGE, "UTF-8");
do {
if (game_intro () == GAME_QUIT) break;
//if (game_intro () == GAME_QUIT) break;
if (game_loop () == GAME_QUIT) break;
if (game_finish () == GAME_QUIT) break;
//if (game_finish () == GAME_QUIT) break;
} while (1 == 0);
SDL_Quit ();
return EXIT_SUCCESS;
}
#if 0
int game_intro (void) {
int done = 0;
SDL_Event event;
@ -206,6 +366,7 @@ int game_finish (void) {
return done;
}
#endif
int game_loop (void) {
int done = 0;
@ -213,10 +374,17 @@ int game_loop (void) {
SDLKey key;
Uint32 last_time, now_time;
SDL_Rect rect;
int penguinx, handposx;
int bags = 0;
int penguin_frame = 0;
int i;
/* Predibujar todo */
SDL_FillRect (screen, NULL, 0);
SDL_Flip (screen);
/*SDL_FillRect (screen, NULL, 0);
SDL_Flip (screen);*/
SDL_EventState (SDL_MOUSEMOTION, SDL_IGNORE);
do {
last_time = SDL_GetTicks ();
@ -243,10 +411,70 @@ int game_loop (void) {
if (key == SDLK_ESCAPE) {
done = GAME_QUIT;
}
if (key == SDLK_a) {
if (bags < 9) {
bags++;
penguin_frame = 0;
}
} else if (key == SDLK_s) {
if (bags > 0) {
bags--;
penguin_frame = 0;
}
}
break;
}
}
SDL_GetMouseState (&handposx, NULL);
penguinx = handposx;
if (penguinx < 190) {
penguinx = 190;
} else if (penguinx > 555) {
penguinx = 555;
}
SDL_BlitSurface (images[IMG_BACKGROUND], NULL, screen, NULL);
if (bags >= 0 && bags < 4) {
i = PENGUIN_FRAME_1 + bags;
} else if (bags == 4) {
i = PENGUIN_FRAME_5_1 + (penguin_frame / 2);
penguin_frame++;
if (penguin_frame >= 6) {
penguin_frame = 0;
}
} else if (bags == 5) {
i = PENGUIN_FRAME_6_1 + (penguin_frame / 2);
penguin_frame++;
if (penguin_frame >= 12) {
penguin_frame = 0;
}
} else if (bags == 6) {
i = PENGUIN_FRAME_7;
} else if (bags > 6) {
i = PENGUIN_FRAME_8 + (bags - 7);
}
/* Dibujar al pinguino */
rect.x = penguinx - 120;
rect.y = 251;
rect.w = penguin_images[i]->w;
rect.h = penguin_images[i]->h;
SDL_BlitSurface (penguin_images[i], NULL, screen, &rect);
/* Dibujar la plataforma */
rect.x = 0;
rect.y = 355;
rect.w = images[IMG_PLATAFORM]->w;
rect.h = images[IMG_PLATAFORM]->h;
SDL_BlitSurface (images[IMG_PLATAFORM], NULL, screen, &rect);
SDL_Flip (screen);
now_time = SDL_GetTicks ();
@ -269,9 +497,6 @@ SDL_Surface * set_video_mode (unsigned flags) {
void setup (void) {
SDL_Surface * image;
TTF_Font *ttf10, *ttf14, *ttf16, *ttf26, *temp_font;
SDL_Color color;
SDL_Rect rect, rect2;
int g;
char buffer_file[8192];
char *systemdata_path = get_systemdata_path ();
@ -338,8 +563,16 @@ void setup (void) {
/* TODO: Mostrar la carga de porcentaje */
}
/* Generador de números aleatorios */
srand ((unsigned int) getpid ());
/* Colorear y organizar las imágenes de pingüinos */
color_penguin = RANDOM (18);
setup_and_color_penguin ();
if (use_sound) {
for (g = 0; g < NUM_SOUNDS; g++) {
/*for (g = 0; g < NUM_SOUNDS; g++) {
sprintf (buffer_file, "%s%s", systemdata_path, sound_names[g]);
sounds[g] = Mix_LoadWAV (buffer_file);
@ -353,7 +586,7 @@ void setup (void) {
exit (1);
}
Mix_VolumeChunk (sounds[g], MIX_MAX_VOLUME / 2);
}
}*/
/* Cargar la música */
//sprintf (buffer_file, "%s%s", systemdata_path, MUS_CARNIE);
@ -380,8 +613,154 @@ void setup (void) {
// TODO: Favor de manejar correctamente el bind_textdomain_codeset
//bind_textdomain_codeset (PACKAGE, "UTF-8");
/* Generador de números aleatorios */
srand (SDL_GetTicks ());
}
void setup_and_color_penguin (void) {
int g;
SDL_Surface * image, *color_surface;
SDL_Surface *temp_penguins[NUM_PENGUIN_IMGS];
char buffer_file[8192];
char *systemdata_path = get_systemdata_path ();
for (g = 0; g < NUM_PENGUIN_IMGS; g++) {
sprintf (buffer_file, "%s%s", systemdata_path, penguin_images_names[g]);
image = IMG_Load (buffer_file);
if (image == NULL) {
fprintf (stderr,
_("Failed to load data file:\n"
"%s\n"
"The error returned by SDL is:\n"
"%s\n"), buffer_file, SDL_GetError());
SDL_Quit ();
exit (1);
}
temp_penguins[g] = image;
/* TODO: Mostrar la carga de porcentaje */
}
color_surface = SDL_CreateRGBSurface (SDL_SWSURFACE, 196, 199, 32, RMASK, GMASK, BMASK, AMASK);
SDL_FillRect (color_surface, NULL, SDL_MapRGB (color_surface->format, penguin_colors[color_penguin].r, penguin_colors[color_penguin].g, penguin_colors[color_penguin].b));
for (g = 0; g < 4; g++) {
penguin_images[PENGUIN_FRAME_1 + g] = temp_penguins[IMG_PENGUIN_1_BACK + (g * 3)];
/* Colorear el pingüino */
SDL_BlitSurface (color_surface, NULL, temp_penguins[IMG_PENGUIN_1_COLOR + (g * 3)], NULL);
/* Copiar el color sobre el fondo */
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_1_COLOR + (g * 3)], NULL, penguin_images[PENGUIN_FRAME_1 + g], NULL);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_1_COLOR + (g * 3)]);
temp_penguins[IMG_PENGUIN_1_COLOR + (g * 3)] = NULL;
/* Copiar el frente */
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_1_FRONT + (g * 3)], NULL, penguin_images[PENGUIN_FRAME_1 + g], NULL);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_1_FRONT + (g * 3)]);
temp_penguins[IMG_PENGUIN_1_FRONT + (g * 3)] = NULL;
}
/* Duplicar el fondo del frame 5 */
penguin_images[PENGUIN_FRAME_5_1] = temp_penguins[IMG_PENGUIN_5_BACK];
penguin_images[PENGUIN_FRAME_5_2] = SDL_CreateRGBSurface (SDL_SWSURFACE, 196, 199, 32, RMASK, GMASK, BMASK, AMASK);
penguin_images[PENGUIN_FRAME_5_3] = SDL_CreateRGBSurface (SDL_SWSURFACE, 196, 199, 32, RMASK, GMASK, BMASK, AMASK);
SDL_SetAlpha (penguin_images[PENGUIN_FRAME_5_1], 0, 0);
SDL_BlitSurface (penguin_images[PENGUIN_FRAME_5_1], NULL, penguin_images[PENGUIN_FRAME_5_2], NULL);
SDL_BlitSurface (penguin_images[PENGUIN_FRAME_5_1], NULL, penguin_images[PENGUIN_FRAME_5_3], NULL);
SDL_SetAlpha (penguin_images[PENGUIN_FRAME_5_1], SDL_SRCALPHA, SDL_ALPHA_OPAQUE);
/* Colorear el pingüino */
SDL_BlitSurface (color_surface, NULL, temp_penguins[IMG_PENGUIN_5_COLOR], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_5_COLOR], NULL, penguin_images[PENGUIN_FRAME_5_1], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_5_COLOR], NULL, penguin_images[PENGUIN_FRAME_5_2], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_5_COLOR], NULL, penguin_images[PENGUIN_FRAME_5_3], NULL);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_5_COLOR]);
temp_penguins[IMG_PENGUIN_5_COLOR] = NULL;
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_5_1_FRONT], NULL, penguin_images[PENGUIN_FRAME_5_1], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_5_2_FRONT], NULL, penguin_images[PENGUIN_FRAME_5_2], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_5_3_FRONT], NULL, penguin_images[PENGUIN_FRAME_5_3], NULL);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_5_1_FRONT]);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_5_2_FRONT]);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_5_3_FRONT]);
temp_penguins[IMG_PENGUIN_5_1_FRONT] = temp_penguins[IMG_PENGUIN_5_2_FRONT] = temp_penguins[IMG_PENGUIN_5_3_FRONT] = NULL;
/* Vamos por el frame 6 */
SDL_SetAlpha (temp_penguins[IMG_PENGUIN_6_1_BACK], 0, 0);
SDL_SetAlpha (temp_penguins[IMG_PENGUIN_6_2_BACK], 0, 0);
SDL_BlitSurface (color_surface, NULL, temp_penguins[IMG_PENGUIN_6_1_COLOR], NULL);
SDL_BlitSurface (color_surface, NULL, temp_penguins[IMG_PENGUIN_6_2_COLOR], NULL);
/* 6 frames de animación del frame 6 */
for (g = 0; g < 6; g++) {
penguin_images[PENGUIN_FRAME_6_1 + g] = SDL_CreateRGBSurface (SDL_SWSURFACE, 196, 199, 32, RMASK, GMASK, BMASK, AMASK);
/* Clonar el fondo */
SDL_BlitSurface (temp_penguins[IMG_PENGUIN_6_1_BACK + (g % 2)], NULL, penguin_images[PENGUIN_FRAME_6_1 + g], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_6_1_COLOR + (g % 2)], NULL, penguin_images[PENGUIN_FRAME_6_1 + g], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_6_1_FRONT + g], NULL, penguin_images[PENGUIN_FRAME_6_1 + g], NULL);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_6_1_FRONT + g]);
temp_penguins[IMG_PENGUIN_6_1_FRONT + g] = NULL;
}
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_6_1_BACK]);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_6_2_BACK]);
temp_penguins[IMG_PENGUIN_6_1_BACK] = temp_penguins[IMG_PENGUIN_6_2_BACK] = NULL;
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_6_1_COLOR]);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_6_2_COLOR]);
temp_penguins[IMG_PENGUIN_6_1_COLOR] = temp_penguins[IMG_PENGUIN_6_2_COLOR] = NULL;
/* Armar el frame 7 */
penguin_images[PENGUIN_FRAME_7] = temp_penguins[IMG_PENGUIN_7_BACK];
/* Colorear el pingüino */
SDL_BlitSurface (color_surface, NULL, temp_penguins[IMG_PENGUIN_7_COLOR], NULL);
/* Copiar el color sobre el fondo */
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_7_COLOR], NULL, penguin_images[PENGUIN_FRAME_7], NULL);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_7_COLOR]);
temp_penguins[IMG_PENGUIN_7_COLOR] = NULL;
/* Copiar el frente */
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_7_FRONT], NULL, penguin_images[PENGUIN_FRAME_7], NULL);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_7_FRONT]);
temp_penguins[IMG_PENGUIN_7_FRONT] = NULL;
/* Generar los otros 3 estados */
penguin_images[PENGUIN_FRAME_8] = temp_penguins[IMG_PENGUIN_8_BACK];
penguin_images[PENGUIN_FRAME_9] = SDL_CreateRGBSurface (SDL_SWSURFACE, 196, 199, 32, RMASK, GMASK, BMASK, AMASK);
penguin_images[PENGUIN_FRAME_10] = SDL_CreateRGBSurface (SDL_SWSURFACE, 196, 199, 32, RMASK, GMASK, BMASK, AMASK);
SDL_SetAlpha (penguin_images[PENGUIN_FRAME_8], 0, 0);
SDL_BlitSurface (penguin_images[PENGUIN_FRAME_8], NULL, penguin_images[PENGUIN_FRAME_9], NULL);
SDL_BlitSurface (penguin_images[PENGUIN_FRAME_8], NULL, penguin_images[PENGUIN_FRAME_10], NULL);
SDL_SetAlpha (penguin_images[PENGUIN_FRAME_8], SDL_SRCALPHA, SDL_ALPHA_OPAQUE);
/* Colorear el pingüino */
SDL_BlitSurface (color_surface, NULL, temp_penguins[IMG_PENGUIN_8_COLOR], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_8_COLOR], NULL, penguin_images[PENGUIN_FRAME_8], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_8_COLOR], NULL, penguin_images[PENGUIN_FRAME_9], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_8_COLOR], NULL, penguin_images[PENGUIN_FRAME_10], NULL);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_8_COLOR]);
temp_penguins[IMG_PENGUIN_8_COLOR] = NULL;
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_8_1_FRONT], NULL, penguin_images[PENGUIN_FRAME_8], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_8_2_FRONT], NULL, penguin_images[PENGUIN_FRAME_9], NULL);
SDL_gfxBlitRGBA (temp_penguins[IMG_PENGUIN_8_3_FRONT], NULL, penguin_images[PENGUIN_FRAME_10], NULL);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_8_1_FRONT]);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_8_2_FRONT]);
SDL_FreeSurface (temp_penguins[IMG_PENGUIN_8_3_FRONT]);
temp_penguins[IMG_PENGUIN_8_1_FRONT] = temp_penguins[IMG_PENGUIN_8_2_FRONT] = temp_penguins[IMG_PENGUIN_8_3_FRONT] = NULL;
}

609
src/gfx_blit_func.c 100644
View File

@ -0,0 +1,609 @@
/*
SDL_gfxBlitFunc.c: custom blitters
Copyright (C) 2001-2012 Andreas Schiffler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Andreas Schiffler -- aschiffler at ferzkopp dot net
*/
#include <SDL.h>
#include <SDL_video.h>
/*!
\brief Unwrap RGBA values from a pixel using mask, shift and loss for surface.
*/
#define GFX_RGBA_FROM_PIXEL(pixel, fmt, r, g, b, a) \
{ \
r = ((pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss; \
g = ((pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss; \
b = ((pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss; \
a = ((pixel&fmt->Amask)>>fmt->Ashift)<<fmt->Aloss; \
}
/*!
\brief Disassemble buffer pointer into a pixel and separate RGBA values.
*/
#define GFX_DISASSEMBLE_RGBA(buf, bpp, fmt, pixel, r, g, b, a) \
do { \
pixel = *((Uint32 *)(buf)); \
GFX_RGBA_FROM_PIXEL(pixel, fmt, r, g, b, a); \
pixel &= ~fmt->Amask; \
} while(0)
/*!
\brief Wrap a pixel from RGBA values using mask, shift and loss for surface.
*/
#define GFX_PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a) \
{ \
pixel = ((r>>fmt->Rloss)<<fmt->Rshift)| \
((g>>fmt->Gloss)<<fmt->Gshift)| \
((b>>fmt->Bloss)<<fmt->Bshift)| \
((a<<fmt->Aloss)<<fmt->Ashift); \
}
/*!
\brief Assemble pixel into buffer pointer from separate RGBA values.
*/
#define GFX_ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a) \
{ \
Uint32 pixel; \
\
GFX_PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a); \
*((Uint32 *)(buf)) = pixel; \
}
/*!
\brief Blend the RGB values of two pixels based on a source alpha value.
*/
#define GFX_ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB) \
do { \
dR = (((sR-dR)*(A))/255)+dR; \
dG = (((sG-dG)*(A))/255)+dG; \
dB = (((sB-dB)*(A))/255)+dB; \
} while(0)
#define PERFECT_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB, dA) \
do { \
dR = ((sR * sA) + (dR * dA * (255 - sA))); \
dG = ((sG * sA) + (dG * dA * (255 - sA))); \
dB = ((sB * sA) + (dB * dA * (255 - sA))); \
dA = (sA + (dA * (255 - sA))); \
dR = dR / dA; \
dG = dG / dA; \
dB = dB / dA; \
dA = (dA / 255); \
} while (0)
/*!
\brief 4-times unrolled DUFFs loop.
This is a very useful loop for optimizing blitters.
*/
#define GFX_DUFFS_LOOP4(pixel_copy_increment, width) \
{ int n = (width+3)/4; \
switch (width & 3) { \
case 0: do { pixel_copy_increment; \
case 3: pixel_copy_increment; \
case 2: pixel_copy_increment; \
case 1: pixel_copy_increment; \
} while ( --n > 0 ); \
} \
}
typedef struct {
Uint8 *s_pixels;
int s_width;
int s_height;
int s_skip;
Uint8 *d_pixels;
int d_width;
int d_height;
int d_skip;
void *aux_data;
SDL_PixelFormat *src;
Uint8 *table;
SDL_PixelFormat *dst;
} SDL_gfxBlitInfo;
/*!
\brief Alpha adjustment table for custom blitter.
The table provides values for a modified, non-linear
transfer function which maintain brightness.
*/
const unsigned int GFX_ALPHA_ADJUST_ARRAY[256] = {
0, /* 0 */
15, /* 1 */
22, /* 2 */
27, /* 3 */
31, /* 4 */
35, /* 5 */
39, /* 6 */
42, /* 7 */
45, /* 8 */
47, /* 9 */
50, /* 10 */
52, /* 11 */
55, /* 12 */
57, /* 13 */
59, /* 14 */
61, /* 15 */
63, /* 16 */
65, /* 17 */
67, /* 18 */
69, /* 19 */
71, /* 20 */
73, /* 21 */
74, /* 22 */
76, /* 23 */
78, /* 24 */
79, /* 25 */
81, /* 26 */
82, /* 27 */
84, /* 28 */
85, /* 29 */
87, /* 30 */
88, /* 31 */
90, /* 32 */
91, /* 33 */
93, /* 34 */
94, /* 35 */
95, /* 36 */
97, /* 37 */
98, /* 38 */
99, /* 39 */
100, /* 40 */
102, /* 41 */
103, /* 42 */
104, /* 43 */
105, /* 44 */
107, /* 45 */
108, /* 46 */
109, /* 47 */
110, /* 48 */
111, /* 49 */
112, /* 50 */
114, /* 51 */
115, /* 52 */
116, /* 53 */
117, /* 54 */
118, /* 55 */
119, /* 56 */
120, /* 57 */
121, /* 58 */
122, /* 59 */
123, /* 60 */
124, /* 61 */
125, /* 62 */
126, /* 63 */
127, /* 64 */
128, /* 65 */
129, /* 66 */
130, /* 67 */
131, /* 68 */
132, /* 69 */
133, /* 70 */
134, /* 71 */
135, /* 72 */
136, /* 73 */
137, /* 74 */
138, /* 75 */
139, /* 76 */
140, /* 77 */
141, /* 78 */
141, /* 79 */
142, /* 80 */
143, /* 81 */
144, /* 82 */
145, /* 83 */
146, /* 84 */
147, /* 85 */
148, /* 86 */
148, /* 87 */
149, /* 88 */
150, /* 89 */
151, /* 90 */
152, /* 91 */
153, /* 92 */
153, /* 93 */
154, /* 94 */
155, /* 95 */
156, /* 96 */
157, /* 97 */
158, /* 98 */
158, /* 99 */
159, /* 100 */
160, /* 101 */
161, /* 102 */
162, /* 103 */
162, /* 104 */
163, /* 105 */
164, /* 106 */
165, /* 107 */
165, /* 108 */
166, /* 109 */
167, /* 110 */
168, /* 111 */
168, /* 112 */
169, /* 113 */
170, /* 114 */
171, /* 115 */
171, /* 116 */
172, /* 117 */
173, /* 118 */
174, /* 119 */
174, /* 120 */
175, /* 121 */
176, /* 122 */
177, /* 123 */
177, /* 124 */
178, /* 125 */
179, /* 126 */
179, /* 127 */
180, /* 128 */
181, /* 129 */
182, /* 130 */
182, /* 131 */
183, /* 132 */
184, /* 133 */
184, /* 134 */
185, /* 135 */
186, /* 136 */
186, /* 137 */
187, /* 138 */
188, /* 139 */
188, /* 140 */
189, /* 141 */
190, /* 142 */
190, /* 143 */
191, /* 144 */
192, /* 145 */
192, /* 146 */
193, /* 147 */
194, /* 148 */
194, /* 149 */
195, /* 150 */
196, /* 151 */
196, /* 152 */
197, /* 153 */
198, /* 154 */
198, /* 155 */
199, /* 156 */
200, /* 157 */
200, /* 158 */
201, /* 159 */
201, /* 160 */
202, /* 161 */
203, /* 162 */
203, /* 163 */
204, /* 164 */
205, /* 165 */
205, /* 166 */
206, /* 167 */
206, /* 168 */
207, /* 169 */
208, /* 170 */
208, /* 171 */
209, /* 172 */
210, /* 173 */
210, /* 174 */
211, /* 175 */
211, /* 176 */
212, /* 177 */
213, /* 178 */
213, /* 179 */
214, /* 180 */
214, /* 181 */
215, /* 182 */
216, /* 183 */
216, /* 184 */
217, /* 185 */
217, /* 186 */
218, /* 187 */
218, /* 188 */
219, /* 189 */
220, /* 190 */
220, /* 191 */
221, /* 192 */
221, /* 193 */
222, /* 194 */
222, /* 195 */
223, /* 196 */
224, /* 197 */
224, /* 198 */
225, /* 199 */
225, /* 200 */
226, /* 201 */
226, /* 202 */
227, /* 203 */
228, /* 204 */
228, /* 205 */
229, /* 206 */
229, /* 207 */
230, /* 208 */
230, /* 209 */
231, /* 210 */
231, /* 211 */
232, /* 212 */
233, /* 213 */
233, /* 214 */
234, /* 215 */
234, /* 216 */
235, /* 217 */
235, /* 218 */
236, /* 219 */
236, /* 220 */
237, /* 221 */
237, /* 222 */
238, /* 223 */
238, /* 224 */
239, /* 225 */
240, /* 226 */
240, /* 227 */
241, /* 228 */
241, /* 229 */
242, /* 230 */
242, /* 231 */
243, /* 232 */
243, /* 233 */
244, /* 234 */
244, /* 235 */
245, /* 236 */
245, /* 237 */
246, /* 238 */
246, /* 239 */
247, /* 240 */
247, /* 241 */
248, /* 242 */
248, /* 243 */
249, /* 244 */
249, /* 245 */
250, /* 246 */
250, /* 247 */
251, /* 248 */
251, /* 249 */
252, /* 250 */
252, /* 251 */
253, /* 252 */
253, /* 253 */
254, /* 254 */
255 /* 255 */
};
/*!
\brief Internal blitter using adjusted destination alpha during RGBA->RGBA blits.
Performs the blit based on the 'info' structure and applies the transfer function
to the destination 'a' values.
\param info The blit info to use.
*/
void _SDL_gfxBlitBlitterRGBA(SDL_gfxBlitInfo * info)
{
int width = info->d_width;
int height = info->d_height;
Uint8 *src = info->s_pixels;
int srcskip = info->s_skip;
Uint8 *dst = info->d_pixels;
int dstskip = info->d_skip;
SDL_PixelFormat *srcfmt = info->src;
SDL_PixelFormat *dstfmt = info->dst;
Uint8 srcbpp = srcfmt->BytesPerPixel;
Uint8 dstbpp = dstfmt->BytesPerPixel;
while (height--) {
GFX_DUFFS_LOOP4( {
Uint32 pixel;
unsigned sR;
unsigned sG;
unsigned sB;
unsigned sA;
unsigned dR;
unsigned dG;
unsigned dB;
unsigned dA;
unsigned sAA;
GFX_DISASSEMBLE_RGBA(src, srcbpp, srcfmt, pixel, sR, sG, sB, sA);
GFX_DISASSEMBLE_RGBA(dst, dstbpp, dstfmt, pixel, dR, dG, dB, dA);
//sAA=GFX_ALPHA_ADJUST_ARRAY[sA & 255];
sAA = sA & 255;
GFX_ALPHA_BLEND(sR, sG, sB, sAA, dR, dG, dB);
dA |= sAA;
GFX_ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
src += srcbpp; dst += dstbpp;
}, width);
src += srcskip;
dst += dstskip;
}
}
/*!
\brief Internal blitter setup wrapper for RGBA->RGBA blits.
Sets up the blitter info based on the 'src' and 'dst' surfaces and rectangles.
\param src The source surface.
\param srcrect The source rectangle.
\param dst The destination surface.
\param dstrect The destination rectangle.
\returns Returns 1 if blit was performed, 0 otherwise.
*/
int _SDL_gfxBlitRGBACall(SDL_Surface * src, SDL_Rect * srcrect, SDL_Surface * dst, SDL_Rect * dstrect)
{
/*
* Set up source and destination buffer pointers, then blit
*/
if (srcrect->w && srcrect->h) {
SDL_gfxBlitInfo info;
/*
* Set up the blit information
*/
#if (SDL_MINOR_VERSION == 3)
info.s_pixels = (Uint8 *) src->pixels + (Uint16) srcrect->y * src->pitch + (Uint16) srcrect->x * src->format->BytesPerPixel;
#else
info.s_pixels = (Uint8 *) src->pixels + src->offset + (Uint16) srcrect->y * src->pitch + (Uint16) srcrect->x * src->format->BytesPerPixel;
#endif
info.s_width = srcrect->w;
info.s_height = srcrect->h;
info.s_skip = (int)(src->pitch - info.s_width * src->format->BytesPerPixel);
#if (SDL_MINOR_VERSION == 3)
info.d_pixels = (Uint8 *) dst->pixels + (Uint16) dstrect->y * dst->pitch + (Uint16) dstrect->x * dst->format->BytesPerPixel;
#else
info.d_pixels = (Uint8 *) dst->pixels + dst->offset + (Uint16) dstrect->y * dst->pitch + (Uint16) dstrect->x * dst->format->BytesPerPixel;
#endif
info.d_width = dstrect->w;
info.d_height = dstrect->h;
info.d_skip = (int)(dst->pitch - info.d_width * dst->format->BytesPerPixel);
info.aux_data = NULL;
info.src = src->format;
info.table = NULL;
info.dst = dst->format;
/*
* Run the actual software blitter
*/
_SDL_gfxBlitBlitterRGBA(&info);
return 1;
}
return (0);
}
/*!
\brief Blitter for RGBA->RGBA blits with alpha adjustment.
Verifies the input 'src' and 'dst' surfaces and rectangles and performs blit.
The destination clip rectangle is honored.
\param src The source surface.
\param srcrect The source rectangle.
\param dst The destination surface.
\param dstrect The destination rectangle.
\returns Returns 1 if blit was performed, 0 otherwise, or -1 if an error occured.
*/
int SDL_gfxBlitRGBA(SDL_Surface * src, SDL_Rect * srcrect, SDL_Surface * dst, SDL_Rect * dstrect)
{
SDL_Rect sr, dr;
int srcx, srcy, w, h;
/*
* Make sure the surfaces aren't locked
*/
if (!src || !dst) {
SDL_SetError("SDL_UpperBlit: passed a NULL surface");
return (-1);
}
if ((src->locked) || (dst->locked)) {
SDL_SetError("Surfaces must not be locked during blit");
return (-1);
}
/*
* If the destination rectangle is NULL, use the entire dest surface
*/
if (dstrect == NULL) {
dr.x = dr.y = 0;
dr.w = dst->w;
dr.h = dst->h;
} else {
dr = *dstrect;
}
/*
* Clip the source rectangle to the source surface
*/
if (srcrect) {
int maxw, maxh;
srcx = srcrect->x;
w = srcrect->w;
if (srcx < 0) {
w += srcx;
dr.x -= srcx;
srcx = 0;
}
maxw = src->w - srcx;
if (maxw < w)
w = maxw;
srcy = srcrect->y;
h = srcrect->h;
if (srcy < 0) {
h += srcy;
dr.y -= srcy;
srcy = 0;
}
maxh = src->h - srcy;
if (maxh < h)
h = maxh;
} else {
srcx = srcy = 0;
w = src->w;
h = src->h;
}
/*
* Clip the destination rectangle against the clip rectangle
*/
{
SDL_Rect *clip = &dst->clip_rect;
int dx, dy;
dx = clip->x - dr.x;
if (dx > 0) {
w -= dx;
dr.x += dx;
srcx += dx;
}
dx = dr.x + w - clip->x - clip->w;
if (dx > 0)
w -= dx;
dy = clip->y - dr.y;
if (dy > 0) {
h -= dy;
dr.y += dy;
srcy += dy;
}
dy = dr.y + h - clip->y - clip->h;
if (dy > 0)
h -= dy;
}
if (w > 0 && h > 0) {
sr.x = srcx;
sr.y = srcy;
sr.w = dr.w = w;
sr.h = dr.h = h;
return (_SDL_gfxBlitRGBACall(src, &sr, dst, &dr));
}
return 0;
}

View File

@ -0,0 +1,29 @@
/*
* sdl_blit_func.h
* This file is part of Pizzatron
*
* Copyright (C) 2017 - Félix Arreola Rodríguez
*
* Pizzatron is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Pizzatron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Pizzatron; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#ifndef __GFX_BLIT_FUNC_H__
#define __GFX_BLIT_FUNC_H__
int SDL_gfxBlitRGBA(SDL_Surface * src, SDL_Rect * srcrect, SDL_Surface * dst, SDL_Rect * dstrect);
#endif /* __GFX_BLIT_FUNC_H__ */