zacbrannelly / SwinEngine

Basic 2D Game Engine with Lua scripting

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SwinEngine

Basic 2D Game Engine with minimal scripting.

Technologies used

  • SDL - Display creation
  • OpenGL - Rendering
  • Lua - Scripting
  • JSON - Map/World representation

Built for an assignment where I wrote a tutorial on creating a 2D Game Engine in C++.

The tutorial can be seen here: How to create a 2D Game Engine in C++ using OpenGL, SDL, Lua, and JSON.

Example

Example main.lua code:

function main.SetupWindow(window)
	window.Title = "Test Game"
	window.Width = 800
	window.Height = 600
	window.Resizable = false
end

function main.LoadMaps(mapLoader, stateManager)
	-- Load the test map
	if mapLoader:IsValid("test_map.json") then
		local testMap = mapLoader:LoadMap("test_map.json")

		-- Register the map to a state and set that as the current state
		stateManager:RegisterMap("main", testMap)
		stateManager.State = "main"
	end
	
end

Example map/world (test_map.json):

{
	"name": "test_map",
	"backgroundColor": [ 0.0, 0.0, 0.0 , 1.0 ],
	"scriptPath": "test_map_script.lua",
	"scriptName": "test_map",
	"objects": [
		{
			"name": "test rectangle",
			"position": [0, 0],
			"type": "rectangle",
			"width": 100,
			"height": 100,
			"color": [1, 0, 0, 1]
		},
		{
			"name": "test circle",
			"position": [0, 0],
			"type": "circle",
			"radius": 50,
			"color": [1, 1, 1, 1]
		}
	]
}

Example map/world script code:

function test_map.Initialize(map)
	local rectangle = map:GetObject("test rectangle")
	
	rectangle.Position = vec2(400 - 25, 300 - 25)
	rectangle.Origin = vec2(50, 50)
	rectangle.AngularVelocity = 0.05	

	test_map.counter = 0
end

function test_map.Update(map)
	
	local rectangle = map:GetObject("test rectangle")
		
	test_map.counter = test_map.counter + 0.05
	
	if Keyboard.IsKeyDown("left") then
		rectangle.Velocity = vec2(-5, 0)
	elseif Keyboard.IsKeyDown("right") then
		rectangle.Velocity = vec2(5, 0)
	end
	
	if rectangle.Position.x + rectangle.Velocity.x <= 0 then
		rectangle.Velocity = vec2(5, 0)
	elseif rectangle.Position.x + rectangle.Width + rectangle.Velocity.x >= 800 then
		rectangle.Velocity = vec2(-5, 0)
	end
end

function test_map.Render(renderingContext, interpolation)

end

Result:

This example code results in a stationary circle and a rotating rectangle that bounces off the walls or changes horizontal direction when the left/right keys are pressed.

About

Basic 2D Game Engine with Lua scripting


Languages

Language:C++ 97.6%Language:Lua 1.6%Language:GLSL 0.8%