diff --git a/README.md b/README.md index 0f8d1e0..58fdfcc 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ I'm building this while learning from [Lazy Foo' Productions' SDL Tutorials](http://lazyfoo.net/SDL_tutorials/). ## About -Trololo is written in C++ with SDL 1.2.12 (32 bit) with the SDL extension library: SDL_image (1.2.12). +Trololo is written in C++ with SDL 1.2.15 (32 bit) with the SDL extension libraries: SDL_image (1.2.12) and SDL_tff (2.0.11). ## License This software is released under the GNU General Public License Version 3. Please read the LICENSE file to know your rights before continuing. diff --git a/commonSDL.h b/commonSDL.h index 0321ab3..d5807dc 100644 --- a/commonSDL.h +++ b/commonSDL.h @@ -1,6 +1,7 @@ // SDL + Extensions #include "SDL/SDL.h" #include "SDL/SDL_image.h" +#include "SDL/SDL_ttf.h" // Standard #include @@ -37,7 +38,14 @@ SDL_Surface *StartUp(const char *title, int x, int y) return NULL; } - SDL_WM_SetCaption(title, NULL ); + SDL_WM_SetCaption(title, NULL); + + //Initialize SDL_ttf + if(TTF_Init() == -1) + { + fprintf(stderr, "True Type Font initialization failed: %s", SDL_GetError()); + return NULL; + } // Success: return video surface return screen; diff --git a/images/ttf/CaviarDreams.ttf b/images/ttf/CaviarDreams.ttf new file mode 100644 index 0000000..4ba4279 Binary files /dev/null and b/images/ttf/CaviarDreams.ttf differ diff --git a/main.cpp b/main.cpp index b651d26..b32dcd3 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ // SDL + Extensions #include "SDL/SDL.h" #include "SDL/SDL_image.h" +#include "SDL/SDL_ttf.h" // Common SDL functions for general processing. #include "commonSDL.h" @@ -13,6 +14,8 @@ SDL_Surface *screen = NULL; SDL_Surface *background = NULL; SDL_Surface *characters = NULL; +SDL_Surface *message = NULL; + // Image filenames const char *background_image = "stage1.bmp"; const char *characters_image = "characters.bmp"; @@ -55,10 +58,37 @@ int main(int argc, char* args[]) apply_image(0, 0, background, screen); // Blit troll image - apply_image(0, 0, characters, screen, &clip[0]); + apply_image(700, 40, characters, screen, &clip[0]); // Blit codfish image - apply_image(300, 300, characters, screen, &clip[1]); + apply_image(150, 300, characters, screen, &clip[1]); + + // Setup welcome text + SDL_Color whitecolor = { 255, 255, 255 }; + TTF_Font *font = TTF_OpenFont("CaviarDreams.ttf", 80); + const char* text = "Welcome to Trololo"; + + + if(!font) + { + fprintf(stderr, "TTF_OpenFont: %s\n", TTF_GetError()); + } + + /* + Checks if font or text is NULL due to the documentation: + NULL font into this function will cause a segfault. + NULL text into this function will result in undefined behavior. + */ + if(font != NULL && text != NULL) + message = TTF_RenderText_Solid(font, text, whitecolor); + else + fprintf(stderr, "TFF Render Text Solid Error: init error\n"); + + // Apply message + if(message != NULL) + apply_image(100, 200, message, screen); + else + fprintf(stderr, "TFF Render Text Solid Error: apply error\n"); if(screen != NULL) SDL_Flip(screen);