Skip to content

Commit

Permalink
Draw textures for floor (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Feb 6, 2018
1 parent bf093ac commit 4d6ab98
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/cdogs/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void CameraInit(Camera *camera)
{
memset(camera, 0, sizeof *camera);
DrawBufferInit(
&camera->Buffer, svec2i(X_TILES, Y_TILES), &gGraphicsDevice);
&camera->Buffer, svec2i(X_TILES, Y_TILES), &gGraphicsDevice, false);
camera->lastPosition = svec2_zero();
HUDInit(&camera->HUD, &gGraphicsDevice, &gMission);
camera->shake = ScreenShakeZero();
Expand Down
25 changes: 18 additions & 7 deletions src/cdogs/draw/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,35 @@ static void DrawFloor(DrawBuffer *b, struct vec2i offset)
if (tile->pic != NULL && tile->pic->pic.Data != NULL &&
!(tile->flags & MAPTILE_IS_WALL))
{
const NamedPic *pic = tile->pic;
color_t mask = colorWhite;
switch (GetTileLOS(tile, useFog))
{
case TILE_LOS_NORMAL:
Blit(&gGraphicsDevice, &tile->pic->pic, pos);
break;
case TILE_LOS_FOG:
BlitMasked(
&gGraphicsDevice,
&tile->pic->pic,
pos,
colorFog,
false);
mask = colorFog;
break;
case TILE_LOS_NONE:
default:
// don't draw
pic = NULL;
break;
}
if (pic == NULL)
{
continue;
}
if (b->renderToTex)
{
BlitMasked(&gGraphicsDevice, &pic->pic, pos, mask, false);
}
else
{
PicRender(
&pic->pic, gGraphicsDevice.gameWindow.renderer,
pos, mask);
}
}
}
tile += X_TILES - b->Size.x;
Expand Down
7 changes: 5 additions & 2 deletions src/cdogs/draw/draw_buffer.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-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 @@ -54,7 +54,9 @@
#include "los.h"


void DrawBufferInit(DrawBuffer *b, struct vec2i size, GraphicsDevice *g)
void DrawBufferInit(
DrawBuffer *b, struct vec2i size, GraphicsDevice *g,
const bool renderToTex)
{
b->OrigSize = size;
CMALLOC(b->tiles, size.x * sizeof *b->tiles);
Expand All @@ -66,6 +68,7 @@ void DrawBufferInit(DrawBuffer *b, struct vec2i size, GraphicsDevice *g)
b->g = g;
CArrayInit(&b->displaylist, sizeof(const TTileItem *));
CArrayReserve(&b->displaylist, 32);
b->renderToTex = renderToTex;
}
void DrawBufferTerminate(DrawBuffer *b)
{
Expand Down
7 changes: 5 additions & 2 deletions src/cdogs/draw/draw_buffer.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, 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 @@ -60,9 +60,12 @@ typedef struct
struct vec2i Size; // size in tiles
Tile **tiles;
CArray displaylist; // of const TTileItem *, to determine draw order
bool renderToTex;
} DrawBuffer;

void DrawBufferInit(DrawBuffer *b, struct vec2i size, GraphicsDevice *g);
void DrawBufferInit(
DrawBuffer *b, struct vec2i size, GraphicsDevice *g,
const bool renderToTex);
void DrawBufferTerminate(DrawBuffer *b);

void DrawBufferSetFromMap(
Expand Down
2 changes: 0 additions & 2 deletions src/cdogs/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ void FontLoad(Font *f, const char *imgPath, const bool isProportional)
pos.x += step.x, x++, chars++)
{
Pic p;
p.size = f->Size;
p.offset = svec2i_zero();
PicLoad(
&p, f->Size,
svec2i_add(pos, svec2i(f->Padding.Left, f->Padding.Top)),
Expand Down
4 changes: 2 additions & 2 deletions src/cdogs/grafx_bg.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void GrafxMakeRandomBackground(
rand() * 360.0 / RAND_MAX, rand() * 1.0 / RAND_MAX, 0.5
};
DrawBuffer buffer;
DrawBufferInit(&buffer, svec2i(X_TILES, Y_TILES), device);
DrawBufferInit(&buffer, svec2i(X_TILES, Y_TILES), device, true);
co->MissionIndex = 0;
GrafxMakeBackground(
device, &buffer, co, mo, map, tint, false, svec2_zero(), NULL);
Expand Down Expand Up @@ -99,7 +99,7 @@ void GrafxRedrawBackground(GraphicsDevice *g, const struct vec2 pos)
{
memset(g->buf, 0, GraphicsGetMemSize(&g->cachedConfig));
DrawBuffer buffer;
DrawBufferInit(&buffer, svec2i(X_TILES, Y_TILES), g);
DrawBufferInit(&buffer, svec2i(X_TILES, Y_TILES), g, true);
const HSV tint = {
rand() * 360.0 / RAND_MAX, rand() * 1.0 / RAND_MAX, 0.5
};
Expand Down
14 changes: 8 additions & 6 deletions src/cdogs/pic.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ Uint32 ColorToPixel(
}


static bool TryMakeTex(Pic *p);
void PicLoad(
Pic *p, const struct vec2i size, const struct vec2i offset, const SDL_Surface *image)
{
memset(p, 0, sizeof *p);
p->size = size;
p->offset = svec2i_zero();
CMALLOC(p->Data, size.x * size.y * sizeof *((Pic *)0)->Data);
Expand Down Expand Up @@ -86,7 +86,7 @@ void PicLoad(
}
}

if (!TryMakeTex(p))
if (!PicTryMakeTex(p))
{
goto bail;
}
Expand All @@ -95,8 +95,9 @@ void PicLoad(
bail:
PicFree(p);
}
static bool TryMakeTex(Pic *p)
bool PicTryMakeTex(Pic *p)
{
SDL_DestroyTexture(p->Tex);
p->Tex = TextureCreate(
gGraphicsDevice.gameWindow.renderer, SDL_TEXTUREACCESS_STATIC,
p->size, SDL_BLENDMODE_NONE, 255);
Expand Down Expand Up @@ -188,7 +189,7 @@ void PicTrim(Pic *pic, const bool xTrim, const bool yTrim)
pic->Data = newData;
pic->size = newSize;
pic->offset = svec2i_zero();
TryMakeTex(pic);
PicTryMakeTex(pic);
}

bool PicPxIsEdge(const Pic *pic, const struct vec2i pos, const bool isPixel)
Expand Down Expand Up @@ -217,8 +218,9 @@ bool PicPxIsEdge(const Pic *pic, const struct vec2i pos, const bool isPixel)
}
}

void PicRender(const Pic *p, SDL_Renderer *r, const struct vec2i pos)
void PicRender(
const Pic *p, SDL_Renderer *r, const struct vec2i pos, const color_t mask)
{
const Rect2i dest = Rect2iNew(pos, p->size);
TextureRender(p->Tex, r, dest);
TextureRender(p->Tex, r, dest, mask);
}
7 changes: 5 additions & 2 deletions src/cdogs/pic.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ Uint32 ColorToPixel(
ColorToPixel(gGraphicsDevice.Format, gGraphicsDevice.Format->Ashift, _c)

void PicLoad(
Pic *p, const struct vec2i size, const struct vec2i offset, const SDL_Surface *image);
Pic *p, const struct vec2i size, const struct vec2i offset,
const SDL_Surface *image);
bool PicTryMakeTex(Pic *p);
Pic PicCopy(const Pic *src);
void PicFree(Pic *pic);
bool PicIsNone(const Pic *pic);
Expand All @@ -59,4 +61,5 @@ void PicTrim(Pic *pic, const bool xTrim, const bool yTrim);

bool PicPxIsEdge(const Pic *pic, const struct vec2i pos, const bool isPixel);

void PicRender(const Pic *p, SDL_Renderer *r, const struct vec2i pos);
void PicRender(
const Pic *p, SDL_Renderer *r, const struct vec2i pos, const color_t mask);
5 changes: 5 additions & 0 deletions src/cdogs/pic_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,11 @@ void PicManagerGenerateMaskedPic(
}
p.Data[i] = COLOR2PIXEL(c);
// TODO: more channels

if (!PicTryMakeTex(&p))
{
p.Tex = NULL;
}
}
AddNamedPic(pm->customPics, maskedName, &p);

Expand Down
9 changes: 8 additions & 1 deletion src/cdogs/texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,18 @@ SDL_Texture *TextureCreate(
return t;
}

void TextureRender(SDL_Texture *t, SDL_Renderer *r, const Rect2i dest)
void TextureRender(
SDL_Texture *t, SDL_Renderer *r, const Rect2i dest, const color_t mask)
{
const SDL_Rect destRect = {
dest.Pos.x, dest.Pos.y, dest.Size.x, dest.Size.y
};
if (!ColorEquals(mask, colorTransparent) &&
SDL_SetTextureColorMod(t, mask.r, mask.g, mask.b) != 0)
{
LOG(LM_MAIN, LL_ERROR, "Failed to set texture mask: %s",
SDL_GetError());
}
if (SDL_RenderCopy(r, t, NULL, Rect2iIsZero(dest) ? NULL : &destRect) != 0)
{
LOG(LM_MAIN, LL_ERROR, "Failed to render texture: %s", SDL_GetError());
Expand Down
3 changes: 2 additions & 1 deletion src/cdogs/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ SDL_Texture *TextureCreate(
SDL_Renderer *renderer, const SDL_TextureAccess access, const struct vec2i res,
const SDL_BlendMode blend, const Uint8 alpha);

void TextureRender(SDL_Texture *t, SDL_Renderer *r, const Rect2i dest);
void TextureRender(
SDL_Texture *t, SDL_Renderer *r, const Rect2i dest, const color_t mask);
4 changes: 2 additions & 2 deletions src/cdogs/window_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ void WindowContextPreRender(WindowContext *wc)
return;
}
CA_FOREACH(SDL_Texture *, t, wc->texturesBkg)
TextureRender(*t, wc->renderer, Rect2iZero());
TextureRender(*t, wc->renderer, Rect2iZero(), colorTransparent);
CA_FOREACH_END()
}

void WindowContextPostRender(WindowContext *wc)
{
CA_FOREACH(SDL_Texture *, t, wc->textures)
TextureRender(*t, wc->renderer, Rect2iZero());
TextureRender(*t, wc->renderer, Rect2iZero(), colorTransparent);
CA_FOREACH_END()

SDL_RenderPresent(wc->renderer);
Expand Down
2 changes: 1 addition & 1 deletion src/cdogs/yajl/yajl_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void yajl_string_decode(yajl_buf buf, const unsigned char * str,
break;
}
default:
assert("this should never happen" == NULL);
assert("this should never happen" && 0);
}
yajl_buf_append(buf, unescaped, (unsigned int)strlen(unescaped));
beg = ++end;
Expand Down
6 changes: 4 additions & 2 deletions src/cdogsed/cdogsed.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ static void MakeBackground(const bool changedMission)
extra.guideImageAlpha = brush.GuideImageAlpha;

DrawBufferTerminate(&sDrawBuffer);
DrawBufferInit(&sDrawBuffer, svec2i(X_TILES, Y_TILES), &gGraphicsDevice);
DrawBufferInit(
&sDrawBuffer, svec2i(X_TILES, Y_TILES), &gGraphicsDevice, true);
GrafxMakeBackground(
ec.g, &sDrawBuffer, &gCampaign, &gMission, &gMap,
tintNone, true, ec.camera, &extra);
Expand Down Expand Up @@ -1377,7 +1378,8 @@ int main(int argc, char *argv[])
// Note: must do this after text init since positions depend on text height
sObjs = CreateMainObjs(&gCampaign, &brush, svec2i(320, 240));
memset(&sDrawObjs, 0, sizeof sDrawObjs);
DrawBufferInit(&sDrawBuffer, svec2i(X_TILES, Y_TILES), &gGraphicsDevice);
DrawBufferInit(
&sDrawBuffer, svec2i(X_TILES, Y_TILES), &gGraphicsDevice, true);

// Reset campaign (graphics init may have created dummy campaigns)
CampaignSettingTerminate(&gCampaign.Setting);
Expand Down

0 comments on commit 4d6ab98

Please sign in to comment.