From 89e736050e165f79010c30d4815dd6273797d6b9 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Tue, 28 Aug 2012 19:50:05 +0200 Subject: [PATCH] Add many new methods to OGKBitmap. --- src/OGKBitmap.h | 22 ++++++++++ src/OGKBitmap.m | 102 ++++++++++++++++++++++++++++++++++++++++++++++- src/OGKEvent.h | 2 + tests/TestMain.h | 2 + tests/TestMain.m | 27 ++++++++++++- 5 files changed, 152 insertions(+), 3 deletions(-) diff --git a/src/OGKBitmap.h b/src/OGKBitmap.h index 726e61a..1a0fe40 100644 --- a/src/OGKBitmap.h +++ b/src/OGKBitmap.h @@ -41,10 +41,32 @@ extern ogk_color_t OGK_COLOR_BLACK; ALLEGRO_BITMAP *bitmap; } +@property (readonly) of_dimension_t size; + + (void)setTarget: (id)target; + (void)clearToColor: (ogk_color_t)color; - initWithSize: (of_dimension_t)size; - initWithFile: (OFString*)file; +- (instancetype)subBitmapWithRegion: (of_rectangle_t)region; - (void)drawAtPosition: (of_point_t)position; +- (void)drawAtPosition: (of_point_t)position + region: (of_rectangle_t)region; +- (void)drawAtPosition: (of_point_t)position + scale: (of_dimension_t)scale; +- (void)drawAtPosition: (of_point_t)position + region: (of_rectangle_t)region + scale: (of_dimension_t)scale; +- (void)drawAtPosition: (of_point_t)position + tint: (ogk_color_t)tint; +- (void)drawAtPosition: (of_point_t)position + scale: (of_dimension_t)scale + tint: (ogk_color_t)tint; +- (void)drawAtPosition: (of_point_t)position + region: (of_rectangle_t)region + tint: (ogk_color_t)tint; +- (void)drawAtPosition: (of_point_t)position + region: (of_rectangle_t)region + scale: (of_dimension_t)scale + tint: (ogk_color_t)tint; - (ALLEGRO_BITMAP*)OGK_allegroBitmap; @end diff --git a/src/OGKBitmap.m b/src/OGKBitmap.m index bd22216..bf1026b 100644 --- a/src/OGKBitmap.m +++ b/src/OGKBitmap.m @@ -26,6 +26,12 @@ ogk_color_t OGK_COLOR_BLACK = { 0, 0, 0, 1 }; +static ALLEGRO_COLOR +ogk_color_to_allegro(ogk_color_t color) +{ + return al_map_rgba_f(color.red, color.green, color.blue, color.alpha); +} + @implementation OGKBitmap + (void)initialize { @@ -47,8 +53,7 @@ ogk_color_t OGK_COLOR_BLACK = { 0, 0, 0, 1 }; + (void)clearToColor: (ogk_color_t)color { - al_clear_to_color( - al_map_rgb(color.red * 256, color.green * 256, color.blue * 256)); + al_clear_to_color(ogk_color_to_allegro(color)); } - initWithSize: (of_dimension_t)size @@ -84,11 +89,104 @@ ogk_color_t OGK_COLOR_BLACK = { 0, 0, 0, 1 }; al_destroy_bitmap(bitmap); } +- copy +{ + OGKBitmap *copy = [[[self class] alloc] init]; + + copy->bitmap = al_clone_bitmap(bitmap); + + if (copy->bitmap == NULL) + @throw [OFInitializationFailedException + exceptionWithClass: [self class]]; + + return copy; +} + +- (instancetype)subBitmapWithRegion: (of_rectangle_t)region +{ + OGKBitmap *subBitmap = [[[self class] alloc] init]; + + subBitmap->bitmap = al_create_sub_bitmap(bitmap, region.origin.x, + region.origin.y, region.size.width, region.size.height); + + if (subBitmap->bitmap == NULL) + @throw [OFInitializationFailedException + exceptionWithClass: [self class]]; + + return subBitmap; +} + +- (of_dimension_t)size +{ + return of_dimension(al_get_bitmap_width(bitmap), + al_get_bitmap_height(bitmap)); +} + - (void)drawAtPosition: (of_point_t)position { al_draw_bitmap(bitmap, position.x, position.y, 0); } +- (void)drawAtPosition: (of_point_t)position + scale: (of_dimension_t)scale +{ + al_draw_scaled_bitmap(bitmap, 0, 0, al_get_bitmap_width(bitmap), + al_get_bitmap_height(bitmap), position.x, position.y, + scale.width, scale.height, 0); +} + +- (void)drawAtPosition: (of_point_t)position + region: (of_rectangle_t)region +{ + al_draw_bitmap_region(bitmap, region.origin.x, region.origin.y, + region.size.width, region.size.height, position.x, position.y, 0); +} + +- (void)drawAtPosition: (of_point_t)position + region: (of_rectangle_t)region + scale: (of_dimension_t)scale +{ + al_draw_scaled_bitmap(bitmap, region.origin.x, region.origin.y, + region.size.width, region.size.height, position.x, position.y, + scale.width, scale.height, 0); +} + +- (void)drawAtPosition: (of_point_t)position + tint: (ogk_color_t)tint +{ + al_draw_tinted_bitmap(bitmap, ogk_color_to_allegro(tint), + position.x, position.y, 0); +} + +- (void)drawAtPosition: (of_point_t)position + scale: (of_dimension_t)scale + tint: (ogk_color_t)tint +{ + al_draw_tinted_scaled_bitmap(bitmap, ogk_color_to_allegro(tint), + 0, 0, al_get_bitmap_width(bitmap), al_get_bitmap_height(bitmap), + position.x, position.y, scale.width, scale.height, 0); +} + +- (void)drawAtPosition: (of_point_t)position + region: (of_rectangle_t)region + tint: (ogk_color_t)tint +{ + al_draw_tinted_bitmap_region(bitmap, ogk_color_to_allegro(tint), + region.origin.x, region.origin.y, region.size.width, + region.size.height, position.x, position.y, 0); +} + +- (void)drawAtPosition: (of_point_t)position + region: (of_rectangle_t)region + scale: (of_dimension_t)scale + tint: (ogk_color_t)tint +{ + al_draw_tinted_scaled_bitmap(bitmap, ogk_color_to_allegro(tint), + region.origin.x, region.origin.y, region.size.width, + region.size.height, position.x, position.y, scale.width, + scale.height, 0); +} + - (ALLEGRO_BITMAP*)OGK_allegroBitmap { return bitmap; diff --git a/src/OGKEvent.h b/src/OGKEvent.h index 1e97d6c..d0a39c9 100644 --- a/src/OGKEvent.h +++ b/src/OGKEvent.h @@ -22,6 +22,8 @@ #import +#import "keycodes.h" + @interface OGKEvent: OFObject { ALLEGRO_EVENT event; diff --git a/tests/TestMain.h b/tests/TestMain.h index 22eb057..c3a08c4 100644 --- a/tests/TestMain.h +++ b/tests/TestMain.h @@ -30,6 +30,8 @@ OGKEventQueue *eventQueue; OGKBitmap *bitmap; of_point_t position; + of_dimension_t scale; BOOL running; + ogk_color_t tint; } @end diff --git a/tests/TestMain.m b/tests/TestMain.m index 0e9ef0c..d9485c0 100644 --- a/tests/TestMain.m +++ b/tests/TestMain.m @@ -37,6 +37,24 @@ OF_APPLICATION_DELEGATE(TestMain) display: (OGKDisplay*)display { of_log(@"Pressed: %d", event.keycode); + + switch (event.keycode) { + case OGK_KEY_R: + tint = ogk_color(1, 0.5, 0.5, 0); + break; + case OGK_KEY_G: + tint = ogk_color(0.5, 1, 0.5, 0); + break; + case OGK_KEY_B: + tint = ogk_color(0.5, 0.5, 1, 0); + break; + case OGK_KEY_N: + tint = ogk_color(1, 1, 1, 0); + break; + case OGK_KEY_Q: + running = NO; + break; + } } - (void)keyWasReleased: (OGKKeyReleaseEvent*)event @@ -55,6 +73,8 @@ OF_APPLICATION_DELEGATE(TestMain) event.wheel.y, event.deltaWheel.y); position = event.cursor; + scale = of_dimension(bitmap.size.width + event.wheel.x, + bitmap.size.height + event.wheel.y); } - (void)mouseButtonWasPressed: (OGKMouseButtonPressedEvent*)event @@ -81,7 +101,9 @@ OF_APPLICATION_DELEGATE(TestMain) - (void)draw { [OGKBitmap clearToColor: OGK_COLOR_BLACK]; - [bitmap drawAtPosition: position]; + [bitmap drawAtPosition: position + scale: scale + tint: tint]; [display update]; } @@ -110,6 +132,9 @@ OF_APPLICATION_DELEGATE(TestMain) [eventQueue registerMouse]; bitmap = [[OGKBitmap alloc] initWithFile: @"test.bmp"]; + position = of_point(display.size.width / 2, display.size.height / 2); + scale = bitmap.size; + tint = ogk_color(1, 1, 1, 0); for (running = YES; running;) { @autoreleasepool {