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

Collision callbacks #75

Merged
merged 5 commits into from
Oct 6, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
nimcache
testresults
*.exe
.modules
85 changes: 85 additions & 0 deletions config.nims
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import os
import strformat

# NOTE: Must copy or symlink dependencies into ./.modules
const deps = [
"safeseq",
"nimtest",
"sdl2_nim",
"seq2d",
"zippy"
]

for dep in deps:
switch("path", fmt"./.modules/{dep}")
switch("path", fmt"./.modules/{dep}/src")

switch("gc", "orc")
switch("multimethods", "on")
Expand Down Expand Up @@ -26,3 +40,74 @@ when defined(linux):
putEnv("LD_LIBRARY_PATH", libPath)
putEnv("LIBRARY_PATH", libPath)

task create_deps_artifact, "Compresses contents of .usr dir needed for development":
exec "nim r -d:release src/shade.nim --compress"

task fetch_deps, "Fetches dependencies and extracts them to .usr/lib":
exec "nim r -d:release -d:ssl src/shade.nim --fetch"

task extract_deps, "Extracts local dependencies (deps_artifact.tar.gz) to .usr/lib":
exec "nim r -d:release -d:ssl src/shade.nim --extract"

# Tasks
task build_deps, "Builds submodule dependencies":
exec "git submodule update --init"
when defined(linux):
let localUsrPath = joinPath(thisDir(), ".usr")
withDir "submodules/sdl":
exec fmt"./configure --prefix={localUsrPath} --enable-hidapi-libusb"
exec "make -j install"

withDir "submodules/sdl-gpu":
mkDir "build"
withDir "build":
exec fmt"cmake -B . -S .. -G 'Unix Makefiles' -DCMAKE_INSTALL_PREFIX={localUsrPath}"
exec "make -j install"

withDir "submodules/sdl_ttf":
exec fmt"./configure --prefix={localUsrPath}"
exec "make -j install"

withDir "submodules/sdl_mixer":
mkDir "build"
withDir "build":
exec fmt"../configure --prefix={localUsrPath}"
exec "make -j"
exec "make install"

withDir fmt"{localUsrPath}/lib":
exec "rm -r *.a *.la cmake pkgconfig"

task shaders, "Runs the shader example":
exec "nim r -d:release examples/shaders/water_shader.nim"

task post_shader, "Runs the post-processing shader example":
exec "nim r -d:release examples/shaders/postprocessing.nim"

task animations, "Runs the animation player example":
exec "nim r examples/basic/animationplayer_example.nim"

task physics, "Runs the physics example":
exec "nim r -d:release examples/physics/physics_example.nim"

task physicsd, "Runs the physics example in debug mode":
exec "nim r -d:debug -d:collisionoutlines -d:spatialgrid examples/physics/physics_example.nim"

task platformer, "Runs the platformer example":
exec "nim r -d:release examples/platformer/platformer_example.nim"

task platformerd, "Runs the platformer example in debug mode":
exec "nim r -d:debug -d:collisionoutlines examples/platformer/platformer_example.nim"

task particles, "Runs the particles example":
exec "nim r -d:release examples/particles/particles_example.nim"

task particlesd, "Runs the particles example in debug mode":
exec "nim r -d:debug examples/particles/particles_example.nim"

task textbox, "Runs the textbox example":
exec "nim r -d:release examples/textbox/textbox_example.nim"

task runtests, "Runs all tests":
exec "nimtest"

Binary file added examples/assets/images/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 4 additions & 7 deletions examples/shaders/common.vert
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#version 400
attribute vec3 gpu_Vertex;
attribute vec2 gpu_TexCoord;
attribute vec4 gpu_Color;
uniform mat4 gpu_ModelViewProjectionMatrix;

varying vec4 color;
varying vec2 texCoord;

void main(void)
{
color = gpu_Color;
texCoord = vec2(gpu_TexCoord);
gl_Position = gpu_ModelViewProjectionMatrix * vec4(gpu_Vertex, 1.0);
void main(void) {
texCoord = gpu_TexCoord;
gl_Position = gpu_ModelViewProjectionMatrix * vec4(gpu_Vertex, 1.0);
}

6 changes: 2 additions & 4 deletions examples/shaders/rectangle.frag
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#version 330
#version 400

varying vec2 vertex;
varying vec2 texCoord;
varying vec4 color;

uniform sampler2D tex;
uniform float time;
uniform vec2 resolution;

// See the "gl_FragCoord" section of https://thebookofshaders.com/03/
void main(void) {
vec2 st = gl_FragCoord.xy / resolution;
gl_FragColor = vec4(st.x, st.y, 0.0, 1.0);
gl_FragColor = vec4(texCoord, 0.5, 1.0);
}

35 changes: 19 additions & 16 deletions examples/shaders/rectangle_shader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,31 @@ const
fragShaderPath = "./examples/shaders/rectangle.frag"
vertShaderPath = "./examples/shaders/common.vert"

let shaderProgram = newShader(vertShaderPath, fragShaderPath)
let (_, image) = Images.loadImage("./examples/assets/images/default.png")
setImageFilter(image, FILTER_NEAREST)

type Background = ref object of Node
let shaderProgram = newShader(vertShaderPath, fragShaderPath)

Background.renderAsEntityChild:
ctx.rectangleFilled(
0,
0,
gamestate.resolution.x,
gamestate.resolution.y,
WHITE
type Rectangle = ref object of Entity
image: Image

Rectangle.renderAsEntityChild:
blitScale(
image,
nil,
ctx,
gamestate.resolution.x / 2,
gamestate.resolution.y / 2,
16,
16,
)

let bg = Background(shader: shaderProgram)
initNode(Node bg, RENDER)
let bg = Rectangle(shader: shaderProgram)
initNode(Entity bg, RENDER)
layer.addChild(bg)

Input.addKeyPressedListener(
K_ESCAPE,
proc(key: Keycode, state: KeyState) =
Game.stop()
)
Input.onKeyEvent(K_ESCAPE):
Game.stop()

Game.start()

4 changes: 2 additions & 2 deletions examples/shaders/water.frag
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

varying vec2 vertex;
varying vec2 texCoord;
varying vec4 color;

uniform sampler2D tex;
uniform float time;
Expand Down Expand Up @@ -106,7 +105,8 @@ void main(void) {
}

float t = time * 0.5;
vec2 coord = gl_FragCoord.xy * 0.015 - vec2(t * 0.5, resolution.y / 2.0);
ivec2 texSize = textureSize(tex, 0);
vec2 coord = gl_FragCoord.xy * 0.015 - vec2(t * 0.5, texSize.y / 2.0);
float speed = 0.3 * SPEED;
float limit = 0.1;
float border = 0.025;
Expand Down
26 changes: 15 additions & 11 deletions examples/shaders/water_shader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@ const
fragShaderPath = "./examples/shaders/water.frag"
vertShaderPath = "./examples/shaders/common.vert"

let shaderProgram = newShader(vertShaderPath, fragShaderPath)
let (_, image) = Images.loadImage("./examples/assets/images/default.png")

type Background = ref object of Entity
let shaderProgram = newShader(vertShaderPath, fragShaderPath)

Background.renderAsEntityChild:
ctx.rectangleFilled(
0,
0,
gamestate.resolution.x,
gamestate.resolution.y,
WHITE
type Rectangle = ref object of Node

Rectangle.renderAsNodeChild:
blitScale(
image,
nil,
ctx,
gamestate.resolution.x / 2,
gamestate.resolution.y / 2,
16,
16,
)

let bg = Background(shader: shaderProgram)
initEntity(Entity bg, RENDER)
let bg = Rectangle(shader: shaderProgram)
initNode(Node bg, RENDER)
layer.addChild(bg)

Input.onKeyPressed(K_ESCAPE):
Expand Down
93 changes: 0 additions & 93 deletions shade.nimble

This file was deleted.

2 changes: 1 addition & 1 deletion src/shadepkg/game/animation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ macro addNewAnimationTrack*[T: TrackType](
field: T,
frames: openArray[Keyframe[T]],
wrapInterpolation: bool = false,
ease: EasingFunction[T] = lerp
ease: EasingFunction[T] = mathutils.lerp
) =
## Adds a new "track" to the animation.
## This is a value that's updated at set intervals as the animation is updated.
Expand Down
11 changes: 4 additions & 7 deletions src/shadepkg/game/camera.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export entity, aabb, vector2, mathutils
type
Camera* = ref object of Entity
z*: float
bounds: AABB
bounds*: AABB
viewport*: AABB

# For entity tracking
Expand Down Expand Up @@ -83,8 +83,9 @@ proc bounds*(this: Camera): AABB =
template confineToBounds(this: Camera) =
if this.bounds != AABB_ZERO:
let
halfViewportWidth = this.viewport.width * 0.5
halfViewportHeight = this.viewport.height * 0.5
distToPlane = 1.0 - this.z
halfViewportWidth = this.viewport.width * 0.5 * distToPlane
halfViewportHeight = this.viewport.height * 0.5 * distToPlane

this.x = clamp(
this.bounds.left + halfViewportWidth,
Expand All @@ -111,10 +112,6 @@ proc screenToWorldCoord*(this: Camera, screenPoint: Vector, relativeZ: float = 1
template screenToWorldCoord*(this: Camera, x, y: float|int, relativeZ: float = 1.0): Vector =
this.screenToWorldCoord(vector(x, y), relativeZ)

method setLocation*(this: Camera, x, y: float) =
procCall Entity(this).setLocation(x, y)
this.updateViewport()

method update*(this: Camera, deltaTime: float) =
procCall Entity(this).update(deltaTime)

Expand Down
6 changes: 0 additions & 6 deletions src/shadepkg/game/entity.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import
node,
gamestate,
../render/render,
../render/shader,
../math/vector2

export
Expand All @@ -17,7 +16,6 @@ export

type
Entity* = ref object of Node
shader*: Shader
location: Vector
# Rotation in degrees (clockwise).
rotation*: float
Expand Down Expand Up @@ -63,7 +61,3 @@ method move*(this: Entity, v: Vector) {.base.} =
method hash*(this: Entity): Hash {.base.} =
return hash(this[].unsafeAddr)

Entity.renderAsChildOf(Node):
if this.shader != nil:
this.shader.render(gamestate.runTime, gamestate.resolution)

Loading
Loading