Skip to content

Commit

Permalink
A whole lot of progress. Also add neccesary DLLs and fix chipmunA whole
Browse files Browse the repository at this point in the history
lot of progress. Also add neccesary DLLs and fix chipmunkk
  • Loading branch information
tatjam committed Jul 8, 2022
1 parent 1113312 commit 08d4d87
Show file tree
Hide file tree
Showing 37 changed files with 2,419 additions and 138 deletions.
Binary file added chipmunk.dll
Binary file not shown.
Binary file added libsoloud.dll
Binary file not shown.
4 changes: 2 additions & 2 deletions minijam.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ license = "MIT"

requires "nim >= 1.6.4"
requires "nimgl"
requires "https://github.com/oprypin/nim-chipmunk"
requires "stb_image"
requires "parasound"
requires "glm"
requires "https://github.com/zacharycarter/soloud-nim"
requires "yaml"

# Executable
bin = @["src/main"]
Binary file added res/intro/fore_act.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/intro/fore_deact.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/intro/intro.mp3
Binary file not shown.
Binary file added res/intro/island.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/intro/monk.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/intro/sea_f1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/intro/sea_f2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/intro/sky.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/intro/stars.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/intro/sun.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/level1/ground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/level1/map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions res/level1/map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
seed: 1234
tiles:
- class: ground
texture: res/level1/ground.png
edge: [99, 78, 35]
edge_width: 4
noise: 1.0
color: [101, 101, 0]

- class: ground
texture: res/level1/wood.png
edge: [89, 50, 21]
edge_width: 4
noise: 0.0
color: [101, 101, 101]

areas:
- name: water
color: [0, 0, 255]

points:
- name: tree1
color: [200, 0, 255]

- name: tree2
color: [255, 0, 255]

- name: log_spawn
color: [0, 255, 150]

- name: box_spawn
color: [0, 255, 200]

- name: player_spawn
color: [255, 0, -1]
Binary file added res/level1/music.mp3
Binary file not shown.
Binary file added res/level1/tree1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/level1/tree2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/level1/wood.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion res/shader/fullscreen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
out vec4 FragColor;

in vec2 vTex;
in vec2 fTex;

uniform sampler2D tex;

void main()
{
vec2 coord = vTex;

coord = vec2(vTex.x, 1.0 - vTex.y);
coord = vec2(vTex.x, vTex.y);

vec4 col = texture(tex, coord);

Expand Down
8 changes: 4 additions & 4 deletions res/shader/sprite.fs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#version 330
layout(location = 0) out vec3 color;
layout(location = 0) out vec4 color;
layout(location = 1) out vec3 effect;

in vec2 vTex;

uniform sampler2D tex;
uniform vec4 tint;

void main()
{
vec2 coord = vTex;

coord = vec2(vTex.x, 1.0 - vTex.y);

vec4 col = texture(tex, coord);

color = col.xyz;
color = texture(tex, coord) * tint;
}
3 changes: 2 additions & 1 deletion res/shader/sprite.vs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ layout (location = 1) in vec2 aTex;
out vec2 vTex;

uniform mat4 tform;
uniform mat4 sprite_tform;

void main()
{
vTex = aTex;
gl_Position = tform * vec4(aPos.x, aPos.y, aPos.z, 1.0);
gl_Position = tform * sprite_tform * vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
35 changes: 35 additions & 0 deletions src/engine/audio/audio_engine.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Very simple wrapper for the soloud engine so its more nim-like

import system/io
import solouddotnim

var soloud : ptr Soloud

type AudioHandle* = distinct int
type WavHandle* = object
wave: ptr Wav

proc `=destroy`(x: var WavHandle) =
if x.wave != nil:
echo "Destroying sound " & repr x.wave
Wav_destroy(x.wave)



proc init_soloud*() =
soloud = Soloud_create()
discard Soloud_init(soloud)

proc deinit_soloud*() =
Soloud_deinit(soloud)
Soloud_destroy(soloud)

proc load_sound*(path: cstring): WavHandle =
var wave = Wav_create()
discard Wav_load(wave, path)
return WavHandle(wave: wave)

proc play_sound*(sound: WavHandle, loop: bool = false): AudioHandle =
let handle = Soloud_play(soloud, sound.wave)
Soloud_setLooping(soloud, handle, loop.int32)
return cast[AudioHandle](handle)
74 changes: 11 additions & 63 deletions src/engine/base.nim
Original file line number Diff line number Diff line change
@@ -1,63 +1,11 @@
import nimgl/[glfw, opengl]
import os
include base/renderer
include base/scene_manager

var should_quit* = false
var renderer*: Renderer = nil
var dt*: float32

var update_fnc*: proc() = nil
var render_fnc*: proc() = nil
var quit_fnc*: proc() = nil

assert glfwInit()

glfwWindowHint(GLFWContextVersionMajor, 3)
glfwWindowHint(GLFWContextVersionMinor, 3)
glfwWindowHint(GLFWOpenglForwardCompat, GLFW_TRUE) # Used for Mac
glfwWindowHint(GLFWOpenglProfile, GLFW_OPENGL_CORE_PROFILE)
glfwWindowHint(GLFWResizable, GLFW_FALSE)

let glfw_window*: GLFWWindow = glfwCreateWindow(800, 600, "Minijam 110")
if glfw_window == nil:
quit(-1)

glfw_window.makeContextCurrent()

# Active V-sync, useful to avoid FPS being in the thousands!
glfwSwapInterval(1)

assert glInit()

renderer = create_renderer("res/shader/fullscreen", 800, 600)

proc launch_game*() =
assert scene_stack.len > 0, "Remember to add a scene before starting the game"

var last_time = glfwGetTime()

# Launch the main loop
while not should_quit:
glfwPollEvents()

update_fnc()
scene_manager_update()

glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT)

renderer.before_render()
if render_fnc != nil: render_fnc()
scene_manager_render(renderer)
renderer.render()

glfw_window.swapBuffers()
var new_time = glfwGetTime()
dt = new_time - last_time
last_time = new_time

if quit_fnc != nil: quit_fnc()

glfw_window.destroyWindow()
glfwTerminate()
# include (not import) this file to have the base classes

import launcher
import base/scene_manager
import graphics/sprite
import graphics/camera
import globals
import base/renderer as rnd
import audio/audio_engine
import solouddotnim
import glm
47 changes: 32 additions & 15 deletions src/engine/base/renderer.nim
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# Do not "import" as it will cause issues with the global "renderer"!
# (it's already included by base)

# Implements generic, layered drawing for drawables, binding
# only the most basic uniforms in the shaders:
# tform: camera + object transformation
# It renders to a framebuffer to allow fullscreen effects
# It renders to a framebuffer to allow fullscreen effects,
# and also has a effect texture, used for lighting in this case
# Layering is achieved by the use of the depth buffer!
# Drawables must implement:
# -> .position (returns Vec2f, position of the drawable), (optional if using draw_at)
# -> .shader (returns a shader to be used for the draw)
# -> .do_draw (calls the appropiate OpenGL for actually drawing the object, including additional shader stuff)
# -> .layer (returns int, the layer of the object)
# Keep layers below LAYER_CAP, as its used for scene overlaying
# Note that layering doesn't work with alpha blending! You must manually implement sorting if needed

import ../graphics/camera
import ../graphics/shader
Expand All @@ -24,13 +23,14 @@ const LAYER_CAP = 100000
type Renderer* = ref object
camera*: Camera
fullscreen_shader*: Shader
fbuffer, rnd_texture, depth_buffer: GLuint
fbuffer, rnd_texture, light_texture, depth_buffer: GLuint
# All drawable calls while layer_bias is enabled get increased by LAYER_CAP
layer_bias*: bool
width*, height*: int
# global scale factor
scale*: int

proc resize(renderer: var Renderer, width: int, height: int) =
glViewport(0, 0, width.GLsizei, height.GLsizei)

renderer.width = width
renderer.height = height
Expand All @@ -46,6 +46,14 @@ proc resize(renderer: var Renderer, width: int, height: int) =

glGenTextures(1, addr renderer.rnd_texture)
glBindTexture(GL_TEXTURE_2D, renderer.rnd_texture)

glTexImage2D(GL_TEXTURE_2D, 0'i32, GL_RGBA.GLint, width.GLsizei, height.GLsizei, 0.GLint, GL_RGBA, GL_UNSIGNED_BYTE, nil)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST.GLint)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST.GLint)

glGenTextures(1, addr renderer.light_texture)
glBindTexture(GL_TEXTURE_2D, renderer.light_texture)

glTexImage2D(GL_TEXTURE_2D, 0'i32, GL_RGB.GLint, width.GLsizei, height.GLsizei, 0.GLint, GL_RGB, GL_UNSIGNED_BYTE, nil)

Expand All @@ -58,33 +66,42 @@ proc resize(renderer: var Renderer, width: int, height: int) =
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderer.depth_buffer)

glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderer.rnd_texture, 0)
var dbuffers = [GL_COLOR_ATTACHMENT_0]
glDrawBuffers(1, addr dbuffers[0])
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, renderer.light_texture, 0)
var dbuffers = [GL_COLOR_ATTACHMENT_0, GL_COLOR_ATTACHMENT1]
glDrawBuffers(2, addr dbuffers[0])



proc create_renderer*(shader_path: string, width: int, height: int): Renderer =
proc create_renderer*(shader_path: string, width: int, height: int, scale: int): Renderer =
let shader = load_shader(shader_path)
let camera = create_camera()
var rnd = Renderer(camera: camera, fullscreen_shader: shader)
rnd.resize(width, height)
var rnd = Renderer(camera: camera, fullscreen_shader: shader, scale: scale)
rnd.resize(toInt(width / scale), toInt(height / scale))
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
return rnd

# Render stuff to the framebuffer after this call
proc before_render*(renderer: Renderer) =
glViewport(0, 0, renderer.width.GLsizei, renderer.height.GLsizei)
glBindFramebuffer(GL_FRAMEBUFFER, renderer.fbuffer)

# Stop rendering stuff to the framebuffer after this call
proc render*(renderer: Renderer) =

glViewport(0, 0, (renderer.width * renderer.scale).GLsizei, (renderer.height * renderer.scale).GLsizei)
# Draw the fullscreen rectangle, binding texture 0
glBindFramebuffer(GL_FRAMEBUFFER, 0);
renderer.fullscreen_shader.use()
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, renderer.rnd_texture)
renderer.fullscreen_shader.set_int("tex", 0)
renderer.fullscreen_shader.set_int("vTex", 0)
glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, renderer.light_texture)
renderer.fullscreen_shader.set_int("fTex", 1)

var tform = translate(mat4f(), -1.0, -1.0, 0.0).scale(2.0, 2.0, 2.0)
var tform = translate(mat4f(), -1.0, -1.0, 0.0)
.scale(vec3f(2.0))
renderer.fullscreen_shader.set_mat4("tform", tform)
draw_rectangle()

Expand All @@ -96,7 +113,7 @@ proc draw*[T](renderer: Renderer, drawable: T) =
assert layer < LAYER_CAP
if renderer.layer_bias:
layer += LAYER_CAP
var tform = renderer.camera.get_transform_matrix()
var tform = renderer.camera.get_transform_matrix(renderer.width, renderer.height)
tform = tform.translate(pos.x, pos.y, layer.toFloat())

drawable.shader.set_mat4("tform", tform)
Expand All @@ -109,7 +126,7 @@ proc draw_at*[T](renderer: Renderer, drawable: T, pos: Vec2f) =
assert layer < LAYER_CAP
if renderer.layer_bias:
layer += LAYER_CAP
var tform = renderer.camera.get_transform_matrix()
var tform = renderer.camera.get_transform_matrix(renderer.width, renderer.height)
tform = tform.translate(pos.x, pos.y, layer.toFloat())

drawable.shader.set_mat4("tform", tform)
Expand Down
Loading

0 comments on commit 08d4d87

Please sign in to comment.