new structure

This commit is contained in:
Masséna Fezard | Nounouille 2021-12-22 16:18:06 +01:00
parent c8850ee774
commit e136461f63
14 changed files with 248 additions and 4 deletions

View File

@ -12,14 +12,12 @@ find_package(Gint 2.1 REQUIRED)
set(SOURCES
src/main.c
src/input.c
src/draw.c
src/player.c
# ...
src/input.c
)
set(ASSETS_cg
assets-cg/tileset.png
# ...
)
fxconv_declare_assets(${ASSETS} ${ASSETS_fx} ${ASSETS_cg} WITH_METADATA)

BIN
assets-cg/icon-sel.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

BIN
assets-cg/icon-uns.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

3
include/conf.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
#define PLAYER_S 12

4
include/draw.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include <gint/display.h>
void draw_rectangle(int c, int x, int y, int w, int h);

31
include/input.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
/* CODE FROM FISFOR */
typedef enum Key {
K_A,
K_B,
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_EXIT,
K_COUNT
} Key;
typedef enum KeyState { KS_UP, KS_RELEASE, KS_DOWN, KS_PRESS } KeyState;
typedef struct Input {
int keys[K_COUNT];
KeyState states[K_COUNT];
} Input;
/* called by main.c once */
void input_init(void);
/* call this to update key states once per frame */
void input_step(void);
int input_down(Key);
int input_pressed(Key);
int input_up(Key);
int input_released(Key);

1
include/main.h Normal file
View File

@ -0,0 +1 @@
#pragma once

11
include/player.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "type.h"
struct Player {
struct Vec2 pos;
struct FVec2 spd;
struct Vec2 spawn;
};
void player_update(void);
void player_draw(void);

9
include/type.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
struct Vec2 {
int x, y;
};
struct FVec2 {
float x, y;
};

9
src/draw.c Normal file
View File

@ -0,0 +1,9 @@
#include <gint/display.h>
void
draw_rectangle(int x, int y, int w, int h, int c)
{
/* draw a rectangle at a given position (x, y) with a given size (w, h)
*/
drect(x, y, x + w - 1, y + h - 1, c);
}

70
src/input.c Normal file
View File

@ -0,0 +1,70 @@
/* CODE FROM FISFOR */
#include "input.h"
#include <gint/keyboard.h>
static Input input;
void
input_init(void)
{
int i = K_COUNT;
while (i-- > 0) {
input.states[i] = KS_UP;
}
input.keys[K_A] = KEY_SHIFT;
input.keys[K_B] = KEY_ALPHA;
input.keys[K_LEFT] = KEY_LEFT;
input.keys[K_RIGHT] = KEY_RIGHT;
input.keys[K_UP] = KEY_UP;
input.keys[K_DOWN] = KEY_DOWN;
input.keys[K_EXIT] = KEY_MENU;
}
void
input_step(void)
{
int i = K_COUNT;
clearevents();
while (i-- > 0) {
const int kdown = keydown(input.keys[i]);
KeyState *const state = &input.states[i];
const KeyState prev_state = input.states[i];
switch (prev_state) {
case KS_UP:
case KS_RELEASE:
*state = (kdown) ? (KS_PRESS) : (KS_UP);
break;
case KS_DOWN:
case KS_PRESS:
*state = (kdown) ? (KS_DOWN) : (KS_RELEASE);
break;
default:
break;
}
}
}
int
input_down(Key k)
{
return input.states[k] == KS_DOWN || input.states[k] == KS_PRESS;
}
int
input_pressed(Key k)
{
return input.states[k] == KS_PRESS;
}
int
input_up(Key k)
{
return !input_down(k);
}
int
input_released(Key k)
{
return input.states[k] == KS_RELEASE;
}

46
src/main.c Normal file
View File

@ -0,0 +1,46 @@
#include "main.h"
#include "draw.h"
#include "input.h"
#include "player.h"
static int running = 1;
static void init(void);
static void update(void);
static void draw(void);
int
main(void)
{
init();
while (running) {
update();
draw();
if (input_pressed(K_EXIT)) {
running = 0;
}
}
return 1;
}
static void
init(void)
{
input_init();
}
static void
update(void)
{
input_step();
player_update();
}
static void
draw(void)
{
dclear(C_WHITE);
player_draw();
dupdate();
}

51
src/player.c Normal file
View File

@ -0,0 +1,51 @@
#include "player.h"
#include "conf.h"
#include "draw.h"
#include "input.h"
static int k_up, k_down, k_left, k_right, k_jump;
static struct Player player;
static void player_get_input(void);
static void player_move(void);
void
player_init(void)
{
player.pos = (struct Vec2){0, 0};
player.spd = (struct FVec2){0.0f, 0.0f};
}
void
player_update(void)
{
player_get_input();
const struct Vec2 mov = (struct Vec2){k_right - k_left, k_down - k_up};
player.spd.x = mov.x;
player.spd.y = mov.y;
player_move();
}
void
player_draw(void)
{
draw_rectangle(player.pos.x, player.pos.y, PLAYER_S, PLAYER_S, C_BLACK);
}
static void
player_get_input(void)
{
k_up = input_down(K_UP);
k_down = input_down(K_DOWN);
k_left = input_down(K_LEFT);
k_right = input_down(K_RIGHT);
k_jump = input_pressed(K_A);
}
static void
player_move(void)
{
player.pos.x += player.spd.x;
player.pos.y += player.spd.y;
}

11
src/type.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
typedef int tile_t;
typedef struct Vec2 {
int x, y;
} Vec2;
typedef struct FVec2 {
float x, y;
} FVec2;