Add OGKBitmap.

This commit is contained in:
Jonathan Schleifer 2012-08-20 00:49:57 +02:00
parent 7953872267
commit 96c5a44ae0
9 changed files with 172 additions and 15 deletions

View file

@ -1,7 +1,9 @@
ALLEGRO_MODULES = allegro-5.0 allegro_main-5.0 allegro_image-5.0
all:
@mkdir -p build
@objfw-compile --lib 0.0 -o objgamekit --builddir build *.m \
--arc `pkg-config --cflags --libs allegro-5.0 allegro_main-5.0`
--arc `pkg-config --cflags --libs ${ALLEGRO_MODULES}`
clean:
@rm -fr build libobjgamekit.* *~

50
src/OGKBitmap.h Normal file
View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2012 Jonathan Schleifer <js@webkeks.org>
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the
* use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1.) The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2.) Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
* 3.) This notice may not be removed or altered from any source distribution.
*/
#include <allegro5/allegro.h>
#import <ObjFW/ObjFW.h>
typedef struct ogk_color_t {
float red, green, blue, alpha;
} ogk_color_t;
static OF_INLINE ogk_color_t
ogk_color(float red, float green, float blue, float alpha)
{
ogk_color_t color = { red, green, blue, alpha};
return color;
}
extern ogk_color_t OGK_COLOR_BLACK;
@interface OGKBitmap: OFObject
{
ALLEGRO_BITMAP *bitmap;
}
+ (void)setTarget: (id)target;
+ (void)clearToColor: (ogk_color_t)color;
- initWithSize: (of_dimension_t)size;
- initWithFile: (OFString*)file;
- (void)drawAtPosition: (of_point_t)position;
- (ALLEGRO_BITMAP*)OGK_allegroBitmap;
@end

91
src/OGKBitmap.m Normal file
View file

@ -0,0 +1,91 @@
/*
* Copyright (c) 2012 Jonathan Schleifer <js@webkeks.org>
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the
* use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1.) The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2.) Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
* 3.) This notice may not be removed or altered from any source distribution.
*/
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#import "OGKBitmap.h"
#import "OGKDisplay.h"
ogk_color_t OGK_COLOR_BLACK = { 0, 0, 0, 1 };
@implementation OGKBitmap
+ (void)initialize
{
if (self != [OGKBitmap class])
return;
if (!al_install_system(ALLEGRO_VERSION_INT, NULL) ||
!al_init_image_addon())
@throw [OFInitializationFailedException
exceptionWithClass: self];
}
+ (void)setTarget: (id)target
{
if ([target isKindOfClass: [OGKDisplay class]])
al_set_target_backbuffer([target OGK_allegroDisplay]);
else
al_set_target_bitmap([target OGK_allegroBitmap]);
}
+ (void)clearToColor: (ogk_color_t)color
{
al_clear_to_color(
al_map_rgb(color.red * 256, color.green * 256, color.blue * 256));
}
- initWithSize: (of_dimension_t)size
{
self = [super init];
bitmap = al_create_bitmap(size.width, size.height);
if (bitmap == NULL)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
return self;
}
- initWithFile: (OFString*)path
{
self = [super init];
bitmap = al_load_bitmap(
[path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]);
if (bitmap == NULL)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
return self;
}
- (void)drawAtPosition: (of_point_t)position
{
al_draw_bitmap(bitmap, position.x, position.y, 0);
}
- (ALLEGRO_BITMAP*)OGK_allegroBitmap
{
return bitmap;
}
@end

View file

@ -27,11 +27,9 @@
ALLEGRO_DISPLAY *display;
}
+ displayWithSize: (of_dimension_t)size
fullscreen: (BOOL)fullscreen
resizable: (BOOL)resizable;
- initWithSize: (of_dimension_t)size
fullscreen: (BOOL)fullscreen
resizable: (BOOL)resizable;
- (void)update;
- (ALLEGRO_DISPLAY*)OGK_allegroDisplay;
@end

View file

@ -31,15 +31,6 @@
exceptionWithClass: self];
}
+ displayWithSize: (of_dimension_t)size
fullscreen: (BOOL)fullscreen
resizable: (BOOL)resizable
{
return [[self alloc] initWithSize: size
fullscreen: fullscreen
resizable: resizable];
}
- initWithSize: (of_dimension_t)size
fullscreen: (BOOL)fullscreen
resizable: (BOOL)resizable
@ -75,6 +66,11 @@
al_destroy_display(display);
}
- (void)update
{
al_flip_display();
}
- (ALLEGRO_DISPLAY*)OGK_allegroDisplay
{
return display;

View file

@ -1,7 +1,9 @@
ALLEGRO_MODULES = allegro-5.0 allegro_main-5.0 allegro_image-5.0
all:
@objfw-compile -o test --arc *.m \
-I../src -L../src -lobjgamekit \
`pkg-config --cflags --libs allegro-5.0 allegro_main-5.0`
`pkg-config --cflags --libs ${ALLEGRO_MODULES}`
clean:
@rm -f test *.o *~

View file

@ -22,11 +22,13 @@
#import "OGKDisplay.h"
#import "OGKEventQueue.h"
#import "OGKBitmap.h"
@interface TestMain: OFObject <OFApplicationDelegate, OGKEventQueueDelegate>
{
OGKDisplay *display;
OGKEventQueue *eventQueue;
OGKBitmap *bitmap;
BOOL running;
}
@end

View file

@ -21,6 +21,7 @@
#import "OGKDisplay.h"
#import "OGKEvent.h"
#import "OGKEventQueue.h"
#import "OGKBitmap.h"
#import "TestMain.h"
OF_APPLICATION_DELEGATE(TestMain)
@ -64,6 +65,18 @@ OF_APPLICATION_DELEGATE(TestMain)
event.wheel.x, event.wheel.y);
}
- (void)handleEvents
{
[eventQueue handleNextEvent];
}
- (void)draw
{
[OGKBitmap clearToColor: OGK_COLOR_BLACK];
[bitmap drawAtPosition: of_point(160, 120)];
[display update];
}
- (void)applicationDidFinishLaunching
{
display = [[OGKDisplay alloc] initWithSize: of_dimension(640, 480)
@ -76,9 +89,12 @@ OF_APPLICATION_DELEGATE(TestMain)
[eventQueue registerKeyboard];
[eventQueue registerMouse];
bitmap = [[OGKBitmap alloc] initWithFile: @"test.bmp"];
for (running = YES; running;) {
@autoreleasepool {
[eventQueue handleNextEvent];
[self handleEvents];
[self draw];
}
}
}

BIN
test/test.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 KiB