rework to context: now there's engine and user code copies, renaming of fields, most things that shouldn't be there are hidden

This commit is contained in:
2024-10-12 20:24:47 +03:00
parent 7886650339
commit e70366f82f
14 changed files with 106 additions and 97 deletions

View File

@ -17,7 +17,7 @@ void handle_input(void)
{
State *state = ctx.udata;
if (ctx.mouse_window_position.y <= 60)
if (ctx.mouse_position.y <= 60)
return;
if (input_is_action_pressed("add_a_bit"))
@ -27,10 +27,10 @@ void handle_input(void)
if (state->bunniesCount < MAX_BUNNIES)
{
state->bunnies[state->bunniesCount].position = input_get_action_position("add_a_bit");
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0;
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0;
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0f;
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0f;
state->bunnies[state->bunniesCount].color =
(Color){rand() % 190 + 50, rand() % 160 + 80, rand() % 140 + 100, 255};
(Color){(uint8_t)(rand() % 190 + 50), (uint8_t)(rand() % 160 + 80), (uint8_t)(rand() % 140 + 100), 255};
state->bunniesCount++;
}
}
@ -43,10 +43,10 @@ void handle_input(void)
if (state->bunniesCount < MAX_BUNNIES)
{
state->bunnies[state->bunniesCount].position = input_get_action_position("add_a_lot");
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0;
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0;
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0f;
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0f;
state->bunnies[state->bunniesCount].color =
(Color){rand() % 190 + 50, rand() % 160 + 80, rand() % 140 + 100, 255};
(Color){(uint8_t)(rand() % 190 + 50), (uint8_t)(rand() % 160 + 80), (uint8_t)(rand() % 140 + 100), 255};
state->bunniesCount++;
}
}
@ -73,32 +73,29 @@ void game_tick(void)
State *state = ctx.udata;
const double delta =
(double)(ctx.delta_time) / 1000.0; // Receiving floating point delta value (diving by 1000 based on vibe)
for (int i = 0; i < state->bunniesCount; i++)
{
state->bunnies[i].position.x += state->bunnies[i].speed.x;
state->bunnies[i].position.y += state->bunnies[i].speed.y;
if (((state->bunnies[i].position.x + BUNNY_W / 2) > ctx.base_draw_w) ||
((state->bunnies[i].position.x + BUNNY_W / 2) < 0))
if (((state->bunnies[i].position.x + (float)BUNNY_W / 2) > (float)ctx.resolution.x) ||
((state->bunnies[i].position.x + (float)BUNNY_W / 2) < 0))
state->bunnies[i].speed.x *= -1;
if (((state->bunnies[i].position.y + BUNNY_H / 2) > ctx.base_draw_h) ||
((state->bunnies[i].position.y + BUNNY_H / 2 - 60) < 0))
if (((state->bunnies[i].position.y + (float)BUNNY_H / 2) > (float)ctx.resolution.y) ||
((state->bunnies[i].position.y + (float)BUNNY_H / 2 - 60) < 0))
state->bunnies[i].speed.y *= -1;
}
handle_input();
// Clear window with Gray color (set the background color this way)
draw_rectangle((Rect){0, 0, ctx.base_draw_w, ctx.base_draw_h}, GRAY);
draw_rectangle((Rect){0, 0, (float)ctx.resolution.x, (float)ctx.resolution.y}, GRAY);
for (int i = 0; i < state->bunniesCount; i++)
{ // Draw each bunny based on their position and color, also scale accordingly
m_sprite(m_set(path, "wabbit_alpha.png"),
m_set(rect, ((Rect){.x = (int)state->bunnies[i].position.x,
.y = (int)state->bunnies[i].position.y,
m_set(rect, ((Rect){.x = state->bunnies[i].position.x,
.y = state->bunnies[i].position.y,
.w = BUNNY_W * SPRITE_SCALE,
.h = BUNNY_H * SPRITE_SCALE})),
m_opt(color, (state->bunnies[i].color)), m_opt(stretch, true), );

View File

@ -19,15 +19,13 @@ static void title_tick(State *state) {
return;
}
m_sprite("/assets/title.png", ((Rect) {
((float)ctx.base_draw_w / 2) - ((float)320 / 2), 64, 320, 128 }));
((float)ctx.resolution.x / 2) - ((float)320 / 2), 64, 320, 128 }));
/* draw the tick count as an example of dynamic text */
size_t text_str_len = snprintf(NULL, 0, "%lu", state->ctx->tick_count) + 1;
size_t text_str_len = snprintf(NULL, 0, "%lu", state->ctx->frame_number) + 1;
char *text_str = cmalloc(text_str_len);
snprintf(text_str, text_str_len, "%lu", state->ctx->tick_count);
snprintf(text_str, text_str_len, "%lu", state->ctx->frame_number);
const char *font = "fonts/kenney-pixel.ttf";
int text_h = 32;

View File

@ -17,8 +17,8 @@ static void ingame_tick(State *state) {
if (input_is_mouse_captured()) {
const float sensitivity = 0.6f; /* TODO: put this in a better place */
scn->yaw += (float)ctx.mouse_relative_position.x * sensitivity;
scn->pitch -= (float)ctx.mouse_relative_position.y * sensitivity;
scn->yaw += (float)ctx.mouse_movement.x * sensitivity;
scn->pitch -= (float)ctx.mouse_movement.y * sensitivity;
scn->pitch = clampf(scn->pitch, -89.0f, 89.0f);
const float yaw_rad = scn->yaw * (float)DEG2RAD;
@ -62,8 +62,8 @@ static void ingame_tick(State *state) {
for (int ly = 64; ly--;) {
for (int lx = 64; lx--;) {
float x = SDL_truncf(scn->cam.pos.x + 32 - lx);
float y = SDL_truncf(scn->cam.pos.z + 32 - ly);
float x = SDL_truncf(scn->cam.pos.x + 32 - (float)lx);
float y = SDL_truncf(scn->cam.pos.z + 32 - (float)ly);
float d0 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
float d1 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
@ -89,7 +89,7 @@ static void ingame_tick(State *state) {
}
draw_skybox("/assets/miramar/miramar_*.tga");
draw_fog(0.9, 1.0, 0.05, (Color){ 140, 147, 160, 255 });
draw_fog(0.9f, 1.0f, 0.05f, (Color){ 140, 147, 160, 255 });
}

View File

@ -16,15 +16,13 @@ static void title_tick(State *state) {
return;
}
m_sprite("/assets/title.png", ((Rect) {
((float)ctx.base_draw_w / 2) - ((float)320 / 2), 64, 320, 128 }));
((float)ctx.resolution.x / 2) - ((float)320 / 2), 64, 320, 128 }));
/* draw the tick count as an example of dynamic text */
size_t text_str_len = snprintf(NULL, 0, "%lu", state->ctx->tick_count) + 1;
size_t text_str_len = snprintf(NULL, 0, "%lu", state->ctx->frame_number) + 1;
char *text_str = cmalloc(text_str_len);
snprintf(text_str, text_str_len, "%lu", state->ctx->tick_count);
snprintf(text_str, text_str_len, "%lu", state->ctx->frame_number);
const char *font = "/fonts/kenney-pixel.ttf";
int text_h = 32;
@ -39,6 +37,7 @@ static void title_tick(State *state) {
},
(Color) { 0, 0, 0, 255 }
);
draw_text(text_str, (Vec2){ 0, 0 }, text_h, (Color) { 255, 255, 255, 255 }, font);
free(text_str);