Commit 2e7fe02b by beoran

Move console from old bbwidget system to zori UI system.

parent e8a4ffc2
......@@ -20,7 +20,7 @@ include(CheckCXXSourceCompiles)
if(CMAKE_COMPILER_IS_GNUCC)
set(COMPILER_GCC 1)
set(CMAKE_C_FLAGS "-W -Wall -Wno-unused -Wno-unknown-pragmas -g3 -gdwarf-2 -std=c99 -ffast-math -fsanitize=address -fstrict-aliasing -Wstrict-aliasing=2")
set(CMAKE_C_FLAGS "-W -Wall -Wno-unused -Wno-unknown-pragmas -g3 -gdwarf-2 -std=c99 -ffast-math -fsanitize=address -fno-omit-frame-pointer -fstrict-aliasing -Wstrict-aliasing=2")
# set(CMAKE_LD_FLAGS "-pg")
# always use gnu99, debugging, all warnings except unused and unknown pragmas.
# when compiling with gnu compiler.
......
......@@ -76,7 +76,6 @@ set(ERUTA_SRC_FILES
src/tr_thing.c
src/ui.c
src/utf8.c
src/widget.c
src/xresor.c
src/zori.c
)
......@@ -43,10 +43,14 @@
#define miao_pop(ARR, DEFAULT) (((ARR)->n > 0) ? miao_unsafe_pop(ARR) : DEFAULT)
#define miao_pop_ptr(ARR) (((ARR)->n > 0) ? miao_unsafe_pop_ptr(ARR) : NULL)
#define miao_unsafe_push(ARR, VAL) ((ARR)->n++, (ARR)->a[(ARR)->n] = (VAL), &(VAL))
#define miao_unsafe_push(ARR, VAL) \
((ARR)->a[(ARR)->n] = (VAL), (ARR)->n++, ((ARR)->a + (ARR)->n))
#define miao_push(ARR, VAL) \
(miao_grow(ARR, ((ARR)->n + 1)) ? miao_unsafe_push(ARR, VAL) : NULL)
( \
(miao_grow(ARR, ((ARR)->n + 1))) ? \
miao_unsafe_push(ARR, VAL) : NULL \
)
#define miao_push_ptr(ARR) \
( \
......
......@@ -12,6 +12,7 @@ typedef struct State_ State;
#include "rh.h"
#include "sprite.h"
#include "spritelist.h"
#include "zori.h"
#define STATE_COLORS 16
#define STATE_BLACK 0
......@@ -35,7 +36,7 @@ ALLEGRO_COLOR
float r , float g , float b , float a );
Ruby * state_ruby (State * state );
BBConsole * state_console (State * state );
struct zori_console * state_console (State * state );
int state_initjoystick (State * self );
State * state_init (State * self , BOOL fullscreen );
BOOL state_done (State * state );
......
#ifndef bbwidget_H_INCLUDED
#define bbwidget_H_INCLUDED
#include "eruta.h"
#include "image.h"
#include "rebox.h"
/* BBBBWidget, Beoran's Bad BBWidget set for allegro. */
/* Custom action types compatible with Allegro events */
enum BBWidgetEventNumbers_ {
BBWIDGET_EVENT_DRAW = AL_ID('B', 'W', 'I', 0),
BBWIDGET_EVENT_UPDATE = AL_ID('B', 'W', 'I', 1),
BBWIDGET_EVENT_DONE = AL_ID('B', 'W', 'I', 2),
BBWIDGET_EVENT_FREE = AL_ID('B', 'W', 'I', 3),
BBWIDGET_EVENT_FOCUS = AL_ID('B', 'W', 'I', 4)
};
/* Style describes the style of a widget. */
struct Style_ {
Color forecolor;
Color backcolor;
Font * font;
Image * background;
};
typedef struct Style_ Style;
/* Predefine BBWidget typedef. */
typedef struct BBWidget_ BBWidget;
/* Predefine BBConsole typedef. */
typedef struct BBConsole_ BBConsole;
/* Predefine widget method table. */
typedef struct BBWidgetMetab_ BBWidgetMetab;
/* Very simple array based event handler. It's O(N) for now,
but N is very small here, so the simplicity of creating a method table
is more important. */
typedef struct BBWidgetAction_ BBWidgetAction;
typedef int BBWidgetHandler(BBWidget * widget, void * data);
struct BBWidgetAction_ {
int type;
BBWidgetHandler * handler;
};
/* BBWidget flags. A widget can be : visible or not
* listening (accepting input) input or not (if it invisble and not listening
* input it's inactive, otherwise active)
* it can have focus or not (no focus listens to input
* but may ignore it, focus accepts input).
* Selected is for checkboxes that are selected or buttons that are down, etc...
*
*/
enum BBWidgetFlags_ {
BBWIDGET_FLAG_VISIBLE = 1,
BBWIDGET_FLAG_LISTENING = 2,
BBWIDGET_FLAG_ACTIVE = 3,
BBWIDGET_FLAG_FOCUSED = 4,
BBWIDGET_FLAG_SELECTED = 8,
};
/* Widget state flags . */
typedef enum BBWidgetFlags_ BBWidgetFlags;
/* Flag testing macros. */
/* Checks if flag is set.
Flag will be evaluated 2 times so must be a constant or a variable that is
only read.
*/
#define BBWIDGET_FLAG_P(BBWIDGET, FLAG) (((BBWIDGET)->flag&(FLAG))==(FLAG))
#define BBWIDGET_VISIBLE_P(BBWIDGET, FLAG) (((BBWIDGET)->flag&(FLAG))==(FLAG))
#define BBWIDGET_HANDLE_OK 0
#define BBWIDGET_HANDLE_IGNORE 1
#define BBWIDGET_HANDLE_ERROR -1
/* BBWidget method cache for commonly used methods */
struct BBWidgetMetab_ {
BBWidgetHandler * free;
BBWidgetHandler * done;
BBWidgetHandler * draw;
BBWidgetHandler * update;
};
/* BBWidget interface */
struct BBWidgetIfa_ {
BBWidget * self;
BBWidgetMetab * metab;
};
/* BBWidgets are individual parts of the UI.
As a simplification, BBWidgets are considered to occupy "panes" ordered
in the user interface from back to front. They do not contain other widgets
and do not have any generic relations between them.
A note on pointer ownership: the pointers to font and image in style
are NOT cleaned up, since style is intended to be mostly a shallow copy in which
font and background image are repeated many times.
*/
struct BBWidget_ {
/* Event handler table. */
BBWidgetAction * acts;
/* Method cache */
BBWidgetMetab metab;
/* Bounds, this is a rectangular box. */
Rebox bounds;
/* Styling elements. */
Style style;
/* BBWidget elements: */
/* Unique ID. */
int id;
/* Flags (active, disabled, etc) */
int flags;
/* Priority of widget */
int z;
};
// BBConsole input handler, called when a line of text (a command)
// is typed.
typedef int (BBConsoleCommand)
(BBConsole * console, const char * command, void * extra);
/* This file was generated with:
'cfunctions -c -aoff -n -w bbwidget_proto src/widget.c' */
#ifndef CFH_BBWIDGET_PROTO
#define CFH_BBWIDGET_PROTO
/* From 'src/widget.c': */
Style style_make (Color fore , Color back , Font * font , Image * background );
Style * style_initstyle (Style * self , Style style );
Style * style_init (Style * self , Color fore , Color back , Font * font , Image * background );
Color style_forecolor (Style * self );
Color style_backcolor (Style * self );
Image * style_background (Style * self );
Font * style_font (Style * self );
Rebox bbwidget_bounds (BBWidget * self );
int bbwidget_w (BBWidget * self );
int bbwidget_h (BBWidget * self );
int bbwidget_x (BBWidget * self );
int bbwidget_y (BBWidget * self );
int bbwidget_z (BBWidget * self );
Color bbwidget_forecolor (BBWidget * self );
Color bbwidget_backcolor (BBWidget * self );
Font * bbwidget_font (BBWidget * self );
Image * bbwidget_background (BBWidget * self );
int bbwidget_flags (BBWidget * self );
int bbwidget_id (BBWidget * self , int id );
int bbwidget_flags_ (BBWidget * self , int flags );
int bbwidget_id_ (BBWidget * self , int id );
int bbwidget_flag (BBWidget * self , int flag );
int bbwidget_unflag (BBWidget * self , int flag );
int bbwidget_doflag (BBWidget * self , int flag , int set );
int bbwidget_flag_p (BBWidget * self , int flag );
int bbwidget_visible (BBWidget * self );
int bbwidget_listening (BBWidget * self );
int bbwidget_active (BBWidget * self );
int bbwidget_focused (BBWidget * self );
int bbwidget_selected (BBWidget * self );
int bbwidget_visible_ (BBWidget * self , int set );
int bbwidget_listening_ (BBWidget * self , int set );
int bbwidget_active_ (BBWidget * self , int set );
int bbwidget_focused_ (BBWidget * self , int set );
int bbwidget_selected_ (BBWidget * self , int set );
BBWidget *
bbwidget_acts_(BBWidget * self, BBWidgetAction * acts);
BBWidget *
bbwidget_initall(BBWidget * self, int id, BBWidgetAction * acts,
Rebox bounds, Style style);
BBWidget *
bbwidget_initbounds(BBWidget * self, int id, BBWidgetAction * acts, Rebox bounds);
BBWidget *
bbwidget_initparent (BBWidget * self , int id , BBWidget * parent );
BBWidget *
bbwidget_allocate(void);
BBWidget *
bbwidget_done (BBWidget * widget );
BBWidget *
bbwidget_free (BBWidget * self );
void bbwidget_draw (BBWidget * self );
void bbwidget_handle (BBWidget * self , ALLEGRO_EVENT * event );
void bbwidget_update (BBWidget * self , ALLEGRO_EVENT * event );
void bbwidget_drawroundframe (BBWidget * self );
int bbconsole_handle(BBWidget * widget, ALLEGRO_EVENT * event);
void
bbconsole_command_(BBConsole * self , BBConsoleCommand * command , void * data );
int bbconsole_docommand (BBConsole * self , const char * text );
int bbconsole_addstr (BBConsole * self , const char * str );
int bbconsole_puts(BBConsole * self , const char * str );
int bbconsole_vprintf(BBConsole * self, const char * format, va_list args);
int bbconsole_printf(BBConsole * self, const char * format, ...);
int bbconsole_draw(BBWidget * widget, void * data);
void bbconsole_active_ (BBConsole * self , int active );
int bbconsole_active (BBConsole * self );
int bbconsole_scroll (BBConsole * self , int direction );
int bbconsole_done(BBWidget * widget, void * data );
int bbconsole_free(BBWidget * widget, void * data );
BBConsole * bbconsole_alloc(void);
BBConsole * bbconsole_initall (BBConsole * self , int id , Rebox bounds , Style style );
BBConsole * bbconsole_new (int id , Rebox bounds , Style style );
#endif /* CFH_BBWIDGET_PROTO */
typedef BBWidget * (BBWidgetDraw)(BBWidget * self);
#endif
......@@ -81,6 +81,7 @@ ALLEGRO_PATH * fifi_data_path_ = NULL;
/* Looks for the data folder in various places, return NULL if not found. */
static ALLEGRO_PATH * fifi_find_data_path(void) {
char * cwd;
ALLEGRO_PATH * path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
// ALLEGRO_RESOURCES_PATH is normally where the program itself is installed.
......@@ -97,6 +98,13 @@ static ALLEGRO_PATH * fifi_find_data_path(void) {
al_append_path_component(path, "data");
if(PATH_EXISTS(path)) return path;
al_destroy_path(path);
/** $CWD /data */
cwd = al_get_current_directory();
path = al_create_path_for_directory(cwd);
al_free(cwd);
al_append_path_component(path, "data");
if(PATH_EXISTS(path)) return path;
al_destroy_path(path);
return NULL;
}
......
......@@ -63,11 +63,11 @@ React * main_react_key_down(React * self, ALLEGRO_KEYBOARD_EVENT * event) {
case ALLEGRO_KEY_F1:
case ALLEGRO_KEY_F3:
/* Toggle the console here. */
bbconsole_active_(state_console(state), !bbconsole_active(state_console(state)));
zori_console_active_(state_console(state), !zori_console_active(state_console(state)));
break;
case ALLEGRO_KEY_F2:
/* Hide the console. */
bbconsole_active_(state_console(state), FALSE);
zori_console_active_(state_console(state), FALSE);
break;
case ALLEGRO_KEY_F5:
/* Reload main script (and hence all other scripts that it loads) on F5 */
......
......@@ -4,7 +4,7 @@
#include "mode.h"
#include "state.h"
#include "react.h"
#include "widget.h"
#include "zori.h"
/** Initializes the react structure so it does nothing at all in all cases. */
React * react_initempty(React * self, void * data) {
......@@ -99,7 +99,7 @@ React * react_react(React * self, ALLEGRO_EVENT * event) {
React * react_poll(React * self, void * state) {
int res;
ALLEGRO_EVENT * event;
BBConsole * console = state_console(state);
struct zori_console * console = state_console(state);
if(!self) return NULL;
// yes an assignment is fine here :)
......@@ -107,8 +107,8 @@ React * react_poll(React * self, void * state) {
/* Let react react first, then if that fails, send to the console,
but only if it is active. If not active, send the event to ruby */
if(!react_react(self, event)) {
if (bbconsole_active(console)) {
bbconsole_handle((BBWidget *)console, event);
if (zori_console_active(console)) {
zori_console_handle(&console->widget, event);
} else {
rh_poll_event(state_ruby(state_get()), event);
}
......
......@@ -90,7 +90,7 @@ struct State_ {
The ruby and error message GUI console.
Implemented in C so it's usable even if there are script bugs.
*/
BBConsole * console;
struct zori_console * console;
/* The current actor, controlled by the player. */
Thing * actor;
......@@ -106,13 +106,13 @@ struct State_ {
static State * global_state_ = NULL;
/** Various loggers. One for stdout if not on windows,
* one to a file and one to the BBConsole */
* one to a file and one to the Console */
int state_console_logf(char * file, int line, char * level,
void * data, char * format, va_list args) {
BBConsole * console = data;
struct zori_console * console = data;
if (console) {
bbconsole_printf(console, "%s %s %d:", level, file, line);
return bbconsole_vprintf(console, format, args);
zori_console_printf(console, "%s %s %d:", level, file, line);
return zori_console_vprintf(console, format, args);
}
return -1;
}
......@@ -231,7 +231,6 @@ void state_free(State * self) {
zori_shutdown();
rh_free(self->ruby);
bbconsole_free((BBWidget *)self->console, NULL);
self->console = NULL; /* disable console immediately. */
/* Deallocate stored objects. */
store_done();
......@@ -283,7 +282,7 @@ Ruby * state_ruby(State * state) {
}
/** Gets console intepreter for state. */
BBConsole * state_console(State * state) {
struct zori_console * state_console(State * state) {
return state->console;
}
......@@ -639,17 +638,20 @@ State * state_init(State * self, BOOL fullscreen) {
/* Set up console. */
{
Style style = { color_rgb(255,255,255), color_rgba(64,0,0, 191),
self->font, NULL};
struct zori_style style;
memset(&style, 0, sizeof(style));
style.text.font = self->font;
style.text.color = color_rgb(255,255,255);
style.back.color = color_rgba(64,0,0, 191);
Rebox bounds = { {20, 20} , {600, 400} };
self->console = bbconsole_new(1, bounds, style);
self->console = zori_console_new(1, &bounds, &style);
if(!self->console) {
return state_errmsg_(self, "Out of memory when allocating console.");
}
}
bbconsole_puts(self->console, "BBConsole started ok!");
zori_console_puts(self->console, "Zori Console started ok!");
// set up ruby callback for console commands
bbconsole_command_(self->console, rh_run_console_command, self->ruby);
zori_console_command_(self->console, rh_run_console_command, self->ruby);
// set up logging to console
monolog_add_logger(self->console, &state_console_logger);
......@@ -758,8 +760,8 @@ void state_draw(State * self) {
10, 10, 0, "FPS: %.0f", state_fps(self));
}
/* Draw the console (will autohide if not active). */
bbwidget_draw((BBWidget *)state_console(self));
/* Draw the ui and the console (will autohide if not active). */
zori_draw_all();
state_scale_display(self);
}
......
......@@ -135,12 +135,36 @@ TEST_FUNC(miao_delete) {
TEST_INTNEQ(0, miao_delete_bsearch(a, foo_compare, &f1));
TEST_INTEQ(4, miao_size(a));
miao_each_with_result(a, foo_each_with_result, f3, foo_check, "foo");
miao_done(a);
TEST_DONE();
}
TEST_FUNC(miao_push) {
struct foo f1, f2 , f3;
struct foo * e1, * e2, *e3;
struct foo_array a[1];
struct foo_array d[1];
miao_init(a);
TEST_NOTNULL(miao_grow(a, 1));
f1.s = "0 zero";
miao_unsafe_push(a, f1);
f1.s = "2 two";
miao_push(a, f1);
f1.s = "1 one";
miao_push(a, f1);
miao_qsort(a, foo_compare);
miao_each_with_result(a, foo_each_with_result, f3, foo_check, "foo");
miao_done(a);
TEST_DONE();
}
int main(void) {
TEST_INIT();
TEST_RUN(miao_push);
TEST_RUN(miao_delete);
TEST_RUN(miao);
TEST_REPORT();
......
......@@ -2,7 +2,7 @@
* This is a test for widget in $package$
*/
#include "si_test.h"
#include "widget.h"
// #include "widget.h"
TEST_FUNC(widget) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment