-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
316 lines (273 loc) · 12.4 KB
/
Copy pathmain.cpp
File metadata and controls
316 lines (273 loc) · 12.4 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "box2d/box2d.h"
#include <memory>
#include <vector>
#include "raylib.h"
#include "raytween.h"
const int GAME_WIDTH = 800, GAME_HEIGHT = 600;
enum Category {
PADDLE = 0x0001,
BRICK = 0x0002,
SQUARE = 0x0003
};
class Paddle {
private:
Rectangle rectangle;
float defaultY;
b2BodyId bodyId;
b2ShapeId shapeId;
public:
Paddle(float width, float height, b2WorldId worldId) {
this->defaultY = GAME_HEIGHT - height / 2 - 10;
this->rectangle.x = GAME_WIDTH / 2.0f - width / 2;
this->rectangle.y = this->defaultY - height / 2;
this->rectangle.width = width;
this->rectangle.height = height;
b2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = b2_kinematicBody;
bodyDef.fixedRotation = true;
bodyDef.position = (b2Vec2){GAME_WIDTH / 2.0f, this->defaultY};
this->bodyId = b2CreateBody(worldId, &bodyDef);
b2ShapeDef shapeDef = b2DefaultShapeDef();
b2Filter filter;
filter.categoryBits = Category::PADDLE;
filter.maskBits = Category::SQUARE;
shapeDef.filter = filter;
shapeDef.enableContactEvents = true;
b2Polygon polygon = b2MakeBox(width / 2, height / 2);
this->shapeId = b2CreatePolygonShape(this->bodyId, &shapeDef, &polygon);
}
void Update() {
Vector2 mousePosition = GetMousePosition();
b2Vec2 position = {mousePosition.x, defaultY};
if(position.x < rectangle.width / 2) position.x = rectangle.width / 2;
if(position.x > GAME_WIDTH - rectangle.width / 2) position.x = GAME_WIDTH - rectangle.width / 2;
b2Body_SetTransform(bodyId, position, b2Rot_identity);
}
void Draw() {
b2Vec2 position = b2Body_GetPosition(bodyId);
rectangle.x = position.x - rectangle.width / 2;
rectangle.y = position.y - rectangle.height / 2;
DrawRectangleRec(rectangle, WHITE);
}
};
class Square {
private:
Rectangle rectangle;
float speed;
float paddleWidth;
float defaultY;
bool isReady = false;
b2Vec2 direction;
b2BodyId bodyId;
b2ShapeId shapeId;
public:
Square(float size, float speed, float paddleWidth, b2WorldId worldId) {
this->speed = speed;
this->paddleWidth = paddleWidth;
this->defaultY = GAME_HEIGHT - size / 2 - 30;
this->rectangle.x = GAME_WIDTH / 2.0f - size / 2;
this->rectangle.y = this->defaultY - size / 2;
this->rectangle.width = size;
this->rectangle.height = size;
b2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = b2_dynamicBody;
bodyDef.fixedRotation = true;
bodyDef.position = (b2Vec2){GAME_WIDTH / 2.0f, this->defaultY};
this->bodyId = b2CreateBody(worldId, &bodyDef);
b2ShapeDef shapeDef = b2DefaultShapeDef();
b2Filter filter;
filter.categoryBits = Category::SQUARE;
filter.maskBits = Category::PADDLE | Category::BRICK;
shapeDef.filter = filter;
shapeDef.enableContactEvents = true;
b2Polygon polygon = b2MakeSquare(size / 2);
this->shapeId = b2CreatePolygonShape(this->bodyId, &shapeDef, &polygon);
}
void Update(bool isBricksReady) {
if(!isReady) {
Vector2 mousePosition = GetMousePosition();
b2Vec2 position = {mousePosition.x, defaultY};
if(position.x < paddleWidth / 2) position.x = paddleWidth / 2;
if(position.x > GAME_WIDTH - paddleWidth / 2) position.x = GAME_WIDTH - paddleWidth / 2;
b2Body_SetTransform(bodyId, position, b2Rot_identity);
if(IsMouseButtonPressed(0) && isBricksReady) {
isReady = true;
float rad = 310 * DEG2RAD;
direction.x = cosf(rad);
direction.y = sinf(rad);
b2Body_SetLinearVelocity(bodyId, direction * speed);
}
}
if(b2Body_GetPosition(bodyId).y > GAME_HEIGHT) {
this->Reset();
}
}
void Draw() {
b2Vec2 position = b2Body_GetPosition(bodyId);
rectangle.x = position.x - rectangle.width / 2;
rectangle.y = position.y - rectangle.height / 2;
DrawRectangleRec(rectangle, WHITE);
}
void Reflection(b2Vec2 normal) {
direction = direction - 2 * b2Dot(direction, normal) * normal;
b2Body_SetLinearVelocity(bodyId, direction * speed);
}
void Reset() {
isReady = false;
b2Body_SetLinearVelocity(bodyId, (b2Vec2){0, 0});
b2Body_SetTransform(bodyId, (b2Vec2){GAME_WIDTH / 2.0f, defaultY}, b2Rot_identity);
}
};
struct BrickData {
int index;
};
class Brick {
private:
b2BodyId bodyId;
b2ShapeId shapeId;
public:
Rectangle rectangle;
Brick(float x, float y, float width, float height, b2WorldId worldId) {
this->rectangle.x = x;
this->rectangle.y = y;
this->rectangle.width = width;
this->rectangle.height = height;
b2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = b2_staticBody;
bodyDef.position = (b2Vec2){x + width / 2, y + height / 2};
this->bodyId = b2CreateBody(worldId, &bodyDef);
b2ShapeDef shapeDef = b2DefaultShapeDef();
b2Filter filter;
filter.categoryBits = Category::BRICK;
filter.maskBits = Category::SQUARE;
shapeDef.filter = filter;
shapeDef.enableContactEvents = true;
b2Polygon polygon = b2MakeBox(width / 2, height / 2);
this->shapeId = b2CreatePolygonShape(this->bodyId, &shapeDef, &polygon);
}
void Draw() {
DrawRectangleRec(rectangle, WHITE);
}
void SetUserData(BrickData* brickData) {
b2Shape_SetUserData(shapeId, brickData);
}
};
void RerenderTexture(RenderTexture2D& texture, std::unique_ptr<Brick>& brick) {
BeginTextureMode(texture);
BeginScissorMode(brick->rectangle.x, brick->rectangle.y, brick->rectangle.width, brick->rectangle.height);
ClearBackground(BLANK);
EndScissorMode();
EndTextureMode();
}
void RespawnBricks(std::vector<std::unique_ptr<Brick>>& bricks, const RenderTexture2D& bricksTexture, b2WorldId worldId, Vector2 brickNum, Vector2 brickSize, int& brickCount, bool& isReady, Rectangle& dest) {
bricks.clear();
bricks.shrink_to_fit();
BeginTextureMode(bricksTexture);
ClearBackground(BLANK);
for(int i = 0; i < brickNum.x * brickNum.y; i++) {
int y = i / brickNum.x;
int x = i % (int)brickNum.x;
bricks.push_back(std::make_unique<Brick>(10 + x * brickSize.x + x * 10, 10 + y * brickSize.y + y * 10, brickSize.x, brickSize.y, worldId));
Brick* brick = bricks[i].get();
BrickData* brickData = new BrickData();
brickData->index = i;
brick->SetUserData(brickData);
brick->Draw();
}
EndTextureMode();
brickCount = (int)(brickNum.x * brickNum.y);
dest.y = -150;
Raytween::Value(-150, 0, 1.5)->SetOnUpdate([&dest](float value) { dest.y = value; })->SetOnComplete([&isReady]() { isReady = true; });
}
int main() {
SetConfigFlags(FLAG_VSYNC_HINT);
InitWindow(GAME_WIDTH, GAME_HEIGHT, "Breakout");
SetTargetFPS(60);
float timeStep = 1 / 60.0f;
int subStepCount = 4;
b2WorldDef worldDef = b2DefaultWorldDef();
worldDef.gravity = (b2Vec2){0, 0};
b2WorldId worldId = b2CreateWorld(&worldDef);
// Left, right and top border
b2BodyDef borderBodyDef = b2DefaultBodyDef();
borderBodyDef.type = b2_staticBody;
borderBodyDef.position = (b2Vec2){-2.5f, GAME_HEIGHT / 2.0f};
b2BodyId leftBorderBodyId = b2CreateBody(worldId, &borderBodyDef);
borderBodyDef.position = (b2Vec2){GAME_WIDTH + 2.5f, GAME_HEIGHT / 2.0f};
b2BodyId rightBorderBodyId = b2CreateBody(worldId, &borderBodyDef);
borderBodyDef.position = (b2Vec2){GAME_WIDTH / 2.0f, -2.5f};
b2BodyId topBorderBodyId = b2CreateBody(worldId, &borderBodyDef);
b2ShapeDef borderShapeDef = b2DefaultShapeDef();
b2Filter filter;
filter.categoryBits = Category::PADDLE;
filter.maskBits = Category::SQUARE;
borderShapeDef.filter = filter;
borderShapeDef.enableContactEvents = true;
b2Polygon borderPolygon = b2MakeBox(2.5f, GAME_HEIGHT / 2.0f);
b2CreatePolygonShape(leftBorderBodyId, &borderShapeDef, &borderPolygon);
b2CreatePolygonShape(rightBorderBodyId, &borderShapeDef, &borderPolygon);
borderPolygon = b2MakeBox(GAME_WIDTH / 2.0f, 2.5f);
b2CreatePolygonShape(topBorderBodyId, &borderShapeDef, &borderPolygon);
Paddle paddle(140, 10, worldId);
Square square(8, 350, 140, worldId);
bool isReady = false;
Vector2 brickNum = {5, 3};
int brickCount = (int)(brickNum.x * brickNum.y);
float totalWidth = GAME_WIDTH - 20;
Vector2 brickSize = {(totalWidth - (brickNum.x - 1) * 10.0f) / brickNum.x, 10};
RenderTexture2D bricksTexture = LoadRenderTexture(GAME_WIDTH, brickSize.y * brickNum.y + (brickNum.y + 2) * 10);
std::vector<std::unique_ptr<Brick>> bricks;
Rectangle bricksTextureSource = {0, 0, (float)bricksTexture.texture.width, (float)-bricksTexture.texture.height};
Rectangle bricksTextureDest = {0, 0, (float)bricksTexture.texture.width, (float)bricksTexture.texture.height};
RespawnBricks(bricks, bricksTexture, worldId, brickNum, brickSize, brickCount, isReady, bricksTextureDest);
Vector2 bricksTextureOrigin = {0, 0};
while(!WindowShouldClose()) {
b2World_Step(worldId, timeStep, subStepCount);
b2ContactEvents contactEvents = b2World_GetContactEvents(worldId);
for(int i = 0; i < contactEvents.beginCount; i++) {
b2ContactBeginTouchEvent* contactBeginTouchEvent = contactEvents.beginEvents + i;
b2ShapeId shapeIdA = contactBeginTouchEvent->shapeIdA;
b2ShapeId shapeIdB = contactBeginTouchEvent->shapeIdB;
b2Vec2 normal = contactBeginTouchEvent->manifold.normal;
if(b2Shape_GetFilter(shapeIdA).categoryBits == Category::SQUARE) {
normal = -normal;
square.Reflection(normal);
if(b2Shape_GetFilter(shapeIdB).categoryBits == Category::BRICK) {
BrickData* brickData = (BrickData*)b2Shape_GetUserData(shapeIdB);
RerenderTexture(bricksTexture, bricks[brickData->index]);
b2DestroyBody(b2Shape_GetBody(shapeIdB));
delete brickData;
brickCount -= 1;
if(brickCount == 0) {
isReady = false;
square.Reset();
RespawnBricks(bricks, bricksTexture, worldId, brickNum, brickSize, brickCount, isReady, bricksTextureDest);
}
}
} else if(b2Shape_GetFilter(shapeIdB).categoryBits == Category::SQUARE) {
square.Reflection(normal);
if(b2Shape_GetFilter(shapeIdA).categoryBits == Category::BRICK) {
BrickData* brickData = (BrickData*)b2Shape_GetUserData(shapeIdA);
RerenderTexture(bricksTexture, bricks[brickData->index]);
b2DestroyBody(b2Shape_GetBody(shapeIdA));
delete brickData;
brickCount -= 1;
}
}
}
Raytween::DoTweens(GetFrameTime());
paddle.Update();
square.Update(isReady);
BeginDrawing();
ClearBackground(BLACK);
DrawTexturePro(bricksTexture.texture, bricksTextureSource, bricksTextureDest, bricksTextureOrigin, 0, WHITE);
paddle.Draw();
square.Draw();
DrawFPS(0, 0);
EndDrawing();
}
b2DestroyWorld(worldId);
UnloadRenderTexture(bricksTexture);
CloseWindow();
return 0;
}