File: sge_textpp.h

package info (click to toggle)
ruby-sdl 2.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,344 kB
  • ctags: 4,200
  • sloc: cpp: 7,598; ansic: 4,475; ruby: 2,258; sh: 102; makefile: 97
file content (270 lines) | stat: -rw-r--r-- 7,751 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
*	SDL Graphics Extension
*	Text/TrueType classes (header)
*
*	Started 990826 / 010207 (new version)
*
*	License: LGPL v2+ (see the file LICENSE)
*	(c)1999-2003 Anders Lindstrm
*
*	Uses the excellent FreeType 2 library, available at:
*	http://www.freetype.org/
*/

/*********************************************************************
 *  This library is free software; you can redistribute it and/or    *
 *  modify it under the terms of the GNU Library General Public      *
 *  License as published by the Free Software Foundation; either     *
 *  version 2 of the License, or (at your option) any later version. *
 *********************************************************************/

#ifndef sge_textpp_H
#define sge_textpp_H

#include "SDL.h"
#include "sge_internal.h"

#ifndef _SGE_NO_CLASSES

#include <string>
#include <stdio.h>
#include "sge_tt_text.h"
#include "sge_bm_text.h"
#include "sge_shape.h"

//==================================================================================
// Edits text from SDL_Event
//==================================================================================
class DECLSPEC sge_TextEditor
{
protected:
	//The text is stored in a double linked list

	//The elements in the linked list
	struct node{
		Uint16 c;    //Unicode char
		
		node* next;
		node* prev;
	};
	
	//List metadata
	node* start;
	node* end;
	node* cursor;   //The current node (the cursor)
	
	Uint16 cursor_char;     //The charactar for the cursor
	unsigned int chars;     //Size of the string (without the cursor)
	unsigned int mChars;    //Max chars, 0 is default (unlimited)
	
	//Create and fill a new node
	node* new_node(node* p, node* n, Uint16 c);
	
	//Swap two nodes
	bool nswap(node* one, node* two);
	
	//This indicates that the text has changed since last
	//set to false (used for derived classes)
	bool text_changed;
public:
	//Creator
	sge_TextEditor(void);
	//Destructor
	virtual ~sge_TextEditor(void);
	
	//Adds an char before the cursor
	bool insert(Uint16 c);
	//Removes the char left of cursor
	bool remove_left(void);
	//Removes the char right of cursor
	bool remove_right(void);
	
	//Move cursor left
	inline bool move_left(void);
	//Move cursor right
	inline bool move_right(void);
	//Move cursor to the start
	bool move_start(void);
	//Move cursor to the end
	bool move_end(void);
	
	//Returns text as latin1 or unicode with or without the cursor char
	std::string get_string(bool wCursor=true);
	//std::basic_string<Uint16> get_ustring(bool wCursor=true);
	//Returns a unicode c-style string (allocated with new)
	Uint16* get_ucstring(bool wCursor=true);
	
	//Process a SDL_Event
	//Returns true if the text changed
	virtual bool check(SDL_Event* event);
	
	//Change the cursor char
	void change_cursor(Uint16 c){cursor_char=c; cursor->c=c;}
	
	//Change the text
	void clear_text(void);
	void change_text(const std::string s);
	//void change_utext(const std::basic_string<Uint16> s);
	void change_uctext(Uint16 *text);
	void change_textf(const char *text, ...);             //printf c-style... urk
	
	//Set max chars (default: limited only by memory)
	void max_chars(unsigned int c){mChars=c;}
	
	//Returns the number of characters in the current string
	unsigned int get_chars(void){return chars;}
};



//==================================================================================
// A class for rendering text
//==================================================================================
class DECLSPEC sge_text: public sge_TextEditor
{
protected:
#ifndef _SGE_NOTTF
	sge_TTFont *tt_font;  //The truetype font
#else
	Uint8 *tt_font;
#endif

	//TT Font color
	SDL_Color color;
	SDL_Color background;
	
	sge_bmpFont *bm_font; //The bitmap font
	
	Uint8 alpha_level; //Alpha level (default: opaque)
	
	SDL_Surface* text_surface;	
	virtual void set_textSurface(SDL_Surface *new_surf){;}
	
	//Use what render?
	bool use_tt;
	
	//Show cursor when rendering text?
	bool sCursor;
	
public:
	//Constructor
	sge_text(void){tt_font=NULL; bm_font=NULL; text_surface=NULL; use_tt=true; text_surface=NULL; sCursor=false; alpha_level=SDL_ALPHA_OPAQUE;}
	virtual ~sge_text(void){if(text_surface){SDL_FreeSurface(text_surface);}}
	
	//Get a pointer to the text surface or (if copy=true) returns
	//a copy (don't forget to free it later)
	SDL_Surface* get_textSurface(bool copy=false);
	
	//Updates the textsurface if the text has changed (or if force=true)
	//and returns true if the surface was updated
	bool update_textSurface(bool force=false);
	
#ifndef _SGE_NOTTF
	void set_ttFont(sge_TTFont *font, Uint8 r, Uint8 g, Uint8 b, Uint8 br=0, Uint8 bg=0, Uint8 bb=0);
#endif
	
	bool get_color(SDL_Color *fg){if( tt_font ) {fg->r = color.r; fg->g = color.g; fg->b = color.b; return true;} else return false;}
	bool get_bg(SDL_Color *bg)   {if( tt_font ) {bg->r = background.r; bg->g = background.g; bg->b = background.b; return true;} else return false;}

	void set_bmFont(sge_bmpFont *bm_font);
	
	//Should a cursor be drawn?
	void show_cursor(bool mode){if(mode!=sCursor){text_changed=true;} sCursor=mode;}
	
	//Render text to a surface
	SDL_Rect render_text(SDL_Surface *surface, Sint16 x, Sint16 y);
	
	void use_TTrender(void){if(tt_font){use_tt=true;}}
	void use_BMrender(void){if(bm_font){use_tt=false;}}
	
	//set alpha level (default: opaque)
	void set_alpha(Uint8 alpha){alpha_level = alpha;}
};


//==================================================================================
//sge_TextSurface (derived public from sge_text and sge_surface)
//sge_TextSsprite (derived public from sge_text and sge_ssprite)
//sge_TextSprite  (derived public from sge_text and sge_sprite)
//==================================================================================
class DECLSPEC sge_TextSurface: public sge_text, public sge_surface 
{
protected:
	virtual void set_textSurface(SDL_Surface *new_surf);

public:
	sge_TextSurface(SDL_Surface *screen, Sint16 x=0, Sint16 y=0):
		sge_surface(screen,screen,x,y)
		{\
			surface=NULL;
			current_pos.w=0; current_pos.h=0;
		}
		
	sge_TextSurface(SDL_Surface *screen, const std::string text, Sint16 x=0, Sint16 y=0):
		sge_surface(screen,screen,x,y)
		{
			change_text(text);
			current_pos.w=0; current_pos.h=0;
		}
		
	virtual void draw(void);
};


class DECLSPEC sge_TextSsprite: public sge_text, public sge_ssprite 
{
protected:
	virtual void set_textSurface(SDL_Surface *new_surf);

public:
	sge_TextSsprite(SDL_Surface *screen, Sint16 x=0, Sint16 y=0):
		sge_ssprite(screen,screen,x,y)
		{\
			surface=NULL;
			current_pos.w=0; current_pos.h=0;
		}
		
	sge_TextSsprite(SDL_Surface *screen, const std::string text, Sint16 x=0, Sint16 y=0):
		sge_ssprite(screen,screen,x,y)
		{
			change_text(text);
			current_pos.w=0; current_pos.h=0;
		}
		
	virtual void draw(void);
};


class DECLSPEC sge_TextSprite: public sge_text, public sge_sprite 
{
protected:
	virtual void set_textSurface(SDL_Surface *new_surf);

public:
	sge_TextSprite(SDL_Surface *screen, Sint16 x=0, Sint16 y=0):
		sge_sprite(screen,screen,x,y)
		{\
			surface=NULL;
			current_pos.w=0; current_pos.h=0;
		}
		
	sge_TextSprite(SDL_Surface *screen, const std::string text, Sint16 x=0, Sint16 y=0):
		sge_sprite(screen,screen,x,y)
		{
			change_text(text);
			current_pos.w=0; current_pos.h=0;
		}
		
	virtual void draw(void);
};


//==================================================================================
// A helper function for lazy users: blocking text input for sge_TextSurface
// objects.
// Flags is the same as for BM and TTF input (which now uses this function)
//==================================================================================
DECLSPEC int sge_text_input(sge_TextSurface *tc, Uint8 flags);

#endif /* _SGE_NO_CLASSES */
#endif /* sge_textpp_H */