graphics 1.0.0b1 → 1.0.0b4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/History.rdoc +30 -0
- data/Manifest.txt +39 -7
- data/README.rdoc +48 -4
- data/Rakefile +8 -2
- data/examples/boid.rb +9 -18
- data/examples/bounce.rb +15 -23
- data/examples/canvas.rb +75 -0
- data/examples/collision.rb +6 -6
- data/examples/demo.rb +5 -7
- data/examples/editor.rb +12 -9
- data/examples/fluid.rb +2 -3
- data/examples/fluid2.rb +1 -1
- data/examples/{lito2.rb → gol.rb} +0 -0
- data/examples/{zenspider4.rb → gol2.rb} +0 -0
- data/examples/logo.rb +4 -7
- data/examples/maze.rb +136 -0
- data/examples/tank.rb +10 -11
- data/examples/tank2.rb +12 -17
- data/examples/targeting.rb +1 -1
- data/examples/vants.rb +1 -1
- data/examples/walker.rb +3 -12
- data/examples/walker2.rb +197 -0
- data/examples/zombies.rb +31 -35
- data/ext/sdl/extconf.rb +31 -0
- data/ext/sdl/sdl.c +1067 -0
- data/ext/sdl/sge/INSTALL +72 -0
- data/ext/sdl/sge/LICENSE +504 -0
- data/ext/sdl/sge/Makefile +83 -0
- data/ext/sdl/sge/Makefile.conf +63 -0
- data/ext/sdl/sge/README +219 -0
- data/ext/sdl/sge/Todo +7 -0
- data/ext/sdl/sge/WhatsNew +224 -0
- data/ext/sdl/sge/sge.h +31 -0
- data/ext/sdl/sge/sge_blib.cpp +1939 -0
- data/ext/sdl/sge/sge_blib.h +68 -0
- data/ext/sdl/sge/sge_bm_text.cpp +451 -0
- data/ext/sdl/sge/sge_bm_text.h +71 -0
- data/ext/sdl/sge/sge_collision.cpp +388 -0
- data/ext/sdl/sge/sge_collision.h +54 -0
- data/ext/sdl/sge/sge_config.h +6 -0
- data/ext/sdl/sge/sge_internal.h +152 -0
- data/ext/sdl/sge/sge_misc.cpp +92 -0
- data/ext/sdl/sge/sge_misc.h +37 -0
- data/ext/sdl/sge/sge_primitives.cpp +2516 -0
- data/ext/sdl/sge/sge_primitives.h +111 -0
- data/ext/sdl/sge/sge_rotation.cpp +683 -0
- data/ext/sdl/sge/sge_rotation.h +46 -0
- data/ext/sdl/sge/sge_shape.cpp +762 -0
- data/ext/sdl/sge/sge_shape.h +365 -0
- data/ext/sdl/sge/sge_surface.cpp +1090 -0
- data/ext/sdl/sge/sge_surface.h +100 -0
- data/ext/sdl/sge/sge_textpp.cpp +785 -0
- data/ext/sdl/sge/sge_textpp.h +270 -0
- data/ext/sdl/sge/sge_tt_text.cpp +1456 -0
- data/ext/sdl/sge/sge_tt_text.h +114 -0
- data/graphics_setup.sh +26 -0
- data/lib/graphics.rb +1 -1
- data/lib/graphics/body.rb +50 -3
- data/lib/graphics/extensions.rb +13 -7
- data/lib/graphics/simulation.rb +126 -46
- data/test/test_graphics.rb +52 -12
- data/test/test_sdl.rb +1 -0
- metadata +54 -23
- metadata.gz.sig +0 -0
- data/.gemtest +0 -0
- data/examples/lito.rb +0 -108
- data/examples/zenspider1.rb +0 -93
- data/examples/zenspider2.rb +0 -123
- data/examples/zenspider3.rb +0 -104
- data/rubysdl_setup.sh +0 -34
@@ -0,0 +1,114 @@
|
|
1
|
+
/*
|
2
|
+
* SDL Graphics Extension
|
3
|
+
* Text/TrueType functions (header)
|
4
|
+
*
|
5
|
+
* Started 990815
|
6
|
+
*
|
7
|
+
* License: LGPL v2+ (see the file LICENSE)
|
8
|
+
* (c)1999-2003 Anders Lindstr�m
|
9
|
+
*
|
10
|
+
* Uses the excellent FreeType 2 library, available at:
|
11
|
+
* http://www.freetype.org/
|
12
|
+
*/
|
13
|
+
|
14
|
+
/*********************************************************************
|
15
|
+
* This library is free software; you can redistribute it and/or *
|
16
|
+
* modify it under the terms of the GNU Library General Public *
|
17
|
+
* License as published by the Free Software Foundation; either *
|
18
|
+
* version 2 of the License, or (at your option) any later version. *
|
19
|
+
*********************************************************************/
|
20
|
+
|
21
|
+
#ifndef sge_tt_text_H
|
22
|
+
#define sge_tt_text_H
|
23
|
+
|
24
|
+
#include "SDL.h"
|
25
|
+
#include "sge_internal.h"
|
26
|
+
|
27
|
+
/* Text input flags */
|
28
|
+
#define SGE_IBG SGE_FLAG1
|
29
|
+
#define SGE_IDEL SGE_FLAG2
|
30
|
+
#define SGE_INOKR SGE_FLAG3
|
31
|
+
|
32
|
+
#ifndef _SGE_NOTTF
|
33
|
+
|
34
|
+
/* the truetype font structure */
|
35
|
+
typedef struct _sge_TTFont sge_TTFont;
|
36
|
+
|
37
|
+
/* Font style */
|
38
|
+
#define SGE_TTF_NORMAL SGE_FLAG0
|
39
|
+
#define SGE_TTF_BOLD SGE_FLAG1
|
40
|
+
#define SGE_TTF_ITALIC SGE_FLAG2
|
41
|
+
#define SGE_TTF_UNDERLINE SGE_FLAG3
|
42
|
+
|
43
|
+
/* ZERO WIDTH NO-BREAKSPACE (Unicode byte order mark) */
|
44
|
+
#define UNICODE_BOM_NATIVE 0xFEFF
|
45
|
+
#define UNICODE_BOM_SWAPPED 0xFFFE
|
46
|
+
|
47
|
+
#endif /* _SGE_NOTTF */
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
#ifdef _SGE_C
|
52
|
+
extern "C" {
|
53
|
+
#endif
|
54
|
+
#ifndef _SGE_NOTTF
|
55
|
+
DECLSPEC void sge_TTF_AAOff(void);
|
56
|
+
DECLSPEC void sge_TTF_AAOn(void);
|
57
|
+
DECLSPEC void sge_TTF_AA_Alpha(void);
|
58
|
+
DECLSPEC void sge_TTF_ByteSwappedUNICODE(int swapped);
|
59
|
+
|
60
|
+
DECLSPEC int sge_TTF_Init(void);
|
61
|
+
DECLSPEC sge_TTFont *sge_TTF_OpenFont(const char *file, int ptsize);
|
62
|
+
DECLSPEC sge_TTFont *sge_TTF_OpenFontRW( SDL_RWops *src, int freesrc, int ptsize, int xdpi, int ydpi);
|
63
|
+
DECLSPEC sge_TTFont *sge_TTF_OpenFontIndex(const char *file, int ptsize, long index, int xdpi, int ydpi);
|
64
|
+
DECLSPEC sge_TTFont *sge_TTF_OpenFontIndexRW(SDL_RWops *src, int freesrc, int ptsize, long index, int xdpi, int ydpi);
|
65
|
+
DECLSPEC int sge_TTF_SetFontSize(sge_TTFont *font, int ptsize);
|
66
|
+
DECLSPEC int sge_TTF_SetFontSizeDPI(sge_TTFont *font, int ptsize, int xdpi, int ydpi);
|
67
|
+
|
68
|
+
DECLSPEC int sge_TTF_FontHeight(sge_TTFont *font);
|
69
|
+
DECLSPEC int sge_TTF_FontAscent(sge_TTFont *font);
|
70
|
+
DECLSPEC int sge_TTF_FontDescent(sge_TTFont *font);
|
71
|
+
DECLSPEC int sge_TTF_FontLineSkip(sge_TTFont *font);
|
72
|
+
DECLSPEC long sge_TTF_FontFaces(sge_TTFont *font);
|
73
|
+
DECLSPEC int sge_TTF_FontFaceIsFixedWidth(sge_TTFont *font);
|
74
|
+
DECLSPEC char *sge_TTF_FontFaceFamilyName(sge_TTFont *font);
|
75
|
+
DECLSPEC char *sge_TTF_FontFaceStyleName(sge_TTFont *font);
|
76
|
+
|
77
|
+
DECLSPEC void sge_TTF_SetFontStyle(sge_TTFont *font, Uint8 style);
|
78
|
+
DECLSPEC Uint8 sge_TTF_GetFontStyle(sge_TTFont *font);
|
79
|
+
|
80
|
+
DECLSPEC void sge_TTF_CloseFont(sge_TTFont *font);
|
81
|
+
|
82
|
+
DECLSPEC SDL_Rect sge_TTF_TextSizeUNI(sge_TTFont *font, const Uint16 *text);
|
83
|
+
DECLSPEC SDL_Rect sge_TTF_TextSize(sge_TTFont *Font, char *Text);
|
84
|
+
|
85
|
+
DECLSPEC SDL_Rect sge_tt_textout(SDL_Surface *Surface, sge_TTFont *font, const char *string, Sint16 x, Sint16 y, Uint32 fcolor, Uint32 bcolor, int Alpha);
|
86
|
+
DECLSPEC SDL_Rect sge_tt_textout_UTF8(SDL_Surface *Surface, sge_TTFont *font, const char *string, Sint16 x, Sint16 y, Uint32 fcolor, Uint32 bcolor, int Alpha);
|
87
|
+
DECLSPEC SDL_Rect sge_tt_textout_UNI(SDL_Surface *Surface, sge_TTFont *font, const Uint16 *uni, Sint16 x, Sint16 y, Uint32 fcolor, Uint32 bcolor, int Alpha);
|
88
|
+
|
89
|
+
DECLSPEC SDL_Rect sge_tt_textoutf(SDL_Surface *Surface, sge_TTFont *font, Sint16 x, Sint16 y, Uint8 fR, Uint8 fG, Uint8 fB, Uint8 bR, Uint8 bG, Uint8 bB, int Alpha ,char *format,...);
|
90
|
+
|
91
|
+
DECLSPEC int sge_tt_input_UNI(SDL_Surface *screen,sge_TTFont *font,Uint16 *string,Uint8 flags, int pos,int len,Sint16 x,Sint16 y, Uint32 fcol, Uint32 bcol, int Alpha);
|
92
|
+
DECLSPEC int sge_tt_input(SDL_Surface *screen,sge_TTFont *font,char *string,Uint8 flags, int pos,int len,Sint16 x,Sint16 y, Uint32 fcol, Uint32 bcol, int Alpha);
|
93
|
+
|
94
|
+
DECLSPEC SDL_Surface *sge_TTF_Render(sge_TTFont *font,const Uint16 *text, SDL_Color fg, SDL_Color bg, int alpha_level);
|
95
|
+
DECLSPEC SDL_Surface *sge_TTF_RenderUNICODE(sge_TTFont *font,const Uint16 *text, SDL_Color fg, SDL_Color bg);
|
96
|
+
#endif /* _SGE_NOTTF */
|
97
|
+
|
98
|
+
DECLSPEC Uint16 *sge_Latin1_Uni(const char *text);
|
99
|
+
#ifdef _SGE_C
|
100
|
+
}
|
101
|
+
#endif
|
102
|
+
|
103
|
+
|
104
|
+
#ifndef sge_C_ONLY
|
105
|
+
#ifndef _SGE_NOTTF
|
106
|
+
DECLSPEC SDL_Rect sge_tt_textout(SDL_Surface *Surface, sge_TTFont *font, const char *string, Sint16 x, Sint16 y, Uint8 fR, Uint8 fG, Uint8 fB, Uint8 bR, Uint8 bG, Uint8 bB, int Alpha);
|
107
|
+
DECLSPEC SDL_Rect sge_tt_textout_UTF8(SDL_Surface *Surface, sge_TTFont *font, const char *string, Sint16 x, Sint16 y, Uint8 fR, Uint8 fG, Uint8 fB, Uint8 bR, Uint8 bG, Uint8 bB, int Alpha);
|
108
|
+
DECLSPEC SDL_Rect sge_tt_textout_UNI(SDL_Surface *Surface, sge_TTFont *font, const Uint16 *uni, Sint16 x, Sint16 y, Uint8 fR, Uint8 fG, Uint8 fB, Uint8 bR, Uint8 bG, Uint8 bB, int Alpha);
|
109
|
+
DECLSPEC int sge_tt_input_UNI(SDL_Surface *screen,sge_TTFont *font,Uint16 *string,Uint8 flags, int pos,int len,Sint16 x,Sint16 y, Uint8 fR, Uint8 fG, Uint8 fB, Uint8 bR,Uint8 bG,Uint8 bB, int Alpha);
|
110
|
+
DECLSPEC int sge_tt_input(SDL_Surface *screen,sge_TTFont *font,char *string,Uint8 flags, int pos,int len,Sint16 x,Sint16 y, Uint8 fR, Uint8 fG, Uint8 fB, Uint8 bR,Uint8 bG,Uint8 bB, int Alpha);
|
111
|
+
#endif /* _SGE_NOTTF */
|
112
|
+
#endif /* sge_C_ONLY */
|
113
|
+
|
114
|
+
#endif /* sge_tt_text_H */
|
data/graphics_setup.sh
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
set -e
|
3
|
+
|
4
|
+
for pkg in sdl sdl_image sdl_ttf sdl_mixer sdl_gfx smpeg freetype libogg libvorbis libsmpeg libpng libtiff; do
|
5
|
+
brew uninstall $pkg || true
|
6
|
+
done
|
7
|
+
|
8
|
+
for gem in graphics rubysdl rsdl; do
|
9
|
+
gem uninstall -ax $gem || true
|
10
|
+
done
|
11
|
+
|
12
|
+
# brew update
|
13
|
+
|
14
|
+
brew install sdl
|
15
|
+
brew install sdl_mixer --with-smpeg
|
16
|
+
brew install sdl_ttf
|
17
|
+
brew install sdl_image
|
18
|
+
|
19
|
+
if [ -f $0 ]; then
|
20
|
+
rake clean package
|
21
|
+
gem install pkg/graphics*.gem
|
22
|
+
else
|
23
|
+
gem install graphics --pre
|
24
|
+
fi
|
25
|
+
|
26
|
+
rsdl -rgraphics -e 'Class.new(Graphics::Simulation) { def draw n; clear :white; text "hit escape to quit", 100, 100, :black; end; }.new(500, 250, 0, "Working!").run'
|
data/lib/graphics.rb
CHANGED
data/lib/graphics/body.rb
CHANGED
@@ -27,7 +27,35 @@ class Graphics::Body
|
|
27
27
|
:west => 0,
|
28
28
|
}
|
29
29
|
|
30
|
-
|
30
|
+
##
|
31
|
+
# Body's x coordinate.
|
32
|
+
|
33
|
+
attr_accessor :x
|
34
|
+
|
35
|
+
##
|
36
|
+
# Body's y coordinate.
|
37
|
+
|
38
|
+
attr_accessor :y
|
39
|
+
|
40
|
+
##
|
41
|
+
# Body's angle, in degrees.
|
42
|
+
|
43
|
+
attr_accessor :a
|
44
|
+
|
45
|
+
##
|
46
|
+
# Body's goal angle, in degrees.
|
47
|
+
|
48
|
+
attr_accessor :ga
|
49
|
+
|
50
|
+
##
|
51
|
+
# Body's magnitude.
|
52
|
+
|
53
|
+
attr_accessor :m
|
54
|
+
|
55
|
+
##
|
56
|
+
# Body's window.
|
57
|
+
|
58
|
+
attr_accessor :w
|
31
59
|
|
32
60
|
##
|
33
61
|
# Create a new body in windowing system +w+ with a random x/y and
|
@@ -92,6 +120,25 @@ class Graphics::Body
|
|
92
120
|
[dx, dy]
|
93
121
|
end
|
94
122
|
|
123
|
+
##
|
124
|
+
# Return the angle to another body in degrees.
|
125
|
+
|
126
|
+
def angle_to body
|
127
|
+
dx = body.x - self.x
|
128
|
+
dy = body.y - self.y
|
129
|
+
|
130
|
+
(R2D * Math.atan2(dy, dx)).degrees
|
131
|
+
end
|
132
|
+
|
133
|
+
##
|
134
|
+
# Return the distance to another body, squared.
|
135
|
+
|
136
|
+
def distance_to_squared p
|
137
|
+
dx = p.x - x
|
138
|
+
dy = p.y - y
|
139
|
+
dx * dx + dy * dy
|
140
|
+
end
|
141
|
+
|
95
142
|
def m_a # :nodoc:
|
96
143
|
[m, a]
|
97
144
|
end
|
@@ -139,10 +186,10 @@ class Graphics::Body
|
|
139
186
|
|
140
187
|
if y < 0 then
|
141
188
|
self.y = 0
|
142
|
-
return :
|
189
|
+
return :south
|
143
190
|
elsif y > max_h then
|
144
191
|
self.y = max_h
|
145
|
-
return :
|
192
|
+
return :north
|
146
193
|
end
|
147
194
|
|
148
195
|
nil
|
data/lib/graphics/extensions.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
##
|
2
|
+
# Integer extensions for graphics
|
3
|
+
|
1
4
|
class Integer
|
2
5
|
##
|
3
6
|
# Calculate a random chance using easy notation: 1 =~ 50 :: 1 in 50 chance
|
@@ -7,6 +10,9 @@ class Integer
|
|
7
10
|
end
|
8
11
|
end
|
9
12
|
|
13
|
+
##
|
14
|
+
# Numeric extensions for graphics
|
15
|
+
|
10
16
|
class Numeric
|
11
17
|
##
|
12
18
|
# Is M close to N within a certain delta?
|
@@ -30,16 +36,16 @@ class Numeric
|
|
30
36
|
# Consider this method private, even tho it is in use by the demos.
|
31
37
|
|
32
38
|
def relative_angle n, max
|
33
|
-
|
34
|
-
|
39
|
+
delta_cw = (self - n).degrees
|
40
|
+
delta_cc = (n - self).degrees
|
35
41
|
|
36
|
-
return if
|
42
|
+
return if delta_cc < 0.1 || delta_cw < 0.1
|
37
43
|
|
38
|
-
if
|
39
|
-
|
40
|
-
elsif
|
44
|
+
if delta_cc.abs < max then
|
45
|
+
delta_cc
|
46
|
+
elsif delta_cw.close_to? 180 then
|
41
47
|
[-max, max].sample
|
42
|
-
elsif
|
48
|
+
elsif delta_cw < delta_cc then
|
43
49
|
-max
|
44
50
|
else
|
45
51
|
max
|
data/lib/graphics/simulation.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
|
-
require "sdl"
|
3
|
+
require "sdl/sdl"
|
4
4
|
|
5
5
|
module SDL; end # :nodoc: -- stupid rdoc :(
|
6
6
|
|
@@ -38,6 +38,12 @@ class Graphics::Simulation
|
|
38
38
|
# A hash of color values to their rgb values. For text, apparently. *shrug*
|
39
39
|
attr_accessor :rgb
|
40
40
|
|
41
|
+
# Number of update iterations per drawing tick.
|
42
|
+
attr_accessor :iter_per_tick
|
43
|
+
|
44
|
+
# Procs registered to handle key events.
|
45
|
+
attr_accessor :key_handler
|
46
|
+
|
41
47
|
##
|
42
48
|
# Create a new simulation of a certain width and height. Optionally,
|
43
49
|
# you can set the bits per pixel (0 for current screen settings),
|
@@ -47,11 +53,10 @@ class Graphics::Simulation
|
|
47
53
|
|
48
54
|
def initialize w, h, bpp = 0, name = self.class.name, full = false
|
49
55
|
SDL.init SDL::INIT_VIDEO
|
50
|
-
SDL::TTF.init
|
51
56
|
|
52
57
|
full = full ? SDL::FULLSCREEN : 0
|
53
58
|
|
54
|
-
self.font =
|
59
|
+
self.font = find_font("Menlo", 32)
|
55
60
|
|
56
61
|
SDL::WM.set_caption name, name
|
57
62
|
|
@@ -59,8 +64,29 @@ class Graphics::Simulation
|
|
59
64
|
self.w, self.h = screen.w, screen.h
|
60
65
|
|
61
66
|
self.color = {}
|
62
|
-
self.rgb = Hash.new { |hash, k| hash[k] = screen.get_rgb(color[k]) }
|
67
|
+
self.rgb = Hash.new { |hash, k| hash[k] = screen.format.get_rgb(color[k]) }
|
68
|
+
self.paused = false
|
63
69
|
|
70
|
+
self.iter_per_tick = 1
|
71
|
+
|
72
|
+
self.key_handler = []
|
73
|
+
|
74
|
+
initialize_keys
|
75
|
+
initialize_colors
|
76
|
+
end
|
77
|
+
|
78
|
+
##
|
79
|
+
# Register default key events. Handles ESC & Q (quit) and P (pause).
|
80
|
+
|
81
|
+
def initialize_keys
|
82
|
+
add_key_handler(:ESCAPE) { exit }
|
83
|
+
add_key_handler(:Q) { exit }
|
84
|
+
add_key_handler(:P) { self.paused = !paused }
|
85
|
+
add_key_handler(:SLASH) { self.iter_per_tick += 1 }
|
86
|
+
add_key_handler(:MINUS) { self.iter_per_tick -= 1; self.iter_per_tick = 1 if iter_per_tick < 1 }
|
87
|
+
end
|
88
|
+
|
89
|
+
def initialize_colors # :nodoc:
|
64
90
|
register_color :black, 0, 0, 0
|
65
91
|
register_color :white, 255, 255, 255
|
66
92
|
register_color :red, 255, 0, 0
|
@@ -72,13 +98,27 @@ class Graphics::Simulation
|
|
72
98
|
|
73
99
|
(0..99).each do |n|
|
74
100
|
m = (255 * (n / 100.0)).to_i
|
75
|
-
register_color
|
76
|
-
register_color
|
77
|
-
register_color
|
78
|
-
register_color
|
101
|
+
register_color(("gray%02d" % n).to_sym, m, m, m)
|
102
|
+
register_color(("red%02d" % n).to_sym, m, 0, 0)
|
103
|
+
register_color(("green%02d" % n).to_sym, 0, m, 0)
|
104
|
+
register_color(("blue%02d" % n).to_sym, 0, 0, m)
|
79
105
|
end
|
106
|
+
end
|
80
107
|
|
81
|
-
|
108
|
+
sys_font = "/System/Library/Fonts"
|
109
|
+
lib_font = "/Library/Fonts"
|
110
|
+
user_font = File.expand_path "~/Library/Fonts/"
|
111
|
+
FONT_GLOB = "{#{sys_font},#{lib_font},#{user_font}}" # :nodoc:
|
112
|
+
|
113
|
+
##
|
114
|
+
# Find and open a (TTF) font. Should be as system agnostic as possible.
|
115
|
+
|
116
|
+
def find_font name, size = 16
|
117
|
+
font = Dir["#{FONT_GLOB}/#{name}.{ttc,ttf}"].first
|
118
|
+
|
119
|
+
raise ArgumentError, "Can't find font named '#{name}'" unless font
|
120
|
+
|
121
|
+
SDL::TTF.open(font, size)
|
82
122
|
end
|
83
123
|
|
84
124
|
##
|
@@ -97,7 +137,8 @@ class Graphics::Simulation
|
|
97
137
|
n.times.map {
|
98
138
|
o = klass.new self
|
99
139
|
yield o if block_given?
|
100
|
-
o
|
140
|
+
o
|
141
|
+
}
|
101
142
|
end
|
102
143
|
|
103
144
|
##
|
@@ -110,15 +151,22 @@ class Graphics::Simulation
|
|
110
151
|
end
|
111
152
|
|
112
153
|
##
|
113
|
-
#
|
114
|
-
|
115
|
-
|
116
|
-
|
154
|
+
# Register a block to run for a particular key-press.
|
155
|
+
|
156
|
+
def add_key_handler k, remove = nil, &b
|
157
|
+
k = SDL::Key.const_get k
|
158
|
+
key_handler.delete_if { |a, _| k==a } if remove
|
159
|
+
key_handler.unshift [k, b]
|
160
|
+
end
|
161
|
+
|
162
|
+
##
|
163
|
+
# Handle key events by looking through key_handler and running any
|
164
|
+
# blocks that match the key(s) being pressed.
|
117
165
|
|
118
166
|
def handle_keys
|
119
|
-
|
120
|
-
|
121
|
-
|
167
|
+
key_handler.each do |k, blk|
|
168
|
+
blk[self] if SDL::Key.press? k
|
169
|
+
end
|
122
170
|
end
|
123
171
|
|
124
172
|
##
|
@@ -131,18 +179,16 @@ class Graphics::Simulation
|
|
131
179
|
self.start_time = Time.now
|
132
180
|
n = 0
|
133
181
|
event = nil
|
182
|
+
|
134
183
|
loop do
|
135
184
|
handle_event event, n while event = SDL::Event.poll
|
136
185
|
SDL::Key.scan
|
137
186
|
handle_keys
|
138
187
|
|
139
|
-
|
140
|
-
update n unless paused
|
141
|
-
|
142
|
-
draw_and_flip n
|
188
|
+
next if paused
|
143
189
|
|
144
|
-
|
145
|
-
|
190
|
+
iter_per_tick.times { update n; n += 1 }
|
191
|
+
draw_and_flip n
|
146
192
|
end
|
147
193
|
end
|
148
194
|
|
@@ -179,7 +225,7 @@ class Graphics::Simulation
|
|
179
225
|
|
180
226
|
def line x1, y1, x2, y2, c
|
181
227
|
h = self.h
|
182
|
-
screen.draw_line x1, h-y1, x2, h-y2, color[c]
|
228
|
+
screen.draw_line x1, h-y1-1, x2, h-y2-1, color[c]
|
183
229
|
end
|
184
230
|
|
185
231
|
##
|
@@ -203,7 +249,7 @@ class Graphics::Simulation
|
|
203
249
|
def polygon *points, color
|
204
250
|
points << points.first
|
205
251
|
points.each_cons(2) do |p1, p2|
|
206
|
-
|
252
|
+
line(*p1, *p2, color)
|
207
253
|
end
|
208
254
|
end
|
209
255
|
|
@@ -219,14 +265,14 @@ class Graphics::Simulation
|
|
219
265
|
# Draw a rect at x/y with w by h dimensions in color c. Ignores blending.
|
220
266
|
|
221
267
|
def fast_rect x, y, w, h, c
|
222
|
-
screen.
|
268
|
+
screen.fast_rect x, self.h-y-h, w, h, color[c]
|
223
269
|
end
|
224
270
|
|
225
271
|
##
|
226
272
|
# Draw a point at x/y w/ color c.
|
227
273
|
|
228
274
|
def point x, y, c
|
229
|
-
screen[x, h-y] = color[c]
|
275
|
+
screen[x, h-y-1] = color[c]
|
230
276
|
end
|
231
277
|
|
232
278
|
##
|
@@ -242,21 +288,36 @@ class Graphics::Simulation
|
|
242
288
|
# Draw a rect at x/y with w by h dimensions in color c.
|
243
289
|
|
244
290
|
def rect x, y, w, h, c, fill = false
|
245
|
-
|
291
|
+
y = self.h-y-h-1
|
292
|
+
if fill then
|
293
|
+
screen.fill_rect x, y, w, h, color[c]
|
294
|
+
else
|
295
|
+
screen.draw_rect x, y, w, h, color[c]
|
296
|
+
end
|
246
297
|
end
|
247
298
|
|
248
299
|
##
|
249
300
|
# Draw a circle at x/y with radius r in color c.
|
250
301
|
|
251
302
|
def circle x, y, r, c, fill = false
|
252
|
-
|
303
|
+
y = h-y-1
|
304
|
+
if fill then
|
305
|
+
screen.fill_circle x, y, r, color[c]
|
306
|
+
else
|
307
|
+
screen.draw_circle x, y, r, color[c]
|
308
|
+
end
|
253
309
|
end
|
254
310
|
|
255
311
|
##
|
256
312
|
# Draw a circle at x/y with radiuses w/h in color c.
|
257
313
|
|
258
314
|
def ellipse x, y, w, h, c, fill = false
|
259
|
-
|
315
|
+
y = self.h-y-1
|
316
|
+
if fill then
|
317
|
+
screen.fill_ellipse x, y, w, h, color[c]
|
318
|
+
else
|
319
|
+
screen.draw_ellipse x, y, w, h, color[c]
|
320
|
+
end
|
260
321
|
end
|
261
322
|
|
262
323
|
##
|
@@ -265,7 +326,7 @@ class Graphics::Simulation
|
|
265
326
|
|
266
327
|
def bezier x1, y1, cx1, cy1, cx2, cy2, x2, y2, c, l = 7
|
267
328
|
h = self.h
|
268
|
-
screen.draw_bezier x1, h-y1, cx1, h-cy1, cx2, h-cy2, x2, h-y2, l, color[c]
|
329
|
+
screen.draw_bezier x1, h-y1-1, cx1, h-cy1, cx2, h-cy2, x2, h-y2-1, l, color[c]
|
269
330
|
end
|
270
331
|
|
271
332
|
## Text
|
@@ -281,14 +342,15 @@ class Graphics::Simulation
|
|
281
342
|
# Return the rendered text s in color c in font f.
|
282
343
|
|
283
344
|
def render_text s, c, f = font
|
284
|
-
f.
|
345
|
+
f.render screen, s, color[c]
|
285
346
|
end
|
286
347
|
|
287
348
|
##
|
288
349
|
# Draw text s at x/y in color c in font f.
|
289
350
|
|
290
351
|
def text s, x, y, c, f = font
|
291
|
-
|
352
|
+
y = self.h-y-f.height-1
|
353
|
+
f.draw screen, s, x, y, color[c]
|
292
354
|
end
|
293
355
|
|
294
356
|
##
|
@@ -313,15 +375,7 @@ class Graphics::Simulation
|
|
313
375
|
|
314
376
|
### Blitting Methods:
|
315
377
|
|
316
|
-
|
317
|
-
|
318
|
-
# put_pixel(x, y, color)
|
319
|
-
# []=(x, y, color)
|
320
|
-
# get_pixel(x, y)
|
321
|
-
# [](x, y)
|
322
|
-
# put(src, x, y) # see blit
|
323
|
-
# copy_rect(x,y,w,h)
|
324
|
-
# transform_surface(bgcolor,angle,xscale,yscale,flags)
|
378
|
+
# TODO: copy_rect(x,y,w,h)
|
325
379
|
|
326
380
|
##
|
327
381
|
# Load an image at path into a new surface.
|
@@ -330,6 +384,20 @@ class Graphics::Simulation
|
|
330
384
|
SDL::Surface.load path
|
331
385
|
end
|
332
386
|
|
387
|
+
##
|
388
|
+
# Load an audio file at path
|
389
|
+
|
390
|
+
def audio path
|
391
|
+
SDL::Audio.load path
|
392
|
+
end
|
393
|
+
|
394
|
+
##
|
395
|
+
# Open the audio mixer with a number of +channels+ open.
|
396
|
+
|
397
|
+
def open_mixer channels = 1
|
398
|
+
SDL::Audio.open channels
|
399
|
+
end
|
400
|
+
|
333
401
|
##
|
334
402
|
# Return the current mouse state: x, y, buttons.
|
335
403
|
|
@@ -340,10 +408,22 @@ class Graphics::Simulation
|
|
340
408
|
end
|
341
409
|
|
342
410
|
##
|
343
|
-
# Draw a bitmap at x/y with
|
411
|
+
# Draw a bitmap centered at x/y with optional angle, x/y scale, and flags.
|
412
|
+
|
413
|
+
def blit src, x, y, a° = 0, xscale = 1, yscale = 1, flags = 0
|
414
|
+
img = src.transform src.format.colorkey, -a°, xscale, yscale, flags
|
415
|
+
|
416
|
+
SDL::Surface.blit img, 0, 0, 0, 0, screen, x-img.w/2, h-y-img.h/2
|
417
|
+
end
|
418
|
+
|
419
|
+
##
|
420
|
+
# Draw a bitmap at x/y with optional angle, x/y scale, and flags.
|
421
|
+
|
422
|
+
def put src, x, y, a° = 0, xscale = 1, yscale = 1, flags = 0
|
423
|
+
img = src.transform src.format.colorkey, -a°, xscale, yscale, flags
|
344
424
|
|
345
|
-
|
346
|
-
SDL::Surface.
|
425
|
+
# why x-1? because transform adds a pixel to all sides even if a°==0
|
426
|
+
SDL::Surface.blit img, 0, 0, 0, 0, screen, x-1, h-y-img.h
|
347
427
|
end
|
348
428
|
|
349
429
|
##
|
@@ -352,7 +432,7 @@ class Graphics::Simulation
|
|
352
432
|
# primitives will work and the resulting surface is returned.
|
353
433
|
|
354
434
|
def sprite w, h
|
355
|
-
new_screen = SDL::Surface.new
|
435
|
+
new_screen = SDL::Surface.new w, h, screen.format
|
356
436
|
old_screen = screen
|
357
437
|
old_w, old_h = self.w, self.h
|
358
438
|
self.w, self.h = w, h
|