~ubuntu-branches/ubuntu/wily/python-sfml/wily

« back to all changes in this revision

Viewing changes to doc/source/api/system.rst

  • Committer: Package Import Robot
  • Author(s): James Cowgill
  • Date: 2013-12-09 17:50:52 UTC
  • mfrom: (1.1.3) (8.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20131209175052-11v6drpb6g3yksst
Tags: 1.5.1.is.1.3+dfsg-1
* New upstream version 1.3 (from python-sfml.org)
  - This is a complete rewrite of the Python bindings for SFML2, and
    the new maintainer is using a different version numbering scheme.
* Added myself to the list of uploaders
* Change package priority from extra to optional
* Bumped standards version (to 3.9.5) and debhelper compat (to 9)
* Added Python 3 and documentation packages
* Improve package description for debug packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
System
 
2
======
 
3
.. contents:: :local:
 
4
.. py:module:: sfml.system
 
5
 
 
6
 
 
7
Vector2
 
8
^^^^^^^
 
9
 
 
10
.. class:: Vector2
 
11
 
 
12
   Utility class for manipulating 2-dimensional vectors.
 
13
 
 
14
   :class:`Vector2` is a simple class that defines a mathematical vector with
 
15
   two coordinates (:attr:`x` and :attr:`y`).
 
16
 
 
17
   It can be used to represent anything that has two dimensions: a size, a
 
18
   point, a velocity, etc.
 
19
 
 
20
   :class:`Vector2` supports arithmetic operations (+, -, /, \*), unary
 
21
   operations and comparisons (==, !=).
 
22
 
 
23
   It contains no mathematical function like dot product, cross product,
 
24
   length, etc.
 
25
 
 
26
   Usage example::
 
27
 
 
28
      v1 = sf.Vector2(16.5, 24)
 
29
      v1.x = 18.2
 
30
      y = v1.y
 
31
 
 
32
      v2 = v1 * 5
 
33
 
 
34
      v3 = v1 + v2
 
35
 
 
36
      different = v2 != v3
 
37
 
 
38
   Note: for 3-dimensional vectors, see :class:`.Vector3`
 
39
 
 
40
   .. method:: Vector2(x=0, y=0)
 
41
 
 
42
      Construct an :class:`sfml.system.Vector2`
 
43
 
 
44
   .. attribute:: x
 
45
 
 
46
      X coordinate of the vector.
 
47
 
 
48
   .. attribute:: y
 
49
 
 
50
      Y coordinate of the vector.
 
51
 
 
52
Vector3
 
53
^^^^^^^
 
54
 
 
55
.. class:: Vector3
 
56
 
 
57
   Utility class for manipulating 3-dimensional vectors.
 
58
 
 
59
   :class:`Vector3` is a simple class that defines a mathematical vector with
 
60
   three coordinates (:attr:`x`, :attr:`y` and :attr:`z`).
 
61
 
 
62
   It can be used to represent anything that has three dimensions: a size, a
 
63
   point, a velocity, etc.
 
64
 
 
65
   :class:`Vector3` supports arithmetic operations (+, -, /, \*), unary
 
66
   operations and comparisons (==, !=).
 
67
 
 
68
   It contains no mathematical function like dot product, cross product,
 
69
   length, etc.
 
70
 
 
71
   Usage example::
 
72
 
 
73
      v1 = sf.Vector3(16.5, 24, -8.2)
 
74
      v1.x = 18.2
 
75
      y = v1.y
 
76
      z = v1.z
 
77
 
 
78
      v2 = v1 * 5
 
79
 
 
80
      v3 = v1 + v2
 
81
 
 
82
      different = v2 != v3
 
83
 
 
84
   Note: for 2-dimensional vectors, see :class:`.Vector2`
 
85
 
 
86
   .. method:: Vector3(x=0, y=0, z=0)
 
87
 
 
88
      Construct an :class:`sfml.system.Vector3`
 
89
 
 
90
   .. attribute:: x
 
91
 
 
92
      X coordinate of the vector.
 
93
 
 
94
   .. attribute:: y
 
95
 
 
96
      Y coordinate of the vector.
 
97
 
 
98
   .. attribute:: z
 
99
 
 
100
      Z coordinate of the vector.
 
101
 
 
102
seconds
 
103
^^^^^^^
 
104
 
 
105
.. py:function:: seconds(amount)
 
106
 
 
107
   Construct a time value from a number of seconds.
 
108
 
 
109
   :param float amount: Number of seconds
 
110
   :return: Time value constructed from the amount of seconds
 
111
   :rtype: :class:`sfml.system.Time`
 
112
 
 
113
 
 
114
milliseconds
 
115
^^^^^^^^^^^^
 
116
 
 
117
.. py:function:: milliseconds(amount)
 
118
 
 
119
   Construct a time value from a number of milliseconds.
 
120
 
 
121
   :param int amount: Number of milliseconds
 
122
   :return: Time value constructed from the amount of milliseconds
 
123
   :rtype: :class:`sfml.system.Time`
 
124
 
 
125
 
 
126
microseconds
 
127
^^^^^^^^^^^^
 
128
 
 
129
.. py:function:: microseconds(amount)
 
130
 
 
131
   Construct a time value from a number of microseconds.
 
132
 
 
133
   :param int amount: Number of microseconds
 
134
   :return: Time value constructed from the amount of microseconds
 
135
   :rtype: :class:`sfml.system.Time`
 
136
 
 
137
Time
 
138
^^^^
 
139
 
 
140
.. class:: Time
 
141
 
 
142
   Represents a time value.
 
143
 
 
144
   :class:`Time` encapsulates a time value in a flexible way.
 
145
 
 
146
   It allows to define a time value either as a number of seconds, milliseconds
 
147
   or microseconds. It also works the other way round: you can read a time
 
148
   value as either a number of seconds, milliseconds or microseconds.
 
149
 
 
150
   By using such a flexible interface, the API doesn't impose any fixed type or
 
151
   resolution for time values, and let the user choose its own favorite
 
152
   representation.
 
153
 
 
154
   :class:`Time` values support the usual mathematical operations: you can add
 
155
   or subtract two times, multiply or divide a time by a number, compare two
 
156
   times, etc.
 
157
 
 
158
   Since they represent a time span and not an absolute time value, times can
 
159
   also be negative.
 
160
 
 
161
   Usage example::
 
162
 
 
163
      t1 = sf.seconds(0.1)
 
164
      milli = t1.milliseconds
 
165
 
 
166
      t2 = sf.milliseconds(30)
 
167
      micro = t2.microseconds
 
168
 
 
169
      t3 = sf.microseconds(-800000)
 
170
      sec = t3.seconds
 
171
 
 
172
   ::
 
173
 
 
174
      def update(elapsed):
 
175
         position += speed * elapsed.seconds
 
176
 
 
177
      update(sf.milliseconds(100))
 
178
 
 
179
   See also: :class:`.Clock`
 
180
 
 
181
   .. method:: Time()
 
182
 
 
183
      Construct a :class:`Time` equivalent to :const:`ZERO`
 
184
 
 
185
   .. data:: ZERO
 
186
 
 
187
      Predefined "zero" time value. Copy this value with the **copy** module.
 
188
 
 
189
   .. attribute:: seconds
 
190
 
 
191
      Return the time value as a number of seconds.
 
192
 
 
193
   .. attribute:: milliseconds
 
194
 
 
195
      Return the time value as a number of milliseconds.
 
196
 
 
197
   .. attribute:: microseconds
 
198
 
 
199
      Return the time value as a number of microseconds.
 
200
 
 
201
Clock
 
202
^^^^^
 
203
 
 
204
.. class:: Clock
 
205
 
 
206
   Utility class that measures the elapsed time.
 
207
 
 
208
   :class:`Clock` is a lightweight class for measuring time.
 
209
 
 
210
   It provides the most precise time that the underlying OS can achieve
 
211
   (generally microseconds or nanoseconds). It also ensures monotonicity, which
 
212
   means that the returned time can never go backward, even if the system time
 
213
   is changed.
 
214
 
 
215
   Usage example::
 
216
 
 
217
      clock = sf.Clock()
 
218
      # ...
 
219
      time1 = clock.elapsed_time
 
220
      # ...
 
221
      time2 = clock.restart()
 
222
 
 
223
   The :class:`Time` value returned by the clock can then be converted to a
 
224
   number of seconds, milliseconds or even microseconds.
 
225
 
 
226
   See also: :class:`.Time`
 
227
 
 
228
   .. method:: Clock()
 
229
 
 
230
      Construct a :class:`Clock`
 
231
 
 
232
      The clock starts automatically after being constructed.
 
233
 
 
234
   .. attribute:: elapsed_time
 
235
 
 
236
      Get the elapsed time.
 
237
 
 
238
      This attribute returns the time elapsed since the last call to
 
239
      :func:`restart()` (or the construction of the instance if
 
240
      :func:`restart()` has not been called).
 
241
 
 
242
      :return: :class:`.Time` elapsed
 
243
      :rtype: :class:`sfml.system.Time`
 
244
 
 
245
   .. method:: restart()
 
246
 
 
247
      Restart the clock.
 
248
 
 
249
      This function puts the time counter back to zero. It also returns the
 
250
      time elapsed since the clock was started.
 
251
 
 
252
      :return: :class:`.Time` elapsed
 
253
      :rtype: :class:`sfml.system.Time`
 
254
 
 
255
sleep
 
256
^^^^^
 
257
 
 
258
.. function:: sleep(duration)
 
259
 
 
260
   Make the current thread sleep for a given duration.
 
261
 
 
262
   :func:`sleep` is the best way to block a program or one of its threads, as
 
263
   it doesn't consume any CPU power.
 
264
 
 
265
   :param sfml.system.Time duration: Time to sleep
 
266
 
 
267
Thread
 
268
^^^^^^
 
269
 
 
270
.. class:: Thread
 
271
 
 
272
   Utility class to manipulate threads.
 
273
 
 
274
   Threads provide a way to run multiple parts of the code in parallel.
 
275
 
 
276
   When you launch a new thread, the execution is split and both the new thread
 
277
   and the caller run in parallel.
 
278
 
 
279
   To use a :class:`Thread`, you construct it directly with the function to
 
280
   execute as the entry point of the thread.
 
281
 
 
282
   The thread ends when its function is terminated. If the owner sf.Thread
 
283
   instance is destroyed before the thread is finished, the destructor will
 
284
   wait (see :meth:`wait`)
 
285
 
 
286
   Usage example::
 
287
 
 
288
      def functor(a, b, c):
 
289
         # do something in parallel
 
290
 
 
291
      mythread = sf.Thread(functor, 16.8, 24, -8)
 
292
      mythread.launch()
 
293
 
 
294
   .. method:: Thread(functor, *args, **kwargs)
 
295
 
 
296
      Construct the thread from a callable object with optional arguments.
 
297
 
 
298
      .. note::
 
299
 
 
300
         This does **not** run the thread, use :meth:`launch`.
 
301
 
 
302
   .. method:: launch()
 
303
 
 
304
      Run the thread.
 
305
 
 
306
      This function starts the entry point passed to the thread's constructor,
 
307
      and returns immediately. After this function returns, the thread's
 
308
      function is running in parallel to the calling code.
 
309
 
 
310
   .. method:: terminate()
 
311
 
 
312
      Terminate the thread.
 
313
 
 
314
      This function immediately stops the thread, without waiting for its
 
315
      function to finish. Terminating a thread with this function is not safe,
 
316
      and can lead to local variables not being destroyed on some operating
 
317
      systems. You should rather try to make the thread function terminate by
 
318
      itself.
 
319
 
 
320
   .. method:: wait()
 
321
 
 
322
      Wait until the thread finishes.
 
323
 
 
324
      This function will block the execution until the thread's function ends.
 
325
 
 
326
      .. warning::
 
327
 
 
328
         If the thread function never ends, the calling thread will block
 
329
         forever. If this function is called from its owner thread, it returns
 
330
         without doing anything.
 
331
 
 
332
 
 
333
Mutex
 
334
^^^^^
 
335
 
 
336
.. class:: Mutex
 
337
 
 
338
   Blocks concurrent access to shared resources from multiple threads.
 
339
 
 
340
   Mutex stands for "MUTual EXclusion".
 
341
 
 
342
   A mutex is a synchronization object, used when multiple threads are involved.
 
343
 
 
344
   When you want to protect a part of the code from being accessed
 
345
   simultaneously by multiple threads, you typically use a mutex. When a thread
 
346
   is locked by a mutex, any other thread trying to lock it will be blocked
 
347
   until the mutex is released by the thread that locked it. This way, you can
 
348
   allow only one thread at a time to access a critical region of your code.
 
349
 
 
350
   Usage example::
 
351
 
 
352
      database = Database() # this is a critical resoruce that needs some protection
 
353
 
 
354
      mutex = sf.Mutex()
 
355
 
 
356
      def thread1():
 
357
         mutex.lock() # this call will block the thread if the mutex is already locked by thread2
 
358
         database.write(...)
 
359
         mutex.unlock() # if thread2 was waiting, it will now be unblocked
 
360
 
 
361
      def thread2():
 
362
         mutex.lock() # this call will block the thread if the mutex is already locked by thread1
 
363
         database.write(...)
 
364
         mutex.unlock() # if thread1 was waiting, it will now be unblocked
 
365
 
 
366
   Be very careful with mutexes. A bad usage can lead to bad problems, like
 
367
   deadlocks (two threads are waiting for each other and the application is
 
368
   globally stuck).
 
369
 
 
370
   To make the usage of mutexes more robust, particularly in environments where
 
371
   exceptions can be thrown, you should use the helper class `class:`Lock` to
 
372
   lock/unlock mutexes.
 
373
 
 
374
   pySFML mutexes are recursive, which means that you can lock a mutex multiple
 
375
   times in the same thread without creating a deadlock. In this case, the
 
376
   first call to :meth:`lock` behaves as usual, and the following ones have no
 
377
   effect. However, you must call `meth:`unlock` exactly as many times as you
 
378
   called :meth:`lock`. If you don't, the mutex won't be released.
 
379
 
 
380
   .. method:: Mutex()
 
381
 
 
382
      Construct a mutex.
 
383
 
 
384
   .. method:: lock()
 
385
 
 
386
      Lock the mutex.
 
387
 
 
388
      If the mutex is already locked in another thread, this call will block
 
389
      the execution until the mutex is released.
 
390
 
 
391
   .. method:: unlock()
 
392
 
 
393
      Unlock the mutex.
 
394
 
 
395
 
 
396
 
 
397
Lock
 
398
^^^^
 
399
 
 
400
.. class:: Lock
 
401
 
 
402
   Automatic wrapper for locking and unlocking mutexes.
 
403
 
 
404
   :class:`Lock` is a RAII wrapper for :class:`Mutex`.
 
405
 
 
406
   By unlocking it in its destructor, it ensures that the mutex will always be
 
407
   released when the current scope (most likely a function) ends. This is even
 
408
   more important when an exception or an early return statement can interrupt
 
409
   the execution flow of the function.
 
410
 
 
411
   For maximum robustness, :class:`Lock` should always be used to lock/unlock a
 
412
   mutex.
 
413
 
 
414
   Usage example::
 
415
 
 
416
      mutex = sf.Mutex()
 
417
 
 
418
      def function():
 
419
         lock = sf.Lock(mutex) # mutex is now locked
 
420
 
 
421
         function_that_may_throw_an_exception() # mutex is unlocked if this function throws
 
422
 
 
423
         if (some_condition):
 
424
            return # mutex is unlocked
 
425
 
 
426
         # mutex is unlocked
 
427
 
 
428
   Because the mutex is not explicitely unlocked in the code, it may remain
 
429
   locked longer than needed. If the region of the code that needs to be
 
430
   protected by the mutex is not the entire function, just delete the lock via
 
431
   *del*. ::
 
432
 
 
433
      mutex = sf.Mutex()
 
434
 
 
435
      def function():
 
436
         lock = sf.Lock(mutex)
 
437
         code_that_requires_protection()
 
438
         del lock
 
439
         code_that_doesnt_care_about_the_mutex()
 
440
 
 
441
   Having a mutex locked longer than required is a bad practice which can lead
 
442
   to bad performances. Don't forget that when a mutex is locked, other threads
 
443
   may be waiting doing nothing until it is released.
 
444
 
 
445
   .. method:: Lock(mutex)
 
446
 
 
447
   Construct the lock with a target mutex.
 
448
 
 
449
   The mutex passed to :class:`Lock` is automatically locked.