ObjGameKit  Diff

Differences From Artifact [ffce6aad92]:

  • File src/OGKBitmap.m — part of check-in [4719f25709] at 2012-08-26 12:17:52 on branch trunk — Only call al_*_destroy if Allegro is initialized.

    Otherwise, it would crash if al_uinstall_system() has already been
    called. Handling it this way eliminates the need to dealloc all objects
    before calling al_uninstall_system(), which meant that it was the users
    repsonsibility to call al_uninstall_system() after the user made sure
    all objects are deallocated. Now the user does not get to see any
    al_*() function. (user: js, size: 2427) [annotate] [blame] [check-ins using]

To Artifact [0e51e0ab6e]:


21
22
23
24
25
26
27






28
29
30
31
32
33
34
#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;








>
>
>
>
>
>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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 };

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
{
	if (self != [OGKBitmap class])
		return;

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
		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);







|
<







49
50
51
52
53
54
55
56

57
58
59
60
61
62
63
		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(ogk_color_to_allegro(color));

}

- initWithSize: (of_dimension_t)size
{
	self = [super init];

	bitmap = al_create_bitmap(size.width, size.height);
79
80
81
82
83
84
85
86

































87
88
89
90
91




























































92
93
94
95
96
}

- (void)dealloc
{
	if (bitmap != NULL && al_is_system_installed())
		al_destroy_bitmap(bitmap);
}


































- (void)drawAtPosition: (of_point_t)position
{
	al_draw_bitmap(bitmap, position.x, position.y, 0);
}





























































- (ALLEGRO_BITMAP*)OGK_allegroBitmap
{
	return bitmap;
}
@end








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}

- (void)dealloc
{
	if (bitmap != NULL && al_is_system_installed())
		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;
}
@end