From 193442597266e65a860f409b94f55d38ad7d1ee2 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Thu, 29 Oct 2009 15:22:45 +0000 Subject: [PATCH] Added "Brush Factory", allowing you to run a lua script to generate (or alter, untested yet) a brush. Added a simple (and ugly) sample script. This needs updates to the makefile for platforms other than linux. TODO: add a listbox to select the script you want to run. Do we have a simple API to handle filelists yet ? git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1094 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- Makefile | 17 +++++-- brush.h | 2 + buttons.h | 16 +++--- engine.c | 4 ++ engine.h | 2 +- factory.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ factory.h | 1 + init.c | 41 ++++++++------- main.c | 2 +- misc.c | 6 +-- windows.c | 2 +- windows.h | 2 +- 12 files changed, 208 insertions(+), 35 deletions(-) create mode 100644 factory.c create mode 100644 factory.h diff --git a/Makefile b/Makefile index bc092f29..3397ccf4 100644 --- a/Makefile +++ b/Makefile @@ -227,8 +227,8 @@ else # Compiles a regular linux exectutable for the native platform BIN = grafx2 - COPT = -W -Wall -Wdeclaration-after-statement -std=c99 -c -g `sdl-config --cflags` $(TTFCOPT) - LOPT = `sdl-config --libs` -lSDL_image $(TTFLOPT) -lpng + COPT = -W -Wall -Wdeclaration-after-statement -std=c99 -c -g `sdl-config --cflags` $(TTFCOPT) $(LUACOPT) + LOPT = `sdl-config --libs` -lSDL_image $(TTFLOPT) -lpng $(LUALOPT) # Use gcc for compiling. Use ncc to build a callgraph and analyze the code. CC = gcc #CC = nccgen -ncgcc -ncld -ncfabs @@ -253,6 +253,17 @@ else TTFLABEL = endif +#Lua scripting is optional too +ifeq ($(NOLUA),1) + LUACOPT = + LUALOPT = + LUALABEL = -nolua +else + LUACOPT = -D__ENABLE_LUA__ + LUALOPT = -llua5.1 + LUALABEL = +endif + #To disable Joystick emulation of cursor, make NOJOY=1 (for input.o) #This can be necessary to test keyboard cursor code, because an existing #joystick will keep reporting a contradicting position. @@ -267,7 +278,7 @@ endif .PHONY : all debug release clean depend zip version force install uninstall # This is the list of the objects we want to build. Dependancies are built by "make depend" automatically. -OBJ = $(OBJDIR)/main.o $(OBJDIR)/init.o $(OBJDIR)/graph.o $(OBJDIR)/sdlscreen.o $(OBJDIR)/misc.o $(OBJDIR)/special.o $(OBJDIR)/buttons.o $(OBJDIR)/palette.o $(OBJDIR)/help.o $(OBJDIR)/operatio.o $(OBJDIR)/pages.o $(OBJDIR)/loadsave.o $(OBJDIR)/readline.o $(OBJDIR)/engine.o $(OBJDIR)/filesel.o $(OBJDIR)/op_c.o $(OBJDIR)/readini.o $(OBJDIR)/saveini.o $(OBJDIR)/shade.o $(OBJDIR)/keyboard.o $(OBJDIR)/io.o $(OBJDIR)/version.o $(OBJDIR)/text.o $(OBJDIR)/SFont.o $(OBJDIR)/setup.o $(OBJDIR)/pxsimple.o $(OBJDIR)/pxtall.o $(OBJDIR)/pxwide.o $(OBJDIR)/pxdouble.o $(OBJDIR)/pxtriple.o $(OBJDIR)/pxtall2.o $(OBJDIR)/pxwide2.o $(OBJDIR)/pxquad.o $(OBJDIR)/windows.o $(OBJDIR)/brush.o $(OBJDIR)/realpath.o $(OBJDIR)/mountlist.o $(OBJDIR)/input.o $(OBJDIR)/hotkeys.o $(OBJDIR)/transform.o $(OBJDIR)/pversion.o $(PLATFORMOBJ) +OBJ = $(OBJDIR)/main.o $(OBJDIR)/init.o $(OBJDIR)/graph.o $(OBJDIR)/sdlscreen.o $(OBJDIR)/misc.o $(OBJDIR)/special.o $(OBJDIR)/buttons.o $(OBJDIR)/palette.o $(OBJDIR)/help.o $(OBJDIR)/operatio.o $(OBJDIR)/pages.o $(OBJDIR)/loadsave.o $(OBJDIR)/readline.o $(OBJDIR)/engine.o $(OBJDIR)/filesel.o $(OBJDIR)/op_c.o $(OBJDIR)/readini.o $(OBJDIR)/saveini.o $(OBJDIR)/shade.o $(OBJDIR)/keyboard.o $(OBJDIR)/io.o $(OBJDIR)/version.o $(OBJDIR)/text.o $(OBJDIR)/SFont.o $(OBJDIR)/setup.o $(OBJDIR)/pxsimple.o $(OBJDIR)/pxtall.o $(OBJDIR)/pxwide.o $(OBJDIR)/pxdouble.o $(OBJDIR)/pxtriple.o $(OBJDIR)/pxtall2.o $(OBJDIR)/pxwide2.o $(OBJDIR)/pxquad.o $(OBJDIR)/windows.o $(OBJDIR)/brush.o $(OBJDIR)/realpath.o $(OBJDIR)/mountlist.o $(OBJDIR)/input.o $(OBJDIR)/hotkeys.o $(OBJDIR)/transform.o $(OBJDIR)/pversion.o $(OBJDIR)/factory.o $(PLATFORMOBJ) SKIN_FILES = skins/skin_classic.png skins/skin_modern.png skins/font_Classic.png skins/font_Fun.png diff --git a/brush.h b/brush.h index 437c43cc..f7e20080 100644 --- a/brush.h +++ b/brush.h @@ -25,6 +25,8 @@ #ifndef __BRUSH_H_ #define __BRUSH_H_ +#include "struct.h" + /*! Gets the brush from the picture. @param start_x left edge coordinate in the picture diff --git a/buttons.h b/buttons.h index 65db64a1..4088f0a5 100644 --- a/buttons.h +++ b/buttons.h @@ -17,10 +17,10 @@ along with Grafx2; if not, see */ -////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////// ///@file buttons.h /// Almost all the editor actions that are called by the menu are here. -////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////// #ifndef __BOUTONS_H_ #define __BOUTONS_H_ @@ -28,19 +28,21 @@ #include "struct.h" /*! - Displays an error message when there is no more memory for the requested operation. + Displays an error message when there is no more memory for the requested + operation. */ void Message_out_of_memory(void); /*! - Displays the splash screen at program startup. + Displays the splash screen at program startup. */ void Button_Message_initial(void); /*! - Changes brush shape. - This function saves the current brush shape and swith to the default one (single pixel brush) for the filler and the color picker. - These functions don't need (and will not work with) a custom brush. + Changes brush shape. + This function saves the current brush shape and swith to the default one + (single pixel brush) for the filler and the color picker. + These functions don't need (and will not work with) a custom brush. */ void Change_paintbrush_shape(byte shape); diff --git a/engine.c b/engine.c index 80a49c94..63c86154 100644 --- a/engine.c +++ b/engine.c @@ -79,7 +79,11 @@ char * Menu_tooltip[NB_BUTTONS]= "Grad. spheres / ellipses", "Brush grab. / Restore ", "Lasso / Restore brush ", +#ifdef __ENABLE_LUA__ + "Brush effects / factory ", +#else "Brush effects ", +#endif "Drawing modes (effects) ", "Text ", "Magnify mode / Menu ", diff --git a/engine.h b/engine.h index 94e0704a..14b84244 100644 --- a/engine.h +++ b/engine.h @@ -86,7 +86,7 @@ void Window_dropdown_clear_items(T_Dropdown_button * dropdown); T_List_button * Window_set_list_button(T_Special_button * entry_button, T_Scroller_button * scroller, Func_draw_list_item draw_list_item); void Window_redraw_list(T_List_button * list); -byte Window_click_in_rectangle(short start_x,short start_y,short end_x, +byte Window_click_in_rectangle(short start_x, short start_y, short end_x, short end_y); short Wait_click_in_palette(T_Palette_button * button); void Get_color_behind_window(byte * color, byte * click); diff --git a/factory.c b/factory.c new file mode 100644 index 00000000..5eb20489 --- /dev/null +++ b/factory.c @@ -0,0 +1,148 @@ +/* Grafx2 - The Ultimate 256-color bitmap paint program + + Copyright 2009 Adrien Destugues + + Grafx2 is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; version 2 + of the License. + + Grafx2 is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Grafx2; if not, see +*/ + +/*! \file factory.c + * \brief Brush factory - generates brush from lua scripts + * + * The brush factory allows you to generate brushes with Lua code. + */ + +#include "brush.h" +#include "buttons.h" +#include "engine.h" +#include "global.h" +#include "misc.h" +#include "readline.h" +#include "sdlscreen.h" +#include "windows.h" + +#ifdef __ENABLE_LUA__ + +#include +#include + +// Wrapper functions to call C from Lua +int L_PutPixel(lua_State* L) +{ + Pixel_in_brush(lua_tonumber(L, 1), lua_tonumber(L, 2), lua_tonumber(L, 3)); + return 0; // no values returned for lua +} + +int L_GetPixel(lua_State* L) +{ + uint8_t c = Read_pixel_from_brush(lua_tonumber(L, 1), lua_tonumber(L, 2)); + lua_pushinteger(L, c); + return 1; +} + +void Button_Brush_Factory(void) +{ + short clicked_button; + word height; + word width; + char str[5]; + + T_Special_button* width_button; + T_Special_button* height_button; + + Open_window(154, 162, "Brush Factory"); + + Window_set_normal_button(77, 141, 67, 14, "Cancel", 0, 1, KEY_ESC); // 1 + Window_set_normal_button(10, 141, 67, 14, "Run", 0, 1, 0); // 2 + Print_in_window(10, 17, "Width:", MC_Black, MC_Light); + width_button = Window_set_input_button(64, 15, 4); // 3 + Print_in_window(10, 30, "Height:", MC_Black, MC_Light); + height_button = Window_set_input_button(64, 28, 4); // 4 + + width = Paintbrush_width; + Num2str(width, str, 4); + Window_input_content(width_button, str); + + height = Paintbrush_height; + Num2str(height, str, 4); + Window_input_content(height_button, str); + + Update_window_area(0, 0, Window_width, Window_height); + Display_cursor(); + + do { + clicked_button = Window_clicked_button(); + + switch (clicked_button) + { + case 3 : // Largeur + Num2str(width, str, 4); + Readline(65, 16, str, 4, 1); + width = atoi(str); + // On corrige les dimensions + if (width == 0) + { + width = 1; + Num2str(width, str, 4); + Window_input_content(width_button, str); + } + Display_cursor(); + break; + + case 4 : // Height + Num2str(height, str, 4); + Readline(65, 29, str, 4, 1); + height = atoi(str); + // On corrige les dimensions + if (height == 0) + { + height = 1; + Num2str(height, str, 4); + Window_input_content(height_button, str); + } + Display_cursor(); + break; + + default: + break; + } + + } while (clicked_button <= 0 || clicked_button >= 3); + + if (clicked_button == 2) // Run the script + { + lua_State* L = lua_open(); + + Realloc_brush(width, height); + + lua_register(L,"putpixel",L_PutPixel); + lua_register(L,"getpixel",L_GetPixel); + + if (luaL_loadfile(L,"./test.lua") != 0) + Verbose_error_message(lua_tostring(L, 1)); + + lua_pushinteger(L, width); + lua_pushinteger(L, height); + if (lua_pcall(L, 2, 0, 0) != 0) + Verbose_error_message(lua_tostring(L, 1)); + + lua_close(L); + + Change_paintbrush_shape(PAINTBRUSH_SHAPE_COLOR_BRUSH); + } + + Close_window(); + Display_cursor(); +} + +#endif diff --git a/factory.h b/factory.h new file mode 100644 index 00000000..cefd687c --- /dev/null +++ b/factory.h @@ -0,0 +1 @@ +void Button_Brush_Factory(void); diff --git a/init.c b/init.c index 5ea4596c..63fe3f31 100644 --- a/init.c +++ b/init.c @@ -48,26 +48,27 @@ #include #endif +#include "buttons.h" #include "const.h" -#include "struct.h" +#include "errors.h" #include "global.h" #include "graph.h" -#include "buttons.h" -#include "palette.h" -#include "help.h" -#include "operatio.h" -#include "misc.h" -#include "errors.h" -#include "keyboard.h" -#include "io.h" -#include "hotkeys.h" -#include "setup.h" -#include "windows.h" -#include "sdlscreen.h" -#include "mountlist.h" // read_file_system_list -#include "loadsave.h" // Image_emergency_backup #include "init.h" +#include "io.h" +#include "factory.h" +#include "help.h" +#include "hotkeys.h" +#include "keyboard.h" +#include "loadsave.h" // Image_emergency_backup +#include "misc.h" +#include "mountlist.h" // read_file_system_list +#include "operatio.h" +#include "palette.h" +#include "sdlscreen.h" +#include "setup.h" +#include "struct.h" #include "transform.h" +#include "windows.h" char Gui_loading_error_message[512]; @@ -1058,10 +1059,14 @@ void Init_buttons(void) FAMILY_INTERRUPTION); Init_button(BUTTON_BRUSH_EFFECTS, - 106,18, - 16,16, + 106, 18, + 16, 16, BUTTON_SHAPE_RECTANGLE, - Button_Brush_FX,Button_Brush_FX, +#ifdef __ENABLE_LUA__ + Button_Brush_FX, Button_Brush_Factory, +#else + Button_Brush_FX, Button_Brush_FX, +#endif Do_nothing, FAMILY_INSTANT); diff --git a/main.c b/main.c index ba1f8aaa..ecb63bb8 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,4 @@ -/* Grafx1 - The Ultimate 256-color bitmap paint program +/* Grafx2 - The Ultimate 256-color bitmap paint program Copyright 2008 Peter Gordon Copyright 2008 Franck Charlet diff --git a/misc.c b/misc.c index d2911d1f..3d2c4679 100644 --- a/misc.c +++ b/misc.c @@ -206,12 +206,12 @@ void Init_chrono(dword delay) return; } -void Pixel_in_brush (word x,word y,byte color) +void Pixel_in_brush (word x, word y, byte color) { - *(Brush+y*Brush_width+x)=color; + *(Brush + y * Brush_width + x)=color; } -byte Read_pixel_from_brush (word x,word y) +byte Read_pixel_from_brush (word x, word y) { return *(Brush + y * Brush_width + x); } diff --git a/windows.c b/windows.c index 43c87e01..e6da7849 100644 --- a/windows.c +++ b/windows.c @@ -908,7 +908,7 @@ void Warning_message(char * message) } /// Window that shows a big message, and waits for a click on OK -void Verbose_error_message(char * message) +void Verbose_error_message(const char * message) { short clicked_button; int line; diff --git a/windows.h b/windows.h index d87b3bb6..a612807f 100644 --- a/windows.h +++ b/windows.h @@ -70,7 +70,7 @@ void Print_counter(short x,short y,const char * str,byte text_color,byte backgro byte Confirmation_box(char * message); void Warning_message(char * message); -void Verbose_error_message(char * message); +void Verbose_error_message(const char * message); int Requester_window(char* message, int initial_value); void Display_image_limits(void);