Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeokkim committed Oct 22, 2023
2 parents 5f505a8 + c460fea commit 71ae99d
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 67 deletions.
1 change: 1 addition & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ SOURCE_PATH = src
TARGETS ?= \
${SOURCE_PATH}/basic.out \
${SOURCE_PATH}/cows.out \
${SOURCE_PATH}/melon.out \
${SOURCE_PATH}/raycast.out \
${SOURCE_PATH}/raylib.out

Expand Down
1 change: 1 addition & 0 deletions examples/Makefile.emcc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SOURCE_PATH = src
TARGETS = \
${SOURCE_PATH}/basic.html \
${SOURCE_PATH}/cows.html \
${SOURCE_PATH}/melon.html \
${SOURCE_PATH}/raycast.html \
${SOURCE_PATH}/raylib.html

Expand Down
1 change: 1 addition & 0 deletions examples/Makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SOURCE_PATH = src
TARGETS = \
${SOURCE_PATH}/basic.exe \
${SOURCE_PATH}/cows.exe \
${SOURCE_PATH}/melon.exe \
${SOURCE_PATH}/raycast.exe \
${SOURCE_PATH}/raylib.exe

Expand Down
2 changes: 1 addition & 1 deletion examples/include/ferox-raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Copyright (c) 2021-2023 Jaedeok Kim <jdeokkim@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a
copyof this software and associated documentation files (the "Software"),
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Expand Down
8 changes: 4 additions & 4 deletions examples/src/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Copyright (c) 2021-2023 Jaedeok Kim <jdeokkim@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a
copyof this software and associated documentation files (the "Software"),
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Expand Down Expand Up @@ -45,11 +45,11 @@

/* Constants =============================================================== */

static const float CELL_SIZE = 4.0f, DELTA_TIME = 1.0f / TARGET_FPS;

static const Rectangle SCREEN_BOUNDS = { .width = SCREEN_WIDTH,
.height = SCREEN_HEIGHT };

static const float CELL_SIZE = 4.0f, DELTA_TIME = 1.0f / TARGET_FPS;

/* Private Variables ======================================================= */

static frWorld *world;
Expand Down Expand Up @@ -91,7 +91,7 @@ int main(void) {

static void InitExample(void) {
world = frCreateWorld(frVector2ScalarMultiply(FR_WORLD_DEFAULT_GRAVITY,
4.0f),
2.5f),
CELL_SIZE);

ground = frCreateBodyFromShape(
Expand Down
6 changes: 3 additions & 3 deletions examples/src/cows.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Copyright (c) 2021-2023 Jaedeok Kim <jdeokkim@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a
copyof this software and associated documentation files (the "Software"),
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Expand Down Expand Up @@ -63,8 +63,6 @@ typedef struct _EntityData {

/* Constants =============================================================== */

static const float CELL_SIZE = 4.0f, DELTA_TIME = 1.0f / TARGET_FPS;

static const frMaterial MATERIAL_BULLET = { .density = 2.25f,
.friction = 0.85f,
.restitution = 0.0f };
Expand All @@ -80,6 +78,8 @@ static const frMaterial MATERIAL_PLAYER = { .density = 1.25f,
static const Rectangle SCREEN_BOUNDS = { .width = SCREEN_WIDTH,
.height = SCREEN_HEIGHT };

static const float CELL_SIZE = 4.0f, DELTA_TIME = 1.0f / TARGET_FPS;

/* Private Variables ======================================================= */

static EntityData entityData[ENTITY_COUNT_] = {
Expand Down
261 changes: 261 additions & 0 deletions examples/src/melon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
/*
Copyright (c) 2021-2023 Jaedeok Kim <jdeokkim@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

/* Includes ================================================================ */

#include "ferox.h"
#include "raylib.h"

#define FEROX_RAYLIB_IMPLEMENTATION
#include "ferox-raylib.h"

#ifdef PLATFORM_WEB
#include <emscripten/emscripten.h>
#endif

/* Macros ================================================================== */

// clang-format off

#define TARGET_FPS 60

#define SCREEN_WIDTH 600
#define SCREEN_HEIGHT 800

#define MAX_WALL_COUNT 3
#define MELON_KIND_COUNT 4

// clang-format on

/* Typedefs ================================================================ */

typedef struct _MelonKind {
int index;
Color color;
} MelonKind;

/* Constants =============================================================== */

static const frMaterial MATERIAL_WALL = { .density = 1.25f,
.friction = 0.75f,
.restitution = 0.1f };

static const Rectangle SCREEN_BOUNDS = { .width = SCREEN_WIDTH,
.height = SCREEN_HEIGHT };

static const MelonKind MELON_KINDS[MELON_KIND_COUNT] = {
{ .index = 0, .color = RED },
{ .index = 1, .color = ORANGE },
{ .index = 2, .color = YELLOW },
{ .index = 3, .color = GREEN }
};

static const float CELL_SIZE = 4.0f, DELTA_TIME = 1.0f / (2.0f * TARGET_FPS);

/* Private Variables ======================================================= */

static frWorld *world;

static frShape *melonShapes[MELON_KIND_COUNT];

static frBody *cursor, *walls[MAX_WALL_COUNT];

/* Private Function Prototypes ============================================= */

static void InitExample(void);
static void UpdateExample(void);
static void DeinitExample(void);

/* Public Functions ======================================================== */

int main(void) {
SetConfigFlags(FLAG_MSAA_4X_HINT);

InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "c-krit/ferox | melon.c");

InitExample();

#ifdef PLATFORM_WEB
emscripten_set_main_loop(UpdateExample, 0, 1);
#else
SetTargetFPS(TARGET_FPS);

while (!WindowShouldClose())
UpdateExample();
#endif

DeinitExample();

CloseWindow();

return 0;
}

/* Private Functions ======================================================= */

static void InitExample(void) {
world = frCreateWorld(frVector2ScalarMultiply(FR_WORLD_DEFAULT_GRAVITY,
2.0f),
CELL_SIZE);

for (int i = 0; i < MELON_KIND_COUNT; i++)
melonShapes[i] = frCreateCircle((frMaterial) { .density = 3.5f
/ (i + 1),
.friction = 0.65f,
.restitution = 0.1f },
0.4f * (i + 3));

walls[0] = frCreateBodyFromShape(
FR_BODY_STATIC,
frVector2PixelsToUnits((frVector2) { .x = 0.5f * SCREEN_WIDTH,
.y = 0.95f * SCREEN_HEIGHT }),
frCreateRectangle(MATERIAL_WALL,
frPixelsToUnits(1.0f * SCREEN_WIDTH),
frPixelsToUnits(0.1f * SCREEN_HEIGHT)));

const frVertices wallVertices1 = {
.data = { frVector2PixelsToUnits(
(frVector2) { .x = -80.0f, .y = 400.0f }),
frVector2PixelsToUnits(
(frVector2) { .x = -80.0f, .y = -400.0f }),
frVector2PixelsToUnits(
(frVector2) { .x = 120.0f, .y = 400.0f }),
frVector2PixelsToUnits(
(frVector2) { .x = 0.0f, .y = -400.0f }) },
.count = 4
};

walls[1] = frCreateBodyFromShape(FR_BODY_STATIC,
frVector2PixelsToUnits((frVector2) {
.x = 0.02f * SCREEN_WIDTH,
.y = 0.5f * SCREEN_HEIGHT }),
frCreatePolygon(MATERIAL_WALL,
&wallVertices1));

const frVertices wallVertices2 = {
.data = { frVector2PixelsToUnits(
(frVector2) { .x = 0.0f, .y = -400.0f }),
frVector2PixelsToUnits(
(frVector2) { .x = -120.0f, .y = 400.0f }),
frVector2PixelsToUnits(
(frVector2) { .x = 80.0f, .y = 400.0f }),
frVector2PixelsToUnits(
(frVector2) { .x = 80.0f, .y = -400.0f }) },
.count = 4
};

walls[2] = frCreateBodyFromShape(FR_BODY_STATIC,
frVector2PixelsToUnits((frVector2) {
.x = 0.98f * SCREEN_WIDTH,
.y = 0.5f * SCREEN_HEIGHT }),
frCreatePolygon(MATERIAL_WALL,
&wallVertices2));

for (int i = 0; i < MAX_WALL_COUNT; i++)
frAddBodyToWorld(world, walls[i]);

cursor = frCreateBodyFromShape(FR_BODY_KINEMATIC,
frVector2PixelsToUnits((frVector2) {
.x = 0.5f * SCREEN_WIDTH,
.y = 0.15f * SCREEN_HEIGHT }),
melonShapes[0]);

frSetBodyUserData(cursor, (void *) &MELON_KINDS[0]);
}

static void UpdateExample(void) {
const MelonKind *cursorKind = frGetBodyUserData(cursor);

{
frVector2 cursorPosition = frGetBodyPosition(cursor);

if (IsKeyDown(KEY_LEFT)) cursorPosition.x -= 0.2f;
if (IsKeyDown(KEY_RIGHT)) cursorPosition.x += 0.2f;

if (cursorPosition.x < frPixelsToUnits(0.1f * SCREEN_WIDTH))
cursorPosition.x = frPixelsToUnits(0.1f * SCREEN_WIDTH);

if (cursorPosition.x > frPixelsToUnits(0.9f * SCREEN_WIDTH))
cursorPosition.x = frPixelsToUnits(0.9f * SCREEN_WIDTH);

frSetBodyPosition(cursor, cursorPosition);

if (IsKeyPressed(KEY_SPACE)) {
frBody *melon =
frCreateBodyFromShape(FR_BODY_DYNAMIC,
cursorPosition,
melonShapes[cursorKind->index]);

frSetBodyUserData(melon, (void *) cursorKind);

frAddBodyToWorld(world, melon);

const int cursorIndex = GetRandomValue(0, MELON_KIND_COUNT - 1);

frSetBodyShape(cursor, melonShapes[cursorIndex]);
frSetBodyUserData(cursor, (void *) &MELON_KINDS[cursorIndex]);
}
}

frUpdateWorld(world, DELTA_TIME);

{
BeginDrawing();

ClearBackground(FR_DRAW_COLOR_MATTEBLACK);

frDrawGrid(SCREEN_BOUNDS,
CELL_SIZE,
0.25f,
ColorAlpha(DARKGRAY, 0.75f));

for (int i = 0; i < MAX_WALL_COUNT; i++)
frDrawBodyLines(walls[i], 1.0f, DARKGRAY);

const int bodyCount = frGetBodyCountForWorld(world);

for (int i = MAX_WALL_COUNT; i < bodyCount; i++) {
frBody *melon = frGetBodyFromWorld(world, i);

const MelonKind *melonKind = frGetBodyUserData(melon);

frDrawBodyLines(melon, 2.0f, melonKind->color);
}

frDrawBodyLines(cursor, 2.0f, ColorAlpha(cursorKind->color, 0.5f));

DrawFPS(8, 8);

EndDrawing();
}
}

static void DeinitExample(void) {
for (int i = 0; i < MELON_KIND_COUNT; i++)
frReleaseShape(melonShapes[i]);

for (int i = 0; i < MAX_WALL_COUNT; i++)
frReleaseShape(frGetBodyShape(walls[i]));

frReleaseBody(cursor);
frReleaseWorld(world);
}
6 changes: 3 additions & 3 deletions examples/src/raycast.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Copyright (c) 2021-2023 Jaedeok Kim <jdeokkim@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a
copyof this software and associated documentation files (the "Software"),
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Expand Down Expand Up @@ -47,11 +47,11 @@

/* Constants =============================================================== */

static const float CELL_SIZE = 4.0f, DELTA_TIME = 1.0f / TARGET_FPS;

static const Rectangle SCREEN_BOUNDS = { .width = SCREEN_WIDTH,
.height = SCREEN_HEIGHT };

static const float CELL_SIZE = 4.0f, DELTA_TIME = 1.0f / TARGET_FPS;

/* Private Variables ======================================================= */

static frWorld *world;
Expand Down
Loading

0 comments on commit 71ae99d

Please sign in to comment.