-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
116 lines (91 loc) · 2.6 KB
/
example.cpp
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
#include <iostream>
#include "raylib.h"
#define HLAM_MATH_IMPLEMENTATION
#include "hlam_math.h"
#define HLAM_COLLISIONS2D_IMPLEMENTATION
#include "collisions2D.h"
#define HLAM_VIEWPORT_IMPLEMENTATION
#include "viewport.h"
hlam::Rect r1{100, 20, 30, 30};
hlam::Rect r2{120, 40, 30, 30};
hlam::Vec2 line[2]{{150, 50}, {200, 100}};
hlam::Vec2 line2[2]{{150, 100}, {200, 180}};
hlam::Circle circle{{100, 100}, 30};
hlam::Triangle2 triangle{{130, 150}, {100, 150}, {110, 180}};
#define WORLD_WIDTH 400
#define WORLD_HEIGHT 240
static RenderTexture2D canvas;
static hlam::Viewport viewport = {WORLD_WIDTH, WORLD_HEIGHT, Vector2{0, 0}};
void CollisionDemo() {
hlam::Vec2 ds{};
if (IsKeyDown(KEY_UP)) {
ds.y = -1;
}
if (IsKeyDown(KEY_DOWN)) {
ds.y = 1;
}
if (IsKeyDown(KEY_LEFT)) {
ds.x = -1;
}
if (IsKeyDown(KEY_RIGHT)) {
ds.x = 1;
}
r2.x += ds.x;
r2.y += ds.y;
circle.center += ds;
DrawRectangle(r2.x, r2.y, r2.width, r2.height, BLUE);
DrawCircleV(circle.center, circle.radius, RED);
auto rectColor = BLUE;
if (hlam::collision_rect_rect(r1, r2)) {
rectColor = GREEN;
}
DrawRectangle(r1.x, r1.y, r1.width, r1.height, rectColor);
auto lineColor = BLUE;
if (hlam::collision_line_rect(line[0], line[1], r2)) {
lineColor = GREEN;
}
DrawLineV(line[0], line[1], lineColor);
auto triangleColor = RED;
if (hlam::collision_triangle_circle(triangle, circle.center, circle.radius)) {
triangleColor = GREEN;
}
DrawTriangle(triangle.p1, triangle.p2, triangle.p3, triangleColor);
auto line2Color = RED;
if (hlam::collision_line_circle(line2[0], line2[1], circle.center, circle.radius)) {
line2Color = GREEN;
}
DrawLineV(line2[0], line2[1], line2Color);
}
int main() {
std::cout << "AAA" << std::endl;
hlam::Vec2 v1{1, 2};
hlam::Vec2 v2{2, 3};
auto v = v1 * v2;
std::cout << v.x << " " << v.y << std::endl;
v *= 3;
std::cout << v.x << " " << v.y << std::endl;
std::cout << vec_dot(v1, v2) << std::endl;
InitWindow(400, 240, "Example");
canvas = LoadRenderTexture(viewport.canvas_width, viewport.canvas_height);
SetTextureFilter(canvas.texture, TEXTURE_FILTER_POINT);
hlam::Vec2 p{20, 10};
hlam::Vec2 size{40, 60};
SetTargetFPS(60);
while (!WindowShouldClose()) {
viewport.update();
BeginTextureMode(canvas);
ClearBackground(PINK);
DrawRectangleV(p, size, GREEN);
CollisionDemo();
EndTextureMode();
// actual render in screen
BeginDrawing();
ClearBackground(BLACK);
viewport.draw(canvas.texture);
DrawFPS(5, 5);
EndDrawing();
}
UnloadRenderTexture(canvas);
CloseWindow();
return 0;
}