Skip to content

Instantly share code, notes, and snippets.

@PifyZ
Last active August 29, 2015 14:10
Show Gist options
  • Save PifyZ/10c788c89f86e884b5df to your computer and use it in GitHub Desktop.
Save PifyZ/10c788c89f86e884b5df to your computer and use it in GitHub Desktop.
LD#31
typedef Rgb = {
r: Float,
g: Float,
b: Float
}
typedef Rgba = { > Rgb,
a: Float
}
typedef Point3D = {
x: Float,
y: Float,
z: Float
}
class Hx3D {
public var window : Dynamic;
public function new(title : String, x : Int, y : Int, w : Int, h : Int) {
init();
// Création de la fenêtre
window = createWindow(title, x, y, w, h);
regColoredCube();
regColoredCuboid();
regColoredSurface();
setCameraPos({ x: 0, y: 0, z: 0 });
setCameraAngle({ x: -0.175 * 3.141592, y: 3.141592, z: 0 });
}
public function start() : Void {
startDrawing();
enterEventLoop();
destroyWindow();
}
public function init() : Void {
LibHang.lhInit();
}
// Manipulation de la fenêtre
// --------------------------
public function createWindow(title : String, x : Int, y : Int, w : Int, h : Int) : Dynamic {
return LibHang.lhCreateWindow(title, x, y, w, h, 0);
}
// Déstruction de la fenêtre après fermeture
public function destroyWindow() : Void {
LibHang.lhDestroyWindow(window);
}
// Manipulation des distances d'affichage
// ---------------------------------------
// - Distance minimale d'affichage
// - Distance maximale d'affichage
public function setRenderDistances(near : Float, far : Float) : Void {
LibHang.lhSetRenderDistances(window, { near: near, far: far });
}
// Manipulation de l'arrière-plan
// ------------------------------
// Arrière-plan
public function setBackColor(color : Rgba) : Void {
LibHang.lhSetBackColor(window, {
r: color.r,
g: color.g,
b: color.b,
a: color.a
});
}
// Manipulation de la lumière ambiante
// -----------------------------------
// Lumière ambiente (l'éclairement des objets en absence de toute source de lumière)
public function setAmbientLight(color : Rgb) : Void {
LibHang.lhSetAmbientLight(window, {
r: color.r,
g: color.g,
b: color.b
});
}
// Manipulation des threads
// ------------------------
public function createThread(callback : Dynamic -> Void, ?param : Dynamic = null) : Dynamic {
return LibHang.lhCreateThread(callback, param);
}
// Manipuler le brouillard
// -----------------------
public function setEnableFog(enable : Bool) : Void {
LibHang.lhEnableFog(window, enable);
}
public function setFogParameters(near : Float, far : Float, pos : Point3D, color : Rgb) : Void {
LibHang.lhSetFogParameters(window, {
near: near,
far: far
}, {
r: color.r,
g: color.g,
b: color.b
});
}
// Manipuler les lumières "étoiles"
// --------------------------------
public function enableLight(num : Int, ?type : Int = LibHang.LH_MSAA_NONE) : Void {
LibHang.lhEnableLight(window, num, type);
}
public function setLightParameters(num : Int, color : Rgb, pos : Point3D) : Void {
LibHang.lhSetLightParameters(
window,
0,
{
r: color.r,
g: color.g,
b: color.b
}, {
x: pos.x,
y: pos.y,
z: pos.z
});
}
// Manipuler le crénelage
// ----------------------
public function enableMSAA(nb_samples : Int) : Void {
LibHang.lhEnableMSAA(window, nb_samples);
}
// Gestion de la mémoire
// ---------------------
public function delay(delay : Int) : Void {
LibHang.lhDelay(delay);
}
public function dirtyObjBuffer() : Void {
LibHang.lhDirtyObjBuffer(window);
}
public function regColoredCube() : Void {
LibHang.lhRegColoredCube(window);
}
public function regColoredCuboid() : Void {
LibHang.lhRegColoredCuboid(window);
}
public function regColoredSurface() : Void {
LibHang.lhRegColoredSurface(window);
}
// Permet d'informer la librairie qu'elle peut commencer à dessiner
public function startDrawing() : Void {
LibHang.lhStartDrawing(window);
}
// Bloque l'exécution et capture les événement jusqu'à ce que la fenêtre soit fermée (les événements peuvent être capturés par une fonction utilisateur)
public function enterEventLoop() : Void {
LibHang.lhEnterEventLoop(window);
}
// Gestion des évènements
// ----------------------
public function addEventHandler(callback : Dynamic -> Dynamic -> Void, ?param : Dynamic = null) : Void {
LibHang.lhAddEventHandler(window, callback, param);
}
// Manipuler la caméra
// -------------------
public function getCameraPos() : Point3D {
return LibHang.lhGetCameraPos(window);
}
public function getCameraAngle() : Point3D {
return LibHang.lhGetCameraAngle(window);
}
public function setCameraPos(pos : Point3D) : Void {
LibHang.lhSetCameraPos(window, {
x: pos.x,
y: pos.y,
z: pos.z
});
}
public function setCameraAngle(pos : Point3D) : Void {
LibHang.lhSetCameraAngle(window, {
x: pos.x,
y: pos.y,
z: pos.z
});
}
// Ajouter des objets
// ------------------
public function newColoredCube(size : Float, pos : Point3D, angle : Point3D, color : Rgba, ?visible : Bool = true) : Dynamic {
return LibHang.lhNewColoredCube(window, size, pos, angle, color, visible);
}
public function newColoredCuboid(zoom : Float, sides : Point3D, pos : Point3D, angle : Point3D, color : Rgba, ?visible : Bool = true) : Dynamic {
return LibHang.lhNewColoredCuboid(window, zoom, sides, pos, angle, color, visible);
}
public function newColoredSurface(zoom : Float, a : Dynamic, b : Dynamic, c : Dynamic, d : Dynamic, e : Dynamic, color : Rgba, ?visible : Bool = true) : Dynamic {
return LibHang.lhNewColoredSurface(
window,
zoom,
a,
b,
c,
d,
e,
color,
visible);
}
// Manipuler les objets
// --------------------
public function getObjVisible(obj : Dynamic) : Bool {
return LibHang.lhObjIsVisible(window, obj);
}
public function setObjVisible(obj : Dynamic, visible : Bool) : Void {
LibHang.lhObjSetVisible(window, obj, visible);
}
public function getObjPos(obj : Dynamic) : Dynamic {
return LibHang.lhObjGetPos(window, obj);
}
public function setObjPos(obj : Dynamic, pos : Point3D) : Void {
LibHang.lhObjSetPos(window, obj, pos);
}
public function getObjAngle(obj : Dynamic) : Dynamic {
return LibHang.lhObjGetAngle(window, obj);
}
public function setObjAngle(obj : Dynamic, angle : Point3D) : Void {
LibHang.lhObjSetAngle(window, obj, angle);
}
}
import Hx3D.Point3D;
import cpp.vm.Thread;
class Main extends Hx3D {
public var keys : Map<Int, Bool>;
public var projectiles : Array<Dynamic>;
public var player : Dynamic;
public function new() {
super("LD#31", 0, 0, 640, 480);
keys = new Map<Int, Bool>();
projectiles = new Array<Dynamic>();
setRenderDistances(0.1, 100);
setBackColor({ r: 0, g: 0, b: 0.1, a: 1 });
setRenderDistances(0.1, 100);
setAmbientLight({ r: .5, g: .5, b: .5 });
setCameraPos({ x: 0, y: 2, z: 0 });
setLightParameters(
0,
{ r: 1, g: 1, b: 1 },
{ x: 0, y: 1, z: 1 });
/*
setLightParameters(
1,
{ r: 1, g: 1, b: 0 },
{ x: 1, y: 0, z: 1 });
*/
enableLight(0);
//enableLight(1);
// Cube rouge
newColoredCube(
1,
{ x: 0, y: 0, z: 4 },
{ x: 0, y: 0, z: 0 },
{ r: 1, g: 0, b: 0, a: 1 });
// Sol
newColoredCuboid(
1,
{ x: 6, y: 0.01, z: 3 },
{ x: 0, y: 0, z: 0 },
{ x: 0, y: 0, z: 0 },
{ r: 0, g: 0.6, b: 0, a: 1 });
player = newColoredCuboid(
1,
{ x: 0.2, y: 0.3, z: 0.2 },
{ x: 0, y: 0, z: 0 },
{ x: 0, y: 0, z: 0 },
{ r: 0, g: 0, b: 1, a: 1 });
//newColoredSurface(
// 1,
// 0,
// 0,
// { x: 1, y: 1, z: 1 },
// { x: 1, y: 1, z: 1 },
// { x: 0, y: 0, z: 0 },
// { r: 0, g: 1, b: 1, a: 1});
Thread.create(input);
addEventHandler(update);
start();
}
public function update(e : Dynamic, _) {
var camera = getCameraPos();
var angle = getCameraAngle();
var player_pos = getObjPos(player);
switch (e.type) {
case LibHang.LH_EVENT_KEY_PRESSED:
keys[e.key_id] = true;
case LibHang.LH_EVENT_KEY_RELEASED:
keys[e.key_id] = false;
case LibHang.LH_EVENT_MOUSE_PRESSED:
var cube = newColoredCube(
0.1,
{ x: player_pos.x, y: player_pos.y, z: player_pos.z },
{ x: angle.x, y: angle.y, z: angle.z },
{ r: 0, g: 0, b: 1, a: 1 }
);
projectiles.push(cube);
}
}
public function input() {
while (true) {
var camera = getCameraPos();
var angle = getCameraAngle();
if (keys.get(LibHang.LHK_q) || keys.get(LibHang.LHK_left)) {
setCameraAngle({
x: angle.x,
y: angle.y - 0.1,
z: angle.z
});
}
if (keys.get(LibHang.LHK_d) || keys.get(LibHang.LHK_right)) {
setCameraAngle({
x: angle.x,
y: angle.y + 0.1,
z: angle.z
});
}
if (keys.get(LibHang.LHK_z) || keys.get(LibHang.LHK_up)) {
setCameraPos({
x: camera.x - (Math.sin(angle.y - Math.PI) * 0.1),
y: camera.y,
z: camera.z + (Math.cos(angle.y - Math.PI) * 0.1)
});
}
if (keys.get(LibHang.LHK_s) || keys.get(LibHang.LHK_down)) {
setCameraPos({
x: camera.x + (Math.sin(angle.y - Math.PI) * 0.1),
y: camera.y,
z: camera.z - (Math.cos(angle.y - Math.PI) * 0.1)
});
}
camera = getCameraPos();
angle = getCameraAngle();
var unit_vector : Hx3D.Point3D = { x: 0, y: 0, z: 1 };
vec3_rotateX(unit_vector, angle.x);
vec3_rotateY(unit_vector, angle.y);
vec3_rotateZ(unit_vector, angle.z);
vec3_scale(unit_vector, -2);
setObjPos(player, {
x: camera.x + unit_vector.x,
y: camera.y + unit_vector.y,
z: camera.z + unit_vector.z
});
setObjAngle(player, {
x: 0,
y: angle.y,
z: 0
});
for (projectile in projectiles) {
var pos = getObjPos(projectile);
var angle = getObjAngle(projectile);
setObjPos(projectile, {
x: pos.x - (Math.sin(angle.y - Math.PI) * 0.6),
y: pos.y,
z: pos.z + (Math.cos(angle.y - Math.PI) * 0.6)
});
}
delay(16);
dirtyObjBuffer();
}
}
public static inline function vec3_rotateX(r : Point3D, a : Float) : Void {
var y : Float = r.y * Math.cos(a) - r.z * Math.sin(a);
r.z = r.y * Math.sin(a) + r.z * Math.cos(a);
r.y = y;
}
public static inline function vec3_rotateY(r : Point3D, a : Float) : Void {
var z : Float = r.x * Math.sin(a) + r.z * Math.cos(a);
r.x = r.x * Math.cos(a) - r.z * Math.sin(a);
r.z = z;
}
public static inline function vec3_rotateZ(r : Point3D, a : Float) : Void {
var x : Float = r.x * Math.cos(a) - r.y * Math.sin(a);
r.y = r.x * Math.sin(a) + r.y * Math.cos(a);
r.x = x;
}
public static inline function vec3_scale(r : Point3D, s : Float) : Void {
r.x = r.x * s;
r.y = r.y * s;
r.z = r.z * s;
}
public static function main() {
new Main();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment