Sfoglia il codice sorgente

Add 02.sprites sample

Daniele Bartolini 12 anni fa
parent
commit
dce68eb727

+ 5 - 0
samples/02.sprites/crown.config

@@ -0,0 +1,5 @@
+{
+	"boot" : "lua/game",
+	"window_width" : 1000,
+	"window_height" : 625
+}

+ 19 - 0
samples/02.sprites/level.package

@@ -0,0 +1,19 @@
+{
+	"unit" : [
+		"units/camera",
+		"units/star",
+		"units/circle",
+		"units/pentagon",
+		"units/square"
+	],
+	"texture" : [
+		"textures/shapes"
+	],
+	"sprite" :
+	[
+		"sprites/star",
+		"sprites/circle",
+		"sprites/pentagon",
+		"sprites/square"
+	]
+}

+ 72 - 0
samples/02.sprites/lua/game.lua

@@ -0,0 +1,72 @@
+objects = {}
+max_objects = 70
+
+function init()
+	-- Set the title of the main window
+	Window.set_title("Hello world!")
+
+	-- Create the resource package
+	package = Device.create_resource_package("level")
+	-- Load resource package
+	ResourcePackage.load(package)
+	-- Wait for completion
+	ResourcePackage.flush(package)
+
+	-- Create world
+	world = Device.create_world()
+
+	-- Spawn camera
+	camera_unit = World.spawn_unit(world, "units/camera")
+	camera = Unit.camera(camera_unit, "camera")
+
+	-- Setup camera
+	Camera.set_near_clip_distance(camera, 0.01)
+	Camera.set_far_clip_distance(camera, 1000)
+	Camera.set_projection_type(camera, Camera.ORTHOGRAPHIC)
+	Camera.set_orthographic_metrics(camera, -6, 6, -6 / 1.6, 6 / 1.6)
+	Camera.set_viewport_metrics(camera, 0, 0, 1000, 625)
+	Unit.set_local_position(camera_unit, Vector3(0, 0, 1))
+
+	local unit_names = { "units/star", "units/circle", "units/pentagon", "units/square" }
+	-- Spawn units randomly
+	math.randomseed(os.time())
+	for i = 1, max_objects do
+		objects[i] = {
+						unit = World.spawn_unit(world, unit_names[math.random(#unit_names)],
+									Vector3(math.random(-6, 6), math.random(-6 / 1.6, 6 / 1.6), 0)),
+						rot_speed = math.random(1.5),
+						cur_rot = math.random(3.14)
+					}
+	end
+end
+
+function frame(dt)
+	-- Stop the engine when the 'ESC' key is released
+	if Keyboard.button_released(Keyboard.ESCAPE) then
+		Device.stop()
+	end
+
+	for i = 1, max_objects do
+		local obj = objects[i]
+		local r = obj.rot_speed
+		obj.cur_rot = obj.cur_rot + r * dt
+
+		Unit.set_local_rotation(obj.unit, Quaternion(Vector3(0, 0, 1), obj.cur_rot))
+	end
+
+	Device.render_world(world, camera, dt)
+end
+
+function shutdown()
+	-- Destroy all units
+	for i = 1, max_objects do
+		World.destroy_unit(world, objects[i].unit)
+	end
+	World.destroy_unit(world, camera_unit)
+
+	-- Destroy world
+	Device.destroy_world(world)
+	-- Unload package
+	ResourcePackage.unload(package)
+	Device.destroy_resource_package(package)
+end

+ 21 - 0
samples/02.sprites/sprites/circle.sprite

@@ -0,0 +1,21 @@
+{
+	"name" : "circle",
+	"texture" : "textures/shapes",
+	"num_frames" : 1,
+	"frame_rate" : 1,
+	"playback_mode" : 2,
+	"positions" :
+	[
+		-0.5, -0.5,
+		 0.5, -0.5,
+		 0.5,  0.5,
+		-0.5,  0.5
+	],
+	"texcoords" : 
+	[
+		0.5, 0.5,
+		1.0, 0.5,
+		1.0, 1.0,
+		0.5, 1.0
+	]
+}

+ 21 - 0
samples/02.sprites/sprites/pentagon.sprite

@@ -0,0 +1,21 @@
+{
+	"name" : "pentagon",
+	"texture" : "textures/shapes",
+	"num_frames" : 1,
+	"frame_rate" : 1,
+	"playback_mode" : 2,
+	"positions" :
+	[
+		-0.5, -0.5,
+		 0.5, -0.5,
+		 0.5,  0.5,
+		-0.5,  0.5
+	],
+	"texcoords" : 
+	[
+		0.0, 0.0,
+		0.5, 0.0,
+		0.5, 0.5,
+		0.0, 0.5
+	]
+}

+ 21 - 0
samples/02.sprites/sprites/square.sprite

@@ -0,0 +1,21 @@
+{
+	"name" : "square",
+	"texture" : "textures/shapes",
+	"num_frames" : 1,
+	"frame_rate" : 1,
+	"playback_mode" : 2,
+	"positions" :
+	[
+		-0.5, -0.5,
+		 0.5, -0.5,
+		 0.5,  0.5,
+		-0.5,  0.5
+	],
+	"texcoords" : 
+	[
+		0.5, 0.0,
+		1.0, 0.0,
+		1.0, 0.5,
+		0.5, 0.5
+	]
+}

+ 21 - 0
samples/02.sprites/sprites/star.sprite

@@ -0,0 +1,21 @@
+{
+	"name" : "star",
+	"texture" : "textures/shapes",
+	"num_frames" : 1,
+	"frame_rate" : 1,
+	"playback_mode" : 2,
+	"positions" :
+	[
+		-0.5, -0.5,
+		 0.5, -0.5,
+		 0.5,  0.5,
+		-0.5,  0.5
+	],
+	"texcoords" : 
+	[
+		0.0, 0.5,
+		0.5, 0.5,
+		0.5, 1.0,
+		0.0, 1.0
+	]
+}

BIN
samples/02.sprites/textures/shapes.texture


+ 7 - 0
samples/02.sprites/units/camera.unit

@@ -0,0 +1,7 @@
+{
+	"camera" : [
+		{
+			"name" : "camera"
+		}
+	]
+}

+ 10 - 0
samples/02.sprites/units/circle.unit

@@ -0,0 +1,10 @@
+{
+	"renderable" : [
+		{
+			"type" : "sprite",
+			"resource" : "sprites/circle",
+			"name" : "circle",
+			"visible" : true
+		}
+	]
+}

+ 10 - 0
samples/02.sprites/units/pentagon.unit

@@ -0,0 +1,10 @@
+{
+	"renderable" : [
+		{
+			"type" : "sprite",
+			"resource" : "sprites/pentagon",
+			"name" : "pentagon",
+			"visible" : true
+		}
+	]
+}

+ 10 - 0
samples/02.sprites/units/square.unit

@@ -0,0 +1,10 @@
+{
+	"renderable" : [
+		{
+			"type" : "sprite",
+			"resource" : "sprites/square",
+			"name" : "square",
+			"visible" : true
+		}
+	]
+}

+ 10 - 0
samples/02.sprites/units/star.unit

@@ -0,0 +1,10 @@
+{
+	"renderable" : [
+		{
+			"type" : "sprite",
+			"resource" : "sprites/star",
+			"name" : "star",
+			"visible" : true
+		}
+	]
+}

+ 1 - 1
samples/CMakeLists.txt

@@ -47,4 +47,4 @@ link_directories(${CROWN_BINARY_DIR} ${CROWN_THIRD_LIBS})
 
 # Install samples
 install (DIRECTORY 01.hello-world DESTINATION samples)
-
+install (DIRECTORY 02.sprites DESTINATION samples)