Skip to content

Commit

Permalink
Change GraphicsDevice to own blitting screen
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Jul 16, 2013
1 parent fcf0007 commit d7436a1
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 66 deletions.
25 changes: 10 additions & 15 deletions src/cdogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void CampaignIntro(void *bkg)

debug(D_NORMAL, "\n");

memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memcpy(gGraphicsDevice.buf, bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));

y = gGraphicsDevice.cachedConfig.ResolutionWidth / 4;

Expand All @@ -213,7 +213,7 @@ void MissionBriefing(void *bkg)
char s[512];
int i, y;

memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memcpy(gGraphicsDevice.buf, bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));

y = gGraphicsDevice.cachedConfig.ResolutionWidth / 4;

Expand Down Expand Up @@ -425,7 +425,7 @@ void Bonuses(void)

void MissionSummary(void *bkg)
{
memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memcpy(gGraphicsDevice.buf, bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));

Bonuses();

Expand All @@ -443,7 +443,7 @@ void ShowScore(void *bkg, int score1, int score2)
{
char s[10];

memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memcpy(gGraphicsDevice.buf, bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));

debug(D_NORMAL, "\n");

Expand Down Expand Up @@ -473,7 +473,7 @@ void ShowScore(void *bkg, int score1, int score2)

void FinalScore(void *bkg, int score1, int score2)
{
memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memcpy(gGraphicsDevice.buf, bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));

#define IS_DRAW "It's a draw!"
#define IS_WINNER "Winner!"
Expand Down Expand Up @@ -531,7 +531,7 @@ void Victory(void *bkg)
int x, i;
const char *s;

memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memcpy(gGraphicsDevice.buf, bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));

x = 160 - CDogsTextWidth(CONGRATULATIONS) / 2;
CDogsTextStringAt(x, 100, CONGRATULATIONS);
Expand Down Expand Up @@ -831,13 +831,10 @@ void *MakeBkg(void)
int i;
TranslationTable randomTintTable;

CMALLOC(bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));

SetupQuickPlayCampaign(&gCampaign.Setting);
gCampaign.seed = rand();
SetupMission(0, 1, &gCampaign);
SetupMap();
SetDstScreen(bkg);
SetBuffer(1024, 768, buffer, X_TILES);
FixBuffer(buffer, 255);
DrawBuffer(buffer, 0);
Expand All @@ -846,7 +843,7 @@ void *MakeBkg(void)
FreeTriggersAndWatches();
gCampaign.seed = gConfig.Game.RandomSeed;

p = bkg;
p = gGraphicsDevice.buf;
SetPaletteRanges(15, 12, 10, 0);
BuildTranslationTables();
SetRandomTintTable(&randomTintTable, 256);
Expand All @@ -855,16 +852,14 @@ void *MakeBkg(void)
p[i] = randomTintTable[p[i] & 0xFF];
}

CMALLOC(bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memcpy(bkg, gGraphicsDevice.buf, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
return bkg;
}

void MainLoop(credits_displayer_t *creditsDisplayer, custom_campaigns_t *campaigns)
{
unsigned char *my_screen;

void *bkg = MakeBkg();
CCALLOC(my_screen, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
SetDstScreen(my_screen);

while (MainMenu(bkg, creditsDisplayer, campaigns))
{
Expand Down Expand Up @@ -892,7 +887,6 @@ void MainLoop(credits_displayer_t *creditsDisplayer, custom_campaigns_t *campaig
}
}
debug(D_NORMAL, ">> Leaving Main Game Loop\n");
CFREE(my_screen);
CFREE(bkg);
}

Expand Down Expand Up @@ -1101,6 +1095,7 @@ int main(int argc, char *argv[])
getchar();
}

GraphicsInit(&gGraphicsDevice);
GraphicsInitialize(&gGraphicsDevice, &gConfig.Graphics, forceResolution);
if (!gGraphicsDevice.IsInitialized)
{
Expand Down
6 changes: 3 additions & 3 deletions src/cdogs/automap.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static void DisplayPlayer(TActor * player)

void DrawCross(TTileItem * t, unsigned char color)
{
unsigned char *scr = GetDstScreen();
unsigned char *scr = gGraphicsDevice.buf;

scr += MAP_XOFFS + MAP_FACTOR * t->x / TILE_WIDTH;
scr += (MAP_YOFFS + MAP_FACTOR * t->y / TILE_HEIGHT) * gGraphicsDevice.cachedConfig.ResolutionWidth;
Expand All @@ -125,7 +125,7 @@ static void DisplayObjective(TTileItem * t, int objectiveIndex)

static void DisplayExit(void)
{
unsigned char *scr = GetDstScreen();
unsigned char *scr = gGraphicsDevice.buf;
int i;
int x1, x2, y1, y2;

Expand Down Expand Up @@ -246,7 +246,7 @@ void DisplayAutoMap(int showAll)
TTileItem *t;
int obj;

screen = p = GetDstScreen();
screen = p = gGraphicsDevice.buf;
// Draw faded green overlay
for (x = 0;
x < gGraphicsDevice.cachedConfig.ResolutionWidth * gGraphicsDevice.cachedConfig.ResolutionHeight;
Expand Down
24 changes: 7 additions & 17 deletions src/cdogs/blit.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@
#include "pics.h" /* for gPalette */
#include "utils.h" /* for debug() */

unsigned char *r_screen;


int clipleft = 0, cliptop = 0, clipright = 0, clipbottom = 0;

Expand Down Expand Up @@ -101,7 +99,7 @@ void Blit(int x, int y, Pic *pic, void *table, int mode)
current += pic->w - j;
break;
}
target = r_screen + yoff + xoff;
target = gGraphicsDevice.buf + yoff + xoff;
if ((mode & BLIT_TRANSPARENT && *current) || !(mode & BLIT_TRANSPARENT)){
if (table){
if (mode & BLIT_BACKGROUND)
Expand Down Expand Up @@ -166,17 +164,6 @@ void CDogsSetClip(int left, int top, int right, int bottom)
return;
}

void SetDstScreen(unsigned char *screen)
{
r_screen = screen;
return;
}

unsigned char *GetDstScreen(void)
{
return r_screen;
}

#define PixelIndex(x, y, w) (y * w + x)

static INLINE
Expand Down Expand Up @@ -268,9 +255,12 @@ void CopyToScreen(void)
}

if (scalef == 1)
memcpy(pScreen, r_screen, scr_size); /* 1 -> 1 */
else {
Scale8(pScreen, r_screen, scr_w, scr_h, scalef);
{
memcpy(pScreen, gGraphicsDevice.buf, scr_size); /* 1 -> 1 */
}
else
{
Scale8(pScreen, gGraphicsDevice.buf, scr_w, scr_h, scalef);
}

SDL_UnlockSurface(gGraphicsDevice.screen);
Expand Down
2 changes: 0 additions & 2 deletions src/cdogs/blit.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ void BlitRectangle(
int flags);

void CDogsSetClip(int left, int top, int right, int bottom);
void SetDstScreen(unsigned char *screen);
unsigned char *GetDstScreen(void);
void CopyToScreen(void);
void AltScrCopy(void);
void CDogsSetPalette(void *palette);
Expand Down
2 changes: 1 addition & 1 deletion src/cdogs/drawtools.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

void Draw_Point (const int x, const int y, const unsigned char c)
{
unsigned char *screen = GetDstScreen();
unsigned char *screen = gGraphicsDevice.buf;

//debug("(%d, %d)\n", x, y);

Expand Down
2 changes: 1 addition & 1 deletion src/cdogs/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void ShakeScreen(int amount)
void BlackLine(void)
{
int i;
unsigned char *p = GetDstScreen();
unsigned char *p = gGraphicsDevice.buf;

p += (gGraphicsDevice.cachedConfig.ResolutionWidth / 2) - 1;
for (i = 0; i < gGraphicsDevice.cachedConfig.ResolutionHeight; i++)
Expand Down
23 changes: 14 additions & 9 deletions src/cdogs/grafx.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,7 @@ static int ValidMode(unsigned int w, unsigned int h)
}


GraphicsDevice gGraphicsDevice =
{
0,
0,
NULL,
{
0, 0, 0, 0, 0, 0
}
};
GraphicsDevice gGraphicsDevice;

int IsRestartRequiredForConfig(GraphicsDevice *device, GraphicsConfig *config)
{
Expand All @@ -141,6 +133,15 @@ int IsRestartRequiredForConfig(GraphicsDevice *device, GraphicsConfig *config)
device->cachedConfig.ScaleFactor != config->ScaleFactor;
}

void GraphicsInit(GraphicsDevice *device)
{
device->IsInitialized = 0;
device->IsWindowInitialized = 0;
device->screen = NULL;
memset(&device->cachedConfig, 0, sizeof device->cachedConfig);
device->buf = NULL;
}

/* Initialises the video subsystem.
Note: dynamic resolution change is not supported. */
Expand Down Expand Up @@ -210,6 +211,9 @@ void GraphicsInitialize(GraphicsDevice *device, GraphicsConfig *config, int forc
return;
}

CFREE(device->buf);
CCALLOC(device->buf, GraphicsGetMemSize(config));

if (!device->IsWindowInitialized)
{
/* only do this the first time */
Expand Down Expand Up @@ -243,6 +247,7 @@ void GraphicsTerminate(GraphicsDevice *device)
debug(D_NORMAL, "Shutting down video...\n");
SDL_FreeSurface(device->screen);
SDL_VideoQuit();
CFREE(device->buf);
}

int GraphicsGetMemSize(GraphicsConfig *config)
Expand Down
2 changes: 2 additions & 0 deletions src/cdogs/grafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ typedef struct
int IsWindowInitialized;
SDL_Surface *screen;
GraphicsConfig cachedConfig;
unsigned char *buf;
} GraphicsDevice;

extern GraphicsDevice gGraphicsDevice;
Expand All @@ -80,6 +81,7 @@ typedef struct {
unsigned int w, h;
} GFX_Mode;

void GraphicsInit(GraphicsDevice *device);
void GraphicsInitialize(GraphicsDevice *device, GraphicsConfig *config, int force);
void GraphicsTerminate(GraphicsDevice *device);
int GraphicsGetMemSize(GraphicsConfig *config);
Expand Down
4 changes: 2 additions & 2 deletions src/cdogs/hiscores.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void DisplayAllTimeHighScores(void *bkg)
while (idx < MAX_ENTRY && allTimeHigh[idx].score > 0)
{
memcpy(
GetDstScreen(),
gGraphicsDevice.buf,
bkg,
GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
idx = DisplayPage(
Expand All @@ -221,7 +221,7 @@ void DisplayTodaysHighScores(void *bkg)

while (idx < MAX_ENTRY && todaysHigh[idx].score > 0)
{
memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memcpy(gGraphicsDevice.buf, bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
idx = DisplayPage("Today's highest score:", idx, todaysHigh,
gPlayer1Data.today,
gOptions.twoPlayers ? gPlayer2Data.
Expand Down
4 changes: 2 additions & 2 deletions src/cdogs/hud.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ void DrawHealth(int health, int maxHealth, int flags)
int barTop = 4 * CDogsTextHeight() - 1;
int barHeight = CDogsTextHeight() + 1;
BlitRectangle(
GetDstScreen(),
gGraphicsDevice.buf,
barLeft, barTop,
barWidth,
barHeight,
backColour,
BLIT_FLAG_ROUNDED);
BlitRectangle(
GetDstScreen(),
gGraphicsDevice.buf,
barLeft + 1, barTop + 1,
MAX(0, healthBarWidth - 2),
barHeight - 2,
Expand Down
14 changes: 5 additions & 9 deletions src/cdogsed.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ void Display(int idx, int xc, int yc, int key)
int i;

SetSecondaryMouseRects(NULL);
memset(GetDstScreen(), 58, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memset(gGraphicsDevice.buf, 58, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));

sprintf(s, "Key: 0x%x", key);
CDogsTextStringAt(270, 190, s);
Expand Down Expand Up @@ -1220,7 +1220,7 @@ static void Save(int asCode)
strcpy(filename, lastFile);
for (;;)
{
memset(GetDstScreen(), 58, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memset(gGraphicsDevice.buf, 58, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
CDogsTextStringAt(125, 50, "Save as:");
CDogsTextGoto(125, 50 + CDogsTextHeight());
CDogsTextChar('\020');
Expand Down Expand Up @@ -1267,7 +1267,7 @@ static int ConfirmQuit(void)
{
int c;

memset(GetDstScreen(), 58, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memset(gGraphicsDevice.buf, 58, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
CDogsTextStringAt(80, 50, "Campaign has been modified, but not saved");
CDogsTextStringAt(110, 50 + TH, "Quit anyway? (Y/N)");
CopyToScreen();
Expand Down Expand Up @@ -1557,6 +1557,7 @@ int main(int argc, char *argv[])

ConfigLoadDefault(&gConfig);
ConfigLoad(&gConfig, GetConfigFilePath(CONFIG_FILE));
GraphicsInit(&gGraphicsDevice);
GraphicsInitialize(&gGraphicsDevice, &gConfig.Graphics, 0);
if (!gGraphicsDevice.IsInitialized)
{
Expand All @@ -1566,15 +1567,10 @@ int main(int argc, char *argv[])

CDogsSetPalette(gPalette);

CCALLOC(my_screen, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
SetDstScreen(my_screen);

KeyInit(&gKeyboard);
InitMouse();
EditCampaign();

CFREE(my_screen);

//CDogsTextMode();
GraphicsTerminate(&gGraphicsDevice);
exit(EXIT_SUCCESS);
}
2 changes: 1 addition & 1 deletion src/charsed.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ static void Display(CampaignSetting *setting, int idx, int xc, int yc)
const TBadGuy *b;
int i;

memset(GetDstScreen(), 74, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memset(gGraphicsDevice.buf, 74, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));

sprintf(s, "%d/%d", setting->characterCount, MAX_CHARACTERS);
CDogsTextStringAt(10, 190, s);
Expand Down
2 changes: 1 addition & 1 deletion src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void MenuLoop(MenuSystem *menu)
if (menu->bkg != NULL)
{
memcpy(
GetDstScreen(),
gGraphicsDevice.buf,
menu->bkg,
GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
}
Expand Down
2 changes: 1 addition & 1 deletion src/password.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ static int EnterCode(void *bkg, const char *password)
{
int cmd;
InputPoll(&gJoysticks, &gKeyboard);
memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
memcpy(gGraphicsDevice.buf, bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
cmd = GetMenuCmd();
if (!PasswordEntry(cmd, buffer))
{
Expand Down
Loading

0 comments on commit d7436a1

Please sign in to comment.