Commit 1174dd9f authored by alistert's avatar alistert

Tidied cutscene code. Fixed text positioning and broken characters.

parent 2334cb55
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* Part of the OpenJazz project * Part of the OpenJazz project
* *
* *
* Copyright (c) 2005-2009 Alister Thomson * Copyright (c) 2005-2010 Alister Thomson
* *
* OpenJazz is distributed under the terms of * OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0 * the GNU General Public License, version 2.0
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
#include "io/sound.h" #include "io/sound.h"
#include "level/level.h" #include "level/level.h"
#include "player/player.h" #include "player/player.h"
#include "scene.h" #include "scene/scene.h"
Game::Game () { Game::Game () {
......
...@@ -321,10 +321,8 @@ void File::loadPalette (SDL_Color *palette) { ...@@ -321,10 +321,8 @@ void File::loadPalette (SDL_Color *palette) {
// Palette entries are 6-bit // Palette entries are 6-bit
// Shift them upwards to 8-bit, and fill in the lower 2 bits // Shift them upwards to 8-bit, and fill in the lower 2 bits
palette[count].r = (buffer[count * 3] << 2) + (buffer[count * 3] >> 4); palette[count].r = (buffer[count * 3] << 2) + (buffer[count * 3] >> 4);
palette[count].g = (buffer[(count * 3) + 1] << 2) + palette[count].g = (buffer[(count * 3) + 1] << 2) + (buffer[(count * 3) + 1] >> 4);
(buffer[(count * 3) + 1] >> 4); palette[count].b = (buffer[(count * 3) + 2] << 2) + (buffer[(count * 3) + 2] >> 4);
palette[count].b = (buffer[(count * 3) + 2] << 2) +
(buffer[(count * 3) + 2] >> 4);
} }
......
...@@ -313,7 +313,6 @@ Font::~Font () { ...@@ -313,7 +313,6 @@ Font::~Font () {
} }
int Font::showString (const char * s, int x, int y) { int Font::showString (const char * s, int x, int y) {
SDL_Rect src, dst; SDL_Rect src, dst;
...@@ -340,8 +339,6 @@ int Font::showString (const char * s, int x, int y) { ...@@ -340,8 +339,6 @@ int Font::showString (const char * s, int x, int y) {
// Determine the character's position on the screen // Determine the character's position on the screen
src.w = w[(int)(map[(int)(s[count])])]; src.w = w[(int)(map[(int)(s[count])])];
if(s[count] == 32) src.w >>= 1;
dst.y = yOffset; dst.y = yOffset;
dst.x = xOffset; dst.x = xOffset;
...@@ -352,7 +349,8 @@ int Font::showString (const char * s, int x, int y) { ...@@ -352,7 +349,8 @@ int Font::showString (const char * s, int x, int y) {
// Draw the character to the screen // Draw the character to the screen
SDL_BlitSurface(surface, &src, canvas, &dst); SDL_BlitSurface(surface, &src, canvas, &dst);
xOffset += src.w-1; xOffset += src.w;
} }
} }
...@@ -362,7 +360,45 @@ int Font::showString (const char * s, int x, int y) { ...@@ -362,7 +360,45 @@ int Font::showString (const char * s, int x, int y) {
} }
int Font::showSceneString (const char * s, int x, int y) {
SDL_Rect src, dst;
unsigned int count;
int xOffset;
// Determine the characters' dimensions
src.x = 0;
src.h = h;
// Determine the position at which to draw the first character
xOffset = x;
// Go through each character of the string
for (count = 0; s[count]; count++) {
// Determine the character's position on the screen
src.w = w[(int)(s[count])];
if (s[count] == 0x7F) src.w -= 3;
dst.y = y;
dst.x = xOffset;
// Determine the character's position in the font
if (s[count] >= 0) src.y = s[count] * h;
else src.y = 0;
// Draw the character to the screen
SDL_BlitSurface(surface, &src, canvas, &dst);
xOffset += src.w - 1;
}
return xOffset;
}
void Font::showNumber (int n, int x, int y) { void Font::showNumber (int n, int x, int y) {
SDL_Rect src, dst; SDL_Rect src, dst;
...@@ -471,29 +507,38 @@ int Font::getHeight () { ...@@ -471,29 +507,38 @@ int Font::getHeight () {
} }
int Font::getStringWidth (const char *string) { int Font::getStringWidth (const char *string) {
int count;
int stringWidth = 0;
// Go through each character of the string
for (count = 0; string[count]; count++) {
// Only get the width of the first line
if (string[count] == '\n') return stringWidth;
stringWidth += w[(int)(string[count])];
}
return stringWidth;
}
int Font::getSceneStringWidth (const char *string) {
int count; int count;
int stringWidth = 0; int width, stringWidth = 0;
// Go through each character of the string // Go through each character of the string
for (count = 0; string[count]; count++) { for (count = 0; string[count]; count++) {
if (string[count] == '\n') { width = w[(int)(string[count])];
if (string[count] == 0x7F) width -= 3;
} else {
int width = w[(int)(map[(int)(string[count])])];
if (string[count] == ' ') { stringWidth += width - 1;
width = width >> 1;
}
stringWidth += width - 1;
}
} }
......
...@@ -42,17 +42,18 @@ class Font { ...@@ -42,17 +42,18 @@ class Font {
char map[128]; // Maps ASCII values to letter positions char map[128]; // Maps ASCII values to letter positions
public: public:
Font (const char *fileName); Font (const char *fileName);
Font (File *file, bool big); Font (File *file, bool big);
~Font (); ~Font ();
int showString (const char *s, int x, int y); int showString (const char *s, int x, int y);
void showNumber (int n, int x, int y); int showSceneString (const char *s, int x, int y);
void mapPalette (int start, int length, int newStart, void showNumber (int n, int x, int y);
int newLength); void mapPalette (int start, int length, int newStart, int newLength);
void restorePalette (); void restorePalette ();
int getHeight (); int getHeight ();
int getStringWidth (const char *string); int getStringWidth (const char *string);
int getSceneStringWidth (const char *string);
}; };
// Variables // Variables
......
/* /*
* *
* guardians.cpp * bridge.cpp
* *
* 2nd March 2010: Created bridge.cpp from parts of event.cpp and eventframe.cpp * 2nd March 2010: Created bridge.cpp from parts of event.cpp and eventframe.cpp
* *
......
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
#include "io/sound.h" #include "io/sound.h"
#include "menu/menu.h" #include "menu/menu.h"
#include "player/player.h" #include "player/player.h"
#include "scene.h" #include "scene/scene.h"
#include <string.h> #include <string.h>
......
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
#include "level/level.h" #include "level/level.h"
#include "menu/menu.h" #include "menu/menu.h"
#include "player/player.h" #include "player/player.h"
#include "scene.h" #include "scene/scene.h"
#include <string.h> #include <string.h>
......
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
#include "io/sound.h" #include "io/sound.h"
#include "level/level.h" #include "level/level.h"
#include "player/player.h" #include "player/player.h"
#include "scene.h" #include "scene/scene.h"
int Menu::main () { int Menu::main () {
......
This diff is collapsed.
/*
*
* scene.h
*
* 3rd February 2009: Created scene.h from parts of scene.c
*
* Part of the OpenJazz project
*
*
* Copyright (c) 2005-2009 Alister Thomson
*
* OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef _SCENE_H
#define _SCENE_H
#include <io/file.h>
#include <SDL/SDL.h>
enum EScriptFontTypes
{
EFONT2Type,
EFONTBIGType,
EFONTINYType,
EFONTMN1Type,
EFONTMN2Type
};
class ScriptFont
{
public:
// This maps to any of the available types (already loaded)
EScriptFontTypes fontType;
// Font id given this font
int fontId;
};
class ScriptText
{
public:
ScriptText();
~ScriptText();
char* text;
int alignment;
int fontId;
int x;
int y;
SDL_Rect textRect;
int extraLineHeight;
};
// Class
class ScriptPage
{
public:
ScriptPage();
~ScriptPage();
int backgrounds;
int bgIndex[30];
unsigned int bgPos[30];
// Length of the scene in seconds, or if zero = anim complete, or 256 = user interaction
int pageTime;
ScriptText scriptTexts[100];
int noScriptTexts;
char* musicfile;
int paletteIndex;
};
class ImageInfo
{
public:
ImageInfo();
~ImageInfo();
// SDL_Surface with the image
SDL_Surface *image;
// data index of the image (not the palette) to compare with scripts
int dataIndex;
};
class PaletteInfo
{
public:
// Palette associated with the image
SDL_Color palette[256];
// data index of the image (not the palette) to compare with scripts
int dataIndex;
};
class Scene {
private:
ImageInfo imageInfos[100];
PaletteInfo paletteInfos[100];
unsigned short int scriptItems;
unsigned short int dataItems;
signed long int* scriptStarts;
signed long int* dataOffsets;
int imageIndex;
int paletteIndex;
// Scripts all information needed to render script pages, text etc
ScriptPage* scriptPages;
ImageInfo* FindImage(int dataIndex);
// DataBlock
protected:
void ParseScripts(File *f);
void ParseData(File *f);
void ParseAni(File* f, int dataIndex);
public:
Scene (const char * fileName);
~Scene ();
int play ();
ScriptFont scriptFonts[5];
int noScriptFonts;
};
#endif
/*
*
* scene.cpp
*
* 23rd August 2005: Created scene.c
* 3rd February 2009: Created scene.h from parts of scene.c
* 3rd February 2009: Renamed scene.c to scene.cpp
* 27th March 2010: Created sceneload.cpp from parts of scene.cpp
*
* Part of the OpenJazz project
*
*
* Copyright (c) 2005-2010 Alister Thomson
*
* OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/*
* Deals with the displaying and freeing of the cutscenes.
*
*/
#include "scene/scene.h"
#include "io/controls.h"
#include "io/gfx/font.h"
#include "io/gfx/paletteeffects.h"
#include "io/gfx/video.h"
/**
* This is the 0sc format
* Offset, Size (hex), type
* 0, 0x13, "Digital Dimensions\1a" header
* 0x13, 0x04, Offset to datablock
* 0x17, 0x02, Number of script items
* 0x19, 0x4* Number of script items, Offset to Scripts
*
* ----, 0x02 Number of data items
* * 0x19, 0x4* Number of script items, Offset to datablocks
*
*/
ImageInfo::ImageInfo () {
image = NULL;
}
ImageInfo::~ImageInfo () {
if (image != NULL) SDL_FreeSurface(image);
}
ScriptText::ScriptText() {
x = -1;
y = -1;
textRect.x = -1;
textRect.y = -1;
extraLineHeight = -1;
text = NULL;
}
ScriptText::~ScriptText() {
if (text) delete[] text;
}
ScriptPage::ScriptPage() {
pageTime = 0;
noScriptTexts = 0;
backgrounds = 0;
musicfile = NULL;
paletteIndex = 0;
}
ScriptPage::~ScriptPage() {
if (musicfile) delete[] musicfile;
}
Scene::Scene (const char * fileName) {
File *file;
int loop;
noScriptFonts = 0;
LOG("\nScene", fileName);
try {
file = new File(fileName, false);
} catch (int e) {
throw e;
}
imageIndex = 0;
paletteIndex = 0;
file->seek(0x13, true); // Skip Digital Dimensions header
signed long int dataOffset = file->loadInt(); //get offset pointer to first data block
scriptItems = file->loadShort(); // Get number of script items
scriptStarts = new signed long int[scriptItems];
scriptPages = new ScriptPage[scriptItems];
LOG("Scene: Script items", scriptItems);
for (loop = 0; loop < scriptItems; loop++) {
scriptStarts[loop] = file->loadInt();// Load offset to script
LOG("scriptStart:", scriptStarts[loop]);
}
// Seek to datastart now
file->seek(dataOffset, true); // Seek to data offsets
dataItems = file->loadShort() + 1; // Get number of data items
LOG("Scene: Data items", dataItems);
dataOffsets = new signed long int[dataItems];
for (loop = 0; loop < dataItems; loop++) {
dataOffsets[loop] = file->loadInt();// Load offset to script
LOG("dataOffsets:", dataOffsets[loop]);
}
loadData(file);
loadScripts(file);
delete[] scriptStarts;
delete[] dataOffsets;
delete file;
return;
}
Scene::~Scene () {
delete[] scriptPages;
}
ImageInfo * Scene::FindImage (int dataIndex) {
int loop;
for (loop = 0; loop < imageIndex; loop++) {
if (imageInfos[loop].dataIndex == dataIndex) return imageInfos + loop;
}
return NULL;
}
int Scene::play () {
SDL_Rect dst;
unsigned int sceneIndex = 0;
ImageInfo* imageInfo;
unsigned int pageTime = scriptPages[sceneIndex].pageTime;
unsigned int lastTicks = globalTicks;
int newpage = true;
int fadein = false;
SDL_Rect textRect = {0,0,320,200};
while (true) {
if (loop(NORMAL_LOOP) == E_QUIT) return E_QUIT;
if (controls.release(C_ESCAPE)) return E_NONE;
SDL_Delay(T_FRAME);
int upOrLeft = (controls.release(C_UP) || controls.release(C_LEFT));
if ((sceneIndex > 0 && upOrLeft) ||
controls.release(C_RIGHT) || controls.release(C_DOWN) || controls.release(C_ENTER) ||
((globalTicks-lastTicks) >= pageTime * 1000 && pageTime != 256 && pageTime != 0)) {
if (upOrLeft) sceneIndex--;
else sceneIndex++;
if (sceneIndex == scriptItems) return E_NONE;
lastTicks = globalTicks;
// Get bg for this page
newpage = true;
pageTime = scriptPages[sceneIndex].pageTime;
}
if (newpage) {
//firstPE = new FadeOutPaletteEffect(250, firstPE);
textRect.x = 0;
textRect.y = 0;
textRect.w = 320;
textRect.h = 200;
PaletteInfo* paletteInfo = NULL;
for (int palette = 0; palette < paletteIndex; palette++) {
if (paletteInfos[palette].dataIndex == scriptPages[sceneIndex].paletteIndex) {
paletteInfo = &paletteInfos[palette];
break;
}
}
if (paletteInfo != NULL) {
// usePalette(paletteInfo->palette);
currentPalette = paletteInfo->palette;
fadein = true;
} else restorePalette(screen);
newpage = 0;
}
// First draw the backgrounds associated with this page
if (scriptPages[sceneIndex].backgrounds > 0) {
for (int bg = 0; bg < scriptPages[sceneIndex].backgrounds; bg++) {
imageInfo = FindImage(scriptPages[sceneIndex].bgIndex[bg]);
if (imageInfo != NULL) {
dst.x = (scriptPages[sceneIndex].bgPos[bg] & 65535)*2 + (canvasW - 320) >> 1;
dst.y = ((scriptPages[sceneIndex].bgPos[bg] & (~65535))>>16)*2 + (canvasH - 200) >> 1;
SDL_BlitSurface(imageInfo->image, NULL, canvas, &dst);
}
}
} else clearScreen(0);
// Draw the texts associated with this page
int x = 0;
int y = 0;
int extraLineHeight = 0;
for (int text = 0; text < scriptPages[sceneIndex].noScriptTexts; text++) {
Font *font = NULL;
int xOffset, yOffset;
for (int index = 0; index < noScriptFonts; index++) {
if (scriptPages[sceneIndex].scriptTexts[text].fontId == scriptFonts[index].fontId) {
font = scriptFonts[index].font;
continue;
}
}
if (scriptPages[sceneIndex].scriptTexts[text].x != -1) {
x = scriptPages[sceneIndex].scriptTexts[text].x;
y = scriptPages[sceneIndex].scriptTexts[text].y;
}
if (scriptPages[sceneIndex].scriptTexts[text].textRect.x != -1) {
textRect = scriptPages[sceneIndex].scriptTexts[text].textRect;
x = 0;
y = 0;
}
if (scriptPages[sceneIndex].scriptTexts[text].extraLineHeight != -1) {
extraLineHeight = scriptPages[sceneIndex].scriptTexts[text].extraLineHeight;
}
xOffset = ((canvasW - 320) >> 1) + textRect.x + x;
yOffset = ((canvasH - 200) >> 1) + textRect.y + y;
switch (scriptPages[sceneIndex].scriptTexts[text].alignment) {
case 0: // left
break;
case 1: // right
xOffset += textRect.w - font->getSceneStringWidth(scriptPages[sceneIndex].scriptTexts[text].text);
break;
case 2: // center
xOffset += (textRect.w - font->getSceneStringWidth(scriptPages[sceneIndex].scriptTexts[text].text)) >> 1;
break;
}
// Drop shadow
font->mapPalette(0, 256, 0, 1);
font->showSceneString(scriptPages[sceneIndex].scriptTexts[text].text, xOffset + 1, yOffset + 1);
font->restorePalette();
// Text itself
font->showSceneString(scriptPages[sceneIndex].scriptTexts[text].text, xOffset, yOffset);
y += extraLineHeight + font->getHeight() / 2;
}
// Fade in from black
if (fadein) {
fadein = false;
firstPE = new FadeInPaletteEffect(250, firstPE);
clearScreen(0);
}
}
return E_NONE;
}
/*
*
* scene.h
*
* 3rd February 2009: Created scene.h from parts of scene.c
*
* Part of the OpenJazz project
*
*
* Copyright (c) 2005-2010 Alister Thomson
*
* OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef _SCENE_H
#define _SCENE_H
#include <io/file.h>
#include <SDL/SDL.h>
// Enums
// These are the known script types
enum {
ESceneMusic = 0x2A,
ESceneFadeType = 0x3F,
ESceneTextBlock = 0x40,
ESceneTextColour = 0x41,
ESceneFontFun = 0x45,
ESceneFontIndex = 0x46,
ESceneTextPosition = 0x47,
ESceneTextAlign = 0x4A,
ESceneTextAlign2 = 0x4B,
ESceneBackground = 0x4c,
ESceneBreaker = 0x50,
ESceneSomethingElse = 0x51,
ESceneTextRect = 0x57,
ESceneFontDefine = 0x58,
ESceneTime = 0x5d,
ESceneTextLine = 0x5e,
ESceneTextVAdjust = 0x5f,
ESceneAnimationIndex = 0xA7,
ESceneAnimationSetting = 0xA6,
ESceneTextSetting = 0xb1,
ESceneTextSomething = 0xd9,
ESceneTextShadow = 0xdb
};
// Classes
class Font;
class ScriptFont {
public:
// This points to any of the available types (already loaded)
Font *font;
// Font id given this font
int fontId;
};
class ScriptText {
public:
char *text;
int alignment;
int fontId;
int x;
int y;
SDL_Rect textRect;
int extraLineHeight;
ScriptText ();
~ScriptText ();
};
class ScriptPage {
public:
int backgrounds;
int bgIndex[30];
unsigned int bgPos[30];
// Length of the scene in seconds, or if zero = anim complete, or 256 = user interaction
int pageTime;
ScriptText scriptTexts[100];
int noScriptTexts;
char *musicfile;
int paletteIndex;
ScriptPage();
~ScriptPage();
};
class ImageInfo {
public:
// SDL_Surface with the image
SDL_Surface *image;
// data index of the image (not the palette) to compare with scripts
int dataIndex;
ImageInfo ();
~ImageInfo ();
};
class PaletteInfo {
public:
// Palette associated with the image
SDL_Color palette[256];
// data index of the image (not the palette) to compare with scripts
int dataIndex;
};
class Scene {
private:
ImageInfo imageInfos[100];
PaletteInfo paletteInfos[100];
unsigned short int scriptItems;
unsigned short int dataItems;
signed long int *scriptStarts;
signed long int *dataOffsets;
int imageIndex;
int paletteIndex;
// Scripts all information needed to render script pages, text etc
ScriptPage *scriptPages;
void loadScripts (File *f);
void loadData (File *f);
void loadAni (File* f, int dataIndex);
ImageInfo * FindImage (int dataIndex);
public:
ScriptFont scriptFonts[5];
int noScriptFonts;
Scene (const char * fileName);
~Scene ();
int play ();
};
#endif
This diff is collapsed.
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