Über uns
2D-Spielebibliothek für Odin. Einsteigerfreundlich und leicht anzupassen.
Spiele und EmulationWhat you can do
- Load and draw textures
- Draw geometric shapes
- Cameras for moving, rotating and scaling the view
- Audio playback of audio clips and music
Gemessener Sternzuwachs
- in 24h
- 0
- in 7 days
- +5
- in 15 days
- +5
- in 1 month
- Verlauf wird gesammelt
Nettoänderungen in überlappenden Zeiträumen, keine Tagessummen. Fehlende Messwerte bleiben leer. Sterne zeigen Interesse, nicht Qualität.
README.md
OriginalMake 2D games using the Odin Programming Language! Karl2D is a beginner friendly game creation library. It strives to minimize the number of dependencies, making you feel in control of the technology stack. Using Karl2D you can create desktop games, as well as web games (NO emscripten needed!)
See karl2d.doc.odin for an API overview.
Support the project by becoming a sponsor here on GitHub or on Patreon.
Features
- Load and draw textures
- Draw geometric shapes
- Cameras for moving, rotating and scaling the view
- Audio playback of audio clips and music
- Support for mouse, keyboard and gamepad
- Platforms: Windows, Linux, Mac, Web, and Android (see Running on Android section)
- Touch support on Web and Android
- Custom shader compiler that takes GLSL and turns it into whatever the renderer on each platform needs, including debug information
- Few dependencies: The library attempts to contain all the code down to the OS/rendering APIs
- AI agents have an easy time understanding the library thanks to: (1) An easy-to-overview API (2) The agent can read and modify the library all the way down to the OS calls without digging into dependencies (which is good for humans too!).
Getting started
- Install the Odin compiler using the instructions on odin-lang.org. On Windows? Use my odin-installer
- Create a folder for your game
- Within your game folder, put the contents of this repository into a subfolder called
karl2d - Within your game folder, create a file called
game.odin(orwhatever_you_want.odin) and copy this into it:
package hello_world
import k2 "karl2d"
main :: proc() {
k2.init(1280, 720, "Greetings from Karl2D!")
for k2.update() {
k2.clear(k2.LIGHT_BLUE)
k2.draw_text("Hellope!", {50, 50}, 100, k2.DARK_BLUE)
k2.present()
}
k2.shutdown()
}
- Navigate into the your game folder using a command prompt and run your game by writing
odin run .(note the period!) - A simple program that just shows the word "Hellope!" will appear. See the examples below for ideas on what to do next, or read karl2d.doc.odin for an API overview.
- Enjoy!
- If you want to make a web build of your game, continue to the Making a web build section below. See Running on Android for how to make a phone build of your game.
Get help
Discuss and get help in the #karl2d channel on my Discord server.
Examples
See the examples folder for a wide variety of example programs.
Here are links to live web builds of some of the examples:
- Basics - Draw texture, process some input
- SPACE CAT - Small top-down game demo
- Camera - How to use a camera
- Dual grid tilemap - Minimal tilemap editor using dual grid tilemap technique
- Positional audio - Playing sounds panning them based on world location
- Render Texture - Drawing into a texture and then drawing that texture multiple times
- Box2D
- Fonts
- Gamepad
- Mouse
- Snake
Making a web build
Let's look at how to make your game playable on the web.
Here I assume that you've set your project up like the Getting started guide above says. Change the the following code:
package hello_world
import k2 "karl2d"
main :: proc() {
k2.init(1280, 720, "Greetings from Karl2D!")
for k2.update() {
k2.clear(k2.LIGHT_BLUE)
k2.draw_text("Hellope!", {50, 50}, 100, k2.DARK_BLUE)
k2.present()
}
k2.shutdown()
}
into this:
package hello_world
import k2 "karl2d"
main :: proc() {
init()
for step() {}
shutdown()
}
init :: proc() {
k2.init(1280, 720, "Greetings from Karl2D!")
}
step :: proc() -> bool {
if !k2.update() {
return false
}
k2.clear(k2.LIGHT_BLUE)
k2.draw_text("Hellope!", {50, 50}, 100, k2.DARK_BLUE)
k2.present()
return true
}
shutdown :: proc() {
k2.shutdown()
}
There is still a main procedure that is used for desktop builds. The web builds will use the init and step procedures directly. This is because the web build can't have an "loop" like for k2.update() {}. Instead, it will run step when the browser wants to redraw its content.
To compile the web build, run the build_web tool that is bundled with karl2d. Using a command prompt, navigate to your game folder and run (note the double dash and the period! The period is the path to where your game lives, where a single . means "current folder")
odin run karl2d/build_web -- .
The web build will end up in bin/web. Run your game by navigating into it and opening index.html
[!WARNING] If you open the
index.htmlfile and see nothing, then there might be an error about "cross site policy" stuff in the browser's console. In that case you can use python to run a local web-server and access the web build through it. Runpython -m http.serverin thebin/webfolder and then navigate tohttps://localhost:8000.
[!WARNING] On Linux / Mac you may need to install some
lldpackage that contains thewasm-ldlinker. It's included with Odin on Windows.
[!NOTE] To get better in-browser debug symbols, you can add
-debugwhen running thebuild_webscript:odin run karl2d/build_web -- . -debugNote that it comes after the--: That's the flags that get sent on to thebuild_webprogram! There are also-o:speed/sizeflags to turn on optimization.
What the build_web script does
The build_web tool will copy odin.js file from <odin>/core/sys/wasm/js/odin.js into the bin/web folder. It will also copy a HTML index file into that folder.
It will also create a build/web folder. That's the package it actually builds. It contains a bit of wrapper code that then calls the init and step functions of your game. The result of building the wrapper (and your game) is a main.wasm file that also ends up in bin/web.
Loading assets on the web
Procedures such as k2.load_texture_from_file("image.png") do not work on the web because they depend on there being a file system. Instead you can do k2.load_texture_from_bytes(#load("image.png")). The #load call will bake the image into your executable at compile time.
If you want to load using k2.load_texture_from_file on desktop and k2.load_texture_from_bytes + #load on web, then you can create an abstraction.
Desktop version (put in a file with #+build !js at the top):
load_texture :: proc($name: string) -> k2.Texture {
return k2.load_texture_from_file(name)
}
Web version (put in a file with #+build js at the top):
load_texture :: proc($name: string) -> k2.Texture {
return k2.load_texture_from_bytes(#load(name))
}
The $ in front of the name parameter ensures that you're passing a compile-time constant, which makes it possible to use #load within the web version.
Running on Android
To run your game on your Android phone, use the build_android tool that Karl2D comes with. For example, if you are currently in the Karl2D folder, then you can run the touch example on your Android phone by executing:
odin run build_android -- examples/touch -run
This assumes that you have the phone available for USB or Wireless debugging, so that adb can control it. The -run argument will install the app and start it on your connected phone. Skip -run to just produce a .apk file.
NOTE: If you have
karl2das a subfolder inside your game directory, then you probably want to run:
odin run karl2d/build_android -- . -run
If you do not have the Android SDK or Java installed, then you can download a portable copy of it all into the build_android/sdk folder by running:
odin run build_android/setup
After this completes, build_android will look inside the build_android/sdk folder for the SDK and Java.
Android assets folder
There is an assets folder within an Android app. You can make build_android bundle things into it like so:
odin run build_android -- examples/audio -assets:*.ogg -run
You can also point out an entire folder using -assets. You can use -assets multiple times to include those things. The path you pass to -assets is relative to the root of the program you are building, in this case relative to examples/audio.
Procedures like k2.load_texture_from_file will automatically find the bundled assets. However, if you write your own code that loads them at runtime using for example os.read_entire_file, then use the /android_asset/ prefix on your path. The Karl2D procs resource loading procedures use that prefix automatically.
Hot reload
Some kind of gameplay code hot reload is planned as part of the library. Currently, there is an experimental implementation of this in a separate repository: https://github.com/karl-zylinski/karl2d-hot-reload-template
Beta status
Karl2D is currently is currently in beta. If you find any issues, then please create an issue here on GitHub!
See the milestones for a list of big things to come.
Architecture notes
The platform-independent parts and the API lives in karl2d.odin. There is a karl2d.doc.odin file that is generated from karl2d.odin. It simply strips the bodies of the procedures, creating a nice overview.
karl2d.odin in turn uses interfaces for creating abstractions for the platform, rendering and audio.
The platform abstraction depends on the operating system. I do not use anything like GLFW in order to abstract away window creation and event handling. Less libraries between you and the OS, less trouble when shipping!
The rendering abstraction tells Karl2D how to talk to the GPU. I currently support three rendering APIs: D3D11, OpenGL and WebGL. On some platforms you have multiple choices, for example on Windows you can use both D3D11 and OpenGL (using the compile flag -define:KARL2D_RENDER_BACKEND=gl/d3d11). Each of these are known as rendering backends. All rendering backends use the same shading language: GLSL. There is a custom shader compiler in the shader_compiler folder that takes GLSL and directly outputs whatever each backend needs.
The platform independent rendering code in karl2d.odin creates a list of vertices for each batch it needs to render. That's done independently of the rendering backend. The backend is just fed that list, along with information about what shader and such to use.
The audio support in Karl2D is done using a software mixer that is part of karl2d.odin. I wrote a blog post called Audio in Karl2D: Software mixing, OS APIs and general design where I describe how the audio system works. Since that blog post was written, I've introduced an audio mixer thread. On platforms that support threading (everything except web), the mixer thread calls the mixing procedure and pulls the samples out of it. On web the mixer is run and then the samples are pushed into the web audio backend.
The web builds do not need emscripten, instead I've written a WebGL backend and make use of the official Odin JS runtime.
On Linux there are two windowing systems to choose between: x11 and wayland. Karl2D dynamically opens their shared libraries and tries to connect to them, first it tries wayland, and then x11. Therefore it does not need both to be installed to launch. It is all handled at runtime. End users can define the environment variable KARL2D_LINUX_WINDOWING=x11/wayland if they need to override it (usually for troubleshooting).
How AI programming is used
The core parts of Karl2D were written without any AI support at all:
- The whole API in karl2d.odin
- All documentation. AI is not allowed to write any documentation, not even for non-public code
- The Windows, Linux, Mac and Web platforms
- The D3D11, gl and webgl renderers
- The waveout, ALSA, CoreAudio and WebAudio backends and the audio mixer
- Essentially the whole architecture is designed by Karl Zylinski
AI aided the creation of some late parts of the library, such as:
- Shader compiler
- STB library ports
- Wayland client-side decorations
- Tests
- Additional platform bindings
- Android platform and android build tools
Have fun!
Ähnliche Projekte entdecken
Wird geladen…