Drawing Text with SDL



Similar documents
APPLICATION NOTE. Getting Started with pylon and OpenCV

Installing Java 5.0 and Eclipse on Mac OS X

Visual Basic Programming. An Introduction

Creating Personal Web Sites Using SharePoint Designer 2007

Extracting Course Materials from Blackboard (For Instructors)

Building a Horizontal Menu in Dreamweaver CS3 Using Spry R. Berdan

iphone Objective-C Exercises

BT CONTENT SHOWCASE. JOOMLA EXTENSION User guide Version 2.1. Copyright 2013 Bowthemes Inc.

Outlook basics. Identify user interface elements that you can use to accomplish basic tasks.

2. Click the download button for your operating system (Windows, Mac, or Linux).

TUTORIAL 4 Building a Navigation Bar with Fireworks

O UTLOOK 2003 HELP SHEET MAIL. Opening the program. Mail

OpenCV Tutorial. Part I Using OpenCV with Microsoft Visual Studio.net November Gavin S Page gsp8334@cs.rit.edu

Match Branch Services to Enterprise Services in Kitomba One

Apple Mac Fundamentals: A Tutorial. Updated 24/4/2013 By Mac Thing enquiries@macthing.co.uk Table of Contents:

1 Strategic Planning Matrix v1,0 User s Guide

UniFinger Engine SDK Manual (sample) Version 3.0.0

Tutorial How to upgrade firmware on Phison S8 controller MyDigitalSSD using a Windows PE environment

AEGEE Podio Guidelines

Website Development Komodo Editor and HTML Intro

Adding Images to Broadcast s - 1

Step 1: Download and install the CudaSign for Salesforce app

Section 1: Ribbon Customization

COMMON CUSTOMIZATIONS

Sweet Home 3D user's guide

Advanced Outlook Tutorials

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Making a Poster Using PowerPoint 2007

Smooks Dev Tools Reference Guide. Version: GA

Word basics. Before you begin. What you'll learn. Requirements. Estimated time to complete:

Converting an Excel Spreadsheet Into an Access Database

With a single download, the ADT Bundle includes everything you need to begin developing apps:

When To Work Scheduling Program

App Inventor Beginner Tutorials

Unit 21 - Creating a Button in Macromedia Flash

Excel Reporting with 1010data

Making a Website with Hoolahoop

Debugging with TotalView

1. After installing you can launch the program by clicking on the Start button and then select Programs, then WS_FTP LE, and then WS_FTP LE.

Creating Your Teacher Website using WEEBLY.COM

SQLITE C/C++ TUTORIAL

Weebly Step-by-Step Instructions

How to Build a Web Site using your Web Host s Web Site Builder

Andreas Burghart 6 October 2014 v1.0

First Bytes Programming Lab 2

Dreamweaver: Getting Started Website Structure Why is this relevant?

Comp151. Definitions & Declarations

Tutorial. Introduction to Windows Movie Maker 2.1. A Hands-on Workshop. from the Academic Technology Center. Distributed Learning Services, CIT

Report Card Template Navigating Techniques

GroupWise to MS Outlook 2007

BallBounce: A simple game app

Excel basics. Before you begin. What you'll learn. Requirements. Estimated time to complete:

EDGETECH FTP SITE CUSTOMER & VENDOR ACCESS

Flash MX Image Animation

Creating Interactive PDF Documents with CorelDRAW

Microsoft Office 365 Working with Outlook . Jane Golding September 2015 Version 1

Google Sites: Site Creation and Home Page Design

Microsoft PowerPoint 2010 Computer Jeopardy Tutorial

Basics. a. Click the arrow to the right of the Options button, and then click Bcc.

Contents. Downloading the Data Files Centering Page Elements... 6

DIA Creating Charts and Diagrams

CAPITAL V8. Capital Business Software Tutorial Series. Introduction to Capital Business Manager V8 User Interface 1.2

How to Create a Newsletter Using Common Sense Software - Custom Design

MS Active Sync: Sync with External Memory Files

Sharepoint. Overview. Key features of SharePoint:

Gaggle Account How-To. How do I login to my Gaggle Account? To login, follow these steps: Go to:

Dreamweaver and Fireworks MX Integration Brian Hogan

Click the "Connect" button. You should now see the following screen:

NVCC Alexandria Campus

Introduction to Microsoft Access

CITY OF BURLINGTON PUBLIC SCHOOLS MICROSOFT EXCHANGE 2010 OUTLOOK WEB APP USERS GUIDE

Podium View TM 2.0 Visual Presenter Image Software User Manual - English (WINDOWS)

AVTranz The Court Record Online (TCRO) Operating Instructions Vermont Court System

Adding Links to Resources

Tutorial 2 Online and offline Ship Visualization tool Table of Contents

Chapter 11 Web Development: Importing Gifs and Backgrounds

Image Acquisition Toolbox Adaptor Kit User's Guide

Python Documentation & Startup

Camtasia Recording Settings

DToolsX-DWG. Version: An ActiveX DLL To Retrieve DWG/DXF information. DToolsX-DWG REFERENCE MANUAL

Logging. Working with the POCO logging framework.

Bookstore Application: Client Tier

Adobe Dreamweaver - Basic Web Page Tutorial

USER GUIDE: Trading Central Indicator for the MT4 platform

Kentico CMS 7.0 Personal Site Guide

PISA 2015 MS Online School Questionnaire: User s Manual

Lazy OpenCV installation and use with Visual Studio

Fireworks CS4 Tutorial Part 1: Intro

During the process of creating ColorSwitch, you will learn how to do these tasks:

MICROSOFT OFFICE ACCESS NEW FEATURES

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

Tutorial: setting up a web application

WBU PowerPoint Guidelines

Creating and Using Links and Bookmarks in PDF Documents

Don't have Outlook? Download and configure the Microsoft Office Suite (which includes Outlook)!

Outlook Web App McKinney ISD 5/27/2011

[PRAKTISCHE ASPEKTE DER INFORMATIK SS 2014]

Sophos Anti-virus Basic Level Handout

Joomla Article Advanced Topics: Table Layouts

Transcription:

Drawing Text with SDL Note: This tutorial assumes that you already know how to display a window and draw a sprite with SDL. Setting Up the SDL TrueType Font Library To display text with SDL, you need the SDL_ttf library. You can download the library here (For windows, grab SDL_ttf devel 2.0.7 VC6.zip). Once you've downloaded the file, extract it to a folder of your choice. You now need to tell your IDE where to find the SDL_ttf files. This is the same process as telling SDL where to find the SDL files (remember the lib and include folders?). Since I'm using Visual Studio.NET 2003, I'll go over how to set up SDL_ttf with that IDE. Select Tools >Options. Go to Projects in the left pane and select VC++ Directories. In the Show directories for: menu, select Include files. Click the New Line button (looks like a folder with a star behind it) and then click the... button that appears. Navigate to where you extracted the SDL_ttf files, highlight the include folder and click Open. Again, select Tools >Options. Go to Projects in the left pane and select VC++ Directories. In the Show directories for: menu, select Library files. Click the New Line button (looks like a folder with a star behind it) and then click the... button that appears. Navigate to where you extracted the SDL_ttf files, highlight the lib folder and click Open. Once you've created a project and added a C++ file to it, you need to go to Project >Your Project Properties, click the Linker folder, and select Input. In Additional Dependencies, you need to have "sdl.lib sdlmain.lib sdl_ttf.lib", without the quotes. In the lib folder, under the folder where you extracted the SDL_ttf files, you'll find the file "SDL_ttf.dll". Copy and paste this file into your project directory. When you distribute your program, be sure to include this file in the same directory as the.exe. The TrueType font library requires a font. Fonts come in font files. Click here to get the font file for Arial. Put this file in your project directory.

Displaying Text with SDL_ttf To display text, we first initialize SDL_ttf. We then create a surface that holds the text and blit that surface to our screen buffer. Finally, we shut down SDL_ttf and free the text surface. Initializing SDL_ttf. We initialize SDL_ttf with a call to TTF_Init(), which takes no parameters. Creating a surface with some text on it. To create a surface with the text we want to display, we call TTF_RenderText_Shaded(), which takes four parameters and returns a pointer to an SDL_Surface. The first parameter to TTF_RenderText_Shaded() is a pointer to a TTF_Font structure. We get this structure with a call to TTF_OpenFont(), which takes two paramters and returns a pointer to a TTF_Font structure. The first parameter to TTF_OpenFont() is the name of the file that contains the font information. The second parameter is the size we want our text to be. The second parameter to TTF_RenderText_Shaded() is the text we want displayed. The third parameter is an SDL_Color structure that stores the foreground color of our text. The fourth parameter is an SDL_Color structure that stores the background color of our text. The foreground color of the text you're reading right now is white; the background color is dark blue (at least I think it is...apparently I'm partially color blind, whatever that means). The SDL_Color structure has three variables: a red value, a blue value, and a green value. Blitting the text surface. Blitting the text surface is the same as blitting a sprite surface. The only difference is that you'll usually want to blit the entire text surface. This means that you only need to fill in an SDL_Rect structure for the location of the text on the screen. You can just pass NULL to SDL_BlitSurface() for the source location.

Shutting down SDL_ttf. SDL_ttf is shutdown with a call to TTF_Quit(), which takes no parameters. You also have to free the memory that the TTF_Font structure is using. This is done with a call to TTF_CloseFont(). Since we created a surface to store our text, we shouldn't forget to free it with a call to SDL_FreeSurface(). Here's some code that draws a string of text to the screen. I've bolded the parts that relate to drawing text. #include "SDL.h" #include "SDL_TTF.h" const int WINDOW_WIDTH = 640; const int WINDOW_HEIGHT = 480; const char* WINDOW_TITLE = "SDL Start"; int main(int argc, char **argv) SDL_Init( SDL_INIT_VIDEO ); TTF_Init(); SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE SDL_DOUBLEBUF ); SDL_WM_SetCaption( WINDOW_TITLE, 0 ); TTF_Font* font = TTF_OpenFont("ARIAL.TTF", 12); SDL_Color foregroundcolor = 255, 255, 255 ; SDL_Color backgroundcolor = 0, 0, 255 ; SDL_Surface* textsurface = TTF_RenderText_Shaded(font, "This is my text.", foregroundcolor, backgroundcolor); // Pass zero for width and height to draw the whole surface SDL_Rect textlocation = 100, 100, 0, 0 ; SDL_Event event; bool gamerunning = true; while (gamerunning) if (SDL_PollEvent(&event)) if (event.type == SDL_QUIT) gamerunning = false;

SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); SDL_BlitSurface(textSurface, NULL, screen, &textlocation); SDL_Flip(screen); SDL_FreeSurface(textSurface); TTF_CloseFont(font); TTF_Quit(); SDL_Quit(); return 0; Exercises 1. If you have a lot of text to draw, you'll probably want to put all of the code to do so in a function. Try implementing such a function on your own. Here's an example of a function that draws text. // TTF_Init() must be called before using this function. // Remember to call TTF_Quit() when done. void drawtext(sdl_surface* screen, char* string, int size, int x, int y, int fr, int fg, int fb, int br, int bg, int bb) TTF_Font* font = TTF_OpenFont("ARIAL.TTF", size); SDL_Color foregroundcolor = fr, fg, fb ; SDL_Color backgroundcolor = br, bg, bb ; SDL_Surface* textsurface = TTF_RenderText_Shaded(font, string, foregroundcolor, backgroundcolor); SDL_Rect textlocation = x, y, 0, 0 ; SDL_BlitSurface(textSurface, NULL, screen, &textlocation); SDL_FreeSurface(textSurface); TTF_CloseFont(font);

One thing to notice here is that the font is created and destroyed each time the function is called. I've never found this to be a problem, but if you find yourself having performance issues with your text drawing, this would be one place to look at. Copyright Aaron Cox 2004 2005, All Rights Reserved.