Skip to content
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

Add project organization example #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ jobs:
odin check metal/learn_metal/08-compute $FLAGS
odin check metal/learn_metal/09-compute-to-render $FLAGS

odin check project_organization $FLAGS

odin check sdl2/chase_in_space $FLAGS
odin check sdl2/hellope $FLAGS
odin check sdl2/microui $FLAGS
Expand Down
14 changes: 14 additions & 0 deletions project_organization/awesome_package/add.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package awesome_package

// Our function here is in the `awesome_packge`. It's completely distinct from our `main` functions and package.
// One could simply copy everything in the `awesome_package` directory to their own project and use our functions!
// Packages are most commonly used in Odin for this purpose: self-contained, redistributable libraries.
add :: proc(a: int, b: int) -> int {
return a + b
}

// This function is private to this package and cannot be imported in another package.
@(private)
subtract_private :: proc(a: int, b: int) -> int {
return a - b
}
29 changes: 29 additions & 0 deletions project_organization/game.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import "core:fmt"

// While it's not strictly enforced, it's common to see Odin projects have a single primary package, in this case `main`,
// and use additional files with prefixes, like our `game_`, for organization.
// In Odin, packages such as our `awesome_package`, are most commonly used for self-contained, redistributable libraries
// and not for code organization.
game_init :: proc() {
fmt.println("game_init")
// Even though this function is marked as `private` it's in the same file so we can call it here.
// We can't call it from outside of this file, though.
game_private_inside()
}

game_draw :: proc() {
fmt.println("game_draw")
}

// These functions are private to this particular file.
@(private = "file")
game_private :: proc() {
fmt.println("game_private")
}

@(private = "file")
game_private_inside :: proc() {
fmt.println("game_private_another")
}
24 changes: 24 additions & 0 deletions project_organization/main.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import "./awesome_package"
import "core:fmt"

// See the comments below and be sure to see the other files for additional details!
main :: proc() {
// There's no need to import anything here. Even though `game_init()` and `game_draw()` are in a different file,
// the are in the same `main` package as this file, so are automatically accessible.
game_init()
game_draw()

// This won't compile because `game_private()` is marked as private to the file it is in.
// `Error: Undeclared name: game_private`
// game_private()

// We've imported our `awesome_package` so we can use its function here now.
num := awesome_package.add(2, 3)
fmt.printfln("My awesome number: %d", num)

// But we can't use this function as it's marked as private to its package, so is unexported.
// `Error: 'subtract_private' is not exported by 'awesome_package'`
// num2 := awesome_package.subtract_private(3, 2)
}