Skip to content

Commit

Permalink
Fix editor build (#500)
Browse files Browse the repository at this point in the history
Don't move window when resizing
Fix background texture rendering
Note: editor redraw still broken
  • Loading branch information
cxong committed Feb 7, 2018
1 parent 8b2149d commit 5f9e5c6
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 46 deletions.
4 changes: 0 additions & 4 deletions src/cdogs/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ void CameraInit(Camera *camera)
memset(camera, 0, sizeof *camera);
DrawBufferInit(
&camera->Buffer, svec2i(X_TILES, Y_TILES), &gGraphicsDevice);
if (SDL_SetRenderTarget(gGraphicsDevice.gameWindow.renderer, NULL) != 0)
{
LOG(LM_MAIN, LL_ERROR, "cannot set render target: %s", SDL_GetError());
}
camera->lastPosition = svec2_zero();
HUDInit(&camera->HUD, &gGraphicsDevice, &gMission);
camera->shake = ScreenShakeZero();
Expand Down
40 changes: 29 additions & 11 deletions src/cdogs/grafx.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,25 @@ void GraphicsInitialize(GraphicsDevice *g)

if (initRenderer)
{
Uint32 sdlFlags = SDL_WINDOW_RESIZABLE;
Uint32 windowFlags = SDL_WINDOW_RESIZABLE;
if (g->cachedConfig.Fullscreen)
{
sdlFlags |= SDL_WINDOW_FULLSCREEN;
windowFlags |= SDL_WINDOW_FULLSCREEN;
}

LOG(LM_GFX, LL_INFO, "graphics mode(%dx%d %dx)",
w, h, g->cachedConfig.ScaleFactor);
// Get the previous window's size and recreate it
struct vec2i windowSize = svec2i(
w * g->cachedConfig.ScaleFactor, h * g->cachedConfig.ScaleFactor);
// Get the previous window's dimensions and recreate it
Rect2i windowDim = Rect2iNew(
svec2i(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED),
svec2i_scale(svec2i(w, h), (float)g->cachedConfig.ScaleFactor)
);
if (g->gameWindow.window)
{
SDL_GetWindowPosition(
g->gameWindow.window, &windowDim.Pos.x, &windowDim.Pos.y);
SDL_GetWindowSize(
g->gameWindow.window, &windowSize.x, &windowSize.y);
g->gameWindow.window, &windowDim.Size.x, &windowDim.Size.y);
}
LOG(LM_GFX, LL_DEBUG, "destroying previous renderer");
WindowContextDestroy(&g->gameWindow);
Expand All @@ -235,15 +239,15 @@ void GraphicsInitialize(GraphicsDevice *g)
g->cachedConfig.IsEditor ? "Editor " : "",
CDOGS_SDL_VERSION);
if (!WindowContextCreate(
&g->gameWindow, windowSize, sdlFlags, title, g->icon,
&g->gameWindow, windowDim, windowFlags, title, g->icon,
svec2i(w, h)))
{
return;
}
if (g->cachedConfig.SecondWindow)
{
if (!WindowContextCreate(
&g->secondWindow, windowSize, sdlFlags, title, g->icon,
&g->secondWindow, windowDim, windowFlags, title, g->icon,
svec2i(w, h)))
{
return;
Expand Down Expand Up @@ -288,18 +292,32 @@ void GraphicsInitialize(GraphicsDevice *g)

CFREE(g->buf);
CCALLOC(g->buf, GraphicsGetMemSize(&g->cachedConfig));
g->bkgTgt = WindowContextCreateTexture(
&g->gameWindow, SDL_TEXTUREACCESS_TARGET, svec2i(w, h),
SDL_BLENDMODE_NONE, 255, true);
if (g->bkgTgt == NULL)
{
return;
}
g->bkg = WindowContextCreateTexture(
&g->gameWindow, SDL_TEXTUREACCESS_STATIC, svec2i(w, h),
SDL_BLENDMODE_NONE, 255, true);
SDL_BLENDMODE_BLEND, 255, true);
if (g->bkg == NULL)
{
return;
}
if (g->cachedConfig.SecondWindow)
{
g->bkg2 = WindowContextCreateTexture(
&g->secondWindow, SDL_TEXTUREACCESS_STATIC, svec2i(w, h),
g->bkgTgt2 = WindowContextCreateTexture(
&g->gameWindow, SDL_TEXTUREACCESS_TARGET, svec2i(w, h),
SDL_BLENDMODE_NONE, 255, true);
if (g->bkgTgt2 == NULL)
{
return;
}
g->bkg2 = WindowContextCreateTexture(
&g->secondWindow, SDL_TEXTUREACCESS_TARGET, svec2i(w, h),
SDL_BLENDMODE_BLEND, 255, true);
if (g->bkg2 == NULL)
{
return;
Expand Down
2 changes: 2 additions & 0 deletions src/cdogs/grafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ typedef struct
Uint32 *buf;
SDL_Texture *bkg;
SDL_Texture *bkg2;
SDL_Texture *bkgTgt;
SDL_Texture *bkgTgt2;
SDL_Texture *brightnessOverlay;
} GraphicsDevice;

Expand Down
51 changes: 40 additions & 11 deletions src/cdogs/grafx_bg.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
#include "objs.h"
#include "pickup.h"
#include "quick_play.h"
#include "texture.h"
#include "triggers.h"


void GrafxMakeRandomBackground(
GraphicsDevice *device,
CampaignOptions *co, struct MissionOptions *mo, Map *map)
Expand All @@ -59,38 +61,60 @@ void GrafxMakeRandomBackground(
}

static void DrawBackground(
GraphicsDevice *g, SDL_Texture *t, DrawBuffer *buffer, Map *map,
const HSV tint, const struct vec2 pos, GrafxDrawExtra *extra);
GraphicsDevice *g, SDL_Texture *tTgt, SDL_Texture *t, DrawBuffer *buffer,
Map *map, const HSV tint, const struct vec2 pos, GrafxDrawExtra *extra);
void GrafxDrawBackground(
GraphicsDevice *g, DrawBuffer *buffer,
const HSV tint, const struct vec2 pos, GrafxDrawExtra *extra)
{
if (SDL_SetRenderTarget(g->gameWindow.renderer, g->bkg) != 0)
SDL_RendererInfo ri;
if (SDL_GetRendererInfo(g->gameWindow.renderer, &ri) != 0)
{
LOG(LM_MAIN, LL_ERROR, "cannot set render target: %s", SDL_GetError());
LOG(LM_GFX, LL_ERROR, "cannot set render target: %s", SDL_GetError());
}
else
{
if (!(ri.flags & SDL_RENDERER_TARGETTEXTURE))
{
LOG(LM_GFX, LL_ERROR,
"renderer does not support render to texture");
}
}
if (SDL_SetRenderTarget(g->gameWindow.renderer, g->bkgTgt) != 0)
{
LOG(LM_GFX, LL_ERROR, "cannot set render target: %s", SDL_GetError());
}
if (g->cachedConfig.SecondWindow)
{
if (SDL_SetRenderTarget(g->gameWindow.renderer, g->bkg2) != 0)
if (SDL_SetRenderTarget(g->secondWindow.renderer, g->bkgTgt2) != 0)
{
LOG(LM_MAIN, LL_ERROR, "cannot set render target: %s",
LOG(LM_GFX, LL_ERROR, "cannot set render target: %s",
SDL_GetError());
}
DrawBackground(
g, g->bkg, buffer, &gMap, tint,
g, g->bkgTgt, g->bkg, buffer, &gMap, tint,
svec2(pos.x - g->cachedConfig.Res.x / 2, pos.y), extra);
DrawBackground(
g, g->bkg2, buffer, &gMap, tint,
g, g->bkgTgt2, g->bkg2, buffer, &gMap, tint,
svec2(pos.x + g->cachedConfig.Res.x / 2, pos.y), extra);
if (SDL_SetRenderTarget(g->secondWindow.renderer, NULL) != 0)
{
LOG(LM_GFX, LL_ERROR, "cannot set render target: %s",
SDL_GetError());
}
}
else
{
DrawBackground(g, g->bkg, buffer, &gMap, tint, pos, extra);
DrawBackground(g, g->bkgTgt, g->bkg, buffer, &gMap, tint, pos, extra);
}
if (SDL_SetRenderTarget(g->gameWindow.renderer, NULL) != 0)
{
LOG(LM_GFX, LL_ERROR, "cannot set render target: %s", SDL_GetError());
}
}
static void DrawBackground(
GraphicsDevice *g, SDL_Texture *t, DrawBuffer *buffer, Map *map,
const HSV tint, const struct vec2 pos, GrafxDrawExtra *extra)
GraphicsDevice *g, SDL_Texture *tTgt, SDL_Texture *t, DrawBuffer *buffer,
Map *map, const HSV tint, const struct vec2 pos, GrafxDrawExtra *extra)
{
DrawBufferSetFromMap(buffer, map, pos, X_TILES);
DrawBufferDraw(buffer, svec2i_zero(), extra);
Expand All @@ -102,6 +126,11 @@ static void DrawBackground(
LOG(LM_GFX, LL_ERROR, "cannot set background tint: %s",
SDL_GetError());
}
if (SDL_SetTextureColorMod(tTgt, mask.r, mask.g, mask.b) != 0)
{
LOG(LM_GFX, LL_ERROR, "cannot set background tint: %s",
SDL_GetError());
}
}

void GrafxRedrawBackground(GraphicsDevice *g, const struct vec2 pos)
Expand Down
31 changes: 19 additions & 12 deletions src/cdogs/window_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,32 @@


bool WindowContextCreate(
WindowContext *wc, const struct vec2i windowSize, const int sdlFlags,
const char *title, SDL_Surface *icon, const struct vec2i rendererLogicalSize)
WindowContext *wc, const Rect2i windowDim, const int windowFlags,
const char *title, SDL_Surface *icon,
const struct vec2i rendererLogicalSize)
{
CArrayInit(&wc->texturesBkg, sizeof(SDL_Texture *));
CArrayInit(&wc->textures, sizeof(SDL_Texture *));

LOG(LM_GFX, LL_DEBUG, "creating window %dx%d flags(%X)",
windowSize.x, windowSize.y, sdlFlags);
if (SDL_CreateWindowAndRenderer(
windowSize.x, windowSize.y, sdlFlags,
&wc->window, &wc->renderer) == -1 ||
wc->window == NULL || wc->renderer == NULL)
LOG(LM_GFX, LL_DEBUG, "creating window (%d, %d) %dx%d flags(%X)",
windowDim.Pos.x, windowDim.Pos.y, windowDim.Size.x, windowDim.Size.y,
windowFlags);
wc->window = SDL_CreateWindow(
title, windowDim.Pos.x, windowDim.Pos.y,
windowDim.Size.x, windowDim.Size.y, windowFlags);
if (wc->window == NULL)
{
LOG(LM_GFX, LL_ERROR, "cannot create window or renderer: %s",
SDL_GetError());
LOG(LM_GFX, LL_ERROR, "cannot create window: %s", SDL_GetError());
return false;
}
wc->renderer = SDL_CreateRenderer(
wc->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
if (wc->renderer == NULL)
{
LOG(LM_GFX, LL_ERROR, "cannot create renderer: %s", SDL_GetError());
return false;
}
LOG(LM_GFX, LL_DEBUG, "setting title(%s) and icon", title);
SDL_SetWindowTitle(wc->window, title);
LOG(LM_GFX, LL_DEBUG, "setting icon");
SDL_SetWindowIcon(wc->window, icon);

if (SDL_RenderSetLogicalSize(
Expand Down
5 changes: 3 additions & 2 deletions src/cdogs/window_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ typedef struct
} WindowContext;

bool WindowContextCreate(
WindowContext *wc, const struct vec2i windowSize, const int sdlFlags,
const char *title, SDL_Surface *icon, const struct vec2i rendererLogicalSize);
WindowContext *wc, const Rect2i windowDim, const int windowFlags,
const char *title, SDL_Surface *icon,
const struct vec2i rendererLogicalSize);
void WindowContextDestroy(WindowContext *wc);
void WindowContextDestroyTextures(WindowContext *wc);

Expand Down
10 changes: 4 additions & 6 deletions src/cdogsed/cdogsed.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,13 @@ static void MakeBackground(const bool changedMission)
}

// Clear background first
memset(ec.g->buf, 0, GraphicsGetMemSize(&ec.g->cachedConfig));
BlitClearBuf(ec.g);
GrafxDrawExtra extra;
extra.guideImage = brush.GuideImageSurface;
extra.guideImageAlpha = brush.GuideImageAlpha;

DrawBufferTerminate(&sDrawBuffer);
DrawBufferInit(
&sDrawBuffer, svec2i(X_TILES, Y_TILES), &gGraphicsDevice, true);
DrawBufferInit(&sDrawBuffer, svec2i(X_TILES, Y_TILES), &gGraphicsDevice);
GrafxMakeBackground(
ec.g, &sDrawBuffer, &gCampaign, &gMission, &gMap,
tintNone, true, ec.camera, &extra);
Expand Down Expand Up @@ -191,7 +190,7 @@ static void Display(HandleInputResult result)
if (result.RemakeBg || brush.IsGuideImageNew)
{
// Clear background first
memset(ec.g->buf, 0, GraphicsGetMemSize(&ec.g->cachedConfig));
BlitClearBuf(ec.g);
brush.IsGuideImageNew = false;
GrafxDrawExtra extra;
extra.guideImage = brush.GuideImageSurface;
Expand Down Expand Up @@ -1378,8 +1377,7 @@ 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, true);
DrawBufferInit(&sDrawBuffer, svec2i(X_TILES, Y_TILES), &gGraphicsDevice);

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

0 comments on commit 5f9e5c6

Please sign in to comment.