Skip to content

Commit

Permalink
fix variable types and optimize rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
julesgrc0 committed Feb 20, 2024
1 parent 9709442 commit 0a36dd2
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 87 deletions.
30 changes: 12 additions & 18 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ on:
branches: ["main"]

env:
BUILD_PATH: ./android/app/build/outputs/apk/release
BUILD_PATH: ./app/build/outputs/apk/release
BUILD_TYPE: release
BUILD_PLATFORM: android


permissions:
security-events: write
actions: read
Expand All @@ -27,36 +26,31 @@ jobs:
arch: [x64, x86]
steps:
- uses: actions/checkout@v4
with:
with:
submodules: true

- uses: lukka/get-cmake@latest

- name: Setup Java JDK
uses: actions/setup-java@v3.13.0
with:
distribution: 'adopt'
java-version: '17'
distribution: "adopt"
java-version: "17"

- name: Setup Android SDK
uses: android-actions/setup-android@v2

- name: Build Application
run: |
sudo apt-get update -y
sudo apt-get install -y cmake
cd ./android
chmod +x ./gradlew
./gradlew assembleRelease
# - name: Sign Application
# run: |
# keytool -genkey -v -keystore release-key.keystore -alias wispy -keyalg RSA -keysize 2048 -validity 10000 -storepass ${{ secrets.KEYSTORE_PASSWORD }} -keypass ${{ secrets.KEY_PASSWORD }} -dname "CN=julesgrc0, OU=Engineering, O=Wispy, L=France, S=France, C=FR"
# ${ANDROID_HOME}/build-tools/31.0.0/apksigner sign --ks release-key.keystore --ks-key-alias wispy --ks-pass pass:${{ secrets.KEYSTORE_PASSWORD }} --key-pass pass:${{ secrets.KEY_PASSWORD }} --out ${{env.BUILD_PATH}}signed-app-release.apk ${{env.BUILD_PATH}}app-release.apk

sudo apt-get update -y
sudo apt-get install -y cmake
cd ./android
chmod +x ./gradlew
./gradlew assembleRelease
tree
- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: wipsy-${{env.BUILD_PLATFORM}}-${{matrix.arch}}.apk
path: ${{env.BUILD_PATH}}/app-release.apk

2 changes: 1 addition & 1 deletion src/core/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ Vector2 vec_block_round(Vector2 vec) {
.x = round(vec.x / (float)CUBE_W) * (float)CUBE_W,
.y = round(vec.y / (float)CUBE_H) * (float)CUBE_H,
};
}
}
7 changes: 4 additions & 3 deletions src/core/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ typedef struct w_collision {
#pragma pack(pop)

#define smooth_float(current, target, speed) \
current = (current < target) ? fmin(current + speed, target) \
: fmax(current - speed, target);
current = ((float)current < (float)target) \
? fmin((float)current + (float)speed, (float)target) \
: fmax((float)current - (float)speed, (float)target);

Vector2 center_camera_on_object(Camera2D *camera, Rectangle box);
Vector2 center_object_on_camera(Rectangle box, Camera2D *camera);
Expand All @@ -25,4 +26,4 @@ void smooth_vec(Vector2 *position, Vector2 target, float move);
void smooth_rect(Rectangle *box, Rectangle target, float move);

Vector2 get_mouse_block_center(Camera2D *camera);
Vector2 vec_block_round(Vector2 vec);
Vector2 vec_block_round(Vector2 vec);
9 changes: 5 additions & 4 deletions src/gui/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ void move_button(w_guibutton *button, Vector2 position) {
}

bool update_button(w_guibutton *button) {
bool is_hover =
#ifdef __ANDROID__
has_touch() && check_collision_touch_rect(button->rect);
bool is_hover = has_touch() && check_collision_touch_rect(button->rect);
#else
bool is_hover =
CheckCollisionRecs(button->rect, (Rectangle){
.x = FORMAT_W(GetMouseX()),
.y = FORMAT_H(GetMouseY()),
Expand All @@ -53,8 +53,9 @@ bool update_button(w_guibutton *button) {

DrawRectangleRoundedLines(button->rect, button->ctx->border_radius, 1.f,
button->ctx->border_size, color);
DrawText(button->text, button->position.x, button->position.y,
button->ctx->font_size, color);
DrawText(button->text, (int)button->position.x, (int)button->position.y,
(int)button->ctx->font_size, color);

#ifdef __ANDROID__
return is_hover;
#else
Expand Down
51 changes: 50 additions & 1 deletion src/gui/gui.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "../stdafx.h"


typedef struct w_guicontext {
float border_size;
float margin_width;
Expand All @@ -13,3 +12,53 @@ typedef struct w_guicontext {
w_guicontext *create_gui();
void destroy_gui(w_guicontext *ctx);

/*
TODO: idea for a gui system
<frame
width="X <%|px>"
height="Y <%|px>"
x="X <%|px>"
y="Y <%|px>"
border="X <%|px>"
border-radius="X <%|px>"
padding="X <%|px>"
>
... (frames)
</frame>
typedef struct w_guiframe w_guiframe;
typedef struct w_guiframe {
float width;
float height;
float x;
float y;
float border;
float border_radius;
float padding;
Color background_color;
Color border_color;
char *text;
int font_size;
Texture texture;
size_t len;
w_guiframe *frames;
};
void calculate_frame(w_guiframe* root);
*/
10 changes: 5 additions & 5 deletions src/screen/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ void menu_screen(w_state *state) {
(Vector2){0, title_text->font_size + 10}),
(char *)TextFormat("made by @julesgrc0 - %s", WISPY_VERSION), 20, WHITE);

double angle = 0.0;
float angle = 0.0;
while (!WindowShouldClose() && is_active) {

double speed = GetFrameTime() * 0.1;
float speed = GetFrameTime() * 0.1;
angle += speed;
angle = fmod(angle, 360.0);
angle = fmodf(angle, 360.0);

camera.target.x += (float)((sin(angle) * 1000.0) * speed);
camera.target.y += (float)((cos(angle) * 1000.0) * speed);
camera.target.x += (sinf(angle) * 1000.0) * speed;
camera.target.y += (cosf(angle) * 1000.0) * speed;

update_chunkview(view, grp, get_camera_view(&camera));
update_chunkview_lighting(
Expand Down
55 changes: 12 additions & 43 deletions src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,29 @@
if (x) \
free(x);

#define PHYSICS_TICK (1.0f / 120.0f)

#define RENDER_W 1280
#define RENDER_H 720

#define PERCENT_W(x) RENDER_W *x
#define PERCENT_H(x) RENDER_H *x
#define PIXELS_W(x) RENDER_W / x
#define PIXELS_H(x) RENDER_H / x

#define FORMAT_TO(x, size, target) (x / size) * target
#define FORMAT_W(x) FORMAT_TO(x, GetScreenWidth(), RENDER_W)
#define FORMAT_H(x) FORMAT_TO(x, GetScreenHeight(), RENDER_H)
#define FORMAT_TO(x, size, target) (((float)x / (float)size) * (float)target)
#define FORMAT_W(x) FORMAT_TO(x, GetRenderWidth(), RENDER_W)
#define FORMAT_H(x) FORMAT_TO(x, GetRenderHeight(), RENDER_H)
#define FORMAT_VEC(x) \
(Vector2) { FORMAT_W(x.x), FORMAT_H(x.y) }

#define PHYSICS_TICK (1.0f / 120.0f)

#define RENDER_W 1280
#define RENDER_H 720

#define RENDER_CUBE_COUNT 20
#define RENDER_CUBE_GAP 2

#define CHUNK_MID_H 128
#define CHUNK_H 256 // 2^8
#define CHUNK_W 64 // 2^6
#define CHUNK_MID_H 128

#define CHUNK_GROUP_LEN 10
#define CHUNK_GROUP_UPDATE_DIST 2
#define CHUNK_GROUP_MID_LEN 5
Expand Down Expand Up @@ -113,38 +116,4 @@
#define LOG(...)
#endif // _DEBUG

#if defined(_DEBUG) && defined(_WIN32)
#define measure(name, x) \
do { \
LARGE_INTEGER start, end, frequency; \
long long elapsed_time; \
QueryPerformanceFrequency(&frequency); \
QueryPerformanceCounter(&start); \
\
x; \
\
QueryPerformanceCounter(&end); \
elapsed_time = \
(end.QuadPart - start.QuadPart) * 100000000 / frequency.QuadPart; \
printf("[%s]: %lld ns\n", #name, elapsed_time); \
} while (0);

#elif defined(_DEBUG) && (defined(__linux__) || defined(__ANDROID__))
#define measure(name, x) \
do { \
struct timespec start, end; \
long long elapsed_time; \
clock_gettime(CLOCK_MONOTONIC, &start); \
\
x; \
\
clock_gettime(CLOCK_MONOTONIC, &end); \
elapsed_time = (end.tv_sec - start.tv_sec) * 1000000000 + \
(end.tv_nsec - start.tv_nsec); \
printf("[%s]: %lld ns\n", #name, elapsed_time); \
} while (0);
#else
#define measure(name, x)
#endif // _DEBUG && _WIN32

#endif // _STDAFX_H
10 changes: 4 additions & 6 deletions src/terrain/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ void *create_chunk_thread(void *arg)
LOG("creating chunk");
w_chunk *chunk = arg;

Image topLayer = GenImagePerlinNoise(CHUNK_W, BLOCK_TOP_LAYER_H,
chunk->position * CHUNK_W, 0, 6.f);
Image base = GenImagePerlinNoise(CHUNK_W, BLOCK_TOP_LAYER_H,
chunk->position * CHUNK_W, 0, 6.f);
Image mineral =
GenImageCellular(CHUNK_W, CHUNK_MID_H - BLOCK_TOP_LAYER_H, 10);

// Image caves = GenImageCellular(CHUNK_W, mineralHeight, 10);

for (unsigned int x = 0; x < CHUNK_W; x++) {
unsigned int level = 0;
bool firstLayer = false;
Expand All @@ -68,7 +66,7 @@ void *create_chunk_thread(void *arg)
w_blocktype type = BLOCK_AIR;
if (y >= CHUNK_MID_H) {
if (y < CHUNK_MID_H + BLOCK_TOP_LAYER_H) {
unsigned int value = GetImageColor(topLayer, x, (y - CHUNK_MID_H)).r;
unsigned int value = GetImageColor(base, x, (y - CHUNK_MID_H)).r;

if (level == 0) {
if (y > CHUNK_MID_H + BLOCK_TOP_LAYER_H / 3 || (value < 105)) {
Expand Down Expand Up @@ -112,7 +110,7 @@ void *create_chunk_thread(void *arg)
}
}

UnloadImage(topLayer);
UnloadImage(base);
UnloadImage(mineral);

#ifdef _WIN32
Expand Down
16 changes: 10 additions & 6 deletions src/terrain/chunk_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ void filter_chunkview_blocks(w_chunk *chunk, Rectangle view,

Rectangle block = {.x = 0, .y = 0, .width = CUBE_W, .height = CUBE_H};

#pragma omp parallel for
for (unsigned int x = 0; x < CHUNK_W; x++) {

bool noclear = false;
Expand All @@ -156,12 +157,14 @@ void filter_chunkview_blocks(w_chunk *chunk, Rectangle view,
block.y = y * CUBE_H;

if (CheckCollisionRecs(block, view)) {

blocks[*rendercount] = (w_renderblock){.dst = block,
.src = CUBE_SRC_RECT,
.light = WHITE,
.block = chunk->blocks[index]};
(*rendercount)++;
#pragma omp critical
{
blocks[*rendercount] = (w_renderblock){.dst = block,
.src = CUBE_SRC_RECT,
.light = WHITE,
.block = chunk->blocks[index]};
(*rendercount)++;
}
}
}
}
Expand All @@ -173,6 +176,7 @@ void update_chunkview_lighting(w_chunkview *chunk_view, Vector2 light,
return;
}

#pragma omp parallel for
for (size_t i = 0; i < chunk_view->textures_len; i++) {
Vector2 block_center = {chunk_view->blocks[i].dst.x + (CUBE_W / 2),
chunk_view->blocks[i].dst.y + (CUBE_H / 2)};
Expand Down

0 comments on commit 0a36dd2

Please sign in to comment.