Skip to content

Commit

Permalink
Fix shadow drawing (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Feb 6, 2018
1 parent 4d6ab98 commit b0d25ee
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 39 deletions.
Binary file added graphics/shadow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/cdogs/draw/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ static void DrawThing(DrawBuffer *b, const TTileItem *t, const struct vec2i offs

if (!svec2i_is_zero(t->ShadowSize))
{
DrawShadow(&gGraphicsDevice, picPos, t->ShadowSize);
DrawShadow(&gGraphicsDevice, picPos, t->ShadowSize, b->renderToTex);
}

if (t->CPicFunc)
Expand All @@ -322,7 +322,7 @@ static void DrawThing(DrawBuffer *b, const TTileItem *t, const struct vec2i offs
{
TActor *a = CArrayGet(&gActors, t->id);
ActorPics pics = GetCharacterPicsFromActor(a);
DrawActorPics(&pics, picPos);
DrawActorPics(&pics, picPos, b->renderToTex);
// Draw weapon indicators
DrawLaserSight(&pics, a, picPos);
}
Expand Down
9 changes: 5 additions & 4 deletions src/cdogs/draw/draw_actor.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2017, Cong Xu
Copyright (c) 2013-2018 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -245,7 +245,8 @@ static Character *ActorGetCharacterMutable(TActor *a)

static void DrawDyingBody(
GraphicsDevice *g, const ActorPics *pics, const struct vec2i pos);
void DrawActorPics(const ActorPics *pics, const struct vec2i pos)
void DrawActorPics(
const ActorPics *pics, const struct vec2i pos, const bool renderToTex)
{
if (pics->IsDead)
{
Expand All @@ -259,7 +260,7 @@ void DrawActorPics(const ActorPics *pics, const struct vec2i pos)
// Draw shadow
if (!pics->IsTransparent)
{
DrawShadow(&gGraphicsDevice, pos, svec2i(8, 6));
DrawShadow(&gGraphicsDevice, pos, svec2i(8, 6), renderToTex);
}
for (int i = 0; i < BODY_PART_COUNT; i++)
{
Expand Down Expand Up @@ -459,7 +460,7 @@ void DrawCharacterSimple(
ActorPics pics = GetCharacterPics(
c, d, ACTORANIMATION_IDLE, 0, NULL, GUNSTATE_READY,
false, NULL, NULL, 0);
DrawActorPics(&pics, pos);
DrawActorPics(&pics, pos, true);
if (hilite)
{
FontCh('>', svec2i_add(pos, svec2i(-8, -16)));
Expand Down
5 changes: 3 additions & 2 deletions src/cdogs/draw/draw_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2014, 2016-2017 Cong Xu
Copyright (c) 2013-2014, 2016-2018 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -91,7 +91,8 @@ ActorPics GetCharacterPics(
const bool isTransparent, HSV *tint, color_t *mask,
const int deadPic);
ActorPics GetCharacterPicsFromActor(TActor *a);
void DrawActorPics(const ActorPics *pics, const struct vec2i pos);
void DrawActorPics(
const ActorPics *pics, const struct vec2i pos, const bool renderToTex);
void DrawLaserSight(
const ActorPics *pics, const TActor *a, const struct vec2i picPos);
void DrawActorHighlight(
Expand Down
77 changes: 48 additions & 29 deletions src/cdogs/draw/drawtools.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2014, Cong Xu
Copyright (c) 2013-2014, 2018 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -51,6 +51,8 @@
#include "config.h"
#include "draw/drawtools.h"
#include "palette.h"
#include "pic_manager.h"
#include "texture.h"
#include "utils.h"
#include "blit.h"
#include "grafx.h"
Expand Down Expand Up @@ -118,8 +120,6 @@ Draw_StraightLine(
return;
}

#define ABS(x) ( x > 0 ? x : (-x) )

static void Draw_DiagonalLine(const int x1, const int x2, color_t c)
{
register int i;
Expand Down Expand Up @@ -159,7 +159,7 @@ void Draw_Line(
{
if (x1 == x2 || y1 == y2)
Draw_StraightLine(x1, y1, x2, y2, c);
else if (ABS((x2 - x1)) == ABS((y1 - y2)))
else if (abs((x2 - x1)) == abs((y1 - y2)))
Draw_DiagonalLine(x1, x2, c);
/*else
Draw_OtherLine(x1, y1, x2, y2, c);*/
Expand Down Expand Up @@ -254,45 +254,64 @@ void DrawCross(GraphicsDevice *device, int x, int y, color_t color)
*(screen + gGraphicsDevice.cachedConfig.Res.x) = pixel;
}

void DrawShadow(GraphicsDevice *device, struct vec2i pos, struct vec2i size)
// Note: size is half-size
void DrawShadow(
GraphicsDevice *g, struct vec2i pos, struct vec2i size,
const bool renderToTex)
{
if (!ConfigGetBool(&gConfig, "Graphics.Shadows"))
{
return;
}
struct vec2i drawPos;
for (drawPos.y = pos.y - size.y; drawPos.y < pos.y + size.y; drawPos.y++)
if (renderToTex)
{
if (drawPos.y >= device->clipping.bottom)
{
break;
}
if (drawPos.y < device->clipping.top)
{
continue;
}
for (drawPos.x = pos.x - size.x; drawPos.x < pos.x + size.x; drawPos.x++)
struct vec2i drawPos;
for (drawPos.y = pos.y - size.y;
drawPos.y < pos.y + size.y;
drawPos.y++)
{
// Calculate value tint based on distance from center
struct vec2i scaledPos;
int distance2;
if (drawPos.x >= device->clipping.right)
if (drawPos.y >= g->clipping.bottom)
{
break;
}
if (drawPos.x < device->clipping.left)
if (drawPos.y < g->clipping.top)
{
continue;
}
scaledPos.x = drawPos.x;
scaledPos.y = (drawPos.y - pos.y) * size.x / size.y + pos.y;
distance2 = svec2i_distance_squared(scaledPos, pos);
// Maximum distance is x, so scale distance squared by x squared
const HSV tint =
for (drawPos.x = pos.x - size.x;
drawPos.x < pos.x + size.x;
drawPos.x++)
{
-1.0, 1.0, CLAMP(distance2 * 1.0 / (size.x*size.x), 0.0, 1.0)
};
DrawPointTint(device, drawPos, tint);
// Calculate value tint based on distance from center
struct vec2i scaledPos;
int distance2;
if (drawPos.x >= g->clipping.right)
{
break;
}
if (drawPos.x < g->clipping.left)
{
continue;
}
scaledPos.x = drawPos.x;
scaledPos.y = (drawPos.y - pos.y) * size.x / size.y + pos.y;
distance2 = svec2i_distance_squared(scaledPos, pos);
// Maximum distance is x, so scale distance squared by x squared
const HSV tint =
{
-1.0, 1.0,
CLAMP(distance2 * 1.0 / (size.x*size.x), 0.0, 1.0)
};
DrawPointTint(g, drawPos, tint);
}
}
}
else
{
const Pic *shadow = PicManagerGetPic(&gPicManager, "shadow");
const Rect2i dest =
Rect2iNew(svec2i_subtract(pos, size), svec2i_scale(size, 2));
TextureRender(
shadow->Tex, g->gameWindow.renderer, dest, colorTransparent);
}
}
6 changes: 4 additions & 2 deletions src/cdogs/draw/drawtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2014, Cong Xu
Copyright (c) 2013-2014, 2018 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -79,4 +79,6 @@ void DrawRectangle(
// *
void DrawCross(GraphicsDevice *device, int x, int y, color_t color);

void DrawShadow(GraphicsDevice *device, struct vec2i pos, struct vec2i size);
void DrawShadow(
GraphicsDevice *g, struct vec2i pos, struct vec2i size,
const bool renderToTex);
15 changes: 15 additions & 0 deletions src/cdogs/pic.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ bool PicTryMakeTex(Pic *p)
LOG(LM_GFX, LL_ERROR, "cannot update texture: %s", SDL_GetError());
return false;
}
// Check for alpha pixels - if none we can get away with no blending
bool hasAlpha = false;
for (int i = 0; i < p->size.x * p->size.y; i++)
{
const Uint32 pixel = p->Data[i];
color_t c;
SDL_GetRGBA(pixel, gGraphicsDevice.Format, &c.r, &c.g, &c.b, &c.a);
hasAlpha = hasAlpha || c.a < 255;
}
if (hasAlpha && SDL_SetTextureBlendMode(p->Tex, SDL_BLENDMODE_BLEND) != 0)
{
LOG(LM_GFX, LL_ERROR, "cannot set texture blend mode: %s",
SDL_GetError());
return false;
}
return true;
}

Expand Down

0 comments on commit b0d25ee

Please sign in to comment.