fun measureText(str: String, height: Int): Int
do
var font = app.game.font
- return str.length * (app.game.font.width + app.game.font.hspace)
+ return str.length * (font.width + font.hspace.to_i)
end
# displays a debug rectangle
# ~~~
module a_star
-redef class Object
- protected fun debug_a_star: Bool do return false
- private fun debug(msg: String) do if debug_a_star then
- sys.stderr.write "a_star debug: {msg}\n"
- end
-end
-
# General graph node
class Node
type N: Node
var current_bucket = buckets[cost % nbr_buckets]
if current_bucket.is_empty then # move to next bucket
- debug "b {cost} {cost % nbr_buckets} {buckets[cost % nbr_buckets].hash}"
cost += 1
if cost > max_cost then return null
bucket_searched += 1
if bucket_searched > nbr_buckets then break
else # found a node
- debug "c {cost}"
frontier_node = current_bucket.pop
if frontier_node.open then break
# at destination
else if frontier_node == destination or
(alt_targets != null and alt_targets.accept(frontier_node)) then
- debug "picked {frontier_node}, is destination"
var path = new Path[N](cost)
else
frontier_node.open = false
- debug "w exploring adjacents of {frontier_node}"
-
for link in frontier_node.links do
var peek_node = link.to
- debug "v {context.is_blocked(link)} {peek_node.last_pathfinding_evocation != graph.pathfinding_current_evocation} {peek_node.best_cost_up_to > frontier_node.best_cost_up_to + context.cost(link)}, {peek_node.best_cost_up_to} > {frontier_node.best_cost_up_to} + {context.cost(link)}"
if not context.is_blocked(link) and
(peek_node.last_pathfinding_evocation != graph.pathfinding_current_evocation or
(peek_node.open and
var at_bucket = buckets[est_cost % nbr_buckets]
at_bucket.add(peek_node)
-
- debug "u putting {peek_node} at {est_cost} -> {est_cost % nbr_buckets} {at_bucket.hash}, {cost}+{context.cost(link)}"
end
end
end
var current_bucket = buckets[current_bucket_key]
var next_bucket = new HashSet[Bucketable[G]]
+ buckets[current_bucket_key] = next_bucket
+ self.next_bucket = next_bucket
for e in current_bucket do
var act_at = e.act_at
end
end
end
-
- self.next_bucket = next_bucket
- buckets[current_bucket_key] = next_bucket
end
end
JavaVM_jni_error(NULL, "Could not attach current thread to Java VM", res);
return NULL;
}
+ return env;
`}
end
# Call a method on `obj` designed by `method_id` with an array `args` of argument returning a JavaObject
fun call_object_method(obj: JavaObject, method_id: JMethodID, args: nullable Array[nullable Object]): JavaObject import convert_args_to_jni `{
jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
- (*recv)->CallObjectMethod(recv, obj, method_id, args_tab);
+ jobject res = (*recv)->CallObjectMethod(recv, obj, method_id, args_tab);
free(args_tab);
+ return res;
`}
# Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Bool
fun call_boolean_method(obj: JavaObject, method_id: JMethodID, args: nullable Array[nullable Object]): Bool import convert_args_to_jni `{
jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
- return (*recv)->CallBooleanMethod(recv, obj, method_id, args_tab);
+ jboolean res = (*recv)->CallBooleanMethod(recv, obj, method_id, args_tab);
free(args_tab);
+ return res;
`}
# Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Char
fun call_char_method(obj: JavaObject, method_id: JMethodID, args: nullable Array[nullable Object]): Char import convert_args_to_jni `{
jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
- return (*recv)->CallCharMethod(recv, obj, method_id, args_tab);
+ jchar res = (*recv)->CallCharMethod(recv, obj, method_id, args_tab);
free(args_tab);
+ return res;
`}
# Call a method on `obj` designed by `method_id` with an array `args` of arguments returning an Int
fun call_int_method(obj: JavaObject, method_id: JMethodID, args: nullable Array[nullable Object]): Int import convert_args_to_jni `{
jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
- return (*recv)->CallIntMethod(recv, obj, method_id, args_tab);
+ jint res = (*recv)->CallIntMethod(recv, obj, method_id, args_tab);
free(args_tab);
+ return res;
`}
# Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Float
fun call_float_method(obj: JavaObject, method_id: JMethodID, args: nullable Array[nullable Object]): Float import convert_args_to_jni `{
jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
- return (*recv)->CallFloatMethod(recv, obj, method_id, args_tab);
+ jfloat res = (*recv)->CallFloatMethod(recv, obj, method_id, args_tab);
free(args_tab);
+ return res;
`}
# Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a NativeString
# Additional space to insert horizontally between characters
# A negave value will display tile overlaped
- var hspace: Int = 0 is writable
+ var hspace: Numeric = 0.0 is writable
# Additional space to insert vertically between characters
# A negave value will display tile overlaped
- var vspace: Int = 0 is writable
+ var vspace: Numeric = 0.0 is writable
# The glyph (tile) associated to the caracter `c` according to `chars`
# Returns null if `c` is not in `chars`
# '\n' are rendered as carriage return
fun text(text: String, font: TileSetFont, x, y: Numeric)
do
+ x = x.to_f
var cx = x
- var cy = y
- var sw = font.width + font.hspace
- var sh = font.height + font.vspace
+ var cy = y.to_f
+ var sw = font.width.to_f + font.hspace.to_f
+ var sh = font.height.to_f + font.vspace.to_f
for c in text.chars do
if c == '\n' then
cx = x
int has_alpha;
unsigned int row_bytes;
- png_bytepp row_pointers;
- unsigned char *pixels;
+ png_bytepp row_pointers = NULL;
+ unsigned char *pixels = NULL;
unsigned int i;
unsigned char sig[8];
png_get_IHDR( png_ptr, info_ptr, &width, &height,
&depth, &color_type, NULL, NULL, NULL);
- if (color_type == PNG_COLOR_TYPE_RGBA)
- has_alpha = 1;
- else if (color_type == PNG_COLOR_TYPE_RGB)
- has_alpha = 0;
- else {
- LOGW("unknown color_type");
- goto close_png_ptr;
+ has_alpha = color_type & PNG_COLOR_MASK_ALPHA;
+
+ // If we get gray and alpha only, standardize the format of the pixels.
+ // GA is not supported by OpenGL ES 1.
+ if (!(color_type & PNG_COLOR_MASK_COLOR)) {
+ png_set_gray_to_rgb(png_ptr);
+ png_set_palette_to_rgb(png_ptr);
+ png_read_update_info(png_ptr, info_ptr);
}
LOGW("w: %i, h: %i", width, height);
row_bytes = png_get_rowbytes(png_ptr, info_ptr);
pixels = malloc(row_bytes * height);
- row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
+ row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
- for (i=0; i<height; i++)
- row_pointers[i] = (png_byte*) malloc(row_bytes);
+ for (i=0; i<height; i++)
+ row_pointers[i] = (png_byte*) malloc(row_bytes);
png_read_image(png_ptr, row_pointers);
else
png_destroy_read_struct(&png_ptr, NULL, NULL);
+ if (pixels != NULL)
+ free(pixels);
+
+ if (row_pointers != NULL) {
+ for (i=0; i<height; i++)
+ free(row_pointers[i]);
+ free(row_pointers);
+ }
+
close_stream:
return recv;
`}
// Integer to NativeString method
char* native_int_to_s(long recv){
int len = snprintf(NULL, 0, "%ld", recv);
- char* str = malloc(len);
+ char* str = malloc(len+1);
sprintf(str, "%ld", recv);
return str;
}