From 5069d8fa90db33fd5f68540eda111f1fcf5f2d25 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Sat, 2 Jul 2005 02:19:37 +0000 Subject: [PATCH] update miniswig to handle multiple inheritance SVN-Revision: 2674 --- src/audio/sound_manager.cpp | 4 +- src/scripting/wrapper.cpp | 589 +++++++++++++++++++++++++++----------- src/scripting/wrapper.hpp | 17 +- src/scripting/wrapper_util.cpp | 51 ---- src/scripting/wrapper_util.hpp | 59 ---- src/sprite/sprite_data.cpp | 2 +- src/video/drawing_context.cpp | 124 ++------ src/video/screen.cpp | 108 ++----- src/video/surface.cpp | 315 ++------------------ src/video/surface.hpp | 24 +- tools/miniswig/create_wrapper.cpp | 270 ++++++++--------- tools/miniswig/create_wrapper.hpp | 9 + tools/miniswig/lexer.ll | 3 +- tools/miniswig/parser.yy | 52 +++- tools/miniswig/tree.hpp | 2 + 15 files changed, 654 insertions(+), 975 deletions(-) diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index cd9d9cdd3..9f21af2b4 100644 --- a/src/audio/sound_manager.cpp +++ b/src/audio/sound_manager.cpp @@ -192,7 +192,7 @@ SoundManager::play_music(const std::string& filename, bool fade) void SoundManager::set_listener_position(const Vector& pos) { - static Uint32 lastticks = SDL_GetTicks(); + static Uint32 lastticks = 0; Uint32 current_ticks = SDL_GetTicks(); if(current_ticks - lastticks < 300) @@ -211,7 +211,7 @@ SoundManager::set_listener_velocity(const Vector& vel) void SoundManager::update() { - static Uint32 lastticks = SDL_GetTicks(); + static Uint32 lastticks = 0; Uint32 current_ticks = SDL_GetTicks(); if(current_ticks - lastticks < 300) diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index 2d51cdb07..bcd440608 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -450,172 +450,423 @@ static int import_wrapper(HSQUIRRELVM v) return 0; } -WrappedFunction supertux_global_functions[] = { - { "display_text_file", &display_text_file_wrapper }, - { "wait", &wait_wrapper }, - { "translate", &translate_wrapper }, - { "import", &import_wrapper }, - { 0, 0 } -}; - -WrappedConstant supertux_int_constants[] = { - { 0, 0} -}; - -WrappedConstant supertux_float_constants[] = { - { 0, 0} -}; - -WrappedConstant supertux_string_constants[] = { - { 0, 0} -}; - -static WrappedFunction supertux_DisplayEffect_methods[] = { - { "fade_out", &DisplayEffect_fade_out_wrapper }, - { "fade_in", &DisplayEffect_fade_in_wrapper }, - { "set_black", &DisplayEffect_set_black_wrapper }, - { "is_black", &DisplayEffect_is_black_wrapper }, -}; - -static WrappedConstant supertux_DisplayEffect_int_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_DisplayEffect_float_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_DisplayEffect_string_consts[] = { - { 0, 0} -}; - -static WrappedFunction supertux_Camera_methods[] = { - { "shake", &Camera_shake_wrapper }, - { "set_pos", &Camera_set_pos_wrapper }, - { "set_mode", &Camera_set_mode_wrapper }, -}; - -static WrappedConstant supertux_Camera_int_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_Camera_float_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_Camera_string_consts[] = { - { 0, 0} -}; - -static WrappedFunction supertux_Level_methods[] = { - { "finish", &Level_finish_wrapper }, - { "spawn", &Level_spawn_wrapper }, - { "flip_vertically", &Level_flip_vertically_wrapper }, -}; - -static WrappedConstant supertux_Level_int_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_Level_float_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_Level_string_consts[] = { - { 0, 0} -}; - -static WrappedFunction supertux_ScriptedObject_methods[] = { - { "set_animation", &ScriptedObject_set_animation_wrapper }, - { "get_animation", &ScriptedObject_get_animation_wrapper }, - { "move", &ScriptedObject_move_wrapper }, - { "set_pos", &ScriptedObject_set_pos_wrapper }, - { "get_pos_x", &ScriptedObject_get_pos_x_wrapper }, - { "get_pos_y", &ScriptedObject_get_pos_y_wrapper }, - { "set_velocity", &ScriptedObject_set_velocity_wrapper }, - { "get_velocity_x", &ScriptedObject_get_velocity_x_wrapper }, - { "get_velocity_y", &ScriptedObject_get_velocity_y_wrapper }, - { "set_visible", &ScriptedObject_set_visible_wrapper }, - { "is_visible", &ScriptedObject_is_visible_wrapper }, - { "get_name", &ScriptedObject_get_name_wrapper }, -}; - -static WrappedConstant supertux_ScriptedObject_int_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_ScriptedObject_float_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_ScriptedObject_string_consts[] = { - { 0, 0} -}; - -static WrappedFunction supertux_Sound_methods[] = { - { "play_music", &Sound_play_music_wrapper }, - { "play", &Sound_play_wrapper }, -}; - -static WrappedConstant supertux_Sound_int_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_Sound_float_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_Sound_string_consts[] = { - { 0, 0} -}; - -static WrappedFunction supertux_Text_methods[] = { - { "set_text", &Text_set_text_wrapper }, - { "set_font", &Text_set_font_wrapper }, - { "fade_in", &Text_fade_in_wrapper }, - { "fade_out", &Text_fade_out_wrapper }, - { "set_visible", &Text_set_visible_wrapper }, -}; - -static WrappedConstant supertux_Text_int_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_Text_float_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_Text_string_consts[] = { - { 0, 0} -}; - -static WrappedFunction supertux_Player_methods[] = { - { "set_bonus", &Player_set_bonus_wrapper }, - { "make_invincible", &Player_make_invincible_wrapper }, - { "add_life", &Player_add_life_wrapper }, - { "add_coins", &Player_add_coins_wrapper }, -}; - -static WrappedConstant supertux_Player_int_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_Player_float_consts[] = { - { 0, 0} -}; - -WrappedConstant supertux_Player_string_consts[] = { - { 0, 0} -}; - -WrappedClass supertux_classes[] = { - { "DisplayEffect", supertux_DisplayEffect_methods, supertux_DisplayEffect_int_consts, supertux_DisplayEffect_float_consts, supertux_DisplayEffect_string_consts }, - { "Camera", supertux_Camera_methods, supertux_Camera_int_consts, supertux_Camera_float_consts, supertux_Camera_string_consts }, - { "Level", supertux_Level_methods, supertux_Level_int_consts, supertux_Level_float_consts, supertux_Level_string_consts }, - { "ScriptedObject", supertux_ScriptedObject_methods, supertux_ScriptedObject_int_consts, supertux_ScriptedObject_float_consts, supertux_ScriptedObject_string_consts }, - { "Sound", supertux_Sound_methods, supertux_Sound_int_consts, supertux_Sound_float_consts, supertux_Sound_string_consts }, - { "Text", supertux_Text_methods, supertux_Text_int_consts, supertux_Text_float_consts, supertux_Text_string_consts }, - { "Player", supertux_Player_methods, supertux_Player_int_consts, supertux_Player_float_consts, supertux_Player_string_consts }, - { 0, 0, 0, 0, 0 } -}; - +void register_supertux_wrapper(HSQUIRRELVM v) +{ + sq_pushroottable(v); + sq_pushstring(v, "display_text_file", -1); + sq_newclosure(v, &display_text_file_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'display_text_file'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "wait", -1); + sq_newclosure(v, &wait_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'wait'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "translate", -1); + sq_newclosure(v, &translate_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'translate'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "import", -1); + sq_newclosure(v, &import_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'import'"; + throw SquirrelError(v, msg.str()); + } + + // Register class DisplayEffect + sq_pushstring(v, "DisplayEffect", -1); + if(sq_newclass(v, SQFalse) < 0) { + std::stringstream msg; + msg << "Couldn't create new class 'DisplayEffect'"; + throw SquirrelError(v, msg.str()); + } + sq_pushstring(v, "fade_out", -1); + sq_newclosure(v, &DisplayEffect_fade_out_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'fade_out'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "fade_in", -1); + sq_newclosure(v, &DisplayEffect_fade_in_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'fade_in'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "set_black", -1); + sq_newclosure(v, &DisplayEffect_set_black_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'set_black'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "is_black", -1); + sq_newclosure(v, &DisplayEffect_is_black_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'is_black'"; + throw SquirrelError(v, msg.str()); + } + + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register class'DisplayEffect'"; + throw SquirrelError(v, msg.str()); + } + + // Register class Camera + sq_pushstring(v, "Camera", -1); + if(sq_newclass(v, SQFalse) < 0) { + std::stringstream msg; + msg << "Couldn't create new class 'Camera'"; + throw SquirrelError(v, msg.str()); + } + sq_pushstring(v, "shake", -1); + sq_newclosure(v, &Camera_shake_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'shake'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "set_pos", -1); + sq_newclosure(v, &Camera_set_pos_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'set_pos'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "set_mode", -1); + sq_newclosure(v, &Camera_set_mode_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'set_mode'"; + throw SquirrelError(v, msg.str()); + } + + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register class'Camera'"; + throw SquirrelError(v, msg.str()); + } + + // Register class Level + sq_pushstring(v, "Level", -1); + if(sq_newclass(v, SQFalse) < 0) { + std::stringstream msg; + msg << "Couldn't create new class 'Level'"; + throw SquirrelError(v, msg.str()); + } + sq_pushstring(v, "finish", -1); + sq_newclosure(v, &Level_finish_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'finish'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "spawn", -1); + sq_newclosure(v, &Level_spawn_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'spawn'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "flip_vertically", -1); + sq_newclosure(v, &Level_flip_vertically_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'flip_vertically'"; + throw SquirrelError(v, msg.str()); + } + + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register class'Level'"; + throw SquirrelError(v, msg.str()); + } + + // Register class ScriptedObject + sq_pushstring(v, "ScriptedObject", -1); + if(sq_newclass(v, SQFalse) < 0) { + std::stringstream msg; + msg << "Couldn't create new class 'ScriptedObject'"; + throw SquirrelError(v, msg.str()); + } + sq_pushstring(v, "set_animation", -1); + sq_newclosure(v, &ScriptedObject_set_animation_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'set_animation'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "get_animation", -1); + sq_newclosure(v, &ScriptedObject_get_animation_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'get_animation'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "move", -1); + sq_newclosure(v, &ScriptedObject_move_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'move'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "set_pos", -1); + sq_newclosure(v, &ScriptedObject_set_pos_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'set_pos'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "get_pos_x", -1); + sq_newclosure(v, &ScriptedObject_get_pos_x_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'get_pos_x'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "get_pos_y", -1); + sq_newclosure(v, &ScriptedObject_get_pos_y_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'get_pos_y'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "set_velocity", -1); + sq_newclosure(v, &ScriptedObject_set_velocity_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'set_velocity'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "get_velocity_x", -1); + sq_newclosure(v, &ScriptedObject_get_velocity_x_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'get_velocity_x'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "get_velocity_y", -1); + sq_newclosure(v, &ScriptedObject_get_velocity_y_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'get_velocity_y'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "set_visible", -1); + sq_newclosure(v, &ScriptedObject_set_visible_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'set_visible'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "is_visible", -1); + sq_newclosure(v, &ScriptedObject_is_visible_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'is_visible'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "get_name", -1); + sq_newclosure(v, &ScriptedObject_get_name_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'get_name'"; + throw SquirrelError(v, msg.str()); + } + + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register class'ScriptedObject'"; + throw SquirrelError(v, msg.str()); + } + + // Register class Sound + sq_pushstring(v, "Sound", -1); + if(sq_newclass(v, SQFalse) < 0) { + std::stringstream msg; + msg << "Couldn't create new class 'Sound'"; + throw SquirrelError(v, msg.str()); + } + sq_pushstring(v, "play_music", -1); + sq_newclosure(v, &Sound_play_music_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'play_music'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "play", -1); + sq_newclosure(v, &Sound_play_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'play'"; + throw SquirrelError(v, msg.str()); + } + + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register class'Sound'"; + throw SquirrelError(v, msg.str()); + } + + // Register class Text + sq_pushstring(v, "Text", -1); + if(sq_newclass(v, SQFalse) < 0) { + std::stringstream msg; + msg << "Couldn't create new class 'Text'"; + throw SquirrelError(v, msg.str()); + } + sq_pushstring(v, "set_text", -1); + sq_newclosure(v, &Text_set_text_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'set_text'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "set_font", -1); + sq_newclosure(v, &Text_set_font_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'set_font'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "fade_in", -1); + sq_newclosure(v, &Text_fade_in_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'fade_in'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "fade_out", -1); + sq_newclosure(v, &Text_fade_out_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'fade_out'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "set_visible", -1); + sq_newclosure(v, &Text_set_visible_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'set_visible'"; + throw SquirrelError(v, msg.str()); + } + + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register class'Text'"; + throw SquirrelError(v, msg.str()); + } + + // Register class A + sq_pushstring(v, "A", -1); + if(sq_newclass(v, SQFalse) < 0) { + std::stringstream msg; + msg << "Couldn't create new class 'A'"; + throw SquirrelError(v, msg.str()); + } + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register class'A'"; + throw SquirrelError(v, msg.str()); + } + + // Register class B + sq_pushstring(v, "B", -1); + sq_pushstring(v, "A", -1); + sq_get(v, -3); + if(sq_newclass(v, SQTrue) < 0) { + std::stringstream msg; + msg << "Couldn't create new class 'B'"; + throw SquirrelError(v, msg.str()); + } + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register class'B'"; + throw SquirrelError(v, msg.str()); + } + + // Register class Player + sq_pushstring(v, "Player", -1); + if(sq_newclass(v, SQFalse) < 0) { + std::stringstream msg; + msg << "Couldn't create new class 'Player'"; + throw SquirrelError(v, msg.str()); + } + sq_pushstring(v, "set_bonus", -1); + sq_newclosure(v, &Player_set_bonus_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'set_bonus'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "make_invincible", -1); + sq_newclosure(v, &Player_make_invincible_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'make_invincible'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "add_life", -1); + sq_newclosure(v, &Player_add_life_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'add_life'"; + throw SquirrelError(v, msg.str()); + } + + sq_pushstring(v, "add_coins", -1); + sq_newclosure(v, &Player_add_coins_wrapper, 0); + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register function'add_coins'"; + throw SquirrelError(v, msg.str()); + } + + if(sq_createslot(v, -3) < 0) { + std::stringstream msg; + msg << "Couldn't register class'Player'"; + throw SquirrelError(v, msg.str()); + } + + sq_pop(v, 1); +} diff --git a/src/scripting/wrapper.hpp b/src/scripting/wrapper.hpp index dffd5ca2f..bc4ebfe1f 100644 --- a/src/scripting/wrapper.hpp +++ b/src/scripting/wrapper.hpp @@ -6,22 +6,9 @@ #ifndef __supertux_WRAPPER_H__ #define __supertux_WRAPPER_H__ -#include "wrapper_util.hpp" +#include -extern WrappedFunction supertux_global_functions[]; -extern WrappedClass supertux_classes[]; -extern WrappedConstant supertux_int_constants[]; -extern WrappedConstant supertux_float_constants[]; -extern WrappedConstant supertux_string_constants[]; - -static inline void register_supertux_wrapper(HSQUIRRELVM v) -{ - register_functions(v, supertux_global_functions); - register_classes(v, supertux_classes); - register_constants(v, supertux_int_constants); - register_constants(v, supertux_float_constants); - register_constants(v, supertux_string_constants); -} +void register_supertux_wrapper(HSQUIRRELVM v); #endif diff --git a/src/scripting/wrapper_util.cpp b/src/scripting/wrapper_util.cpp index e70e392df..8b67d344f 100644 --- a/src/scripting/wrapper_util.cpp +++ b/src/scripting/wrapper_util.cpp @@ -4,57 +4,6 @@ #include #include "wrapper_util.hpp" -static void register_function(HSQUIRRELVM v, SQFUNCTION func, const char* name) -{ - sq_pushstring(v, name, -1); - sq_newclosure(v, func, 0); //create a new function - if(sq_createslot(v, -3) < 0) { - std::stringstream msg; - msg << "Couldn't register function '" << name << "'"; - throw SquirrelError(v, msg.str()); - } -} - -static void _register_functions(HSQUIRRELVM v, WrappedFunction* functions) -{ - for(WrappedFunction* func = functions; func->name != 0; ++func) { - register_function(v, func->f, func->name); - } -} - -static void register_class(HSQUIRRELVM v, WrappedClass* wclass) -{ - sq_pushstring(v, wclass->name, -1); - sq_newclass(v, false); - _register_functions(v, wclass->functions); - _register_constants(v, wclass->int_consts); - _register_constants(v, wclass->float_consts); - _register_constants(v, wclass->string_consts); - - if(sq_createslot(v, -3) < 0) { - std::stringstream msg; - msg << "Couldn't register function '" << wclass->name << "'"; - throw SquirrelError(v, msg.str()); - } -} - -void register_functions(HSQUIRRELVM v, WrappedFunction* functions) -{ - sq_pushroottable(v); - _register_functions(v, functions); - sq_pop(v, 1); -} - -void register_classes(HSQUIRRELVM v, WrappedClass* classes) -{ - sq_pushroottable(v); - for(WrappedClass* wclass = classes; wclass->name != 0; ++wclass) { - register_class(v, wclass); - } - sq_pop(v, 1); -} - - void print_squirrel_stack(HSQUIRRELVM v) { printf("--------------------------------------------------------------\n"); diff --git a/src/scripting/wrapper_util.hpp b/src/scripting/wrapper_util.hpp index 66ac99542..54ca2e987 100644 --- a/src/scripting/wrapper_util.hpp +++ b/src/scripting/wrapper_util.hpp @@ -6,25 +6,6 @@ #include #include -struct WrappedFunction { - const char* name; - SQFUNCTION f; -}; - -template -struct WrappedConstant { - const char* name; - T value; -}; - -struct WrappedClass { - const char* name; - WrappedFunction* functions; - WrappedConstant* int_consts; - WrappedConstant* float_consts; - WrappedConstant* string_consts; -}; - class SquirrelError : public std::exception { public: @@ -36,46 +17,6 @@ private: std::string message; }; -void register_functions(HSQUIRRELVM v, WrappedFunction* functions); -void register_classes(HSQUIRRELVM v, WrappedClass* classes); - -static inline void push_value(HSQUIRRELVM v, int val) -{ - sq_pushinteger(v, val); -} - -static inline void push_value(HSQUIRRELVM v, float val) -{ - sq_pushfloat(v, val); -} - -static inline void push_value(HSQUIRRELVM v, const char* str) -{ - sq_pushstring(v, str, -1); -} - -template -void _register_constants(HSQUIRRELVM v, WrappedConstant* constants) -{ - for(WrappedConstant* c = constants; c->name != 0; ++c) { - sq_pushstring(v, c->name, -1); - push_value(v, c->value); - if(sq_createslot(v, -3) < 0) { - std::stringstream msg; - msg << "Couldn't register int constant '" << c->name << "'"; - throw SquirrelError(v, msg.str()); - } - } -} - -template -void register_constants(HSQUIRRELVM v, WrappedConstant* constants) -{ - sq_pushroottable(v); - _register_constants(v, constants); - sq_pop(v, 1); -} - void print_squirrel_stack(HSQUIRRELVM v); #endif diff --git a/src/sprite/sprite_data.cpp b/src/sprite/sprite_data.cpp index 0088fb925..a4597e5cf 100644 --- a/src/sprite/sprite_data.cpp +++ b/src/sprite/sprite_data.cpp @@ -93,7 +93,7 @@ SpriteData::parse_action(const lisp::Lisp* lisp) for(int i = 0; static_cast(i) < act_tmp->surfaces.size(); i++) { Surface* surface = new Surface(sdl_surface_from_sdl_surface( - act_tmp->surfaces[i]->impl->get_sdl_surface(), true), true); + act_tmp->surfaces[i]->impl->get_sdl_surface()), true); surface->apply_filter(HORIZONTAL_FLIP_FILTER); action->surfaces.push_back(surface); } diff --git a/src/video/drawing_context.cpp b/src/video/drawing_context.cpp index 674880591..7c8369527 100644 --- a/src/video/drawing_context.cpp +++ b/src/video/drawing_context.cpp @@ -202,41 +202,14 @@ DrawingContext::draw_gradient(DrawingRequest& request) const Color& top = gradientrequest->top; const Color& bottom = gradientrequest->bottom; -#ifndef NOOPENGL - if(config->use_gl) - { - glBegin(GL_QUADS); - glColor3ub(top.red, top.green, top.blue); - glVertex2f(0, 0); - glVertex2f(SCREEN_WIDTH, 0); - glColor3ub(bottom.red, bottom.green, bottom.blue); - glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT); - glVertex2f(0, SCREEN_HEIGHT); - glEnd(); - } - else - { -#endif - if(&top == &bottom) - { - fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, top.red, top.green, top.blue); - } - else - { - float redstep = (float(bottom.red)-float(top.red)) / float(SCREEN_HEIGHT); - float greenstep = (float(bottom.green)-float(top.green)) / float(SCREEN_HEIGHT); - float bluestep = (float(bottom.blue) - float(top.blue)) / float(SCREEN_HEIGHT); - - for(float y = 0; y < SCREEN_HEIGHT; y += 2) - fillrect(0, (int)y, SCREEN_WIDTH, 2, - int(float(top.red) + redstep * y), - int(float(top.green) + greenstep * y), - int(float(top.blue) + bluestep * y), 255); - } -#ifndef NOOPENGL - - } -#endif + glBegin(GL_QUADS); + glColor3ub(top.red, top.green, top.blue); + glVertex2f(0, 0); + glVertex2f(SCREEN_WIDTH, 0); + glColor3ub(bottom.red, bottom.green, bottom.blue); + glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT); + glVertex2f(0, SCREEN_HEIGHT); + glEnd(); delete gradientrequest; } @@ -262,66 +235,18 @@ DrawingContext::draw_filled_rect(DrawingRequest& request) float w = fillrectrequest->size.x; float h = fillrectrequest->size.y; -#ifndef NOOPENGL - if(config->use_gl) - { - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glColor4ub(fillrectrequest->color.red, fillrectrequest->color.green, - fillrectrequest->color.blue, fillrectrequest->color.alpha); - - glBegin(GL_POLYGON); - glVertex2f(x, y); - glVertex2f(x+w, y); - glVertex2f(x+w, y+h); - glVertex2f(x, y+h); - glEnd(); - glDisable(GL_BLEND); - } - else - { -#endif - SDL_Rect src, rect; - SDL_Surface *temp = NULL; - - rect.x = (int)x; - rect.y = (int)y; - rect.w = (int)w; - rect.h = (int)h; - - if(fillrectrequest->color.alpha != 255) - { - temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel, - screen->format->Rmask, - screen->format->Gmask, - screen->format->Bmask, - screen->format->Amask); - - - src.x = 0; - src.y = 0; - src.w = rect.w; - src.h = rect.h; - - SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, - fillrectrequest->color.red, fillrectrequest->color.green, - fillrectrequest->color.blue)); - - SDL_SetAlpha(temp, SDL_SRCALPHA, fillrectrequest->color.alpha); - - SDL_BlitSurface(temp,0,screen,&rect); - - SDL_FreeSurface(temp); - } - else - SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, - fillrectrequest->color.red, fillrectrequest->color.green, - fillrectrequest->color.blue)); - -#ifndef NOOPENGL - - } -#endif + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glColor4ub(fillrectrequest->color.red, fillrectrequest->color.green, + fillrectrequest->color.blue, fillrectrequest->color.alpha); + + glBegin(GL_POLYGON); + glVertex2f(x, y); + glVertex2f(x+w, y); + glVertex2f(x+w, y+h); + glVertex2f(x, y+h); + glEnd(); + glDisable(GL_BLEND); delete fillrectrequest; } @@ -366,13 +291,10 @@ DrawingContext::do_drawing() } } - // update screen - if(config->use_gl) - SDL_GL_SwapBuffers(); - else - SDL_Flip(screen); - drawingrequests.clear(); + + // update screen + SDL_GL_SwapBuffers(); } void diff --git a/src/video/screen.cpp b/src/video/screen.cpp index d9b44d68e..f7463f0cc 100644 --- a/src/video/screen.cpp +++ b/src/video/screen.cpp @@ -156,48 +156,17 @@ void fillrect(float x, float y, float w, float h, int r, int g, int b, int a) h = -h; } - if(config->use_gl) { - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glColor4ub(r, g, b,a); - - glBegin(GL_POLYGON); - glVertex2f(x, y); - glVertex2f(x+w, y); - glVertex2f(x+w, y+h); - glVertex2f(x, y+h); - glEnd(); - glDisable(GL_BLEND); - } else { - SDL_Rect src, rect; - SDL_Surface *temp = NULL; - - rect.x = (int)x; - rect.y = (int)y; - rect.w = (int)w; - rect.h = (int)h; - - if(a != 255) { - temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel, - screen->format->Rmask, - screen->format->Gmask, - screen->format->Bmask, - screen->format->Amask); - - - src.x = 0; - src.y = 0; - src.w = rect.w; - src.h = rect.h; - - SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b)); - SDL_SetAlpha(temp, SDL_SRCALPHA, a); - SDL_BlitSurface(temp,0,screen,&rect); - SDL_FreeSurface(temp); - } else { - SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b)); - } - } + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glColor4ub(r, g, b,a); + + glBegin(GL_POLYGON); + glVertex2f(x, y); + glVertex2f(x+w, y); + glVertex2f(x+w, y+h); + glVertex2f(x, y+h); + glEnd(); + glDisable(GL_BLEND); } /* Needed for line calculations */ @@ -207,52 +176,15 @@ void fillrect(float x, float y, float w, float h, int r, int g, int b, int a) void draw_line(float x1, float y1, float x2, float y2, int r, int g, int b, int a) { - if(config->use_gl) { - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glColor4ub(r, g, b,a); - - glBegin(GL_LINES); - glVertex2f(x1, y1); - glVertex2f(x2, y2); - glEnd(); - glDisable(GL_BLEND); - } else { - /* Basic unantialiased Bresenham line algorithm */ - int lg_delta, sh_delta, cycle, lg_step, sh_step; - Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a); - - lg_delta = (int)(x2 - x1); - sh_delta = (int)(y2 - y1); - lg_step = SGN(lg_delta); - lg_delta = ABS(lg_delta); - sh_step = SGN(sh_delta); - sh_delta = ABS(sh_delta); - if (sh_delta < lg_delta) { - cycle = lg_delta >> 1; - while (x1 != x2) { - drawpixel((int)x1, (int)y1, color); - cycle += sh_delta; - if (cycle > lg_delta) { - cycle -= lg_delta; - y1 += sh_step; - } - x1 += lg_step; - } - drawpixel((int)x1, (int)y1, color); - } - cycle = sh_delta >> 1; - while (y1 != y2) { - drawpixel((int)x1, (int)y1, color); - cycle += lg_delta; - if (cycle > sh_delta) { - cycle -= sh_delta; - x1 += lg_step; - } - y1 += sh_step; - } - drawpixel((int)x1, (int)y1, color); - } + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glColor4ub(r, g, b,a); + + glBegin(GL_LINES); + glVertex2f(x1, y1); + glVertex2f(x2, y2); + glEnd(); + glDisable(GL_BLEND); } diff --git a/src/video/surface.cpp b/src/video/surface.cpp index 1446a03ae..312129119 100644 --- a/src/video/surface.cpp +++ b/src/video/surface.cpp @@ -84,27 +84,7 @@ SurfaceData::~SurfaceData() SurfaceImpl* SurfaceData::create() { - if (config->use_gl) - return create_SurfaceOpenGL(); - else - return create_SurfaceSDL(); -} - -SurfaceSDL* -SurfaceData::create_SurfaceSDL() -{ - switch(type) - { - case LOAD: - return new SurfaceSDL(file, use_alpha); - case LOAD_PART: - return new SurfaceSDL(file, x, y, w, h, use_alpha); - case SURFACE: - return new SurfaceSDL(surface, use_alpha); - case GRADIENT: - return new SurfaceSDL(top_gradient, bottom_gradient, w, h); - } - assert(0); + return create_SurfaceOpenGL(); } SurfaceOpenGL* @@ -113,11 +93,11 @@ SurfaceData::create_SurfaceOpenGL() switch(type) { case LOAD: - return new SurfaceOpenGL(file, use_alpha); + return new SurfaceOpenGL(file); case LOAD_PART: - return new SurfaceOpenGL(file, x, y, w, h, use_alpha); + return new SurfaceOpenGL(file, x, y, w, h); case SURFACE: - return new SurfaceOpenGL(surface, use_alpha); + return new SurfaceOpenGL(surface); case GRADIENT: return new SurfaceOpenGL(top_gradient, bottom_gradient, w, h); default: @@ -253,7 +233,7 @@ void apply_filter_to_surface(SDL_Surface* surface, int filter, Color color) { if(filter == HORIZONTAL_FLIP_FILTER) { - SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true); + SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface); SDL_BlitSurface(surface, NULL, sur_copy, NULL); SDL_SetAlpha(sur_copy,0,0); @@ -269,7 +249,7 @@ apply_filter_to_surface(SDL_Surface* surface, int filter, Color color) SDL_FreeSurface(sur_copy); } else if(filter == MASK_FILTER) { - SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface, true); + SDL_Surface* sur_copy = sdl_surface_from_sdl_surface(surface); Uint8 r,g,b,a; @@ -289,7 +269,7 @@ apply_filter_to_surface(SDL_Surface* surface, int filter, Color color) } SDL_Surface* -sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, bool use_alpha) +sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h) { SDL_Rect src; SDL_Surface * sdl_surface; @@ -319,10 +299,7 @@ sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, SDL_SetAlpha(temp,0,0); SDL_BlitSurface(temp, &src, conv, NULL); - if(use_alpha == false && !config->use_gl) - sdl_surface = SDL_DisplayFormat(conv); - else - sdl_surface = SDL_DisplayFormatAlpha(conv); + sdl_surface = SDL_DisplayFormatAlpha(conv); if (sdl_surface == NULL) { std::stringstream msg; @@ -330,9 +307,6 @@ sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, throw std::runtime_error(msg.str()); } - if (use_alpha == false && !config->use_gl) - SDL_SetAlpha(sdl_surface, 0, 0); - SDL_FreeSurface(temp); SDL_FreeSurface(conv); @@ -340,7 +314,7 @@ sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, } SDL_Surface* -sdl_surface_from_file(const std::string& file, bool use_alpha) +sdl_surface_from_file(const std::string& file) { SDL_Surface* sdl_surface; SDL_Surface* temp; @@ -352,10 +326,7 @@ sdl_surface_from_file(const std::string& file, bool use_alpha) throw std::runtime_error(msg.str()); } - if(use_alpha == false && !config->use_gl) - sdl_surface = SDL_DisplayFormat(temp); - else - sdl_surface = SDL_DisplayFormatAlpha(temp); + sdl_surface = SDL_DisplayFormatAlpha(temp); if (sdl_surface == NULL) { std::stringstream msg; @@ -363,55 +334,21 @@ sdl_surface_from_file(const std::string& file, bool use_alpha) throw std::runtime_error(msg.str()); } - if (use_alpha == false && !config->use_gl) - SDL_SetAlpha(sdl_surface, 0, 0); - SDL_FreeSurface(temp); return sdl_surface; } SDL_Surface* -sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha) +sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf) { - SDL_Surface* sdl_surface; -#if 0 - Uint32 saved_flags; - Uint8 saved_alpha; - - /* Save the alpha blending attributes */ - saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); - saved_alpha = sdl_surf->format->alpha; - if ( (saved_flags & SDL_SRCALPHA) - == SDL_SRCALPHA ) - { - SDL_SetAlpha(sdl_surf, 0, 0); - } -#endif - - if(use_alpha == false && !config->use_gl) - sdl_surface = SDL_DisplayFormat(sdl_surf); - else - sdl_surface = SDL_DisplayFormatAlpha(sdl_surf); - -#if 0 - /* Restore the alpha blending attributes */ - if ( (saved_flags & SDL_SRCALPHA) - == SDL_SRCALPHA ) - { - SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha); - } -#endif - - if (sdl_surface == NULL) { + SDL_Surface* sdl_surface = SDL_DisplayFormatAlpha(sdl_surf); + if (sdl_surface == 0) { std::stringstream msg; msg << "Can't convert surface to display format."; throw std::runtime_error(msg.str()); } - if (use_alpha == false && !config->use_gl) - SDL_SetAlpha(sdl_surface, 0, 0); - return sdl_surface; } @@ -467,18 +404,18 @@ SDL_Surface* SurfaceImpl::get_sdl_surface() const return sdl_surface; } -SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, bool use_alpha) +SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf) { - sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha); + sdl_surface = sdl_surface_from_sdl_surface(surf); create_gl(sdl_surface,&gl_texture); w = sdl_surface->w; h = sdl_surface->h; } -SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha) +SurfaceOpenGL::SurfaceOpenGL(const std::string& file) { - sdl_surface = sdl_surface_from_file(file, use_alpha); + sdl_surface = sdl_surface_from_file(file); create_gl(sdl_surface,&gl_texture); w = sdl_surface->w; @@ -486,9 +423,9 @@ SurfaceOpenGL::SurfaceOpenGL(const std::string& file, bool use_alpha) } SurfaceOpenGL::SurfaceOpenGL(const std::string& file_, int x_, int y_, - int w_, int h_, bool use_alpha_) + int w_, int h_) { - sdl_surface = sdl_surface_part_from_file(file_,x_,y_,w_,h_,use_alpha_); + sdl_surface = sdl_surface_part_from_file(file_, x_, y_, w_, h_); create_gl(sdl_surface, &gl_texture); w = sdl_surface->w; @@ -844,217 +781,3 @@ SurfaceOpenGL::apply_filter(int filter, Color color) w = sdl_surface->w; h = sdl_surface->h; } - -SurfaceSDL::SurfaceSDL(SDL_Surface* surf, bool use_alpha) -{ - sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha); - w = sdl_surface->w; - h = sdl_surface->h; -} - -SurfaceSDL::SurfaceSDL(const std::string& file, bool use_alpha) -{ - sdl_surface = sdl_surface_from_file(file, use_alpha); - w = sdl_surface->w; - h = sdl_surface->h; -} - -SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int _w, int _h, - bool use_alpha) -{ - sdl_surface = sdl_surface_part_from_file(file, x, y, _w, _h, use_alpha); - w = sdl_surface->w; - h = sdl_surface->h; -} - -SurfaceSDL::SurfaceSDL(Color top_gradient, Color bottom_gradient, - int _w, int _h) -{ - sdl_surface = sdl_surface_from_gradient(top_gradient, bottom_gradient,_w,_h); - w = sdl_surface->w; - h = sdl_surface->h; -} - -int -SurfaceSDL::draw(float x, float y, Uint8 alpha, Uint32 effect) -{ - SDL_Rect dest; - - dest.x = (int)x; - dest.y = (int)y; - dest.w = w; - dest.h = h; - - if(effect & SEMI_TRANSPARENT) - alpha = 128; - - if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP) - { - // FIXME: this hack is damn slow. Just keep it cause it isn't that used. - for(float sx = 0; sx < w; sx++) - for(float sy = 0; sy < h; sy++) - if(draw_part(sx, sy, x+(w-sx), y+(h-sy), 1, 1, alpha, NONE_EFFECT) == -2) - return -2; - return 0; - } - else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack - { - for(float sy = 0; sy < h; sy++) - if(draw_part(0, sy, x, y+(h-sy), w, 1, alpha, NONE_EFFECT) == -2) - return -2; - return 0; - } - else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack - { - for(float sx = 0; sx < w; sx++) - if(draw_part(sx, 0, x+(w-sx), y, 1, h, alpha, NONE_EFFECT) == -2) - return -2; - return 0; - } - - if(alpha != 255) - { - /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha - to temp sur, blit the temp into the screen */ - /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that - already have an alpha mask yet... */ - - SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags, - sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel, - sdl_surface->format->Rmask, sdl_surface->format->Gmask, - sdl_surface->format->Bmask, - 0); - int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255); - SDL_FillRect(sdl_surface_copy, NULL, colorkey); - SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey); - - - SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL); - SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha); - - int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest); - - SDL_FreeSurface (sdl_surface_copy); - return ret; - } - - int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest); - - return ret; -} - -int -SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect) -{ - SDL_Rect src, dest; - - src.x = (int)sx; - src.y = (int)sy; - src.w = (int)w; - src.h = (int)h; - - dest.x = (int)x; - dest.y = (int)y; - dest.w = (int)w; - dest.h = (int)h; - - if(effect & SEMI_TRANSPARENT) - alpha = 128; - - if(effect & VERTICAL_FLIP & HORIZONTAL_FLIP) - { - // FIXME: this hack is damn slow. Just keep it cause it isn't that used. - for(float sx_ = 0; sx_ < w; sx++) - for(float sy_ = 0; sy_ < h; sy++) - if(draw_part(sx_, sy_, sx+(w-sx_), sy+(h-sy_), 1, 1, alpha, NONE_EFFECT) == -2) - return -2; - return 0; - } - else if(effect & VERTICAL_FLIP) // FIXME: feel free to replace this hack - { - for(float sy_ = sy; sy_ < h; sy_++) - if(draw_part(sx, sy_, x, y+(h-sy_), w, 1, alpha, NONE_EFFECT) == -2) - return -2; - return 0; - } - else if(effect & HORIZONTAL_FLIP) // FIXME: feel free to replace this hack - { - for(float sx_ = 0; sx_ < w; sx_++) - if(draw_part(sx_, 0, sx+(w-sx_), sy, 1, h, alpha, NONE_EFFECT) == -2) - return -2; - return 0; - } - - if(alpha != 255) - { - /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha - to temp sur, blit the temp into the screen */ - /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that - already have an alpha mask, yet... */ - - SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags, - (int)w, (int)h, sdl_surface->format->BitsPerPixel, - sdl_surface->format->Rmask, sdl_surface->format->Gmask, - sdl_surface->format->Bmask, - 0); - int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255); - SDL_FillRect(sdl_surface_copy, NULL, colorkey); - SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey); - - - SDL_BlitSurface(sdl_surface, &src, sdl_surface_copy, NULL); - SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha); - - int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest); - - SDL_FreeSurface (sdl_surface_copy); - return ret; - } - - int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest); - - return ret; -} - -int -SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, Uint32 effect) -{ - SDL_Rect dest; - - dest.x = (int)x; - dest.y = (int)y; - dest.w = (int)sw; - dest.h = (int)sh; - - if(effect & SEMI_TRANSPARENT) - alpha = 128; - - SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags, - sw, sh, sdl_surface->format->BitsPerPixel, - sdl_surface->format->Rmask, sdl_surface->format->Gmask, - sdl_surface->format->Bmask, - 0); - - SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL); - SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest); - - if(alpha != 255) - SDL_SetAlpha(sdl_surface_copy,SDL_SRCALPHA,alpha); - - int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest); - SDL_FreeSurface(sdl_surface_copy); - - return ret; -} - -void -SurfaceSDL::apply_filter(int filter, Color color) -{ - ::apply_filter_to_surface(sdl_surface, filter, color); - - w = sdl_surface->w; - h = sdl_surface->h; -} - -SurfaceSDL::~SurfaceSDL() -{} diff --git a/src/video/surface.hpp b/src/video/surface.hpp index d2cae0c15..ea0b89b9b 100644 --- a/src/video/surface.hpp +++ b/src/video/surface.hpp @@ -30,7 +30,7 @@ #include "video/screen.hpp" void apply_filter_to_surface(SDL_Surface *surface, int filter, int value); -SDL_Surface* sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha); +SDL_Surface* sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf); SDL_Surface* sdl_surface_from_nothing(); class SurfaceImpl; @@ -144,31 +144,15 @@ public: virtual void apply_filter(int filter, Color color = Color(0,0,0)) = 0; }; -class SurfaceSDL : public SurfaceImpl -{ -public: - SurfaceSDL(SDL_Surface* surf, bool use_alpha); - SurfaceSDL(const std::string& file, bool use_alpha); - SurfaceSDL(const std::string& file, int x, int y, int w_, int h_, bool use_alpha); - SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h); - virtual ~SurfaceSDL(); - - int draw(float x, float y, Uint8 alpha, Uint32 effect = NONE_EFFECT); - int draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, Uint32 effect = NONE_EFFECT); - int draw_stretched(float x, float y, int w, int h, Uint8 alpha, Uint32 effect = NONE_EFFECT); - - void apply_filter(int filter, Color color); -}; - class SurfaceOpenGL : public SurfaceImpl { public: GLuint gl_texture; public: - SurfaceOpenGL(SDL_Surface* surf, bool use_alpha); - SurfaceOpenGL(const std::string& file, bool use_alpha); - SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, bool use_alpha); + SurfaceOpenGL(SDL_Surface* surf); + SurfaceOpenGL(const std::string& file); + SurfaceOpenGL(const std::string& file, int x, int y, int w, int h); SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h); virtual ~SurfaceOpenGL(); diff --git a/tools/miniswig/create_wrapper.cpp b/tools/miniswig/create_wrapper.cpp index 5ba700c7a..c39f8d435 100644 --- a/tools/miniswig/create_wrapper.cpp +++ b/tools/miniswig/create_wrapper.cpp @@ -20,27 +20,9 @@ WrapperCreator::create_wrapper(Namespace* ns) << "#ifndef __" << modulename << "_WRAPPER_H__\n" << "#define __" << modulename << "_WRAPPER_H__\n" << "\n" - << "#include \"wrapper_util.hpp\"\n" - << "\n" - << "extern WrappedFunction " << modulename << "_global_functions[];\n" - << "extern WrappedClass " << modulename << "_classes[];\n" - << "extern WrappedConstant " - << modulename << "_int_constants[];\n" - << "extern WrappedConstant " - << modulename << "_float_constants[];\n" - << "extern WrappedConstant " - << modulename << "_string_constants[];\n" + << "#include \n" << "\n" - << "static inline void register_" - << modulename << "_wrapper(HSQUIRRELVM v)\n" - << "{\n" - << " register_functions(v, " - << modulename << "_global_functions);\n" - << " register_classes(v, " << modulename << "_classes);\n" - << " register_constants(v, " << modulename << "_int_constants);\n" - << " register_constants(v, " << modulename << "_float_constants);\n" - << " register_constants(v, "<< modulename << "_string_constants);\n" - << "}\n" + << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n" << "\n" << "#endif\n" << "\n"; @@ -77,180 +59,154 @@ WrapperCreator::create_wrapper(Namespace* ns) create_function_wrapper(0, *i); } - // create function list... - create_function_list(ns); - create_const_lists(ns); + out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n"; + out << "{\n"; + out << ind << "sq_pushroottable(v);\n"; - // create class list... - std::ostringstream classlist; - classlist << "WrappedClass " << modulename << "_classes[] = {\n"; + create_register_constants_code(ns); + create_register_functions_code(ns); + create_register_classes_code(ns); - for(std::vector::iterator i = ns->types.begin(); - i != ns->types.end(); ++i) { - AtomicType* type = *i; - Class* _class = dynamic_cast (type); - if(_class == 0) - continue; - - classlist << ind << "{ \"" << _class->name << "\", " - << modulename << "_" << _class->name << "_methods, " - << modulename << "_" << _class->name << "_int_consts, " - << modulename << "_" << _class->name << "_float_consts, " - << modulename << "_" << _class->name << "_string_consts " - << "},\n"; - - out << "static WrappedFunction " << modulename << "_" - << _class->name << "_methods[] = {\n"; - for(std::vector::iterator i = _class->members.begin(); - i != _class->members.end(); ++i) { - ClassMember* member = *i; - if(member->visibility != ClassMember::PUBLIC) - continue; - Function* function = dynamic_cast (member); - if(!function || function->type == Function::DESTRUCTOR) - continue; + out << ind << "sq_pop(v, 1);\n"; + out << "}\n"; +} - out << ind << "{ \"" << function->name << "\", &" - << _class->name << "_" << function->name << "_wrapper },\n"; - } - out << "};\n" - << "\n"; - create_class_const_lists(_class); - } - classlist << ind << "{ 0, 0, 0, 0, 0 }\n"; - classlist << "};\n"; - out << classlist.str(); - out << "\n"; +void +WrapperCreator::create_register_function_code(Function* function, Class* _class) +{ + if(function->type == Function::DESTRUCTOR) + return; + + out << ind << "sq_pushstring(v, \"" << function->name << "\", -1);\n"; + out << ind << "sq_newclosure(v, &" + << (_class != 0 ? _class->name + "_" : "") << function->name + << "_wrapper, 0);\n"; + create_register_slot_code("function", function->name); + out << "\n"; } void -WrapperCreator::create_function_list(Namespace* ns) +WrapperCreator::create_register_functions_code(Namespace* ns) { - out << "WrappedFunction " << modulename << "_global_functions[] = {\n"; for(std::vector::iterator i = ns->functions.begin(); i != ns->functions.end(); ++i) { Function* function = *i; - out << ind << "{ \"" << function->name << "\", &" - << function->name << "_wrapper },\n"; + create_register_function_code(function, 0); } - out << ind << "{ 0, 0 }\n" - << "};\n" - << "\n"; } void -WrapperCreator::create_const_lists(Namespace* ns) +WrapperCreator::create_register_classes_code(Namespace* ns) { - out << "WrappedConstant " << modulename << "_int_constants[] = {\n"; - for(std::vector::iterator i = ns->fields.begin(); - i != ns->fields.end(); ++i) { - Field* field = *i; - if(!field->has_const_value - || field->type->atomic_type != &BasicType::INT) + for(std::vector::iterator i = ns->types.begin(); + i != ns->types.end(); ++i) { + AtomicType* type = *i; + Class* _class = dynamic_cast (type); + if(_class == 0) continue; - out << ind << "{ \"" << field->name << "\", " - << field->const_int_value << "},\n"; - } - out << ind << "{ 0, 0}\n"; - out << "};\n"; - out << "\n"; - - out << "WrappedConstant " - << modulename << "_float_constants[] = {\n"; - for(std::vector::iterator i = ns->fields.begin(); - i != ns->fields.end(); ++i) { - Field* field = *i; - if(!field->has_const_value - || field->type->atomic_type != &BasicType::FLOAT) + if(_class->super_classes.size() > 0) continue; - out << ind << "{ \"" << field->name << "\", " - << field->const_float_value << "},\n"; - } - out << ind << "{ 0, 0}\n"; - out << "};\n"; - out << "\n"; - out << "WrappedConstant " - << modulename << "_string_constants[] = {\n"; - for(std::vector::iterator i = ns->fields.begin(); - i != ns->fields.end(); ++i) { - Field* field = *i; - if(!field->has_const_value - || field->type->atomic_type != StringType::instance()) - continue; - out << ind << "{ \"" << field->name << "\", " - << field->const_float_value << "},\n"; - } - out << ind << "{ 0, 0}\n"; - out << "};\n"; - out << "\n"; + create_register_class_code(_class); + } } void -WrapperCreator::create_class_const_lists(Class* _class) +WrapperCreator::create_register_class_code(Class* _class) { - out << "static WrappedConstant " - << modulename << "_" << _class->name << "_int_consts[] = {\n"; + out << ind << "// Register class " << _class->name << "\n"; + out << ind << "sq_pushstring(v, \"" + << _class->name << "\", -1);\n"; + + if(_class->super_classes.size() > 0) { + if(_class->super_classes.size() > 1) { + std::stringstream msg; + msg << "Multiple inheritance not supported (at class '" + << _class->name << "')"; + throw std::runtime_error(msg.str()); + } + + out << ind << "sq_pushstring(v, \"" + << _class->super_classes[0]->name << "\", -1);\n"; + out << ind << "sq_get(v, -3);\n"; + } + out << ind << "if(sq_newclass(v, " + << (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse") + << ") < 0) {\n"; + out << ind << ind << "std::stringstream msg;\n"; + out << ind << ind << "msg << \"Couldn't create new class '" + << _class->name << "'\";\n"; + out << ind << ind << "throw SquirrelError(v, msg.str());\n"; + out << ind << "}\n"; + for(std::vector::iterator i = _class->members.begin(); i != _class->members.end(); ++i) { ClassMember* member = *i; if(member->visibility != ClassMember::PUBLIC) continue; + Function* function = dynamic_cast (member); + if(function) { + create_register_function_code(function, _class); + } Field* field = dynamic_cast (member); - if(!field) - continue; - if(!field->has_const_value - || field->type->atomic_type != &BasicType::INT) - continue; - out << ind << "{ \"" << field->name << "\", " - << field->const_int_value << "},\n"; + if(field) { + create_register_constant_code(field); + } } - out << ind << "{ 0, 0}\n"; - out << "};\n"; + + create_register_slot_code("class", _class->name); out << "\n"; - out << "WrappedConstant " - << modulename << "_" << _class->name << "_float_consts[] = {\n"; - for(std::vector::iterator i = _class->members.begin(); - i != _class->members.end(); ++i) { - ClassMember* member = *i; - if(member->visibility != ClassMember::PUBLIC) - continue; - Field* field = dynamic_cast (member); - if(!field) - continue; - if(!field->has_const_value - || field->type->atomic_type != &BasicType::FLOAT) - continue; - out << ind << "{ \"" << field->name << "\", " - << field->const_float_value << "},\n"; + for(std::vector::iterator i = _class->sub_classes.begin(); + i != _class->sub_classes.end(); ++i) { + Class* _class = *i; + create_register_class_code(_class); } - out << ind << "{ 0, 0}\n"; - out << "};\n"; - out << "\n"; +} - out << "WrappedConstant " - << modulename << "_" << _class->name << "_string_consts[] = {\n"; - for(std::vector::iterator i = _class->members.begin(); - i != _class->members.end(); ++i) { - ClassMember* member = *i; - if(member->visibility != ClassMember::PUBLIC) - continue; - Field* field = dynamic_cast (member); - if(!field) - continue; - if(!field->has_const_value - || field->type->atomic_type != StringType::instance()) - continue; - out << ind << "{ \"" << field->name << "\", " - << field->const_float_value << "},\n"; - } - out << ind << "{ 0, 0}\n"; - out << "};\n"; +void +WrapperCreator::create_register_constants_code(Namespace* ns) +{ + for(std::vector::iterator i = ns->fields.begin(); + i != ns->fields.end(); ++i) { + Field* field = *i; + create_register_constant_code(field); + } +} + +void +WrapperCreator::create_register_constant_code(Field* field) +{ + if(!field->has_const_value) + return; + out << ind << "sq_pushstring(v, \"" << field->name << "\", -1);\n"; + if(field->type->atomic_type == &BasicType::INT) { + out << ind << "sq_pushinteger(v, " << field->const_int_value << ")\n"; + } else if(field->type->atomic_type == &BasicType::FLOAT) { + out << ind << "sq_pushfloat(v, " << field->const_float_value << ")\n"; + } else if(field->type->atomic_type == StringType::instance()) { + out << ind << "sq_pushstring(v, \"" + << field->const_string_value << "\", -1);\n"; + } else { + throw std::runtime_error("Constant is not int, float or string"); + } + create_register_slot_code("constant", field->name); out << "\n"; } void +WrapperCreator::create_register_slot_code(const std::string& what, + const std::string& name) +{ + out << ind << "if(sq_createslot(v, -3) < 0) {\n"; + out << ind << ind << "std::stringstream msg;\n"; + out << ind << ind << "msg << \"Couldn't register " << what << "'" + << name << "'\";\n"; + out << ind << ind << "throw SquirrelError(v, msg.str());\n"; + out << ind << "}\n"; +} + +void WrapperCreator::create_function_wrapper(Class* _class, Function* function) { if(function->type == Function::DESTRUCTOR) diff --git a/tools/miniswig/create_wrapper.hpp b/tools/miniswig/create_wrapper.hpp index 06b842204..af5a7ec3f 100644 --- a/tools/miniswig/create_wrapper.hpp +++ b/tools/miniswig/create_wrapper.hpp @@ -21,6 +21,15 @@ public: void create_wrapper(Namespace* ns); private: + void create_register_functions_code(Namespace* ns); + void create_register_function_code(Function* function, Class* _class); + void create_register_classes_code(Namespace* ns); + void create_register_class_code(Class* _class); + void create_register_constant_code(Field* field); + void create_register_constants_code(Namespace* ns); + void create_register_slot_code(const std::string& what, + const std::string& name); + void create_function_list(Namespace* ns); void create_const_lists(Namespace* ns); void create_class_const_lists(Class* _class); diff --git a/tools/miniswig/lexer.ll b/tools/miniswig/lexer.ll index 4cf0850ee..ac699ab5b 100644 --- a/tools/miniswig/lexer.ll +++ b/tools/miniswig/lexer.ll @@ -100,8 +100,9 @@ namespace { return T_NAMESPACE; } ns = current_namespace; // is it a type? yylval->atomic_type = ns->_findType(yytext, search_down); - if(yylval->atomic_type) + if(yylval->atomic_type) { return T_ATOMIC_TYPE; + } // or a namespace? (hack for now...) yylval->_namespace = ns->_findNamespace(yytext, search_down); if(yylval->_namespace) { diff --git a/tools/miniswig/parser.yy b/tools/miniswig/parser.yy index 0de8f6c00..8728b87ad 100644 --- a/tools/miniswig/parser.yy +++ b/tools/miniswig/parser.yy @@ -141,7 +141,7 @@ namespace_member: ; class_declaration: - T_CLASS T_ID '{' + T_CLASS T_ID { current_class = new Class(); current_class->name = $2; @@ -150,12 +150,39 @@ class_declaration: last_docucomment = ""; current_visibility = ClassMember::PROTECTED; } - class_body '}' ';' + superclass_list '{' class_body '}' ';' { $$ = current_class; } ; +superclass_list: + /* empty */ + | ':' superclasses +; + +superclasses: + superclass + | superclasses ',' superclass +; + +superclass: + superclass_visibility type_identifier + { + Class* superclass = dynamic_cast ($2); + if(superclass == 0) + throw ParseError("SuperClass is not a Class type"); + current_class->super_classes.push_back(superclass); + superclass->sub_classes.push_back(current_class); + } +; + +superclass_visibility: + T_PUBLIC + | T_PROTECTED + | T_PRIVATE +; + class_body: /* empty */ | class_body class_body_element ; @@ -225,6 +252,8 @@ field_declaration: { current_field = new Field(); current_field->type = $1; + current_field->docu_comment = last_docucomment; + last_docucomment = ""; current_field->name = $2; free($2); } @@ -260,17 +289,6 @@ maybe_const_initialisation: function_declaration: type T_ID '(' { - /* - current_function = new Function(); - current_function->type = Function::FUNCTION; - current_function->return_type = *($2); - delete $2; - current_function->name = $3; - free($3); - current_function->docu_comment = last_docucomment; - last_docucomment = ""; - */ - current_function = new Function(); current_function->type = Function::FUNCTION; current_function->return_type = *($1); @@ -280,12 +298,17 @@ function_declaration: current_function->docu_comment = last_docucomment; last_docucomment = ""; } - parameter_list ')' abstract_declaration ';' + parameter_list ')' maybe_const abstract_declaration ';' { $$ = current_function; } ; +maybe_const: + /* empty */ + | T_CONST +; + abstract_declaration: /* empty */ | '=' T_INT @@ -384,7 +407,6 @@ atomic_type: type_identifier: T_ATOMIC_TYPE { - // search for type in current compilation unit... $$ = $1; } | namespace_refs "::" T_ATOMIC_TYPE diff --git a/tools/miniswig/tree.hpp b/tools/miniswig/tree.hpp index ca0131bcb..6e200dac5 100644 --- a/tools/miniswig/tree.hpp +++ b/tools/miniswig/tree.hpp @@ -195,6 +195,8 @@ public: } std::vector members; + std::vector super_classes; + std::vector sub_classes; std::string docu_comment; }; -- 2.11.0