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)
include_directories(${CMAKE_SOURCE_DIR}/Libraries/FileHandler)
include_directories(${CMAKE_SOURCE_DIR}/Libraries/freetype/include)
if(WIN32)
set(NIOTSOCLIENT_SOURCES
@ -9,11 +10,12 @@ if(WIN32)
MessageHandler.cpp
Audio/Startup.cpp
Audio/windows/XAudio2.cpp
Graphics/Font.cpp
Graphics/Startup.cpp
Graphics/Viewport.cpp
resources/Resource.rc
System/System.cpp
)
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()

View file

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

View file

@ -24,6 +24,15 @@
#define NULL 0
#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
namespace Window {

View file

@ -27,4 +27,19 @@ namespace Graphics {
int InitGL();
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_CULL_FACE);
glEnable(GL_RESCALE_NORMAL);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
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/>.
*/
#include <GL/gl.h>
#define EXIT_SCENE() do { System::SceneFailed = true; delete this; return; } while(0)
#define SCENE_EXIT 0
#define SCENE_NEED_REDRAW 1
@ -22,11 +24,12 @@
class Scene {
const float TickPeriod;
float TimeDelta;
float RealTimeDelta;
virtual int Run(float TimeDelta) = 0;
protected:
Scene(float c) : TickPeriod(c), TimeDelta(0) {}
float TimeDelta;
Scene(float c) : TickPeriod(c), RealTimeDelta(0) {}
public:
int RunFor(float TimeDelta) {
@ -35,54 +38,95 @@ class Scene {
}
bool Redraw = false;
this->TimeDelta += TimeDelta;
while(this->TimeDelta >= 0){
RealTimeDelta += TimeDelta;
while(RealTimeDelta >= 0){
int result = Run(TickPeriod);
if(result == System::SHUTDOWN)
return System::SHUTDOWN;
if(result > 0) Redraw = true;
this->TimeDelta -= TickPeriod;
RealTimeDelta -= TickPeriod;
}
return (Redraw) ? 1 : -1;
}
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 {
float Time;
GLuint texture[TEX_COUNT];
public:
LoginScreen() : Scene(1.0f/15){
Image_t * Image = File::ReadImageFile("eagames.bmp");
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.";
}
Time = 0;
char Buffer[1024];
sprintf(Buffer, Message, "eagames.bmp");
MessageBox(Window::hWnd, Buffer, NULL, MB_OK | MB_ICONERROR);
EXIT_SCENE();
glMatrixMode(GL_TEXTURE);
glGenTextures(TEX_COUNT, texture);
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:
int Run(float){
int Run(float TimeDelta){
Time += TimeDelta;
if(System::UserInput.CloseWindow){
return SCENE_EXIT;
}
@ -91,5 +135,13 @@ class LoginScreen : public Scene {
public:
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;
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);
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;
}
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
namespace System {
int Initialize();
void Shutdown();
extern HINSTANCE hInst;
extern HANDLE Process;
extern HANDLE ProcessHeap;
@ -55,6 +56,10 @@ namespace System {
ERROR_CREATE_WINDOW
};
#define ERROR_INIT_SYSTEM 0x0200
enum {
ERROR_SYSTEM_INIT_FREETYPE = 1,
ERROR_SYSTEM_MISSING_FONT
};
#define ERROR_INIT_GRAPHICS 0x0300
enum {
ERROR_GRAPHICS_OBTAIN_DC = 1,