rubygame 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CREDITS +50 -0
- data/Changelog +162 -0
- data/LICENSE +504 -0
- data/README +122 -0
- data/Rakefile +380 -0
- data/TODO +45 -0
- data/doc/extended_readme.rdoc +49 -0
- data/doc/getting_started.rdoc +47 -0
- data/doc/macosx_install.rdoc +74 -0
- data/doc/windows_install.rdoc +124 -0
- data/ext/rubygame/MANIFEST +25 -0
- data/ext/rubygame/rubygame_event.c +644 -0
- data/ext/rubygame/rubygame_event.h +48 -0
- data/ext/rubygame/rubygame_gfx.c +951 -0
- data/ext/rubygame/rubygame_gfx.h +102 -0
- data/ext/rubygame/rubygame_gl.c +154 -0
- data/ext/rubygame/rubygame_gl.h +32 -0
- data/ext/rubygame/rubygame_image.c +108 -0
- data/ext/rubygame/rubygame_image.h +41 -0
- data/ext/rubygame/rubygame_joystick.c +247 -0
- data/ext/rubygame/rubygame_joystick.h +41 -0
- data/ext/rubygame/rubygame_main.c +155 -0
- data/ext/rubygame/rubygame_main.h +33 -0
- data/ext/rubygame/rubygame_mixer.c +764 -0
- data/ext/rubygame/rubygame_mixer.h +62 -0
- data/ext/rubygame/rubygame_screen.c +420 -0
- data/ext/rubygame/rubygame_screen.h +41 -0
- data/ext/rubygame/rubygame_shared.c +152 -0
- data/ext/rubygame/rubygame_shared.h +54 -0
- data/ext/rubygame/rubygame_surface.c +1107 -0
- data/ext/rubygame/rubygame_surface.h +62 -0
- data/ext/rubygame/rubygame_time.c +183 -0
- data/ext/rubygame/rubygame_time.h +32 -0
- data/ext/rubygame/rubygame_ttf.c +600 -0
- data/ext/rubygame/rubygame_ttf.h +69 -0
- data/lib/rubygame.rb +40 -0
- data/lib/rubygame/MANIFEST +12 -0
- data/lib/rubygame/clock.rb +128 -0
- data/lib/rubygame/constants.rb +238 -0
- data/lib/rubygame/event.rb +313 -0
- data/lib/rubygame/ftor.rb +370 -0
- data/lib/rubygame/hotspot.rb +265 -0
- data/lib/rubygame/keyconstants.rb +237 -0
- data/lib/rubygame/mediabag.rb +94 -0
- data/lib/rubygame/queue.rb +288 -0
- data/lib/rubygame/rect.rb +614 -0
- data/lib/rubygame/sfont.rb +223 -0
- data/lib/rubygame/sprite.rb +477 -0
- data/samples/FreeSans.ttf +0 -0
- data/samples/GPL.txt +340 -0
- data/samples/README +40 -0
- data/samples/chimp.bmp +0 -0
- data/samples/chimp.rb +313 -0
- data/samples/demo_gl.rb +151 -0
- data/samples/demo_gl_tex.rb +197 -0
- data/samples/demo_music.rb +75 -0
- data/samples/demo_rubygame.rb +279 -0
- data/samples/demo_sfont.rb +52 -0
- data/samples/demo_ttf.rb +193 -0
- data/samples/demo_utf8.rb +53 -0
- data/samples/fist.bmp +0 -0
- data/samples/load_and_blit.rb +22 -0
- data/samples/panda.png +0 -0
- data/samples/punch.wav +0 -0
- data/samples/ruby.png +0 -0
- data/samples/term16.png +0 -0
- data/samples/whiff.wav +0 -0
- metadata +123 -0
@@ -0,0 +1,62 @@
|
|
1
|
+
/*
|
2
|
+
* rubygame -- ruby library to make game programming fun.
|
3
|
+
* Copyright (C) 2004-2007 John Croisant
|
4
|
+
*
|
5
|
+
* This library is free software; you can redistribute it and/or
|
6
|
+
* modify it under the terms of the GNU Lesser General Public
|
7
|
+
* License as published by the Free Software Foundation; either
|
8
|
+
* version 2.1 of the License, or (at your option) any later version.
|
9
|
+
*
|
10
|
+
* This library is distributed in the hope that it will be useful,
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
* Lesser General Public License for more details.
|
14
|
+
*
|
15
|
+
* You should have received a copy of the GNU Lesser General Public
|
16
|
+
* License along with this library; if not, write to the Free Software
|
17
|
+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
|
+
*
|
19
|
+
*/
|
20
|
+
|
21
|
+
|
22
|
+
#ifndef _RUBYGAME_SURFACE_H
|
23
|
+
#define _RUBYGAME_SURFACE_H
|
24
|
+
|
25
|
+
extern void Rubygame_Init_Surface();
|
26
|
+
|
27
|
+
extern VALUE rbgm_surface_new(int, VALUE*, VALUE);
|
28
|
+
|
29
|
+
extern VALUE rbgm_surface_get_w(VALUE);
|
30
|
+
extern VALUE rbgm_surface_get_h(VALUE);
|
31
|
+
extern VALUE rbgm_surface_get_size(VALUE);
|
32
|
+
|
33
|
+
extern VALUE rbgm_surface_get_depth(VALUE);
|
34
|
+
extern VALUE rbgm_surface_get_flags(VALUE);
|
35
|
+
extern VALUE rbgm_surface_get_masks(VALUE);
|
36
|
+
|
37
|
+
extern VALUE rbgm_surface_get_alpha(VALUE);
|
38
|
+
extern VALUE rbgm_surface_set_alpha(int, VALUE*, VALUE);
|
39
|
+
|
40
|
+
extern VALUE rbgm_surface_get_colorkey(VALUE);
|
41
|
+
extern VALUE rbgm_surface_set_colorkey(int, VALUE*, VALUE);
|
42
|
+
|
43
|
+
extern VALUE rbgm_surface_blit(int, VALUE*, VALUE);
|
44
|
+
|
45
|
+
extern VALUE rbgm_surface_fill(int, VALUE*, VALUE);
|
46
|
+
|
47
|
+
extern VALUE rbgm_surface_getat(int, VALUE*, VALUE);
|
48
|
+
|
49
|
+
extern VALUE rbgm_surface_pixels(VALUE);
|
50
|
+
|
51
|
+
extern VALUE rbgm_surface_get_cliprect(VALUE);
|
52
|
+
extern VALUE rbgm_surface_set_cliprect(VALUE, VALUE);
|
53
|
+
|
54
|
+
extern VALUE rbgm_surface_convert(int, VALUE*, VALUE);
|
55
|
+
extern VALUE rbgm_surface_displayformat(VALUE);
|
56
|
+
extern VALUE rbgm_surface_displayformatalpha(VALUE);
|
57
|
+
|
58
|
+
extern VALUE rbgm_image_savebmp(VALUE, VALUE);
|
59
|
+
|
60
|
+
extern VALUE rbgm_transform_flip(VALUE, VALUE, VALUE);
|
61
|
+
|
62
|
+
#endif
|
@@ -0,0 +1,183 @@
|
|
1
|
+
/*
|
2
|
+
* Functions for getting the time since initialization and delaying execution
|
3
|
+
* for a specified amounts of time.
|
4
|
+
*
|
5
|
+
* --
|
6
|
+
*
|
7
|
+
* Rubygame -- Ruby code and bindings to SDL to facilitate game creation
|
8
|
+
* Copyright (C) 2004-2007 John Croisant
|
9
|
+
*
|
10
|
+
* This library is free software; you can redistribute it and/or
|
11
|
+
* modify it under the terms of the GNU Lesser General Public
|
12
|
+
* License as published by the Free Software Foundation; either
|
13
|
+
* version 2.1 of the License, or (at your option) any later version.
|
14
|
+
*
|
15
|
+
* This library is distributed in the hope that it will be useful,
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
18
|
+
* Lesser General Public License for more details.
|
19
|
+
*
|
20
|
+
* You should have received a copy of the GNU Lesser General Public
|
21
|
+
* License along with this library; if not, write to the Free Software
|
22
|
+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
+
*
|
24
|
+
* ++
|
25
|
+
*/
|
26
|
+
|
27
|
+
#include "rubygame_shared.h"
|
28
|
+
#include "rubygame_time.h"
|
29
|
+
|
30
|
+
void Rubygame_Init_Time();
|
31
|
+
|
32
|
+
VALUE cClock;
|
33
|
+
|
34
|
+
VALUE rbgm_time_wait(VALUE, VALUE);
|
35
|
+
VALUE rbgm_time_delay(int, VALUE*, VALUE);
|
36
|
+
VALUE rbgm_time_getticks(VALUE);
|
37
|
+
|
38
|
+
|
39
|
+
/* NOTICE: if you change this value "officially", don't forget to update the
|
40
|
+
* documentation for rbgm_time_delay!!
|
41
|
+
*/
|
42
|
+
#define WORST_CLOCK_ACCURACY 12
|
43
|
+
|
44
|
+
|
45
|
+
/*
|
46
|
+
* call-seq:
|
47
|
+
* wait( time ) -> Integer
|
48
|
+
*
|
49
|
+
* time:: how many milliseconds to wait.
|
50
|
+
*
|
51
|
+
* Wait approximately the given time (the accuracy depends upon processor
|
52
|
+
* scheduling, but 10ms is common). Returns the actual delay time, in
|
53
|
+
* milliseconds. This method is less CPU-intensive than #delay, but is
|
54
|
+
* slightly less accurate.
|
55
|
+
*
|
56
|
+
* The Rubygame timer system will be initialized when you call this function,
|
57
|
+
* if it has not been already.
|
58
|
+
*
|
59
|
+
*/
|
60
|
+
VALUE rbgm_time_wait(VALUE module, VALUE milliseconds)
|
61
|
+
{
|
62
|
+
Uint32 start, delay;
|
63
|
+
|
64
|
+
if(!SDL_WasInit(SDL_INIT_TIMER))
|
65
|
+
if(SDL_InitSubSystem(SDL_INIT_TIMER))
|
66
|
+
rb_raise(eSDLError,"Could not initialize timer system: %s",\
|
67
|
+
SDL_GetError());
|
68
|
+
|
69
|
+
delay = NUM2UINT(milliseconds);
|
70
|
+
start = SDL_GetTicks();
|
71
|
+
SDL_Delay(delay);
|
72
|
+
return INT2NUM(SDL_GetTicks() - start);
|
73
|
+
}
|
74
|
+
|
75
|
+
/*--
|
76
|
+
* From pygame code, with a few modifications:
|
77
|
+
* - takes 'accuracy' argument
|
78
|
+
* - ruby syntax for raising exceptions
|
79
|
+
*++
|
80
|
+
*/
|
81
|
+
static int accurate_delay(int ticks,int accuracy)
|
82
|
+
{
|
83
|
+
int funcstart, delay;
|
84
|
+
if(ticks <= 0)
|
85
|
+
return 0;
|
86
|
+
|
87
|
+
if(!SDL_WasInit(SDL_INIT_TIMER))
|
88
|
+
{
|
89
|
+
if(SDL_InitSubSystem(SDL_INIT_TIMER))
|
90
|
+
{
|
91
|
+
rb_raise(eSDLError,"Could not initialize timer system: %s",\
|
92
|
+
SDL_GetError());
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
funcstart = SDL_GetTicks();
|
97
|
+
if(ticks >= accuracy)
|
98
|
+
{
|
99
|
+
delay = (ticks - 2) - (ticks % accuracy);
|
100
|
+
if(delay >= accuracy)
|
101
|
+
{
|
102
|
+
SDL_Delay(delay);
|
103
|
+
}
|
104
|
+
}
|
105
|
+
do{
|
106
|
+
delay = ticks - (SDL_GetTicks() - funcstart);
|
107
|
+
}while(delay > 0);
|
108
|
+
|
109
|
+
return SDL_GetTicks() - funcstart;
|
110
|
+
}
|
111
|
+
|
112
|
+
/*
|
113
|
+
* call-seq:
|
114
|
+
* delay( time, gran=12 ) -> Integer
|
115
|
+
*
|
116
|
+
* time:: how many milliseconds to delay.
|
117
|
+
* gran:: the granularity (in milliseconds) to assume for the system. A
|
118
|
+
* smaller value should use less CPU time, but if it's lower than the
|
119
|
+
* actual system granularity, this function might wait too long. The
|
120
|
+
* default, 12 ms, has a fairly low risk of over-waiting for many
|
121
|
+
* systems.
|
122
|
+
|
123
|
+
* Use the CPU to more accurately wait for the given period. Returns the
|
124
|
+
* actual delay time, in milliseconds. This function is more accurate than
|
125
|
+
* #wait, but is also somewhat more CPU-intensive.
|
126
|
+
*
|
127
|
+
* The Rubygame timer system will be initialized when you call this function,
|
128
|
+
* if it has not been already.
|
129
|
+
*
|
130
|
+
*/
|
131
|
+
VALUE rbgm_time_delay(int argc, VALUE *argv, VALUE module)
|
132
|
+
{
|
133
|
+
int ticks, goal, accuracy;
|
134
|
+
VALUE vtime, vgran;
|
135
|
+
|
136
|
+
rb_scan_args(argc,argv,"11", &vtime, &vgran);
|
137
|
+
|
138
|
+
goal = NUM2INT(vtime);
|
139
|
+
if(goal < 0)
|
140
|
+
goal = 0;
|
141
|
+
|
142
|
+
if( RTEST(vgran) )
|
143
|
+
accuracy = NUM2INT(vgran);
|
144
|
+
else
|
145
|
+
accuracy = WORST_CLOCK_ACCURACY;
|
146
|
+
|
147
|
+
ticks = accurate_delay(goal,accuracy);
|
148
|
+
|
149
|
+
return INT2NUM(ticks);
|
150
|
+
}
|
151
|
+
|
152
|
+
/*
|
153
|
+
* call-seq:
|
154
|
+
* runtime -> Integer
|
155
|
+
*
|
156
|
+
* Return the number of milliseconds since the Rubygame timer system
|
157
|
+
* was initialized.
|
158
|
+
*
|
159
|
+
* The Rubygame timer system will be initialized when you call this function,
|
160
|
+
* if it has not been already.
|
161
|
+
*/
|
162
|
+
VALUE rbgm_time_getticks( VALUE module )
|
163
|
+
{
|
164
|
+
if(!SDL_WasInit(SDL_INIT_TIMER))
|
165
|
+
if(SDL_InitSubSystem(SDL_INIT_TIMER))
|
166
|
+
rb_raise(eSDLError,"Could not initialize timer system: %s",\
|
167
|
+
SDL_GetError());
|
168
|
+
return INT2NUM(SDL_GetTicks());
|
169
|
+
}
|
170
|
+
|
171
|
+
void Rubygame_Init_Time()
|
172
|
+
{
|
173
|
+
#if 0
|
174
|
+
mRubygame = rb_define_module("Rubygame");
|
175
|
+
#endif
|
176
|
+
|
177
|
+
/* Clock class */
|
178
|
+
cClock = rb_define_class_under(mRubygame,"Clock",rb_cObject);
|
179
|
+
/* Clock class methods */
|
180
|
+
rb_define_singleton_method(cClock,"wait",rbgm_time_wait,1);
|
181
|
+
rb_define_singleton_method(cClock,"delay",rbgm_time_delay,-1);
|
182
|
+
rb_define_singleton_method(cClock,"runtime",rbgm_time_getticks,0);
|
183
|
+
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
/*
|
2
|
+
* Rubygame -- Ruby code and bindings to SDL to facilitate game creation
|
3
|
+
* Copyright (C) 2004-2007 John Croisant
|
4
|
+
*
|
5
|
+
* This library is free software; you can redistribute it and/or
|
6
|
+
* modify it under the terms of the GNU Lesser General Public
|
7
|
+
* License as published by the Free Software Foundation; either
|
8
|
+
* version 2.1 of the License, or (at your option) any later version.
|
9
|
+
*
|
10
|
+
* This library is distributed in the hope that it will be useful,
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
* Lesser General Public License for more details.
|
14
|
+
*
|
15
|
+
* You should have received a copy of the GNU Lesser General Public
|
16
|
+
* License along with this library; if not, write to the Free Software
|
17
|
+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
|
+
*
|
19
|
+
*/
|
20
|
+
|
21
|
+
#ifndef _RUBYGAME_TIME_H
|
22
|
+
#define _RUBYGAME_TIME_H
|
23
|
+
|
24
|
+
extern void Rubygame_Init_Time();
|
25
|
+
|
26
|
+
extern VALUE cClock;
|
27
|
+
|
28
|
+
extern VALUE rbgm_time_wait(VALUE, VALUE);
|
29
|
+
extern VALUE rbgm_time_delay(int, VALUE*, VALUE);
|
30
|
+
extern VALUE rbgm_time_getticks(VALUE);
|
31
|
+
|
32
|
+
#endif
|
@@ -0,0 +1,600 @@
|
|
1
|
+
/*
|
2
|
+
* Interface to SDL_ttf library, for rendering TrueType Fonts to Surfaces.
|
3
|
+
*--
|
4
|
+
* Rubygame -- Ruby code and bindings to SDL to facilitate game creation
|
5
|
+
* Copyright (C) 2004-2007 John Croisant
|
6
|
+
*
|
7
|
+
* This library is free software; you can redistribute it and/or
|
8
|
+
* modify it under the terms of the GNU Lesser General Public
|
9
|
+
* License as published by the Free Software Foundation; either
|
10
|
+
* version 2.1 of the License, or (at your option) any later version.
|
11
|
+
*
|
12
|
+
* This library is distributed in the hope that it will be useful,
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15
|
+
* Lesser General Public License for more details.
|
16
|
+
*
|
17
|
+
* You should have received a copy of the GNU Lesser General Public
|
18
|
+
* License along with this library; if not, write to the Free Software
|
19
|
+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
20
|
+
*++
|
21
|
+
*/
|
22
|
+
|
23
|
+
#include "rubygame_shared.h"
|
24
|
+
#include "rubygame_ttf.h"
|
25
|
+
#include "rubygame_surface.h"
|
26
|
+
|
27
|
+
void Rubygame_Init_TTF();
|
28
|
+
VALUE cTTF;
|
29
|
+
|
30
|
+
VALUE rbgm_ttf_setup(VALUE);
|
31
|
+
VALUE rbgm_ttf_quit(VALUE);
|
32
|
+
VALUE rbgm_ttf_new(VALUE, VALUE, VALUE);
|
33
|
+
|
34
|
+
VALUE rbgm_ttf_getbold(VALUE);
|
35
|
+
VALUE rbgm_ttf_setbold(VALUE, VALUE);
|
36
|
+
|
37
|
+
VALUE rbgm_ttf_getitalic(VALUE);
|
38
|
+
VALUE rbgm_ttf_setitalic(VALUE, VALUE);
|
39
|
+
|
40
|
+
VALUE rbgm_ttf_getunderline(VALUE);
|
41
|
+
VALUE rbgm_ttf_setunderline(VALUE, VALUE);
|
42
|
+
|
43
|
+
VALUE rbgm_ttf_height(VALUE);
|
44
|
+
VALUE rbgm_ttf_ascent(VALUE);
|
45
|
+
VALUE rbgm_ttf_descent(VALUE);
|
46
|
+
VALUE rbgm_ttf_lineskip(VALUE);
|
47
|
+
|
48
|
+
VALUE rbgm_ttf_sizetext(VALUE, VALUE);
|
49
|
+
VALUE rbgm_ttf_size_utf8(VALUE, VALUE);
|
50
|
+
VALUE rbgm_ttf_size_unicode(VALUE, VALUE);
|
51
|
+
|
52
|
+
VALUE rbgm_ttf_render(int, VALUE*, VALUE);
|
53
|
+
VALUE rbgm_ttf_render_utf8(int , VALUE*, VALUE);
|
54
|
+
VALUE rbgm_ttf_render_unicode(int , VALUE*, VALUE);
|
55
|
+
|
56
|
+
/*
|
57
|
+
* call-seq:
|
58
|
+
* setup -> nil
|
59
|
+
*
|
60
|
+
* Attempt to setup the TTF class for use by initializing SDL_ttf.
|
61
|
+
* This *must* be called before the TTF class can be used.
|
62
|
+
* Raises SDLError if there is a problem initializing SDL_ttf.
|
63
|
+
*/
|
64
|
+
VALUE rbgm_ttf_setup(VALUE module)
|
65
|
+
{
|
66
|
+
if(!TTF_WasInit() && TTF_Init()!=0)
|
67
|
+
rb_raise(eSDLError,"could not setup TTF class: %s",TTF_GetError());
|
68
|
+
return Qnil;
|
69
|
+
}
|
70
|
+
|
71
|
+
/*
|
72
|
+
* call-seq:
|
73
|
+
* quit -> nil
|
74
|
+
*
|
75
|
+
* Clean up and quit SDL_ttf, making the TTF class unusable as a result
|
76
|
+
* (until it is setup again). This does not need to be called before Rubygame
|
77
|
+
* exits, as it will be done automatically.
|
78
|
+
*/
|
79
|
+
VALUE rbgm_ttf_quit(VALUE module)
|
80
|
+
{
|
81
|
+
if(TTF_WasInit())
|
82
|
+
TTF_Quit();
|
83
|
+
return Qnil;
|
84
|
+
}
|
85
|
+
|
86
|
+
/*
|
87
|
+
* call-seq:
|
88
|
+
* new( file, size ) -> TTF
|
89
|
+
*
|
90
|
+
* Create a new TTF object, which can render text to a Surface with a
|
91
|
+
* particular font style and size.
|
92
|
+
*
|
93
|
+
* This function takes these arguments:
|
94
|
+
* file:: filename of the TrueType font to use. Should be a +TTF+ or
|
95
|
+
* +FON+ file.
|
96
|
+
* size:: point size (based on 72DPI). (That means the height in pixels from
|
97
|
+
* the bottom of the descent to the top of the ascent.)
|
98
|
+
*/
|
99
|
+
VALUE rbgm_ttf_new(VALUE class, VALUE vfile, VALUE vsize)
|
100
|
+
{
|
101
|
+
VALUE self;
|
102
|
+
TTF_Font *font;
|
103
|
+
|
104
|
+
if(!TTF_WasInit())
|
105
|
+
rb_raise(eSDLError,"Font module must be initialized before making new font.");
|
106
|
+
font = TTF_OpenFont(StringValuePtr(vfile), NUM2INT(vsize));
|
107
|
+
if(font == NULL)
|
108
|
+
rb_raise(eSDLError,"could not load font: %s",TTF_GetError());
|
109
|
+
|
110
|
+
self = Data_Wrap_Struct(cTTF,0,TTF_CloseFont,font);
|
111
|
+
return self;
|
112
|
+
}
|
113
|
+
|
114
|
+
/* :nodoc: */
|
115
|
+
VALUE rbgm_ttf_initialize(int argc, VALUE *argv, VALUE self)
|
116
|
+
{
|
117
|
+
return self;
|
118
|
+
}
|
119
|
+
|
120
|
+
|
121
|
+
/*--
|
122
|
+
* Checks to see if the TTF font wrapped in the self VALUE has the style in the parameter style.
|
123
|
+
* ++
|
124
|
+
*/
|
125
|
+
static VALUE RBGM_ttf_get_style(VALUE self, int style)
|
126
|
+
{
|
127
|
+
TTF_Font *font;
|
128
|
+
int mystyle;
|
129
|
+
|
130
|
+
Data_Get_Struct(self, TTF_Font, font);
|
131
|
+
mystyle = TTF_GetFontStyle(font);
|
132
|
+
|
133
|
+
if ((mystyle & style) == style) return Qtrue;
|
134
|
+
return Qfalse;
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
/*--
|
139
|
+
* Sets or unsets the style in int style for the TTF font wrapped in the self VALUE,
|
140
|
+
* using enable to determine what to do. If enable is Qtrue, the style is set, if it is
|
141
|
+
* Qfalse, the style is unset.
|
142
|
+
* ++
|
143
|
+
*/
|
144
|
+
static VALUE RBGM_ttf_set_style(VALUE self, VALUE enable, int style)
|
145
|
+
{
|
146
|
+
int oldstyle;
|
147
|
+
TTF_Font *font;
|
148
|
+
|
149
|
+
Data_Get_Struct(self,TTF_Font,font);
|
150
|
+
oldstyle = TTF_GetFontStyle(font);
|
151
|
+
|
152
|
+
if(((oldstyle & style) ==style) && !RTEST(enable))
|
153
|
+
{ /* The style is set but we want to remove it. */
|
154
|
+
TTF_SetFontStyle(font,oldstyle^style);
|
155
|
+
return Qtrue;
|
156
|
+
/* The old value */
|
157
|
+
}
|
158
|
+
else if( RTEST(enable) )
|
159
|
+
{ /* The style is not set and we want to add it. */
|
160
|
+
TTF_SetFontStyle(font,oldstyle|style);
|
161
|
+
return Qfalse;
|
162
|
+
/* The old value */
|
163
|
+
}
|
164
|
+
|
165
|
+
return enable;
|
166
|
+
}
|
167
|
+
|
168
|
+
/* call-seq:
|
169
|
+
* bold -> Bool
|
170
|
+
*
|
171
|
+
* True if bolding is enabled for the font.
|
172
|
+
*/
|
173
|
+
VALUE rbgm_ttf_getbold(VALUE self)
|
174
|
+
{
|
175
|
+
return RBGM_ttf_get_style(self, TTF_STYLE_BOLD);
|
176
|
+
}
|
177
|
+
|
178
|
+
/* call-seq:
|
179
|
+
* bold = value -> Bool
|
180
|
+
*
|
181
|
+
* Set whether bolding is enabled for this font. Returns the old value.
|
182
|
+
*/
|
183
|
+
VALUE rbgm_ttf_setbold(VALUE self, VALUE bold)
|
184
|
+
{
|
185
|
+
return RBGM_ttf_set_style(self, bold, TTF_STYLE_BOLD);
|
186
|
+
}
|
187
|
+
|
188
|
+
/* call-seq:
|
189
|
+
* italic -> Bool
|
190
|
+
*
|
191
|
+
* True if italicizing is enabled for the font.
|
192
|
+
*/
|
193
|
+
VALUE rbgm_ttf_getitalic(VALUE self)
|
194
|
+
{
|
195
|
+
return RBGM_ttf_get_style(self, TTF_STYLE_ITALIC);
|
196
|
+
}
|
197
|
+
|
198
|
+
/* call-seq:
|
199
|
+
* italic = value -> Bool
|
200
|
+
*
|
201
|
+
* Set whether italicizing is enabled for this font. Returns the old value.
|
202
|
+
*/
|
203
|
+
VALUE rbgm_ttf_setitalic(VALUE self,VALUE italic)
|
204
|
+
{
|
205
|
+
return RBGM_ttf_set_style(self, italic, TTF_STYLE_ITALIC);
|
206
|
+
}
|
207
|
+
|
208
|
+
/* call-seq:
|
209
|
+
* underline -> Bool
|
210
|
+
*
|
211
|
+
* True if underlining is enabled for the font.
|
212
|
+
*/
|
213
|
+
VALUE rbgm_ttf_getunderline(VALUE self)
|
214
|
+
{
|
215
|
+
return RBGM_ttf_get_style(self, TTF_STYLE_UNDERLINE);
|
216
|
+
}
|
217
|
+
|
218
|
+
/* call-seq:
|
219
|
+
* underline = value -> Bool
|
220
|
+
*
|
221
|
+
* Set whether underlining is enabled for this font. Returns the old value.
|
222
|
+
*/
|
223
|
+
VALUE rbgm_ttf_setunderline(VALUE self, VALUE underline)
|
224
|
+
{
|
225
|
+
return RBGM_ttf_set_style(self, underline, TTF_STYLE_UNDERLINE);
|
226
|
+
}
|
227
|
+
|
228
|
+
/* call-seq:
|
229
|
+
* height -> Integer
|
230
|
+
*
|
231
|
+
* Return the biggest height (bottom to top; in pixels) of all glyphs in the
|
232
|
+
* font.
|
233
|
+
*/
|
234
|
+
VALUE rbgm_ttf_height(VALUE self)
|
235
|
+
{
|
236
|
+
TTF_Font *font;
|
237
|
+
Data_Get_Struct(self,TTF_Font,font);
|
238
|
+
return INT2NUM(TTF_FontHeight(font));
|
239
|
+
}
|
240
|
+
|
241
|
+
/* call-seq:
|
242
|
+
* ascent -> Integer
|
243
|
+
*
|
244
|
+
* Return the biggest ascent (baseline to top; in pixels) of all glyphs in
|
245
|
+
* the font.
|
246
|
+
*/
|
247
|
+
VALUE rbgm_ttf_ascent(VALUE self)
|
248
|
+
{
|
249
|
+
TTF_Font *font;
|
250
|
+
Data_Get_Struct(self,TTF_Font,font);
|
251
|
+
return INT2NUM(TTF_FontAscent(font));
|
252
|
+
}
|
253
|
+
|
254
|
+
/* call-seq:
|
255
|
+
* descent -> Integer
|
256
|
+
*
|
257
|
+
* Return the biggest descent (baseline to bottom; in pixels) of all glyphs in
|
258
|
+
* the font.
|
259
|
+
*/
|
260
|
+
VALUE rbgm_ttf_descent(VALUE self)
|
261
|
+
{
|
262
|
+
TTF_Font *font;
|
263
|
+
Data_Get_Struct(self,TTF_Font,font);
|
264
|
+
return INT2NUM(TTF_FontDescent(font));
|
265
|
+
}
|
266
|
+
|
267
|
+
/* call-seq:
|
268
|
+
* lineskip -> Integer
|
269
|
+
*
|
270
|
+
* Return the recommended distance (in pixels) from a point on a line of text
|
271
|
+
* to the same point on the line of text below it.
|
272
|
+
*/
|
273
|
+
VALUE rbgm_ttf_lineskip(VALUE self)
|
274
|
+
{
|
275
|
+
TTF_Font *font;
|
276
|
+
Data_Get_Struct(self,TTF_Font,font);
|
277
|
+
return INT2NUM(TTF_FontLineSkip(font));
|
278
|
+
}
|
279
|
+
|
280
|
+
/*
|
281
|
+
* call-seq:
|
282
|
+
* size_text(text) -> [ width, height ]
|
283
|
+
*
|
284
|
+
* The width and height the text would be if
|
285
|
+
* it were rendered, without the overhead of
|
286
|
+
* actually rendering it.
|
287
|
+
*/
|
288
|
+
VALUE rbgm_ttf_sizetext(VALUE self, VALUE string)
|
289
|
+
{
|
290
|
+
TTF_Font *font;
|
291
|
+
int w;
|
292
|
+
int h;
|
293
|
+
VALUE result;
|
294
|
+
Data_Get_Struct(self, TTF_Font,font);
|
295
|
+
result = rb_ary_new();
|
296
|
+
|
297
|
+
TTF_SizeText(font,StringValuePtr(string),&w,&h);
|
298
|
+
|
299
|
+
rb_ary_push(result, INT2NUM(w));
|
300
|
+
rb_ary_push(result, INT2NUM(h));
|
301
|
+
|
302
|
+
return result;
|
303
|
+
}
|
304
|
+
|
305
|
+
/*
|
306
|
+
* call-seq:
|
307
|
+
* size_utf8(text) -> [ width, height ]
|
308
|
+
*
|
309
|
+
* The width and height the UTF-8 encoded text would be if
|
310
|
+
* it were rendered, without the overhead of
|
311
|
+
* actually rendering it.
|
312
|
+
*/
|
313
|
+
|
314
|
+
VALUE rbgm_ttf_size_utf8(VALUE self, VALUE string)
|
315
|
+
{
|
316
|
+
TTF_Font *font;
|
317
|
+
int w;
|
318
|
+
int h;
|
319
|
+
VALUE result;
|
320
|
+
Data_Get_Struct(self, TTF_Font,font);
|
321
|
+
result = rb_ary_new();
|
322
|
+
|
323
|
+
TTF_SizeUTF8(font,StringValuePtr(string),&w,&h);
|
324
|
+
|
325
|
+
rb_ary_push(result, INT2NUM(w));
|
326
|
+
rb_ary_push(result, INT2NUM(h));
|
327
|
+
|
328
|
+
return result;
|
329
|
+
}
|
330
|
+
|
331
|
+
/*
|
332
|
+
* call-seq:
|
333
|
+
* size_unicode(text) -> [ width, height ]
|
334
|
+
*
|
335
|
+
* The width and height the UNICODE encoded text would be if
|
336
|
+
* it were rendered, without the overhead of
|
337
|
+
* actually rendering it.
|
338
|
+
*/
|
339
|
+
|
340
|
+
VALUE rbgm_ttf_size_unicode(VALUE self, VALUE string)
|
341
|
+
{
|
342
|
+
TTF_Font *font;
|
343
|
+
int w;
|
344
|
+
int h;
|
345
|
+
VALUE result;
|
346
|
+
Data_Get_Struct(self, TTF_Font,font);
|
347
|
+
result = rb_ary_new();
|
348
|
+
TTF_SizeUNICODE(font,(Uint16*)StringValuePtr(string),&w,&h);
|
349
|
+
|
350
|
+
rb_ary_push(result, INT2NUM(w));
|
351
|
+
rb_ary_push(result, INT2NUM(h));
|
352
|
+
|
353
|
+
return result;
|
354
|
+
}
|
355
|
+
|
356
|
+
/*
|
357
|
+
* Helper function for color handling for the rendering functions.
|
358
|
+
*/
|
359
|
+
|
360
|
+
static void RBGM_array_to_color(SDL_Color * color, VALUE arr) {
|
361
|
+
if( RTEST(arr) )
|
362
|
+
{
|
363
|
+
arr = convert_to_array(arr);
|
364
|
+
color->r = NUM2UINT(rb_ary_entry(arr, 0));
|
365
|
+
color->g = NUM2UINT(rb_ary_entry(arr, 1));
|
366
|
+
color->b = NUM2UINT(rb_ary_entry(arr, 2));
|
367
|
+
}
|
368
|
+
}
|
369
|
+
|
370
|
+
/*
|
371
|
+
*--
|
372
|
+
* TODO: Refactor/integrate #render, #render_utf8, and #render_unicode
|
373
|
+
*++
|
374
|
+
*/
|
375
|
+
|
376
|
+
/* call-seq:
|
377
|
+
* render(string, aa, fg, bg) -> Surface
|
378
|
+
*
|
379
|
+
* Renders a string to a Surface with the font's style and the given color(s).
|
380
|
+
*
|
381
|
+
* This method takes these arguments:
|
382
|
+
* string:: the text string to render
|
383
|
+
* aa:: Use anti-aliasing if true. Enabling this makes the text
|
384
|
+
* look much nicer (smooth curves), but is much slower.
|
385
|
+
* fg:: the color to render the text, in the form [r,g,b]
|
386
|
+
* bg:: the color to use as a background for the text. This option can
|
387
|
+
* be omitted to have a transparent background.
|
388
|
+
*/
|
389
|
+
VALUE rbgm_ttf_render(int argc, VALUE *argv, VALUE self)
|
390
|
+
{
|
391
|
+
SDL_Surface *surf;
|
392
|
+
TTF_Font *font;
|
393
|
+
SDL_Color fore, back; /* foreground and background colors */
|
394
|
+
VALUE vstring, vaa, vfg, vbg;
|
395
|
+
|
396
|
+
rb_scan_args(argc, argv, "31", &vstring, &vaa, &vfg, &vbg);
|
397
|
+
|
398
|
+
Data_Get_Struct(self,TTF_Font,font);
|
399
|
+
|
400
|
+
RBGM_array_to_color(&fore, vfg);
|
401
|
+
RBGM_array_to_color(&back, vbg);
|
402
|
+
|
403
|
+
if( RTEST(vaa) ) /* anti-aliasing enabled */
|
404
|
+
{
|
405
|
+
if( RTEST(vbg) ) /* background color provided */
|
406
|
+
surf = TTF_RenderText_Shaded(font,StringValuePtr(vstring),fore,back);
|
407
|
+
else /* no background color */
|
408
|
+
surf = TTF_RenderText_Blended(font,StringValuePtr(vstring),fore);
|
409
|
+
}
|
410
|
+
else /* anti-aliasing not enabled */
|
411
|
+
{
|
412
|
+
if( RTEST(vbg) ) /* background color provided */
|
413
|
+
{
|
414
|
+
/* remove colorkey, set color index 0 to background color */
|
415
|
+
surf = TTF_RenderText_Solid(font,StringValuePtr(vstring),fore);
|
416
|
+
SDL_SetColors(surf,&back,0,1);
|
417
|
+
SDL_SetColorKey(surf,0,0);
|
418
|
+
}
|
419
|
+
else /* no background color */
|
420
|
+
{
|
421
|
+
surf = TTF_RenderText_Solid(font,StringValuePtr(vstring),fore);
|
422
|
+
}
|
423
|
+
}
|
424
|
+
|
425
|
+
if(surf==NULL)
|
426
|
+
rb_raise(eSDLError,"could not render font object: %s",TTF_GetError());
|
427
|
+
return Data_Wrap_Struct(cSurface,0,SDL_FreeSurface,surf);
|
428
|
+
}
|
429
|
+
|
430
|
+
|
431
|
+
/* call-seq:
|
432
|
+
* render_utf8(string, aa, fg, bg) -> Surface
|
433
|
+
*
|
434
|
+
* Renders a string to a Surface with the font's style and the given color(s).
|
435
|
+
*
|
436
|
+
* This method takes these arguments:
|
437
|
+
* string:: the text string to render
|
438
|
+
* aa:: Use anti-aliasing if true. Enabling this makes the text
|
439
|
+
* look much nicer (smooth curves), but is much slower.
|
440
|
+
* fg:: the color to render the text, in the form [r,g,b]
|
441
|
+
* bg:: the color to use as a background for the text. This option can
|
442
|
+
* be omitted to have a transparent background.
|
443
|
+
*/
|
444
|
+
VALUE rbgm_ttf_render_utf8(int argc, VALUE *argv, VALUE self)
|
445
|
+
{
|
446
|
+
SDL_Surface *surf;
|
447
|
+
TTF_Font *font;
|
448
|
+
SDL_Color fore, back; /* foreground and background colors */
|
449
|
+
VALUE vstring, vaa, vfg, vbg;
|
450
|
+
|
451
|
+
rb_scan_args(argc, argv, "31", &vstring, &vaa, &vfg, &vbg);
|
452
|
+
|
453
|
+
Data_Get_Struct(self,TTF_Font,font);
|
454
|
+
|
455
|
+
RBGM_array_to_color(&fore, vfg);
|
456
|
+
RBGM_array_to_color(&back, vbg);
|
457
|
+
|
458
|
+
if( RTEST(vaa) ) /* anti-aliasing enabled */
|
459
|
+
{
|
460
|
+
if( RTEST(vbg) ) /* background color provided */
|
461
|
+
surf = TTF_RenderUTF8_Shaded(font,StringValuePtr(vstring),fore,back);
|
462
|
+
else /* no background color */
|
463
|
+
surf = TTF_RenderUTF8_Blended(font,StringValuePtr(vstring),fore);
|
464
|
+
}
|
465
|
+
else /* anti-aliasing not enabled */
|
466
|
+
{
|
467
|
+
if( RTEST(vbg) ) /* background color provided */
|
468
|
+
{
|
469
|
+
/* remove colorkey, set color index 0 to background color */
|
470
|
+
surf = TTF_RenderUTF8_Solid(font,StringValuePtr(vstring),fore);
|
471
|
+
SDL_SetColors(surf,&back,0,1);
|
472
|
+
SDL_SetColorKey(surf,0,0);
|
473
|
+
}
|
474
|
+
else /* no background color */
|
475
|
+
{
|
476
|
+
surf = TTF_RenderUTF8_Solid(font,StringValuePtr(vstring),fore);
|
477
|
+
}
|
478
|
+
}
|
479
|
+
|
480
|
+
if(surf==NULL)
|
481
|
+
rb_raise(eSDLError,"could not render font object: %s",TTF_GetError());
|
482
|
+
return Data_Wrap_Struct(cSurface,0,SDL_FreeSurface,surf);
|
483
|
+
}
|
484
|
+
|
485
|
+
/* call-seq:
|
486
|
+
* render_unicode(string, aa, fg, bg) -> Surface
|
487
|
+
*
|
488
|
+
* Renders a string to a Surface with the font's style and the given color(s).
|
489
|
+
*
|
490
|
+
* This method takes these arguments:
|
491
|
+
* string:: the text string to render
|
492
|
+
* aa:: Use anti-aliasing if true. Enabling this makes the text
|
493
|
+
* look much nicer (smooth curves), but is much slower.
|
494
|
+
* fg:: the color to render the text, in the form [r,g,b]
|
495
|
+
* bg:: the color to use as a background for the text. This option can
|
496
|
+
* be omitted to have a transparent background.
|
497
|
+
*/
|
498
|
+
VALUE rbgm_ttf_render_unicode(int argc, VALUE *argv, VALUE self)
|
499
|
+
{
|
500
|
+
/* TODO:... ->unicode */
|
501
|
+
SDL_Surface *surf;
|
502
|
+
TTF_Font *font;
|
503
|
+
SDL_Color fore, back; /* foreground and background colors */
|
504
|
+
VALUE vstring, vaa, vfg, vbg;
|
505
|
+
|
506
|
+
rb_scan_args(argc, argv, "31", &vstring, &vaa, &vfg, &vbg);
|
507
|
+
|
508
|
+
Data_Get_Struct(self,TTF_Font,font);
|
509
|
+
|
510
|
+
RBGM_array_to_color(&fore, vfg);
|
511
|
+
RBGM_array_to_color(&back, vbg);
|
512
|
+
|
513
|
+
if( RTEST(vaa) ) /* anti-aliasing enabled */
|
514
|
+
{
|
515
|
+
if( RTEST(vbg) ) /* background color provided */
|
516
|
+
surf = TTF_RenderUNICODE_Shaded(font,(Uint16*)StringValuePtr(vstring),fore,back);
|
517
|
+
else /* no background color */
|
518
|
+
surf = TTF_RenderUNICODE_Blended(font,(Uint16*)StringValuePtr(vstring),fore);
|
519
|
+
}
|
520
|
+
else /* anti-aliasing not enabled */
|
521
|
+
{
|
522
|
+
if( RTEST(vbg) ) /* background color provided */
|
523
|
+
{
|
524
|
+
/* remove colorkey, set color index 0 to background color */
|
525
|
+
surf = TTF_RenderUNICODE_Solid(font,(Uint16*)StringValuePtr(vstring),fore);
|
526
|
+
SDL_SetColors(surf,&back,0,1);
|
527
|
+
SDL_SetColorKey(surf,0,0);
|
528
|
+
}
|
529
|
+
else /* no background color */
|
530
|
+
{
|
531
|
+
surf = TTF_RenderUNICODE_Solid(font,(Uint16*)StringValuePtr(vstring),fore);
|
532
|
+
}
|
533
|
+
}
|
534
|
+
|
535
|
+
if(surf==NULL)
|
536
|
+
rb_raise(eSDLError,"could not render font object: %s",TTF_GetError());
|
537
|
+
return Data_Wrap_Struct(cSurface,0,SDL_FreeSurface,surf);
|
538
|
+
}
|
539
|
+
|
540
|
+
/*
|
541
|
+
* Document-class: Rubygame::TTF
|
542
|
+
*
|
543
|
+
* TTF provides an interface to SDL_ttf, allowing TrueType Font files to be
|
544
|
+
* loaded and used to render text to Surfaces.
|
545
|
+
*
|
546
|
+
* The TTF class *must* be initialized with the #setup method before any
|
547
|
+
* TTF objects can be created or used.
|
548
|
+
*
|
549
|
+
* This class is only usable if Rubygame was compiled with the SDL_ttf
|
550
|
+
* library. You may test if this feature is available with the #usable?
|
551
|
+
* method. If you need more flexibility, you can check the library version
|
552
|
+
* that Rubygame was compiled against with the #version method.
|
553
|
+
*/
|
554
|
+
void Init_rubygame_ttf()
|
555
|
+
{
|
556
|
+
#if 0
|
557
|
+
mRubygame = rb_define_module("Rubygame");
|
558
|
+
#endif
|
559
|
+
|
560
|
+
Init_rubygame_shared();
|
561
|
+
|
562
|
+
rb_hash_aset(rb_ivar_get(mRubygame,rb_intern("VERSIONS")),
|
563
|
+
ID2SYM(rb_intern("sdl_ttf")),
|
564
|
+
rb_ary_new3(3,
|
565
|
+
INT2NUM(SDL_TTF_MAJOR_VERSION),
|
566
|
+
INT2NUM(SDL_TTF_MINOR_VERSION),
|
567
|
+
INT2NUM(SDL_TTF_PATCHLEVEL)));
|
568
|
+
|
569
|
+
|
570
|
+
cTTF = rb_define_class_under(mRubygame,"TTF",rb_cObject);
|
571
|
+
|
572
|
+
rb_define_singleton_method(cTTF,"new",rbgm_ttf_new,2);
|
573
|
+
rb_define_singleton_method(cTTF,"setup",rbgm_ttf_setup,0);
|
574
|
+
rb_define_singleton_method(cTTF,"quit",rbgm_ttf_quit,0);
|
575
|
+
|
576
|
+
rb_define_method(cTTF,"initialize",rbgm_ttf_initialize,-1);
|
577
|
+
rb_define_method(cTTF,"bold",rbgm_ttf_getbold,0);
|
578
|
+
rb_define_method(cTTF,"bold=",rbgm_ttf_setbold,1);
|
579
|
+
rb_define_alias( cTTF,"b","bold");
|
580
|
+
rb_define_alias( cTTF,"b=","bold=");
|
581
|
+
rb_define_method(cTTF,"italic",rbgm_ttf_getitalic,0);
|
582
|
+
rb_define_method(cTTF,"italic=",rbgm_ttf_setitalic,1);
|
583
|
+
rb_define_alias( cTTF,"i","italic");
|
584
|
+
rb_define_alias( cTTF,"i=","italic=");
|
585
|
+
rb_define_method(cTTF,"underline",rbgm_ttf_getunderline,0);
|
586
|
+
rb_define_method(cTTF,"underline=",rbgm_ttf_setunderline,1);
|
587
|
+
rb_define_alias( cTTF,"u","underline");
|
588
|
+
rb_define_alias( cTTF,"u=","underline=");
|
589
|
+
rb_define_method(cTTF,"height",rbgm_ttf_height,0);
|
590
|
+
rb_define_method(cTTF,"ascent",rbgm_ttf_ascent,0);
|
591
|
+
rb_define_method(cTTF,"descent",rbgm_ttf_descent,0);
|
592
|
+
rb_define_method(cTTF,"line_skip",rbgm_ttf_lineskip,0);
|
593
|
+
rb_define_method(cTTF,"size_text",rbgm_ttf_sizetext,1);
|
594
|
+
rb_define_method(cTTF,"size_utf8",rbgm_ttf_size_utf8, 1);
|
595
|
+
rb_define_method(cTTF,"size_unicode",rbgm_ttf_size_unicode, 1);
|
596
|
+
rb_define_method(cTTF,"render",rbgm_ttf_render,-1);
|
597
|
+
rb_define_method(cTTF,"render_utf8",rbgm_ttf_render_utf8,-1);
|
598
|
+
rb_define_method(cTTF,"render_unicode",rbgm_ttf_render_unicode,-1);
|
599
|
+
|
600
|
+
}
|