Config parser
config files to be placed at: ~/.taisei/config see config.example.
This commit is contained in:
parent
c811fa76e7
commit
332a01d53d
9 changed files with 252 additions and 47 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1,2 +1,6 @@
|
|||
build/
|
||||
src/parser.c
|
||||
src/parser.h
|
||||
src/lexer.h
|
||||
src/lexer.c
|
||||
.ctagsdb
|
||||
|
|
15
config.example
Normal file
15
config.example
Normal file
|
@ -0,0 +1,15 @@
|
|||
# -- Taisei example config
|
||||
# move to ~/.taisei/config
|
||||
|
||||
# setting default values.
|
||||
# this config would be useless, as there is a built-in default settings fallback
|
||||
|
||||
key_up = up
|
||||
key_down = down
|
||||
key_left = left
|
||||
key_right = right
|
||||
|
||||
key_focus = shift
|
||||
key_shot = z
|
||||
# key_shot = y # if you are qwertz-fag
|
||||
key_bomb = x
|
|
@ -1,3 +1,18 @@
|
|||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR})
|
||||
|
||||
find_package(SDL REQUIRED)
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(OpenAL REQUIRED)
|
||||
find_package(ALUT REQUIRED)
|
||||
find_package(PNG REQUIRED)
|
||||
find_package(SDL_ttf REQUIRED)
|
||||
find_package(BISON)
|
||||
find_package(FLEX)
|
||||
|
||||
BISON_TARGET(cfgparser config.y ${CMAKE_CURRENT_SOURCE_DIR}/parser.c)
|
||||
FLEX_TARGET(cfgscanner config.l ${CMAKE_CURRENT_SOURCE_DIR}/lexer.c)
|
||||
ADD_FLEX_BISON_DEPENDENCY(cfgscanner cfgparser)
|
||||
|
||||
set(SRCs
|
||||
main.c
|
||||
stage.c
|
||||
|
@ -16,16 +31,9 @@ set(SRCs
|
|||
laser.c
|
||||
shader.c
|
||||
dialog.c
|
||||
stages/stage0.c)
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR})
|
||||
|
||||
find_package(SDL REQUIRED)
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(OpenAL REQUIRED)
|
||||
find_package(ALUT REQUIRED)
|
||||
find_package(PNG REQUIRED)
|
||||
find_package(SDL_ttf REQUIRED)
|
||||
stages/stage0.c
|
||||
${BISON_cfgparser_OUTPUTS}
|
||||
${FLEX_cfgscanner_OUTPUTS})
|
||||
|
||||
add_definitions(-DPREFIX="${CMAKE_INSTALL_PREFIX}" -DGL_GLEXT_PROTOTYPES)
|
||||
|
||||
|
|
33
src/config.h
Normal file
33
src/config.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* This software is licensed under the terms of the MIT-License
|
||||
* See COPYING for further information.
|
||||
* ---
|
||||
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
|
||||
*/
|
||||
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
#include <SDL/SDL_keysym.h>
|
||||
#include "parser.h"
|
||||
|
||||
typedef struct Config {
|
||||
int intval[64];
|
||||
} Config;
|
||||
|
||||
extern Config tconfig;
|
||||
|
||||
enum {
|
||||
KEY_UP = 0,
|
||||
KEY_DOWN,
|
||||
KEY_LEFT,
|
||||
KEY_RIGHT,
|
||||
KEY_FOCUS,
|
||||
KEY_SHOT,
|
||||
KEY_BOMB
|
||||
};
|
||||
|
||||
void parse_config();
|
||||
void config_preset();
|
||||
|
||||
#endif
|
48
src/config.l
Normal file
48
src/config.l
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* This software is licensed under the terms of the MIT-License
|
||||
* See COPYING for further information.
|
||||
* ---
|
||||
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
|
||||
*/
|
||||
|
||||
%{
|
||||
#include "parser.h"
|
||||
#include "config.h"
|
||||
#include <SDL/SDL_keysym.h>
|
||||
|
||||
%}
|
||||
|
||||
%%
|
||||
|
||||
= return EQ;
|
||||
|
||||
#.+ ;
|
||||
|
||||
"key_up" { yylval = KEY_UP; return tKEY_UP; }
|
||||
"key_down" { yylval = KEY_DOWN; return tKEY_DOWN; }
|
||||
"key_left" { yylval = KEY_LEFT; return tKEY_LEFT; }
|
||||
"key_right" { yylval = KEY_RIGHT; return tKEY_RIGHT; }
|
||||
|
||||
"key_focus" { yylval = KEY_FOCUS; return tKEY_FOCUS; }
|
||||
|
||||
"key_shot" { yylval = KEY_SHOT; return tKEY_SHOT; }
|
||||
"key_bomb" { yylval = KEY_BOMB; return tKEY_BOMB; }
|
||||
|
||||
"shift" { yylval = SDLK_LSHIFT; return SKEY; }
|
||||
"ctrl" { yylval = SDLK_LCTRL; return SKEY; }
|
||||
"return" { yylval = SDLK_RETURN; return SKEY; }
|
||||
"alt" { yylval = SDLK_LALT; return SKEY; }
|
||||
|
||||
"up" { yylval = SDLK_UP; return SKEY; }
|
||||
"down" { yylval = SDLK_DOWN; return SKEY; }
|
||||
"right" { yylval = SDLK_RIGHT; return SKEY; }
|
||||
"left" { yylval = SDLK_LEFT; return SKEY; }
|
||||
|
||||
|
||||
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
|
||||
[a-zA-Z] { yylval = yytext[0]; return CHAR; }
|
||||
|
||||
\n return LB;
|
||||
[ \t] ;
|
||||
|
||||
%%
|
107
src/config.y
Normal file
107
src/config.y
Normal file
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
* This software is licensed under the terms of the MIT-License
|
||||
* See COPYING for further information.
|
||||
* ---
|
||||
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
|
||||
*/
|
||||
|
||||
%{
|
||||
#include "config.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Config tconfig;
|
||||
int lineno;
|
||||
|
||||
int yywrap() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int yyerror(char *s) {
|
||||
errx(-1, "!- %d: %s", lineno, s);
|
||||
}
|
||||
|
||||
extern FILE *yyin;
|
||||
%}
|
||||
|
||||
%token tKEY_UP
|
||||
%token tKEY_DOWN
|
||||
%token tKEY_LEFT
|
||||
%token tKEY_RIGHT
|
||||
|
||||
%token tKEY_FOCUS
|
||||
|
||||
%token tKEY_SHOT
|
||||
%token tKEY_BOMB
|
||||
|
||||
|
||||
%token SKEY
|
||||
|
||||
%token NUMBER
|
||||
%token CHAR
|
||||
|
||||
%token SEMI
|
||||
%token EQ
|
||||
%token LB
|
||||
|
||||
%%
|
||||
|
||||
file : line file
|
||||
| ;
|
||||
|
||||
line : line nl
|
||||
| key_key EQ key_val {
|
||||
tconfig.intval[$1] = $3;
|
||||
}
|
||||
| nl;
|
||||
|
||||
key_val : SKEY
|
||||
| CHAR;
|
||||
|
||||
key_key : tKEY_UP
|
||||
| tKEY_DOWN
|
||||
| tKEY_LEFT
|
||||
| tKEY_RIGHT
|
||||
| tKEY_FOCUS
|
||||
| tKEY_SHOT
|
||||
| tKEY_BOMB;
|
||||
|
||||
nl : LB { lineno++; };
|
||||
%%
|
||||
|
||||
void parse_config(char *filename) {
|
||||
config_preset();
|
||||
lineno = 1;
|
||||
|
||||
char *buf = malloc(strlen(filename)+strlen(getenv("HOME")+2));
|
||||
|
||||
strcpy(buf, getenv("HOME"));
|
||||
strcat(buf, "/");
|
||||
strcat(buf, filename);
|
||||
|
||||
yyin = fopen(buf, "r");
|
||||
|
||||
printf("parse_config():\n");
|
||||
if(yyin) {
|
||||
yyparse();
|
||||
fclose(yyin);
|
||||
printf("-- parsing complete\n");
|
||||
} else {
|
||||
printf("-- parsing incomplete; falling back to built-in preset\n");
|
||||
warn("problems with parsing %s", buf);
|
||||
}
|
||||
|
||||
free(buf);
|
||||
}
|
||||
|
||||
void config_preset() {
|
||||
tconfig.intval[KEY_UP] = SDLK_UP;
|
||||
tconfig.intval[KEY_DOWN] = SDLK_DOWN;
|
||||
tconfig.intval[KEY_LEFT] = SDLK_LEFT;
|
||||
tconfig.intval[KEY_RIGHT] = SDLK_RIGHT;
|
||||
|
||||
tconfig.intval[KEY_FOCUS] = SDLK_LSHIFT;
|
||||
tconfig.intval[KEY_SHOT] = SDLK_z;
|
||||
tconfig.intval[KEY_BOMB] = SDLK_x;
|
||||
}
|
|
@ -23,6 +23,8 @@ void init_global() {
|
|||
load_resources();
|
||||
init_fonts();
|
||||
init_rtt();
|
||||
|
||||
parse_config(CONFIG_FILE);
|
||||
}
|
||||
|
||||
void init_rtt() {
|
||||
|
|
|
@ -19,7 +19,10 @@
|
|||
#include "shader.h"
|
||||
#include "dialog.h"
|
||||
#include "list.h"
|
||||
#include "config.h"
|
||||
|
||||
#define FILE_PREFIX PREFIX "/share/taisei/"
|
||||
#define CONFIG_FILE ".taisei/config"
|
||||
enum {
|
||||
SCREEN_W = 800,
|
||||
SCREEN_H = 600,
|
||||
|
@ -42,8 +45,6 @@ enum {
|
|||
FPS = 60
|
||||
};
|
||||
|
||||
#define FILE_PREFIX PREFIX "/share/taisei/"
|
||||
|
||||
typedef struct {
|
||||
Player plr;
|
||||
Projectile *projs;
|
||||
|
|
57
src/stage.c
57
src/stage.c
|
@ -19,40 +19,27 @@ void stage_start() {
|
|||
|
||||
void stage_input() {
|
||||
SDL_Event event;
|
||||
memset(&event, 0, sizeof(event));
|
||||
while(SDL_PollEvent(&event)) {
|
||||
int sym = event.key.keysym.sym;
|
||||
if(event.type == SDL_KEYDOWN) {
|
||||
switch(event.key.keysym.sym) {
|
||||
case SDLK_LSHIFT:
|
||||
global.plr.focus = 1;
|
||||
break;
|
||||
case SDLK_y:
|
||||
if(!global.dialog)
|
||||
global.plr.fire = True;
|
||||
else
|
||||
page_dialog(&global.dialog);
|
||||
break;
|
||||
case SDLK_x:
|
||||
if(!global.dialog)
|
||||
plr_bomb(&global.plr);
|
||||
break;
|
||||
case SDLK_ESCAPE:
|
||||
if(sym == tconfig.intval[KEY_FOCUS])
|
||||
global.plr.focus = 1;
|
||||
else if(sym == tconfig.intval[KEY_SHOT]) {
|
||||
if(!global.dialog)
|
||||
global.plr.fire = True;
|
||||
else
|
||||
page_dialog(&global.dialog);
|
||||
} else if(sym == tconfig.intval[KEY_BOMB]) {
|
||||
if(!global.dialog)
|
||||
plr_bomb(&global.plr);
|
||||
} else if(sym == SDLK_ESCAPE) {
|
||||
exit(1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if(event.type == SDL_KEYUP) {
|
||||
switch(event.key.keysym.sym) {
|
||||
case SDLK_LSHIFT:
|
||||
global.plr.focus = -30; // that's for the transparency timer
|
||||
break;
|
||||
case SDLK_y:
|
||||
global.plr.fire = False;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if(sym == tconfig.intval[KEY_FOCUS])
|
||||
global.plr.focus = -30; // that's for the transparency timer
|
||||
else if(sym == tconfig.intval[KEY_SHOT])
|
||||
global.plr.fire = False;
|
||||
} else if(event.type == SDL_QUIT) {
|
||||
global.game_over = 1;
|
||||
}
|
||||
|
@ -64,21 +51,21 @@ void stage_input() {
|
|||
|
||||
global.plr.moving = False;
|
||||
|
||||
if(keys[SDLK_LEFT] && !keys[SDLK_RIGHT]) {
|
||||
if(keys[tconfig.intval[KEY_LEFT]] && !keys[tconfig.intval[KEY_RIGHT]]) {
|
||||
global.plr.moving = True;
|
||||
global.plr.dir = 1;
|
||||
} else if(keys[SDLK_RIGHT] && !keys[SDLK_LEFT]) {
|
||||
} else if(keys[tconfig.intval[KEY_RIGHT]] && !keys[tconfig.intval[KEY_LEFT]]) {
|
||||
global.plr.moving = True;
|
||||
global.plr.dir = 0;
|
||||
}
|
||||
|
||||
if(keys[SDLK_LEFT] && creal(global.plr.pos) - global.plr.ani->w/2 - speed > 0)
|
||||
if(keys[tconfig.intval[KEY_LEFT]] && creal(global.plr.pos) - global.plr.ani->w/2 - speed > 0)
|
||||
global.plr.pos -= speed;
|
||||
if(keys[SDLK_RIGHT] && creal(global.plr.pos) + global.plr.ani->w/2 + speed < VIEWPORT_W)
|
||||
if(keys[tconfig.intval[KEY_RIGHT]] && creal(global.plr.pos) + global.plr.ani->w/2 + speed < VIEWPORT_W)
|
||||
global.plr.pos += speed;
|
||||
if(keys[SDLK_UP] && cimag(global.plr.pos) - global.plr.ani->h/2 - speed > 0)
|
||||
if(keys[tconfig.intval[KEY_UP]] && cimag(global.plr.pos) - global.plr.ani->h/2 - speed > 0)
|
||||
global.plr.pos -= I*speed;
|
||||
if(keys[SDLK_DOWN] && cimag(global.plr.pos) + global.plr.ani->h/2 + speed < VIEWPORT_H)
|
||||
if(keys[tconfig.intval[KEY_DOWN]] && cimag(global.plr.pos) + global.plr.ani->h/2 + speed < VIEWPORT_H)
|
||||
global.plr.pos += I*speed;
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue