Commit 04e989e5 by beoran

First checkin.

parents
cmake_minimum_required(VERSION 2.8)
# Set policy: link_directories is relative to source dir
cmake_policy(SET CMP0015 NEW)
# Set the project name.
project(A4A5 C)
# tell CMake to search first in the cmake subdirectory for FIND_PACKAGE() or INCLUDE()
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/)
include(CheckCSourceCompiles)
include(CheckCXXSourceCompiles)
if(CMAKE_COMPILER_IS_GNUCC)
set(COMPILER_GCC 1)
set(CMAKE_C_FLAGS "-W -Wall -Wno-unused -Wno-unknown-pragmas -g3 -gdwarf-2 -std=c99 -ffast-math -fno-omit-frame-pointer -fstrict-aliasing -Wstrict-aliasing=2")
# set(CMAKE_LD_FLAGS "-pg")
# always use gnu99, debugging, all warnings except unused and unknown pragmas.
# when compiling with gnu compiler.
# Warn about alisasing because otherwise aliasing problems it may not be detected.
else(CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_C_FLAGS "-W -Wall -Wno-unused -Wno-unknown-pragmas -g -std=c99 -ffast-math -fstrict-aliasing -Wstrict-aliasing=2")
endif(CMAKE_COMPILER_IS_GNUCC)
# Finds Allegro using pkgconfig, so it must be configured correctly
find_package(Allegro5 REQUIRED)
find_package(Allegro4 REQUIRED)
message(ALLEGRO4_INCLUDE_DIR)
message(ALLEGRO5_INCLUDE_DIR)
message(ALLEGRO4_LIBRARIES)
message(ALLEGRO5_LIBRARIES)
# Set include and lib dirs.
include_directories(${ALLEGRO4_INCLUDE_DIR} ${ALLEGRO5_INCLUDE_DIR})
set(A4A5_LIBS ${LIBS} ${ALLEGRO4_LIBRARIES} ${ALLEGRO5_LIBRARIES})
include_directories("include")
# add_subdirectory("src")
include(A4A5Files)
set_source_files_properties(${ERUTA_SRC_FILES} PROPERTIES LANGUAGE C)
add_library(a4a5 ${A4A5_SRC_FILES})
add_executable(ex_a4a5 example/ex_a4a5.c)
target_link_libraries(ex_a4a5 a4a5 ${A4A5_LIBS})
a4a5 is an Allegro 4 to Allegro 5 bridge library.
The idea of this library is to ease porting Allegro 4 programs to Allegro 5
by providing a thin bridge library. The way it is intended to work is that
you link both Allegro 4 and Allegro 5 into your program, but use
A4 only for software bitmap rendering, packfiles and other Allegro 4 specific
functionality. Drawing to the screen, etc is done with Allegro 5. What this
library then does is to use A4 to provide certain conversions and services that
allow you to keep on using the Allegro 4 API in part, but redirect the output
and receive the input through Allegro 5.
Look at the example for now to see how this works.
# All source files
set(A4A5_SRC_FILES
src/a4a5.c
)
find_package(PkgConfig)
# Find main allegro and assume the rest is there too
pkg_check_modules(Allegro4_PKGCONF allegro)
# Perhaps use this later?
# pkg_search_module(ALLEGRO4 REQUIRED allegro allegro-4)
# MESSAGE("lib: ${Allegro5_PKGCONF_LIBRARY_DIRS}")
# MESSAGE("include: ${Allegro5_PKGCONF_INCLUDE_DIRS}")
# Include dir
find_path(Allegro4_INCLUDE_DIR
NAMES allegro5/allegro5.h
PATHS ${Allegro4_PKGCONF_INCLUDE_DIRS}
)
# message("include dir: ${Allegro4_INCLUDE_DIR}")
# Names of all libraries in Allegro, without versions
set(ALLEGRO4_ALL_LIBRARIES
alleggl alleg jpgalleg
)
# set(ALLEGRO5_LIBRARIES "")
# Find all libraries to link
foreach(ALLEGRO4_ONE_LIBRARY ${ALLEGRO4_ALL_LIBRARIES})
# message("${ALLEGRO5_ONE_LIBRARY}")
find_library("${ALLEGRO4_ONE_LIBRARY}_AID" "${ALLEGRO4_ONE_LIBRARY}"
${Allegro4_PKGCONF_LIBRARY_DIRS}
)
set(Allegro4_LIBRARIES
"${Allegro4_LIBRARIES}" "${${ALLEGRO4_ONE_LIBRARY}_AID}")
# MESSAGE("${Allegro5_LIBRARIES}")
endforeach(ALLEGRO4_ONE_LIBRARY)
# Remove first empty erroneous "library"
list(REMOVE_AT Allegro4_LIBRARIES 0)
# message("${Allegro4_LIBRARIES}")
# Set the include dir variables and the libraries and let libfind_process do the rest.
set(ALLEGRO4_INCLUDE_DIR "${Allegro4_INCLUDE_DIR}")
set(ALLEGRO4_LIBRARIES "${Allegro4_LIBRARIES}")
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Allegro4 DEFAULT_MSG ALLEGRO4_LIBRARIES ALLEGRO4_INCLUDE_DIR)
MARK_AS_ADVANCED(ALLEGRO4_INCLUDE_DIR ALLEGRO4_LIBRARIES)
find_package(PkgConfig)
# Find main allegro and assume the rest is there too
pkg_check_modules(Allegro5_PKGCONF allegro-5)
# Perhaps use this later?
# pkg_search_module(ALLEGRO5 REQUIRED allegro-5 allegro-5.0)
# MESSAGE("lib: ${Allegro5_PKGCONF_LIBRARY_DIRS}")
# MESSAGE("include: ${Allegro5_PKGCONF_INCLUDE_DIRS}")
# Include dir
find_path(Allegro5_INCLUDE_DIR
NAMES allegro5/allegro5.h
PATHS ${Allegro5_PKGCONF_INCLUDE_DIRS}
)
# message("include dir: ${Allegro5_INCLUDE_DIR}")
# Names of all libraries in Allegro, without versions
set(ALLEGRO5_ALL_LIBRARIES
allegro allegro_image allegro_font
allegro_primitives allegro_ttf allegro_audio
allegro_dialog allegro_memfile allegro_acodec
allegro_color allegro_main allegro_physfs
)
# set(ALLEGRO5_LIBRARIES "")
# Find all libraries to link
foreach(ALLEGRO5_ONE_LIBRARY ${ALLEGRO5_ALL_LIBRARIES})
# message("${ALLEGRO5_ONE_LIBRARY}")
find_library("${ALLEGRO5_ONE_LIBRARY}_AID" "${ALLEGRO5_ONE_LIBRARY}"
${Allegro5_PKGCONF_LIBRARY_DIRS}
)
set(Allegro5_LIBRARIES
"${Allegro5_LIBRARIES}" "${${ALLEGRO5_ONE_LIBRARY}_AID}")
# MESSAGE("${Allegro5_LIBRARIES}")
endforeach(ALLEGRO5_ONE_LIBRARY)
# Remove first empty erroneous "library"
list(REMOVE_AT Allegro5_LIBRARIES 0)
# message("${Allegro5_LIBRARIES}")
# Set the include dir variables and the libraries and let libfind_process do the rest.
set(ALLEGRO5_INCLUDE_DIR "${Allegro5_INCLUDE_DIR}")
set(ALLEGRO5_LIBRARIES "${Allegro5_LIBRARIES}")
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Allegro5 DEFAULT_MSG ALLEGRO5_LIBRARIES ALLEGRO5_INCLUDE_DIR)
MARK_AS_ADVANCED(ALLEGRO5_INCLUDE_DIR ALLEGRO5_LIBRARIES)
# Locate Lua library
# This module defines
# LUA52_FOUND, if false, do not try to link to Lua
# LUA_LIBRARIES
# LUA_INCLUDE_DIR, where to find lua.h
#
# Note that the expected include convention is
# #include "lua.h"
# and not
# #include <lua/lua.h>
# This is because, the lua location is not standardized and may exist
# in locations other than lua/
#=============================================================================
# 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.)
FIND_PATH(LUA_INCLUDE_DIR lua.h
HINTS
$ENV{LUA_DIR}
PATH_SUFFIXES include/lua52 include/lua5.2 include/lua include
PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
)
FIND_LIBRARY(LUA_LIBRARY
NAMES lua52 lua5.2 lua-5.2 lua
HINTS
$ENV{LUA_DIR}
PATH_SUFFIXES lib64 lib
PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw
/opt/local
/opt/csw
/opt
)
IF(LUA_LIBRARY)
# include the math library for Unix
IF(UNIX AND NOT APPLE)
FIND_LIBRARY(LUA_MATH_LIBRARY m)
SET( LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}" CACHE STRING "Lua Libraries")
# For Windows and Mac, don't need to explicitly include the math library
ELSE(UNIX AND NOT APPLE)
SET( LUA_LIBRARIES "${LUA_LIBRARY}" CACHE STRING "Lua Libraries")
ENDIF(UNIX AND NOT APPLE)
ENDIF(LUA_LIBRARY)
INCLUDE(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
# handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
# all listed variables are TRUE
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua52 DEFAULT_MSG LUA_LIBRARIES LUA_INCLUDE_DIR)
MARK_AS_ADVANCED(LUA_INCLUDE_DIR LUA_LIBRARIES LUA_LIBRARY LUA_MATH_LIBRARY)
# Works the same as find_package, but forwards the "REQUIRED" and "QUIET" arguments
# used for the current package. For this to work, the first parameter must be the
# prefix of the current package, then the prefix of the new package etc, which are
# passed to find_package.
macro (libfind_package PREFIX)
set (LIBFIND_PACKAGE_ARGS ${ARGN})
if (${PREFIX}_FIND_QUIETLY)
set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} QUIET)
endif (${PREFIX}_FIND_QUIETLY)
if (${PREFIX}_FIND_REQUIRED)
set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} REQUIRED)
endif (${PREFIX}_FIND_REQUIRED)
find_package(${LIBFIND_PACKAGE_ARGS})
endmacro (libfind_package)
# CMake developers made the UsePkgConfig system deprecated in the same release (2.6)
# where they added pkg_check_modules. Consequently I need to support both in my scripts
# to avoid those deprecated warnings. Here's a helper that does just that.
# Works identically to pkg_check_modules, except that no checks are needed prior to use.
macro (libfind_pkg_check_modules PREFIX PKGNAME)
if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
include(UsePkgConfig)
pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS)
else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(${PREFIX} ${PKGNAME})
endif (PKG_CONFIG_FOUND)
endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
endmacro (libfind_pkg_check_modules)
# Do the final processing once the paths have been detected.
# If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
# all the variables, each of which contain one include directory.
# Ditto for ${PREFIX}_PROCESS_LIBS and library files.
# Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
# Also handles errors in case library detection was required, etc.
macro (libfind_process PREFIX)
# Skip processing if already processed during this run
if (NOT ${PREFIX}_FOUND)
# Start with the assumption that the library was found
set (${PREFIX}_FOUND TRUE)
# Process all includes and set _FOUND to false if any are missing
foreach (i ${${PREFIX}_PROCESS_INCLUDES})
if (${i})
set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}})
mark_as_advanced(${i})
else (${i})
set (${PREFIX}_FOUND FALSE)
endif (${i})
endforeach (i)
# Process all libraries and set _FOUND to false if any are missing
foreach (i ${${PREFIX}_PROCESS_LIBS})
if (${i})
set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
mark_as_advanced(${i})
else (${i})
set (${PREFIX}_FOUND FALSE)
endif (${i})
endforeach (i)
# Print message and/or exit on fatal error
if (${PREFIX}_FOUND)
if (NOT ${PREFIX}_FIND_QUIETLY)
message (STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}")
endif (NOT ${PREFIX}_FIND_QUIETLY)
else (${PREFIX}_FOUND)
if (${PREFIX}_FIND_REQUIRED)
foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
message("${i}=${${i}}")
endforeach (i)
message (FATAL_ERROR "Required library ${PREFIX} NOT FOUND.\nInstall the library (dev version) and try again. If the library is already installed, use ccmake to set the missing variables manually.")
endif (${PREFIX}_FIND_REQUIRED)
endif (${PREFIX}_FOUND)
endif (NOT ${PREFIX}_FOUND)
endmacro (libfind_process)
macro(libfind_library PREFIX basename)
set(TMP "")
if(MSVC80)
set(TMP -vc80)
endif(MSVC80)
if(MSVC90)
set(TMP -vc90)
endif(MSVC90)
set(${PREFIX}_LIBNAMES ${basename}${TMP})
if(${ARGC} GREATER 2)
set(${PREFIX}_LIBNAMES ${basename}${TMP}-${ARGV2})
string(REGEX REPLACE "\\." "_" TMP ${${PREFIX}_LIBNAMES})
set(${PREFIX}_LIBNAMES ${${PREFIX}_LIBNAMES} ${TMP})
endif(${ARGC} GREATER 2)
find_library(${PREFIX}_LIBRARY
NAMES ${${PREFIX}_LIBNAMES}
PATHS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}
)
endmacro(libfind_library)
#include "a4a5.h"
#include <stdio.h>
#include <stdlib.h>
#define WIDTH 640
#define HEIGHT 460
void abort_example(char * message) {
puts(message);
exit(1);
}
int main(void) {
ALLEGRO_THREAD * thread;
ALLEGRO_DISPLAY * display;
if (!al_init()) {
abort_example("Could not init Allegro.\n");
}
display = al_create_display(WIDTH, HEIGHT);
if (!display) {
abort_example("al_create_display failed\n");
}
al_clear_to_color(al_map_rgb_f(0, 0, 0));
al_flip_display();
if (!al_install_keyboard()) {
abort_example("al_install_keyboard failed\n");
}
thread = a4a5_start_keyboard_thread();
if (!thread) {
abort_example("could not start bridge thread");
}
puts("Waiting for Q");
while (!key[KEY_Q]) {
}
al_set_thread_should_stop(thread);
return 0;
}
#ifndef A4A5_H_INCLUDED
#define A4A5_H_INCLUDED
/* Allegro 5 include. */
#include <allegro5/allegro.h>
/* Allegro 4 include. */
#include <allegro.h>
/** Result of a function that could fail. */
typedef enum A4A5_RESULT_ENUM {
A4A5_RESULT_OK = 0,
A4A5_RESULT_ERROR = -255,
A4A5_RESULT_ENOMEM = -1,
} A4A5_RESULT ;
ALLEGRO_BITMAP * a4a5_convert_bitmap(BITMAP * bitmap);
ALLEGRO_THREAD * a4a5_start_keyboard_thread(void);
ALLEGRO_THREAD * a4a5_start_mouse_thread(void);
ALLEGRO_THREAD * a4a5_start_joystick_thread(void);
#endif
/* Allegro 5 include. */
#include <allegro5/allegro.h>
/* Allegro 4 include. */
#include <allegro.h>
/** Converts an Allegro4 bitmap to an Allegro 5 one. */
ALLEGRO_BITMAP * a4a5_convert_bitmap(BITMAP * bitmap) {
return NULL;
}
static ALLEGRO_EVENT_QUEUE * a4a5_keyboard_queue = NULL;
static ALLEGRO_EVENT_QUEUE * a4a5_mouse_queue = NULL;
static ALLEGRO_EVENT_QUEUE * a4a5_joystick_queue = NULL;
#define A4A5_KEYBOARD_BUFFER_SIZE 128
static volatile int a4a5_keyboard_buffer[A4A5_KEYBOARD_BUFFER_SIZE] = { 0 };
static volatile size_t a4a5_keyboard_buffer_size = 0;
void a4a5_clear_keybuf() {
memset((void *)a4a5_keyboard_buffer, 0 , sizeof(a4a5_keyboard_buffer));
a4a5_keyboard_buffer_size = 0;
}
int a4a5_keypressed() {
return (a4a5_keyboard_buffer_size > 0);
}
void a4a5_simulate_keypress(int key) {
if (a4a5_keyboard_buffer_size < (sizeof(a4a5_keyboard_buffer))) {
a4a5_keyboard_buffer_size++;
a4a5_keyboard_buffer[a4a5_keyboard_buffer_size - 1] = key;
}
}
int a4a5_readkey() {
int result = 0;
size_t index;
while (!a4a5_keypressed()) { ; }
result = a4a5_keyboard_buffer[0];
/* shift keystrokes in buffer. */
for (index = 1; index < (a4a5_keyboard_buffer_size - 1) ; index++) {
a4a5_keyboard_buffer[index - 1] = a4a5_keyboard_buffer[index];
}
a4a5_keyboard_buffer_size--;
return result;
}
static void a4a5_on_key_down(ALLEGRO_KEYBOARD_EVENT * kev) {
/* A4 and A5 keycodes are compatible, phew. */
if (kev->keycode > 0) {
key[kev->keycode] = TRUE;
}
}
static void a4a5_on_key_up(ALLEGRO_KEYBOARD_EVENT * kev) {
/* A4 and A5 keycodes are compatible, phew. */
if (kev->keycode > 0) {
key[kev->keycode] = FALSE;
}
}
static void a4a5_on_key_char(ALLEGRO_KEYBOARD_EVENT * kev) {
/* A4 and A5 modifiers are compatible, phew. */
key_shifts = kev->modifiers;
a4a5_simulate_keypress(kev->keycode);
}
static void * a4a5_keyboard_thread(ALLEGRO_THREAD *thread, void *arg) {
ALLEGRO_EVENT_QUEUE * queue = arg;
ALLEGRO_EVENT event;
while(!al_get_thread_should_stop(thread)) {
while (al_get_next_event(queue, &event)) {
switch (event.type) {
case ALLEGRO_EVENT_KEY_DOWN: a4a5_on_key_down(&event.keyboard); break;
case ALLEGRO_EVENT_KEY_UP: a4a5_on_key_up(&event.keyboard); break;
case ALLEGRO_EVENT_KEY_CHAR: a4a5_on_key_char(&event.keyboard); break;
default: /* Should not happen. */ break;
}
}
}
al_destroy_event_queue(queue);
return NULL;
}
/** Sets up a thread to handle Allegro 5 keyboard input and to map this to Allegro 4's state variables. */
ALLEGRO_THREAD * a4a5_start_keyboard_thread(void) {
ALLEGRO_THREAD * thread = NULL;
ALLEGRO_EVENT_QUEUE * queue = al_create_event_queue();
ALLEGRO_EVENT_SOURCE * source = al_get_keyboard_event_source();
if (!queue) return NULL;
if (!source) return NULL;
al_register_event_source(queue, source);
thread = al_create_thread(a4a5_keyboard_thread, queue);
al_start_thread(thread);
return thread;
}
/** Sets up a thread to handle Allegro 5 mouse input and to map this to Allegro 4's state variables. */
ALLEGRO_THREAD * a4a5_start_mouse_thread(void) {
}
/** Sets up a thread to handle Allegro 5 joystick input and to map this to Allegro 4's state variables. */
ALLEGRO_THREAD * a4a5_start_joystick_thread(void) {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment