Skip to content

Commit

Permalink
Better implementation of colored torch flames (#189)
Browse files Browse the repository at this point in the history
Moved them into blitter system so they are drawn when the other flames are.
  • Loading branch information
NagyD committed Mar 24, 2019
1 parent 57aa898 commit c183aa0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 66 deletions.
6 changes: 3 additions & 3 deletions src/proto.h
Expand Up @@ -457,9 +457,9 @@ void __pascal far draw_tile_base();
void __pascal far draw_tile_anim();
void __pascal far draw_tile_fore();
int __pascal far get_loose_frame(byte modifier);
int __pascal far add_backtable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, byte blit, byte peel);
int __pascal far add_foretable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, byte blit, byte peel);
int __pascal far add_midtable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, byte blit, byte peel);
int __pascal far add_backtable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, int blit, byte peel);
int __pascal far add_foretable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, int blit, byte peel);
int __pascal far add_midtable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, int blit, byte peel);
void __pascal far add_peel(int left,int right,int top,int height);
void __pascal far add_wipetable(sbyte layer,short left,short bottom,sbyte height,short width,sbyte color);
void __pascal far draw_table(int which_table);
Expand Down
70 changes: 11 additions & 59 deletions src/seg008.c
Expand Up @@ -505,58 +505,6 @@ int __pascal far get_spike_frame(byte modifier) {
}
}

#ifdef USE_COLORED_TORCHES
void draw_colored_torch(int color, int frame, int xh, int xl, int ybottom) {
SDL_Surface* image = get_image(id_chtab_1_flameswordpotion, frame - 1);
if (image == NULL) return;

if (SDL_SetColorKey(image, SDL_TRUE, 0) != 0) {
sdlperror("SDL_SetColorKey");
quit(1);
}

SDL_Surface* colored_image = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_ARGB8888, 0);
SDL_SetSurfaceBlendMode(colored_image, SDL_BLENDMODE_NONE);

if (SDL_LockSurface(colored_image) != 0) {
sdlperror("SDL_LockSurface");
quit(1);
}

int w = colored_image->w;
int h = colored_image->h;
int y,x;
int iRed = ((color >> 4) & 3) * 85;
int iGreen = ((color >> 2) & 3) * 85;
int iBlue = ((color >> 0) & 3) * 85;
uint32_t old_color = SDL_MapRGB(colored_image->format, 0xFC, 0x84, 0x00) & 0xFFFFFF; // the orange in the flame
uint32_t new_color = SDL_MapRGB(colored_image->format, iRed, iGreen, iBlue) & 0xFFFFFF;
int stride = colored_image->pitch;
for (y = 0; y < h; ++y) {
uint32_t* pixel_ptr = (uint32_t*) ((byte*)colored_image->pixels + stride * y);
for (x = 0; x < w; ++x) {
if ((*pixel_ptr & 0xFFFFFF) == old_color) {
// set RGB but leave alpha
*pixel_ptr = (*pixel_ptr & 0xFF000000) | new_color;
}
++pixel_ptr;
}
}
SDL_UnlockSurface(colored_image);
/*
SDL_SetSurfaceBlendMode(colored_image, SDL_BLENDMODE_BLEND);
SDL_SetSurfaceBlendMode(current_target_surface, SDL_BLENDMODE_BLEND);
SDL_SetSurfaceAlphaMod(colored_image, 255);
if (SDL_BlitSurface(colored_image, &src_rect, current_target_surface, &dest_rect) != 0) {
sdlperror("SDL_BlitSurface");
quit(1);
}
*/
method_6_blit_img_to_scr(colored_image, xh * 8 + xl, ybottom - h, blitters_0_no_transp);
SDL_FreeSurface(colored_image);
}
#endif

// seg008:08B5
void __pascal far draw_tile_anim_right() {
switch (tile_left) {
Expand All @@ -575,15 +523,15 @@ void __pascal far draw_tile_anim_right() {
case tiles_19_torch:
case tiles_30_torch_with_debris:
if (modifier_left < 9) {
int blit = blitters_0_no_transp;
#ifdef USE_COLORED_TORCHES
int color = torch_colors[drawn_room][drawn_row * 10 + drawn_col - 1];
if (color != 0) {
draw_colored_torch(color, modifier_left + 1, draw_xh + 1, 0, draw_main_y - 40);
return;
blit = blitters_colored_flame + (color & 0x3F);
}
#endif
// images 1..9 are the flames
add_backtable(id_chtab_1_flameswordpotion, modifier_left + 1, draw_xh + 1, 0, draw_main_y - 40, blitters_0_no_transp, 0);
add_backtable(id_chtab_1_flameswordpotion, modifier_left + 1, draw_xh + 1, 0, draw_main_y - 40, blit, 0);
}
break;
}
Expand Down Expand Up @@ -792,7 +740,7 @@ image_type* get_image(short chtab_id, int id) {
}

// seg008:10A8
int __pascal far add_backtable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, byte blit, byte peel) {
int __pascal far add_backtable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, int blit, byte peel) {
word index;
if (id == 0) {
return 0;
Expand Down Expand Up @@ -821,7 +769,7 @@ int __pascal far add_backtable(short chtab_id, int id, sbyte xh, sbyte xl, int y
}

// seg008:1017
int __pascal far add_foretable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, byte blit, byte peel) {
int __pascal far add_foretable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, int blit, byte peel) {
word index;
if (id == 0) return 0;
index = foretable_count;
Expand All @@ -848,7 +796,7 @@ int __pascal far add_foretable(short chtab_id, int id, sbyte xh, sbyte xl, int y
}

// seg008:113A
int __pascal far add_midtable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, byte blit, byte peel) {
int __pascal far add_midtable(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, int blit, byte peel) {
word index;
if (id == 0) {
return 0;
Expand Down Expand Up @@ -1082,7 +1030,11 @@ void __pascal far draw_image(image_type far *image,image_type far *mask,int xpos
method_6_blit_img_to_scr(image, xpos, ypos, blit);
break;
default:
method_3_blit_mono(image, xpos, ypos, 0, blit & 0xBF);
if (blit >= 0x100) {
method_6_blit_img_to_scr(mask, xpos, ypos, blit);
} else {
method_3_blit_mono(image, xpos, ypos, 0, blit & 0xBF);
}
break;
}
if (need_drects) {
Expand Down
49 changes: 49 additions & 0 deletions src/seg009.c
Expand Up @@ -2901,6 +2901,47 @@ void blit_xor(SDL_Surface* target_surface, SDL_Rect* dest_rect, SDL_Surface* ima
SDL_FreeSurface(helper_surface);
}

#ifdef USE_COLORED_TORCHES
void draw_colored_torch(int color, SDL_Surface* image, int xpos, int ypos) {
if (SDL_SetColorKey(image, SDL_TRUE, 0) != 0) {
sdlperror("SDL_SetColorKey");
quit(1);
}

SDL_Surface* colored_image = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_ARGB8888, 0);
SDL_SetSurfaceBlendMode(colored_image, SDL_BLENDMODE_NONE);

if (SDL_LockSurface(colored_image) != 0) {
sdlperror("SDL_LockSurface");
quit(1);
}

int w = colored_image->w;
int h = colored_image->h;
int y,x;
int iRed = ((color >> 4) & 3) * 85;
int iGreen = ((color >> 2) & 3) * 85;
int iBlue = ((color >> 0) & 3) * 85;
uint32_t old_color = SDL_MapRGB(colored_image->format, 0xFC, 0x84, 0x00) & 0xFFFFFF; // the orange in the flame
uint32_t new_color = SDL_MapRGB(colored_image->format, iRed, iGreen, iBlue) & 0xFFFFFF;
int stride = colored_image->pitch;
for (y = 0; y < h; ++y) {
uint32_t* pixel_ptr = (uint32_t*) ((byte*)colored_image->pixels + stride * y);
for (x = 0; x < w; ++x) {
if ((*pixel_ptr & 0xFFFFFF) == old_color) {
// set RGB but leave alpha
*pixel_ptr = (*pixel_ptr & 0xFF000000) | new_color;
}
++pixel_ptr;
}
}
SDL_UnlockSurface(colored_image);

method_6_blit_img_to_scr(colored_image, xpos, ypos, blitters_0_no_transp);
SDL_FreeSurface(colored_image);
}
#endif

image_type far * __pascal far method_6_blit_img_to_scr(image_type far *image,int xpos,int ypos,int blit) {
if (image == NULL) {
printf("method_6_blit_img_to_scr: image == NULL\n");
Expand All @@ -2920,6 +2961,14 @@ image_type far * __pascal far method_6_blit_img_to_scr(image_type far *image,int
blit_xor(current_target_surface, &dest_rect, image, &src_rect);
return image;
}

#ifdef USE_COLORED_TORCHES
if (blit >= blitters_colored_flame && blit <= blitters_colored_flame_last) {
draw_colored_torch(blit - blitters_colored_flame, image, xpos, ypos);
return image;
}
#endif

SDL_SetSurfaceBlendMode(image, SDL_BLENDMODE_NONE);
SDL_SetSurfaceAlphaMod(image, 255);

Expand Down
12 changes: 8 additions & 4 deletions src/types.h
Expand Up @@ -87,15 +87,15 @@ typedef struct tile_and_mod {
byte modifier;
} tile_and_mod;

typedef int __pascal far (*add_table_type)(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, byte blit, byte peel);
typedef int __pascal far (*add_table_type)(short chtab_id, int id, sbyte xh, sbyte xl, int ybottom, int blit, byte peel);

typedef struct back_table_type {
sbyte xh;
sbyte xl;
short y;
byte chtab_id;
byte id;
byte blit;
int blit;
} back_table_type;

typedef struct midtable_type {
Expand All @@ -106,7 +106,7 @@ typedef struct midtable_type {
byte id;
byte peel;
rect_type clip;
byte blit;
int blit;
} midtable_type;

typedef struct wipetable_type {
Expand Down Expand Up @@ -177,7 +177,11 @@ enum blitters {
/* 0x40..0x4F will draw a monochrome image with color 0..15 */
blitters_40h_mono = 0x40,
blitters_46h_mono_6 = 0x46, // used for palace wall patterns
blitters_4Ch_mono_12 = 0x4C // used for chomper blood
blitters_4Ch_mono_12 = 0x4C, // used for chomper blood
#ifdef USE_COLORED_TORCHES
blitters_colored_flame = 0x100,
blitters_colored_flame_last = 0x13F,
#endif
};

enum grmodes {
Expand Down

0 comments on commit c183aa0

Please sign in to comment.