treasurehunting2/PySDL2-0.9.5/doc/html/modules/sdl2.html

296 lines
20 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sdl2 - SDL2 library wrapper &#8212; PySDL2 0.9.5 documentation</title>
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '0.9.5',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="top" title="PySDL2 0.9.5 documentation" href="../index.html" />
<link rel="up" title="API reference" href="index.html" />
<link rel="next" title="sdl2.sdlgfx - SDL2_gfx library wrapper" href="sdl2_sdlgfx.html" />
<link rel="prev" title="API reference" href="index.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="sdl2_sdlgfx.html" title="sdl2.sdlgfx - SDL2_gfx library wrapper"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="index.html" title="API reference"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">PySDL2 0.9.5 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">API reference</a> &#187;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<span class="target" id="module-sdl2"></span><div class="section" id="sdl2-sdl2-library-wrapper">
<h1>sdl2 - SDL2 library wrapper<a class="headerlink" href="#sdl2-sdl2-library-wrapper" title="Permalink to this headline"></a></h1>
<p>The <a class="reference internal" href="#module-sdl2" title="sdl2: SDL2 library wrapper"><code class="xref py py-mod docutils literal"><span class="pre">sdl2</span></code></a> package is a <a class="reference external" href="http://docs.python.org/library/ctypes.html#module-ctypes" title="(in Python v2.7)"><code class="xref py py-mod docutils literal"><span class="pre">ctypes</span></code></a>-based wrapper around
the SDL2 library. It wraps nearly all publicly accessible structures and
functions of the SDL2 library to be accessible from Python code.</p>
<p>A detailed documentation about the behaviour of the different functions
can found within the <a class="reference external" href="http://wiki.libsdl.org/moin.cgi/CategoryAPI">SDL2 documentation</a>.</p>
<div class="section" id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Permalink to this headline"></a></h2>
<p>You can use <a class="reference internal" href="#module-sdl2" title="sdl2: SDL2 library wrapper"><code class="xref py py-mod docutils literal"><span class="pre">sdl2</span></code></a> in nearly exactly the same way as you would do with
the SDL library and C code.</p>
<p>A brief example in C code:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;SDL.h&gt;</span><span class="cp"></span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">running</span><span class="p">;</span>
<span class="n">SDL_Window</span> <span class="o">*</span><span class="n">window</span><span class="p">;</span>
<span class="n">SDL_Surface</span> <span class="o">*</span><span class="n">windowsurface</span><span class="p">;</span>
<span class="n">SDL_Surface</span> <span class="o">*</span><span class="n">image</span><span class="p">;</span>
<span class="n">SDL_Event</span> <span class="n">event</span><span class="p">;</span>
<span class="n">SDL_Init</span><span class="p">(</span><span class="n">SDL_INIT_VIDEO</span><span class="p">);</span>
<span class="n">window</span> <span class="o">=</span> <span class="n">SDL_CreateWindow</span><span class="p">(</span><span class="s">&quot;Hello World&quot;</span><span class="p">,</span>
<span class="n">SDL_WINDOWPOS_CENTERED</span><span class="p">,</span> <span class="n">SDL_WINDOWPOS_CENTERED</span><span class="p">,</span>
<span class="mi">592</span><span class="p">,</span> <span class="mi">460</span><span class="p">,</span> <span class="n">SDL_WINDOW_SHOWN</span><span class="p">);</span>
<span class="n">windowsurface</span> <span class="o">=</span> <span class="n">SDL_GetWindowSurface</span><span class="p">(</span><span class="n">window</span><span class="p">);</span>
<span class="n">image</span> <span class="o">=</span> <span class="n">SDL_LoadBMP</span><span class="p">(</span><span class="s">&quot;exampleimage.bmp&quot;</span><span class="p">);</span>
<span class="n">SDL_BlitSurface</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="n">windowsurface</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
<span class="n">SDL_UpdateWindowSurface</span><span class="p">(</span><span class="n">window</span><span class="p">);</span>
<span class="n">SDL_FreeSurface</span><span class="p">(</span><span class="n">image</span><span class="p">);</span>
<span class="n">running</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">running</span><span class="p">)</span> <span class="p">{</span>
<span class="k">while</span> <span class="p">(</span><span class="n">SDL_PollEvent</span><span class="p">(</span><span class="o">&amp;</span><span class="n">event</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">event</span><span class="p">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">SDL_QUIT</span><span class="p">)</span> <span class="p">{</span>
<span class="n">running</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">break</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">SDL_DestroyWindow</span><span class="p">(</span><span class="n">window</span><span class="p">);</span>
<span class="n">SDL_Quit</span><span class="p">();</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Doing the same in Python:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">ctypes</span>
<span class="kn">from</span> <span class="nn">sdl2</span> <span class="kn">import</span> <span class="o">*</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
<span class="n">SDL_Init</span><span class="p">(</span><span class="n">SDL_INIT_VIDEO</span><span class="p">)</span>
<span class="n">window</span> <span class="o">=</span> <span class="n">SDL_CreateWindow</span><span class="p">(</span><span class="n">b</span><span class="s2">&quot;Hello World&quot;</span><span class="p">,</span>
<span class="n">SDL_WINDOWPOS_CENTERED</span><span class="p">,</span> <span class="n">SDL_WINDOWPOS_CENTERED</span><span class="p">,</span>
<span class="mi">592</span><span class="p">,</span> <span class="mi">460</span><span class="p">,</span> <span class="n">SDL_WINDOW_SHOWN</span><span class="p">)</span>
<span class="n">windowsurface</span> <span class="o">=</span> <span class="n">SDL_GetWindowSurface</span><span class="p">(</span><span class="n">window</span><span class="p">)</span>
<span class="n">image</span> <span class="o">=</span> <span class="n">SDL_LoadBMP</span><span class="p">(</span><span class="n">b</span><span class="s2">&quot;exampleimage.bmp&quot;</span><span class="p">)</span>
<span class="n">SDL_BlitSurface</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span> <span class="n">windowsurface</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
<span class="n">SDL_UpdateWindowSurface</span><span class="p">(</span><span class="n">window</span><span class="p">)</span>
<span class="n">SDL_FreeSurface</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
<span class="n">running</span> <span class="o">=</span> <span class="bp">True</span>
<span class="n">event</span> <span class="o">=</span> <span class="n">SDL_Event</span><span class="p">()</span>
<span class="k">while</span> <span class="n">running</span><span class="p">:</span>
<span class="k">while</span> <span class="n">SDL_PollEvent</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">event</span><span class="p">))</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">if</span> <span class="n">event</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">SDL_QUIT</span><span class="p">:</span>
<span class="n">running</span> <span class="o">=</span> <span class="bp">False</span>
<span class="k">break</span>
<span class="n">SDL_DestroyWindow</span><span class="p">(</span><span class="n">window</span><span class="p">)</span>
<span class="n">SDL_Quit</span><span class="p">()</span>
<span class="k">return</span> <span class="mi">0</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>
</pre></div>
</div>
<p>You can port code in a straightforward manner from one language to the other,
though it is important to know about the limitations and slight differences
mentioned below. Also, PySDL2 offers advanced functionality, which also feels
more <em>&#8216;pythonic&#8217;</em>, via the <a class="reference internal" href="sdl2ext.html#module-sdl2.ext" title="sdl2.ext: Python extensions for SDL2"><code class="xref py py-mod docutils literal"><span class="pre">sdl2.ext</span></code></a> package.</p>
</div>
<div class="section" id="missing-interfaces">
<h2>Missing interfaces<a class="headerlink" href="#missing-interfaces" title="Permalink to this headline"></a></h2>
<p>The following functions, classes, constants and macros of SDL2 are <em>not</em>
available within <a class="reference internal" href="#module-sdl2" title="sdl2: SDL2 library wrapper"><code class="xref py py-mod docutils literal"><span class="pre">sdl2</span></code></a>.</p>
<ul class="simple">
<li><code class="xref c c-data docutils literal"><span class="pre">SDL_REVISION</span></code> and <code class="xref c c-data docutils literal"><span class="pre">SDL_REVISION_NUMBER</span></code> from <code class="docutils literal"><span class="pre">SDL_revision.h</span></code></li>
<li><code class="xref c c-data docutils literal"><span class="pre">SDL_NAME()</span></code> from <code class="docutils literal"><span class="pre">SDL_name.h</span></code></li>
<li><code class="xref c c-func docutils literal"><span class="pre">SDL_MostSignificantBitIndex32()</span></code> from <code class="docutils literal"><span class="pre">SDL_bits.h</span></code></li>
<li>Everything from <code class="docutils literal"><span class="pre">SDL_main.h</span></code></li>
<li>Everything from <code class="docutils literal"><span class="pre">SDL_system.h</span></code></li>
<li>Everything from <code class="docutils literal"><span class="pre">SDL_assert.h</span></code></li>
<li>Everything from <code class="docutils literal"><span class="pre">SDL_thread.h</span></code></li>
<li>Everything from <code class="docutils literal"><span class="pre">SDL_atomic.h</span></code></li>
<li>Everything from <code class="docutils literal"><span class="pre">SDL_opengl.h</span></code></li>
<li>Everything from <code class="docutils literal"><span class="pre">SDL_mutex.h</span></code></li>
</ul>
</div>
<div class="section" id="additional-interfaces">
<h2>Additional interfaces<a class="headerlink" href="#additional-interfaces" title="Permalink to this headline"></a></h2>
<p>The following functions, classes, constants and macros are are <em>not</em> part of
SDL2, but were introduced by <a class="reference internal" href="#module-sdl2" title="sdl2: SDL2 library wrapper"><code class="xref py py-mod docutils literal"><span class="pre">sdl2</span></code></a>.</p>
<dl class="data">
<dt id="sdl2.ALL_PIXELFORMATS">
<code class="descclassname">sdl2.</code><code class="descname">ALL_PIXELFORMATS</code><a class="headerlink" href="#sdl2.ALL_PIXELFORMATS" title="Permalink to this definition"></a></dt>
<dd><p>Tuple containing all SDL2 pixel format constants (SDL_PIXELFORMAT_INDEX1LSB,
..., SDL_PIXELFORMAT_RGB565, ...).</p>
</dd></dl>
<dl class="data">
<dt id="sdl2.AUDIO_FORMATS">
<code class="descclassname">sdl2.</code><code class="descname">AUDIO_FORMATS</code><a class="headerlink" href="#sdl2.AUDIO_FORMATS" title="Permalink to this definition"></a></dt>
<dd><p>Set containing all SDL2 audio format constants (AUDIO_U8, AUDIO_S8,
... AUDIO_F32LSB, ... ).</p>
</dd></dl>
<dl class="function">
<dt id="sdl2.sdl2.rw_from_object">
<code class="descclassname">sdl2.</code><code class="descname">rw_from_object</code><span class="sig-paren">(</span><em>obj : object</em><span class="sig-paren">)</span> &rarr; SDL_RWops<a class="headerlink" href="#sdl2.sdl2.rw_from_object" title="Permalink to this definition"></a></dt>
<dd><p>Creates a SDL_RWops from any Python object. The Python object must at least
support the following methods:</p>
<p>read(length) -&gt; data</p>
<blockquote>
<div>length is the size in bytes to be read. A call to len(data) must
return the correct amount of bytes for the data, so that
len(data) / [size in bytes for a single element from data] returns
the amount of elements. Must raise an error on failure.</div></blockquote>
<p>seek(offset, whence) -&gt; int</p>
<blockquote>
<div><p>offset denotes the offset to move the read/write pointer of the
object to. whence indicates the movement behaviour and can be one
of the following values:</p>
<ul class="simple">
<li>RW_SEEK_SET - move to offset from the start of the file</li>
<li>RW_SEEK_CUR - move by offset from the relative location</li>
<li>RW_SEEK_END - move to offset from the end of the file</li>
</ul>
<p>If it could not move read/write pointer to the desired location,
an error must be raised.</p>
</div></blockquote>
<p>tell() -&gt; int</p>
<blockquote>
<div>Must return the current offset. This method must only be
provided, if seek() does not return any value.</div></blockquote>
<p>close() -&gt; None</p>
<blockquote>
<div>Closes the object(or its internal data access methods). Must raise
an error on failure.</div></blockquote>
<p>write(data) -&gt; None</p>
<blockquote>
<div><p>Writes the passed data(which is a string of bytes) to the object.
Must raise an error on failure.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The write() method is optional and only necessary, if the passed
object should be able to write data.</p>
</div>
</div></blockquote>
<p>The returned <code class="xref py py-class docutils literal"><span class="pre">sdl2.SDL_RWops</span></code> is a pure Python object and
<strong>must not</strong> be freed via <code class="xref py py-func docutils literal"><span class="pre">sdl2.SDL_FreeRW()</span></code>.</p>
</dd></dl>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">sdl2 - SDL2 library wrapper</a><ul>
<li><a class="reference internal" href="#usage">Usage</a></li>
<li><a class="reference internal" href="#missing-interfaces">Missing interfaces</a></li>
<li><a class="reference internal" href="#additional-interfaces">Additional interfaces</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="index.html"
title="previous chapter">API reference</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="sdl2_sdlgfx.html"
title="next chapter">sdl2.sdlgfx - SDL2_gfx library wrapper</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/modules/sdl2.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="sdl2_sdlgfx.html" title="sdl2.sdlgfx - SDL2_gfx library wrapper"
>next</a> |</li>
<li class="right" >
<a href="index.html" title="API reference"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">PySDL2 0.9.5 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="index.html" >API reference</a> &#187;</li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2013-2016, Marcus von Appen.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.6.
</div>
</body>
</html>