Skip to content

Commit

Permalink
Render HUD to separate texture (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed May 20, 2017
1 parent 98d0f64 commit 7edf23a
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 111 deletions.
8 changes: 6 additions & 2 deletions src/briefing_screens.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void CampaignIntroDraw(void *data)
// This will only draw once
const CampaignSetting *c = data;

GraphicsClear(&gGraphicsDevice);
BlitClearBuf(&gGraphicsDevice);
const int w = gGraphicsDevice.cachedConfig.Res.x;
const int h = gGraphicsDevice.cachedConfig.Res.y;
const int y = h / 4;
Expand All @@ -86,6 +86,8 @@ static void CampaignIntroDraw(void *data)
FontStr(buf, Vec2iNew(w / 12, y));
CFREE(buf);
}

BlitUpdateFromBuf(&gGraphicsDevice, gGraphicsDevice.screen);
}

typedef struct
Expand Down Expand Up @@ -211,7 +213,7 @@ static void MissionBriefingDraw(void *data)
{
const MissionBriefingData *mData = data;

GraphicsClear(&gGraphicsDevice);
BlitClearBuf(&gGraphicsDevice);

// Mission title
FontStrOpt(mData->Title, Vec2iZero(), mData->TitleOpts);
Expand All @@ -234,6 +236,8 @@ static void MissionBriefingDraw(void *data)
offset.x = -16 * (_ca_index & 1);
DrawObjectiveInfo(o, Vec2iAdd(mData->ObjectiveInfoPos, offset));
CA_FOREACH_END()

BlitUpdateFromBuf(&gGraphicsDevice, gGraphicsDevice.screen);
}

#define PERFECT_BONUS 500
Expand Down
17 changes: 12 additions & 5 deletions src/cdogs/blit.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-2016, Cong Xu
Copyright (c) 2013-2017 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -397,19 +397,26 @@ void BlitBlend(
}
}

void BlitClearBuf(GraphicsDevice *g)
{
memset(g->buf, 0, GraphicsGetMemSize(&g->cachedConfig));
}
void BlitUpdateFromBuf(GraphicsDevice *g, SDL_Texture *t)
{
SDL_UpdateTexture(t, NULL, g->buf, g->cachedConfig.Res.x * sizeof(Uint32));
}

static void RenderTexture(SDL_Renderer *r, SDL_Texture *t);
void BlitFlip(GraphicsDevice *g)
{
SDL_UpdateTexture(
g->screen, NULL, g->buf, g->cachedConfig.Res.x * sizeof(Uint32));
if (SDL_RenderClear(g->renderer) != 0)
{
LOG(LM_MAIN, LL_ERROR, "Failed to clear renderer: %s\n",
SDL_GetError());
LOG(LM_MAIN, LL_ERROR, "Failed to clear renderer: %s", SDL_GetError());
return;
}
RenderTexture(g->renderer, g->bkg);
RenderTexture(g->renderer, g->screen);
RenderTexture(g->renderer, g->hud);
// Apply brightness as an overlay texture
RenderTexture(g->renderer, g->brightnessOverlay);

Expand Down
5 changes: 3 additions & 2 deletions src/cdogs/blit.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 Cong Xu
Copyright (c) 2013-2014, 2016-2017 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -92,7 +92,8 @@ void BlitBlend(
GraphicsDevice *g, const Pic *pic, Vec2i pos, const color_t blend);
void BlitPicHighlight(
GraphicsDevice *g, const Pic *pic, const Vec2i pos, const color_t color);

void BlitClearBuf(GraphicsDevice *g);
void BlitUpdateFromBuf(GraphicsDevice *g, SDL_Texture *t);
void BlitFlip(GraphicsDevice *g);

Uint32 PixelMult(const Uint32 p, const Uint32 m);
Expand Down
123 changes: 61 additions & 62 deletions src/cdogs/camera.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2013-2016, Cong Xu
Copyright (c) 2013-2017 Cong Xu
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -117,21 +117,14 @@ void CameraUpdate(Camera *camera, const int ticks, const int ms)
static void FollowPlayer(Vec2i *pos, const int playerUID);
static void DoBuffer(
DrawBuffer *b, Vec2i center, int w, Vec2i noise, Vec2i offset);
void CameraDraw(
Camera *camera, const input_device_e pausingDevice,
const bool controllerUnplugged)
void CameraDraw(Camera *camera)
{
Vec2i centerOffset = Vec2iZero();
const PlayerData *firstPlayer = NULL;
const int numPlayersScreen = GetNumPlayersScreen(&firstPlayer);
const int w = gGraphicsDevice.cachedConfig.Res.x;
const int h = gGraphicsDevice.cachedConfig.Res.y;

// clear screen
memset(
gGraphicsDevice.buf, 0,
GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));

const Vec2i noise = ScreenShakeGetDelta(camera->shake);

GraphicsResetBlitClip(&gGraphicsDevice);
Expand Down Expand Up @@ -338,9 +331,29 @@ void CameraDraw(
}
}
GraphicsResetBlitClip(&gGraphicsDevice);
}
// Try to follow a player
static void FollowPlayer(Vec2i *pos, const int playerUID)
{
const PlayerData *p = PlayerDataGetByUID(playerUID);
if (p == NULL) return;
const TActor *a = ActorGetByUID(p->ActorUID);
if (a == NULL) return;
*pos = Vec2iFull2Real(a->Pos);
}
static void DoBuffer(
DrawBuffer *b, Vec2i center, int w, Vec2i noise, Vec2i offset)
{
DrawBufferSetFromMap(b, &gMap, Vec2iAdd(center, noise), w);
if (gPlayerDatas.size > 0)
{
DrawBufferFix(b);
}
DrawBufferDraw(b, offset, NULL);
}

HUDDraw(&camera->HUD, pausingDevice, controllerUnplugged);

void CameraDrawMode(const Camera *camera)
{
// Draw camera mode
char cameraNameBuf[256];
bool drawCameraMode = false;
Expand All @@ -365,61 +378,47 @@ void CameraDraw(
CASSERT(false, "Unknown spectate mode");
break;
}
if (drawCameraMode)
if (!drawCameraMode)
{
// Draw the message centered at the bottom
FontStrMask(
cameraNameBuf,
Vec2iNew((w - FontStrW(cameraNameBuf)) / 2, h - FontH() * 2),
colorYellow);

// Show camera controls
const PlayerData *p = GetFirstPlayer(false, true, true);
// Use default keyboard controls
input_device_e inputDevice = INPUT_DEVICE_KEYBOARD;
int deviceIndex = 0;
if (p != NULL)
{
inputDevice = p->inputDevice;
deviceIndex = p->deviceIndex;
}
Vec2i pos = Vec2iNew(
(w - FontStrW("foo/bar to follow player, baz to free-look")) / 2,
h - FontH());
char buf[256];
color_t c = colorYellow;
InputGetButtonNameColor(
inputDevice, deviceIndex, CMD_BUTTON1, buf, &c);
pos = FontStrMask(buf, pos, c);
pos = FontStrMask("/", pos, colorYellow);
c = colorYellow;
InputGetButtonNameColor(
inputDevice, deviceIndex, CMD_BUTTON2, buf, &c);
pos = FontStrMask(buf, pos, c);
pos = FontStrMask(" to follow player, ", pos, colorYellow);
InputGetDirectionNames(buf, inputDevice, deviceIndex);
pos = FontStrMask(buf, pos, colorYellow);
FontStrMask(" to free-look", pos, colorYellow);
return;
}
}
// Try to follow a player
static void FollowPlayer(Vec2i *pos, const int playerUID)
{
const PlayerData *p = PlayerDataGetByUID(playerUID);
if (p == NULL) return;
const TActor *a = ActorGetByUID(p->ActorUID);
if (a == NULL) return;
*pos = Vec2iFull2Real(a->Pos);
}
static void DoBuffer(
DrawBuffer *b, Vec2i center, int w, Vec2i noise, Vec2i offset)
{
DrawBufferSetFromMap(b, &gMap, Vec2iAdd(center, noise), w);
if (gPlayerDatas.size > 0)

const int w = gGraphicsDevice.cachedConfig.Res.x;
const int h = gGraphicsDevice.cachedConfig.Res.y;

// Draw the message centered at the bottom
FontStrMask(
cameraNameBuf,
Vec2iNew((w - FontStrW(cameraNameBuf)) / 2, h - FontH() * 2),
colorYellow);

// Show camera controls
const PlayerData *p = GetFirstPlayer(false, true, true);
// Use default keyboard controls
input_device_e inputDevice = INPUT_DEVICE_KEYBOARD;
int deviceIndex = 0;
if (p != NULL)
{
DrawBufferFix(b);
inputDevice = p->inputDevice;
deviceIndex = p->deviceIndex;
}
DrawBufferDraw(b, offset, NULL);
Vec2i pos = Vec2iNew(
(w - FontStrW("foo/bar to follow player, baz to free-look")) / 2,
h - FontH());
char buf[256];
color_t c = colorYellow;
InputGetButtonNameColor(
inputDevice, deviceIndex, CMD_BUTTON1, buf, &c);
pos = FontStrMask(buf, pos, c);
pos = FontStrMask("/", pos, colorYellow);
c = colorYellow;
InputGetButtonNameColor(
inputDevice, deviceIndex, CMD_BUTTON2, buf, &c);
pos = FontStrMask(buf, pos, c);
pos = FontStrMask(" to follow player, ", pos, colorYellow);
InputGetDirectionNames(buf, inputDevice, deviceIndex);
pos = FontStrMask(buf, pos, colorYellow);
FontStrMask(" to free-look", pos, colorYellow);
}

bool CameraIsSingleScreen(void)
Expand Down
7 changes: 3 additions & 4 deletions src/cdogs/camera.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2015-2016, Cong Xu
Copyright (c) 2015-2017 Cong Xu
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -59,8 +59,7 @@ void CameraTerminate(Camera *camera);

void CameraInput(Camera *camera, const int cmd, const int lastCmd);
void CameraUpdate(Camera *camera, const int ticks, const int ms);
void CameraDraw(
Camera *camera, const input_device_e pausingDevice,
const bool controllerUnplugged);
void CameraDraw(Camera *camera);
void CameraDrawMode(const Camera *camera);

bool CameraIsSingleScreen(void);
29 changes: 19 additions & 10 deletions src/cdogs/grafx.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-2016, Cong Xu
Copyright (c) 2013-2017 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -173,6 +173,7 @@ static void AddSupportedGraphicsModes(GraphicsDevice *device)
// Initialises the video subsystem.
// To prevent needless screen flickering, config is compared with cache
// to see if anything changed. If not, don't recreate the screen.
static void DestroyTextures(GraphicsDevice *g);
static SDL_Texture *CreateTexture(
SDL_Renderer *renderer, const SDL_TextureAccess access, const Vec2i res,
const SDL_BlendMode blend, const Uint8 alpha);
Expand Down Expand Up @@ -224,9 +225,7 @@ void GraphicsInitialize(GraphicsDevice *g)
SDL_GetWindowSize(g->window, &windowSize.x, &windowSize.y);
}
LOG(LM_GFX, LL_DEBUG, "destroying previous renderer");
SDL_DestroyTexture(g->screen);
SDL_DestroyTexture(g->bkg);
SDL_DestroyTexture(g->brightnessOverlay);
DestroyTextures(g);
SDL_DestroyRenderer(g->renderer);
SDL_FreeFormat(g->Format);
SDL_DestroyWindow(g->window);
Expand Down Expand Up @@ -265,9 +264,7 @@ void GraphicsInitialize(GraphicsDevice *g)
{
if (!initRenderer)
{
SDL_DestroyTexture(g->screen);
SDL_DestroyTexture(g->bkg);
SDL_DestroyTexture(g->brightnessOverlay);
DestroyTextures(g);
}

// Set render scale mode
Expand Down Expand Up @@ -308,6 +305,13 @@ void GraphicsInitialize(GraphicsDevice *g)
{
return;
}
g->hud = CreateTexture(
g->renderer, SDL_TEXTUREACCESS_STREAMING, Vec2iNew(w, h),
SDL_BLENDMODE_BLEND, 255);
if (g->hud == NULL)
{
return;
}
}

if (initBrightness)
Expand Down Expand Up @@ -341,6 +345,13 @@ void GraphicsInitialize(GraphicsDevice *g)
g->cachedConfig.Res.y = h;
g->cachedConfig.RestartFlags = 0;
}
static void DestroyTextures(GraphicsDevice *g)
{
SDL_DestroyTexture(g->screen);
SDL_DestroyTexture(g->bkg);
SDL_DestroyTexture(g->hud);
SDL_DestroyTexture(g->brightnessOverlay);
}
static SDL_Texture *CreateTexture(
SDL_Renderer *renderer, const SDL_TextureAccess access, const Vec2i res,
const SDL_BlendMode blend, const Uint8 alpha)
Expand All @@ -367,11 +378,9 @@ static SDL_Texture *CreateTexture(

void GraphicsTerminate(GraphicsDevice *g)
{
debug(D_NORMAL, "Shutting down video...\n");
CArrayTerminate(&g->validModes);
SDL_FreeSurface(g->icon);
SDL_DestroyTexture(g->screen);
SDL_DestroyTexture(g->bkg);
DestroyTextures(g);
SDL_DestroyTexture(g->brightnessOverlay);
SDL_DestroyRenderer(g->renderer);
SDL_FreeFormat(g->Format);
Expand Down
3 changes: 2 additions & 1 deletion src/cdogs/grafx.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-2016, Cong Xu
Copyright (c) 2013-2017 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -88,6 +88,7 @@ typedef struct
int IsWindowInitialized;
SDL_Surface *icon;
SDL_Texture *screen;
SDL_Texture *hud;
SDL_Renderer *renderer;
SDL_Window *window;
SDL_PixelFormat *Format;
Expand Down
Loading

0 comments on commit 7edf23a

Please sign in to comment.