1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2006 Jean Privat <jean@pryen.org>
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
13 # This module contains some classes that help to develop games
18 # This class helps to count events by unit of time.
19 # For example, an FPS counter:
20 # var fps = new TickCounter
29 # Return true every time interval
32 return update_with
(sdl_get_ticks
)
35 # Like `update' but with an explicit time instead of `sdl_get_ticks`
36 meth update_with
(time
: Int): Bool
40 var ti
= _time_interval
44 else if _time
== 0 then
56 # The number of ticks in the last time interval
57 readable attr _count
: Int
59 redef meth to_s
: String
71 readable writable attr _time_interval
: Int
73 # Create a new counter (time interval is 1000)
79 # Create a new counter
80 init with_time
(time_interval
: Int)
82 _time_interval
= time_interval
87 meth left
: Int is abstract
88 meth right
: Int is abstract
89 meth top
: Int is abstract
90 meth bottom
: Int is abstract
91 meth width
: Int is abstract
92 meth height
: Int is abstract
94 meth contain_pixel
(x
: Int, y
: Int): Bool
95 # Is `self' contains the point (`x',`y') ?
103 meth collide
(o
: Rectangle): Bool
113 return ((s_l
<= o_l
and o_l
< s_r
) or
114 (s_l
< o_r
and o_r
<= s_r
) or
115 (o_l
<= s_l
and s_l
< o_r
) or
116 (o_l
< s_r
and s_r
<= o_r
)) and
117 ((s_t
<= o_t
and o_t
< s_b
) or
118 (s_t
< o_b
and o_b
<= s_b
) or
119 (o_t
<= s_t
and s_t
< o_b
) or
120 (o_t
< s_b
and s_b
<= o_b
))
124 # A sprite is a drawable element.
125 # It is represented by a main pixel (x,y) and an image (image)
129 # Absolute X coordinate of the main pixel in the screen
130 readable writable attr _x
: Int
132 # Absolute Y coordinate of the main pixel in the screen
133 readable writable attr _y
: Int
135 # Set two coordinates in one instruction
136 meth set_xy
(x
: Int, y
: Int)
142 # The current image of the object
143 readable attr _image
: SDL_Surface
145 # Relative X cordinate of the main pixel in the image
146 readable attr _x_image
: Int
148 # Relative Y cordinate of the main pixel in the image
149 readable attr _y_image
: Int
151 # Set image and relative coordinates in one instruction
152 meth set_image
(i
: SDL_Surface, x
: Int, y
: Int)
159 # Set image centered on the main pixel (adjust x_image and y_image)
160 meth set_centered_image
(i
: SDL_Surface)
163 _x_image
= i
.width
/ 2
164 _y_image
= i
.height
/ 2
177 redef meth right
: Int
179 return _x
- _x_image
+ _image
.width
182 redef meth bottom
: Int
184 return _y
- _y_image
+ _image
.height
187 redef meth width
: Int
192 redef meth height
: Int
197 # Draw the image on the surface
198 meth blit_on
(s
: SDL_Surface)
200 _image
.blit_on_xy
(s
, _x
- _x_image
, _y
- _y_image
)