Added SDL_ttf to the project and attempted to draw to the screen but nothing is visible
authorIvan Hernanez <iturtleman128@gmail.com>
Mon, 28 May 2012 04:00:52 +0000 (23:00 -0500)
committerIvan Hernanez <iturtleman128@gmail.com>
Mon, 28 May 2012 04:00:52 +0000 (23:00 -0500)
12 files changed:
Animation.cpp
ArtAssets/Sprites/sun.anim
FreeSans.ttf [new file with mode: 0644]
Game.cpp
Level.cpp
LevelFns.h
SDL_ttf.dll [new file with mode: 0644]
SDL_ttf.h [new file with mode: 0644]
SDL_ttf.pdb [new file with mode: 0644]
SDL_ttf_Windows.dll [new file with mode: 0644]
SDL_ttf_Windows.pdb [new file with mode: 0644]
game.vcxproj

index 1ea3328..902cbe9 100644 (file)
@@ -102,12 +102,12 @@ int Animation::loadAnimation(std::string animFile)
 
                                // Check that the image\92s width is a power of 2
                                if ( (surface->w & (surface->w - 1)) != 0 ) {
-                                       cout<<"warning: image.bmp's width is not a power of 2"<<endl;
+                                       cout<<"warning: "<<file<<"'s width is not a power of 2"<<endl;
                                }
 
                                // Also check if the height is a power of 2
                                if ( (surface->h & (surface->h - 1)) != 0 ) {
-                                       cout<<"warning: image.bmp's height is not a power of 2"<<endl;
+                                       cout<<"warning: "<<file<<"'s height is not a power of 2"<<endl;
                                }
 
                                //get number of channels in the SDL surface
index f750806..c4acde7 100644 (file)
@@ -3,8 +3,8 @@
      pause: 20
      transparency:
           r: 0
-          g: 255
-          b: 0
+          g: 0
+          b: 255
      offset:
           x: 0.0
           y: 0.0
@@ -20,8 +20,8 @@
      pause: 20
      transparency:
           r: 0
-          g: 255
-          b: 0
+          g: 0
+          b: 255
      offset:
           x: 0.0
           y: 0.0
@@ -37,8 +37,8 @@
      pause: 20
      transparency:
           r: 0
-          g: 255
-          b: 0
+          g: 0
+          b: 255
      offset:
           x: 0.0
           y: 0.0
@@ -54,8 +54,8 @@
      pause: 20
      transparency:
           r: 0
-          g: 255
-          b: 0
+          g: 0
+          b: 255
      offset:
           x: 0.0
           y: 0.0
diff --git a/FreeSans.ttf b/FreeSans.ttf
new file mode 100644 (file)
index 0000000..6de62eb
Binary files /dev/null and b/FreeSans.ttf differ
index 1fce064..d793d46 100644 (file)
--- a/Game.cpp
+++ b/Game.cpp
@@ -3,6 +3,7 @@
 #include <string>
 #include <iostream>
 #include <GL/gl.h>
+#include "SDL_ttf.h"
 
 const Uint32 Game::waitTime = 1000/60; /* ms */
 
@@ -52,7 +53,14 @@ Game::Game() : mCurrentLevel(0), mScreen(0), ShowCollisions(false), ShowFPS(fals
                exit (1);
        }
        atexit (SDL_Quit);
-
+       // Initialize SDL_ttf library
+   if (TTF_Init() != 0)
+   {
+      cerr << "TTF_Init() Failed: " << TTF_GetError() << endl;
+      SDL_Quit();
+      exit(1);
+   }
+       atexit (TTF_Quit);
        /** Set 800x600 video mode */
        mScreen = SDL_SetVideoMode(screenwidth, screenheight, 32, SDL_OPENGL);
        if (!mScreen) {
index ba366b9..3fe9e7e 100644 (file)
--- a/Level.cpp
+++ b/Level.cpp
@@ -2,6 +2,7 @@
 #include "Game.h"
 #include "fns.h"
 #include <GL/gl.h>
+#include "SDL_ttf.h"
 
 Level::Level(SDL_Surface* screen) : mScreen(screen)
 {
@@ -25,44 +26,76 @@ Level::~Level()
 
 void Level::DrawIMG(SDL_Surface *img, int x, int y)
 {
-    SDL_Rect dest;
-    dest.x = x;
-    dest.y = y;
-    SDL_BlitSurface(img, NULL, mScreen, &dest);
+       SDL_Rect dest;
+       dest.x = x;
+       dest.y = y;
+       SDL_BlitSurface(img, NULL, mScreen, &dest);
 }
 
 
 void Level::DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
 {
-    SDL_Rect dest;
-    dest.x = x;
-    dest.y = y;
-    SDL_Rect src;
-    src.x = x2;
-    src.y = y2;
-    src.w = w;
-    src.h = h;
-    SDL_BlitSurface(img, &src, mScreen, &dest);
+       SDL_Rect dest;
+       dest.x = x;
+       dest.y = y;
+       SDL_Rect src;
+       src.x = x2;
+       src.y = y2;
+       src.w = w;
+       src.h = h;
+       SDL_BlitSurface(img, &src, mScreen, &dest);
 }
 
-
 void Level::drawScene()
 {
        Uint32 color;
 
-    /** Create a black mBackgroundground using the mScreen pixel format (32 bpp) */
+       /** Create a black mBackgroundground using the mScreen pixel format (32 bpp) */
        glClear(GL_COLOR_BUFFER_BIT);
-       
+
        /** Draw BG */
        if(mBackground!=NULL)
                mBackground->draw();
-       
+
        /** Draw Sprites*/
        DrawSprites();
-    
+
        /** Draw Boundary Data */
        DrawCollisions();
 
+       // Load a font
+       TTF_Font *font;
+       font = TTF_OpenFont("FreeSans.ttf", 24);
+
+       if(font){
+               // Write text to surface
+               SDL_Surface *text;
+               SDL_Color text_color = {255, 255, 255};
+               string txt="A journey of a thousand miles begins with a single step.";
+               text = TTF_RenderText_Solid(font, txt.c_str(), text_color);
+
+               if (text == NULL)
+               {
+                       cerr << "TTF_RenderText_Solid() Failed: " << TTF_GetError() << endl;
+                       TTF_Quit();
+                       SDL_Quit();
+                       exit(1);
+               }
+
+               // Apply the text to the display
+               if (SDL_BlitSurface(text, NULL, Game::game()->Screen(), NULL) != 0)
+               {
+                       cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
+               }
+               else
+                       cout<<"Drew: "<<txt<<endl;
+
+               SDL_FreeSurface(text);
+       }
+       else
+               cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl;
+
+       TTF_CloseFont(font);
        /** Flip the working image buffer with the mScreen buffer */
        SDL_GL_SwapBuffers();
 }
@@ -93,8 +126,8 @@ void Level::postEvent(SDL_Event event)
 {
        switch (event.type) {
        case SDL_KEYDOWN: {
-               
-       }
+
+                                         }
        }
 }
 
index 854190a..13d0333 100644 (file)
@@ -2,7 +2,7 @@
 #define __LEVELFNS_H__
 #include "Game.h"
 #include "Level.h"
-
+#include "SDL_ttf.h"
 ///\file this file should contain all Behaviors for Levels
 
 void LevelWorldLoad(){
diff --git a/SDL_ttf.dll b/SDL_ttf.dll
new file mode 100644 (file)
index 0000000..a1b058b
Binary files /dev/null and b/SDL_ttf.dll differ
diff --git a/SDL_ttf.h b/SDL_ttf.h
new file mode 100644 (file)
index 0000000..6c041d5
--- /dev/null
+++ b/SDL_ttf.h
@@ -0,0 +1,249 @@
+/*
+  SDL_ttf:  A companion library to SDL for working with TrueType (tm) fonts
+  Copyright (C) 2001-2012 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+/* This library is a wrapper around the excellent FreeType 2.0 library,
+   available at:
+       http://www.freetype.org/
+*/
+
+#ifndef _SDL_TTF_H
+#define _SDL_TTF_H
+
+#include <SDL/SDL.h>
+#include <SDL/begin_code.h>
+
+/* Set up for C function definitions, even when using C++ */
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL
+*/
+#define SDL_TTF_MAJOR_VERSION  2
+#define SDL_TTF_MINOR_VERSION  0
+#define SDL_TTF_PATCHLEVEL     11
+
+/* This macro can be used to fill a version structure with the compile-time
+ * version of the SDL_ttf library.
+ */
+#define SDL_TTF_VERSION(X)                                             \
+{                                                                      \
+       (X)->major = SDL_TTF_MAJOR_VERSION;                             \
+       (X)->minor = SDL_TTF_MINOR_VERSION;                             \
+       (X)->patch = SDL_TTF_PATCHLEVEL;                                \
+}
+
+/* Backwards compatibility */
+#define TTF_MAJOR_VERSION      SDL_TTF_MAJOR_VERSION
+#define TTF_MINOR_VERSION      SDL_TTF_MINOR_VERSION
+#define TTF_PATCHLEVEL         SDL_TTF_PATCHLEVEL
+#define TTF_VERSION(X)         SDL_TTF_VERSION(X)
+
+/* This function gets the version of the dynamically linked SDL_ttf library.
+   it should NOT be used to fill a version structure, instead you should
+   use the SDL_TTF_VERSION() macro.
+ */
+extern DECLSPEC const SDL_version * SDLCALL TTF_Linked_Version(void);
+
+/* ZERO WIDTH NO-BREAKSPACE (Unicode byte order mark) */
+#define UNICODE_BOM_NATIVE     0xFEFF
+#define UNICODE_BOM_SWAPPED    0xFFFE
+
+/* This function tells the library whether UNICODE text is generally
+   byteswapped.  A UNICODE BOM character in a string will override
+   this setting for the remainder of that string.
+*/
+extern DECLSPEC void SDLCALL TTF_ByteSwappedUNICODE(int swapped);
+
+/* The internal structure containing font information */
+typedef struct _TTF_Font TTF_Font;
+
+/* Initialize the TTF engine - returns 0 if successful, -1 on error */
+extern DECLSPEC int SDLCALL TTF_Init(void);
+
+/* Open a font file and create a font of the specified point size.
+ * Some .fon fonts will have several sizes embedded in the file, so the
+ * point size becomes the index of choosing which size.  If the value
+ * is too high, the last indexed size will be the default. */
+extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFont(const char *file, int ptsize);
+extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndex(const char *file, int ptsize, long index);
+extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontRW(SDL_RWops *src, int freesrc, int ptsize);
+extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndexRW(SDL_RWops *src, int freesrc, int ptsize, long index);
+
+/* Set and retrieve the font style */
+#define TTF_STYLE_NORMAL       0x00
+#define TTF_STYLE_BOLD         0x01
+#define TTF_STYLE_ITALIC       0x02
+#define TTF_STYLE_UNDERLINE    0x04
+#define TTF_STYLE_STRIKETHROUGH        0x08
+extern DECLSPEC int SDLCALL TTF_GetFontStyle(const TTF_Font *font);
+extern DECLSPEC void SDLCALL TTF_SetFontStyle(TTF_Font *font, int style);
+extern DECLSPEC int SDLCALL TTF_GetFontOutline(const TTF_Font *font);
+extern DECLSPEC void SDLCALL TTF_SetFontOutline(TTF_Font *font, int outline);
+
+/* Set and retrieve FreeType hinter settings */
+#define TTF_HINTING_NORMAL    0
+#define TTF_HINTING_LIGHT     1
+#define TTF_HINTING_MONO      2
+#define TTF_HINTING_NONE      3
+extern DECLSPEC int SDLCALL TTF_GetFontHinting(const TTF_Font *font);
+extern DECLSPEC void SDLCALL TTF_SetFontHinting(TTF_Font *font, int hinting);
+
+/* Get the total height of the font - usually equal to point size */
+extern DECLSPEC int SDLCALL TTF_FontHeight(const TTF_Font *font);
+
+/* Get the offset from the baseline to the top of the font
+   This is a positive value, relative to the baseline.
+ */
+extern DECLSPEC int SDLCALL TTF_FontAscent(const TTF_Font *font);
+
+/* Get the offset from the baseline to the bottom of the font
+   This is a negative value, relative to the baseline.
+ */
+extern DECLSPEC int SDLCALL TTF_FontDescent(const TTF_Font *font);
+
+/* Get the recommended spacing between lines of text for this font */
+extern DECLSPEC int SDLCALL TTF_FontLineSkip(const TTF_Font *font);
+
+/* Get/Set whether or not kerning is allowed for this font */
+extern DECLSPEC int SDLCALL TTF_GetFontKerning(const TTF_Font *font);
+extern DECLSPEC void SDLCALL TTF_SetFontKerning(TTF_Font *font, int allowed);
+
+/* Get the number of faces of the font */
+extern DECLSPEC long SDLCALL TTF_FontFaces(const TTF_Font *font);
+
+/* Get the font face attributes, if any */
+extern DECLSPEC int SDLCALL TTF_FontFaceIsFixedWidth(const TTF_Font *font);
+extern DECLSPEC char * SDLCALL TTF_FontFaceFamilyName(const TTF_Font *font);
+extern DECLSPEC char * SDLCALL TTF_FontFaceStyleName(const TTF_Font *font);
+
+/* Check wether a glyph is provided by the font or not */
+extern DECLSPEC int SDLCALL TTF_GlyphIsProvided(const TTF_Font *font, Uint16 ch);
+
+/* Get the metrics (dimensions) of a glyph
+   To understand what these metrics mean, here is a useful link:
+    http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
+ */
+extern DECLSPEC int SDLCALL TTF_GlyphMetrics(TTF_Font *font, Uint16 ch,
+                                    int *minx, int *maxx,
+                                     int *miny, int *maxy, int *advance);
+
+/* Get the dimensions of a rendered string of text */
+extern DECLSPEC int SDLCALL TTF_SizeText(TTF_Font *font, const char *text, int *w, int *h);
+extern DECLSPEC int SDLCALL TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h);
+extern DECLSPEC int SDLCALL TTF_SizeUNICODE(TTF_Font *font, const Uint16 *text, int *w, int *h);
+
+/* Create an 8-bit palettized surface and render the given text at
+   fast quality with the given font and color.  The 0 pixel is the
+   colorkey, giving a transparent background, and the 1 pixel is set
+   to the text color.
+   This function returns the new surface, or NULL if there was an error.
+*/
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Solid(TTF_Font *font,
+                               const char *text, SDL_Color fg);
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Solid(TTF_Font *font,
+                               const char *text, SDL_Color fg);
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Solid(TTF_Font *font,
+                               const Uint16 *text, SDL_Color fg);
+
+/* Create an 8-bit palettized surface and render the given glyph at
+   fast quality with the given font and color.  The 0 pixel is the
+   colorkey, giving a transparent background, and the 1 pixel is set
+   to the text color.  The glyph is rendered without any padding or
+   centering in the X direction, and aligned normally in the Y direction.
+   This function returns the new surface, or NULL if there was an error.
+*/
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Solid(TTF_Font *font,
+                                       Uint16 ch, SDL_Color fg);
+
+/* Create an 8-bit palettized surface and render the given text at
+   high quality with the given font and colors.  The 0 pixel is background,
+   while other pixels have varying degrees of the foreground color.
+   This function returns the new surface, or NULL if there was an error.
+*/
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Shaded(TTF_Font *font,
+                               const char *text, SDL_Color fg, SDL_Color bg);
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Shaded(TTF_Font *font,
+                               const char *text, SDL_Color fg, SDL_Color bg);
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Shaded(TTF_Font *font,
+                               const Uint16 *text, SDL_Color fg, SDL_Color bg);
+
+/* Create an 8-bit palettized surface and render the given glyph at
+   high quality with the given font and colors.  The 0 pixel is background,
+   while other pixels have varying degrees of the foreground color.
+   The glyph is rendered without any padding or centering in the X
+   direction, and aligned normally in the Y direction.
+   This function returns the new surface, or NULL if there was an error.
+*/
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Shaded(TTF_Font *font,
+                               Uint16 ch, SDL_Color fg, SDL_Color bg);
+
+/* Create a 32-bit ARGB surface and render the given text at high quality,
+   using alpha blending to dither the font with the given color.
+   This function returns the new surface, or NULL if there was an error.
+*/
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Blended(TTF_Font *font,
+                               const char *text, SDL_Color fg);
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Blended(TTF_Font *font,
+                               const char *text, SDL_Color fg);
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended(TTF_Font *font,
+                               const Uint16 *text, SDL_Color fg);
+
+/* Create a 32-bit ARGB surface and render the given glyph at high quality,
+   using alpha blending to dither the font with the given color.
+   The glyph is rendered without any padding or centering in the X
+   direction, and aligned normally in the Y direction.
+   This function returns the new surface, or NULL if there was an error.
+*/
+extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Blended(TTF_Font *font,
+                                               Uint16 ch, SDL_Color fg);
+
+/* For compatibility with previous versions, here are the old functions */
+#define TTF_RenderText(font, text, fg, bg)     \
+       TTF_RenderText_Shaded(font, text, fg, bg)
+#define TTF_RenderUTF8(font, text, fg, bg)     \
+       TTF_RenderUTF8_Shaded(font, text, fg, bg)
+#define TTF_RenderUNICODE(font, text, fg, bg)  \
+       TTF_RenderUNICODE_Shaded(font, text, fg, bg)
+
+/* Close an opened font file */
+extern DECLSPEC void SDLCALL TTF_CloseFont(TTF_Font *font);
+
+/* De-initialize the TTF engine */
+extern DECLSPEC void SDLCALL TTF_Quit(void);
+
+/* Check if the TTF engine is initialized */
+extern DECLSPEC int SDLCALL TTF_WasInit(void);
+
+/* Get the kerning size of two glyphs */
+extern DECLSPEC int TTF_GetFontKerningSize(TTF_Font *font, int prev_index, int index);
+
+/* We'll use SDL for reporting errors */
+#define TTF_SetError   SDL_SetError
+#define TTF_GetError   SDL_GetError
+
+/* Ends C function definitions when using C++ */
+#ifdef __cplusplus
+}
+#endif
+#include <SDL/close_code.h>
+
+#endif /* _SDL_TTF_H */
diff --git a/SDL_ttf.pdb b/SDL_ttf.pdb
new file mode 100644 (file)
index 0000000..0aeec64
Binary files /dev/null and b/SDL_ttf.pdb differ
diff --git a/SDL_ttf_Windows.dll b/SDL_ttf_Windows.dll
new file mode 100644 (file)
index 0000000..a1b058b
Binary files /dev/null and b/SDL_ttf_Windows.dll differ
diff --git a/SDL_ttf_Windows.pdb b/SDL_ttf_Windows.pdb
new file mode 100644 (file)
index 0000000..0aeec64
Binary files /dev/null and b/SDL_ttf_Windows.pdb differ
index 7eb644d..6cc0485 100644 (file)
@@ -56,7 +56,7 @@
       <SubSystem>Console</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <AdditionalLibraryDirectories>C:\SDL\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
-      <AdditionalDependencies>libyaml-cppmdd.lib;SDL.lib;SDL_image.lib;SDLmain.lib;SDL_draw.lib;glu32.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>SDL_ttf.lib;libyaml-cppmdd.lib;SDL.lib;SDL_image.lib;SDLmain.lib;SDL_draw.lib;glu32.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <IgnoreAllDefaultLibraries>false</IgnoreAllDefaultLibraries>
     </Link>
     <PostBuildEvent>