Lazy integers, Zig-like typing.

This commit is contained in:
KikooDX 2021-03-04 10:38:50 +01:00
parent bb8164a07e
commit 74d31e5744
11 changed files with 84 additions and 60 deletions

View File

@ -5,8 +5,8 @@
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "lazyint.h"
#define KEYS_COUNT 6
enum {
@ -24,8 +24,8 @@ enum {
#define S_UP 3
typedef struct Input {
uint8_t keys[KEYS_COUNT];
uint8_t states[KEYS_COUNT];
u8 keys[KEYS_COUNT];
u8 states[KEYS_COUNT];
} Input;
/* Check for new key inputs and update accordingly. */
@ -35,7 +35,7 @@ void input_update(Input *input);
void input_init(Input *input);
/* Get state of keys. */
bool input_is_pressed(Input input, uint8_t key);
bool input_is_down(Input input, uint8_t key);
bool input_is_released(Input input, uint8_t key);
bool input_is_up(Input input, uint8_t key);
bool input_is_pressed(Input input, u8 key);
bool input_is_down(Input input, u8 key);
bool input_is_released(Input input, u8 key);
bool input_is_up(Input input, u8 key);

22
include/lazyint.h Normal file
View File

@ -0,0 +1,22 @@
/* SPDX-License-Identifier: MIT
* Copyright (c) 2021 KikooDX
* This file is part of
* [Painfull Success CG](https://git.sr.ht/~kikoodx/painfull-success-cg),
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#pragma once
#include <stdint.h>
/* This file represents my laziness in all it's glory. */
typedef int8_t i8;
typedef uint8_t u8;
typedef int16_t i16;
typedef uint16_t u16;
typedef int32_t i32;
typedef uint32_t u32;
typedef int64_t i64;
typedef uint64_t u64;
typedef float f32;
typedef double f64;
typedef long double f80; /* Do you hate it? Me too. */

View File

@ -5,7 +5,6 @@
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#pragma once
#include <stdint.h>
#include "conf.h"
#include "tiles.h"
#include "vec2.h"

View File

@ -5,8 +5,8 @@
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "lazyint.h"
#include "vec2.h"
#include "level.h"
#include "input.h"
@ -29,21 +29,21 @@
typedef struct Player{
Vec2 pos;
float spd_x;
float spd_y;
int8_t facing;
f32 spd_x;
f32 spd_y;
i8 facing;
bool stun;
bool knocked;
uint8_t keys_left;
uint8_t jump_buffer;
uint8_t coyot;
u8 keys_left;
u8 jump_buffer;
u8 coyot;
} Player;
Player player_init();
void player_update(Player *player, Level *level, Input input, uint8_t *level_id);
void player_update(Player *player, Level *level, Input input, u8 *level_id);
void player_draw(Player player);
/* Helper functions. */
bool player_collide(Level level, Vec2 pos, tile_t tile, uint8_t margin);
bool player_move(Player *player, Level level, int8_t spd_x, int8_t spd_y);
int8_t sign(int8_t value);
int8_t round(float value);
bool player_collide(Level level, Vec2 pos, tile_t tile, u8 margin);
bool player_move(Player *player, Level level, i8 spd_x, i8 spd_y);
i8 sign(i8 value);
i8 round(f32 value);

View File

@ -5,8 +5,9 @@
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#pragma once
#include "lazyint.h"
typedef uint8_t tile_t;
typedef u8 tile_t;
enum {
AIR_TILE = 0,
SOLID_TILE,
@ -19,4 +20,4 @@ enum {
};
#define OUT_OF_BOUNDS AIR_TILE
int tile_color(tile_t tile);
u32 tile_color(tile_t tile);

View File

@ -6,8 +6,9 @@
* included in all copies and substantial portions of the software. */
#pragma once
#include <stdbool.h>
#include "lazyint.h"
typedef int16_t vec2_int_t;
typedef i16 vec2_int_t;
typedef struct Vec2{
vec2_int_t x;
vec2_int_t y;

View File

@ -5,16 +5,16 @@
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#include <gint/keyboard.h>
#include <stdint.h>
#include "lazyint.h"
#include "input.h"
void input_update(Input *input) {
/* Read full input stream. */
clearevents();
/* For each key, update state. */
for (uint8_t i = 0; i < KEYS_COUNT; ++i) {
uint8_t *state = &input->states[i];
const uint8_t key = input->keys[i];
for (u8 i = 0; i < KEYS_COUNT; ++i) {
u8 *state = &input->states[i];
const u8 key = input->keys[i];
/* See if the key is pressed. */
const bool pressed = keydown(key);
/* Update input status. */
@ -41,24 +41,24 @@ void input_init(Input *input) {
input->keys[K_JUMP] = KEY_SHIFT;
input->keys[K_RESTART] = KEY_6;
input->keys[K_EXIT] = KEY_EXIT;
for (uint8_t i = 0; i < KEYS_COUNT; ++i)
for (u8 i = 0; i < KEYS_COUNT; ++i)
input->states[i] = S_UP;
}
bool input_is_pressed(Input input, uint8_t key) {
bool input_is_pressed(Input input, u8 key) {
return input.states[key] == S_PRESSED;
}
bool input_is_down(Input input, uint8_t key) {
bool input_is_down(Input input, u8 key) {
return ((input.states[key] == S_DOWN) ||
(input.states[key] == S_PRESSED));
}
bool input_is_released(Input input, uint8_t key) {
bool input_is_released(Input input, u8 key) {
return input.states[key] == S_RELEASED;
}
bool input_is_up(Input input, uint8_t key) {
bool input_is_up(Input input, u8 key) {
return ((input.states[key] == S_UP) ||
(input.states[key] == S_RELEASED));
}

View File

@ -5,7 +5,7 @@
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#include <gint/display.h>
#include <stdint.h>
#include "lazyint.h"
#include "level.h"
#include "tiles.h"
#include "conf.h"
@ -13,15 +13,15 @@
void level_draw(Level level) {
/* Pixel position (where we draw). */
uint16_t x = DRAW_OFFSET_X;
uint16_t y = DRAW_OFFSET_Y;
u16 x = DRAW_OFFSET_X;
u16 y = DRAW_OFFSET_Y;
/* Cursor position. */
uint8_t cx = 0;
u8 cx = 0;
while (cx < LEVEL_WIDTH) {
uint8_t cy = 0;
u8 cy = 0;
while (cy < LEVEL_HEIGHT) {
const tile_t tile = level.content[cy * LEVEL_WIDTH + cx];
const int color = tile_color(tile);
const u32 color = tile_color(tile);
drect(x, y, x + TILE_SIZE - 1, y + TILE_SIZE - 1, color);
y += TILE_SIZE;
cy += 1;
@ -37,8 +37,8 @@ tile_t level_get_tile_at_px(Level level, Vec2 pos) {
if (pos.x < 0 || pos.y < 0)
return OUT_OF_BOUNDS;
/* Convert to unsigned integers. */
uint8_t x = pos.x / TILE_SIZE;
uint8_t y = pos.y / TILE_SIZE;
u8 x = pos.x / TILE_SIZE;
u8 y = pos.y / TILE_SIZE;
/* Out of bounds check. */
if (pos.x >= LEVEL_WIDTH * TILE_SIZE || pos.y >= LEVEL_HEIGHT * TILE_SIZE)
return OUT_OF_BOUNDS;
@ -48,7 +48,7 @@ tile_t level_get_tile_at_px(Level level, Vec2 pos) {
void level_set_tile_at_px(Level *level, Vec2 pos, tile_t tile) {
/* Convert to unsigned integers. */
uint8_t x = pos.x / TILE_SIZE;
uint8_t y = pos.y / TILE_SIZE;
u8 x = pos.x / TILE_SIZE;
u8 y = pos.y / TILE_SIZE;
level->content[x + y * LEVEL_WIDTH] = tile;
}

View File

@ -8,14 +8,14 @@
#include <gint/timer.h>
#include <gint/clock.h>
#include <gint/std/string.h>
#include <stdint.h>
#include <stdbool.h>
#include "lazyint.h"
#include "conf.h"
#include "input.h"
#include "level.h"
#include "player.h"
void load_level(Level *level, Player *player, uint8_t id) {
void load_level(Level *level, Player *player, u8 id) {
extern Level levels[LEVEL_SIZE];
memcpy(level->content, levels[id].content, LEVEL_SIZE);
level->start_pos = levels[id].start_pos;
@ -33,7 +33,7 @@ int main(void) {
Player player = player_init();
/* Initialize level. */
Level level = (Level){};
uint8_t level_id = 0;
u8 level_id = 0;
load_level(&level, &player, level_id);
/* Initialize input. */
Input input = (Input){};
@ -47,7 +47,7 @@ int main(void) {
/* Core loop. */
while (!input_is_down(input, K_EXIT)) {
/* Repeat step event so the UPS is constant. */
for (uint8_t i = 0; i < TARGET_UPS / TARGET_FPS; i += 1) {
for (u8 i = 0; i < TARGET_UPS / TARGET_FPS; i += 1) {
/* UPS control. */
while (!has_ticked) sleep();
has_ticked = false;

View File

@ -5,8 +5,8 @@
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#include <gint/display.h>
#include <stdint.h>
#include <stdbool.h>
#include "lazyint.h"
#include "player.h"
#include "conf.h"
#include "vec2.h"
@ -28,14 +28,14 @@ Player player_init() {
};
}
void player_update(Player *player, Level *level, Input input, uint8_t *level_id) {
void player_update(Player *player, Level *level, Input input, u8 *level_id) {
const Vec2 one_px_down = (Vec2){player->pos.x, player->pos.y + 1};
const bool on_ground =
player_collide(*level, one_px_down, SOLID_TILE, 0) ||
player_collide(*level, one_px_down, SEMI_SOLID_TILE, 0);
if (!player->stun) {
/* Get input. */
const int8_t move_x =
const i8 move_x =
input_is_down(input, K_RIGHT) -
input_is_down(input, K_LEFT);
const bool fastfall = input_is_down(input, K_DOWN);
@ -51,9 +51,9 @@ void player_update(Player *player, Level *level, Input input, uint8_t *level_id)
player->spd_x *= 1 - AIR_FRICTION;
/* Acceleration. */
if (on_ground)
player->spd_x += (float)(move_x) * GROUND_ACCELERATION;
player->spd_x += (f32)(move_x) * GROUND_ACCELERATION;
else
player->spd_x += (float)(move_x) * AIR_ACCELERATION;
player->spd_x += (f32)(move_x) * AIR_ACCELERATION;
/* Move the player. */
player_move(player, *level, round(player->spd_x), 0);
player_move(player, *level, 0, round(player->spd_y));
@ -62,7 +62,7 @@ void player_update(Player *player, Level *level, Input input, uint8_t *level_id)
void player_draw(Player player) {
/* Draw colored rectangle depending on player state. */
int color = 0;
u32 color = 0;
if (player.stun)
color = C_RGB(230/4, 41/4, 55/4); /* Red. */
else
@ -74,7 +74,7 @@ void player_draw(Player player) {
/* Helper functions */
bool player_collide(Level level, Vec2 pos, tile_t tile, uint8_t margin) {
bool player_collide(Level level, Vec2 pos, tile_t tile, u8 margin) {
const vec2_int_t left = pos.x + margin;
const vec2_int_t right = pos.x + PLAYER_WIDTH - 1 - margin * 2;
const vec2_int_t up = pos.y + margin;
@ -85,13 +85,13 @@ bool player_collide(Level level, Vec2 pos, tile_t tile, uint8_t margin) {
(tile == level_get_tile_at_px(level, (Vec2){right, down})));
}
bool player_move(Player *player, Level level, int8_t spd_x, int8_t spd_y) {
bool player_move(Player *player, Level level, i8 spd_x, i8 spd_y) {
player->pos.x += spd_x;
player->pos.y += spd_y;
/* If player is in solid, move back until it ain't. */
if (player_collide(level, player->pos, SOLID_TILE, 0)) {
const uint8_t sign_spd_x = sign(spd_x);
const uint8_t sign_spd_y = sign(spd_y);
const u8 sign_spd_x = sign(spd_x);
const u8 sign_spd_y = sign(spd_y);
if (sign_spd_x != 0) player->spd_x = 0;
if (sign_spd_y != 0) player->spd_y = 0;
while (player_collide(level, player->pos, SOLID_TILE, 0)) {
@ -103,7 +103,7 @@ bool player_move(Player *player, Level level, int8_t spd_x, int8_t spd_y) {
return false;
}
int8_t sign(int8_t value) {
i8 sign(i8 value) {
if (value > 0)
return 1;
else if (value < 0)
@ -112,6 +112,6 @@ int8_t sign(int8_t value) {
return 0;
}
int8_t round(float value) {
return (int8_t)value;
i8 round(f32 value) {
return (i8)value;
}

View File

@ -5,9 +5,10 @@
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#include <gint/display.h>
#include "lazyint.h"
#include "tiles.h"
int tile_color(tile_t tile) {
u32 tile_color(tile_t tile) {
int color = 0;
switch (tile) {
case AIR_TILE: color = C_RGB(0/4, 0/4, 0/4); break;