Replaces many hard-coded numbers with references to screen->w and screen->h and adds the constants screenWidth, screenHeight, and viewBorder. This is a step towards supporting multiple resolutions.

This commit is contained in:
Julian 2012-03-11 23:16:56 -04:00
parent 8ebc96b97c
commit 0d189c858c
11 changed files with 90 additions and 81 deletions

View File

@ -26,19 +26,19 @@ object enemy[MAX_ALIENS];
static bool placeAlien(object *theEnemy) static bool placeAlien(object *theEnemy)
{ {
if (rand() % 2 == 0) if (rand() % 2 == 0)
theEnemy->x = rrand(800, 1600); theEnemy->x = rrand(screen->w, screen->w * 2);
else else
theEnemy->x = rrand(-800, 0); theEnemy->x = rrand(-screen->w, 0);
if (rand() % 2 == 0) if (rand() % 2 == 0)
theEnemy->y = rrand(600, 1200); theEnemy->y = rrand(screen->h, screen->h * 2);
else else
theEnemy->y = rrand(-600, 0); theEnemy->y = rrand(-screen->h, 0);
if (currentGame.area == 24) if (currentGame.area == 24)
{ {
theEnemy->x = 800; theEnemy->x = screen->w;
theEnemy->y = rrand(200, 400); theEnemy->y = rrand((int)((1/3) * screen->h), (int)((2/3) * screen->h));
} }
for (int i = 0 ; i < MAX_ALIENS ; i++) for (int i = 0 ; i < MAX_ALIENS ; i++)
@ -396,8 +396,8 @@ static void getPreDefinedAliens()
if (currentGame.area == 5) if (currentGame.area == 5)
{ {
enemy[WC_BOSS].target = &player; enemy[WC_BOSS].target = &player;
enemy[WC_BOSS].x = -400; enemy[WC_BOSS].x = (int)(-screen->w / 2);
enemy[WC_BOSS].y = 300; enemy[WC_BOSS].y = (int)(screen->h / 2);
enemy[13].owner = &enemy[WC_BOSS]; enemy[13].owner = &enemy[WC_BOSS];
enemy[13].target = &player; enemy[13].target = &player;
@ -412,8 +412,8 @@ static void getPreDefinedAliens()
else if ((currentGame.area == 11) || (currentGame.area == 14)) else if ((currentGame.area == 11) || (currentGame.area == 14))
{ {
enemy[WC_BOSS].target = &player; enemy[WC_BOSS].target = &player;
enemy[WC_BOSS].x = -400; enemy[WC_BOSS].x = (int)(-screen->w / 2);
enemy[WC_BOSS].y = 300; enemy[WC_BOSS].y = (int)(screen->h / 2);
enemy[13].owner = &enemy[WC_BOSS]; enemy[13].owner = &enemy[WC_BOSS];
enemy[13].target = &player; enemy[13].target = &player;
@ -475,14 +475,14 @@ static void addFriendly(int type)
enemy[type].active = true; enemy[type].active = true;
if (rand() % 2 == 0) if (rand() % 2 == 0)
enemy[type].x = rrand(400, 550); enemy[type].x = rrand((int)(screen->w / 2), (int)(screen->w / 2) + 150);
else else
enemy[type].x = rrand(250, 400); enemy[type].x = rrand((int)(screen->w / 2) - 150, (int)(screen->w / 2));
if (rand() % 2 == 0) if (rand() % 2 == 0)
enemy[type].y = rrand(300, 450); enemy[type].y = rrand((int)(screen->h / 2), (int)(screen->h / 2) + 150);
else else
enemy[type].y = rrand(150, 300); enemy[type].y = rrand((int)(screen->h / 2) - 150, (int)(screen->h / 2));
if (type == FR_PHOEBE) if (type == FR_PHOEBE)
enemy[type].classDef = CD_PHOEBE; enemy[type].classDef = CD_PHOEBE;
@ -596,8 +596,8 @@ void initAliens()
{ {
enemy[WC_KLINE].flags |= FL_IMMORTAL | FL_NOFIRE | FL_NOMOVE; enemy[WC_KLINE].flags |= FL_IMMORTAL | FL_NOFIRE | FL_NOMOVE;
enemy[WC_KLINE].x = 600; enemy[WC_KLINE].x = (int)(screen->w * (2 / 3));
enemy[WC_KLINE].y = 300; enemy[WC_KLINE].y = (int)(screen->h / 2);
enemy[WC_KLINE].deathCounter = -250; enemy[WC_KLINE].deathCounter = -250;
enemy[WC_KLINE].maxShield = 1500; enemy[WC_KLINE].maxShield = 1500;
@ -1188,7 +1188,7 @@ void doAliens()
limitCharAdd(&theEnemy->hit, -1, 0, 100); limitCharAdd(&theEnemy->hit, -1, 0, 100);
if ((theEnemy->x + theEnemy->image[0]->w > 0) && (theEnemy->x < 800) && (theEnemy->y + theEnemy->image[0]->h > 0) && (theEnemy->y < 600)) if ((theEnemy->x + theEnemy->image[0]->w > 0) && (theEnemy->x < screen->w) && (theEnemy->y + theEnemy->image[0]->h > 0) && (theEnemy->y < screen->h))
{ {
if ((!(theEnemy->flags & FL_DISABLED)) && (theEnemy->classDef != CD_ASTEROID) && (theEnemy->classDef != CD_ASTEROID2)) if ((!(theEnemy->flags & FL_DISABLED)) && (theEnemy->classDef != CD_ASTEROID) && (theEnemy->classDef != CD_ASTEROID2))
addEngine(theEnemy); addEngine(theEnemy);
@ -1207,7 +1207,7 @@ void doAliens()
else else
{ {
theEnemy->shield--; theEnemy->shield--;
if ((theEnemy->x > 0) && (theEnemy->x < 800) && (theEnemy->y > 0) && (theEnemy->y < 600)) if ((theEnemy->x > 0) && (theEnemy->x < screen->w) && (theEnemy->y > 0) && (theEnemy->y < screen->h))
{ {
blit(theEnemy->image[theEnemy->face], (int)theEnemy->x, (int)theEnemy->y); blit(theEnemy->image[theEnemy->face], (int)theEnemy->x, (int)theEnemy->y);
addExplosion(theEnemy->x + (rand() % theEnemy->image[0]->w), theEnemy->y + (rand() % theEnemy->image[0]->h), E_BIG_EXPLOSION); addExplosion(theEnemy->x + (rand() % theEnemy->image[0]->w), theEnemy->y + (rand() % theEnemy->image[0]->h), E_BIG_EXPLOSION);

View File

@ -305,3 +305,8 @@ extern const char *systemNames[];
extern const char *systemBackground[]; extern const char *systemBackground[];
static const signed char rate2reload[6] = {15, 15, 13, 11, 9, 7}; static const signed char rate2reload[6] = {15, 15, 13, 11, 9, 7};
const int screenWidth = 800;
const int screenHeight = 600;
const int viewBorder = 100;

View File

@ -233,7 +233,7 @@ int mainGameLoop()
if (engine.paused) if (engine.paused)
{ {
textSurface(22, "PAUSED", -1, 300, FONT_WHITE); textSurface(22, "PAUSED", -1, (int)(screen->h / 2), FONT_WHITE);
blitText(22); blitText(22);
updateScreen(); updateScreen();

View File

@ -718,8 +718,8 @@ void doStarfield()
else if (star[i].speed == 1) else if (star[i].speed == 1)
color = darkGrey; color = darkGrey;
wrapFloat(&(star[i].x += (engine.ssx * star[i].speed)), 0, 799); wrapFloat(&(star[i].x += (engine.ssx * star[i].speed)), 0, screen->w - 1);
wrapFloat(&(star[i].y += (engine.ssy * star[i].speed)), 0, 599); wrapFloat(&(star[i].y += (engine.ssy * star[i].speed)), 0, screen->h - 1);
putpixel(screen, (int)star[i].x, (int)star[i].y, color); putpixel(screen, (int)star[i].x, (int)star[i].y, color);
r.x = (int)star[i].x; r.x = (int)star[i].x;

View File

@ -34,10 +34,10 @@ void initVars()
{ {
srand(time(NULL)); srand(time(NULL));
for (int i = 0 ; i < 200 ; i++) for (int i = 0 ; i < (int)(screen->w * screen->h / 2400) ; i++)
{ {
star[i].x = rand() % 800; star[i].x = rand() % screen->w;
star[i].y = rand() % 600; star[i].y = rand() % screen->h;
star[i].speed = 1 + (rand() % 3); star[i].speed = 1 + (rand() % 3);
} }
@ -188,12 +188,12 @@ void initSystem()
SDL_WM_SetIcon(loadImage("gfx/alienDevice.png"), NULL); SDL_WM_SetIcon(loadImage("gfx/alienDevice.png"), NULL);
if (engine.fullScreen) if (engine.fullScreen)
screen = SDL_SetVideoMode(800, 600, 0, SDL_DOUBLEBUF|SDL_HWPALETTE|SDL_FULLSCREEN); screen = SDL_SetVideoMode(screenWidth, screenHeight, 0, SDL_DOUBLEBUF|SDL_HWPALETTE|SDL_FULLSCREEN);
else else
screen = SDL_SetVideoMode(800, 600, 0, SDL_DOUBLEBUF|SDL_HWPALETTE); screen = SDL_SetVideoMode(screenWidth, screenHeight, 0, SDL_DOUBLEBUF|SDL_HWPALETTE);
if (screen == NULL) { if (screen == NULL) {
printf("Couldn't set 800x600x16 video mode: %s\n", SDL_GetError()); printf("Couldn't set %ix%ix16 video mode: %s\n", screenWidth, screenHeight, SDL_GetError());
exit(1); exit(1);
} }

View File

@ -34,8 +34,8 @@ static void doCursor()
{ {
getPlayerInput(); getPlayerInput();
limitInt(&engine.cursor_x, 10, 790); limitInt(&engine.cursor_x, 10, screen->w - 10);
limitInt(&engine.cursor_y, 10, 590); limitInt(&engine.cursor_y, 10, screen->h - 10);
blit(shape[0], engine.cursor_x, engine.cursor_y); blit(shape[0], engine.cursor_x, engine.cursor_y);
} }
@ -442,7 +442,7 @@ static void showOptions(SDL_Surface *optionsSurface)
#if LINUX #if LINUX
SDL_WM_ToggleFullScreen(screen); SDL_WM_ToggleFullScreen(screen);
#else #else
screen = SDL_SetVideoMode(800, 600, 0, SDL_DOUBLEBUF|SDL_HWPALETTE|SDL_FULLSCREEN); screen = SDL_SetVideoMode(screen->w, screen->h, 0, SDL_DOUBLEBUF|SDL_HWPALETTE|SDL_FULLSCREEN);
drawBackground(); drawBackground();
flushBuffer(); flushBuffer();
#endif #endif
@ -457,7 +457,7 @@ static void showOptions(SDL_Surface *optionsSurface)
#if LINUX #if LINUX
SDL_WM_ToggleFullScreen(screen); SDL_WM_ToggleFullScreen(screen);
#else #else
screen = SDL_SetVideoMode(800, 600, 0, SDL_DOUBLEBUF|SDL_HWPALETTE); screen = SDL_SetVideoMode(screen->w, screen->h, 0, SDL_DOUBLEBUF|SDL_HWPALETTE);
drawBackground(); drawBackground();
flushBuffer(); flushBuffer();
#endif #endif
@ -503,7 +503,8 @@ int galaxyMap()
char string[25]; char string[25];
engine.cursor_x = engine.cursor_y = 500; engine.cursor_x = (int)(screen->w / 2);
engine.cursor_y = (int)(screen->h / 2);
shape[0] = loadImage("gfx/cursor.png"); shape[0] = loadImage("gfx/cursor.png");
// Icons 1 - 29 // Icons 1 - 29
@ -631,9 +632,9 @@ int galaxyMap()
r.x = 0; r.x = 0;
r.y = 0; r.y = 0;
r.h = 600; r.h = screen->h;
r.w = 1; r.w = 1;
for (int i = 40 ; i < 800 ; i+= 40) for (int i = 40 ; i < screen->w ; i+= 40)
{ {
r.x = i; r.x = i;
SDL_FillRect(screen, &r, darkerBlue); SDL_FillRect(screen, &r, darkerBlue);
@ -642,8 +643,8 @@ int galaxyMap()
r.x = 0; r.x = 0;
r.y = 0; r.y = 0;
r.h = 1; r.h = 1;
r.w = 800; r.w = screen->w;
for (int i = 40 ; i < 600 ; i+= 40) for (int i = 40 ; i < screen->h ; i+= 40)
{ {
r.y = i; r.y = i;
SDL_FillRect(screen, &r, darkerBlue); SDL_FillRect(screen, &r, darkerBlue);

View File

@ -107,28 +107,31 @@ static void doTargetArrow()
int distX = (int)(enemy[engine.targetIndex].x - player.x); int distX = (int)(enemy[engine.targetIndex].x - player.x);
int distY = (int)(enemy[engine.targetIndex].y - player.y); int distY = (int)(enemy[engine.targetIndex].y - player.y);
if (distY < -300) // TODO: This is not very good. It assumes the player is always at (400,300),
// which is not always true, resulting in the arrows sometimes not appearing when
// the enemy is not visible and sometimes appearing when the enemy is visible.
if (distY < (int)(-screen->h / 2))
engine.targetArrow = 36; engine.targetArrow = 36;
if (distY > 300) if (distY > (int)(screen->h / 2))
engine.targetArrow = 40; engine.targetArrow = 40;
if (distX < -400) if (distX < (int)(-screen->w / 2))
engine.targetArrow = 42; engine.targetArrow = 42;
if (distX > 400) if (distX > (int)(screen->w / 2))
engine.targetArrow = 38; engine.targetArrow = 38;
if ((distY < -300) && (distX > 400)) if ((distY < (int)(-screen->h / 2)) && (distX > (int)(screen->w / 2)))
engine.targetArrow = 37; engine.targetArrow = 37;
if ((distY > 300) && (distX > 400)) if ((distY > (int)(screen->h / 2)) && (distX > (int)(screen->w / 2)))
engine.targetArrow = 39; engine.targetArrow = 39;
if ((distY > 300) && (distX < -400)) if ((distY > (int)(screen->h / 2)) && (distX < (int)(-screen->w / 2)))
engine.targetArrow = 41; engine.targetArrow = 41;
if ((distY < -300) && (distX < -400)) if ((distY < (int)(-screen->h / 2)) && (distX < (int)(-screen->w / 2)))
engine.targetArrow = 43; engine.targetArrow = 43;
if (engine.targetArrow != -1) if (engine.targetArrow != -1)

View File

@ -672,14 +672,14 @@ bool missionFailed()
static void drawBriefScreen() static void drawBriefScreen()
{ {
SDL_Rect r = {0, 0, 800, 2}; SDL_Rect r = {0, 0, screen->w, 2};
for (int i = 0 ; i < 120 ; i++) for (int i = 0 ; i < (int)(screen->h / 4) - 30 ; i++)
{ {
r.y = (i * 2) + 60; r.y = (i * 2) + 62; // Not a typo; a black gap is left in the middle if it's 60.
SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 0, i, 0));
r.y = (screen->h - (i * 2) - 60);
SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 0, i, 0)); SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 0, i, 0));
r.y = (300 + (i * 2));
SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 0, (120 - i), 0));
} }
blevelRect(140, 70, 500, 20, 0x00, 0x77, 0x00); blevelRect(140, 70, 500, 20, 0x00, 0x77, 0x00);

View File

@ -28,8 +28,8 @@ Initialises the player for a new game.
void initPlayer() void initPlayer()
{ {
player.active = true; player.active = true;
player.x = 200; player.x = (int)(screen->w / 2);
player.y = 200; player.y = (int)(screen->h / 2);
player.speed = 2; player.speed = 2;
player.maxShield = (25 * currentGame.shieldUnits); player.maxShield = (25 * currentGame.shieldUnits);
player.systemPower = player.maxShield; player.systemPower = player.maxShield;
@ -217,8 +217,8 @@ void doPlayer()
if (engine.done == 0) if (engine.done == 0)
{ {
limitFloat(&player.x, 100, 700); limitFloat(&player.x, viewBorder, screen->w - viewBorder);
limitFloat(&player.y, 100, 500); limitFloat(&player.y, viewBorder, screen->h - viewBorder);
} }
if (player.shield > engine.lowShield) if (player.shield > engine.lowShield)
@ -362,9 +362,9 @@ void leaveSector()
{ {
player.x += engine.ssx; player.x += engine.ssx;
engine.ssx -= 1; engine.ssx -= 1;
if (player.y > 300) if (player.y > screen->h)
player.y--; player.y--;
if (player.y < 300) if (player.y < screen->h)
player.y++; player.y++;
} }
@ -380,7 +380,7 @@ void leaveSector()
player.face = 0; player.face = 0;
player.x += 12; player.x += 12;
engine.ssx -= 0.2; engine.ssx -= 0.2;
if (player.x > 1600) if (player.x > (2 * screen->w))
engine.done = 1; engine.done = 1;
} }
} }

View File

@ -272,14 +272,14 @@ void doCutscene(int scene)
enemy[i].y += enemy[i].dy; enemy[i].y += enemy[i].dy;
enemy[i].x += engine.ssx; enemy[i].x += engine.ssx;
blit(enemy[i].image[0], (int)enemy[i].x, (int)enemy[i].y); blit(enemy[i].image[0], (int)enemy[i].x, (int)enemy[i].y);
if (enemy[i].x > 850) if (enemy[i].x > (screen->w + 50))
{ {
enemy[i].x = -50; enemy[i].x = -50;
enemy[i].y = rand() % 560; enemy[i].y = rand() % (screen->h - 40);
} }
if (enemy[i].y < -50) if (enemy[i].y < -50)
enemy[i].y = 650; enemy[i].y = (screen->h + 50);
if (enemy[i].y > 650) if (enemy[i].y > (screen->h + 50))
enemy[i].y = -50; enemy[i].y = -50;
} }
} }
@ -308,7 +308,7 @@ void doCutscene(int scene)
} }
if ((showMessage) && (messageBox != NULL)) if ((showMessage) && (messageBox != NULL))
blit(messageBox, (800 - messageBox->w) / 2, 500); blit(messageBox, (screen->w - messageBox->w) / 2, screen->h - 100);
delayFrame(); delayFrame();

View File

@ -171,11 +171,11 @@ int doTitle()
prlogo = loadImage("gfx/prlogo.png"); prlogo = loadImage("gfx/prlogo.png");
sflogo = loadImage("gfx/sflogo.png"); sflogo = loadImage("gfx/sflogo.png");
int prx = ((800 - prlogo->w) / 2); int prx = ((screen->w - prlogo->w) / 2);
int pry = ((600 - prlogo->h) / 2); int pry = ((screen->h - prlogo->h) / 2);
int sfx = ((800 - sflogo->w) / 2); int sfx = ((screen->w - sflogo->w) / 2);
int sfy = ((600 - sflogo->h) / 2); int sfy = ((screen->h - sflogo->h) / 2);
textSurface(0, "PRESENTS", -1, 300, FONT_WHITE); textSurface(0, "PRESENTS", -1, 300, FONT_WHITE);
textSurface(1, "AN SDL GAME", -1, 300, FONT_WHITE); textSurface(1, "AN SDL GAME", -1, 300, FONT_WHITE);
@ -205,8 +205,8 @@ int doTitle()
enemy[i] = defEnemy[CD_TRANSPORTSHIP]; enemy[i] = defEnemy[CD_TRANSPORTSHIP];
if ((rand() % 5) == 0) if ((rand() % 5) == 0)
enemy[i] = defEnemy[CD_MINER]; enemy[i] = defEnemy[CD_MINER];
enemy[i].x = rand() % 800; enemy[i].x = rand() % screen->w;
enemy[i].y = rand() % 560; enemy[i].y = rand() % (screen->h - 40);
enemy[i].dx = 1 + rand() % 3; enemy[i].dx = 1 + rand() % 3;
enemy[i].face = 0; enemy[i].face = 0;
} }
@ -258,7 +258,7 @@ int doTitle()
if (enemy[i].x > 830) if (enemy[i].x > 830)
{ {
enemy[i].x = -40; enemy[i].x = -40;
enemy[i].y = rand() % 580; enemy[i].y = rand() % (screen->h - 20);
enemy[i].dx = 1 + rand() % 3; enemy[i].dx = 1 + rand() % 3;
} }
} }
@ -414,9 +414,9 @@ int doTitle()
SDL_WM_ToggleFullScreen(screen); SDL_WM_ToggleFullScreen(screen);
#else #else
if (engine.fullScreen) if (engine.fullScreen)
screen = SDL_SetVideoMode(800, 600, 0, SDL_DOUBLEBUF|SDL_HWPALETTE|SDL_FULLSCREEN); screen = SDL_SetVideoMode(screen->w, screen->h, 0, SDL_DOUBLEBUF|SDL_HWPALETTE|SDL_FULLSCREEN);
else else
screen = SDL_SetVideoMode(800, 600, 0, SDL_DOUBLEBUF|SDL_HWPALETTE); screen = SDL_SetVideoMode(screen->w, screen->h, 0, SDL_DOUBLEBUF|SDL_HWPALETTE);
drawBackground(); drawBackground();
flushBuffer(); flushBuffer();
@ -491,7 +491,7 @@ void showStory()
{ {
freeGraphics(); freeGraphics();
int y = 620; int y = screen->h + 20;
FILE *fp; FILE *fp;
@ -540,7 +540,7 @@ void showStory()
if ((engine.keyState[SDLK_LCTRL]) || (engine.keyState[SDLK_RCTRL]) || (engine.keyState[SDLK_SPACE])) if ((engine.keyState[SDLK_LCTRL]) || (engine.keyState[SDLK_RCTRL]) || (engine.keyState[SDLK_SPACE]))
break; break;
if (textShape[8].y > 450) if (textShape[8].y > (int)(screen->h / 2) + 150)
{ {
for (int i = 0 ; i < 9 ; i++) for (int i = 0 ; i < 9 ; i++)
{ {
@ -585,8 +585,8 @@ void gameover()
Mix_PlayMusic(engine.music, 1); Mix_PlayMusic(engine.music, 1);
} }
int x = (800 - gameover->w) / 2; int x = (screen->w - gameover->w) / 2;
int y = (600 - gameover->h) / 2; int y = (screen->h - gameover->h) / 2;
updateScreen(); updateScreen();
@ -603,8 +603,8 @@ void gameover()
updateScreen(); updateScreen();
unBuffer(); unBuffer();
x = ((800 - gameover->w) / 2) - rrand(-2, 2); x = ((screen->w - gameover->w) / 2) - rrand(-2, 2);
y = ((600 - gameover->h) / 2) - rrand(-2, 2); y = ((screen->h - gameover->h) / 2) - rrand(-2, 2);
blit(gameover, x, y); blit(gameover, x, y);
delayFrame(); delayFrame();
@ -632,7 +632,7 @@ void doCredits()
int lastCredit = 0; int lastCredit = 0;
int yPos = 0; int yPos = 0;
int yPos2 = 600; int yPos2 = screen->h;
char text[255]; char text[255];
textObject *credit; textObject *credit;
@ -665,7 +665,7 @@ void doCredits()
{ {
fscanf(fp, "%d %[^\n]%*c", &yPos, text); fscanf(fp, "%d %[^\n]%*c", &yPos, text);
credit[i].image = textSurface(text, FONT_WHITE); credit[i].image = textSurface(text, FONT_WHITE);
credit[i].x = (800 - credit[i].image->w) / 2; credit[i].x = (screen->w - credit[i].image->w) / 2;
yPos2 += yPos; yPos2 += yPos;
credit[i].y = yPos2; credit[i].y = yPos2;
} }
@ -706,11 +706,11 @@ void doCredits()
for (int i = 0 ; i < numberOfCredits ; i++) for (int i = 0 ; i < numberOfCredits ; i++)
{ {
if ((credit[i].y > -10) && (credit[i].y < 610)) if ((credit[i].y > -10) && (credit[i].y < (screen->h + 10)))
blit(credit[i].image, (int)credit[i].x, (int)credit[i].y); blit(credit[i].image, (int)credit[i].x, (int)credit[i].y);
if (speed > 0 && credit[lastCredit].y > 400) if (speed > 0 && credit[lastCredit].y > ((int)(screen->h / 2) + 100))
credit[i].y -= speed; credit[i].y -= speed;
else if(speed < 0 && credit[0].y < 600) else if(speed < 0 && credit[0].y < screen->h)
credit[i].y -= speed; credit[i].y -= speed;
} }