Commit b482cdbe authored by alistert's avatar alistert

Improved font handling. Little more on bonus levels.

parent c0b880a1
......@@ -37,12 +37,14 @@ BaseLevel::BaseLevel () {
// Arbitrary initial value
smoothfps = 50.0f;
paused = false;
return;
}
void BaseLevel::timeCalcs (bool paused) {
void BaseLevel::timeCalcs () {
// Calculate smoothed fps
smoothfps = smoothfps + 1.0f -
......
......@@ -57,8 +57,9 @@ class BaseLevel {
unsigned int endTime;
float smoothfps;
int items;
bool paused;
void timeCalcs (bool paused);
void timeCalcs ();
void drawStats (int stats);
public:
......
......@@ -37,7 +37,7 @@
#include "menu/menu.h"
#include "player/player.h"
#include <math.h>
#include <math.h>
int Bonus::loadTiles (char *fileName) {
......@@ -55,7 +55,11 @@ int Bonus::loadTiles (char *fileName) {
}
file->skipRLE();
// Load palette
file->loadPalette(palette);
// Load tile graphics
tileSet = file->loadSurface(32, 32 * 60);
delete file;
......@@ -200,25 +204,23 @@ int Bonus::play () {
const char *options[3] = {"continue game", "setup options", "quit game"};
PaletteEffect *levelPE;
bool paused, pmenu;
bool pmenu, pmessage;
int stats, option;
SDL_Rect src, dst;
int x, y;
fixed pX, pY, pDirection;
int mspf;
int msps;
tickOffset = globalTicks;
ticks = 16;
prevStepTicks = 0;
pmenu = paused = false;
pmessage = pmenu = false;
option = 0;
stats = S_NONE;
// Arbitrary position
localPlayer->setPosition(TTOF(32) + F16, TTOF(7) + F16);
pDirection = FQ;
while (true) {
......@@ -226,12 +228,12 @@ int Bonus::play () {
if (controls.release(C_ESCAPE)) {
if (!gameMode) paused = !paused;
pmenu = !pmenu;
option = 0;
}
if (controls.release(C_PAUSE) && !gameMode) paused = !paused;
if (controls.release(C_PAUSE)) pmessage = !pmessage;
if (controls.release(C_STATS)) {
......@@ -254,7 +256,7 @@ int Bonus::play () {
case 0: // Continue
paused = pmenu = false;
pmenu = false;
break;
......@@ -288,35 +290,23 @@ int Bonus::play () {
}
timeCalcs(paused);
if (!gameMode) paused = pmessage || pmenu;
mspf = ticks - prevTicks;
timeCalcs();
// Milliseconds per step
msps = ticks - prevStepTicks;
prevStepTicks = ticks;
pX = localPlayer->getX();
pY = localPlayer->getY();
if (!paused) {
if (controls.getState(C_UP)) {
pX += fixed(sin(pDirection * 6.283185 / 1024.0) * 64 * mspf);
pY -= fixed(cos(pDirection * 6.283185 / 1024.0) * 64 * mspf);
}
if (controls.getState(C_DOWN)) {
pX -= fixed(sin(pDirection * 6.283185 / 1024.0) * 32 * mspf);
pY += fixed(cos(pDirection * 6.283185 / 1024.0) * 32 * mspf);
}
if (controls.getState(C_LEFT)) pDirection -= mspf / 2;
if (controls.getState(C_RIGHT)) pDirection += mspf / 2;
// Apply controls to local player
for (x = 0; x < PCONTROLS; x++)
localPlayer->setControl(x, controls.getState(x));
localPlayer->setPosition(pX, pY);
// Process players
for (x = 0; x < nPlayers; x++) players[x].bonusStep(ticks, msps);
}
......@@ -324,8 +314,8 @@ int Bonus::play () {
src.w = 32;
src.h = 32;
int vX = FTOI(pX) - (canvasW >> 1);
int vY = FTOI(pY) - (canvasH >> 1);
int vX = FTOI(localPlayer->getX()) - (canvasW >> 1);
int vY = FTOI(localPlayer->getY()) - (canvasH >> 1);
for (y = 0; y <= ITOT(canvasH - 1) + 1; y++) {
......@@ -384,8 +374,8 @@ int Bonus::play () {
// Draw the "player"
drawRect(
(canvasW >> 1) + fixed(sin(pDirection * 6.283185 / 1024.0) * 3) - 4,
(canvasH >> 1) - fixed(cos(pDirection * 6.283185 / 1024.0) * 3) - 4, 8, 8, 0);
(canvasW >> 1) + fixed(sin(localPlayer->getDirection() * 6.283185 / 1024.0) * 3) - 4,
(canvasH >> 1) - fixed(cos(localPlayer->getDirection() * 6.283185 / 1024.0) * 3) - 4, 8, 8, 0);
drawRect((canvasW >> 1) - 4, (canvasH >> 1) - 4, 8, 8, 22);
......@@ -401,7 +391,7 @@ int Bonus::play () {
// If paused, draw "PAUSE"
if (paused && !pmenu)
fontmn1->showString("PAUSE", (canvasW >> 1) - 44, 32);
fontmn1->showString("pause", (canvasW >> 1) - 44, 32);
// Draw statistics
drawStats(stats);
......
......@@ -103,11 +103,11 @@ int Game::setLevel (char *fileName) {
}
int Game::setBonus (char *fileName) {
int Game::setBonus (int ext) {
if (bonusFile) delete[] bonusFile;
if (fileName) bonusFile = createString(fileName);
if (level >= 0) bonusFile = createFileName(F_BONUSMAP, ext);
else bonusFile = NULL;
return E_NONE;
......@@ -130,6 +130,8 @@ int Game::play () {
sendTime = checkTime = 0;
level = NULL;
if (levelFile) {
// Load and play the level
......@@ -146,10 +148,10 @@ int Game::play () {
levelRet = level->play();
}
} else levelRet = WON;
if (bonusFile) {
if (bonusFile && (levelRet == WON)) {
// Load and play the bonus level
......@@ -177,7 +179,7 @@ int Game::play () {
}
if (!levelFile) continue;
if (!level) continue;
switch (levelRet) {
......
......@@ -96,7 +96,7 @@ class Game {
virtual ~Game ();
virtual int setLevel (char *fileName);
int setBonus (char *fileName);
int setBonus (int ext);
int play ();
void view (int change);
virtual void send (unsigned char *buffer);
......
......@@ -212,7 +212,7 @@ unsigned char * File::loadRLE (int length) {
rle = fgetc(f);
if (rle > 127) {
if (rle & 128) {
byte = fgetc(f);
......@@ -223,7 +223,7 @@ unsigned char * File::loadRLE (int length) {
}
} else if (rle > 0) {
} else if (rle) {
for (count = 0; count < rle; count++) {
......@@ -302,8 +302,17 @@ char * File::loadString () {
}
SDL_Surface * File::loadSurface (int width, int height) {
SDL_Surface *surface;
unsigned char *pixels;
pixels = loadRLE(width * height);
return createSurface(loadRLE(width * height), width, height);
surface = createSurface(pixels, width, height);
delete[] pixels;
return surface;
}
......
This diff is collapsed.
......@@ -8,7 +8,7 @@
* Part of the OpenJazz project
*
*
* Copyright (c) 2005-2009 Alister Thomson
* Copyright (c) 2005-2010 Alister Thomson
*
* OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0
......@@ -36,10 +36,10 @@ class File;
class Font {
private:
SDL_Surface *surface;
unsigned char *w;
unsigned char h; // Dimensions of the letters
char map[128]; // Maps ASCII values to letter positions
SDL_Surface *characters[128];
int nCharacters;
unsigned char lineHeight;
char map[128]; // Maps ASCII values to letter positions
public:
Font (const char *fileName);
......@@ -47,13 +47,14 @@ class Font {
~Font ();
int showString (const char *s, int x, int y);
int showSceneString (const char *s, int x, int y);
int showSceneString (const unsigned char *s, int x, int y);
void showNumber (int n, int x, int y);
void mapPalette (int start, int length, int newStart, int newLength);
void restorePalette ();
int getHeight ();
int getStringWidth (const char *string);
int getSceneStringWidth (const char *string);
int getSceneStringWidth (const unsigned char *string);
};
// Variables
......
......@@ -45,13 +45,12 @@ Sprite::~Sprite () {
void Sprite::clearPixels () {
unsigned char *data;
unsigned char data;
if (pixels) SDL_FreeSurface(pixels);
data = new unsigned char[1];
*data = SKEY;
pixels = createSurface(data, 1, 1);
data = SKEY;
pixels = createSurface(&data, 1, 1);
SDL_SetColorKey(pixels, SDL_SRCCOLORKEY, SKEY);
return;
......
......@@ -53,9 +53,6 @@ SDL_Surface * createSurface (unsigned char * pixels, int width, int height) {
if (SDL_MUSTLOCK(ret)) SDL_UnlockSurface(ret);
// Free redundant pixel data
delete[] pixels;
}
return ret;
......
......@@ -71,8 +71,7 @@ EXTERN SDL_Surface *panelAmmo[5];
// Functions
EXTERN SDL_Surface * createSurface (unsigned char *pixels, int width,
int height);
EXTERN SDL_Surface * createSurface (unsigned char *pixels, int width, int height);
EXTERN void createScreen ();
EXTERN void usePalette (SDL_Color *palette);
EXTERN void restorePalette (SDL_Surface *surface);
......
......@@ -113,7 +113,7 @@ int DemoLevel::play () {
if (controls.release(C_STATS)) stats ^= S_SCREEN;
timeCalcs(false);
timeCalcs();
......@@ -171,7 +171,7 @@ int DemoLevel::play () {
drawStats(stats);
fontmn1->showString("DEMO", (canvasW >> 1) - 36, 32);
fontmn1->showString("demo", (canvasW >> 1) - 36, 32);
}
......
......@@ -525,7 +525,7 @@ int Level::play () {
{"continue game", "save game", "load game", "setup options", "quit game"};
PaletteEffect *levelPE;
char *string;
bool paused, pmenu;
bool pmessage, pmenu;
int stats, option;
unsigned int returnTime;
int perfect;
......@@ -537,7 +537,7 @@ int Level::play () {
ticks = 16;
prevStepTicks = 0;
pmenu = paused = false;
pmessage = pmenu = false;
option = 0;
stats = S_NONE;
......@@ -547,18 +547,16 @@ int Level::play () {
while (true) {
// Do general processing
if (loop(NORMAL_LOOP) == E_QUIT) return E_QUIT;
if (controls.release(C_ESCAPE)) {
if (!gameMode) paused = !paused;
pmenu = !pmenu;
option = 0;
}
if (controls.release(C_PAUSE) && !gameMode) paused = !paused;
if (controls.release(C_PAUSE)) pmessage = !pmessage;
if (controls.release(C_STATS)) {
......@@ -581,7 +579,7 @@ int Level::play () {
case 0: // Continue
paused = pmenu = false;
pmenu = false;
break;
......@@ -623,8 +621,9 @@ int Level::play () {
}
timeCalcs(paused);
if (!gameMode) paused = pmessage || pmenu;
timeCalcs();
......@@ -689,7 +688,7 @@ int Level::play () {
// If paused, draw "PAUSE"
if (paused && !pmenu)
fontmn1->showString("PAUSE", (canvasW >> 1) - 44, 32);
fontmn1->showString("pause", (canvasW >> 1) - 44, 32);
// If this is a competitive game, draw the score
......@@ -745,27 +744,27 @@ int Level::play () {
// Display statistics & bonuses
// TODO: Display percentage symbol
fontmn1->showString("TIME", (canvasW >> 1) - 152, (canvasH >> 1) - 60);
fontmn1->showString("time", (canvasW >> 1) - 152, (canvasH >> 1) - 60);
fontmn1->showNumber(timeBonus, (canvasW >> 1) + 124, (canvasH >> 1) - 60);
fontmn1->showString("ENEMIES", (canvasW >> 1) - 152, (canvasH >> 1) - 40);
fontmn1->showString("enemies", (canvasW >> 1) - 152, (canvasH >> 1) - 40);
if (enemies)
fontmn1->showNumber((localPlayer->getEnemies() * 100) / enemies, (canvasW >> 1) + 124, (canvasH >> 1) - 40);
else
fontmn1->showNumber(0, (canvasW >> 1) + 124, (canvasH >> 1) - 40);
fontmn1->showString("ITEMS", (canvasW >> 1) - 152, (canvasH >> 1) - 20);
fontmn1->showString("items", (canvasW >> 1) - 152, (canvasH >> 1) - 20);
if (items)
fontmn1->showNumber((localPlayer->getItems() * 100) / items, (canvasW >> 1) + 124, (canvasH >> 1) - 20);
else
fontmn1->showNumber(0, (canvasW >> 1) + 124, (canvasH >> 1) - 20);
fontmn1->showString("PERFECT", (canvasW >> 1) - 152, canvasH >> 1);
fontmn1->showString("perfect", (canvasW >> 1) - 152, canvasH >> 1);
fontmn1->showNumber(perfect, (canvasW >> 1) + 124, canvasH >> 1);
fontmn1->showString("SCORE", (canvasW >> 1) - 152, (canvasH >> 1) + 40);
fontmn1->showString("score", (canvasW >> 1) - 152, (canvasH >> 1) + 40);
fontmn1->showNumber(localPlayer->getScore(), (canvasW >> 1) + 124, (canvasH >> 1) + 40);
}
......@@ -789,8 +788,8 @@ int Level::play () {
fontmn2->restorePalette();
}
// Networking
if (gameMode) {
......
......@@ -182,7 +182,8 @@ void Level::draw () {
Bullet *bullet;
SDL_Rect src, dst;
int vX, vY;
int x, y, bgScale;
int x, y, bgScale;
unsigned int change;
// Set tile drawing dimensions
......@@ -275,12 +276,16 @@ void Level::draw () {
}
// Calculate change since last step
change = paused? 0: ticks - prevStepTicks;
// Show active events
event = firstEvent;
while (event) {
event->draw(ticks, ticks - prevStepTicks);
event->draw(ticks, change);
event = event->getNext();
}
......@@ -288,7 +293,7 @@ void Level::draw () {
// Show the players
for (x = 0; x < nPlayers; x++)
players[x].draw(ticks, ticks - prevStepTicks);
players[x].draw(ticks, change);
// Show bullets
......@@ -296,7 +301,7 @@ void Level::draw () {
while (bullet) {
bullet->draw(ticks - prevStepTicks);
bullet->draw(change);
bullet = bullet->getNext();
}
......
......@@ -284,7 +284,8 @@ int Level::loadSprites (char * fileName) {
spriteSet[count].setPixels(sorted, width, height);
// Free redundant data
delete[] pixels;
delete[] pixels;
delete[] sorted;
// Check if the next sprite exists
......@@ -402,6 +403,8 @@ int Level::loadTiles (char * fileName) {
tileSet = createSurface(buffer, TTOI(1), TTOI(tiles));
SDL_SetColorKey(tileSet, SDL_SRCCOLORKEY, TKEY);
delete[] buffer;
return tiles;
......
......@@ -345,9 +345,10 @@ int loadMain (int argc, char *argv[]) {
}
panelAmmo[0] = createSurface(sorted, 64, 27);
sorted = pixels; // Re-use the allocated memory
panelAmmo[0] = createSurface(sorted, 64, 27);
delete[] pixels;
file->seek(8264, true);
pixels = file->loadRLE(64 * 27);
......@@ -359,7 +360,8 @@ int loadMain (int argc, char *argv[]) {
}
panelAmmo[1] = createSurface(sorted, 64, 27);
sorted = pixels; // Re-use the allocated memory
delete[] pixels;
file->seek(9550, true);
pixels = file->loadRLE(64 * 27);
......@@ -372,7 +374,8 @@ int loadMain (int argc, char *argv[]) {
}
panelAmmo[2] = createSurface(sorted, 64, 27);
sorted = pixels; // Re-use the allocated memory
delete[] pixels;
file->seek(11060, true);
pixels = file->loadRLE(64 * 27);
......@@ -385,7 +388,8 @@ int loadMain (int argc, char *argv[]) {
}
panelAmmo[3] = createSurface(sorted, 64, 27);
sorted = pixels; // Re-use the allocated memory
delete[] pixels;
file->seek(12258, true);
pixels = file->loadRLE(64 * 27);
......@@ -398,7 +402,9 @@ int loadMain (int argc, char *argv[]) {
}
panelAmmo[4] = createSurface(sorted, 64, 27);
delete[] pixels; // Don't re-use the allocated memory
delete[] pixels;
delete[] sorted;
// Load fonts
......
......@@ -14,7 +14,7 @@
* Part of the OpenJazz project
*
*
* Copyright (c) 2005-2009 Alister Thomson
* Copyright (c) 2005-2010 Alister Thomson
*
* OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0
......@@ -44,7 +44,7 @@
Menu::Menu () {
File *file;
unsigned char *pixels;
unsigned char pixel;
time_t currentTime;
int count, col;
......@@ -154,9 +154,8 @@ Menu::Menu () {
for (; count < 11; count++) {
pixels = new unsigned char[1];
*pixels = 0;
screens[count + 3] = createSurface(pixels, 1, 1);
pixel = 0;
screens[count + 3] = createSurface(&pixel, 1, 1);
}
......
......@@ -211,7 +211,8 @@ void Player::reset () {
energy = 4;
shield = 0;
floating = false;
facing = true;
facing = true;
direction = FQ;
reaction = PR_NONE;
reactionTime = 0;
jumpHeight = ITOF(92);
......@@ -402,6 +403,8 @@ bool Player::takeEvent (unsigned char gridX, unsigned char gridY, unsigned int t
break;
case 37: // Diamond
if (game) game->setBonus(0);
// Yellow flash
firstPE = new FlashPaletteEffect(255, 255, 0, 320, firstPE);
......@@ -694,6 +697,13 @@ bool Player::getFacing () {
}
fixed Player::getDirection () {
return direction;
}
Anim * Player::getAnim () {
return level->getAnim(anims[animType]);
......
......@@ -113,6 +113,10 @@
#define PYS_FALL ITOF(350)
#define PYS_SINK ITOF(150)
#define PYS_JUMP ITOF(-350)
#define PRS_REVERSE ITOF(-32)
#define PRS_RUN ITOF(64)
#define PRS_ROLL ITOF(96)
// Player accelerations
#define PXA_REVERSE 900
......@@ -122,6 +126,10 @@
#define PXA_FFRUN 200
#define PYA_GRAVITY 2750
#define PYA_SINK 1000
#define PRA_REVERSE 450
#define PRA_STOP 500
#define PRA_RUN 100
// Player colours
#define PC_WHITE 0
......@@ -172,7 +180,8 @@ class Player : public Movable {
int lives;
int shield; /* 0 = none, 1 = 1 yellow, 2 = 2 yellow, 3 = 1 orange, 4 = 2 orange, 5 = 3 orange, 6 = 4 orange */
bool floating; /* false = normal, true = boarding/bird/etc. */
bool facing;
bool facing;
fixed direction;
unsigned char animType;
unsigned char eventX;
unsigned char eventY; /* Position of an event (spring, platform, bridge) */
......@@ -221,13 +230,15 @@ class Player : public Movable {
bool overlap (fixed left, fixed top, fixed width, fixed height);
void setPosition (fixed newX, fixed newY);
void setSpeed (fixed newDx, fixed newDy);
bool getFacing ();
bool getFacing ();
fixed getDirection ();
Anim * getAnim ();
unsigned char getTeam ();
void send (unsigned char *data);
void receive (unsigned char *buffer);
void control (unsigned int ticks, int msps);
void move (unsigned int ticks, int msps);
void bonusStep (unsigned int ticks, int msps);
void view (unsigned int ticks, int mspf);
void draw (unsigned int ticks, int change);
int reacted (unsigned int ticks);
......
......@@ -619,6 +619,63 @@ void Player::move (unsigned int ticks, int msps) {
return;
}
void Player::bonusStep (unsigned int ticks, int msps) {
// Bonus stages use polar coordinates for movement (but not position)
// Treat dx as change in radius
if (pcontrols[C_UP]) {
// Walk/run forwards
if (dx < 0) dx += PRA_REVERSE * msps;
else if (dx < PRS_RUN) dx += PRA_RUN * msps;
facing = true;
} else if (pcontrols[C_DOWN]) {
// Walk/run back
if (dx > 0) dx -= PRA_REVERSE * msps;
else if (dx > PRS_REVERSE) dx -= PRA_RUN * msps;
facing = false;
} else {
// Slow down
if (dx > 0) {
if (dx < PRA_STOP * msps) dx = 0;
else dx -= PRA_STOP * msps;
}
if (dx < 0) {
if (dx > -PRA_STOP * msps) dx = 0;
else dx += PRA_STOP * msps;
}
}
if (pcontrols[C_LEFT]) direction -= msps >> 1;
if (pcontrols[C_RIGHT]) direction += msps >> 1;
// Apply trajectory
x += fixed(sin(direction * 6.283185 / 1024.0) * dx * msps) >> 10;
y -= fixed(cos(direction * 6.283185 / 1024.0) * dx * msps) >> 10;
return;
}
void Player::view (unsigned int ticks, int mspf) {
......
......@@ -67,13 +67,13 @@ class Font;
class SceneText {
public:
char *text;
int alignment;
int fontId;
int x;
int y;
SDL_Rect textRect;
int extraLineHeight;
unsigned char *text;
int alignment;
int fontId;
int x;
int y;
SDL_Rect textRect;
int extraLineHeight;
SceneText ();
~SceneText ();
......
......@@ -659,20 +659,20 @@ void Scene::loadScripts (File *f) {
if (datalen > 0) {
text->text = f->loadBlock(datalen + 1);
f->seek(-1, false);
text->text = f->loadString();
// Convert number placeholders
for (int pos = 1; pos < datalen; pos++) {
if (text->text[pos] == -117) {
if (text->text[pos] == 0x8B) {
if (loop >= 9)
text->text[pos - 1] = ((loop + 1) / 10) + 53;
text->text[pos] = ((loop + 1) % 10) + 53;
} else if (text->text[pos] == -118) {
} else if (text->text[pos] == 0x8A) {
if (scriptItems >= 10)
text->text[pos - 1] = (scriptItems / 10) + 53;
......@@ -683,9 +683,11 @@ void Scene::loadScripts (File *f) {
}
text->text[datalen] = 0;
} else {
text->text = new char[1];
text->text = new unsigned char[1];
text->text[0] = 0;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment