Press "Enter" to skip to content

Jacob Kovac – Revitalizing Python Game Development- Packaging, Performance, and Platforms.mp4

Jacob Kovac – Revitalizing Python Game Development- Packaging, Performance, and Platforms.mp4 moderator Please help me in welcoming Jacob Kovac. Jacob Kovac Hello everyone. Thank you for coming. Im Jacob Kovac.

I run a small front end development company that does a combo of mobile and desktop apps using Kivy and Python and web apps with react.js, which is not what Im going to talk about today. I also in the Kivy core developer where I focus on graphics programming. And Im the developer of the KivEnt game engine which is an entity component game engine built on top of Kivy. Lets see here.

So… I guess were going to cover three major things here. Were going to cover platforms, which really is about ensuring that your code will run everywhere even when it has compiled dependencies because not all Python modules and not everything can be done purely in Python.

And also, making sure that you can access system-specific functionality because a lot of these different platforms have all kinds of things in them that you either — you may want for your games or you may need. Oftentimes you might want to, you know, open a native file chooser or something like that so that you can provide a simple, you know, user-friendly way for them to pick files out of their system or whatever. The second part of that is distribution, which is somewhat different from just getting your code to run on an operating system. Its about getting your code to run on random peoples computers who have done who knows what to them. And, in general, I sort of have a very…

Youve got to be distributing compiled binaries for the specific platform. Theres no other way to do this. You cant trust anything else, and most other things are going to be, especially for games, a little too much for your users, who pretty much want to install an app and click go and play a game. They dont want to bother about package management or anything. And finally, performance, which — building games in Python has not really been all that popular, in large — er, at least a little bit because there is performance problems writing everything in Python.

Its not exactly the best for gaming. It isnt really optimized for that. Its meant for other things. But we can get past that because Kivy is based heavily on Cython, which is a sort of an unholy union of C and Python all at the same time that is super sweet. I really like it, and its the reason Kivy can work.

Its the reason Kivy can be fast enough everywhere and its the reason the KivEnt game engine can do what it can do. So in addition, I guess I felt like I needed to justify a little bit why Pygame, Vispy, Pyglet, your other favorite thing is not really — why I had to build another game engine, basically, in Python and another way to do this. Part of it is that the fundamental approach Im taking is really heavily rooted in Cython, and the thing with going in that direction is that for the optimizations to truly work, you also want everything to be Cython, or a lot of things to be Cython, because as soon as youre falling back into hitting a different Python library or something else, you may not be able to — you may impose a lot of the penalties of Python lookups and attribute access and so on, because youre having to write shims between your code that you have written and taken care of to be nice efficiency, but who knows what other libraries do. In addition, I guess on desktop theres also been some attempts to do this by using NumPy or something like that, which is a great approach.

When I started KivEnt, NumPy did not run on mobile. The Kivy guys have done a really great job of fixing that since then. But — so I decided to avoid any heavy dependencies like numpy. And in addition, I really think if youre trying to make Mario or Flappy Bird or something, you dont really need the entire scientific numerical analysis package.

And in addition, some of these various frameworks have limited or no mobile compatibility, which I think building games in mobile is super fun. Its also, I think, the majority of devices out there now, so… Especially — when I think this, I oftentimes — one of my biggest reasons for building KivEnt was that… …we want children to learn to program, and we often teach programming via games. And most of the time, as students get older, theyre probably going to end up using some type of Python for something, whether its research or whatever it is you do with the scientific stack of Python. Im not really certain.

So I wanted to make sure that kids could build games in Python, because thats going to be one of the most useful languages for them going forward if they dont become professional computer programmers. I am very much in love with the idea of computer literacy becoming much — or programming literacy becoming much more than a thing that the top 5 of the world does. So, yeah.

And in addition, some of these things require you to write your own event loop or make blit calls and flip buffers and all that kind of stuff. Thats all really, really not a good — like, that means that everybody has a bunch of spaghetti code where theyve decided how to do all of these things for their unique project. And I was hoping I could come up with a way to make it so we can all share the same basic low-level event loops and flipping buffers and such, so that we can share our code more readily instead of having to cobble together things sort of haphazardly. And in addition, the Kivy framework is just wonderful. Its a much — it addresses all of these kind of problems Ive just mentioned, and it also provides a very powerful and simple API that has all kinds of cool things like replacements.

The KV language is sort of a weird CSS thats super better than CSS and doesnt have any of its problems, at least from my opinion. Okay, so when it comes to platforms, the Kivy ecosystem gives us several really great ways to access these things. There are the Pyobjus and Pynjius tools, which give you access to Objective-C and Java code dynamically, so that you can basically call anything. This is a much better approach in the long term than simply providing a nice wrapper for yall to use based on what we think you want, because now you can use any part of the Android or iOS systems depending on what you need rather than having to wait for us to wrap it.

In addition, Cython is a great tool here because it will let you call C or C code fairly transparently in your Python files — well, your Cython files. So between that, I mean if you can get access to C, C, Java, and Objective-C code, I think you pretty much have access to a good bit of the ecosystem out there beyond Python that is a related to game development. In addition, I did want to mention that Kivy has a really cool project called Plyer, which is a platform-independent compatibility layer, which aims to make it so that you dont have to individually wrap all of these features. And instead weve already done it so that you can just import gyroscope, and if theres a gyroscope on your platform, youll get the gyroscope data, and if theres not, you will — youll get a not implemented error. And so, when it comes to making sure all of your dependencies work everywhere, there is basically two approaches.

You can look for a library thats already been built to work everywhere — something like the STL 2, which prides itself on working on all desktop and mobile platforms. But sometimes you cant do that. Sometimes youre working with various libraries based on, you know, depending on the platform such as maybe interacting with the Windows API or the iOS API or something like that. And so you sometimes need to find a solution that abstracts over those various APIs to find a common ground that lets you transparently write Python to use those features.

And Kivy provides really very good tools for doing this and for really getting your hands dirty and being able to make use of the whole platform without it being too much of a pain. So I just wanted to give a quick example. This is the — from Plyer, this is the Java wrapper for the gyroscope. And… This is calling Java code.

Almost everything in here is Java. And it doesnt really look like Java, I dont think. And its definitely a lot better than actually starting out the clips and writing some Java. And so… I guess I dont really want to get too in detail here.

If youre really interested in this, check out Plyer. Look at the code. Heres the Objective-C version, and notice that theyre sort of very different because in the Android version of gyroscope, you need to schedule a callback thats going to get called, whereas in the Objective-C, you just hit the end point and check what the value is at that time. And so thats a great example of how, like, any of these features could be implemented vastly different across platform. And so oftentimes a one-to-one conversion can be a little difficult to come up with, although not impossible.

And Plyer helps you do that. And this is what your code would look like if youre using the gyroscope in Plyer. Wherever you wanted to start your code, youd just call gyroscope.enable. Wherever you wanted to — once youre done with the gyroscope, you call gyroscope.disable and you get the orientation. You no longer have to think about, you know, Objective-C.

You no longer have to think about Java. And this is sort of the dream of the Kivy projects view for Plyer is that we will get to a point where you never think about wrapping Java or wrapping Objective-C. We will just have all of this ready for you and ready to go in Python, which I think is super neat.

And Plyer also works for desktop platforms, although its more developed for mobile because theres a lot more sensors and hardware that you actually want to query, and desktop Python actually has fairly established ways to get access to a lot of that stuff compared to mobile. In addition, I wanted to mention briefly that you can also, oftentimes when youre building games you are going to be interacting with C libraries because thats where a lot of the tools in game development is. This is taken from Kivys code for loading SVG files. And this is just an example of what Cython code looks like if youve never seen it before.

And this is for libtest2 which is a triangulation library, and so something you may want in games because you deal with a lot of triangles there. So its kind of ugly but not super ugly. Youve got some static types, things like that. Its — I like it because you can kind of start out by writing Python code, and then as you realize what you want to do, and also what parts of it are slow, you can conditionally convert that over to a kind of more C-like algorithm without really having to start over from scratch. And so…lets see here.

The real — Kivy is the key to how all of this works, in my opinion, because it really does manage almost all these platform complexity problems. Not only does it provide a consistent API for abstracting over various windowing, input, and getting images loaded and sound loaded and getting input from touch devices of all kinds — TUIO, mtdev, practically everything thats out there on the market. It also is built in case you dont want something generic but you have very, very specific use cases. Lets say FF Player is totally not useful for you in terms of your article displaying needs, but GStreamer is.

Or you want to just target one specific operating system because youve got a very specific idea thats going to make use of Android or iOS or something. Kivy gives you the ability to configure it so that it works exactly the way you want with the providers you want, rather than just providing a single abstraction over everything. So it gives you a lot of flexibility, and that comes in really handy when youre doing things like connecting, you know, the connects to a 50-inch touchscreen and doing some kind of weird mall advertising display with liquor bottles or something.

Thats actually something one of our Kivy core developers does and its really cool. His companys name is Tangible Display and you should check it out if you want to see the type of things that people are doing with Python and Kivy. And then…

I always think its important to mention that Kivy is far more than just a GUI platform. It is an ecosystem of packaging devi– packaging tools, compatibility libraries, and pretty much everything you need to get a really fully functional app deployed on Android. And so that gives you a very good starting point.

For instance, these are all of the compiled libraries that you might commonly use in Python that are already ready for use on Android. These had to be patched or in some other way given special compiler instructions to compile on Android, but the Kivy community has already taken care of that for you. Unfortunately, on iOS, these are the modules that work. Were significantly behind on iOS and we do need more iOS and OS X developers.

Theres a lot of low-hanging fruit out there. If youre interested in how this stuff works, come on by to the Kivy chat channel or our user group or whatever and get started. Its really — we need help. I know a lot of you guys — a lot of programmers are OS X people.

The Kivy team are largely Ubuntu and Windows and, you know, Linux and Windows and so on, and Android. Were not really Apple people on account of Apple prices, is what it really comes down to. So in terms of the packaging story, we really have great news because just recently in the last few months, Python 3 support was added to Python for Android, so that you can now build Python 2 and Python 3 applications with that tool. Thats the Android new deployment target. And its also super neat because prior to this release, we were manually patching Python to make sure it would compile on Android.

And we have instead switched to the CrystaX NDK replacement, which is much more compatible and works as expected. And so it actually just deploys Python 2 and Python 3 from their original source without any patches, which is a super big deal, I think, going forward for just not having all this weird random code that you need to apply to get it on Android. And so CrystaX is really cool. Kivy iOS is still only Py 2. Once again were much further behind in the iOS world. And then finally, at least — for me, I use PyInstaller on desktop platforms because its worked for me.

Theres several other things people could use. Theres Nuitka, which is a C compiler thing that I have heard from some members of the Kivy community that theyve managed to compile applications with that. So thats a way to go.

Theres also py2exe, py2app, and cx_Freeze, so, you know, you can get these in a couple ways. But I think PyInstaller is the most consistent and the most supported for Python 2 and 3. The goal of the Kivy team eventually is to have one packaging tool for all platforms. Weve already started this. Its called Buildozer and it currently has OS X, iOS, and Android support. So you have one spec file that manages to take care of all this complexity, and you basically just say, Buildozer Android or Buildozer iOS.

And hopefully in the future, we will extend this to all desktop platforms. Weve mainly been focused on the ones where theres actually tools missing, which is iOS and Android, but OS X was recently added to Buildozer because weve got some — or at least the creator of Kivy, Tito, Mathieu Virbel, has some interesting ideas about how to deploy OSX and make it better. So lets get into kind of the meat of what I like to do, which is performance stuff. This is — performance comes down to several things.

One, its — and, I mean, I think the most important for visual simulations is correctly using graphics APIs — in this case OpenGL is what Kivy likes to use — because, I mean, these GPUs have fantastic levels of power, and if we use them correctly, we dont even have to worry about Python being that slow, because all the codes can be computed on the GPU, or at least a lot of the most intensive code is. In addition, as I mentioned, Cython is a great tool for allowing us to interact with low-level APIs such as OpenGL or a physics engine or whatever else it is you want to use it for, spatial hashing and so on. And I will go over what I — the way I think Cython APIs should be built, because I have a sort of unique thing going on in the game engine where youre actually intended to either write Python code or Cython code that C imports, which is Cythons code word for importing C code rather than Python code, so that you can avoid the call overhead. A lot of Cython libraries are sort of built to be one-way used from Python, but theres actually a lot of power that can be gained from actually building an API that can also be used from Cython and properly type everything. Not only do you get much better static typing, which can help with large code bases, but you also get a lot of speed.

Then Im going to talk a little bit about an entity-component architecture, because Im sure a lot of you have never heard of it. Its very popular in games nowadays. And finally, Ill introduce KivEnt a little more fully. So really, in a lot of ways, this comes down to: when were working with visualizations, were sending geometry to the GPU.

Thats all were doing. Theres basically two ways we can do this. The first way is sort of to send a flat array. Like if we have a bunch of — like, say we have an X, Y coordinate representing a 2D, you know, position on the screen, we would send a whole bunch of floats in an array.

And then maybe we want to have colors, and colors might be an unsigned character because thats a lot cheaper than a float to store. You can store the whole color in the same size as a single number. And OpenGL has really great support for converting those automatically to floating point numbers so that your math can work properly. And right now Kivy works by doing just that. So if youve got a bunch of floating-point numbers, it throws them into an array.

And if youve got a bunch of whatever else you want, whatever other types — theres several in GL — you know, each one is unique. And thats useful from Python because its much simpler. We can simply take a Python list of floats and parse it and turn into an array.

However, a lot of the time you actually want to interleave your data types, and this is something that KivEnt has brought to the Kivy engine, the graphics engine, is that you can now mix your floating-point numbers and your unsigned characters into a single struct which can be much better, I think, in terms of thinking about your code, because now youre no longer having to track several different arrays. Youre uploading and making sure they all get uploaded and have their memory allocated and everything. You can just allocate one giant array of a single struct thats all the data you need for your geometry. And so ultimately the goal of any game engine is to make these arrays of your geometry and send them to the GPU and you get back what looks like a game.

And so this is one of the most performance-intensive parts of games. You do it probably 60 times a second, and it can take a little while, and so… KivEnt has been built to minimize this time as much as possible.

OK, so, Ive said entity-component architecture a few times without ever explaining myself. It is a method of compositing game objects instead of using object-oriented inheritance. And one of the best examples, I guess, for why this is important is you take something like — say youre building the WarCraft 2 game and youve, you know, youve got this little object hierarchy and youve got, like, a basic game object and then a movable game object that you turn into all your units, and then, like, maybe a building object that is a game object that doesnt move, and so on. And then you make an archer unit thats based off of the movable unit, and then youre like, But I want a tower thats like an archer but it doesnt move. And suddenly your whole carefully orchestrated object-oriented thing has sort of got a wrench thrown in it because you want your tower to behave like an archer.

Youre either going to copy the code or youre going to try some funky multiple inheritance. Its much easier to simply split those up into a component that governs whether something can move, a component that governs how its rendered to the screen, a component that governs whether it can shoot an arrow. And then if you want a tower, you simply add the shooting an arrow component to the building. And if you want a — if you want an invisible archer, you simply remove the rendering component, and this flattens out the structure of your game and makes it much easier for you to experiment with the code youve already written and come up with new features faster and more efficiently. In addition, it allows us to better consider performance optimizations because each individual feature has been isolated so that we can turn it off, turn it on, test how much time its taking, so on and so forth, and reorganize the way it manages its data or whatever so that it works best for what needs to happen.

And finally, it allows us to build game systems that can work together with minimal integration costs, because I can go and write a physics system that will work for anybodys game without even knowing what your game is. And as long as you… I mean, you can just throw it in your game and its going to do what it was going to do regardless of what else is in your game.

Everything else doesnt matter. Just the very specific dependencies of an individual component. And so… Heres my first introduction to KivEnt here. The number one idea behind KivEnt is that we need to take this velocity component Python object and turn it into a struct.

And thats actually going to give you a huge amount of speedup if you follow this through, hundreds to thousands of times faster, because if youre, you know, looking up velocity on 10, 000 different objects and youre looking it up in Python, that is a lot of look-up calls, and thats going to slow you down immensely. And so, now that weve turned it into a struct, the problem is you can no longer write from it from Python. Luckily, Cython does provide us with a really cool cdef class that allows us to sort of write a bunch of ugly boilerplate that eventually allows you to interact with your new C struct in a way that — from Python that is going to be pretty transparent to the user. Theyre just going to, you know, hit vx here and vy and its, you know, just going to be object.vx and object.vy. And so Cython — thats where I think Cython is better than, say, just writing a bunch of C or C code and then, you know, using C types or something to link it together, is that we have this ability to write some kind of PythonCython shim here in the same language without really — with — actually, Im not entirely certain on this, but probably less of an overhead than going through CFFI.

And so to kind of continue this example, the original components update function would look like the one at the top, and the one below it is our new Cythonized update function. Its got — everything has been typed out and its going to be much faster and its pretty much a flat C loop at this point. And at no point does the actual for loop there hit Python objects or go through Python anything.

Its all C. And… I really think one of the big things here is that when you use these entity-component architectures, you have a lot of code reusability. This is why the Unity 3D engine is so big, is that they have split everything into little systems that do individual things so that they can go share them with each other or sell them to each other and do all the things so that youre not starting from scratch every time or struggling to integrate very different models. Its a — you know, weve agreed upon a way to set up the data, to store the data, and to initialize the game objects so that we can all share our work. And so I hope maybe youll go try to build some games with Kivy and KivEnt in the near future.

You can make them for all platforms. You can worry less about low-level concerns. You will have a plan for when you — something is too slow for Python so that your game doesnt just stop in its tracks right there. And you will have a much better organization, I think, than just spaghetti coding everything together.

If you want to learn more, my favorite blog about entity-component systems is the T-machine blog. He has many articles written over the last, like, eight years about it. And then of course Kivy and KivEnts address there. Yeah. Thats kind of my talk. moderator Thank you.

We have a few more minutes for questions. Jacob Kovac I have remembered that I have some — moderator Can you please approach the microphone? Jacob Kovac Oh, sweet. I do have some demos I was going to show off here. This is KivEnt initializing game objects from Python.

Theyre not being deleted. The counter at the bottom is the number of objects. You cannot do this in Python normally. audience member So, with your game engines, have you started looking into AR and VR applications? Jacob Kovac Can you speak up a little? audience member Im sorry.

With your game engine, have you started looking into AR and VR applications? Jacob Kovac I have not looked into any AR or VR stuff yet. Not really something thats in Kivys wheelhouse right now.

Weve mainly stuck in the 2D area. Im still interested in but have not started doing any 3D work. audience member Thank you. audience member So I missed the beginning. You might have mentioned this already, but have you looked into pypy on mobile devices yet, and if so, have you run into any particular challenges with that? Jacob Kovac I have not looked into it, but from my perspective, one of the issues with the just-in-time compilation method for apps like games and such is that oftentimes, particularly on mobile, your app may not run for more than 2, 3 minutes, and thats not exactly JITs wheelhouse, really.

In addition, as soon as you switch to pypy, you pretty much lose the entire Kivy ecosystem, so its not exactly easy to just slot in a replacement since Kivy is so hooked into C and C-based things. audience member So you mentioned briefly about the Unity engine. Do you know if theres a way to use Python interface to actually call Unity libraries, or using the engine to develop games? Jacob Kovac Can you repeat your question a little louder? audience member You mentioned briefly about the Unity engine. Im just wondering if theres a way to use Python interface to, like, develop games using Unity engine. Jacob Kovac I am not certain on that.

Im sure theres a way, because with programming, theres almost always a way, but I imagine it might be a little dirty, knowing how annoying it is to interopt just with C code or C code from C in Unity. Its not exactly fun. audience member OK. Thanks. moderator If we dont have any further questions, lets give Jacob a round of applause. Jacob Kovac Thank you all very much. Jacob Kovac – Revitalizing Python Game Development- Packaging, Performance, and Platforms.mp4 moderator Please help me in welcoming Jacob Kovac.

Jacob Kovac Hello everyone. Thank you for coming. Im Jacob Kovac. I run a small front end development company that does a combo of mobile and desktop apps using Kivy and Python and web apps with react.js, which is not what Im going to talk about today.

I also in the Kivy core developer where I focus on graphics programming. And Im the developer of the KivEnt game engine which is an entity component game engine built on top of Kivy. Lets see here.

So… I guess were going to cover three major things here. Were going to cover platforms, which really is about ensuring that your code will run everywhere even when it has compiled dependencies because not all Python modules and not everything can be done purely in Python.

And also, making sure that you can access system-specific functionality because a lot of these different platforms have all kinds of things in them that you either — you may want for your games or you may need. Oftentimes you might want to, you know, open a native file chooser or something like that so that you can provide a simple, you know, user-friendly way for them to pick files out of their system or whatever. The second part of that is distribution, which is somewhat different from just getting your code to run on an operating system.

Its about getting your code to run on random peoples computers who have done who knows what to them. And, in general, I sort of have a very… Youve got to be distributing compiled binaries for the specific platform. Theres no other way to do this. You cant trust anything else, and most other things are going to be, especially for games, a little too much for your users, who pretty much want to install an app and click go and play a game.

They dont want to bother about package management or anything. And finally, performance, which — building games in Python has not really been all that popular, in large — er, at least a little bit because there is performance problems writing everything in Python. Its not exactly the best for gaming. It isnt really optimized for that. Its meant for other things.

But we can get past that because Kivy is based heavily on Cython, which is a sort of an unholy union of C and Python all at the same time that is super sweet. I really like it, and its the reason Kivy can work. Its the reason Kivy can be fast enough everywhere and its the reason the KivEnt game engine can do what it can do. So in addition, I guess I felt like I needed to justify a little bit why Pygame, Vispy, Pyglet, your other favorite thing is not really — why I had to build another game engine, basically, in Python and another way to do this.

Part of it is that the fundamental approach Im taking is really heavily rooted in Cython, and the thing with going in that direction is that for the optimizations to truly work, you also want everything to be Cython, or a lot of things to be Cython, because as soon as youre falling back into hitting a different Python library or something else, you may not be able to — you may impose a lot of the penalties of Python lookups and attribute access and so on, because youre having to write shims between your code that you have written and taken care of to be nice efficiency, but who knows what other libraries do. In addition, I guess on desktop theres also been some attempts to do this by using NumPy or something like that, which is a great approach. When I started KivEnt, NumPy did not run on mobile. The Kivy guys have done a really great job of fixing that since then.

But — so I decided to avoid any heavy dependencies like numpy. And in addition, I really think if youre trying to make Mario or Flappy Bird or something, you dont really need the entire scientific numerical analysis package. And in addition, some of these various frameworks have limited or no mobile compatibility, which I think building games in mobile is super fun. Its also, I think, the majority of devices out there now, so… Especially — when I think this, I oftentimes — one of my biggest reasons for building KivEnt was that… …we want children to learn to program, and we often teach programming via games.

And most of the time, as students get older, theyre probably going to end up using some type of Python for something, whether its research or whatever it is you do with the scientific stack of Python. Im not really certain. So I wanted to make sure that kids could build games in Python, because thats going to be one of the most useful languages for them going forward if they dont become professional computer programmers.

I am very much in love with the idea of computer literacy becoming much — or programming literacy becoming much more than a thing that the top 5 of the world does. So, yeah. And in addition, some of these things require you to write your own event loop or make blit calls and flip buffers and all that kind of stuff.

Thats all really, really not a good — like, that means that everybody has a bunch of spaghetti code where theyve decided how to do all of these things for their unique project. And I was hoping I could come up with a way to make it so we can all share the same basic low-level event loops and flipping buffers and such, so that we can share our code more readily instead of having to cobble together things sort of haphazardly. And in addition, the Kivy framework is just wonderful. Its a much — it addresses all of these kind of problems Ive just mentioned, and it also provides a very powerful and simple API that has all kinds of cool things like replacements. The KV language is sort of a weird CSS thats super better than CSS and doesnt have any of its problems, at least from my opinion.

Okay, so when it comes to platforms, the Kivy ecosystem gives us several really great ways to access these things. There are the Pyobjus and Pynjius tools, which give you access to Objective-C and Java code dynamically, so that you can basically call anything. This is a much better approach in the long term than simply providing a nice wrapper for yall to use based on what we think you want, because now you can use any part of the Android or iOS systems depending on what you need rather than having to wait for us to wrap it.

In addition, Cython is a great tool here because it will let you call C or C code fairly transparently in your Python files — well, your Cython files. So between that, I mean if you can get access to C, C, Java, and Objective-C code, I think you pretty much have access to a good bit of the ecosystem out there beyond Python that is a related to game development. In addition, I did want to mention that Kivy has a really cool project called Plyer, which is a platform-independent compatibility layer, which aims to make it so that you dont have to individually wrap all of these features.

And instead weve already done it so that you can just import gyroscope, and if theres a gyroscope on your platform, youll get the gyroscope data, and if theres not, you will — youll get a not implemented error. And so, when it comes to making sure all of your dependencies work everywhere, there is basically two approaches. You can look for a library thats already been built to work everywhere — something like the STL 2, which prides itself on working on all desktop and mobile platforms. But sometimes you cant do that.

Sometimes youre working with various libraries based on, you know, depending on the platform such as maybe interacting with the Windows API or the iOS API or something like that. And so you sometimes need to find a solution that abstracts over those various APIs to find a common ground that lets you transparently write Python to use those features. And Kivy provides really very good tools for doing this and for really getting your hands dirty and being able to make use of the whole platform without it being too much of a pain. So I just wanted to give a quick example.

This is the — from Plyer, this is the Java wrapper for the gyroscope. And… This is calling Java code. Almost everything in here is Java.

And it doesnt really look like Java, I dont think. And its definitely a lot better than actually starting out the clips and writing some Java. And so… I guess I dont really want to get too in detail here.

If youre really interested in this, check out Plyer. Look at the code. Heres the Objective-C version, and notice that theyre sort of very different because in the Android version of gyroscope, you need to schedule a callback thats going to get called, whereas in the Objective-C, you just hit the end point and check what the value is at that time.

And so thats a great example of how, like, any of these features could be implemented vastly different across platform. And so oftentimes a one-to-one conversion can be a little difficult to come up with, although not impossible. And Plyer helps you do that.

And this is what your code would look like if youre using the gyroscope in Plyer. Wherever you wanted to start your code, youd just call gyroscope.enable. Wherever you wanted to — once youre done with the gyroscope, you call gyroscope.disable and you get the orientation. You no longer have to think about, you know, Objective-C. You no longer have to think about Java.

And this is sort of the dream of the Kivy projects view for Plyer is that we will get to a point where you never think about wrapping Java or wrapping Objective-C. We will just have all of this ready for you and ready to go in Python, which I think is super neat. And Plyer also works for desktop platforms, although its more developed for mobile because theres a lot more sensors and hardware that you actually want to query, and desktop Python actually has fairly established ways to get access to a lot of that stuff compared to mobile. In addition, I wanted to mention briefly that you can also, oftentimes when youre building games you are going to be interacting with C libraries because thats where a lot of the tools in game development is. This is taken from Kivys code for loading SVG files.

And this is just an example of what Cython code looks like if youve never seen it before. And this is for libtest2 which is a triangulation library, and so something you may want in games because you deal with a lot of triangles there. So its kind of ugly but not super ugly. Youve got some static types, things like that. Its — I like it because you can kind of start out by writing Python code, and then as you realize what you want to do, and also what parts of it are slow, you can conditionally convert that over to a kind of more C-like algorithm without really having to start over from scratch.

And so…lets see here. The real — Kivy is the key to how all of this works, in my opinion, because it really does manage almost all these platform complexity problems. Not only does it provide a consistent API for abstracting over various windowing, input, and getting images loaded and sound loaded and getting input from touch devices of all kinds — TUIO, mtdev, practically everything thats out there on the market. It also is built in case you dont want something generic but you have very, very specific use cases. Lets say FF Player is totally not useful for you in terms of your article displaying needs, but GStreamer is.

Or you want to just target one specific operating system because youve got a very specific idea thats going to make use of Android or iOS or something. Kivy gives you the ability to configure it so that it works exactly the way you want with the providers you want, rather than just providing a single abstraction over everything. So it gives you a lot of flexibility, and that comes in really handy when youre doing things like connecting, you know, the connects to a 50-inch touchscreen and doing some kind of weird mall advertising display with liquor bottles or something. Thats actually something one of our Kivy core developers does and its really cool. His companys name is Tangible Display and you should check it out if you want to see the type of things that people are doing with Python and Kivy.

And then… I always think its important to mention that Kivy is far more than just a GUI platform. It is an ecosystem of packaging devi– packaging tools, compatibility libraries, and pretty much everything you need to get a really fully functional app deployed on Android. And so that gives you a very good starting point. For instance, these are all of the compiled libraries that you might commonly use in Python that are already ready for use on Android.

These had to be patched or in some other way given special compiler instructions to compile on Android, but the Kivy community has already taken care of that for you. Unfortunately, on iOS, these are the modules that work. Were significantly behind on iOS and we do need more iOS and OS X developers.

Theres a lot of low-hanging fruit out there. If youre interested in how this stuff works, come on by to the Kivy chat channel or our user group or whatever and get started. Its really — we need help.

I know a lot of you guys — a lot of programmers are OS X people. The Kivy team are largely Ubuntu and Windows and, you know, Linux and Windows and so on, and Android. Were not really Apple people on account of Apple prices, is what it really comes down to. So in terms of the packaging story, we really have great news because just recently in the last few months, Python 3 support was added to Python for Android, so that you can now build Python 2 and Python 3 applications with that tool.

Thats the Android new deployment target. And its also super neat because prior to this release, we were manually patching Python to make sure it would compile on Android. And we have instead switched to the CrystaX NDK replacement, which is much more compatible and works as expected. And so it actually just deploys Python 2 and Python 3 from their original source without any patches, which is a super big deal, I think, going forward for just not having all this weird random code that you need to apply to get it on Android. And so CrystaX is really cool.

Kivy iOS is still only Py 2. Once again were much further behind in the iOS world. And then finally, at least — for me, I use PyInstaller on desktop platforms because its worked for me. Theres several other things people could use. Theres Nuitka, which is a C compiler thing that I have heard from some members of the Kivy community that theyve managed to compile applications with that. So thats a way to go.

Theres also py2exe, py2app, and cx_Freeze, so, you know, you can get these in a couple ways. But I think PyInstaller is the most consistent and the most supported for Python 2 and 3. The goal of the Kivy team eventually is to have one packaging tool for all platforms. Weve already started this. Its called Buildozer and it currently has OS X, iOS, and Android support.

So you have one spec file that manages to take care of all this complexity, and you basically just say, Buildozer Android or Buildozer iOS. And hopefully in the future, we will extend this to all desktop platforms. Weve mainly been focused on the ones where theres actually tools missing, which is iOS and Android, but OS X was recently added to Buildozer because weve got some — or at least the creator of Kivy, Tito, Mathieu Virbel, has some interesting ideas about how to deploy OSX and make it better. So lets get into kind of the meat of what I like to do, which is performance stuff.

This is — performance comes down to several things. One, its — and, I mean, I think the most important for visual simulations is correctly using graphics APIs — in this case OpenGL is what Kivy likes to use — because, I mean, these GPUs have fantastic levels of power, and if we use them correctly, we dont even have to worry about Python being that slow, because all the codes can be computed on the GPU, or at least a lot of the most intensive code is. In addition, as I mentioned, Cython is a great tool for allowing us to interact with low-level APIs such as OpenGL or a physics engine or whatever else it is you want to use it for, spatial hashing and so on.

And I will go over what I — the way I think Cython APIs should be built, because I have a sort of unique thing going on in the game engine where youre actually intended to either write Python code or Cython code that C imports, which is Cythons code word for importing C code rather than Python code, so that you can avoid the call overhead. A lot of Cython libraries are sort of built to be one-way used from Python, but theres actually a lot of power that can be gained from actually building an API that can also be used from Cython and properly type everything. Not only do you get much better static typing, which can help with large code bases, but you also get a lot of speed. Then Im going to talk a little bit about an entity-component architecture, because Im sure a lot of you have never heard of it. Its very popular in games nowadays.

And finally, Ill introduce KivEnt a little more fully. So really, in a lot of ways, this comes down to: when were working with visualizations, were sending geometry to the GPU. Thats all were doing.

Theres basically two ways we can do this. The first way is sort of to send a flat array. Like if we have a bunch of — like, say we have an X, Y coordinate representing a 2D, you know, position on the screen, we would send a whole bunch of floats in an array.

And then maybe we want to have colors, and colors might be an unsigned character because thats a lot cheaper than a float to store. You can store the whole color in the same size as a single number. And OpenGL has really great support for converting those automatically to floating point numbers so that your math can work properly. And right now Kivy works by doing just that. So if youve got a bunch of floating-point numbers, it throws them into an array.

And if youve got a bunch of whatever else you want, whatever other types — theres several in GL — you know, each one is unique. And thats useful from Python because its much simpler. We can simply take a Python list of floats and parse it and turn into an array. However, a lot of the time you actually want to interleave your data types, and this is something that KivEnt has brought to the Kivy engine, the graphics engine, is that you can now mix your floating-point numbers and your unsigned characters into a single struct which can be much better, I think, in terms of thinking about your code, because now youre no longer having to track several different arrays.

Youre uploading and making sure they all get uploaded and have their memory allocated and everything. You can just allocate one giant array of a single struct thats all the data you need for your geometry. And so ultimately the goal of any game engine is to make these arrays of your geometry and send them to the GPU and you get back what looks like a game.

And so this is one of the most performance-intensive parts of games. You do it probably 60 times a second, and it can take a little while, and so… KivEnt has been built to minimize this time as much as possible. OK, so, Ive said entity-component architecture a few times without ever explaining myself.

It is a method of compositing game objects instead of using object-oriented inheritance. And one of the best examples, I guess, for why this is important is you take something like — say youre building the WarCraft 2 game and youve, you know, youve got this little object hierarchy and youve got, like, a basic game object and then a movable game object that you turn into all your units, and then, like, maybe a building object that is a game object that doesnt move, and so on. And then you make an archer unit thats based off of the movable unit, and then youre like, But I want a tower thats like an archer but it doesnt move. And suddenly your whole carefully orchestrated object-oriented thing has sort of got a wrench thrown in it because you want your tower to behave like an archer. Youre either going to copy the code or youre going to try some funky multiple inheritance.

Its much easier to simply split those up into a component that governs whether something can move, a component that governs how its rendered to the screen, a component that governs whether it can shoot an arrow. And then if you want a tower, you simply add the shooting an arrow component to the building. And if you want a — if you want an invisible archer, you simply remove the rendering component, and this flattens out the structure of your game and makes it much easier for you to experiment with the code youve already written and come up with new features faster and more efficiently. In addition, it allows us to better consider performance optimizations because each individual feature has been isolated so that we can turn it off, turn it on, test how much time its taking, so on and so forth, and reorganize the way it manages its data or whatever so that it works best for what needs to happen.

And finally, it allows us to build game systems that can work together with minimal integration costs, because I can go and write a physics system that will work for anybodys game without even knowing what your game is. And as long as you… I mean, you can just throw it in your game and its going to do what it was going to do regardless of what else is in your game.

Everything else doesnt matter. Just the very specific dependencies of an individual component. And so… Heres my first introduction to KivEnt here. The number one idea behind KivEnt is that we need to take this velocity component Python object and turn it into a struct.

And thats actually going to give you a huge amount of speedup if you follow this through, hundreds to thousands of times faster, because if youre, you know, looking up velocity on 10, 000 different objects and youre looking it up in Python, that is a lot of look-up calls, and thats going to slow you down immensely. And so, now that weve turned it into a struct, the problem is you can no longer write from it from Python. Luckily, Cython does provide us with a really cool cdef class that allows us to sort of write a bunch of ugly boilerplate that eventually allows you to interact with your new C struct in a way that — from Python that is going to be pretty transparent to the user. Theyre just going to, you know, hit vx here and vy and its, you know, just going to be object.vx and object.vy.

And so Cython — thats where I think Cython is better than, say, just writing a bunch of C or C code and then, you know, using C types or something to link it together, is that we have this ability to write some kind of PythonCython shim here in the same language without really — with — actually, Im not entirely certain on this, but probably less of an overhead than going through CFFI. And so to kind of continue this example, the original components update function would look like the one at the top, and the one below it is our new Cythonized update function. Its got — everything has been typed out and its going to be much faster and its pretty much a flat C loop at this point.

And at no point does the actual for loop there hit Python objects or go through Python anything. Its all C. And… I really think one of the big things here is that when you use these entity-component architectures, you have a lot of code reusability.

This is why the Unity 3D engine is so big, is that they have split everything into little systems that do individual things so that they can go share them with each other or sell them to each other and do all the things so that youre not starting from scratch every time or struggling to integrate very different models. Its a — you know, weve agreed upon a way to set up the data, to store the data, and to initialize the game objects so that we can all share our work. And so I hope maybe youll go try to build some games with Kivy and KivEnt in the near future. You can make them for all platforms. You can worry less about low-level concerns.

You will have a plan for when you — something is too slow for Python so that your game doesnt just stop in its tracks right there. And you will have a much better organization, I think, than just spaghetti coding everything together. If you want to learn more, my favorite blog about entity-component systems is the T-machine blog.

He has many articles written over the last, like, eight years about it. And then of course Kivy and KivEnts address there. Yeah.

Thats kind of my talk. moderator Thank you. We have a few more minutes for questions. Jacob Kovac I have remembered that I have some — moderator Can you please approach the microphone?

Jacob Kovac Oh, sweet. I do have some demos I was going to show off here. This is KivEnt initializing game objects from Python.

Theyre not being deleted. The counter at the bottom is the number of objects. You cannot do this in Python normally. audience member So, with your game engines, have you started looking into AR and VR applications? Jacob Kovac Can you speak up a little? audience member Im sorry.

With your game engine, have you started looking into AR and VR applications? Jacob Kovac I have not looked into any AR or VR stuff yet. Not really something thats in Kivys wheelhouse right now. Weve mainly stuck in the 2D area. Im still interested in but have not started doing any 3D work. audience member Thank you. audience member So I missed the beginning.

You might have mentioned this already, but have you looked into pypy on mobile devices yet, and if so, have you run into any particular challenges with that? Jacob Kovac I have not looked into it, but from my perspective, one of the issues with the just-in-time compilation method for apps like games and such is that oftentimes, particularly on mobile, your app may not run for more than 2, 3 minutes, and thats not exactly JITs wheelhouse, really. In addition, as soon as you switch to pypy, you pretty much lose the entire Kivy ecosystem, so its not exactly easy to just slot in a replacement since Kivy is so hooked into C and C-based things. audience member So you mentioned briefly about the Unity engine. Do you know if theres a way to use Python interface to actually call Unity libraries, or using the engine to develop games?

Jacob Kovac Can you repeat your question a little louder? audience member You mentioned briefly about the Unity engine. Im just wondering if theres a way to use Python interface to, like, develop games using Unity engine. Jacob Kovac I am not certain on that. Im sure theres a way, because with programming, theres almost always a way, but I imagine it might be a little dirty, knowing how annoying it is to interopt just with C code or C code from C in Unity.

Its not exactly fun. audience member OK. Thanks. moderator If we dont have any further questions, lets give Jacob a round of applause. Jacob Kovac Thank you all very much.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *