-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.zig
129 lines (111 loc) · 3.93 KB
/
main.zig
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
//!zig-autodoc-section: BaseBox2D\\main.zig
//! main.zig :
//! Template for a console program that hide the console window.
// Build using Zig 0.13.0
const std = @import("std");
// NOTE ABOUT VSCODE + ZLS:
// Use full path for all cIncludes:
// @cInclude("C:/zig_workbench/BaseBox2D/lib/box2d/box2d.h");
const box = @cImport({
@cInclude("lib/box2d/box2d.h");
});
const Entity = struct {
bodyId: box.b2BodyId,
extent: box.b2Vec2,
};
const width: c_int = 1920;
const height: c_int = 1080;
const GROUND_COUNT: c_int = 14;
const BOX_COUNT: c_int = 10;
pub fn main() u8 {
//HideConsoleWindow();
const lengthUnitsPerMeter = 128.0;
box.b2SetLengthUnitsPerMeter(lengthUnitsPerMeter);
var worldDef = box.b2DefaultWorldDef();
worldDef.gravity.y = 9.8 * lengthUnitsPerMeter;
const worldId = box.b2CreateWorld(&worldDef);
const groundExtent = box.b2Vec2{ .x = 0.5 * 48, .y = 0.5 * 48 };
const boxExtent = box.b2Vec2{ .x = 0.5 * 48, .y = 0.5 * 48 };
const groundPolygon = box.b2MakeBox(groundExtent.x, groundExtent.y);
const boxPolygon = box.b2MakeBox(boxExtent.x, boxExtent.y);
var groundEntities: [GROUND_COUNT]Entity = undefined;
for (0..GROUND_COUNT) |i| {
var entity = &groundEntities[i];
var bodyDef = box.b2DefaultBodyDef();
bodyDef.position = box.b2Vec2{ .x = (2.0 * @as(f32, @floatFromInt(i)) + 2.0) * groundExtent.x, .y = height - groundExtent.y - 100.0 };
entity.bodyId = box.b2CreateBody(worldId, &bodyDef);
var shapeDef = box.b2DefaultShapeDef();
_ = box.b2CreatePolygonShape(entity.bodyId, &shapeDef, &groundPolygon);
}
var boxEntities: [BOX_COUNT]Entity = undefined;
var boxIndex: usize = 0;
for (0..4) |i| {
const y = height - groundExtent.y - 100.0 - (2.5 * @as(f32, @floatFromInt(i)) + 2.0) * boxExtent.y - 20.0;
for (i..4) |j| {
const x = 0.5 * @as(f32, @floatFromInt(width)) + (3.0 * @as(f32, @floatFromInt(j)) - @as(f32, @floatFromInt(i)) - 3.0) * boxExtent.x;
var entity = &boxEntities[boxIndex];
var bodyDef = box.b2DefaultBodyDef();
bodyDef.type = box.b2_dynamicBody;
bodyDef.position = box.b2Vec2{ .x = x, .y = y };
entity.bodyId = box.b2CreateBody(worldId, &bodyDef);
var shapeDef = box.b2DefaultShapeDef();
_ = box.b2CreatePolygonShape(entity.bodyId, &shapeDef, &boxPolygon);
boxIndex += 1;
}
}
var i: u8 = 0;
const deltaTime: f32 = 0.00016;
while (i < 100) {
box.b2World_Step(worldId, deltaTime, 4);
for (0..BOX_COUNT) |j| {
std.debug.print("ID:{d} - x:{d} y:{d}\n", .{
groundEntities[j].bodyId.index1,
groundEntities[j].extent.x,
groundEntities[j].extent.y });
}
std.time.sleep(@intFromFloat(16 * std.time.ns_per_ms));
i += 1;
}
return 0;
}
// ============================================================================
// Helpers
//
// _ = MessageBoxA(null, "Console window is hide.", "BaseBox2D", MB_OK);
const win = struct {
usingnamespace std.os.windows;
usingnamespace std.os.windows.kernel32;
};
fn HideConsoleWindow() void {
const BUF_TITLE = 1024;
var hwndFound: win.HWND = undefined;
var pszWindowTitle: [BUF_TITLE:0]win.CHAR = std.mem.zeroes([BUF_TITLE:0]win.CHAR);
_ = GetConsoleTitleA(&pszWindowTitle, BUF_TITLE);
hwndFound=FindWindowA(null, &pszWindowTitle);
_ = ShowWindow(hwndFound, SW_HIDE);
}
pub extern "kernel32" fn GetConsoleTitleA(
lpConsoleTitle: win.LPSTR,
nSize: win.DWORD,
) callconv(win.WINAPI) win.DWORD;
pub extern "kernel32" fn FindWindowA(
lpClassName: ?win.LPSTR,
lpWindowName: ?win.LPSTR,
) callconv(win.WINAPI) win.HWND;
pub const SW_HIDE = 0;
pub extern "user32" fn ShowWindow(
hWnd: win.HWND,
nCmdShow: i32
) callconv(win.WINAPI) win.BOOL;
pub const MB_OK = 0x00000000;
pub extern "user32" fn MessageBoxA(
hWnd: ?win.HWND,
lpText: [*:0]const u8,
lpCaption: [*:0]const u8,
uType: win.UINT
) callconv(win.WINAPI) win.INT;
// ============================================================================
// Tests
//
test " " {
}