/kivy/graphics/__init__.py

https://github.com/akshayaurora/kivy · Python · 121 lines · 61 code · 9 blank · 51 comment · 10 complexity · 757bfa7c3c5c835648262950f64a61ea MD5 · raw file

  1. '''
  2. Graphics
  3. ========
  4. This package assembles many low level functions used for drawing. The whole
  5. graphics package is compatible with OpenGL ES 2.0 and has many rendering
  6. optimizations.
  7. The basics
  8. ----------
  9. For drawing on a screen, you will need :
  10. 1. a :class:`~kivy.graphics.instructions.Canvas` object.
  11. 2. :class:`~kivy.graphics.instructions.Instruction` objects.
  12. Each :class:`~kivy.uix.widget.Widget`
  13. in Kivy already has a :class:`Canvas` by default. When you create
  14. a widget, you can create all the instructions needed for drawing. If
  15. `self` is your current widget, you can do::
  16. from kivy.graphics import *
  17. with self.canvas:
  18. # Add a red color
  19. Color(1., 0, 0)
  20. # Add a rectangle
  21. Rectangle(pos=(10, 10), size=(500, 500))
  22. The instructions :class:`Color` and :class:`Rectangle` are automatically added
  23. to the canvas object and will be used when the window is drawn.
  24. .. note::
  25. Kivy drawing instructions are not automatically relative to the position
  26. or size of the widget. You, therefore, need to consider these factors when
  27. drawing. In order to make your drawing instructions relative to the widget,
  28. the instructions need either to be
  29. declared in the :mod:`KvLang <kivy.lang>` or bound to pos and size changes.
  30. Please see :ref:`adding_widget_background` for more detail.
  31. GL Reloading mechanism
  32. ----------------------
  33. .. versionadded:: 1.2.0
  34. During the lifetime of the application, the OpenGL context might be lost. This
  35. happens:
  36. - when the window is resized on OS X or the Windows platform and you're
  37. using pygame as a window provider. This is due to SDL 1.2. In the SDL 1.2
  38. design, it needs to recreate a GL context everytime the window is
  39. resized. This was fixed in SDL 1.3 but pygame is not yet available on it
  40. by default.
  41. - when Android releases the app resources: when your application goes to the
  42. background, Android might reclaim your opengl context to give the
  43. resource to another app. When the user switches back to your application, a
  44. newly created gl context is given to your app.
  45. Starting from 1.2.0, we have introduced a mechanism for reloading all the
  46. graphics resources using the GPU: Canvas, FBO, Shader, Texture, VBO,
  47. and VertexBatch:
  48. - VBO and VertexBatch are constructed by our graphics instructions. We have all
  49. the data needed to reconstruct when reloading.
  50. - Shader: same as VBO, we store the source and values used in the
  51. shader so we are able to recreate the vertex/fragment/program.
  52. - Texture: if the texture has a source (an image file or atlas), the image
  53. is reloaded from the source and reuploaded to the GPU.
  54. You should cover these cases yourself:
  55. - Textures without a source: if you manually created a texture and manually
  56. blit data / a buffer to it, you must handle the reloading yourself. Check the
  57. :doc:`api-kivy.graphics.texture` to learn how to manage that case. (The text
  58. rendering already generates the texture and handles the reloading. You
  59. don't need to reload text yourself.)
  60. - FBO: if you added / removed / drew things multiple times on the FBO, we
  61. can't reload it. We don't keep a history of the instructions put on it.
  62. As for textures without a source, check the :doc:`api-kivy.graphics.fbo` to
  63. learn how to manage that case.
  64. '''
  65. from kivy.graphics.instructions import Callback, Canvas, CanvasBase, \
  66. ContextInstruction, Instruction, InstructionGroup, RenderContext, \
  67. VertexInstruction
  68. from kivy.graphics.context_instructions import BindTexture, Color, \
  69. PushState, ChangeState, PopState, MatrixInstruction, ApplyContextMatrix, \
  70. PopMatrix, PushMatrix, Rotate, Scale, Translate, LoadIdentity, \
  71. UpdateNormalMatrix, gl_init_resources
  72. from kivy.graphics.vertex_instructions import Bezier, BorderImage, Ellipse, \
  73. GraphicException, Line, Mesh, Point, Quad, Rectangle, RoundedRectangle, \
  74. Triangle, SmoothLine
  75. from kivy.graphics.stencil_instructions import StencilPop, StencilPush, \
  76. StencilUse, StencilUnUse
  77. from kivy.graphics.gl_instructions import ClearColor, ClearBuffers
  78. from kivy.graphics.fbo import Fbo
  79. from kivy.graphics.scissor_instructions import ScissorPush, ScissorPop
  80. # very hacky way to avoid pyflakes warning...
  81. __all__ = (Bezier.__name__, BindTexture.__name__, BorderImage.__name__,
  82. Callback.__name__, Canvas.__name__, CanvasBase.__name__,
  83. Color.__name__, ContextInstruction.__name__,
  84. Ellipse.__name__, Fbo.__name__, GraphicException.__name__,
  85. Instruction.__name__, InstructionGroup.__name__,
  86. Line.__name__, SmoothLine.__name__, MatrixInstruction.__name__,
  87. Mesh.__name__, Point.__name__, PopMatrix.__name__,
  88. PushMatrix.__name__, Quad.__name__, Rectangle.__name__,
  89. RenderContext.__name__, Rotate.__name__, Scale.__name__,
  90. StencilPop.__name__, StencilPush.__name__, StencilUse.__name__,
  91. StencilUnUse.__name__, Translate.__name__, Triangle.__name__,
  92. VertexInstruction.__name__, ClearColor.__name__,
  93. ClearBuffers.__name__, gl_init_resources.__name__,
  94. PushState.__name__, ChangeState.__name__, PopState.__name__,
  95. ApplyContextMatrix.__name__, UpdateNormalMatrix.__name__,
  96. LoadIdentity.__name__)