-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy path3dcam.c
135 lines (104 loc) · 4.19 KB
/
3dcam.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Umka embedded scripting example (original raylib example by Ramon Santamaria)
#include <stdio.h>
#include "raylib.h"
#include "umka_api.h"
// Umka extension functions
void rlDrawPlane(UmkaStackSlot *params, UmkaStackSlot *result)
{
Vector3 *centerPos = (Vector3 *)umkaGetParam(params, 0);
Vector2 *size = (Vector2 *)umkaGetParam(params, 1);
Color *color = (Color *)umkaGetParam(params, 2);
DrawPlane(*centerPos, *size, *color);
}
void rlDrawCube(UmkaStackSlot *params, UmkaStackSlot *result)
{
Vector3 *position = (Vector3 *)umkaGetParam(params, 0);
float width = umkaGetParam(params, 1)->realVal;
float height = umkaGetParam(params, 2)->realVal;
float length = umkaGetParam(params, 3)->realVal;
Color *color = (Color *)umkaGetParam(params, 4);
DrawCube(*position, width, height, length, *color);
}
int main(void)
{
// Umka initialization
UmkaFuncContext umkaInitBodies = {0}, umkaDrawBodies = {0};
void *umka = umkaAlloc();
bool umkaOk = umkaInit(umka, "3dcam.um", NULL, 1024 * 1024, NULL, 0, NULL, false, false, NULL);
if (umkaOk)
{
umkaAddFunc(umka, "drawPlane", &rlDrawPlane);
umkaAddFunc(umka, "drawCube", &rlDrawCube);
umkaAddModule(umka, "rl.um",
"type Vector2* = struct {x, y: real32}\n"
"type Vector3* = struct {x, y, z: real32}\n"
"type Color* = struct {r, g, b, a: uint8}\n"
"fn drawPlane*(centerPos: Vector3, size: Vector2, color: Color)\n"
"fn drawCube*(position: Vector3, width, height, length: real, color: Color)\n"
);
umkaOk = umkaCompile(umka);
}
if (umkaOk)
{
printf("Umka initialized\n");
umkaGetFunc(umka, NULL, "initBodies", &umkaInitBodies);
umkaGetFunc(umka, NULL, "drawBodies", &umkaDrawBodies);
}
else
{
UmkaError *error = umkaGetError(umka);
printf("Umka error %s (%d, %d): %s\n", error->fileName, error->line, error->pos, error->msg);
}
// Raylib initialization
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");
// Define the camera to look into our 3D world (position, target, up vector)
Camera camera = { 0 };
camera.position = (Vector3){ 4.0f, 2.0f, 4.0f };
camera.target = (Vector3){ 0.0f, 1.8f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.fovy = 60.0f;
if (umkaOk)
umkaOk = umkaCall(umka, &umkaInitBodies) == 0;
if (!umkaOk)
{
UmkaError *error = umkaGetError(umka);
printf("Umka runtime error %s (%d): %s\n", error->fileName, error->line, error->msg);
}
SetTargetFPS(60); // Set our game to run at 60 frames per second
int exitCode = 0;
// Main game loop
if (umkaOk)
{
while (!WindowShouldClose()) // Detect window close button or ESC key
{
UpdateCamera(&camera, CAMERA_FIRST_PERSON);
BeginDrawing();
ClearBackground((Color){ 190, 190, 255, 255 });
BeginMode3D(camera);
exitCode = umkaCall(umka, &umkaDrawBodies);
if (!umkaAlive(umka))
{
if (exitCode != 0)
{
UmkaError *error = umkaGetError(umka);
printf("Umka runtime error %s (%d): %s\n", error->fileName, error->line, error->msg);
}
break;
}
EndMode3D();
DrawRectangle( 10, 10, 220, 70, Fade(SKYBLUE, 0.5f));
DrawRectangleLines( 10, 10, 220, 70, BLUE);
DrawText("First person camera default controls:", 20, 20, 10, BLACK);
DrawText("- Move with keys: W, A, S, D", 40, 40, 10, DARKGRAY);
DrawText("- Mouse move to look around", 40, 60, 10, DARKGRAY);
EndDrawing();
}
}
// Raylib de-initialization
CloseWindow(); // Close window and OpenGL context
// Umka de-initialization
umkaFree(umka);
return exitCode;
}