This commit is contained in:
mibi88 2024-07-27 15:20:02 +02:00
parent 685e4b8824
commit 47cd6dc351
5 changed files with 127 additions and 2 deletions

View file

@ -27,6 +27,7 @@ set(SOURCES
src/game.c src/game.c
src/dialogs.c src/dialogs.c
src/npc.c src/npc.c
src/events.c
# ... # ...
) )
# Shared assets, fx-9860G-only assets and fx-CG-50-only assets # Shared assets, fx-9860G-only assets and fx-CG-50-only assets

View file

@ -8,6 +8,7 @@
#include "config.h" #include "config.h"
#include "game.h" #include "game.h"
#include "npc.h" #include "npc.h"
#include "events.h"
#define BOX_HEIGHT (F_HEIGHT/PXSIZE+8) #define BOX_HEIGHT (F_HEIGHT/PXSIZE+8)

123
src/events.c Normal file
View file

@ -0,0 +1,123 @@
#include <stdlib.h>
#include <string.h>
#include "events.h"
void events_init_handler(EventHandler *handler) {
handler->vars = 0;
}
int events_bind_variable(EventHandler *handler, int *var, char *name) {
if(handler->vars < MAX_VARIABLES){
handler->variables[handler->vars] = var;
handler->var_names[handler->vars++] = name;
}else{
return 1;
}
return 0;
}
char op_chars[OP_AMOUNT+1] = " =+-/*%";
int _op_null(int a, int b) {
return 0;
}
int _op_set(int a, int b) {
return b;
}
int _op_add(int a, int b) {
return a+b;
}
int _op_sub(int a, int b) {
return a-b;
}
int _op_div(int a, int b) {
if(b == 0) return 0;
return a/b;
}
int _op_mul(int a, int b) {
return a*b;
}
int _op_mod(int a, int b) {
if(b == 0) return 0;
return a%b;
}
int (*_operations[OP_AMOUNT])(int, int) = {
_op_null,
_op_set,
_op_add,
_op_sub,
_op_div,
_op_mul,
_op_mod
};
#define MIN(a, b) a < b ? a : b
char _message_buffer[MESSAGE_BUFFER_SZ];
char *events_parse_string(EventHandler *handler, char *message) {
size_t message_pos = 0;
char in_token = 0;
char var_name[TOKEN_MAX_SZ];
size_t name_pos = 0;
Operation var_op = OP_NULL;
char num[TOKEN_MAX_SZ];
size_t num_pos = 0;
Token tok_type = T_NULL;
char c;
size_t i, n;
int *var;
for(i=0;i<strlen(message);i++){
c = message[i];
if(c == '`'){
in_token = !in_token;
if(!in_token){
if(tok_type == T_VAR_EDIT){
/* Do the calculation */
var_name[MIN(name_pos, TOKEN_MAX_SZ)] = '\0';
num[MIN(num_pos, TOKEN_MAX_SZ)] = '\0';
for(n=0;n<handler->vars;n++){
if(!strcmp(var_name, handler->var_names[n])){
var = handler->variables[n];
if(var_op){
*var = _operations[var_op](*var, atoi(num));
}
break;
}
}
}
/* Reset everything */
tok_type = T_NULL;
name_pos = 0;
var_op = OP_NULL;
num_pos = 0;
}
}else if(!in_token){
if(message_pos < TOKEN_MAX_SZ) _message_buffer[message_pos++] = c;
}
if(in_token && c != ' '){
if(tok_type == T_VAR_EDIT){
if(var_op != OP_NULL){
if(num_pos < TOKEN_MAX_SZ) num[num_pos++] = c;
}
if(strchr(op_chars, c)){
var_op = (Operation)(strchr(op_chars, c)-op_chars);
}
if(var_op == OP_NULL){
if(name_pos < TOKEN_MAX_SZ) var_name[name_pos++] = c;
}
}
if(c == '$'){
tok_type = T_VAR_EDIT;
}
}
}
_message_buffer[MIN(message_pos, MESSAGE_BUFFER_SZ)] = '\0';
return _message_buffer;
}

View file

@ -94,7 +94,6 @@ void game_draw(Game *game) {
map_render_by_layer(game, FOREGROUND); map_render_by_layer(game, FOREGROUND);
game_render_indicator(game); game_render_indicator(game);
dprint(8, 8, C_BLACK, "Lifes: %d", game->player.life); dprint(8, 8, C_BLACK, "Lifes: %d", game->player.life);
dprint(8, 16, C_BLACK, "Mana: %d", game->mana);
} }
/* Key management */ /* Key management */

View file

@ -5,7 +5,7 @@
#include <gint/display.h> #include <gint/display.h>
#include <stdint.h> #include <stdint.h>
#include "events.h"
/* The direction where the player is going to. */ /* The direction where the player is going to. */
typedef enum { typedef enum {
@ -132,6 +132,7 @@ typedef struct {
typedef struct { typedef struct {
Map *map_level; /* The level that the player is currently playing */ Map *map_level; /* The level that the player is currently playing */
Player player; /* The player data (see player.h). */ Player player; /* The player data (see player.h). */
EventHandler handler;
/* Some global variables */ /* Some global variables */
/* Set to true when asked for exit */ /* Set to true when asked for exit */
bool exittoOS; bool exittoOS;