Graphics: Added font rendering functions Graphics::DrawText and Graphics::StringImage.

FileHandler: Added support for wav
Audio: Added support to play back a sound with XAudio2. We can't delete sounds until we make our own system to keep track of available voices.
LoginScreen scene: Upped framerate from 15fps to tickless, and added new logic to clone the scrolling text at the bottom. All of it.
This commit is contained in:
Fatbag 2012-04-20 19:37:08 -05:00
parent 64a5c0a425
commit 06f13d50ac
14 changed files with 429 additions and 162 deletions

View file

@ -16,8 +16,21 @@
*/
#include "windows/xaudio2.hpp"
#ifdef PlaySound //defined by the Windows API
#undef PlaySound
#endif
struct PlayableSound_t {
bool Playing;
uint8_t * Data;
IXAudio2SourceVoice* pSourceVoice;
};
namespace Audio {
int Initialize();
PlayableSound_t * LoadSound(const Sound_t * Sound);
bool PlaySound(PlayableSound_t * Sound);
bool StopSound(PlayableSound_t * Sound);
void DeleteSound(PlayableSound_t * Sound);
void Shutdown();
}

View file

@ -49,6 +49,65 @@ int Initialize(){
return 0;
}
PlayableSound_t * LoadSound(const Sound_t * Sound){
const WAVEFORMATEX wfx = {
WAVE_FORMAT_PCM, //wFormatTag
Sound->Channels, //nChannels
Sound->SamplingRate, //nSamplesPerSec
((Sound->Channels * Sound->BitDepth) >> 3) * Sound->SamplingRate, //nAvgBytesPerSec
((Sound->Channels * Sound->BitDepth) >> 3), //nBlockAlign
Sound->BitDepth, //wBitsPerSample;
0 //cbSize
};
const XAUDIO2_BUFFER buffer = {
0, //Flags
Sound->Duration * wfx.nBlockAlign, //AudioBytes
Sound->Data, //pAudioData
0, //PlayBegin
0, //PlayLength
0, //LoopBegin
0, //LoopLength
XAUDIO2_LOOP_INFINITE, //LoopCount
NULL, //pContext
};
IXAudio2SourceVoice* pSourceVoice;
if(FAILED(pXAudio2->CreateSourceVoice(&pSourceVoice, &wfx)))
return NULL;
if(FAILED(pSourceVoice->SubmitSourceBuffer(&buffer)))
return NULL;
PlayableSound_t * PlayableSound = (PlayableSound_t*) malloc(sizeof(PlayableSound_t));
if(!PlayableSound)
return NULL;
PlayableSound->pSourceVoice = pSourceVoice;
PlayableSound->Playing = false;
PlayableSound->Data = Sound->Data;
return PlayableSound;
}
bool PlaySound(PlayableSound_t * Sound){
if(!Sound->Playing && !FAILED(Sound->pSourceVoice->Start(0))){
Sound->Playing = true;
return true;
}
return false;
}
bool StopSound(PlayableSound_t * Sound){
int success = false;
if(Sound->Playing && !FAILED(Sound->pSourceVoice->Stop(0)))
success = true;
Sound->Playing = false;
return success;
}
void DeleteSound(PlayableSound_t * Sound){
StopSound(Sound);
//Sound->pSourceVoice->Release();
}
void Shutdown(){
if(MasterVoice){
MasterVoice->DestroyVoice();

View file

@ -23,6 +23,7 @@ DECLARE_INTERFACE(IXAudio2Voice);
#define XAUDIO2_DEFAULT_SAMPLERATE 0
#define XAUDIO2_DEFAULT_FREQ_RATIO 4.0f
#define XAUDIO2_DEBUG_ENGINE 0x0001
#define XAUDIO2_LOOP_INFINITE 255
#define XAUDIO2_VOICE_NOSRC 0x0004
enum XAUDIO2_DEVICE_ROLE

View file

@ -22,7 +22,7 @@ namespace Graphics {
FT_Library FreeTypeLibrary;
FT_Face FontFace;
static void FindStringSize(const wchar_t * String, unsigned * width, unsigned * height, int font){
static void FindStringSize(const wchar_t * String, unsigned * width, unsigned * height, int * xoffset, int * yoffset, int font){
int x = 0, y = 0;
int lowestx = 0, lowesty = 0, highestx = 0, highesty = 0;
@ -30,19 +30,22 @@ static void FindStringSize(const wchar_t * String, unsigned * width, unsigned *
int error = FT_Load_Char(FontFace, letter, FT_LOAD_RENDER);
if(error) continue;
if(x < lowestx) lowestx = x;
if(y < lowesty) lowesty = y;
int bottomx = x + FontFace->glyph->bitmap_left;
int bottomy = y + FontFace->glyph->bitmap_top - FontFace->glyph->bitmap.rows;
if(bottomx < lowestx) lowestx = bottomx;
if(bottomy < lowesty) lowesty = bottomy;
int newx = x + FontFace->glyph->bitmap.width + FontFace->glyph->bitmap_left;
int newy = y + FontFace->glyph->bitmap_top;
if(newx > highestx) highestx = newx;
if(newy > highesty) highesty = newy;
int topx = x + FontFace->glyph->bitmap_left + FontFace->glyph->bitmap.width;
int topy = y + FontFace->glyph->bitmap_top;
if(topx > highestx) highestx = topx;
if(topy > highesty) highesty = topy;
x += FontFace->glyph->advance.x >> 6;
y += FontFace->glyph->advance.y >> 6;
}
*width = highestx-lowestx, *height = highesty-lowesty;
*xoffset = -lowestx, *yoffset = -lowesty;
}
void DrawText(Image_t * Image, const wchar_t * String, int x, int y, unsigned width, unsigned height,
@ -52,13 +55,11 @@ void DrawText(Image_t * Image, const wchar_t * String, int x, int y, unsigned wi
//The destination image must be stored in bottom-up order.
if(x >= (signed)Image->Width || y >= (signed)Image->Height) return;
int Blue = GetBValue(Color), Green = GetGValue(Color), Red = GetRValue(Color);
//(stringx,stringy) will refer to the top-left of the string in bottom-up coordinates
int stringx, stringy;
unsigned StringWidth, StringHeight;
FindStringSize(String, &StringWidth, &StringHeight, font);
FindStringSize(String, &StringWidth, &StringHeight, &stringx, &stringy, font);
//Horizontal alignment
if(Alignment < 2) stringx = x; //Left
@ -98,11 +99,11 @@ void DrawText(Image_t * Image, const wchar_t * String, int x, int y, unsigned wi
int originalcolor;
originalcolor = *ptr;
*ptr++ = (uint8_t) (originalcolor + (int)((Blue-originalcolor)*2*value+255)/510);
*ptr++ = (uint8_t) (originalcolor + (int)((GetBValue(Color)-originalcolor)*2*value+255)/510);
originalcolor = *ptr;
*ptr++ = (uint8_t) (originalcolor + (int)((Green-originalcolor)*2*value+255)/510);
*ptr++ = (uint8_t) (originalcolor + (int)((GetGValue(Color)-originalcolor)*2*value+255)/510);
originalcolor = *ptr;
*ptr++ = (uint8_t) (originalcolor + (int)((Red-originalcolor)*2*value+255)/510);
*ptr++ = (uint8_t) (originalcolor + (int)((GetRValue(Color)-originalcolor)*2*value+255)/510);
}
}
stringx -= FontFace->glyph->bitmap_left;
@ -115,4 +116,61 @@ void DrawText(Image_t * Image, const wchar_t * String, int x, int y, unsigned wi
}
}
Image_t * StringImage(const wchar_t * String, int font, COLORREF Color){
Image_t * Image = (Image_t*) malloc(sizeof(Image_t));
if(Image == NULL) return NULL;
unsigned StringWidth, StringHeight;
int stringx, stringy;
FindStringSize(String, &StringWidth, &StringHeight, &stringx, &stringy, font);
Image->Data = (uint8_t*) malloc(4 * StringWidth * StringHeight);
if(Image->Data == NULL){
free(Image);
return NULL;
}
for(unsigned i=0; i<4*StringWidth*StringHeight;){
Image->Data[i++] = GetBValue(Color);
Image->Data[i++] = GetGValue(Color);
Image->Data[i++] = GetRValue(Color);
Image->Data[i++] = 0;
}
for(wchar_t letter=*String; letter!='\0'; letter=*(++String)){
int error = FT_Load_Char(FontFace, letter, FT_LOAD_RENDER);
if(error) continue;
int cWidth = FontFace->glyph->bitmap.width, cHeight = FontFace->glyph->bitmap.rows;
if(cWidth && cHeight){
uint8_t * cRender; /* Convert to Bottom-up */
uint8_t * OriginalRender = FontFace->glyph->bitmap.buffer;
if(FontFace->glyph->bitmap.pitch > 0){
cRender = (uint8_t *) malloc(cWidth * cHeight);
for(int i=0; i<cHeight; i++)
memcpy(cRender + i*cWidth, OriginalRender + (cHeight-i-1)*cWidth, cWidth);
}else cRender = OriginalRender;
stringx += FontFace->glyph->bitmap_left;
stringy += FontFace->glyph->bitmap_top-cHeight;
for(int i=0; i<cHeight; i++){
for(int j=0; j<cWidth; j++){
uint8_t *ptr = Image->Data + 4*((stringy+i)*StringWidth + (stringx+j));
ptr[3] = cRender[i*cWidth + j];
}
}
stringx -= FontFace->glyph->bitmap_left;
stringy -= FontFace->glyph->bitmap_top-cHeight;
if(FontFace->glyph->bitmap.pitch > 0) free(cRender);
}
stringx += FontFace->glyph->advance.x >> 6;
stringy += FontFace->glyph->advance.y >> 6;
}
Image->Width = StringWidth;
Image->Height = StringHeight;
Image->Format = FIMG_BGRA32;
return Image;
}
}

View file

@ -42,4 +42,5 @@ namespace Graphics {
extern FT_Face FontFace;
void DrawText(Image_t * Image, const wchar_t * String, int x, int y, unsigned width, unsigned height,
TextAlignment Alignment, int font, COLORREF Color);
Image_t * StringImage(const wchar_t * String, int font, COLORREF Color);
}

View file

@ -96,8 +96,9 @@ int InitGL(){
glEnable(GL_CULL_FACE);
glEnable(GL_RESCALE_NORMAL);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glEnable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return 0;
}

View file

@ -53,27 +53,62 @@ class Scene {
virtual ~Scene() {};
};
static const wchar_t * const StatusStrings[] = {
L"Extruding Terrain Web",
L"Adjusting Emotional Weights",
L"Calibrating Personality Matrix",
L"Calculating Domestic Coefficients",
L"Readjusting Career Ladder",
L"Accessing Money Supply",
L"Hacking the Social Network",
L"Tweaking Chaos Control",
L"Downloading Reticulated Splines"
};
enum {
IMG_COPYRIGHT,
IMG_STATUS, //value = 1..9
IMG_COUNT = 10
};
enum {
TEX_EAGAMES,
TEX_MAXIS,
TEX_SETUP,
TEX_COUNT
TEX_COPYRIGHT,
TEX_STATUS, //value = 4..12
TEX_COUNT = 13
};
static const char * const images[] = {"eagames.bmp", "maxis.png", "setup.bmp"};
enum {
SND_LOADLOOP,
SND_COUNT
};
static const char * const sounds[] = {"loadloop.wav"};
class LoginScreen : public Scene {
enum { Screen_EAGames, Screen_Maxis, Screen_Setup } Screen;
float Time;
float ScrollPos;
Image_t * image[IMG_COUNT];
GLuint texture[TEX_COUNT];
PlayableSound_t * sound[SND_COUNT];
public:
LoginScreen() : Scene(1.0f/15){
LoginScreen() : Scene(0){
Screen = Screen_EAGames;
Time = 0;
ScrollPos = -1;
memset(image, 0, IMG_COUNT * sizeof(Image_t *));
memset(texture, 0, TEX_COUNT * sizeof(GLuint));
memset(sound, 0, SND_COUNT * sizeof(PlayableSound_t *));
glMatrixMode(GL_TEXTURE);
glGenTextures(TEX_COUNT, texture);
for(int i=0; i<TEX_COUNT; i++){
for(int i=TEX_EAGAMES; i<=TEX_SETUP; i++){
Image_t * Image = File::ReadImageFile(images[i]);
if(!Image){
const char * Message;
@ -117,15 +152,100 @@ class LoginScreen : public Scene {
free(Image->Data);
free(Image);
}
image[IMG_COPYRIGHT] = Graphics::StringImage(L"(c) 2002, 2003 Electronic Arts Inc. All rights reserved.",
0, RGB(0xef, 0xe3, 0x8c));
if(image[IMG_COPYRIGHT] == NULL){
EXIT_SCENE();
}
glBindTexture(GL_TEXTURE_2D, texture[TEX_COPYRIGHT]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image[IMG_COPYRIGHT]->Width, image[IMG_COPYRIGHT]->Height, 0, GL_BGRA,
GL_UNSIGNED_BYTE, image[IMG_COPYRIGHT]->Data);
free(image[IMG_COPYRIGHT]->Data);
for(int i=0; i<9; i++){
image[IMG_STATUS+i] = Graphics::StringImage(StatusStrings[i], 0, RGB(0xef, 0xe3, 0x8c));
if(image[IMG_STATUS+i] == NULL){
EXIT_SCENE();
}
glBindTexture(GL_TEXTURE_2D, texture[TEX_STATUS+i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image[IMG_STATUS+i]->Width, image[IMG_STATUS+i]->Height, 0, GL_BGRA,
GL_UNSIGNED_BYTE, image[IMG_STATUS+i]->Data);
free(image[IMG_STATUS+i]->Data);
}
for(int i=0; i<SND_COUNT; i++){
Sound_t * Sound = File::ReadSoundFile(sounds[i]);
if(!Sound){
const char * Message;
switch(File::Error){
case FERR_NOT_FOUND:
Message = "%s does not exist.";
break;
case FERR_OPEN:
Message = "%s could not be opened for reading.";
break;
case FERR_BLANK:
case FERR_UNRECOGNIZED:
case FERR_INVALIDDATA:
Message = "%s is corrupt or invalid.";
break;
case FERR_MEMORY:
Message = "Memory for %s could not be allocated.";
break;
default:
Message = "%s could not be read.";
}
char Buffer[1024];
sprintf(Buffer, Message, sounds[i]);
MessageBox(Window::hWnd, Buffer, NULL, MB_OK | MB_ICONERROR);
EXIT_SCENE();
}
sound[i] = Audio::LoadSound(Sound);
free(Sound);
if(!sound){
MessageBox(Window::hWnd, "Sound could not be created", NULL, MB_OK | MB_ICONERROR);
EXIT_SCENE();
}
Audio::PlaySound(sound[i]);
}
}
~LoginScreen(){
for(int i=0; i<IMG_COUNT; i++){ if(image[i]) free(image[i]); }
glDeleteTextures(TEX_COUNT, texture);
for(int i=0; i<SND_COUNT; i++){
if(sound[i]){
Audio::DeleteSound(sound[i]);
free(sound[i]->Data);
free(sound[i]);
}
}
}
private:
int Run(float TimeDelta){
Time += TimeDelta;
if(ScrollPos != 8){
ScrollPos += TimeDelta*0.75;
if(ScrollPos > 8) ScrollPos = 8;
}
if(Screen != Screen_Setup && Time >= 4.0){
Screen = (Screen==Screen_EAGames) ? Screen_Maxis : Screen_Setup;
Time = 0;
}
if(System::UserInput.CloseWindow){
return SCENE_EXIT;
@ -136,12 +256,34 @@ class LoginScreen : public Scene {
public:
void Render(){
glMatrixMode(GL_TEXTURE);
glBindTexture(GL_TEXTURE_2D, texture[(Time>=4) + (Time>=8)]);
//Background
glBindTexture(GL_TEXTURE_2D, texture[Screen]);
glBegin(GL_QUADS);
glTexCoord2i(0,0); glVertex2i(0,0);
glTexCoord2i(1,0); glVertex2i(800,0);
glTexCoord2i(1,1); glVertex2i(800,600);
glTexCoord2i(0,1); glVertex2i(0,600);
glEnd();
if(Screen != Screen_Setup) return;
glBindTexture(GL_TEXTURE_2D, texture[TEX_COPYRIGHT]);
glBegin(GL_QUADS);
glTexCoord2i(0,0); glVertex2i((800-image[IMG_COPYRIGHT]->Width)/2,58);
glTexCoord2i(1,0); glVertex2i((800-image[IMG_COPYRIGHT]->Width)/2 + image[IMG_COPYRIGHT]->Width,58);
glTexCoord2i(1,1); glVertex2i((800-image[IMG_COPYRIGHT]->Width)/2 + image[IMG_COPYRIGHT]->Width,image[IMG_COPYRIGHT]->Height + 58);
glTexCoord2i(0,1); glVertex2i((800-image[IMG_COPYRIGHT]->Width)/2,image[IMG_COPYRIGHT]->Height + 58);
glEnd();
for(int i=0; i<9; i++){
glBindTexture(GL_TEXTURE_2D, texture[TEX_STATUS+i]);
glBegin(GL_QUADS);
glTexCoord2i(0,0); glVertex2i(((float)i - ScrollPos)*800 + (800-image[IMG_STATUS+i]->Width)/2,20);
glTexCoord2i(1,0); glVertex2i(((float)i - ScrollPos)*800 + (800-image[IMG_STATUS+i]->Width)/2 + image[IMG_STATUS+i]->Width,20);
glTexCoord2i(1,1); glVertex2i(((float)i - ScrollPos)*800 + (800-image[IMG_STATUS+i]->Width)/2 + image[IMG_STATUS+i]->Width,image[IMG_STATUS+i]->Height + 20);
glTexCoord2i(0,1); glVertex2i(((float)i - ScrollPos)*800 + (800-image[IMG_STATUS+i]->Width)/2,image[IMG_STATUS+i]->Height + 20);
glEnd();
}
}
};