From a98c97edb2fda2107ed741feee6bd40dc5c2d4c1 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Tue, 28 Aug 2012 21:48:53 +0200 Subject: [PATCH] Add methods for drawing rotated. --- src/OGKBitmap.h | 25 +++++++++++++++++++++++++ src/OGKBitmap.m | 35 +++++++++++++++++++++++++++++++++++ tests/TestMain.h | 1 + tests/TestMain.m | 9 +++++++++ 4 files changed, 70 insertions(+) diff --git a/src/OGKBitmap.h b/src/OGKBitmap.h index 1a0fe40..759891f 100644 --- a/src/OGKBitmap.h +++ b/src/OGKBitmap.h @@ -26,6 +26,11 @@ typedef struct ogk_color_t { float red, green, blue, alpha; } ogk_color_t; +typedef struct ogk_rotation_t { + of_point_t center; + float angle; +} ogk_rotation_t; + static OF_INLINE ogk_color_t ogk_color(float red, float green, float blue, float alpha) { @@ -34,6 +39,14 @@ ogk_color(float red, float green, float blue, float alpha) return color; } +static OF_INLINE ogk_rotation_t +ogk_rotation(float x, float y, float angle) +{ + ogk_rotation_t rotation = { of_point(x, y), angle }; + + return rotation; +} + extern ogk_color_t OGK_COLOR_BLACK; @interface OGKBitmap: OFObject @@ -68,5 +81,17 @@ extern ogk_color_t OGK_COLOR_BLACK; region: (of_rectangle_t)region scale: (of_dimension_t)scale tint: (ogk_color_t)tint; +- (void)drawAtPosition: (of_point_t)position + rotation: (ogk_rotation_t)rotation; +- (void)drawAtPosition: (of_point_t)position + scale: (of_dimension_t)scale + rotation: (ogk_rotation_t)rotation; +- (void)drawAtPosition: (of_point_t)position + rotation: (ogk_rotation_t)rotation + tint: (ogk_color_t)tint; +- (void)drawAtPosition: (of_point_t)position + scale: (of_dimension_t)scale + rotation: (ogk_rotation_t)rotation + tint: (ogk_color_t)tint; - (ALLEGRO_BITMAP*)OGK_allegroBitmap; @end diff --git a/src/OGKBitmap.m b/src/OGKBitmap.m index 92d9326..10c9895 100644 --- a/src/OGKBitmap.m +++ b/src/OGKBitmap.m @@ -191,6 +191,41 @@ ogk_color_to_allegro(ogk_color_t color) scale.height * al_get_bitmap_height(bitmap), 0); } +- (void)drawAtPosition: (of_point_t)position + rotation: (ogk_rotation_t)rotation +{ + al_draw_rotated_bitmap(bitmap, rotation.center.x, rotation.center.y, + position.x, position.y, rotation.angle, 0); +} + +- (void)drawAtPosition: (of_point_t)position + scale: (of_dimension_t)scale + rotation: (ogk_rotation_t)rotation +{ + al_draw_scaled_rotated_bitmap(bitmap, rotation.center.x, + rotation.center.y, position.x, position.y, scale.width, + scale.height, rotation.angle, 0); +} + +- (void)drawAtPosition: (of_point_t)position + rotation: (ogk_rotation_t)rotation + tint: (ogk_color_t)tint +{ + al_draw_tinted_rotated_bitmap(bitmap, ogk_color_to_allegro(tint), + rotation.center.x, rotation.center.y, position.x, position.y, + rotation.angle, 0); +} + +- (void)drawAtPosition: (of_point_t)position + scale: (of_dimension_t)scale + rotation: (ogk_rotation_t)rotation + tint: (ogk_color_t)tint +{ + al_draw_tinted_scaled_rotated_bitmap(bitmap, ogk_color_to_allegro(tint), + rotation.center.x, rotation.center.y, position.x, position.y, + scale.width, scale.height, rotation.angle, 0); +} + - (ALLEGRO_BITMAP*)OGK_allegroBitmap { return bitmap; diff --git a/tests/TestMain.h b/tests/TestMain.h index c3a08c4..78346e3 100644 --- a/tests/TestMain.h +++ b/tests/TestMain.h @@ -32,6 +32,7 @@ of_point_t position; of_dimension_t scale; BOOL running; + ogk_rotation_t rotation; ogk_color_t tint; } @end diff --git a/tests/TestMain.m b/tests/TestMain.m index 67d9497..35ec6af 100644 --- a/tests/TestMain.m +++ b/tests/TestMain.m @@ -51,6 +51,12 @@ OF_APPLICATION_DELEGATE(TestMain) case OGK_KEY_N: tint = ogk_color(1, 1, 1, 0); break; + case OGK_KEY_LEFT: + rotation.angle -= M_PI / 128; + break; + case OGK_KEY_RIGHT: + rotation.angle += M_PI / 128; + break; case OGK_KEY_Q: running = NO; break; @@ -104,6 +110,7 @@ OF_APPLICATION_DELEGATE(TestMain) [OGKBitmap clearToColor: OGK_COLOR_BLACK]; [bitmap drawAtPosition: position scale: scale + rotation: rotation tint: tint]; [display update]; } @@ -135,6 +142,8 @@ OF_APPLICATION_DELEGATE(TestMain) bitmap = [[OGKBitmap alloc] initWithFile: @"test.bmp"]; position = of_point(display.size.width / 2, display.size.height / 2); scale = of_dimension(1, 1); + rotation = ogk_rotation(bitmap.size.width / 2, bitmap.size.height / 2, + 0); tint = ogk_color(1, 1, 1, 0); for (running = YES; running;) {