Skip to content

Commit

Permalink
Merge branch 'feat/ex-query' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeokkim committed Nov 29, 2023
2 parents 0bde9cb + 0a2f6bb commit 46816ca
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ TARGETS ?= \
${SOURCE_PATH}/basic.out \
${SOURCE_PATH}/cows.out \
${SOURCE_PATH}/melon.out \
${SOURCE_PATH}/query.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 @@ -35,6 +35,7 @@ TARGETS = \
${SOURCE_PATH}/basic.html \
${SOURCE_PATH}/cows.html \
${SOURCE_PATH}/melon.html \
${SOURCE_PATH}/query.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 @@ -34,6 +34,7 @@ TARGETS = \
${SOURCE_PATH}/basic.exe \
${SOURCE_PATH}/cows.exe \
${SOURCE_PATH}/melon.exe \
${SOURCE_PATH}/query.exe \
${SOURCE_PATH}/raycast.exe \
${SOURCE_PATH}/raylib.exe

Expand Down
216 changes: 216 additions & 0 deletions examples/src/query.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
Copyright (c) 2021-2023 Jaedeok Kim <[email protected]>
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 1280
#define SCREEN_HEIGHT 800

#define CURSOR_SIZE_IN_PIXELS 128.0f

#define MAX_OBJECT_COUNT 256

// clang-format on

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

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

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

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

static frSpatialHash *hash;

static frBody *bodies[MAX_OBJECT_COUNT];

static Color primaryColor, secondaryColor, greenColor;

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

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

static void DrawCursor(void);
static frAABB GetCursorBounds(void);

static bool OnHashQuery(int index, void *ctx);

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

int main(void) {
SetConfigFlags(FLAG_MSAA_4X_HINT);

InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "c-krit/ferox | query.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) {
hash = frCreateSpatialHash(CELL_SIZE);

primaryColor = ColorAlpha(LIGHTGRAY, 0.35f);
secondaryColor = ColorAlpha(LIME, 0.85f);

for (int i = 0; i < MAX_OBJECT_COUNT; i++) {
frVector2 position = {
.x = GetRandomValue(0.02f * SCREEN_WIDTH, 0.98f * SCREEN_WIDTH),
.y = GetRandomValue(0.02f * SCREEN_HEIGHT, 0.98f * SCREEN_HEIGHT)
};

bodies[i] = frCreateBodyFromShape(
FR_BODY_STATIC,
frVector2PixelsToUnits(position),
frCreateRectangle(FR_API_STRUCT_ZERO(frMaterial),
0.35f * GetRandomValue(1, 3),
0.35f * GetRandomValue(1, 3)));

frSetBodyAngle(bodies[i], DEG2RAD * GetRandomValue(0, 360));
}

HideCursor();

#ifdef PLATFORM_WEB
// TODO: https://github.com/emscripten-core/emscripten/issues/5446
emscripten_hide_mouse();
#endif

SetMousePosition(0.5f * SCREEN_WIDTH, 0.5f * SCREEN_HEIGHT);
}

static void UpdateExample(void) {
{
frClearSpatialHash(hash);

for (int i = 0; i < MAX_OBJECT_COUNT; i++) {
frSetBodyUserData(bodies[i], (void *) &primaryColor);

frInsertToSpatialHash(hash, frGetBodyAABB(bodies[i]), i);
}

frQuerySpatialHash(hash, GetCursorBounds(), OnHashQuery, NULL);
}

{
BeginDrawing();

ClearBackground(FR_DRAW_COLOR_MATTEBLACK);

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

for (int i = 0; i < MAX_OBJECT_COUNT; i++) {
const Color *color = frGetBodyUserData(bodies[i]);

frDrawBodyLines(bodies[i], 2.0f, *color);
}

DrawCursor();

DrawFPS(8, 8);

EndDrawing();
}
}

static void DeinitExample(void) {
for (int i = 0; i < MAX_OBJECT_COUNT; i++)
frReleaseBody(bodies[i]);

frReleaseSpatialHash(hash);
}

static void DrawCursor(void) {
const Vector2 mousePosition = GetMousePosition();

Rectangle bounds = { .x = mousePosition.x - 0.5f * CURSOR_SIZE_IN_PIXELS,
.y = mousePosition.y - 0.5f * CURSOR_SIZE_IN_PIXELS,
.width = CURSOR_SIZE_IN_PIXELS,
.height = CURSOR_SIZE_IN_PIXELS };

Color color = ColorAlpha(GREEN, 0.85f);

DrawLineEx((Vector2) { .x = mousePosition.x - 4.0f, .y = mousePosition.y },
(Vector2) { .x = mousePosition.x + 4.0f, .y = mousePosition.y },
2.0f,
color);

DrawLineEx((Vector2) { .x = mousePosition.x, .y = mousePosition.y - 4.0f },
(Vector2) { .x = mousePosition.x, .y = mousePosition.y + 4.0f },
2.0f,
color);

DrawRectangleLinesEx(bounds, 2.0f, color);
}

static frAABB GetCursorBounds(void) {
const Vector2 mousePosition = GetMousePosition();

return (frAABB) {
.x = frPixelsToUnits(mousePosition.x - 0.5f * CURSOR_SIZE_IN_PIXELS),
.y = frPixelsToUnits(mousePosition.y - 0.5f * CURSOR_SIZE_IN_PIXELS),
.width = frPixelsToUnits(CURSOR_SIZE_IN_PIXELS),
.height = frPixelsToUnits(CURSOR_SIZE_IN_PIXELS)
};
}

static bool OnHashQuery(int index, void *ctx) {
frSetBodyUserData(bodies[index], (void *) &secondaryColor);
return true;
}

0 comments on commit 46816ca

Please sign in to comment.