i can't sleep fuck

This commit is contained in:
KikooDX 2021-11-10 06:15:41 +01:00
parent 0644801b73
commit 6e8041a926
9 changed files with 135 additions and 5 deletions

View File

@ -15,6 +15,7 @@ set(SOURCES
src/main.c
src/input.c
src/level.c
src/player.c
)
set(LEVELS

View File

@ -1,3 +1,6 @@
#pragma once
#define TILE_SIZE 16
#define TARGET_FPS 60
#define TILE_SIZE 16
#define PLAYER_WIDTH 12
#define PLAYER_HEIGHT 12

View File

@ -1,4 +1,6 @@
#pragma once
#include "tile.h"
#include "vec.h"
struct LevelBin {
unsigned char format;
@ -11,9 +13,11 @@ struct LevelBin {
struct Level {
int width;
int height;
int size;
char *data;
};
struct Level level_load(struct LevelBin *restrict);
void level_free(struct Level *restrict);
void level_draw(struct Level *restrict);
struct Vec level_find(struct Level *restrict, enum Tile);

12
inc/player.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include "vec.h"
struct Player {
struct Vec pos;
struct VecF spd;
struct VecF rem;
};
struct Player player_new(struct Vec pos);
void player_update(struct Player *restrict);
void player_draw(struct Player *restrict);

3
inc/tile.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
enum Tile { TILE_VOID, TILE_SOLID };

19
inc/vec.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
struct Vec {
int x;
int y;
};
#define VEC(x, y) \
(struct Vec) { x, y }
#define VECZ VEC(0, 0)
struct VecF {
float x;
float y;
};
#define VECF(x, y) \
(struct VecF) { x, y }
#define VECFZ VECF(0.0, 0.0)

View File

@ -1,5 +1,7 @@
#include "level.h"
#include "conf.h"
#include "tile.h"
#include "vec.h"
#include <gint/display.h>
#include <stdlib.h>
@ -10,6 +12,7 @@ level_load(struct LevelBin *restrict s)
int i = s->width * s->height;
level.width = s->width;
level.height = s->height;
level.size = i;
level.data = malloc(i);
while (i-- > 0) {
level.data[i] = s->data[i];
@ -32,7 +35,7 @@ level_draw(struct Level *restrict s)
int x = 0;
int y = 0;
for (i = 0; i < s->width * s->height; i++) {
for (i = 0; i < s->size; i++) {
const int sx = x * TILE_SIZE;
const int sy = y * TILE_SIZE;
const int tile = s->data[i];
@ -48,3 +51,17 @@ level_draw(struct Level *restrict s)
}
}
}
struct Vec
level_find(struct Level *restrict s, enum Tile seek)
{
int i;
for (i = 0; i < s->size; i++) {
const enum Tile tile = s->data[i];
if (tile == seek) {
return VEC(i % s->width * TILE_SIZE,
(int)(i / s->height) * TILE_SIZE);
}
}
return VEC(0, 0);
}

View File

@ -1,30 +1,73 @@
#include "conf.h"
#include "input.h"
#include "level.h"
#include "player.h"
#include "vec.h"
#include <gint/cpu.h>
#include <gint/display.h>
#include <gint/timer.h>
extern struct LevelBin lvl_test;
struct Level level;
struct Player player;
static void update(void);
static void draw(void);
static int callback(volatile int *);
int
main(void)
{
int timer;
int frameskip = 1;
volatile int has_ticked = 0;
timer = timer_configure(TIMER_ANY, 1000000 / TARGET_FPS,
GINT_CALL(callback, &has_ticked));
timer_start(timer);
input_init();
level = level_load(&lvl_test);
player = player_new(VECZ);
draw();
while (!input_pressed(K_EXIT))
input_update();
while (!input_pressed(K_EXIT)) {
int i;
for (i = 0; i < frameskip; i++) {
if (has_ticked > frameskip) {
frameskip = has_ticked;
}
while (!has_ticked)
sleep();
while (has_ticked)
has_ticked = 0;
update();
}
draw();
}
timer_stop(timer);
level_free(&level);
return 1;
}
static void
update(void)
{
input_update();
player_update(&player);
}
static void
draw(void)
{
dclear(C_BLACK);
level_draw(&level);
player_draw(&player);
dupdate();
}
static int
callback(volatile int *arg)
{
*arg += 1;
return 0;
}

28
src/player.c Normal file
View File

@ -0,0 +1,28 @@
#include "player.h"
#include "conf.h"
#include "vec.h"
#include <gint/display.h>
struct Player
player_new(struct Vec pos)
{
struct Player p;
p.pos = pos;
p.spd = VECFZ;
p.rem = VECFZ;
return p;
}
void
player_update(struct Player *restrict p)
{
p->pos.x++;
}
void
player_draw(struct Player *restrict p)
{
const int x2 = p->pos.x + PLAYER_WIDTH - 1;
const int y2 = p->pos.y + PLAYER_HEIGHT - 1;
drect(p->pos.x, p->pos.y, x2, y2, C_BLUE);
}