Added Graphics::DrawText which can draw text on to an image with alignment. No multi-line support yet.

This commit is contained in:
Fatbag 2012-04-09 23:58:16 -05:00
parent 7d9259b63d
commit 7a5124a687
8 changed files with 145 additions and 35 deletions

View file

@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 2.6)
project(NiotsoClient) project(NiotsoClient)
include_directories(${CMAKE_SOURCE_DIR}/Libraries/FileHandler) include_directories(${CMAKE_SOURCE_DIR}/Libraries/FileHandler)
include_directories(${CMAKE_SOURCE_DIR}/Libraries/freetype/include)
if(WIN32) if(WIN32)
set(NIOTSOCLIENT_SOURCES set(NIOTSOCLIENT_SOURCES
@ -9,11 +10,12 @@ if(WIN32)
MessageHandler.cpp MessageHandler.cpp
Audio/Startup.cpp Audio/Startup.cpp
Audio/windows/XAudio2.cpp Audio/windows/XAudio2.cpp
Graphics/Font.cpp
Graphics/Startup.cpp Graphics/Startup.cpp
Graphics/Viewport.cpp Graphics/Viewport.cpp
resources/Resource.rc resources/Resource.rc
System/System.cpp System/System.cpp
) )
add_executable(TSO WIN32 ${NIOTSOCLIENT_SOURCES}) add_executable(TSO WIN32 ${NIOTSOCLIENT_SOURCES})
target_link_libraries(TSO FileHandler_shared ole32 opengl32) target_link_libraries(TSO FileHandler_shared freetype_shared ole32 opengl32)
endif() endif()

View file

@ -69,8 +69,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
} }
CurrentScene = new LoginScreen(); CurrentScene = new LoginScreen();
if(System::SceneFailed || CurrentScene == NULL){ if(CurrentScene == NULL || System::SceneFailed){
if(System::SceneFailed) delete CurrentScene;
Shutdown(); Shutdown();
return ERROR_INIT | ERROR_INIT_LOGIC | ERROR_LOGIC_CREATE_SCENE; return ERROR_INIT | ERROR_INIT_LOGIC | ERROR_LOGIC_CREATE_SCENE;
} }
@ -99,6 +98,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
if(result == System::SHUTDOWN) if(result == System::SHUTDOWN)
break; break;
if(result > 0){ if(result > 0){
glClear(GL_COLOR_BUFFER_BIT);
CurrentScene->Render(); CurrentScene->Render();
SwapBuffers(Graphics::hDC); SwapBuffers(Graphics::hDC);
} }
@ -130,7 +130,7 @@ int CreateWindowInvisible(HINSTANCE hInst, unsigned Width, unsigned Height, bool
NULL, //hCursor NULL, //hCursor
NULL, //hbrBackground NULL, //hbrBackground
NULL, //lpszMenuName NULL, //lpszMenuName
"TSO_NIOTSO" //lpszClassName "TSO_NIOTSO" //lpszClassName
}; };
if(!RegisterClass(&wc)){ if(!RegisterClass(&wc)){
@ -195,6 +195,7 @@ void Shutdown()
{ {
Audio::Shutdown(); Audio::Shutdown();
Graphics::Shutdown(); Graphics::Shutdown();
System::Shutdown();
if(Window::hWnd){ if(Window::hWnd){
DestroyWindow(Window::hWnd); DestroyWindow(Window::hWnd);

View file

@ -24,6 +24,15 @@
#define NULL 0 #define NULL 0
#include "FileHandler.hpp" #include "FileHandler.hpp"
#include "ft2build.h"
#include FT_FREETYPE_H
#ifndef min
#define min(x,y) ((x)<(y)?(x):(y))
#endif
#ifndef max
#define max(x,y) ((x)>(y)?(x):(y))
#endif
//IsometricEngine.cpp //IsometricEngine.cpp
namespace Window { namespace Window {

View file

@ -27,4 +27,19 @@ namespace Graphics {
int InitGL(); int InitGL();
void ResizeViewport(unsigned width, unsigned height); void ResizeViewport(unsigned width, unsigned height);
enum TextAlignment {
ALIGN_LEFT_TOP,
ALIGN_LEFT_CENTER,
ALIGN_CENTER_TOP,
ALIGN_CENTER_CENTER,
ALIGN_RIGHT_TOP,
ALIGN_RIGHT_CENTER
};
//Font.cpp
extern FT_Library FreeTypeLibrary;
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);
} }

View file

@ -95,6 +95,7 @@ int InitGL(){
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glEnable(GL_RESCALE_NORMAL); glEnable(GL_RESCALE_NORMAL);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND); glDisable(GL_BLEND);
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

View file

@ -15,6 +15,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <GL/gl.h>
#define EXIT_SCENE() do { System::SceneFailed = true; delete this; return; } while(0) #define EXIT_SCENE() do { System::SceneFailed = true; delete this; return; } while(0)
#define SCENE_EXIT 0 #define SCENE_EXIT 0
#define SCENE_NEED_REDRAW 1 #define SCENE_NEED_REDRAW 1
@ -22,11 +24,12 @@
class Scene { class Scene {
const float TickPeriod; const float TickPeriod;
float TimeDelta; float RealTimeDelta;
virtual int Run(float TimeDelta) = 0; virtual int Run(float TimeDelta) = 0;
protected: protected:
Scene(float c) : TickPeriod(c), TimeDelta(0) {} float TimeDelta;
Scene(float c) : TickPeriod(c), RealTimeDelta(0) {}
public: public:
int RunFor(float TimeDelta) { int RunFor(float TimeDelta) {
@ -35,54 +38,95 @@ class Scene {
} }
bool Redraw = false; bool Redraw = false;
this->TimeDelta += TimeDelta; RealTimeDelta += TimeDelta;
while(this->TimeDelta >= 0){ while(RealTimeDelta >= 0){
int result = Run(TickPeriod); int result = Run(TickPeriod);
if(result == System::SHUTDOWN) if(result == System::SHUTDOWN)
return System::SHUTDOWN; return System::SHUTDOWN;
if(result > 0) Redraw = true; if(result > 0) Redraw = true;
this->TimeDelta -= TickPeriod; RealTimeDelta -= TickPeriod;
} }
return (Redraw) ? 1 : -1; return (Redraw) ? 1 : -1;
} }
virtual void Render() = 0; virtual void Render() = 0;
virtual ~Scene() {};
}; };
enum {
TEX_EAGAMES,
TEX_MAXIS,
TEX_SETUP,
TEX_COUNT
};
static const char * const images[] = {"eagames.bmp", "maxis.png", "setup.bmp"};
class LoginScreen : public Scene { class LoginScreen : public Scene {
float Time;
GLuint texture[TEX_COUNT];
public: public:
LoginScreen() : Scene(1.0f/15){ LoginScreen() : Scene(1.0f/15){
Image_t * Image = File::ReadImageFile("eagames.bmp"); Time = 0;
if(!Image){
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]; glMatrixMode(GL_TEXTURE);
sprintf(Buffer, Message, "eagames.bmp"); glGenTextures(TEX_COUNT, texture);
MessageBox(Window::hWnd, Buffer, NULL, MB_OK | MB_ICONERROR);
EXIT_SCENE(); for(int i=0; i<TEX_COUNT; i++){
Image_t * Image = File::ReadImageFile(images[i]);
if(!Image){
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, images[i]);
MessageBox(Window::hWnd, Buffer, NULL, MB_OK | MB_ICONERROR);
EXIT_SCENE();
}
if(i == TEX_MAXIS){
FT_Set_Char_Size(Graphics::FontFace, 0, 22*64, 0, 0);
Graphics::DrawText(Image, L"Maxis\x2122 is an Electronic Arts\x2122 brand.", 0, 600-146, 800, 146,
Graphics::ALIGN_CENTER_CENTER, 0, RGB(0xef, 0xe3, 0x8c));
}
glBindTexture(GL_TEXTURE_2D, texture[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_RGB8, Image->Width, Image->Height, 0, GL_BGR, GL_UNSIGNED_BYTE, Image->Data);
free(Image->Data);
free(Image);
} }
} }
~LoginScreen(){
glDeleteTextures(TEX_COUNT, texture);
}
private: private:
int Run(float){ int Run(float TimeDelta){
Time += TimeDelta;
if(System::UserInput.CloseWindow){ if(System::UserInput.CloseWindow){
return SCENE_EXIT; return SCENE_EXIT;
} }
@ -91,5 +135,13 @@ class LoginScreen : public Scene {
public: public:
void Render(){ void Render(){
glMatrixMode(GL_TEXTURE);
glBindTexture(GL_TEXTURE_2D, texture[(Time>=4) + (Time>=8)]);
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();
} }
}; };

View file

@ -34,7 +34,32 @@ namespace System {
DEVMODE dm; DEVMODE dm;
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm); EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);
System::FramePeriod = 1.0f/dm.dmDisplayFrequency; System::FramePeriod = 1.0f/dm.dmDisplayFrequency;
if(FT_Init_FreeType(&Graphics::FreeTypeLibrary)){
MessageBox(Window::hWnd, "Failed to initialize FreeType.", NULL, MB_OK | MB_ICONERROR);
Graphics::FreeTypeLibrary = NULL;
Shutdown();
return ERROR_SYSTEM_INIT_FREETYPE;
};
if(FT_New_Face(Graphics::FreeTypeLibrary, "simdialogue-uni-game.ttf", 0, &Graphics::FontFace)){
MessageBox(Window::hWnd, "simdialogue-uni-game.ttf does not exist or is corrupt or invalid.",
NULL, MB_OK | MB_ICONERROR);
Graphics::FontFace = NULL;
Shutdown();
return ERROR_SYSTEM_MISSING_FONT;
}
return 0; return 0;
} }
void Shutdown(){
if(Graphics::FontFace){
FT_Done_Face(Graphics::FontFace);
Graphics::FontFace = NULL;
}
if(Graphics::FreeTypeLibrary){
FT_Done_FreeType(Graphics::FreeTypeLibrary);
Graphics::FreeTypeLibrary = NULL;
}
}
} }

View file

@ -18,6 +18,7 @@
//System/System.cpp //System/System.cpp
namespace System { namespace System {
int Initialize(); int Initialize();
void Shutdown();
extern HINSTANCE hInst; extern HINSTANCE hInst;
extern HANDLE Process; extern HANDLE Process;
extern HANDLE ProcessHeap; extern HANDLE ProcessHeap;
@ -55,6 +56,10 @@ namespace System {
ERROR_CREATE_WINDOW ERROR_CREATE_WINDOW
}; };
#define ERROR_INIT_SYSTEM 0x0200 #define ERROR_INIT_SYSTEM 0x0200
enum {
ERROR_SYSTEM_INIT_FREETYPE = 1,
ERROR_SYSTEM_MISSING_FONT
};
#define ERROR_INIT_GRAPHICS 0x0300 #define ERROR_INIT_GRAPHICS 0x0300
enum { enum {
ERROR_GRAPHICS_OBTAIN_DC = 1, ERROR_GRAPHICS_OBTAIN_DC = 1,