1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
|
#include "image.h"
#include "log.h"
#include <stdbool.h>
struct imv_image {
int width; /* width of the image overall */
int height; /* height of the image overall */
int num_chunks; /* number of chunks allocated */
SDL_Texture **chunks; /* array of chunks */
int num_chunks_wide; /* number of chunks per row of the image */
int num_chunks_tall; /* number of chunks per column of the image */
int chunk_width; /* chunk width */
int chunk_height; /* chunk height */
int last_chunk_width; /* width of rightmost chunk */
int last_chunk_height; /* height of bottommost chunk */
SDL_Renderer *renderer; /* SDL renderer to draw to */
};
struct imv_image *imv_image_create(SDL_Renderer *r)
{
struct imv_image *image = malloc(sizeof *image);
memset(image, 0, sizeof(struct imv_image));
image->renderer = r;
SDL_RendererInfo ri;
SDL_GetRendererInfo(r, &ri);
image->chunk_width = ri.max_texture_width != 0 ? ri.max_texture_width : 4096;
image->chunk_height = ri.max_texture_height != 0 ? ri.max_texture_height : 4096;
return image;
}
void imv_image_free(struct imv_image *image)
{
if(!image) {
return;
}
if(image->num_chunks > 0) {
for(int i = 0; i < image->num_chunks; ++i) {
SDL_DestroyTexture(image->chunks[i]);
}
free(image->chunks);
image->num_chunks = 0;
image->chunks = NULL;
image->renderer = NULL;
}
free(image);
}
static int convert_pixelformat(enum imv_pixelformat fmt)
{
if (fmt == IMV_ARGB) {
return SDL_PIXELFORMAT_ARGB8888;
} else if (fmt == IMV_ABGR) {
return SDL_PIXELFORMAT_ABGR8888;
} else {
imv_log(IMV_WARNING, "Unknown pixel format. Defaulting to ARGB\n");
return SDL_PIXELFORMAT_ARGB8888;
}
}
int imv_image_set_bitmap(struct imv_image *image, struct imv_bitmap *bmp)
{
image->width = bmp->width;
image->height = bmp->height;
/* figure out how many chunks are needed, and create them */
if(image->num_chunks > 0) {
for(int i = 0; i < image->num_chunks; ++i) {
SDL_DestroyTexture(image->chunks[i]);
}
free(image->chunks);
}
image->num_chunks_wide = 1 + image->width / image->chunk_width;
image->num_chunks_tall = 1 + image->height / image->chunk_height;
image->last_chunk_width = image->width % image->chunk_width;
image->last_chunk_height = image->height % image->chunk_height;
if(image->last_chunk_width == 0) {
image->last_chunk_width = image->chunk_width;
}
if(image->last_chunk_height == 0) {
image->last_chunk_height = image->chunk_height;
}
image->num_chunks = image->num_chunks_wide * image->num_chunks_tall;
image->chunks = malloc(sizeof(SDL_Texture*) * image->num_chunks);
const int format = convert_pixelformat(bmp->format);
size_t failed_at = -1;
for(int y = 0; y < image->num_chunks_tall; ++y) {
for(int x = 0; x < image->num_chunks_wide; ++x) {
const bool is_last_h_chunk = (x == image->num_chunks_wide - 1);
const bool is_last_v_chunk = (y == image->num_chunks_tall - 1);
const size_t index = x + y * image->num_chunks_wide;
image->chunks[index] =
SDL_CreateTexture(image->renderer,
format,
SDL_TEXTUREACCESS_STATIC,
is_last_h_chunk ? image->last_chunk_width : image->chunk_width,
is_last_v_chunk ? image->last_chunk_height : image->chunk_height);
SDL_SetTextureBlendMode(image->chunks[index],
SDL_BLENDMODE_BLEND);
if (image->chunks[index] == NULL) {
failed_at = x + y * image->num_chunks_wide;
break;
}
}
}
if (failed_at != (size_t)-1) {
for (size_t i = 0; i < failed_at; ++i) {
SDL_DestroyTexture(image->chunks[i]);
}
free(image->chunks);
image->num_chunks = 0;
image->chunks = NULL;
return 1;
}
for (int y = 0; y < image->num_chunks_tall; ++y) {
for (int x = 0; x < image->num_chunks_wide; ++x) {
ptrdiff_t offset = 4 * x * image->chunk_width +
y * 4 * image->width * image->chunk_height;
unsigned char* addr = bmp->data + offset;
SDL_UpdateTexture(image->chunks[x + y * image->num_chunks_wide],
NULL, addr, 4 * image->width);
}
}
return 0;
}
void imv_image_draw(struct imv_image *image, int bx, int by, double scale)
{
int offset_x = 0;
int offset_y = 0;
for(int y = 0; y < image->num_chunks_tall; ++y) {
for(int x = 0; x < image->num_chunks_wide; ++x) {
int img_w, img_h;
SDL_QueryTexture(image->chunks[x + y * image->num_chunks_wide], NULL, NULL,
&img_w, &img_h);
SDL_Rect view_area = {
bx + offset_x,
by + offset_y,
img_w * scale,
img_h * scale
};
SDL_RenderCopy(image->renderer,
image->chunks[x + y * image->num_chunks_wide], NULL, &view_area);
offset_x += image->chunk_width * scale;
}
offset_x = 0;
offset_y += image->chunk_height * scale;
}
}
int imv_image_width(const struct imv_image *image)
{
return image->width;
}
int imv_image_height(const struct imv_image *image)
{
return image->height;
}
/* vim:set ts=2 sts=2 sw=2 et: */
|