From 332a01d53dbadead27190fd9f5bf7b1068fe93b7 Mon Sep 17 00:00:00 2001 From: laochailan Date: Sat, 21 May 2011 18:20:04 +0200 Subject: [PATCH] Config parser config files to be placed at: ~/.taisei/config see config.example. --- .gitignore | 4 ++ config.example | 15 +++++++ src/CMakeLists.txt | 28 +++++++----- src/config.h | 33 ++++++++++++++ src/config.l | 48 ++++++++++++++++++++ src/config.y | 107 +++++++++++++++++++++++++++++++++++++++++++++ src/global.c | 2 + src/global.h | 5 ++- src/stage.c | 57 ++++++++++-------------- 9 files changed, 252 insertions(+), 47 deletions(-) create mode 100644 config.example create mode 100644 src/config.h create mode 100644 src/config.l create mode 100644 src/config.y diff --git a/.gitignore b/.gitignore index f43a0963..769f095b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ build/ +src/parser.c +src/parser.h +src/lexer.h +src/lexer.c .ctagsdb diff --git a/config.example b/config.example new file mode 100644 index 00000000..38cb1c03 --- /dev/null +++ b/config.example @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e297982e..6cf3c6b2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) diff --git a/src/config.h b/src/config.h new file mode 100644 index 00000000..c3419798 --- /dev/null +++ b/src/config.h @@ -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 + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include +#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 \ No newline at end of file diff --git a/src/config.l b/src/config.l new file mode 100644 index 00000000..01936c0d --- /dev/null +++ b/src/config.l @@ -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 + */ + +%{ + #include "parser.h" + #include "config.h" + #include + +%} + +%% + += 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] ; + +%% diff --git a/src/config.y b/src/config.y new file mode 100644 index 00000000..47498c61 --- /dev/null +++ b/src/config.y @@ -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 + */ + +%{ + #include "config.h" + #include + #include + #include + + 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; +} \ No newline at end of file diff --git a/src/global.c b/src/global.c index 714e4b01..610c9adb 100644 --- a/src/global.c +++ b/src/global.c @@ -23,6 +23,8 @@ void init_global() { load_resources(); init_fonts(); init_rtt(); + + parse_config(CONFIG_FILE); } void init_rtt() { diff --git a/src/global.h b/src/global.h index ebc39411..7b33956b 100644 --- a/src/global.h +++ b/src/global.h @@ -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; diff --git a/src/stage.c b/src/stage.c index 78fbd3bc..7ddb5621 100644 --- a/src/stage.c +++ b/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; }