From 2d839607791e1bcd56285204764e357986348adf Mon Sep 17 00:00:00 2001 From: Karthik Kumar Viswanathan Date: Wed, 1 Feb 2012 00:31:10 +0530 Subject: [PATCH 1/8] Structure Padding --- include/widgetz/widgetz.h | 27 +++++++++++++++------------ src/theme.c | 1 + 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/include/widgetz/widgetz.h b/include/widgetz/widgetz.h index bbe51a9..3880506 100644 --- a/include/widgetz/widgetz.h +++ b/include/widgetz/widgetz.h @@ -18,12 +18,13 @@ A struct that defines a bunch of rendering functions to render various types of */ typedef struct WZ_THEME { - void (*draw_button)(struct WZ_THEME* theme, float x, float y, float w, float h, ALLEGRO_USTR* text, int style); - void (*draw_box)(struct WZ_THEME* theme, float x, float y, float w, float h, int style); - void (*draw_textbox)(struct WZ_THEME* theme, float x, float y, float w, float h, int halign, int valign, ALLEGRO_USTR* text, int style); - void (*draw_scroll)(struct WZ_THEME* theme, float x, float y, float w, float h, float fraction, int style); - void (*draw_editbox)(struct WZ_THEME* theme, float x, float y, float w, float h, int cursor_pos, ALLEGRO_USTR* text, int style); - void (*draw_image)(struct WZ_THEME* theme, float x, float y, float w, float h, ALLEGRO_BITMAP* image); + int pad; + const void (*draw_button)(struct WZ_THEME* theme, float x, float y, float w, float h, ALLEGRO_USTR* text, int style); + const void (*draw_box)(struct WZ_THEME* theme, float x, float y, float w, float h, int style); + const void (*draw_textbox)(struct WZ_THEME* theme, float x, float y, float w, float h, int halign, int valign, ALLEGRO_USTR* text, int style); + const void (*draw_scroll)(struct WZ_THEME* theme, float x, float y, float w, float h, float fraction, int style); + const void (*draw_editbox)(struct WZ_THEME* theme, float x, float y, float w, float h, int cursor_pos, ALLEGRO_USTR* text, int style); + const void (*draw_image)(struct WZ_THEME* theme, float x, float y, float w, float h, ALLEGRO_BITMAP* image); ALLEGRO_FONT*(*get_font)(struct WZ_THEME* theme, int font_num); } WZ_THEME; @@ -40,12 +41,6 @@ typedef struct WZ_DEF_THEME { WZ_THEME theme; - /* - Variable: font - Font to use for this GUI - */ - ALLEGRO_FONT* font; - /* Variable: color1 The background color @@ -56,6 +51,14 @@ typedef struct WZ_DEF_THEME The foreground color */ ALLEGRO_COLOR color2; + + /* + Variable: font + Font to use for this GUI + */ + ALLEGRO_FONT* font; + int pad[3]; + } WZ_DEF_THEME; typedef struct WZ_SHORTCUT diff --git a/src/theme.c b/src/theme.c index 38e0390..017a854 100644 --- a/src/theme.c +++ b/src/theme.c @@ -426,6 +426,7 @@ The default theme. const WZ_DEF_THEME wz_def_theme = { { + 0, wz_def_draw_button, wz_def_draw_box, wz_def_draw_textbox, From 487501feaf4489e926a976e70d29cccaba008666 Mon Sep 17 00:00:00 2001 From: Karthik Kumar Viswanathan Date: Wed, 1 Feb 2012 23:49:20 +0530 Subject: [PATCH 2/8] Correct Const Declaration --- include/widgetz/widgetz.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/widgetz/widgetz.h b/include/widgetz/widgetz.h index 3880506..b3ef09f 100644 --- a/include/widgetz/widgetz.h +++ b/include/widgetz/widgetz.h @@ -19,12 +19,12 @@ A struct that defines a bunch of rendering functions to render various types of typedef struct WZ_THEME { int pad; - const void (*draw_button)(struct WZ_THEME* theme, float x, float y, float w, float h, ALLEGRO_USTR* text, int style); - const void (*draw_box)(struct WZ_THEME* theme, float x, float y, float w, float h, int style); - const void (*draw_textbox)(struct WZ_THEME* theme, float x, float y, float w, float h, int halign, int valign, ALLEGRO_USTR* text, int style); - const void (*draw_scroll)(struct WZ_THEME* theme, float x, float y, float w, float h, float fraction, int style); - const void (*draw_editbox)(struct WZ_THEME* theme, float x, float y, float w, float h, int cursor_pos, ALLEGRO_USTR* text, int style); - const void (*draw_image)(struct WZ_THEME* theme, float x, float y, float w, float h, ALLEGRO_BITMAP* image); + void (*const draw_button)(struct WZ_THEME* theme, float x, float y, float w, float h, ALLEGRO_USTR* text, int style); + void (*const draw_box)(struct WZ_THEME* theme, float x, float y, float w, float h, int style); + void (*const draw_textbox)(struct WZ_THEME* theme, float x, float y, float w, float h, int halign, int valign, ALLEGRO_USTR* text, int style); + void (*const draw_scroll)(struct WZ_THEME* theme, float x, float y, float w, float h, float fraction, int style); + void (*const draw_editbox)(struct WZ_THEME* theme, float x, float y, float w, float h, int cursor_pos, ALLEGRO_USTR* text, int style); + void (*const draw_image)(struct WZ_THEME* theme, float x, float y, float w, float h, ALLEGRO_BITMAP* image); ALLEGRO_FONT*(*get_font)(struct WZ_THEME* theme, int font_num); } WZ_THEME; From 620dc201b9e4537a99396e10419586dfa33ebc66 Mon Sep 17 00:00:00 2001 From: Karthik Kumar Viswanathan Date: Sun, 19 Feb 2012 23:19:11 +0530 Subject: [PATCH 3/8] Better Library Detection --- .gitignore | 4 ++ CMake/Modules/FindAllegro.cmake | 56 ++++++++++++++++++++ CMake/Modules/FindFreetype.cmake | 89 ++++++++++++++++++++++++++++++++ CMakeLists.txt | 25 +++++++-- example/example.c | 3 +- 5 files changed, 172 insertions(+), 5 deletions(-) create mode 100644 CMake/Modules/FindAllegro.cmake create mode 100644 CMake/Modules/FindFreetype.cmake diff --git a/.gitignore b/.gitignore index da38775..318b14f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ build Build +bin +Bin +deps +Deps diff --git a/CMake/Modules/FindAllegro.cmake b/CMake/Modules/FindAllegro.cmake new file mode 100644 index 0000000..367f4c8 --- /dev/null +++ b/CMake/Modules/FindAllegro.cmake @@ -0,0 +1,56 @@ +# - Try to find Allegro 5.0 +# Once done, this will define +# ALLEGRO_INCLUDE_DIR - allegro.h etc. +# ALLEGRO_LIBRARIES - for linking +# ALLEGRO_FOUND - True if found +# ALLEGRO_DEFINITIONS - compiler switches used for Allegro + +set(ALLEGRO_NAMES allegro allegro_acodec allegro_audio allegro allegro_color allegro_font allegro_image allegro_memfile allegro_primitives allegro_ttf allegro_shader) +if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + list(APPEND ALLEGRO_NAMES allegro_main) +endif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") +set(ALLEGRO_SUFFIX "-5.1") +set(ALLEGRO_VERSION "-5.1.0") +set(ALLEGRO_WIN32_FLAVOR "-mt") + +# use pkg-config +find_package(PkgConfig) +if(PkgConfig_FOUND) +foreach(ALLEGRO_SPECIFIC_LIB ${ALLEGRO_NAMES}) + pkg_check_modules(PC_ALLEGRO_${ALLEGRO_SPECIFIC_LIB} REQUIRED ${ALLEGRO_SPECIFIC_LIB}${ALLEGRO_SUFFIX}) + list(APPEND ALLEGRO_DEFINITIONS ${PC_ALLEGRO_${ALLEGRO_SPECIFIC_LIB}_CFLAGS_OTHER}) + string(COMPARE EQUAL "${PC_ALLEGRO_INCLUDE_DIRS}" "${PC_ALLEGRO_${ALLEGRO_SPECIFIC_LIB}_INCLUDE_DIRS}" INC_ALREADY_ADDED) + string(COMPARE EQUAL "${PC_ALLEGRO_LIBRARY_DIRS}" "${PC_ALLEGRO_${ALLEGRO_SPECIFIC_LIB}_LIBRARY_DIRS}" LIB_ALREADY_ADDED) + if(INC_ALREADY_ADDED) + else(INC_ALREADY_ADDED) + list(APPEND PC_ALLEGRO_INCLUDE_DIRS ${PC_ALLEGRO_${ALLEGRO_SPECIFIC_LIB}_INCLUDE_DIRS}) + endif(INC_ALREADY_ADDED) + if(LIB_ALREADY_ADDED) + else(LIB_ALREADY_ADDED) + list(APPEND PC_ALLEGRO_LIBRARY_DIRS ${PC_ALLEGRO_${ALLEGRO_SPECIFIC_LIB}_LIBRARY_DIRS}) + endif(LIB_ALREADY_ADDED) +endforeach(ALLEGRO_SPECIFIC_LIB) +endif(PkgConfig_FOUND) + +# use pkg-config returns as hints to find include directory +find_path(ALLEGRO_INCLUDE_DIRS allegro5/allegro.h + HINTS ${PC_ALLEGRO_INCLUDEDIR} ${PC_ALLEGRO_INCLUDE_DIRS} "${PROJECT_SOURCE_DIR}/Deps/include" "$ENV{ALLEGRO_HOME}/include" "$ENV{EXTDEV}/include" "$ENV{EPREFIX}/include" "$ENV{PREFIX}/include" "/usr/include" "/usr/local/include" +) + +# use pkg-config returns as hints to find lib directory +foreach(ALLEGRO_SPECIFIC_LIB ${ALLEGRO_NAMES}) + find_library(ALLEGRO_${ALLEGRO_SPECIFIC_LIB}_LIB + NAMES ${ALLEGRO_SPECIFIC_LIB} ${ALLEGRO_SPECIFIC_LIB}${ALLEGRO_SUFFIX} ${ALLEGRO_SPECIFIC_LIB}${ALLEGRO_VERSION} ${ALLEGRO_SPECIFIC_LIB}${ALLEGRO_VERSION}${ALLEGRO_WIN32_FLAVOR} ${ALLEGRO_SPECIFIC_LIB}-static + HINTS ${PC_ALLEGRO_LIBDIR} ${PC_ALLEGRO_LIBRARY_DIRS} "${PROJECT_SOURCE_DIR}/Deps/lib" "$ENV{ALLEGRO_HOME}/lib" "$ENV{EXTDEV}/lib" "$ENV{EXTDEV}/lib/MSVC" "$ENV{EXTDEV}/lib/MinGW" "$ENV{EXTDEV}/lib/BCC" "$ENV{EPREFIX}/lib" "$ENV{PREFIX}/lib" "/usr/lib" "/usr/local/lib" + ) + list(APPEND ALLEGRO_LIBRARY ${ALLEGRO_${ALLEGRO_SPECIFIC_LIB}_LIB}) +endforeach(ALLEGRO_SPECIFIC_LIB) + +set(ALLEGRO_LIBRARIES "${ALLEGRO_LIBRARY}") +set(ALLEGRO_INCLUDE_DIR "${ALLEGRO_INCLUDE_DIRS}") + +include(FindPackageHandleStandardArgs) +# handle QUIETLY and REQUIRED args and set ALLEGRO_FOUND to TRUE if all listed variables are TRUE +find_package_handle_standard_args(Allegro DEFAULT_MSG ALLEGRO_LIBRARY ALLEGRO_INCLUDE_DIRS) + +mark_as_advanced(ALLEGRO_INCLUDE_DIRS ALLEGRO_LIBRARY) diff --git a/CMake/Modules/FindFreetype.cmake b/CMake/Modules/FindFreetype.cmake new file mode 100644 index 0000000..6bb84dd --- /dev/null +++ b/CMake/Modules/FindFreetype.cmake @@ -0,0 +1,89 @@ +#Modified for Convenience from CMake's Existing Detection. Do not modify. + +# - Locate FreeType library +# This module defines +# FREETYPE_LIBRARIES, the library to link against +# FREETYPE_FOUND, if false, do not try to link to FREETYPE +# FREETYPE_INCLUDE_DIRS, where to find headers. +# This is the concatenation of the paths: +# FREETYPE_INCLUDE_DIR_ft2build +# FREETYPE_INCLUDE_DIR_freetype2 +# +# $FREETYPE_DIR is an environment variable that would +# correspond to the ./configure --prefix=$FREETYPE_DIR +# used in building FREETYPE. + +#============================================================================= +# Copyright 2007-2009 Kitware, Inc. +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +# Created by Eric Wing. +# Modifications by Alexander Neundorf. +# This file has been renamed to "FindFreetype.cmake" instead of the correct +# "FindFreeType.cmake" in order to be compatible with the one from KDE4, Alex. + +# Ugh, FreeType seems to use some #include trickery which +# makes this harder than it should be. It looks like they +# put ft2build.h in a common/easier-to-find location which +# then contains a #include to a more specific header in a +# more specific location (#include ). +# Then from there, they need to set a bunch of #define's +# so you can do something like: +# #include FT_FREETYPE_H +# Unfortunately, using CMake's mechanisms like INCLUDE_DIRECTORIES() +# wants explicit full paths and this trickery doesn't work too well. +# I'm going to attempt to cut out the middleman and hope +# everything still works. +FIND_PATH(FREETYPE_INCLUDE_DIR_ft2build ft2build.h + HINTS + $ENV{FREETYPE_DIR}/include + $ENV{FREETYPE_DIR} + PATHS + /usr/local/X11R6/include + /usr/local/X11/include + /usr/freeware/include +) + +FIND_PATH(FREETYPE_INCLUDE_DIR_freetype2 freetype/config/ftheader.h + HINTS + $ENV{FREETYPE_DIR}/include/freetype2 + PATHS + /usr/local/X11R6/include + /usr/local/X11/include + /usr/freeware/include + PATH_SUFFIXES freetype2 +) + +FIND_LIBRARY(FREETYPE_LIBRARY + NAMES freetype libfreetype freetype219 + HINTS + $ENV{FREETYPE_DIR} + PATH_SUFFIXES lib64 lib + PATHS + /usr/local/X11R6 + /usr/local/X11 + /usr/freeware +) + +# set the user variables +IF(FREETYPE_INCLUDE_DIR_ft2build AND FREETYPE_INCLUDE_DIR_freetype2) + SET(FREETYPE_INCLUDE_DIRS "${FREETYPE_INCLUDE_DIR_ft2build};${FREETYPE_INCLUDE_DIR_freetype2}") +ENDIF(FREETYPE_INCLUDE_DIR_ft2build AND FREETYPE_INCLUDE_DIR_freetype2) +SET(FREETYPE_LIBRARIES "${FREETYPE_LIBRARY}") + +# handle the QUIETLY and REQUIRED arguments and set FREETYPE_FOUND to TRUE if +# all listed variables are TRUE +#include(FindPackageHandleStandardArgs) +#FIND_PACKAGE_HANDLE_STANDARD_ARGS(Freetype DEFAULT_MSG FREETYPE_LIBRARY FREETYPE_INCLUDE_DIRS) + + +MARK_AS_ADVANCED(FREETYPE_LIBRARY FREETYPE_INCLUDE_DIR_freetype2 FREETYPE_INCLUDE_DIR_ft2build) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8dd30f9..81029d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,13 +13,30 @@ set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -g") include(FindPkgConfig) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMake/Modules") +find_package(Allegro REQUIRED) +include_directories(${ALLEGRO_INCLUDE_DIR}) +find_package(Freetype REQUIRED) +set(WZ_ALLEGRO_LDFLAGS ${ALLEGRO_LIBRARIES} ${FREETYPE_LIBRARIES}) + if(WIN32) set(EXECUTABLE_TYPE "WIN32") - set(WZ_ALLEGRO_LDFLAGS -lallegro_ttf -lallegro_font -lallegro -lallegro_primitives) -else(WIN32) - pkg_check_modules(WZ_ALLEGRO REQUIRED allegro_primitives-5.0 allegro_ttf-5.0) endif(WIN32) +if(APPLE) + set(WANT_X11 off) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99") + if(IPHONE) + set(WZ_ALLEGRO_LDFLAGS ${WZ_ALLEGRO_LDFLAGS}"-framework CoreGraphics -framework QuartzCore -framework UIKit -framework Foundation -framework CoreFoundation -framework OpenGLES -framework OpenAL -lm") + set(SDKROOT "iphoneos") + set(CMAKE_OSX_SYSROOT "${SDKROOT}") + add_definitions(-DIPHONE) + else(IPHONE) + set(WZ_ALLEGRO_LDFLAGS ${WZ_ALLEGRO_LDFLAGS} "-framework Cocoa -framework QuartzCore -framework Foundation -framework CoreFoundation -framework OpenGL -framework IOKit -framework AudioToolbox -framework OpenAL -lm") + add_definitions(-DAPPLE) + endif(IPHONE) +endif(APPLE) + add_custom_target(example_data ALL DEPENDS ${EXAMPLE_DATA}) foreach(file ${EXAMPLE_DATA}) @@ -40,4 +57,4 @@ install(DIRECTORY "${WIDGETZ_SOURCE_DIR}/include/widgetz" DESTINATION include) install(TARGETS widgetz ARCHIVE DESTINATION lib) else(NOT USE_INTERNAL_LIB) target_link_libraries(widgetz ${ALLEGRO_LIBRARIES}) -endif(NOT USE_INTERNAL_LIB) \ No newline at end of file +endif(NOT USE_INTERNAL_LIB) diff --git a/example/example.c b/example/example.c index 0c44c03..b074fa8 100644 --- a/example/example.c +++ b/example/example.c @@ -69,7 +69,8 @@ int main(int argc, char *argv[]) Define custom theme wz_def_theme is a global vtable defined by the header */ - theme = wz_def_theme; + memset(&theme, 0, sizeof(theme)); + memcpy(&theme, &wz_def_theme, sizeof(theme)); theme.font = font; theme.color1 = al_map_rgba_f(0, 0.3, 0, 1); theme.color2 = al_map_rgba_f(1, 1, 0, 1); From 5d21afd62c1478cac5d81cf30f8dba235f3e4a45 Mon Sep 17 00:00:00 2001 From: Karthik Kumar Viswanathan Date: Sun, 19 Feb 2012 23:53:10 +0530 Subject: [PATCH 4/8] First iPhone Build --- CMakeLists.txt | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 81029d5..239d6e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,7 @@ set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -g") include(FindPkgConfig) +set(ENV{FREETYPE_DIR} Deps) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMake/Modules") find_package(Allegro REQUIRED) include_directories(${ALLEGRO_INCLUDE_DIR}) @@ -27,7 +28,7 @@ if(APPLE) set(WANT_X11 off) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99") if(IPHONE) - set(WZ_ALLEGRO_LDFLAGS ${WZ_ALLEGRO_LDFLAGS}"-framework CoreGraphics -framework QuartzCore -framework UIKit -framework Foundation -framework CoreFoundation -framework OpenGLES -framework OpenAL -lm") + set(WZ_ALLEGRO_LDFLAGS ${WZ_ALLEGRO_LDFLAGS} "-framework CoreGraphics -framework QuartzCore -framework UIKit -framework Foundation -framework CoreFoundation -framework OpenGLES -framework OpenAL -lm") set(SDKROOT "iphoneos") set(CMAKE_OSX_SYSROOT "${SDKROOT}") add_definitions(-DIPHONE) @@ -39,18 +40,36 @@ endif(APPLE) add_custom_target(example_data ALL DEPENDS ${EXAMPLE_DATA}) +if(NOT IPHONE) foreach(file ${EXAMPLE_DATA}) add_custom_command( OUTPUT ${file} - COMMAND "${CMAKE_COMMAND}" -E copy - "${CMAKE_CURRENT_SOURCE_DIR}/${file}" "${file}" - ) + COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_SOURCE_DIR}/${file}" "${file}" + ) endforeach(file) -endif(NOT USE_INTERNAL_LIB) +endif(NOT IPHONE) add_library(widgetz ${SOURCES}) if(NOT USE_INTERNAL_LIB) target_link_libraries(widgetz ${WZ_ALLEGRO_LDFLAGS}) -add_executable(example "example/example.c") +if(IPHONE) + add_executable(example MACOSX_BUNDLE "example/example.c") + set_target_properties(example PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER "org.widgetz.example") + set_target_properties(example PROPERTIES XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer") + set(APP_RES_DIR \${TARGET_BUILD_DIR}/\${FULL_PRODUCT_NAME}) +foreach(file ${EXAMPLE_DATA}) + add_custom_command( + TARGET example + POST_BUILD + COMMAND mkdir -p ${APP_RES_DIR} && + /Developer/Library/PrivateFrameworks/DevToolsCore.framework/Resources/pbxcp -resolve-src-symlinks "${CMAKE_CURRENT_SOURCE_DIR}/${file}" ${APP_RES_DIR} && + echo "Copied Data to Bundle." + ) +endforeach(file) +else(IPHONE) + add_executable(example "example/example.c") +endif(IPHONE) +endif(NOT USE_INTERNAL_LIB) + target_link_libraries(example widgetz ${WZ_ALLEGRO_LDFLAGS}) install(DIRECTORY "${WIDGETZ_SOURCE_DIR}/include/widgetz" DESTINATION include) From 435b0afa3c7f4e575cb899d569893ec48e6cba96 Mon Sep 17 00:00:00 2001 From: Karthik Kumar Viswanathan Date: Mon, 20 Feb 2012 00:52:11 +0530 Subject: [PATCH 5/8] Get Example to Show on iOS --- example/example.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/example/example.c b/example/example.c index b074fa8..f51c03d 100644 --- a/example/example.c +++ b/example/example.c @@ -36,6 +36,8 @@ int main(int argc, char *argv[]) ALLEGRO_FONT *font; ALLEGRO_EVENT_QUEUE *queue; int refresh_rate; + float size=1.0; + int font_size=18; double fixed_dt; double old_time; double game_time; @@ -48,10 +50,16 @@ int main(int argc, char *argv[]) al_init_font_addon(); al_init_primitives_addon(); al_init_ttf_addon(); +#ifdef IPHONE + size=0.5; + al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS, ALLEGRO_DISPLAY_ORIENTATION_LANDSCAPE, 1); + display = al_create_display(0, 0); +#else //IPHONE display = al_create_display(1000, 600); +#endif //IPHONE al_install_keyboard(); // Load a font - font = al_load_font("DejaVuSans.ttf", 18, 0); + font = al_load_font("DejaVuSans.ttf", font_size*size, 0); al_install_mouse(); @@ -84,27 +92,27 @@ int main(int argc, char *argv[]) /* Define all other gui elements, fill_layout is an automatic layout container */ - wz_create_fill_layout(gui, 50, 50, 300, 450, 50, 20, WZ_ALIGN_CENTRE, WZ_ALIGN_TOP, -1); + wz_create_fill_layout(gui, 50*size, 50*size, 300*size, 450*size, 50*size, 20*size, WZ_ALIGN_CENTRE, WZ_ALIGN_TOP, -1); - wz_create_textbox(gui, 0, 0, 200, 50, WZ_ALIGN_CENTRE, WZ_ALIGN_CENTRE, al_ustr_new("Welcome to WidgetZ!"), 1, -1); - wz_create_toggle_button(gui, 0, 0, 200, 50, al_ustr_new("Toggle 1"), 1, 1, 5); - wz_create_toggle_button(gui, 0, 0, 200, 50, al_ustr_new("Toggle 2"), 1, 1, 6); - wz_create_toggle_button(gui, 0, 0, 200, 50, al_ustr_new("Toggle 3"), 1, 1, 7); + wz_create_textbox(gui, 0*size, 0*size, 200*size, 50*size, WZ_ALIGN_CENTRE, WZ_ALIGN_CENTRE, al_ustr_new("Welcome to WidgetZ!"), 1, -1); + wz_create_toggle_button(gui, 0*size, 0*size, 200*size, 50*size, al_ustr_new("Toggle 1"), 1, 1, 5); + wz_create_toggle_button(gui, 0*size, 0*size, 200*size, 50*size, al_ustr_new("Toggle 2"), 1, 1, 6); + wz_create_toggle_button(gui, 0*size, 0*size, 200*size, 50*size, al_ustr_new("Toggle 3"), 1, 1, 7); - wgt = (WZ_WIDGET*)wz_create_button(gui, 0, 0, 200, 50, al_ustr_new("Quit"), 1, 1); + wgt = (WZ_WIDGET*)wz_create_button(gui, 0*size, 0*size, 200*size, 50*size, al_ustr_new("Quit"), 1, 1); wz_set_shortcut(wgt, ALLEGRO_KEY_Q, ALLEGRO_KEYMOD_CTRL); - wz_create_fill_layout(gui, 350, 50, 300, 450, 50, 20, WZ_ALIGN_CENTRE, WZ_ALIGN_TOP, -1); + wz_create_fill_layout(gui, 350*size, 50*size, 300*size, 450*size, 50*size, 20*size, WZ_ALIGN_CENTRE, WZ_ALIGN_TOP, -1); - wz_create_textbox(gui, 0, 0, 200, 50, WZ_ALIGN_CENTRE, WZ_ALIGN_CENTRE, al_ustr_new("Scroll Bars:"), 1, -1); - wz_create_scroll(gui, 0, 0, 200, 50, 20, 9); - wz_create_scroll(gui, 0, 0, 50, 200, 20, 9); - wz_create_scroll(gui, 0, 0, 50, 200, 20, 9); + wz_create_textbox(gui, 0*size, 0*size, 200*size, 50*size, WZ_ALIGN_CENTRE, WZ_ALIGN_CENTRE, al_ustr_new("Scroll Bars:"), 1, -1); + wz_create_scroll(gui, 0*size, 0*size, 200*size, 50*size, 20*size, 9); + wz_create_scroll(gui, 0*size, 0*size, 50*size, 200*size, 20*size, 9); + wz_create_scroll(gui, 0*size, 0*size, 50*size, 200*size, 20*size, 9); - wz_create_fill_layout(gui, 650, 50, 300, 450, 20, 20, WZ_ALIGN_CENTRE, WZ_ALIGN_TOP, -1); - wz_create_textbox(gui, 0, 0, 200, 50, WZ_ALIGN_CENTRE, WZ_ALIGN_CENTRE, al_ustr_new("Edit Box:"), 1, -1); - wgt = (WZ_WIDGET*)wz_create_editbox(gui, 0, 0, 200, 50, al_ustr_new("Type here..."), 1, -1); - wz_create_textbox(gui, 0, 0, 200, 50, WZ_ALIGN_LEFT, WZ_ALIGN_TOP, al_ustr_new("A textbox with a lot of text" + wz_create_fill_layout(gui, 650*size, 50*size, 300*size, 450*size, 20*size, 20*size, WZ_ALIGN_CENTRE, WZ_ALIGN_TOP, -1); + wz_create_textbox(gui, 0*size, 0*size, 200*size, 50*size, WZ_ALIGN_CENTRE, WZ_ALIGN_CENTRE, al_ustr_new("Edit Box:"), 1, -1); + wgt = (WZ_WIDGET*)wz_create_editbox(gui, 0*size, 0*size, 200*size, 50*size, al_ustr_new("Type here..."), 1, -1); + wz_create_textbox(gui, 0*size, 0*size, 200*size, 50*size, WZ_ALIGN_LEFT, WZ_ALIGN_TOP, al_ustr_new("A textbox with a lot of text" " in it. Also supports new lines:\n\nNew paragraph.\n" "Also supports unicode:\n\n" "Привет"), 1, -1); From 19d5701329d70c5764109b10b57f7857c61376d2 Mon Sep 17 00:00:00 2001 From: Karthik Kumar Viswanathan Date: Mon, 20 Feb 2012 00:55:30 +0530 Subject: [PATCH 6/8] Fix Linking Flags --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 239d6e3..5518988 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,8 +48,8 @@ foreach(file ${EXAMPLE_DATA}) ) endforeach(file) endif(NOT IPHONE) -add_library(widgetz ${SOURCES}) if(NOT USE_INTERNAL_LIB) +add_library(widgetz ${SOURCES}) target_link_libraries(widgetz ${WZ_ALLEGRO_LDFLAGS}) if(IPHONE) add_executable(example MACOSX_BUNDLE "example/example.c") From 4d23504d045dbc044fbf2c67dbd017d553b1372e Mon Sep 17 00:00:00 2001 From: Karthik Kumar Viswanathan Date: Mon, 20 Feb 2012 01:00:30 +0530 Subject: [PATCH 7/8] Fix Linking Flags --- .gitignore | 1 + CMakeLists.txt | 6 +----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 318b14f..45d207c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ bin Bin deps Deps +Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt index 5518988..d9ea8cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,11 +8,9 @@ include_directories( file(GLOB_RECURSE SOURCES "src/*.c") if(NOT USE_INTERNAL_LIB) set(EXAMPLE_DATA "DejaVuSans.ttf") - set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -g") include(FindPkgConfig) - set(ENV{FREETYPE_DIR} Deps) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMake/Modules") find_package(Allegro REQUIRED) @@ -48,7 +46,6 @@ foreach(file ${EXAMPLE_DATA}) ) endforeach(file) endif(NOT IPHONE) -if(NOT USE_INTERNAL_LIB) add_library(widgetz ${SOURCES}) target_link_libraries(widgetz ${WZ_ALLEGRO_LDFLAGS}) if(IPHONE) @@ -68,12 +65,11 @@ endforeach(file) else(IPHONE) add_executable(example "example/example.c") endif(IPHONE) -endif(NOT USE_INTERNAL_LIB) target_link_libraries(example widgetz ${WZ_ALLEGRO_LDFLAGS}) - install(DIRECTORY "${WIDGETZ_SOURCE_DIR}/include/widgetz" DESTINATION include) install(TARGETS widgetz ARCHIVE DESTINATION lib) else(NOT USE_INTERNAL_LIB) +add_library(widgetz ${SOURCES}) target_link_libraries(widgetz ${ALLEGRO_LIBRARIES}) endif(NOT USE_INTERNAL_LIB) From 139f4674c5bffd3c2bebac093bdefbedd475b959 Mon Sep 17 00:00:00 2001 From: Karthik Kumar Viswanathan Date: Mon, 27 Feb 2012 02:38:07 +0530 Subject: [PATCH 8/8] Add Touch Support --- example/example.c | 2 ++ src/widgets/box.c | 9 +++++++++ src/widgets/button.c | 19 +++++++++++++++++++ src/widgets/editbox.c | 20 ++++++++++++++++++++ src/widgets/scroll.c | 33 +++++++++++++++++++++++++++++++++ src/widgets/toggle.c | 14 ++++++++++++++ src/widgets/widget.c | 18 ++++++++++++++++++ 7 files changed, 115 insertions(+) diff --git a/example/example.c b/example/example.c index f51c03d..bcee3e3 100644 --- a/example/example.c +++ b/example/example.c @@ -67,6 +67,8 @@ int main(int argc, char *argv[]) queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); + if (al_install_touch_input()) + al_register_event_source(queue, al_get_touch_input_event_source()); refresh_rate = 60; fixed_dt = 1.0f / refresh_rate; diff --git a/src/widgets/box.c b/src/widgets/box.c index d09c896..0e16010 100644 --- a/src/widgets/box.c +++ b/src/widgets/box.c @@ -47,6 +47,15 @@ int wz_box_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event) ret = 0; break; } + case ALLEGRO_EVENT_TOUCH_BEGIN: + { + if (wz_widget_rect_test(wgt, event->touch.x, event->touch.y)) + { + wz_ask_parent_for_focus(wgt); + } + ret = 0; + break; + } case WZ_DRAW: { if (wgt->flags & WZ_STATE_HIDDEN) diff --git a/src/widgets/button.c b/src/widgets/button.c index e2bb8bc..0405092 100644 --- a/src/widgets/button.c +++ b/src/widgets/button.c @@ -183,6 +183,25 @@ int wz_button_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event) } break; } + case ALLEGRO_EVENT_TOUCH_BEGIN: + { + if (wgt->flags & WZ_STATE_DISABLED) + { + ret = 0; + } + else if ((event->touch.x != 0 || event->touch.y != 0) && wz_widget_rect_test(wgt, event->touch.x, event->touch.y)) + { + but->down = 1; + wz_ask_parent_for_focus(wgt); + wz_trigger(wgt); + } + else + { + but->down = 0; + ret = 0; + } + break; + } case WZ_DESTROY: { if(but->own) diff --git a/src/widgets/editbox.c b/src/widgets/editbox.c index 98b0b53..6c7f06e 100644 --- a/src/widgets/editbox.c +++ b/src/widgets/editbox.c @@ -131,6 +131,26 @@ int wz_editbox_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event) ret = 0; break; } + case ALLEGRO_EVENT_TOUCH_BEGIN: + { + if (wgt->flags & WZ_STATE_DISABLED) + { + ret = 0; + } + else if (wz_widget_rect_test(wgt, event->touch.x, event->touch.y)) + { + int len = al_ustr_length(box->text); + ALLEGRO_USTR_INFO info; + ALLEGRO_USTR* text = al_ref_ustr(&info, box->text, box->scroll_pos, len - 1); + + ALLEGRO_FONT* font = wgt->theme->get_font(wgt->theme, 0); + wz_ask_parent_for_focus(wgt); + box->cursor_pos = wz_get_text_pos(font, text, event->touch.x - wgt->x) + box->scroll_pos; + } + else + ret = 0; + break; + } case WZ_HANDLE_SHORTCUT: { wz_ask_parent_for_focus(wgt); diff --git a/src/widgets/scroll.c b/src/widgets/scroll.c index b345172..e188947 100644 --- a/src/widgets/scroll.c +++ b/src/widgets/scroll.c @@ -138,6 +138,39 @@ int wz_scroll_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event) ret = 0; break; } + case ALLEGRO_EVENT_TOUCH_BEGIN: + { + int button_down = 1; + if (wgt->flags & WZ_STATE_DISABLED) + { + ret = 0; + } + else if(wz_widget_rect_test(wgt, event->touch.x, event->touch.y)) + { + wz_ask_parent_for_focus(wgt); + if(button_down == 1) + { + float fraction; + int old_pos; + + if (vertical) + fraction = ((float)(event->touch.y - wgt->y)) / ((float)wgt->h); + else + fraction = ((float)(event->touch.x - wgt->x)) / ((float)wgt->w); + + old_pos = scl->cur_pos; + scl->cur_pos = (int)(((float)scl->max_pos) * fraction + 0.5f); + if (old_pos != scl->cur_pos) + { + wz_trigger(wgt); + } + } + } + else + ret = 0; + break; + + } case ALLEGRO_EVENT_KEY_CHAR: { int old_pos = scl->cur_pos;; diff --git a/src/widgets/toggle.c b/src/widgets/toggle.c index 20d904c..9bc9506 100644 --- a/src/widgets/toggle.c +++ b/src/widgets/toggle.c @@ -102,6 +102,20 @@ int wz_toggle_button_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event) return wz_widget_proc(wgt, event); break; } + case ALLEGRO_EVENT_TOUCH_BEGIN: + { + if (wgt->flags & WZ_STATE_DISABLED) + { + ret = 0; + } + if (wz_widget_rect_test(wgt, event->touch.x, event->touch.y)) + { + wz_trigger(wgt); + wz_ask_parent_for_focus(wgt); + } + return wz_widget_proc(wgt, event); + break; + } case ALLEGRO_EVENT_KEY_UP: case ALLEGRO_EVENT_MOUSE_BUTTON_UP: { diff --git a/src/widgets/widget.c b/src/widgets/widget.c index 8a339bd..0172f22 100644 --- a/src/widgets/widget.c +++ b/src/widgets/widget.c @@ -136,6 +136,24 @@ int wz_widget_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event) } break; } + /* Switch through elements on Touch: + case ALLEGRO_EVENT_TOUCH_BEGIN: + { + if (wgt->flags & WZ_STATE_DISABLED) + { + ret = 0; + } + else if (wgt->first_child == 0 && wgt->parent != 0) + { + wz_ask_parent_to_focus_next(wgt); + } + else + { + ret = 0; + } + break; + } + */ case ALLEGRO_EVENT_KEY_CHAR: { if(event->keyboard.keycode == wgt->shortcut.keycode && ((event->keyboard.modifiers & wgt->shortcut.modifiers) || wgt->shortcut.modifiers == 0))