-
-
Notifications
You must be signed in to change notification settings - Fork 3
Add karl2d support #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
uvd540
wants to merge
5
commits into
atomicptr:master
Choose a base branch
from
uvd540:karl2d_support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
23fb33f
add [karl2d](https://github.com/karl-zylinski/karl2d) support
uvd540 ff457a7
fix incorrect library reference
uvd540 960e49d
add karl2d example
uvd540 e6dc2cc
fixed package name
uvd540 8e0dfb8
moved draw_text outside of the game camera to workaround a karl2d bug
uvd540 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package anima_karl2d | ||
|
|
||
| import ".." | ||
| import k2 "../../../karl2d" | ||
| import "../anima_fsm" | ||
|
|
||
| draw :: proc( | ||
| self: ^anima.Animation, | ||
| texture: k2.Texture, | ||
| x: f32, | ||
| y: f32, | ||
| rotation: f32 = 0.0, | ||
| color: k2.Color = k2.WHITE, | ||
| ) { | ||
| frame := anima.current_frame(self) | ||
|
|
||
| flip_x: f32 = self.flip_v ? -1.0 : 1.0 | ||
| flip_y: f32 = self.flip_h ? -1.0 : 1.0 | ||
|
|
||
| k2.draw_texture_ex( | ||
| texture, | ||
| {f32(frame.x), f32(frame.y), flip_x * f32(frame.width), flip_y * f32(frame.height)}, | ||
| {x, y, f32(frame.width), f32(frame.height)}, | ||
| {0, 0}, | ||
| rotation, | ||
| color, | ||
| ) | ||
| } | ||
|
|
||
| fsm_draw :: proc( | ||
| self: ^anima_fsm.FSM($Ident), | ||
| texture: k2.Texture, | ||
| x: f32, | ||
| y: f32, | ||
| rotation: f32 = 0.0, | ||
| color: k2.Color = k2.WHITE, | ||
| ) { | ||
| animation := anima_fsm.current_animation(self) | ||
| draw(animation, texture, x, y, rotation, color) | ||
| } | ||
|
|
||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package cat_fighter_fsm_karl2d | ||
|
|
||
| import k2 "../../../karl2d" | ||
| import "../../anima" | ||
| import "../../anima/anima_fsm" | ||
| import "../../anima/anima_karl2d" | ||
| import "core:fmt" | ||
| import "core:mem" | ||
|
|
||
| CatAnim :: enum u8 { | ||
| PunchRight, | ||
| PunchLeft, | ||
| KickRight, | ||
| KickLeft, | ||
| } | ||
|
|
||
| anim_speed :: 0.1 | ||
|
|
||
| main :: proc() { | ||
| when ODIN_DEBUG { | ||
| track: mem.Tracking_Allocator | ||
| mem.tracking_allocator_init(&track, context.allocator) | ||
| context.allocator = mem.tracking_allocator(&track) | ||
|
|
||
| defer { | ||
| if len(track.allocation_map) > 0 { | ||
| fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map)) | ||
| for _, entry in track.allocation_map { | ||
| fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location) | ||
| } | ||
| } | ||
| if len(track.bad_free_array) > 0 { | ||
| fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array)) | ||
| for entry in track.bad_free_array { | ||
| fmt.eprintf("- %p @ %v\n", entry.memory, entry.location) | ||
| } | ||
| } | ||
| mem.tracking_allocator_destroy(&track) | ||
| } | ||
| } | ||
|
|
||
| k2.init(800, 600, "anima - example - cat fighter fsm") | ||
| defer k2.shutdown() | ||
|
|
||
| texture := k2.load_texture_from_file("assets/cat_fighter.png") | ||
| defer k2.destroy_texture(texture) | ||
|
|
||
| camera := k2.Camera{0, 0, 0, 2.0} | ||
|
|
||
| g := anima.new_grid(50, 50, uint(texture.width), uint(texture.height)) | ||
|
|
||
| cat := anima_fsm.create(CatAnim) | ||
| defer anima_fsm.destroy(cat) | ||
|
|
||
| anima_fsm.add( | ||
| cat, | ||
| CatAnim.PunchRight, | ||
| anima.new_animation(anima.grid_frames(&g, "6-9", 3), anim_speed), | ||
| ) | ||
| anima_fsm.add( | ||
| cat, | ||
| CatAnim.PunchLeft, | ||
| anima.new_animation(anima.grid_frames(&g, "6-9", 3), anim_speed, flip_v = true), | ||
| ) | ||
| anima_fsm.add( | ||
| cat, | ||
| CatAnim.KickLeft, | ||
| anima.new_animation(anima.grid_frames(&g, "0-9", 4, "8-1", 4), anim_speed), | ||
| ) | ||
| anima_fsm.add( | ||
| cat, | ||
| CatAnim.KickRight, | ||
| anima.new_animation(anima.grid_frames(&g, "0-9", 4, "8-1", 4), anim_speed, flip_v = true), | ||
| ) | ||
|
|
||
| anima_fsm.play(cat, CatAnim.PunchRight) | ||
|
|
||
| for k2.update() { | ||
| dt := k2.get_frame_time() | ||
|
|
||
| if k2.key_went_down(.Q) { | ||
| anima_fsm.play(cat, CatAnim.PunchLeft) | ||
| } | ||
|
|
||
| if k2.key_went_down(.W) { | ||
| anima_fsm.play(cat, CatAnim.PunchRight) | ||
| } | ||
|
|
||
| if k2.key_went_down(.A) { | ||
| anima_fsm.play(cat, CatAnim.KickRight) | ||
| } | ||
|
|
||
| if k2.key_went_down(.S) { | ||
| anima_fsm.play(cat, CatAnim.KickLeft) | ||
| } | ||
|
|
||
| anima_fsm.update(cat, dt) | ||
|
|
||
| k2.clear(k2.BLACK) | ||
|
|
||
| k2.set_camera(camera) | ||
|
|
||
| anima_karl2d.fsm_draw(cat, texture, 100, 100) | ||
|
|
||
| k2.set_camera(nil) | ||
|
|
||
| k2.draw_text("Q/W to punch left/right, A/S to kick left/right", {10, 10}, 20, k2.GREEN) | ||
|
|
||
| k2.present() | ||
| } | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This gives me a bit of head scratching because this forces us to have karl2d beside anima in the file tree but I'm also not sure if there even is a better solution 🤔
A super random idea would be:
and then when building you can pick the path yourself using
Do you have another idea? I'm not yet sure if this is a good idea, but I'd prefer if we don't have to hardcode the path (even though this seems like a very sensible standard)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, unfortunately I can't think of a clean way to do this for a non-vendored library. I like your idea as well instead of needing karl2d in the parent directory. Either way will require some explanation in the README. I will defer to your preference.